Java禁用JTree / TransferHandler的剪切操作

时间:2011-07-26 01:15:16

标签: java export jtree transfer cut

我为我的JTree创建了一个自定义的TransferHandler,因此禁用了Copy(仅支持Move)和Paste(通过检查canImport中的support.isDrop())但是我无法弄清楚如何禁用Cut操作

看起来我必须在exportDone方法中做出决定,但到目前为止还没有运气。到目前为止,我的方法看起来像这样,但Drag和Cut都与Move动作相关联。

protected void exportDone(JComponent source, Transferable data, int action) {
    if(action == TransferHandler.MOVE) {
        try {
            List<TreePath> list = ((TestTreeList) data.getTransferData(TestTreeList.testTreeListFlavor)).getNodes();

            int count = list.size();
            for(int i = 0; i < count; i++) {
                TestTreeNode        node    = (TestTreeNode) list.get(i).getLastPathComponent();
                DefaultTreeModel    model   = (DefaultTreeModel) tree.getModel();
                model.removeNodeFromParent(node);
            }
            tree.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        } catch (UnsupportedFlavorException e) {
            Log.logException(e);
        } catch (IOException e) {
            Log.logException(e);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您可以从用户界面中删除CUT,COPY,PASTE,也可以从ActionMap中删除操作。

JTree tree = new JTree();
...
tree.getActionMap().put( "cut", null );
tree.getActionMap().put( "copy", null );
tree.getActionMap().put( "paste", null );

这可以阻止某人使用此树作为源进行复制,剪切或粘贴。

答案 1 :(得分:0)

JTree“WHEN_FOCUSED”InputMap,但不是第一个“代”:InputMaps可以有“父”(祖父母,曾祖父母等)InputMap(s)。

tree.getInputMap( JComponent.WHEN_FOCUSED ).getParent().remove( KeyStroke.getKeyStroke( KeyEvent.VK_X, KE.CTRL_DOWN_MASK ) )

请注意,为了避免让人头疼,您可能还想知道不同类型的InputMap之间存在“层次结构”(或更准确地说是“咨询”的顺序):首先咨询WHEN_FOCUSED,然后是WHEN_ANCESTOR,最后WHEN_IN_FOCUSED_WINDOW。如果将Ctrl-X放在JComponent的WHEN_ANCESTOR输入映射中(可能希望它会覆盖已存在的内容),如果同一JComponent的WHEN_FOCUSED InputMap中存在Ctrl-X,则会“重叠”。

通过一个简单的方法来探索给定组件的所有层次结构,可以获得很多启示,显示所有键绑定(至少在层次结构中上升:显示给定Window中的所有WHEN_IN_FOCUSED_WINDOW键击更多一点参与)。

我是一名Jython用户,但这应该是可以理解的:类似(但不可避免地不那么优雅)的实用程序可以用Java编写。

def show_ancestor_comps( comp, method ):
    height = 0
    while comp:
        if method:
            # this method can return True if it wants the ancestor exploration to stop
            if method( comp, height ):
                return
        height = height + 1
        comp = comp.parent

''' NB this can be combined with the previous one: show_ancestor_comps( comp, show_all_inputmap_gens_key_value_pairs ):
gives you all the InputMaps in the entire Window/Frame, etc. ''' 
def show_all_inputmap_gens_key_value_pairs( component, height ):
    height_indent = '  ' * height
    if not isinstance( component, javax.swing.JComponent ):
        logger.info( '%s# %s not a JComponent... no InputMaps' % ( height_indent, type( component ), ))
        return
    logger.info( '%s# InputMap stuff for component of type %s' % ( height_indent, type( component ), ))
    map_types = [ 'when focused', 'ancestor of focused', 'in focused window' ]
    for i in range( 3 ):
        im = component.getInputMap( i )
        logger.info( '%s# focus type %s' % ( height_indent, map_types[ i ], ))
        generation = 1
        while im: 
            gen_indent = '  ' * generation
            logger.info( '%s%s# generation %d InputMap %s' % ( height_indent, gen_indent, generation, im, )) 
            if im.keys():
                for keystroke in im.keys():
                    logger.info( '%s%s# keystroke %s value %s' % ( height_indent, gen_indent + '  ', keystroke, im.get( keystroke )))
            im = im.parent
            generation += 1

答案 2 :(得分:0)

    ActionMap actionMap = tree.getActionMap();
    actionMap.put( "cut", null );
    actionMap.put( "copy", null );
    actionMap.put( "paste", null );

    actionMap.getParent().put( "cut", null );
    actionMap.getParent().put( "copy", null );
    actionMap.getParent().put( "paste", null );