Java Code Examples for org.eclipse.jface.viewers.IStructuredSelection#getFirstElement()

The following examples show how to use org.eclipse.jface.viewers.IStructuredSelection#getFirstElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExternalizeWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void openEditButton(ISelection selection) {
	try {
		IStructuredSelection sel= (IStructuredSelection) fTableViewer.getSelection();
		NLSSubstitution substitution= (NLSSubstitution) sel.getFirstElement();
		if (substitution == null) {
			return;
		}

		NLSInputDialog dialog= new NLSInputDialog(getShell(), substitution);
		if (dialog.open() == Window.CANCEL)
			return;
		KeyValuePair kvPair= dialog.getResult();
		if (substitution.getState() == NLSSubstitution.EXTERNALIZED) {
			substitution.setKey(kvPair.getKey());
		}
		substitution.setValue(kvPair.getValue());
		validateKeys(false);
	} finally {
		fTableViewer.refresh();
		fTableViewer.getControl().setFocus();
		fTableViewer.setSelection(selection);
	}
}
 
Example 2
Source File: CreateEIPRouteDesignActionHandler.java    From eip-designer with Apache License 2.0 6 votes vote down vote up
@Override
protected String checkEventCurrentSelection(ISelection currentSelection) {
   if (currentSelection != null && currentSelection instanceof IStructuredSelection) {
      // Retrieve semantic element (ie. a togaf service) corresponding to current selection.
      IStructuredSelection selection = (IStructuredSelection)currentSelection;
    
      Object receiver = selection.getFirstElement();
      if (receiver instanceof IGraphicalEditPart) {
         final IGraphicalEditPart part = (IGraphicalEditPart) receiver;
         final EObject model = part.resolveSemanticElement();
       
         // We have retrieved here a DNodeSpec element or something that subclasses DDiagramElement...
         if (model instanceof DDiagramElement) {
            DDiagramElement dde = (DDiagramElement) model;
          
            if (dde.getTarget() instanceof InformationSystemService) {
               service = (InformationSystemService) dde.getTarget();
               return service.getName() + "Route";
            }
         }
      }
   }
   return null;
}
 
Example 3
Source File: AttachDebuggerAction.java    From codewind-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
  public void selectionChanged(IStructuredSelection sel) {
if (sel.size() == 1) {
	Object obj = sel.getFirstElement();
	if (obj instanceof CodewindEclipseApplication) {
          	app = (CodewindEclipseApplication) obj;
          	if (app.projectLanguage.isJavaScript()) {
          		this.setText(NodeJSDebugLauncher.useBuiltinDebugger() ? Messages.AttachDebuggerLabel : Messages.LaunchDebugSessionLabel);
          	} else {
          		this.setText(Messages.AttachDebuggerLabel);
          	}
          	if (app.isAvailable() && app.readyForDebugSession() &&
          			(app.getAppStatus() == AppStatus.STARTED || app.getAppStatus() == AppStatus.STARTING)) {
          		setEnabled(app.canAttachDebugger());
          		return;
          	}
          }
}
setEnabled(false);
  }
 
Example 4
Source File: WhiteSpaceTabPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void selectionChanged(SelectionChangedEvent event) {
     final IStructuredSelection selection= (IStructuredSelection)event.getSelection();

     if (selection.isEmpty() || !(selection.getFirstElement() instanceof Node))
         return;

     final Node selected= (Node)selection.getFirstElement();

     if (selected == null || selected == fLastSelected)
return;


     if (event.getSource() == fInnerViewer && selected instanceof InnerNode) {
         fLastSelected= (InnerNode)selected;
         fDialogSettings.put(PREF_INNER_INDEX, selected.index);
         innerViewerChanged((InnerNode)selected);
     }
     else if (event.getSource() == fOptionsViewer && selected instanceof OptionNode)
         fDialogSettings.put(PREF_OPTION_INDEX, selected.index);
 }
 
Example 5
Source File: OpenAction.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean isEnabled() {
    ISelection selection = selectionProvider.getSelection();
    if (!selection.isEmpty()) {
        IStructuredSelection sSelection = (IStructuredSelection) selection;
        Object firstElement = sSelection.getFirstElement();
        if ((sSelection.size() == 1) && (firstElement instanceof TmfTraceElement ||
                firstElement instanceof TmfExperimentElement ||
                firstElement instanceof TmfOnDemandAnalysisElement ||
                firstElement instanceof TmfAnalysisOutputElement ||
                firstElement instanceof TmfReportElement ||
                firstElement instanceof TmfAnalysisElement)) {
            element = (TmfProjectModelElement) firstElement;
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: LanguageCodesPreferencePage.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 根据当前选择的语言,更新数字、货币等的显示示例
 * @param selection
 *            ;
 */
protected void refreshFormatControls(IStructuredSelection selection) {
	Object firstSelected = selection.getFirstElement();
	if (firstSelected instanceof Language) {
		Language language = (Language) firstSelected;
		digitalValue.setText(language.getName());
		digitalValue.setToolTipText(language.getName());

		currencyValue.setText(language.getName());
		currencyValue.setToolTipText(language.getName());

		timeValue.setText(language.getName());
		timeValue.setToolTipText(language.getName());

		shortDateValue.setText(language.getName());
		shortDateValue.setToolTipText(language.getName());

		longDateValue.setText(language.getName());
		longDateValue.setToolTipText(language.getName());
	}
}
 
Example 7
Source File: ShowPropertiesSynchronizeAction.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
protected FastSyncInfoFilter getSyncInfoFilter() {
	return new FastSyncInfoFilter() {
		public boolean select(SyncInfo info) {
			SyncInfoDirectionFilter outgoingFilter = new SyncInfoDirectionFilter(new int[] {SyncInfo.OUTGOING});
		    if (!outgoingFilter.select(info)) return false;
			IStructuredSelection selection = getStructuredSelection();
		    if (selection.size() != 1) return false;
	        ISynchronizeModelElement element = (ISynchronizeModelElement)selection.getFirstElement();
		    IResource resource = element.getResource();
	        if (resource == null) return false;
               ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);			    
               try {
                   return !svnResource.getStatus().isDeleted() && svnResource.getStatus().isManaged() && resource.exists();
               } catch (SVNException e) {
                   return false;
               }
		}
	};
}
 
Example 8
Source File: OpenEditorActionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void addOpenWithMenu(IMenuManager menu) {
	ISelection selection= getContext().getSelection();
	if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
		return;
	IStructuredSelection ss= (IStructuredSelection)selection;
	if (ss.size() != 1)
		return;

	Object o= ss.getFirstElement();
	if (!(o instanceof IAdaptable))
		return;

	IAdaptable element= (IAdaptable)o;
	Object resource= element.getAdapter(IResource.class);
	if (!(resource instanceof IFile))
		return;

	// Create a menu.
	IMenuManager submenu= new MenuManager(ActionMessages.OpenWithMenu_label);
	submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));

	// Add the submenu.
	menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
}
 
Example 9
Source File: ShowBugInfoAction.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public final void run(final IAction action) {
    if (targetPart == null) {
        return;
    }
    try {
        if (!selection.isEmpty() && (selection instanceof IStructuredSelection)) {
            IStructuredSelection ssel = (IStructuredSelection) selection;
            Object element = ssel.getFirstElement();
            if (element instanceof IMarker) {
                IMarker marker = (IMarker) element;
                FindbugsPlugin.showMarker(marker, FindbugsPlugin.DETAILS_VIEW_ID, targetPart);
            }
        }
    } finally {
        targetPart = null;
    }
}
 
Example 10
Source File: IntroduceFactoryAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IStructuredSelection selection) {
	try {
		// we have to call this here - no selection changed event is sent after a refactoring but it may still invalidate enablement
		if (RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection)) {
			IMethod method= (IMethod) selection.getFirstElement();
			if (!ActionUtil.isEditable(getShell(), method))
				return;
			ISourceRange range= method.getNameRange();
			RefactoringExecutionStarter.startIntroduceFactoryRefactoring(method.getCompilationUnit(), new TextSelection(range.getOffset(), range.getLength()), getShell());
		}
	} catch (CoreException e) {
		ExceptionHandler.handle(e, RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringMessages.IntroduceFactoryAction_exception);
	}
}
 
Example 11
Source File: AddConnectionAction.java    From codewind-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void selectionChanged(IStructuredSelection sel) {
	if (sel.size() == 1) {
		Object obj = sel.getFirstElement();
		if (obj instanceof CodewindManager) {
			setEnabled(true);
			return;
		}
	}
	setEnabled(false);
}
 
Example 12
Source File: AssetsPart.java    From offspring with MIT License 5 votes vote down vote up
@Inject
@Optional
private void onAssetSelected(
    @UIEventTopic(IAssetExchange.TOPIC_ASSET_SELECTED) Asset asset) {
  if (assetsViewer != null && !assetsViewer.getControl().isDisposed()) {
    IStructuredSelection selection = (IStructuredSelection) assetsViewer
        .getSelection();
    Object selectedAsset = selection.getFirstElement();
    if (selectedAsset instanceof Asset && asset instanceof Asset) {
      if (!selectedAsset.equals(asset)) {
        assetsViewer.setSelection(new StructuredSelection(asset));
      }
    }
  }
}
 
Example 13
Source File: GenerateNewConstructorUsingFieldsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IStructuredSelection selection) {
	try {
		IType selectionType= getSelectedType(selection);
		if (selectionType == null) {
			MessageDialog.openInformation(getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_not_applicable);
			notifyResult(false);
			return;
		}

		IField[] selectedFields= getSelectedFields(selection);

		if (canRunOn(selectedFields)) {
			run(selectedFields[0].getDeclaringType(), selectedFields, false);
			return;
		}
		Object firstElement= selection.getFirstElement();

		if (firstElement instanceof IType) {
			run((IType) firstElement, new IField[0], false);
		} else if (firstElement instanceof ICompilationUnit) {
			IType type= ((ICompilationUnit) firstElement).findPrimaryType();
			if (type.isAnnotation()) {
				MessageDialog.openInformation(getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_annotation_not_applicable);
				notifyResult(false);
				return;
			} else if (type.isInterface()) {
				MessageDialog.openInformation(getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_interface_not_applicable);
				notifyResult(false);
				return;
			} else
				run(((ICompilationUnit) firstElement).findPrimaryType(), new IField[0], false);
		}
	} catch (CoreException exception) {
		ExceptionHandler.handle(exception, getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_error_actionfailed);
	}
}
 
Example 14
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isMoveInnerAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.size() == 1) {
		Object first= selection.getFirstElement();
		if (first instanceof IType) {
			return isMoveInnerAvailable((IType) first);
		}
	}
	return false;
}
 
Example 15
Source File: ReadCamelProcess.java    From tesb-studio-se with Apache License 2.0 5 votes vote down vote up
@Override
public void init(TreeViewer viewer, IStructuredSelection selection) {
    boolean canWork = !selection.isEmpty() && selection.size() == 1;
    if (canWork) {
        final IRepositoryNode node = (IRepositoryNode) selection.getFirstElement();
        canWork = node.getType() == ENodeType.REPOSITORY_ELEMENT
            //&& node.getObject() != null
            //&& ProxyRepositoryFactory.getInstance().getStatus(node.getObject()) != ERepositoryStatus.LOCK_BY_USER
            && node.getObjectType() == CamelRepositoryNodeType.repositoryRoutesType
            && !RepositoryManager.isOpenedItemInEditor(node.getObject());
    }
    setEnabled(canWork);
}
 
Example 16
Source File: UserLibraryPreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IPath getWorkbenchWindowSelection() {
	IWorkbenchWindow window= fWorkbench.getActiveWorkbenchWindow();
	if (window != null) {
		ISelection selection= window.getSelectionService().getSelection();
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection= (IStructuredSelection) selection;
			Object element= structuredSelection.getFirstElement();
			if (element != null) {
				Object resource= Platform.getAdapterManager().getAdapter(element, IResource.class);
				if (resource != null) {
					return ((IResource) resource).getFullPath();
				}
				if (structuredSelection instanceof ITreeSelection) {
					TreePath treePath= ((ITreeSelection) structuredSelection).getPaths()[0];
					while ((treePath = treePath.getParentPath()) != null) {
						element= treePath.getLastSegment();
						resource= Platform.getAdapterManager().getAdapter(element, IResource.class);
						if (resource != null) {
							return ((IResource) resource).getFullPath();
						}
					}
				}
			}
			
		}
	}
	return null;
}
 
Example 17
Source File: TypeScriptTemplatePreferencePage.java    From typescript.java with MIT License 5 votes vote down vote up
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();
	
	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JSDTTypeScriptUIPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);
		
		IDocument doc= viewer.getDocument();
		
		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$
		
		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}		
}
 
Example 18
Source File: CustomXmlParserInputWizardPage.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void selectionChanged(SelectionChangedEvent event) {
    if (selectedElement != null) {
        selectedElement.dispose();
    }
    if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
        IStructuredSelection sel = (IStructuredSelection) event.getSelection();
        CustomXmlInputElement inputElement = (CustomXmlInputElement) sel.getFirstElement();
        selectedElement = new ElementNode(elementContainer, inputElement);
        elementContainer.layout();
        elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
                elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
        container.layout();
        validate();
        updatePreviews();
        removeButton.setEnabled(true);
        addChildButton.setEnabled(true);
        addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement);
        if (definition.rootInputElement == inputElement) {
            addNextButton.setEnabled(false);
        } else {
            addNextButton.setEnabled(true);
        }
        moveUpButton.setEnabled(true);
        moveDownButton.setEnabled(true);
    } else {
        removeButton.setEnabled(false);
        if (definition.rootInputElement == null) {
            addChildButton.setEnabled(true);
            addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement);
        } else {
            addChildButton.setEnabled(false);
        }
        addNextButton.setEnabled(false);
        moveUpButton.setEnabled(false);
        moveDownButton.setEnabled(false);
    }
}
 
Example 19
Source File: ResolveTreeConflictAction.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public void run() {
	IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();
	SVNTreeConflict treeConflict = (SVNTreeConflict)selection.getFirstElement();
	ResolveTreeConflictWizard wizard = new ResolveTreeConflictWizard(treeConflict, targetPart);
	WizardDialog dialog = new SizePersistedWizardDialog(Display.getDefault().getActiveShell(), wizard, "ResolveTreeConflict"); //$NON-NLS-1$
	dialog.open();
}
 
Example 20
Source File: JarPackageActionDelegate.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns the description file for the first description file in
 * the selection. Use this method if this action is only active if
 * one single file is selected.
 * @param selection the current selection
 * @return description file
 */
protected IFile getDescriptionFile(IStructuredSelection selection) {
	return (IFile)selection.getFirstElement();
}