2011-07-26 4 views
1

JTree 용 사용자 정의 TransferHandler를 작성 했으므로 Move (Move 만 지원)와 Paste (canImport에서 support.isDrop()을 확인하여)를 사용할 수 없지만 ' 잘라 내기 작업을 비활성화하는 방법을 알아보십시오.Java JTree/TransferHandler의 잘라 내기 동작 사용 안 함

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); 
     } 
    } 
} 

답변

0

당신은 액션의에서 작업을 제거하여뿐만 아니라 잘라 내기, 복사, 사용자 인터페이스에서 붙여 넣기를 비활성화 할 수 있습니다.

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

누군가가이 트리를 원본으로 사용하여 복사, 잘라 내기 또는 붙여 넣기를 중지합니다.

+0

답장을 보내 주셔서 감사하지만 나를 위해 작동하지 않는 것 같습니다. 필자는 원본 트리와 TransferHandler가 저장 한 참조에서이 작업을 수행했습니다. 나는 또한 삽입물과 함께 importData 메소드로 노드를 제거하는 방법을 생각해 보았지만 같은 방법으로 처리하는 것이 어렵다는 것을 증명했다. 이것은 'cut'이 exportDone 메소드의 노드를 제거 할 가능성을 제거합니다. – testelemental

0

JTree "WHEN_FOCUSED"InputMap,하지만 최초의 "세대"는 아닙니다. InputMaps는 "부모"(조부모, 증조부모 등)의 InputMap을 가질 수 있습니다. 다음, 먼저 상담을 WHEN_FOCUSED :

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

NB 당신은 또한의 InputMap의 다른 유형 사이에서 "계층"(또는 "협의"의 더 정확하게 순서)가 있다는 것을 알고하는 것 같아서 긁적 머리를 많이 피하기 위해 WHEN_ANCESTOR 및 마지막으로 WHEN_IN_FOCUSED_WINDOW. JComponent의 WHEN_ANCESTOR InputMap에 Ctrl-X를 두는 경우 (이미 존재하는 것을 오버라이드 (override)하고 싶을 가능성이 있습니다), 같은 JComponent의 WHEN_FOCUSED InputMap에 Ctrl-X가있는 경우, 이것은 "일식 (eclips)"됩니다.

주어진 구성 요소에서 모든 계층 구조를 탐색하여 모든 키 바인딩을 보여주는 간단한 방법을 통해 많은 깨달음을 얻을 수 있습니다 (적어도 계층 구조가 올라간다 : 주어진 Window의 모든 WHEN_IN_FOCUSED_WINDOW 키 입력이 약간 더 관여하는).

저는 자이 썬 사용자입니다. 그러나 이것은 이해할 수 있어야합니다. 유사하지만 (필연적으로 덜 우아함) 유틸리티는 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 
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); 
+1

스택 오버플로에 대한 설명의 일부 단어가 인정됩니다. – mkl