我已经为我的JTree创建了一个自定义TransferHandler,因此已经禁用了复制(仅支持移动)和粘贴(通过检查canImport中的support.isDrop()),但我不知道如何禁用Cut操作。
看起来我必须在exportDone方法中做出决定,但到目前为止还没有运气。到目前为止,我的方法看起来是这样的,但是拖动和剪切都与移动操作相关联。
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);
}
}
}
你也可以通过移除ActionMap中的Actions来禁用用户界面中的剪切、复制、粘贴。
JTree tree = new JTree();
...
tree.getActionMap().put( "cut", null );
tree.getActionMap().put( "copy", null );
tree.getActionMap().put( "paste", null );
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。如果你在一个JComponent的WHEN_ANCESTOR InputMap中放了一个Ctrl-X(也许希望它会覆盖已经存在的东西),如果在同一个JComponent的WHEN_FOCUSED InputMap中有一个Ctrl-X,这个Ctrl-X将会被"遮蔽"。
通过创建一个简单的方法来探索给定组件的所有层次结构,显示所有的键绑定(至少在层次结构中向上:显示给定窗口中所有的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
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 );