List of usage examples for org.eclipse.jface.viewers TreeViewer getSelection
@Override
public ISelection getSelection()
AbstractTreeViewer
implementation of this method returns the result as an ITreeSelection
. From source file:org.eclipse.emf.cdo.explorer.ui.checkouts.wizards.CheckoutBranchPointPage.java
License:Open Source License
@Override protected boolean doValidate() throws ValidationProblem { TreeViewer branchViewer = branchPointComposite.getBranchViewer(); if (branchViewer.getInput() == null) { return false; }//from w w w.ja v a2 s . c om ISelection selection = branchViewer.getSelection(); if (selection == null || selection.isEmpty()) { return false; } if (timeStampError != null) { throw new ValidationProblem(timeStampError); } return true; }
From source file:org.eclipse.emf.ecp.ecoreeditor.internal.ui.MasterDetailRenderer.java
License:Open Source License
/** * Fill context menu./*from ww w .j a va 2 s . c o m*/ * Fills the context menu. Adds create actions for all possible children and a delete action. * * @param treeViewer the tree viewer * @param editingDomain the editing domain */ private void fillContextMenu(final TreeViewer treeViewer, final EditingDomain editingDomain) { final ChildrenDescriptorCollector childrenDescriptorCollector = new ChildrenDescriptorCollector(); final MenuManager menuMgr = new MenuManager(); menuMgr.setRemoveAllWhenShown(true); menuMgr.addMenuListener(new IMenuListener() { @Override public void menuAboutToShow(IMenuManager manager) { if (treeViewer.getSelection().isEmpty()) { return; } if (treeViewer.getSelection() instanceof IStructuredSelection) { final IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection(); if (selection.size() == 1 && selection.getFirstElement() instanceof EObject) { final EObject eObject = (EObject) selection.getFirstElement(); final EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(eObject); if (domain == null) { return; } final Collection<?> descriptors = childrenDescriptorCollector.getDescriptors(eObject); fillContextMenu(manager, descriptors, editingDomain, eObject); } manager.add(new Separator()); addDeleteActionToContextMenu(editingDomain, menuMgr, selection); } } }); final Menu menu = menuMgr.createContextMenu(treeViewer.getControl()); treeViewer.getControl().setMenu(menu); }
From source file:org.eclipse.emf.ecp.view.treemasterdetail.ui.swt.internal.TreeMasterDetailSWTRenderer.java
License:Open Source License
/** * @param treeViewer/*from w w w .j a v a 2 s .c o m*/ * @param editingDomain */ private void fillContextMenu(final TreeViewer treeViewer, final EditingDomain editingDomain) { final ChildrenDescriptorCollector childrenDescriptorCollector = new ChildrenDescriptorCollector(); final MenuManager menuMgr = new MenuManager(); menuMgr.setRemoveAllWhenShown(true); menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { if (treeViewer.getSelection().isEmpty()) { return; } final Object root = ((RootObject) treeViewer.getInput()).getRoot(); if (treeViewer.getSelection() instanceof IStructuredSelection) { final IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection(); if (selection.size() == 1) { final EObject eObject = (EObject) selection.getFirstElement(); final Collection<?> descriptors = childrenDescriptorCollector.getDescriptors(eObject); fillContextMenu(manager, descriptors, editingDomain, eObject); } if (!selection.toList().contains(root)) { manager.add(new Separator(GLOBAL_ADDITIONS)); addDeleteActionToContextMenu(editingDomain, manager, selection); } } } }); final Menu menu = menuMgr.createContextMenu(treeViewer.getControl()); treeViewer.getControl().setMenu(menu); }
From source file:org.eclipse.gef4.cloudio.CloudOptionsComposite.java
License:Open Source License
protected Group addFontButtons(final Composite parent) { Group buttons = new Group(parent, SWT.SHADOW_IN); buttons.setLayout(new GridLayout(2, false)); buttons.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); Label l = new Label(buttons, SWT.NONE); l.setText("Fonts"); GridData gd = new GridData(); gd.horizontalSpan = 2;// w ww .j a v a 2 s. c om l.setLayoutData(gd); final TreeViewer tv = new TreeViewer(buttons); Composite comp = new Composite(buttons, SWT.NONE); comp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true)); comp.setLayout(new RowLayout(SWT.VERTICAL)); tv.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); ListContentProvider cp = new ListContentProvider(); tv.setContentProvider(cp); tv.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { FontData fd = (FontData) element; return fd.getName(); } }); fonts.add(getFont().getFontData()[0]); tv.setInput(fonts); Button add = new Button(comp, SWT.FLAT); add.setImage(Activator.getDefault().getImageRegistry().get(Activator.ADD)); add.setToolTipText("Add font..."); add.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { FontDialog fd = new FontDialog(parent.getShell()); FontData fontData = fd.open(); if (fontData != null) { fonts.add(fontData); tv.setInput(fonts); updateFonts(); } } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); Button remove = new Button(comp, SWT.FLAT); remove.setToolTipText("Remove selected fonts"); remove.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { IStructuredSelection selection = (IStructuredSelection) tv.getSelection(); fonts.removeAll(selection.toList()); tv.setInput(fonts); updateFonts(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); remove.setImage(Activator.getDefault().getImageRegistry().get(Activator.REMOVE)); return buttons; }
From source file:org.eclipse.gef4.cloudio.CloudOptionsComposite.java
License:Open Source License
protected Group addColorButtons(final Composite parent) { Group buttons = new Group(parent, SWT.SHADOW_IN); buttons.setLayout(new GridLayout(2, false)); buttons.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); Label l = new Label(buttons, SWT.NONE); l.setText("Colors"); GridData gd = new GridData(); gd.horizontalSpan = 2;//from w w w. jav a2 s .co m l.setLayoutData(gd); final TreeViewer tv = new TreeViewer(buttons); Composite comp = new Composite(buttons, SWT.NONE); comp.setLayout(new RowLayout(SWT.VERTICAL)); comp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true)); tv.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); ListContentProvider cp = new ListContentProvider(); tv.setContentProvider(cp); tv.setLabelProvider(new ColumnLabelProvider() { private Map<Object, Image> images = new HashMap<Object, Image>(); @Override public Image getImage(Object element) { Image image = images.get(element); if (image == null) { RGB rgb = (RGB) element; image = createImageFromColor(rgb, 24); images.put(element, image); } return image; } @Override public void dispose() { Collection<Image> images = this.images.values(); for (Image image : images) { image.dispose(); } this.images.clear(); } }); initColors(); tv.setInput(colors); Button add = new Button(comp, SWT.FLAT); add.setImage(Activator.getDefault().getImageRegistry().get(Activator.ADD)); add.setToolTipText("Add color..."); add.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ColorDialog cd = new ColorDialog(parent.getShell()); RGB color = cd.open(); if (color != null) { colors.add(color); tv.setInput(colors); updateColors(); } } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); Button remove = new Button(comp, SWT.FLAT); remove.setToolTipText("Remove selected colors"); remove.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { IStructuredSelection selection = (IStructuredSelection) tv.getSelection(); colors.removeAll(selection.toList()); tv.setInput(colors); updateColors(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); remove.setImage(Activator.getDefault().getImageRegistry().get(Activator.REMOVE)); Button toggle = new Button(comp, SWT.FLAT); toggle.setToolTipText("Toggle Colors"); toggle.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { nextColors(); tv.setInput(colors); updateColors(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); toggle.setImage(Activator.getDefault().getImageRegistry().get(Activator.TOGGLE_COLORS)); comp = new Composite(buttons, SWT.NONE); gd = new GridData(SWT.FILL, SWT.FILL, true, false); gd.horizontalSpan = 2; comp.setLayout(new GridLayout(2, true)); comp.setLayoutData(gd); final Button bg = new Button(comp, SWT.FLAT); bg.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); bg.setText("Background"); bg.setImage(createImageFromColor(viewer.getCloud().getBackground().getRGB(), 16)); bg.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ColorDialog cd = new ColorDialog(parent.getShell()); RGB color = cd.open(); if (color == null) return; Color old = viewer.getCloud().getBackground(); Color c = new Color(Display.getDefault(), color); viewer.getCloud().setBackground(c); old.dispose(); viewer.getCloud().redrawTextLayerImage(); Image oldImage = bg.getImage(); Image newImage = createImageFromColor(color, 16); bg.setImage(newImage); oldImage.dispose(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); final Button sel = new Button(comp, SWT.FLAT); sel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); sel.setText("Selection"); sel.setImage(createImageFromColor(viewer.getCloud().getSelectionColor().getRGB(), 16)); sel.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ColorDialog cd = new ColorDialog(parent.getShell()); RGB color = cd.open(); if (color == null) return; Color old = viewer.getCloud().getSelectionColor(); Color c = new Color(Display.getDefault(), color); viewer.getCloud().setSelectionColor(c); old.dispose(); viewer.getCloud().redrawTextLayerImage(); Image oldImage = sel.getImage(); Image newImage = createImageFromColor(color, 16); sel.setImage(newImage); oldImage.dispose(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return buttons; }
From source file:org.eclipse.gef4.cloudio.internal.ui.view.CloudOptionsComposite.java
License:Open Source License
protected Group addFontButtons(final Composite parent) { Group buttons = new Group(parent, SWT.SHADOW_IN); buttons.setLayout(new GridLayout(2, false)); buttons.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); Label l = new Label(buttons, SWT.NONE); l.setText("Fonts"); GridData gd = new GridData(); gd.horizontalSpan = 2;//from w ww . j av a 2 s . co m l.setLayoutData(gd); final TreeViewer tv = new TreeViewer(buttons); Composite comp = new Composite(buttons, SWT.NONE); comp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true)); comp.setLayout(new RowLayout(SWT.VERTICAL)); tv.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); ListContentProvider cp = new ListContentProvider(); tv.setContentProvider(cp); tv.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { FontData fd = (FontData) element; return fd.getName(); } }); fonts.add(getFont().getFontData()[0]); tv.setInput(fonts); Button add = new Button(comp, SWT.FLAT); add.setImage(CloudioUiBundle.getDefault().getImageRegistry().get(CloudioUiBundle.ADD)); add.setToolTipText("Add font..."); add.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { FontDialog fd = new FontDialog(parent.getShell()); FontData fontData = fd.open(); if (fontData != null) { fonts.add(fontData); tv.setInput(fonts); updateFonts(); } } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); Button remove = new Button(comp, SWT.FLAT); remove.setToolTipText("Remove selected fonts"); remove.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { IStructuredSelection selection = (IStructuredSelection) tv.getSelection(); fonts.removeAll(selection.toList()); tv.setInput(fonts); updateFonts(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); remove.setImage(CloudioUiBundle.getDefault().getImageRegistry().get(CloudioUiBundle.REMOVE)); return buttons; }
From source file:org.eclipse.gef4.cloudio.internal.ui.view.CloudOptionsComposite.java
License:Open Source License
protected Group addColorButtons(final Composite parent) { Group buttons = new Group(parent, SWT.SHADOW_IN); buttons.setLayout(new GridLayout(2, false)); buttons.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); Label l = new Label(buttons, SWT.NONE); l.setText("Colors"); GridData gd = new GridData(); gd.horizontalSpan = 2;/*from ww w . ja va 2 s. c o m*/ l.setLayoutData(gd); final TreeViewer tv = new TreeViewer(buttons); Composite comp = new Composite(buttons, SWT.NONE); comp.setLayout(new RowLayout(SWT.VERTICAL)); comp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true)); tv.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); ListContentProvider cp = new ListContentProvider(); tv.setContentProvider(cp); tv.setLabelProvider(new ColumnLabelProvider() { private Map<Object, Image> images = new HashMap<>(); @Override public Image getImage(Object element) { Image image = images.get(element); if (image == null) { RGB rgb = (RGB) element; image = createImageFromColor(rgb, 24); images.put(element, image); } return image; } @Override public void dispose() { Collection<Image> images = this.images.values(); for (Image image : images) { image.dispose(); } this.images.clear(); } }); initColors(); tv.setInput(colors); Button add = new Button(comp, SWT.FLAT); add.setImage(CloudioUiBundle.getDefault().getImageRegistry().get(CloudioUiBundle.ADD)); add.setToolTipText("Add color..."); add.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ColorDialog cd = new ColorDialog(parent.getShell()); RGB color = cd.open(); if (color != null) { colors.add(color); tv.setInput(colors); updateColors(); } } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); Button remove = new Button(comp, SWT.FLAT); remove.setToolTipText("Remove selected colors"); remove.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { IStructuredSelection selection = (IStructuredSelection) tv.getSelection(); colors.removeAll(selection.toList()); tv.setInput(colors); updateColors(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); remove.setImage(CloudioUiBundle.getDefault().getImageRegistry().get(CloudioUiBundle.REMOVE)); Button toggle = new Button(comp, SWT.FLAT); toggle.setToolTipText("Toggle Colors"); toggle.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { nextColors(); tv.setInput(colors); updateColors(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); toggle.setImage(CloudioUiBundle.getDefault().getImageRegistry().get(CloudioUiBundle.TOGGLE_COLORS)); comp = new Composite(buttons, SWT.NONE); gd = new GridData(SWT.FILL, SWT.FILL, true, false); gd.horizontalSpan = 2; comp.setLayout(new GridLayout(2, true)); comp.setLayoutData(gd); final Button bg = new Button(comp, SWT.FLAT); bg.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); bg.setText("Background"); bg.setImage(createImageFromColor(viewer.getCloud().getBackground().getRGB(), 16)); bg.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ColorDialog cd = new ColorDialog(parent.getShell()); RGB color = cd.open(); if (color == null) return; Color old = viewer.getCloud().getBackground(); Color c = new Color(Display.getDefault(), color); viewer.getCloud().setBackground(c); old.dispose(); viewer.getCloud().redrawTextLayerImage(); Image oldImage = bg.getImage(); Image newImage = createImageFromColor(color, 16); bg.setImage(newImage); oldImage.dispose(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); final Button sel = new Button(comp, SWT.FLAT); sel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); sel.setText("Selection"); sel.setImage(createImageFromColor(viewer.getCloud().getSelectionColor().getRGB(), 16)); sel.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ColorDialog cd = new ColorDialog(parent.getShell()); RGB color = cd.open(); if (color == null) return; Color old = viewer.getCloud().getSelectionColor(); Color c = new Color(Display.getDefault(), color); viewer.getCloud().setSelectionColor(c); old.dispose(); viewer.getCloud().redrawTextLayerImage(); Image oldImage = sel.getImage(); Image newImage = createImageFromColor(color, 16); sel.setImage(newImage); oldImage.dispose(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return buttons; }
From source file:org.eclipse.jst.jsp.ui.tests.TaglibIndexDeltaTraceView.java
License:Open Source License
void showSelectionDetail(final Composite composite) { IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection(); if (!selection.isEmpty()) { final TaglibIndexDelta selectedDelta = (TaglibIndexDelta) selection.getFirstElement(); final ITableLabelProvider tableLabelProvider = ((ITableLabelProvider) fViewer.getLabelProvider()); String columnText = tableLabelProvider.getColumnText(selectedDelta, 1); columnText = columnText + ":" + tableLabelProvider.getColumnText(selectedDelta, 2); new Dialog(composite.getShell()) { public void create() { setShellStyle(getShellStyle() | SWT.RESIZE); super.create(); }/* w w w . j a va 2s .c o m*/ protected Control createDialogArea(final Composite parent) { final Composite inner = new Composite(parent, SWT.NONE); inner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); final Sash sash = new Sash(inner, SWT.HORIZONTAL); final TreeViewer treeViewer = new TreeViewer(inner); treeViewer.setContentProvider(new ITreeContentProvider() { public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } public void dispose() { } public Object[] getElements(Object inputElement) { return getChildren(inputElement); } public boolean hasChildren(Object element) { return getChildren(element).length > 0; } public Object getParent(Object element) { return selectedDelta; } public Object[] getChildren(Object parentElement) { if (parentElement instanceof TaglibIndexDelta) { TaglibIndexDelta taglibIndexDelta = ((TaglibIndexDelta) parentElement); if (taglibIndexDelta.getTrigger() != null) { List combined = new ArrayList(); combined.add(taglibIndexDelta.getTrigger()); combined.addAll(Arrays.asList(taglibIndexDelta.getAffectedChildren())); return combined.toArray(); } return taglibIndexDelta.getAffectedChildren(); } return new Object[0]; } }); treeViewer.setLabelProvider(new LabelProvider() { public String getText(Object element) { if (element instanceof ITaglibIndexDelta) { ITaglibIndexDelta taglibIndexDelta = ((ITaglibIndexDelta) element); if (taglibIndexDelta.getTaglibRecord() != null) return taglibIndexDelta.toString(); String text = tableLabelProvider.getColumnText(selectedDelta, 1); text = text + ":" + tableLabelProvider.getColumnText(selectedDelta, 2); return text; } return StringUtils.firstLineOf(super.getText(element)); } }); treeViewer.setInput(selectedDelta); final Text text = new Text(inner, SWT.MULTI); treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection2 = (IStructuredSelection) treeViewer.getSelection(); if (!selection2.isEmpty()) text.setText("" + (selection2).getFirstElement()); else text.setText(""); } }); final FormLayout form = new FormLayout(); inner.setLayout(form); FormData firstData = new FormData(); firstData.top = new FormAttachment(0, 0); firstData.bottom = new FormAttachment(sash, 2); firstData.left = new FormAttachment(0, 0); firstData.right = new FormAttachment(100, 0); treeViewer.getControl().setLayoutData(firstData); FormData secondData = new FormData(); secondData.top = new FormAttachment(sash, 2); secondData.left = new FormAttachment(0, 0); secondData.right = new FormAttachment(100, 0); secondData.bottom = new FormAttachment(100, 0); text.setLayoutData(secondData); final FormData sashData = new FormData(); sashData.top = new FormAttachment(60, 0); sashData.left = new FormAttachment(0, 0); sashData.right = new FormAttachment(100, 0); sash.setLayoutData(sashData); sash.addListener(SWT.Selection, new org.eclipse.swt.widgets.Listener() { public void handleEvent(Event e) { sashData.top = new FormAttachment(0, e.y); inner.layout(); } }); return sash; } }.open(); } }
From source file:org.eclipse.jubula.client.ui.controllers.TreeViewContainerGUIController.java
License:Open Source License
/** * expand a subtree/*from w w w . ja v a 2 s . c o m*/ * * @param tv * the tree viewer to use; may be <code>null</code> */ public static void expandSubTree(TreeViewer tv) { if (tv == null) { return; } if (tv.getSelection() instanceof IStructuredSelection) { IStructuredSelection selection = (IStructuredSelection) tv.getSelection(); for (Object obj : selection.toArray()) { tv.expandToLevel(obj, 2); } } }
From source file:org.eclipse.jubula.client.ui.handlers.AbstractGoToTestResultErrorHandler.java
License:Open Source License
/** * {@inheritDoc}//w ww.j a v a 2 s .c om */ public final Object execute(ExecutionEvent event) throws ExecutionException { List<IWorkbenchPart> listOfPossibleParts = new LinkedList<IWorkbenchPart>(); listOfPossibleParts.add(HandlerUtil.getActivePart(event)); listOfPossibleParts.add(HandlerUtil.getActiveEditor(event)); TreeViewer viewer = handleActiveWorkbenchParts(listOfPossibleParts); IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); ITreeContentProvider contentProvider = (ITreeContentProvider) viewer.getContentProvider(); TestResultNode startingNode = null; if (selection.getFirstElement() instanceof TestResultNode) { startingNode = (TestResultNode) selection.getFirstElement(); } else { Object[] rootElements = contentProvider.getElements(viewer.getInput()); for (Object element : rootElements) { if (element instanceof TestResultNode) { startingNode = (TestResultNode) element; break; } } } TestResultNode targetNode = null; TreeViewerIterator iter = new TreeViewerIterator(viewer, startingNode, isForwardIteration()); while (iter.hasNext() && targetNode == null) { Object nextElement = iter.next(); if (nextElement instanceof TestResultNode) { TestResultNode node = (TestResultNode) nextElement; if (isErrorNode(node)) { targetNode = node; } } } if (targetNode != null) { viewer.reveal(targetNode); viewer.setSelection(new StructuredSelection(targetNode)); } return null; }