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

The following examples show how to use org.eclipse.jface.viewers.IStructuredSelection#size() . 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: OpenEditorActionGroup.java    From xds-ide 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("Open With");
	submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));

	// Add the submenu.
	menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
}
 
Example 2
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isPullUpAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (!selection.isEmpty()) {
		if (selection.size() == 1) {
			if (selection.getFirstElement() instanceof ICompilationUnit)
				return true; // Do not force opening
			final IType type= getSingleSelectedType(selection);
			if (type != null)
				return Checks.isAvailable(type) && isPullUpAvailable(new IType[] { type});
		}
		for (final Iterator<?> iterator= selection.iterator(); iterator.hasNext();) {
			if (!(iterator.next() instanceof IMember))
				return false;
		}
		final Set<IMember> members= new HashSet<IMember>();
		@SuppressWarnings("unchecked")
		List<IMember> selectionList= (List<IMember>) (List<?>) Arrays.asList(selection.toArray());
		members.addAll(selectionList);
		return isPullUpAvailable(members.toArray(new IMember[members.size()]));
	}
	return false;
}
 
Example 3
Source File: ChangeDataColumnAction.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return the model of selected GUI object.
 */
ReportElementHandle getSelectedElement( )
{
	Object obj = super.getSelection( );
	if ( obj instanceof IStructuredSelection )
	{
		IStructuredSelection selection = (IStructuredSelection) obj;
		if ( selection.size( ) != 1 )
		{// multiple selection
			return null;
		}
		obj = selection.getFirstElement( );
	}
	if ( obj instanceof ReportElementHandle )
	{
		return (ReportElementHandle) obj;
	}
	return null;
}
 
Example 4
Source File: RevertToTemplateAction.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return the model of selected GUI object.
 */
DesignElementHandle getSelectedElement( )
{
	Object obj = super.getSelection( );
	if ( obj instanceof IStructuredSelection )
	{
		IStructuredSelection selection = (IStructuredSelection) obj;
		if ( selection.size( ) != 1 )
		{// multiple selection
			return null;
		}
		obj = selection.getFirstElement( );
	}
	if ( obj instanceof DesignElementHandle )
	{
		return (DesignElementHandle) obj;
	}
	return null;
}
 
Example 5
Source File: FilterPatternAction.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void selectionChanged(IAction action, ISelection selection) {
    if (!(selection instanceof IStructuredSelection)) {
        data = null;
        action.setEnabled(false);
        return;
    }
    IStructuredSelection ss = (IStructuredSelection) selection;
    if (ss.size() != 1) {
        data = null;
        action.setEnabled(false);
        return;
    }
    Object firstElement = ss.getFirstElement();
    if (firstElement instanceof IMarker) {
        data = firstElement;
        action.setEnabled(true);
        return;
    }
    if (!(firstElement instanceof BugGroup)) {
        data = null;
        action.setEnabled(false);
        return;
    }
    data = ((BugGroup) firstElement).getData();
    action.setEnabled(data != null);
}
 
Example 6
Source File: AddFolderToBuildpathAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canHandle(IStructuredSelection elements) {
	if (elements.size() == 0)
		return false;
	try {
		for (Iterator<?> iter= elements.iterator(); iter.hasNext();) {
			Object element= iter.next();
			if (element instanceof IJavaProject) {
				if (ClasspathModifier.isSourceFolder((IJavaProject)element))
					return false;
			} else if (element instanceof IPackageFragment) {
				IPackageFragment fragment= (IPackageFragment)element;
				if (ClasspathModifier.isDefaultFragment(fragment))
                    return false;

				if (ClasspathModifier.isInExternalOrArchive(fragment))
                    return false;
				IResource res;
				if ((res= fragment.getResource()) != null && res.isVirtual())
					return false;
			} else if (element instanceof IFolder) {
				IProject project= ((IFolder)element).getProject();
				IJavaProject javaProject= JavaCore.create(project);
				if (javaProject == null || !javaProject.exists() || ((IFolder)element).isVirtual())
					return false;
			} else {
				return false;
			}
		}
		return true;
	} catch (CoreException e) {
	}
	return false;
}
 
Example 7
Source File: N4JSMarkerResolutionGenerator.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns true iff the user is trying to apply quick fixes to multiple issues / markers at once.
 * <p>
 * Implementation note: this method assumes that the entire code of class MarkerResolutionGenerator is only invoked
 * if quick fixes are initiated via the Problems view (not if they are initiated from within the editor). Therefore,
 * this method simply checks whether the Problems view contains a selection of multiple, i.e. two or more, elements.
 */
private boolean isMultiApplyAttempt() {
	if (workbench == null)
		return false;
	try {
		// get the current selection in the problems view
		final ISelectionService service = workbench.getActiveWorkbenchWindow().getSelectionService();
		final IStructuredSelection sel = (IStructuredSelection) service.getSelection(IPageLayout.ID_PROBLEM_VIEW);
		return sel != null && sel.size() >= 2;
	} catch (Exception e) {
		return false;
	}
}
 
Example 8
Source File: OpenSuperImplementationAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IMethod getMethod(IStructuredSelection selection) {
	if (selection.size() != 1)
		return null;
	Object element= selection.getFirstElement();
	if (element instanceof IMethod) {
		return (IMethod) element;
	}
	return null;
}
 
Example 9
Source File: DFSActionImpl.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void disconnect(IStructuredSelection selection) {
  if (selection.size() != 1)
    return;

  Object first = selection.getFirstElement();
  if (!(first instanceof DFSLocationsRoot))
    return;

  DFSLocationsRoot root = (DFSLocationsRoot) first;
  root.disconnect();
  root.refresh();
}
 
Example 10
Source File: NewWizardsActionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean canEnable(IStructuredSelection sel) {
	if (sel.size() == 0)
		return true;

	List<?> list= sel.toList();
	for (Iterator<?> iterator= list.iterator(); iterator.hasNext();) {
		if (!isNewTarget(iterator.next()))
			return false;
	}

	return true;
}
 
Example 11
Source File: OpenCamelExistVersionProcessAction.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.size() == 1;
    if (canWork) {
        Object o = selection.getFirstElement();
        if (o instanceof IRepositoryNode) {
            IRepositoryNode node = (IRepositoryNode) o;
            switch (node.getType()) {
            case REPOSITORY_ELEMENT:
                if (node.getObjectType() == CamelRepositoryNodeType.repositoryRoutesType) {
                    canWork = true;
                } else {
                    canWork = false;
                }
                break;
            default:
                canWork = false;
                break;
            }
            if (canWork) {
                canWork = node.getObject().getRepositoryStatus() != ERepositoryStatus.DELETED;
            }
            if (canWork) {
                canWork = isLastVersion(node);
            }
        }
    }
    setEnabled(canWork);
}
 
Example 12
Source File: CompareWithEachOtherAction.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean updateSelection(final IStructuredSelection sel) {
	fSelection = sel;
	isEnabled = sel.size() == 2 && selectionIsOfType(IResource.FILE);
	action.selectionChanged(this, sel);
	return isEnabled;
}
 
Example 13
Source File: SVNHistoryPage.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
private void fillChangePathsMenu(IMenuManager manager) {
//
// Commented out Get Contents, Revert and Switch options until when/if
// they can be fixed.  Problem is that we need a way to get the local
// resource from the LogEntryChangePath.
//
	  IStructuredSelection sel = (IStructuredSelection)changePathsViewer.getSelection();
	  if (sel.size() == 1) {
		  if (sel.getFirstElement() instanceof LogEntryChangePath) {
//			  manager.add(getGetContentsAction());
		  }
		  manager.add(getCreateTagFromRevisionChangedPathAction());
	  }
//	  manager.add(getRevertChangesChangedPathAction());
//	  manager.add(getSwitchChangedPathAction());
	  manager.add(new Separator("exportImportGroup")); //$NON-NLS-1$
	  if (sel.size() == 1) {
		  if (sel.getFirstElement() instanceof LogEntryChangePath) {
			  manager.add(getExportAction());
			  if (((LogEntryChangePath)sel.getFirstElement()).getAction() == 'D') {
				  manager.add(getCopyChangedPathAction());
			  }
		  }		  
	  }
	  manager.add(new Separator("openGroup")); //$NON-NLS-1$
	  if (sel.size() == 1) {
		  if (sel.getFirstElement() instanceof LogEntryChangePath) {
			  manager.add(getShowAnnotationAction());
		  }
		  manager.add(getCompareAction());
	  }
	  if (sel.getFirstElement() instanceof LogEntryChangePath) {
		  manager.add(getOpenChangedPathAction());
	  }
	  if (sel.size() == 1) manager.add(getShowHistoryAction());	
  }
 
Example 14
Source File: ManageLinksAction.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 CodewindApplication) {
			app = (CodewindApplication) obj;
			setEnabled(app.isAvailable() && app.getAppStatus() == AppStatus.STARTED);
			return;
		}
	}
	setEnabled(false);
}
 
Example 15
Source File: OpenActionProvider.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public void fillActionBars(IActionBars theActionBars) {
	if (!contribute) {
		return;
	}
	IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
	if (selection.size() == 1 && selection.getFirstElement() instanceof IFile) {
		openFileAction.selectionChanged(selection);
		theActionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, openFileAction);
	}

}
 
Example 16
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isMoveMethodAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.size() == 1) {
		final Object first= selection.getFirstElement();
		return first instanceof IMethod && isMoveMethodAvailable((IMethod) first);
	}
	return false;
}
 
Example 17
Source File: ShowInPackageExplorerAction.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void selectionChanged(IAction action, ISelection selection) {
    if (!(selection instanceof IStructuredSelection)) {
        data = null;
        action.setEnabled(false);
        return;
    }
    IStructuredSelection ss = (IStructuredSelection) selection;
    if (ss.size() != 1) {
        data = null;
        action.setEnabled(false);
        return;
    }
    Object firstElement = ss.getFirstElement();
    if (firstElement instanceof IMarker) {
        IMarker marker = (IMarker) firstElement;
        data = marker.getResource();
        action.setEnabled(data != null);
        return;
    }
    if (!(firstElement instanceof BugGroup)) {
        data = null;
        action.setEnabled(false);
        return;
    }
    BugGroup group = (BugGroup) firstElement;
    if (group.getType() == GroupType.Class || group.getType() == GroupType.Package || group.getType() == GroupType.Project) {
        data = group.getData();
        action.setEnabled(data != null);
    } else {
        data = null;
        action.setEnabled(false);
    }
}
 
Example 18
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isInlineMethodAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.isEmpty() || selection.size() != 1)
		return false;
	final Object first= selection.getFirstElement();
	return (first instanceof IMethod) && isInlineMethodAvailable(((IMethod) first));
}
 
Example 19
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isInlineConstantAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.isEmpty() || selection.size() != 1)
		return false;
	final Object first= selection.getFirstElement();
	return (first instanceof IField) && isInlineConstantAvailable(((IField) first));
}
 
Example 20
Source File: CreateCiteHandler.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	// TODO: seems to be there is some refactoring needed in here
	ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
	if (selection == null || !(selection instanceof IStructuredSelection)) {
		return null;
	}
	IStructuredSelection currentSelection = (IStructuredSelection) selection;
	IViewPart part = null;
	try {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				part = page.showView(chartViewId);
			}
		}
	} catch (PartInitException e) {
		e.printStackTrace();
		return null;
	}
	if (part instanceof ICommunicationView) {
		view = (ICommunicationView) part;
	} else {
		return null;
	}

	view.getPreview().setTextToShow(noDataToDisplay);
	if (currentSelection.size() == 1) {
		view.getPreview().setDataPresent(true);
		ChartDataProvider provider = new ChartDataProvider();
		Term input = (Term) currentSelection.getFirstElement();
		Map<String, Integer> citeChartData = provider.calculateNumberOfPapersPerClass(input);
		BarChartConfiguration.get().getGeneralSettings().setChartTitle("Number of cites per subclass of " + input.getName());
		BarChartConfiguration.get().setTermSort(TermSort.SUBCLASS);
		Chart citeChart = ChartGenerator.createCiteBar(citeChartData);
		view.setAndRenderChart(citeChart);
	} else {
		view.setAndRenderChart(null);
	}
	return null;
}