Example usage for org.eclipse.jface.viewers TreeViewer editElement

List of usage examples for org.eclipse.jface.viewers TreeViewer editElement

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TreeViewer editElement.

Prototype

@Override
    public void editElement(Object element, int column) 

Source Link

Usage

From source file:org.eclipse.jface.snippets.viewers.Snippet043TreeViewerKeyboardEditing.java

License:Open Source License

public Snippet043TreeViewerKeyboardEditing(final Shell shell) {
    Button b = new Button(shell, SWT.PUSH);
    b.setText("Press Tab, or click here to edit first child element");
    final TreeViewer v = new TreeViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
    b.addSelectionListener(new SelectionAdapter() {

        @Override/*from   w w w  .j  av  a2s .  c o m*/
        public void widgetSelected(SelectionEvent e) {
            MyModel root = (MyModel) v.getInput();
            TreePath path = new TreePath(
                    new Object[] { root, root.child.get(1), root.child.get(1).child.get(0) });
            v.editElement(path, 0);
        }

    });

    TreeViewerColumn viewerColumn = new TreeViewerColumn(v, SWT.NONE);
    viewerColumn.getColumn().setWidth(300);
    viewerColumn.setLabelProvider(new ColumnLabelProvider());
    viewerColumn.setEditingSupport(new EditingSupport(v) {

        @Override
        protected void setValue(Object element, Object value) {
            ((MyModel) element).counter = Integer.parseInt(value.toString());
            getViewer().update(element, null);
        }

        @Override
        protected Object getValue(Object element) {
            return ((MyModel) element).counter + "";
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new TextCellEditor((Composite) getViewer().getControl());
        }

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }
    });

    TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(v,
            new FocusCellOwnerDrawHighlighter(v));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && event.keyCode == SWT.CR)
                    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
        }
    };

    TreeViewerEditor.create(v, focusCellManager, actSupport,
            ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                    | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    v.setContentProvider(new MyContentProvider());
    v.setInput(createModel());
}

From source file:org.eclipse.jface.snippets.viewers.Snippet055HideShowColumn.java

License:Open Source License

public Snippet055HideShowColumn(final Shell shell) {
    final TreeViewer v = new TreeViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
    v.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    v.getTree().setLinesVisible(true);/*ww w . j  a  va2 s.c  om*/
    v.getTree().setHeaderVisible(true);

    TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(v,
            new FocusCellOwnerDrawHighlighter(v));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && event.keyCode == SWT.CR)
                    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
        }
    };

    TreeViewerEditor.create(v, focusCellManager, actSupport,
            ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                    | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    final TextCellEditor textCellEditor = new TextCellEditor(v.getTree());

    createColumnFor(v, "Column 1", textCellEditor);
    final TreeViewerColumn column_2 = createColumnFor(v, "Column 2", textCellEditor);
    createColumnFor(v, "Column 3", textCellEditor);

    v.setContentProvider(new MyContentProvider());
    v.setInput(createModel());

    Button b = createButtonFor(shell, "Edit-Element");
    b.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            MyModel root = (MyModel) v.getInput();
            TreePath path = new TreePath(
                    new Object[] { root, root.child.get(1), root.child.get(1).child.get(0) });
            v.editElement(path, 0);
        }

    });
    b = createButtonFor(shell, "Hide/Show 2nd Column");
    b.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            int width = column_2.getColumn().getWidth();
            column_2.getColumn().setWidth(width == 0 ? 200 : 0);
        }
    });
}

From source file:org.eclipse.papyrus.diagram.common.service.palette.StereotypePostAction.java

License:Open Source License

/**
 * Action to do when the F2 key is pressed
 * <ul>/*from   w  w w. jav a2 s .  c o m*/
 * <li>open a CellEditor for the PrimitiveType and DataType</li>
 * <li>open a popup editor for others types</li>
 * </ul>
 * 
 * @param viewer
 *        the viewer on which the F2 action is applied
 */
protected void performF2Action(TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if ((selection != null) && (selection.size() == 1)) {
        Object objectToEdit = selection.getFirstElement();
        if (objectToEdit instanceof PropertyRepresentation) {
            Property prop = ((PropertyRepresentation) objectToEdit).getUMLProperty();
            if (prop.isMultivalued() == false) {
                if ((prop.getType() instanceof PrimitiveType) || (prop.getType() instanceof DataType)) {
                    viewer.editElement(prop, 0);
                } else {
                    //TODO
                }
            }
        } else if (objectToEdit instanceof Value) {
            PropertyRepresentation propRep = ((Value) objectToEdit).getPropertyRepresentation();
            Type type = propRep.getType();
            if (type != null) {
                if (type instanceof PrimitiveType || type instanceof DataType) {
                    viewer.editElement(objectToEdit, 0);

                    //the refresh is do by the cellEditor! (if the resfresh is do here, there is a bug!)
                    //viewer.refresh(property);
                } else {//popupEditor
                    editMe(propRep, (Value) objectToEdit);
                    viewer.refresh(propRep);
                }
            }
        }
    }

}

From source file:org.eclipse.papyrus.uml.diagram.common.service.palette.StereotypePostAction.java

License:Open Source License

/**
 * Action to do when the F2 key is pressed
 * <ul>//from w w  w  .ja  v  a 2s  .c o  m
 * <li>open a CellEditor for the PrimitiveType and DataType</li>
 * <li>open a popup editor for others types</li>
 * </ul>
 * 
 * @param viewer
 *        the viewer on which the F2 action is applied
 */
protected void performF2Action(TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if ((selection != null) && (selection.size() == 1)) {
        Object objectToEdit = selection.getFirstElement();
        if (objectToEdit instanceof PropertyRepresentation) {
            Property prop = ((PropertyRepresentation) objectToEdit).getUMLProperty();
            if (prop.isMultivalued() == false) {
                if ((prop.getType() instanceof PrimitiveType) || (prop.getType() instanceof DataType)) {
                    viewer.editElement(prop, 0);
                } else {
                    // TODO
                }
            }
        } else if (objectToEdit instanceof Value) {
            PropertyRepresentation propRep = ((Value) objectToEdit).getPropertyRepresentation();
            Type type = propRep.getType();
            if (type != null) {
                if (type instanceof PrimitiveType || type instanceof DataType) {
                    viewer.editElement(objectToEdit, 0);

                    // the refresh is do by the cellEditor! (if the resfresh
                    // is do here, there is a bug!)
                    // viewer.refresh(property);
                } else {// popupEditor
                    editMe(propRep, (Value) objectToEdit);
                    viewer.refresh(propRep);
                }
            }
        }
    }

}

From source file:org.eclipse.rcptt.tesla.internal.ui.player.SWTUIPlayer.java

License:Open Source License

private static void checkCell(Item item, int column, boolean state) {
    if (item instanceof TreeItem) {
        Tree tree = ((TreeItem) item).getParent();
        TreeViewer v = (TreeViewer) TeslaSWTAccess.getViewer(tree);
        if (v == null)
            throw new RuntimeException("No tree viewer.");
        if (column < 0 || column >= tree.getColumnCount())
            throw new RuntimeException("Invalid tree column index.");
        TreeColumn c = tree.getColumn(column);
        Object data = c.getData("org.eclipse.jface.columnViewer");
        if (!(data instanceof ViewerColumn))
            throw new RuntimeException("No tree column viewer.");
        ViewerColumn cv = (ViewerColumn) data;
        EditingSupport es = TeslaSWTAccess.getField(EditingSupport.class, cv, "editingSupport");
        if (es == null)
            throw new RuntimeException("No editing support for tree column viewer.");
        Object value = TeslaSWTAccess.callMethod(EditingSupport.class, es, "getValue",
                new Class[] { Object.class }, item.getData());
        if (!(value instanceof Boolean))
            throw new RuntimeException("Does not look like a checkbox tree column.");
        if ((Boolean) value == state)
            return;
        v.editElement(item.getData(), column);
    } else if (item instanceof TableItem) {
        Table table = ((TableItem) item).getParent();
        TableViewer v = (TableViewer) TeslaSWTAccess.getViewer(table);
        if (v == null)
            throw new RuntimeException("No table viewer.");
        if (column < 0 || column >= table.getColumnCount())
            throw new RuntimeException("Invalid table column index.");
        TableColumn c = table.getColumn(column);
        Object data = c.getData("org.eclipse.jface.columnViewer");
        if (!(data instanceof ViewerColumn))
            throw new RuntimeException("No table column viewer.");
        ViewerColumn cv = (ViewerColumn) data;
        EditingSupport es = TeslaSWTAccess.getField(EditingSupport.class, cv, "editingSupport");
        if (es == null)
            throw new RuntimeException("No editing support for table column viewer.");
        Object value = TeslaSWTAccess.callMethod(EditingSupport.class, es, "getValue",
                new Class[] { Object.class }, item.getData());
        if (!(value instanceof Boolean))
            throw new RuntimeException("Does not look like a checkbox table column.");
        if ((Boolean) value == state)
            return;
        v.editElement(item.getData(), column);
    }//from   www  . j  ava 2 s .  c o  m
}

From source file:org.eclipse.rcptt.tesla.internal.ui.processors.CellEditorSupport.java

License:Open Source License

protected Response handleActivateCellEditor(final ActivateCellEditor command) {
    final Element element = command.getElement();
    final SWTUIElement uiElement = getMapper().get(element);
    // Check for already activated
    if (uiElement != null) {
        final int column = command.getColumn();
        if (uiElement.isTree()) {
            final Tree tree = (Tree) unwrapWidget(uiElement);
            final String[] path = uiElement.getPathSelection();
            final Object current = Viewers.getTreeItem(tree, path);
            if (current != null) {
                getPlayer().exec("Run cell editor", new Runnable() {

                    public void run() {
                        final TreeViewer viewer = TeslaSWTAccess.getTreeViewer(tree);

                        if (current instanceof TreeItem && viewer != null) {
                            final TreeItem ti = (TreeItem) current;
                            if (!edit(uiElement, column, viewer, ti))
                                edit(uiElement, column, viewer, ti);

                        }// w ww.j  a  va 2  s .  co m
                        // Try to check for property pages
                        final Object vv = TeslaSWTAccess.getPropertySheet(tree);
                        final TeslaCellEditorManager instance = TeslaCellEditorManager.getInstance();
                        if (vv != null) {
                            final TreeItem[] selection = tree.getSelection();
                            if (selection.length == 1) {
                                instance.makeNewAsForced(true);
                                TeslaSWTAccess.callHandleSelect(vv, selection);
                                cellEditorActivations.put(uiElement.widget, instance.getLastActivated());
                                instance.makeNewAsForced(false);
                            }
                        }
                    }

                    private boolean edit(final SWTUIElement uiElement, final int column,
                            final TreeViewer viewer, final TreeItem ti) {
                        final TeslaCellEditorManager instance = TeslaCellEditorManager.getInstance();

                        if (viewer.isCellEditorActive()) {
                            CellEditor[] editors = viewer.getCellEditors();
                            if (editors != null && editors.length == 1) {
                                cellEditorActivations.put(uiElement.widget, editors[0]);
                                return true;
                            }
                            ColumnViewerEditor cve = viewer.getColumnViewerEditor();
                            if (cve != null) {
                                if (cve.getFocusCell() != null
                                        && cve.getFocusCell().getColumnIndex() == column) {
                                    CellEditor editor = TeslaSWTAccess.getCellEditorFromColumnEditor(cve);
                                    if (editor != null) {
                                        cellEditorActivations.put(uiElement.widget, editor);
                                        return true;
                                    }
                                } else {
                                    viewer.cancelEditing();
                                }
                            }
                            // viewer.cancelEditing();
                        }
                        try {
                            instance.makeNewAsForced(true);
                            Object toEdit = ti.getData();
                            if (toEdit == null)
                                // TODO: Need a way to activate cell
                                // editor from TableItem without data
                                toEdit = ti;
                            viewer.editElement(toEdit, column);
                            if (instance.getLastActivated() != null) {
                                cellEditorActivations.put(uiElement.widget, instance.getLastActivated());
                            } else {
                                return false;
                            }
                        } finally {
                            instance.makeNewAsForced(false);
                        }
                        return true;
                    }
                });
            }
        } else if (uiElement.isTable()) {
            final Table table = (Table) unwrapWidget(uiElement);
            final String[] path = uiElement.getPathSelection();
            String curPath = "";
            if (path != null && path.length > 0) {
                curPath = path[0];
            }
            final Object current = Viewers.getTableItem(table, curPath);
            if (current != null) {
                final TableViewer viewer = TeslaSWTAccess.getTableViewer(table);
                final TeslaCellEditorManager instance = TeslaCellEditorManager.getInstance();
                if (viewer != null) {
                    getPlayer().exec("Run cell editor", new Runnable() {

                        public void run() {
                            boolean done = false;
                            if (current instanceof TableItem) {
                                final TableItem ti = (TableItem) current;
                                if (ti.getData() != null) {
                                    done = true;
                                    if (viewer.isCellEditorActive()) {
                                        CellEditor[] editors = viewer.getCellEditors();
                                        if (editors != null && editors.length == 1) {
                                            cellEditorActivations.put(uiElement.widget, editors[0]);
                                            return;
                                        }
                                        ColumnViewerEditor cve = viewer.getColumnViewerEditor();
                                        if (cve != null) {
                                            if (cve.getFocusCell() != null
                                                    && cve.getFocusCell().getColumnIndex() == column) {
                                                CellEditor editor = TeslaSWTAccess
                                                        .getCellEditorFromColumnEditor(cve);
                                                if (editor != null) {
                                                    cellEditorActivations.put(uiElement.widget, editor);
                                                    return;
                                                }
                                            } else {
                                                viewer.cancelEditing();
                                            }
                                        }
                                    }
                                    instance.makeNewAsForced(true);
                                    try {
                                        viewer.editElement(ti.getData(), column);
                                        cellEditorActivations.put(uiElement.widget,
                                                instance.getLastActivated());
                                    } finally {
                                        instance.makeNewAsForced(false);
                                    }
                                }
                            }
                            if (!done) {
                                if (viewer.isCellEditorActive()) {
                                    viewer.cancelEditing();
                                }
                                try {
                                    instance.makeNewAsForced(true);
                                    if (current instanceof TableItem
                                            && ((TableItem) current).getData() != null) {
                                        viewer.editElement(((TableItem) current).getData(), column);
                                    } else {
                                        // TODO: Need a way to activate cell
                                        // editor from TableItem without
                                        // data
                                        viewer.editElement(current, column);
                                    }
                                    cellEditorActivations.put(uiElement.widget, instance.getLastActivated());
                                } finally {
                                    instance.makeNewAsForced(false);
                                }
                            }
                        }
                    });
                } else {
                    // Try to check for property pages
                    final Object vv = TeslaSWTAccess.getPropertySheet(table);
                    if (vv != null) {
                        final TableItem[] selection = table.getSelection();
                        if (selection.length == 1) {
                            instance.makeNewAsForced(true);
                            TeslaSWTAccess.callHandleSelect(vv, selection);
                            cellEditorActivations.put(uiElement.widget, instance.getLastActivated());
                            instance.makeNewAsForced(false);
                        }
                    }
                }
            }
        }
    }

    return ProtocolFactory.eINSTANCE.createBooleanResponse();
}

From source file:org.locationtech.udig.ui.FeatureTypeEditorTest.java

License:Open Source License

@Ignore
@Test/*from   www. j av  a2  s.c  om*/
public void testCellModifierGetValue() throws Exception {
    TreeViewer testingGetViewer = editor.testingGetViewer();
    testingGetViewer.editElement(featureType.getDescriptor(0), 0);
    testingGetViewer.cancelEditing();
    testingGetViewer.editElement(featureType.getDescriptor(0), 1);
    testingGetViewer.cancelEditing();

    List<LegalAttributeTypes> types = FeatureTypeEditor.testingGetTYPES();
    int geomindex = 0;
    int stringindex = 0;
    int i = 0;
    for (LegalAttributeTypes type : types) {
        if (type.getType() == String.class) {
            stringindex = i;
        } else if (type.getType() == Geometry.class) {
            geomindex = i;
        }
        i++;
    }

    ICellModifier cellModifier = testingGetViewer.getCellModifier();
    assertEquals("geom", cellModifier.getValue(featureType.getDescriptor(0), "0")); //$NON-NLS-1$ //$NON-NLS-2$
    assertEquals(geomindex, cellModifier.getValue(featureType.getDescriptor(0), "1")); //$NON-NLS-1$ 
    assertEquals("name", cellModifier.getValue(featureType.getDescriptor(1), "0")); //$NON-NLS-1$ //$NON-NLS-2$
    assertEquals(stringindex, cellModifier.getValue(featureType.getDescriptor(1), "1")); //$NON-NLS-1$ 
}