2014-11-09 2 views
1

TextCellEditor를 확장하고 org.eclipse.jface.fieldassist를 사용하여 콘텐츠 제안을 시작하는 코드 블록을 실행하려고합니다. 코드가 잘 실행되고 있지만 콘텐츠 제안 팝업이 실행되지 않습니다. 또한 셀을 편집 할 수 없습니다. 여기서 뭐가 잘못 됐는지 알려주시겠습니까?SWT/JFace TextCellEditor 필드 지원이 작동하지 않습니다.

public class Snippet060TextCellEditorWithContentProposal { 
private static class Color { 
    public String name; 

    public Color(String name) { 
     this.name = name; 
    } 

    public String toString() { 
     return name; 
    } 
} 

public static class TextCellEditorWithContentProposal extends 
     TextCellEditor { 

    private ContentProposalAdapter contentProposalAdapter; 
    private boolean popupOpen = false; // true, iff popup is currently open 

    public TextCellEditorWithContentProposal(Composite parent, 
      IContentProposalProvider contentProposalProvider, 
      KeyStroke keyStroke, char[] autoActivationCharacters) { 
     super(parent); 

     enableContentProposal(contentProposalProvider, keyStroke, 
       autoActivationCharacters); 
    } 

    private void enableContentProposal(
      IContentProposalProvider contentProposalProvider, 
      KeyStroke keyStroke, char[] autoActivationCharacters) { 
     contentProposalAdapter = new ContentProposalAdapter(text, 
       new TextContentAdapter(), contentProposalProvider, 
       keyStroke, autoActivationCharacters); 

     // Listen for popup open/close events to be able to handle focus 
     // events correctly 
     contentProposalAdapter 
       .addContentProposalListener(new IContentProposalListener2() { 

        public void proposalPopupClosed(
          ContentProposalAdapter adapter) { 
         popupOpen = false; 
        } 

        public void proposalPopupOpened(
          ContentProposalAdapter adapter) { 
         popupOpen = true; 
        } 
       }); 
    } 

    /** 
    * Return the {@link ContentProposalAdapter} of this cell editor. 
    * 
    * @return the {@link ContentProposalAdapter} 
    */ 
    public ContentProposalAdapter getContentProposalAdapter() { 
     return contentProposalAdapter; 
    } 

    protected void focusLost() { 
     if (!popupOpen) { 
      // Focus lost deactivates the cell editor. 
      // This must not happen if focus lost was caused by activating 
      // the completion proposal popup. 
      super.focusLost(); 
     } 
    } 

    protected boolean dependsOnExternalFocusListener() { 
     // Always return false; 
     // Otherwise, the ColumnViewerEditor will install an additional 
     // focus listener 
     // that cancels cell editing on focus lost, even if focus gets lost 
     // due to 
     // activation of the completion proposal popup. See also bug 58777. 
     return false; 
    } 
} 

private static class ColorNameEditingSupport extends EditingSupport { 
    private TextCellEditorWithContentProposal cellEditor; 

    public ColorNameEditingSupport(TableViewer viewer) { 
     super(viewer); 

     IContentProposalProvider contentProposalProvider = new SimpleContentProposalProvider(
       new String[] { "red", "green", "blue" }); 
     cellEditor = new TextCellEditorWithContentProposal(
       viewer.getTable(), contentProposalProvider, null, null); 
    } 

    protected boolean canEdit(Object element) { 
     return (element instanceof Color); 
    } 

    protected CellEditor getCellEditor(Object element) { 
     return cellEditor; 
    } 

    protected Object getValue(Object element) { 
     return ((Color) element).name; 
    } 

    protected void setValue(Object element, Object value) { 
     ((Color) element).name = value.toString(); 
     getViewer().update(element, null); 
    } 

} 

public Snippet060TextCellEditorWithContentProposal(Shell shell) { 
    final TableViewer viewer = new TableViewer(shell, SWT.BORDER 
      | SWT.FULL_SELECTION); 
    final Table table = viewer.getTable(); 
    table.setLinesVisible(true); 
    table.setHeaderVisible(true); 

    final TableViewerColumn colorColumn = new TableViewerColumn(viewer, 
      SWT.LEFT); 
    colorColumn.getColumn().setText("Color name"); 
    colorColumn.getColumn().setWidth(200); 
    colorColumn.setLabelProvider(new ColumnLabelProvider()); 
    colorColumn.setEditingSupport(new ColorNameEditingSupport(viewer)); 

    viewer.setContentProvider(new ArrayContentProvider()); 

    ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(
      viewer) { 
     protected boolean isEditorActivationEvent(
       ColumnViewerEditorActivationEvent event) { 
      return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL 
        || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION 
        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC 
        || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == KeyLookupFactory 
          .getDefault().formalKeyLookup(
            IKeyLookup.ENTER_NAME)); 
     } 
    }; 
    activationSupport.setEnableEditorActivationWithKeyboard(true); 

    /* 
    * Without focus highlighter, keyboard events will not be delivered to 
    * ColumnViewerEditorActivationStragety#isEditorActivationEvent(...) 
    * (see above) 
    */ 
    FocusCellHighlighter focusCellHighlighter = new FocusCellOwnerDrawHighlighter(
      viewer); 
    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(
      viewer, focusCellHighlighter); 

    TableViewerEditor.create(viewer, focusCellManager, activationSupport, 
      ColumnViewerEditor.TABBING_VERTICAL 
        | ColumnViewerEditor.KEYBOARD_ACTIVATION); 

    viewer.setInput(createModel()); 
} 

private Color[] createModel() { 
    return new Color[] { new Color("red"), new Color("green") }; 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    Display display = new Display(); 

    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 
    new Snippet060TextCellEditorWithContentProposal(shell); 
    shell.open(); 

    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 

    display.dispose(); 
} 
} 
+0

코드를 표시 할 때까지는 알려 드릴 수 없습니다. –

+0

예. 확실히 .. 편집 중이었습니다. – Amrit

답변

0

예상대로 작동하며 마우스가 더블 클릭 동작을 위해 셀이 등록되었음을 인식하지 못했습니다. 더블 클릭하면 팝업이 나타납니다.