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

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

Introduction

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

Prototype

@Override
public ISelection getSelection() 

Source Link

Document

The AbstractTreeViewer implementation of this method returns the result as an ITreeSelection.

Usage

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

License:Open Source License

/**
 * Action done when a DoubleClick is received by the tree
 * /*from  w w w.  j  av a  2s  .  c  o m*/
 * @param viewer
 *        the viewer on which the DoubleClick action is applied
 */
protected void performDoubleClickAction(TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if (selection != null && selection.size() == 1) {
        Object selectedObject = selection.getFirstElement();
        if (selectedObject instanceof StereotypeRepresentation) {
            //we expand or collapse the node
            viewer.setExpandedState(selectedObject, !viewer.getExpandedState(selectedObject));
        } else if (selectedObject instanceof PropertyRepresentation) {
            Property prop = ((PropertyRepresentation) selectedObject).getUMLProperty();
            if (prop.isMultivalued()) {
                //we expand or collapse the node
                viewer.setExpandedState(selectedObject, !viewer.getExpandedState(selectedObject));
            } else {
                //we edit a value
                if (((PropertyRepresentation) selectedObject).getValues().isEmpty()) {
                    editMe((PropertyRepresentation) selectedObject, null);
                } else {
                    editMe((PropertyRepresentation) selectedObject,
                            ((PropertyRepresentation) selectedObject).getValues().get(0));
                }

                viewer.update(selectedObject, null);
            }
        } else if (selectedObject instanceof Value) {
            editMe(((Value) selectedObject).getPropertyRepresentation(), (Value) selectedObject);
            viewer.refresh(selectedObject);
        }
    }
}

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

License:Open Source License

/**
 * Action done when the user does a CTRL^N on the tree
 * /*ww w.ja v a 2  s  .  co m*/
 * @param e
 *        the KeyEvent
 * @param viewer
 *        the TreeViewer in which we add something
 */
protected void performCTRL_N_Action(KeyEvent e, TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if (selection != null && selection.size() == 1) {
        Object selectedObject = selection.getFirstElement();
        if (selectedObject != null) {
            if (selectedObject instanceof PropertyRepresentation) {
                Property property = ((PropertyRepresentation) selectedObject).getUMLProperty();
                if (property.isMultivalued()) {
                    //can we add a new value?
                    int upperMultiplicity = property.getUpper();
                    ArrayList<Value> values = ((PropertyRepresentation) selectedObject).getValues();
                    if (upperMultiplicity == -1 || values.size() < upperMultiplicity) {
                        editMe((PropertyRepresentation) selectedObject, null);
                    } else if (values.size() == 0) {
                        editMe((PropertyRepresentation) selectedObject, null);
                    }
                    viewer.refresh(selectedObject);
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * move the selected element in stereotypeViewer following shiftValue
 * /*from  ww w  .j  a v a  2s. c  om*/
 * 
 * 
 * @param shiftValue
 *        shiftValue can take the values :
 *        <ul>
 *        <li>{@link StereotypePostAction#MOVE_UP}</li>
 *        <li>{@link StereotypePostAction#MOVE_DOWN}</li>
 *        </ul>
 * @param viewer
 *        the TreeViewer in which we move elements
 * 
 */
protected void moveElement(String shiftValue, TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if (selection != null && selection.size() == 1) {
        Object selectedElement = selection.getFirstElement();
        if (selectedElement instanceof Value) {

            /** it's a property value */
            exchangeTwoValues(((Value) selectedElement).getPropertyRepresentation().getValues(),
                    selectedElement, shiftValue);

            viewer.refresh(((Value) selectedElement).getPropertyRepresentation());

        } else if (selectedElement instanceof StereotypeRepresentation) {
            /** it's a stereotype */
            exchangeTwoValues(config.getStereotypesRepresentations(), selectedElement, shiftValue);
            viewer.refresh();
        }
    }
}

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

License:Open Source License

/**
 * Action done when the add button is pressed
 * // w ww. j  a  v a2  s.  c  om
 * @param tree
 * 
 */
protected void performAddButtonPressed(TreeViewer tree) {
    TreeSelection selection = (TreeSelection) tree.getSelection();
    Object selectedElement = selection.getFirstElement();
    if (selection != null && selection.size() == 1 && !(selectedElement instanceof StereotypeRepresentation)) {
        if (selectedElement instanceof PropertyRepresentation) {
            // We want add a value to a multi-valued property or edit a
            // value to a mono-valued property
            PropertyRepresentation prop = (PropertyRepresentation) selectedElement;
            if (prop.getUpperMultiplicity() != 1) {
                editMe(prop, null);// we add a value
            } else if (!prop.getValues().isEmpty()) { // mono-valued
                // property and the
                // valu exists
                editMe(prop, prop.getValues().get(0));
            } else {// monovalued property, the value doesn't exist
                editMe(prop, null);
            }
            tree.refresh(selectedElement);
        }
        return;
    }

    // pops-up a dialog window where users can select the additionnal
    // stereotypes to apply
    // it needs the metaclass information...
    // for this, entry Proxy should be a Palette entry proxy
    if (!(entryProxy instanceof IPaletteAspectToolEntryProxy)) {
        return;
    }

    PaletteEntry entry = ((IPaletteAspectToolEntryProxy) entryProxy).getEntry();
    if (!(entry instanceof ToolEntry)) {
        return;
    }

    // this is a PaletteAspectToolEntryProxy
    // try to find the metaclass on which the element is working
    EClass metaClass = PaletteUtil.getToolMetaclass((ToolEntry) entry);

    if (metaClass == null) {
        Activator.log.error("Impossible to find metaclass", null); //$NON-NLS-1$
        return;
    }

    // retrieve the original stereotype list from the qualified names
    List<Stereotype> stereotypeList = config.getUMLStereotypes();// retrieveStereotypesFromQualifiedNames();

    // we have all applied profiles, the list of already applied
    // stereotypes, the metaclass of the created element...
    // just have to open a selection dialog
    final CheckedTreeSelectionDialog selectionDialog = new CheckedTreeSelectionDialog(
            DisplayUtils.getDisplay().getActiveShell(), editorLabelProvider,
            new ProfileContentProvider(metaClass));
    selectionDialog.setTitle(Messages.StereotypePostAction_StereotypeSelectionTitle);
    selectionDialog.setMessage(Messages.StereotypePostAction_StereotypeSelectionMessage);
    selectionDialog.setContainerMode(true);
    selectionDialog.setInitialElementSelections(stereotypeList);
    selectionDialog.setInput(config.getAppliedProfiles());
    selectionDialog.setExpandedElements(config.getAppliedProfiles().toArray());

    ViewerComparator comparator = new ViewerComparator();
    selectionDialog.setComparator(comparator);

    if (CheckedTreeSelectionDialog.OK == selectionDialog.open()) {
        // update the list of stereotype to apply
        Object[] result = selectionDialog.getResult();
        ArrayList<String> stereotypesToApply = new ArrayList<String>();
        for (int i = 0; i < result.length; i++) {
            if (result[i] instanceof Stereotype) {
                stereotypesToApply.add(((Stereotype) result[i]).getQualifiedName());

            }
        }
        config.setStereotypesRepresentations(stereotypesToApply);
        // it's not possible to use refresh or update here!
        updateStereotypeViewer();
    }

}

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

License:Open Source License

/**
 * Action done when the remove button is pressed
 * /*from  w w  w. j  a v a 2s .  co m*/
 * @param viewer
 *        the viewer on which the remove action is applied
 */
protected void performRemoveAction(TreeViewer viewer) {

    ITreeSelection selection = (ITreeSelection) viewer.getSelection();
    if (selection != null) {
        List<?> selectedElements = ((TreeSelection) selection).toList();
        if (sameLevelForFullSelection(selection)) {
            // all the element have the same type
            if (selection.getFirstElement() instanceof StereotypeRepresentation) {
                for (Object object : selectedElements) {
                    if (object instanceof StereotypeRepresentation) {
                        config.removeStereotype((StereotypeRepresentation) object);
                    }
                }

            } else if (selection.getFirstElement() instanceof PropertyRepresentation) {
                // we delete the value for mono-valued property
                for (Object object : selectedElements) {
                    if (object instanceof PropertyRepresentation) {
                        if (!((PropertyRepresentation) object).getUMLProperty().isMultivalued()) {
                            ((PropertyRepresentation) object).getValues().remove(0);
                        }
                    }
                }
            } else if (selection.getFirstElement() instanceof Value) {
                // we remove value from a multivalued property
                for (Object object : selectedElements) {
                    if (object instanceof Value) {
                        ((Value) object).getPropertyRepresentation().removeValue((Value) object);
                    }
                }
            }
            viewer.refresh();
        }
    }
}

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  . j  a 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.papyrus.uml.diagram.common.service.palette.StereotypePostAction.java

License:Open Source License

/**
 * Action done when a DoubleClick is received by the tree
 * /*from   ww w  .  j  a  v a  2 s.  co  m*/
 * @param viewer
 *        the viewer on which the DoubleClick action is applied
 */
protected void performDoubleClickAction(TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if (selection != null && selection.size() == 1) {
        Object selectedObject = selection.getFirstElement();
        if (selectedObject instanceof StereotypeRepresentation) {
            // we expand or collapse the node
            viewer.setExpandedState(selectedObject, !viewer.getExpandedState(selectedObject));
        } else if (selectedObject instanceof PropertyRepresentation) {
            Property prop = ((PropertyRepresentation) selectedObject).getUMLProperty();
            if (prop.isMultivalued()) {
                // we expand or collapse the node
                viewer.setExpandedState(selectedObject, !viewer.getExpandedState(selectedObject));
            } else {
                // we edit a value
                if (((PropertyRepresentation) selectedObject).getValues().isEmpty()) {
                    editMe((PropertyRepresentation) selectedObject, null);
                } else {
                    editMe((PropertyRepresentation) selectedObject,
                            ((PropertyRepresentation) selectedObject).getValues().get(0));
                }

                viewer.update(selectedObject, null);
            }
        } else if (selectedObject instanceof Value) {
            editMe(((Value) selectedObject).getPropertyRepresentation(), (Value) selectedObject);
            viewer.refresh(selectedObject);
        }
    }
}

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

License:Open Source License

/**
 * Action done when the user does a CTRL^N on the tree
 * //from   ww w  .  j a  va2  s  .  co  m
 * @param e
 *        the KeyEvent
 * @param viewer
 *        the TreeViewer in which we add something
 */
protected void performCTRL_N_Action(KeyEvent e, TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if (selection != null && selection.size() == 1) {
        Object selectedObject = selection.getFirstElement();
        if (selectedObject != null) {
            if (selectedObject instanceof PropertyRepresentation) {
                Property property = ((PropertyRepresentation) selectedObject).getUMLProperty();
                if (property.isMultivalued()) {
                    // can we add a new value?
                    int upperMultiplicity = property.getUpper();
                    ArrayList<Value> values = ((PropertyRepresentation) selectedObject).getValues();
                    if (upperMultiplicity == -1 || values.size() < upperMultiplicity) {
                        editMe((PropertyRepresentation) selectedObject, null);
                    } else if (values.size() == 0) {
                        editMe((PropertyRepresentation) selectedObject, null);
                    }
                    viewer.refresh(selectedObject);
                }
            }
        }
    }
}

From source file:org.eclipse.pde.emfforms.editor.actions.RemoveAction.java

License:Open Source License

/**
 * Find the next element to be selected in the tree once the delete operation has been performed
 * /*from   w ww  . j a  va2s  .  c o  m*/
 * @param treeViewer The {@link TreeViewer}
 * @return the next {@link EObject} to be selected in the tree or <code>null</code> if none found. A possible reason to that last case is because there is no remaining element to select in the tree.
 */
protected EObject computeElementToSelectAfterDeletion(TreeViewer treeViewer) {
    EObject nextObjToSelect = null;

    // Iterate over all the selected element to be removed to define the new element to select in the tree after the delete operation is performed. 
    List<EObject> selectedElements = (List<EObject>) ((StructuredSelection) treeViewer.getSelection()).toList();
    for (EObject eltToRemove : selectedElements) {
        EObject containerElt = eltToRemove.eContainer();
        EStructuralFeature eContainingFeature = eltToRemove.eContainingFeature();
        // Retrieve all the siblings elements and try to select the nearest one
        Object featureValue = containerElt.eGet(eContainingFeature);
        if (featureValue instanceof List<?>) {
            List<EObject> siblingElts = (List<EObject>) featureValue;
            EObject candidateElt = getSelectableElement(selectedElements, siblingElts, eltToRemove);
            if (candidateElt != null) {
                return candidateElt;
            }
        }
    }
    return nextObjToSelect;
}

From source file:org.eclipse.rcptt.ctx.filesystem.ui.FilesystemContextEditor.java

License:Open Source License

private void createTree(FormToolkit toolkit, Composite client) {
    final Tree tree = new Tree(client, SWT.BORDER | SWT.MULTI);
    GridDataFactory.fillDefaults().grab(true, true).span(1, 1).hint(100, 50).applyTo(tree);

    viewer = new TreeViewer(tree);
    viewer.setLabelProvider(new FilesystemContextLabelProvider());
    viewer.setContentProvider(new FilesystemContextContentProvider());
    viewer.setInput(getContextElement());
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
            Object[] res = sel.toArray();
            FSResource[] resources = new FSResource[res.length];
            for (int i = 0; i < res.length; i++)
                resources[i] = (FSResource) res[i];
            setSelection(resources);//from w  w  w  . j  av  a2 s.  co m
        }
    });
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        /**
         * Opens file or expands/collapses folder.
         */
        public void doubleClick(DoubleClickEvent event) {
            if (openFileAction.isEnabled()) {
                openFileAction.run();
            } else {
                TreeViewer viewer = (TreeViewer) event.getViewer();
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                Object selectedNode = selection.getFirstElement();
                viewer.setExpandedState(selectedNode, !viewer.getExpandedState(selectedNode));
            }
        }
    });

    // Setups renaming
    TreeViewerEditor.create(viewer, new DefaultTreeViewerEditStrategy(viewer), ColumnViewerEditor.DEFAULT);
    viewer.setCellEditors(new CellEditor[] { new TextCellEditor(tree) });
    viewer.setCellModifier(new ICellModifier() {
        public void modify(Object element, String property, Object value) {
            TreeItem item = (TreeItem) element;
            FSResource res = (FSResource) item.getData();
            EObject parent = res.eContainer();
            Set<String> allNames = getAllNames(parent);
            allNames.remove(res.getName());

            String newValue = (String) value;
            if (allNames.contains(newValue)) {
                return;
            }

            if (newValue != null && !newValue.isEmpty() && !newValue.equals(res.getName())) {
                res.setName(newValue);
            }
        }

        public Object getValue(Object element, String property) {
            return ((FSResource) element).getName();
        }

        public boolean canModify(Object element, String property) {
            return true;
        }
    });
    viewer.setColumnProperties(new String[] { "" });

    viewer.getControl().addKeyListener(new KeyListener() {

        public void keyReleased(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
            if ((SWT.DEL == e.character) && (0 == e.stateMask)) {
                if (removeAction.isEnabled()) {
                    removeAction.run();
                    e.doit = false;
                }
            }
        }
    });

    UIContentAdapter adapter = new UIContentAdapter() {
        @Override
        protected void changed(Notification notification) {
            if (viewer.getControl().isDisposed()) {
                return;
            }
            viewer.refresh();
        }
    };
    getContextElement().eAdapters().add(adapter);
}