| /******************************************************************************* |
| * Copyright (c) 2009, 2018 xored software, Inc. and others. |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * xored software, Inc. - initial API and Implementation (Alex Panchenko) |
| *******************************************************************************/ |
| package org.eclipse.dltk.ui.wizards; |
| |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.dltk.internal.ui.scriptview.ScriptExplorerPart; |
| import org.eclipse.dltk.internal.ui.workingsets.WorkingSetIDs; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.ITreeSelection; |
| import org.eclipse.jface.viewers.TreePath; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.IWorkbenchPart; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.IWorkingSet; |
| |
| /** |
| * Detects {@link IWorkingSet}s selected before project wizard was called. |
| * |
| * @since 2.0 |
| */ |
| public class WorkingSetDetector { |
| |
| private static final IWorkingSet[] EMPTY_WORKING_SET_ARRAY = new IWorkingSet[0]; |
| |
| /** |
| * Detects workings sets currently selected |
| * |
| * @param selection |
| * @param workbench |
| * @return |
| */ |
| public IWorkingSet[] detect(IStructuredSelection selection, |
| IWorkbench workbench) { |
| IWorkingSet[] selected = getSelectedWorkingSet(selection); |
| if (selected != null && selected.length > 0) { |
| for (int i = 0; i < selected.length; i++) { |
| if (!isValidWorkingSet(selected[i])) |
| return EMPTY_WORKING_SET_ARRAY; |
| } |
| return selected; |
| } |
| |
| IWorkbenchPart activePart = getActivePart(workbench); |
| |
| if (!(activePart instanceof ScriptExplorerPart)) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| ScriptExplorerPart explorerPart = (ScriptExplorerPart) activePart; |
| if (explorerPart |
| .getRootMode() == ScriptExplorerPart.PROJECTS_AS_ROOTS) { |
| // Get active filter |
| IWorkingSet filterWorkingSet = explorerPart.getFilterWorkingSet(); |
| if (filterWorkingSet == null) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| if (!isValidWorkingSet(filterWorkingSet)) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| return new IWorkingSet[] { filterWorkingSet }; |
| } |
| // If we have been gone into a working set return the working set |
| Object input = explorerPart.getViewPartInput(); |
| if (!(input instanceof IWorkingSet)) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| IWorkingSet workingSet = (IWorkingSet) input; |
| if (!isValidWorkingSet(workingSet)) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| return new IWorkingSet[] { workingSet }; |
| } |
| |
| private IWorkbenchPart getActivePart(IWorkbench workbench) { |
| IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow(); |
| if (activeWindow != null) { |
| IWorkbenchPage activePage = activeWindow.getActivePage(); |
| if (activePage != null) { |
| return activePage.getActivePart(); |
| } |
| } |
| return null; |
| } |
| |
| private IWorkingSet[] getSelectedWorkingSet( |
| IStructuredSelection selection) { |
| if (!(selection instanceof ITreeSelection)) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| ITreeSelection treeSelection = (ITreeSelection) selection; |
| if (treeSelection.isEmpty()) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| List<?> elements = treeSelection.toList(); |
| if (elements.size() == 1) { |
| Object element = elements.get(0); |
| TreePath[] paths = treeSelection.getPathsFor(element); |
| if (paths.length != 1) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| TreePath path = paths[0]; |
| if (path.getSegmentCount() == 0) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| Object candidate = path.getSegment(0); |
| if (!(candidate instanceof IWorkingSet)) |
| return EMPTY_WORKING_SET_ARRAY; |
| |
| IWorkingSet workingSetCandidate = (IWorkingSet) candidate; |
| if (isValidWorkingSet(workingSetCandidate)) |
| return new IWorkingSet[] { workingSetCandidate }; |
| |
| return EMPTY_WORKING_SET_ARRAY; |
| } |
| |
| List<IWorkingSet> result = new ArrayList<>(); |
| for (Iterator<?> iterator = elements.iterator(); iterator.hasNext();) { |
| Object element = iterator.next(); |
| if (element instanceof IWorkingSet) { |
| final IWorkingSet ws = (IWorkingSet) element; |
| if (isValidWorkingSet(ws)) { |
| result.add(ws); |
| } |
| } |
| } |
| return result.toArray(new IWorkingSet[result.size()]); |
| } |
| |
| protected boolean isValidWorkingSet(IWorkingSet workingSet) { |
| String id = workingSet.getId(); |
| if (!WorkingSetIDs.SCRIPT.equals(id) |
| && !WorkingSetIDs.RESOURCE.equals(id)) |
| return false; |
| |
| if (workingSet.isAggregateWorkingSet()) |
| return false; |
| |
| return true; |
| } |
| |
| } |