List of usage examples for org.eclipse.jface.viewers IStructuredSelection getFirstElement
public Object getFirstElement();
null
if the selection is empty. From source file:coloredide.astview.ASTView.java
License:Open Source License
public void createPartControl(Composite parent) { fSash = new SashForm(parent, SWT.VERTICAL | SWT.SMOOTH); fViewer = new TreeViewer(fSash, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); fDrillDownAdapter = new DrillDownAdapter(fViewer); fViewer.setContentProvider(new ASTViewContentProvider()); fASTLabelProvider = new ASTViewLabelProvider(this); fViewer.setLabelProvider(fASTLabelProvider); fViewer.addSelectionChangedListener(fSuperListener); fViewer.addDoubleClickListener(fSuperListener); fViewer.addFilter(fNonRelevantFilter); ViewForm trayForm = new ViewForm(fSash, SWT.NONE); Label label = new Label(trayForm, SWT.NONE); label.setText(" Comparison Tray (* = selection in the upper tree):"); //$NON-NLS-1$ trayForm.setTopLeft(label);/*from www. jav a2s . c o m*/ fTray = new TreeViewer(trayForm, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); trayForm.setContent(fTray.getTree()); fTrayRoots = new ArrayList(); fTray.setContentProvider(new TrayContentProvider()); final TrayLabelProvider trayLabelProvider = new TrayLabelProvider(); fTray.setLabelProvider(trayLabelProvider); fTray.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS); fTrayUpdater = new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection viewerSelection = (IStructuredSelection) fViewer.getSelection(); if (viewerSelection.size() == 1) { if (DynamicAttributeProperty.unwrapAttribute(viewerSelection.getFirstElement()) != null) { trayLabelProvider.setViewerElement(viewerSelection.getFirstElement()); return; } } trayLabelProvider.setViewerElement(null); } }; fTray.addPostSelectionChangedListener(fTrayUpdater); fViewer.addPostSelectionChangedListener(fTrayUpdater); fTray.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { performTrayDoubleClick(); } }); fTray.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); boolean deleteEnabled = false; if (selection.size() == 1 && selection.getFirstElement() instanceof Binding) deleteEnabled = fTray.getTree().isFocusControl(); fDeleteAction.setEnabled(deleteEnabled); } }); fTray.getTree().addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { IStructuredSelection selection = (IStructuredSelection) fTray.getSelection(); boolean deleteEnabled = false; if (selection.size() == 1 && selection.getFirstElement() instanceof Binding) deleteEnabled = true; fDeleteAction.setEnabled(deleteEnabled); } public void focusLost(FocusEvent e) { fDeleteAction.setEnabled(false); } }); makeActions(); hookContextMenu(); hookTrayContextMenu(); contributeToActionBars(); getSite().setSelectionProvider(new ASTViewSelectionProvider()); try { IEditorPart part = EditorUtility.getActiveEditor(); if (part instanceof ITextEditor) { setInput((ITextEditor) part); } } catch (CoreException e) { // ignore } if (fColoredJavaSourceFile == null) { clearView(); } else { setASTUptoDate(fColoredJavaSourceFile != null); } }
From source file:coloredide.astview.ASTView.java
License:Open Source License
protected void handleSelectionChanged(ISelection selection) { fExpandAction.setEnabled(!selection.isEmpty()); fCollapseAction.setEnabled(!selection.isEmpty()); fCopyAction.setEnabled(!selection.isEmpty()); boolean addEnabled = false; IStructuredSelection structuredSelection = (IStructuredSelection) selection; if (structuredSelection.size() == 1 && fViewer.getTree().isFocusControl()) { Object first = structuredSelection.getFirstElement(); Object unwrapped = DynamicAttributeProperty.unwrapAttribute(first); addEnabled = unwrapped != null;/*from w w w . j ava2s. c o m*/ } fAddToTrayAction.setEnabled(addEnabled); }
From source file:coloredide.astview.ASTView.java
License:Open Source License
protected void performTrayDoubleClick() { IStructuredSelection selection = (IStructuredSelection) fTray.getSelection(); if (selection.size() != 1) return;//w ww .j av a2s.c o m Object obj = selection.getFirstElement(); if (obj instanceof ExceptionAttribute) { RuntimeException exception = ((ExceptionAttribute) obj).getException(); if (exception != null) { String label = ((ExceptionAttribute) obj).getLabel(); showAndLogError("An error occurred while calculating an AST View Label:\n" + label, exception); //$NON-NLS-1$ return; } } if (obj instanceof Binding) { Binding binding = (Binding) obj; fViewer.setSelection(new StructuredSelection(binding), true); } }
From source file:coloredide.astview.ASTView.java
License:Open Source License
protected void performDelete() { IStructuredSelection selection = (IStructuredSelection) fTray.getSelection(); if (selection.size() != 1) return;/*from www.j av a 2 s.c o m*/ Object obj = selection.getFirstElement(); if (obj instanceof Binding) { int index = fTrayRoots.indexOf(obj); if (index != -1) { fTrayRoots.remove(index); fTray.setInput(fTrayRoots); int newSize = fTrayRoots.size(); if (newSize == 0) return; else if (index == newSize) setTraySelection(new StructuredSelection(fTrayRoots.get(newSize - 1))); else setTraySelection(new StructuredSelection(fTrayRoots.get(index))); } } }
From source file:com.agynamix.platform.frontend.action.RemoveSelectedClipboardEntry.java
License:Open Source License
public void selectionChanged(SelectionChangedEvent event) { // System.out.println("Source: "+event.getSource()); // System.out.println("Selection Class "+event.getSelection().getClass()); // System.out.println(event.getSelectionProvider().getClass().getName()); if (event.getSelection() instanceof IStructuredSelection) { IStructuredSelection ss = (IStructuredSelection) event.getSelection(); Object first = ss.getFirstElement(); if (first != null) { // System.out.println("First Element Class: "+first.getClass().getName()); if (first instanceof IClipboardItem) { setEnabled(true);/*from w w w. j av a 2 s . c o m*/ } } else { // System.out.println("First Element EMPTY"); setEnabled(false); } } }
From source file:com.agynamix.simidude.frontend.gui.ClipboardTableDragSource.java
License:Open Source License
protected IClipboardItem getSelectedItem() { ISelection s = tableViewer.getSelection(); if (s instanceof IStructuredSelection) { IStructuredSelection ss = (IStructuredSelection) s; Object item = ss.getFirstElement(); if (item instanceof IClipboardItem) { return (IClipboardItem) item; }//from w w w . java 2s . co m } return null; }
From source file:com.aliyun.odps.eclipse.launch.shortcut.udf.UDFLaunchShortcuts1.java
License:Apache License
public IResource getLaunchableResource(ISelection selection) { if (selection instanceof IStructuredSelection) { IStructuredSelection ss = (IStructuredSelection) selection; if (ss.size() == 1) { Object element = ss.getFirstElement(); if (element instanceof IAdaptable) { return getLaunchableResource((IAdaptable) element); }// w w w . j a v a2 s. co m } } return null; }
From source file:com.amalto.workbench.actions.XSDAddComplexTypeElementAction.java
License:Open Source License
private boolean init() { IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection(); if (selection.getFirstElement() instanceof XSDParticle) { selParticle = (XSDParticle) selection.getFirstElement(); if (!(selParticle.getContainer() instanceof XSDModelGroup)) { return false; }/*www . ja v a 2 s . c o m*/ modelGroup = (XSDModelGroup) selParticle.getContainer(); } else { if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) { XSDComplexTypeDefinition ctd = (XSDComplexTypeDefinition) selection.getFirstElement(); if (!(ctd.getContent() instanceof XSDParticle)) { return false; } if (!(((XSDParticle) ctd.getContent()).getTerm() instanceof XSDModelGroup)) { return false; } ; modelGroup = (XSDModelGroup) ((XSDParticle) ctd.getContent()).getTerm(); } else if (selection.getFirstElement() instanceof XSDParticle) { modelGroup = (XSDModelGroup) ((XSDParticle) selection.getFirstElement()).getTerm(); } else if (selection.getFirstElement() instanceof XSDModelGroup) { modelGroup = (XSDModelGroup) selection.getFirstElement(); } else { log.info(Messages.bind(Messages._UnkownSection, selection.getFirstElement().getClass().getName(), selection.getFirstElement().toString())); return false; } } return true; }
From source file:com.amalto.workbench.actions.XSDAnnotationLookupFieldsAction.java
License:Open Source License
public IStatus doAction() { try {// w w w . j a v a2s.c om IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection(); XSDComponent xSDCom = null; if (selection.getFirstElement() instanceof Element) { TreePath tPath = ((TreeSelection) selection).getPaths()[0]; for (int i = 0; i < tPath.getSegmentCount(); i++) { if (tPath.getSegment(i) instanceof XSDAnnotation) xSDCom = (XSDAnnotation) (tPath.getSegment(i)); } } else xSDCom = (XSDComponent) selection.getFirstElement(); XSDAnnotationsStructure struc = new XSDAnnotationsStructure(xSDCom); struc.setXSDSchema(schema); // IStructuredSelection selection = (IStructuredSelection) page // .getTreeViewer().getSelection(); // XSDAnnotationsStructure struc = new XSDAnnotationsStructure( // (XSDComponent) selection.getFirstElement()); if (struc.getAnnotation() == null) { throw new RuntimeException(Messages.bind(Messages.XSDAnnotationLookupFieldsAction_ExceptionInfo, xSDCom.getClass().getName())); } dlg = new AnnotationOrderedListsDialog(new ArrayList(struc.getLookupFields().values()), new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { dlg.close(); } }, page.getSite().getShell(), Messages.XSDAnnotationLookupFieldsAction_SetLookupFields, Messages.XSDAnnotationLookupFieldsAction_LookupFields, page, AnnotationOrderedListsDialog.AnnotationLookupField_ActionType, null); dlg.setBlockOnOpen(true); int ret = dlg.open(); if (ret == Window.CANCEL) { return Status.CANCEL_STATUS; } struc.setAccessRole(dlg.getXPaths(), false, (IStructuredContentProvider) page.getTreeViewer().getContentProvider(), "X_Lookup_Field");//$NON-NLS-1$ if (struc.hasChanged()) { page.refresh(); page.getTreeViewer().expandToLevel(xSDCom, 2); page.markDirty(); } } catch (Exception e) { log.error(e.getMessage(), e); MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDAnnotationLookupFieldsAction_ErrorMsg, e.getLocalizedMessage())); return Status.CANCEL_STATUS; } return Status.OK_STATUS; }
From source file:com.amalto.workbench.actions.XSDChangeBaseTypeAction.java
License:Open Source License
@Override public IStatus doAction() { try {//from w ww . j a va 2s . c o m IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection(); XSDSimpleTypeDefinition typedef = (XSDSimpleTypeDefinition) selection.getFirstElement(); // Cannot change the simple type definition of built in type // if (schema.getSchemaForSchemaNamespace().equals(typedef.getTargetNamespace())) return // Status.CANCEL_STATUS; // build list of custom types and built in types ArrayList customTypes = new ArrayList(); for (Object element : schema.getTypeDefinitions()) { XSDTypeDefinition type = (XSDTypeDefinition) element; if (type instanceof XSDSimpleTypeDefinition) { if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null) { customTypes.add(type.getName()); } } } List builtInTypes = XSDUtil.getBuiltInTypes(); // can't change builtin's base type if (builtInTypes.contains(typedef.getName())) { return Status.CANCEL_STATUS; } dialog = new SimpleTypeInputDialog(this, page.getSite().getShell(), schema, Messages.XSDChangeBaseTypeAction_DialogTitle, customTypes, builtInTypes, typedef.getBaseTypeDefinition().getName()); dialog.setBlockOnOpen(true); int ret = dialog.open(); if (ret == Window.CANCEL) { return Status.CANCEL_STATUS; } // backup current Base Type XSDTypeDefinition current = typedef.getBaseTypeDefinition(); // set new one if (builtIn) { typedef.setBaseTypeDefinition( schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), typeName)); } else { // check if simple type definition already exists XSDSimpleTypeDefinition std = schema.resolveSimpleTypeDefinition(typeName); if (!schema.getTypeDefinitions().contains(std)) { std.setBaseTypeDefinition( schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));//$NON-NLS-1$ schema.getContents().add(std); } typedef.setBaseTypeDefinition(std); } // remove current facets typedef.getFacetContents().removeAll(typedef.getFacetContents()); typedef.updateElement(); if (builtIn) { EList<XSDConstrainingFacet> constrainFacts = typedef.getBaseTypeDefinition().getFacetContents(); for (XSDConstrainingFacet fact : constrainFacts) { if (fact instanceof XSDPatternFacet) { XSDPatternFacet newFact = XSDSchemaBuildingTools.getXSDFactory().createXSDPatternFacet(); newFact.setLexicalValue(((XSDPatternFacet) fact).getLexicalValue()); typedef.getFacetContents().add(newFact); } } typedef.updateElement(); } // remove current if no more in use // if ( (current.getName()!=null) && //anonymous type // (!schema.getSchemaForSchemaNamespace().equals(current.getTargetNamespace())) // ){ // List eut =Util.findElementsUsingType(schema, current.getTargetNamespace(), current.getName()); // if (eut.size()==0) // schema.getContents().remove(current); // } page.refresh(); page.markDirty(); } catch (Exception e) { log.error(e.getMessage(), e); MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDChangeBaseTypeAction_ErrorMsg1, e.getLocalizedMessage())); return Status.CANCEL_STATUS; } return Status.OK_STATUS; }