Search in sources :

Example 1 with WorkingSetDiffBuilder

use of org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder in project n4js by eclipse.

the class AssignWorkingSetsAction method performWorkingSetUpdate.

/**
 * Performs the working sets update specified by the given dialog result.
 *
 * @param dialogResult
 *            The result of the list selection dialog.
 * @param selectionProjectNames
 *            A list of names of the selected projects
 * @returns {@code true} if the update was successfully performed. {@code false} otherwise.
 */
private boolean performWorkingSetUpdate(Object[] dialogResult, Iterable<String> selectionProjectNames) {
    if (dialogResult == null) {
        return false;
    }
    WorkingSetManager workingSetManager = broker.getActiveManager();
    if (!(workingSetManager instanceof ManualAssociationAwareWorkingSetManager)) {
        return false;
    }
    // get copies of the workspace working sets.
    List<WorkingSet> allWorkingSets = new ArrayList<>(Arrays.asList(workingSetManager.getAllWorkingSets()));
    List<WorkingSet> workingSets = new ArrayList<>(Arrays.asList(workingSetManager.getWorkingSets()));
    // build WorkingSet edit difference
    WorkingSetDiffBuilder builder = new WorkingSetDiffBuilder(workingSetManager);
    for (Object resultItem : dialogResult) {
        // abort if the dialog returned a non-working-set item
        if (!(resultItem instanceof ManualAssociationWorkingSet)) {
            return false;
        }
        ManualAssociationWorkingSet oldState = (ManualAssociationWorkingSet) resultItem;
        ManualAssociationWorkingSet newState = getWorkingSetWithAddedProjects(oldState, selectionProjectNames);
        // register working set edit
        builder.edit(oldState, newState);
        // replace working set with its new state
        replaceWorkingSet(workingSets, newState);
        replaceWorkingSet(allWorkingSets, newState);
    }
    // apply WorkingSet update and refresh UI
    workingSetManager.updateState(builder.build(workingSets, allWorkingSets));
    broker.refreshNavigator();
    return true;
}
Also used : WorkingSetDiffBuilder(org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) ArrayList(java.util.ArrayList) WorkingSet(org.eclipse.n4js.ui.workingsets.WorkingSet) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) ManualAssociationAwareWorkingSetManager(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager) ManualAssociationAwareWorkingSetManager(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager) WorkingSetManager(org.eclipse.n4js.ui.workingsets.WorkingSetManager)

Example 2 with WorkingSetDiffBuilder

use of org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder in project n4js by eclipse.

the class ShowHiddenWorkingSetsDropDownAction method showWorkingSet.

private void showWorkingSet(final WorkingSetManager manager, final WorkingSet... workingSets) {
    final WorkingSetDiffBuilder builder = new WorkingSetDiffBuilder(manager);
    final Collection<WorkingSet> visibleItems = newArrayList(manager.getWorkingSets());
    visibleItems.addAll(newArrayList(workingSets));
    final WorkingSet[] newAllItems = manager.getAllWorkingSets();
    final Iterable<WorkingSet> newItems = newArrayList(manager.getAllWorkingSets());
    for (final Iterator<WorkingSet> itr = newItems.iterator(); itr.hasNext(); ) /**/
    {
        final WorkingSet next = itr.next();
        if (!visibleItems.contains(next)) {
            itr.remove();
        }
    }
    final Diff<WorkingSet> diff = builder.build(Iterables.toArray(newItems, WorkingSet.class), newAllItems);
    if (!diff.isEmpty()) {
        manager.updateState(diff);
        manager.saveState(new NullProgressMonitor());
        manager.getWorkingSetManagerBroker().refreshNavigator();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) WorkingSetDiffBuilder(org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder) WorkingSet(org.eclipse.n4js.ui.workingsets.WorkingSet)

Example 3 with WorkingSetDiffBuilder

use of org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder in project n4js by eclipse.

the class HideWorkingSetAction method run.

@Override
public void run() {
    final IStructuredSelection selection = getStructuredSelection();
    final Object[] selectionElements = selection.toArray();
    final WorkingSet[] selectedWorkingSets = Arrays2.filter(selectionElements, WorkingSet.class);
    final WorkingSetManager manager = selectedWorkingSets[0].getWorkingSetManager();
    final WorkingSetDiffBuilder builder = new WorkingSetDiffBuilder(manager);
    final WorkingSet[] newAllItems = manager.getAllWorkingSets();
    final List<WorkingSet> newItems = newArrayList(manager.getWorkingSets());
    for (final WorkingSet toHide : selectedWorkingSets) {
        newItems.remove(toHide);
    }
    final Diff<WorkingSet> diff = builder.build(Iterables.toArray(newItems, WorkingSet.class), newAllItems);
    if (!diff.isEmpty()) {
        manager.updateState(diff);
        manager.saveState(new NullProgressMonitor());
        manager.getWorkingSetManagerBroker().refreshNavigator();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) WorkingSetDiffBuilder(org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) WorkingSet(org.eclipse.n4js.ui.workingsets.WorkingSet) WorkingSetManager(org.eclipse.n4js.ui.workingsets.WorkingSetManager)

Example 4 with WorkingSetDiffBuilder

use of org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder in project n4js by eclipse.

the class N4JSProjectInWorkingSetDropAdapterAssistant method handleDrop.

@Override
public IStatus handleDrop(CommonDropAdapter dropAdapter, DropTargetEvent dropTargetEvent, Object target) {
    WorkingSet oldTarget = (WorkingSet) target;
    WorkingSetManager manager = oldTarget.getWorkingSetManager();
    List<WorkingSet> allItems = newArrayList(manager.getAllWorkingSets());
    List<WorkingSet> visibleItems = newArrayList(manager.getWorkingSets());
    WorkingSetDiffBuilder diffBuilder = new WorkingSetDiffBuilder(manager);
    ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
    if (selection instanceof ITreeSelection) {
        ManualAssociationWorkingSet oldSource = null;
        for (TreePath path : ((ITreeSelection) selection).getPaths()) {
            IProject project = ((IAdaptable) path.getLastSegment()).getAdapter(IProject.class);
            if (project != null) {
                if (!(target instanceof ManualAssociationWorkingSet)) {
                    return CANCEL_STATUS;
                }
                if (!ManualAssociationAwareWorkingSetManager.class.getName().equals(manager.getId())) {
                    return CANCEL_STATUS;
                }
                if (!workingSetContains(oldTarget, project) && !OTHERS_WORKING_SET_ID.equals(oldTarget.getId())) {
                    Collection<String> projectNames = newHashSet(((ManualAssociationWorkingSet) oldTarget).getAssociatedProjectNames());
                    projectNames.add(project.getName());
                    ManualAssociationWorkingSet newTarget = new ManualAssociationWorkingSet(projectNames, oldTarget.getId(), manager);
                    int allIndex = indexOfById(oldTarget, allItems);
                    allItems.remove(allIndex);
                    allItems.add(allIndex, newTarget);
                    int visibleIndex = indexOfById(oldTarget, visibleItems);
                    if (visibleIndex >= 0) {
                        visibleItems.remove(visibleIndex);
                        visibleItems.add(visibleIndex, newTarget);
                    }
                    diffBuilder.edit(oldTarget, newTarget);
                    oldTarget = newTarget;
                }
                // Check if our top-level element is a working set so that we can perform a move
                if (path.getFirstSegment() instanceof ManualAssociationWorkingSet) {
                    if (oldSource == null) {
                        oldSource = ((ManualAssociationWorkingSet) path.getFirstSegment());
                    }
                    if (oldSource != null && !OTHERS_WORKING_SET_ID.equals(oldSource.getId())) {
                        Collection<String> projectNames = newHashSet(oldSource.getAssociatedProjectNames());
                        projectNames.remove(project.getName());
                        ManualAssociationWorkingSet newSource = new ManualAssociationWorkingSet(projectNames, oldSource.getId(), manager);
                        int allIndex = indexOfById(oldSource, allItems);
                        allItems.remove(allIndex);
                        allItems.add(allIndex, newSource);
                        int visibleIndex = indexOfById(oldSource, visibleItems);
                        if (visibleIndex >= 0) {
                            visibleItems.remove(visibleIndex);
                            visibleItems.add(visibleIndex, newSource);
                        }
                        diffBuilder.edit(oldSource, newSource);
                        oldSource = newSource;
                    }
                }
            } else if (path.getLastSegment() instanceof WorkingSet) {
                WorkingSet movedWorkingSet = (WorkingSet) path.getLastSegment();
                int sourceVisibleIndex = indexOfById(movedWorkingSet, visibleItems);
                int sourceAllIndex = indexOfById(movedWorkingSet, allItems);
                if (sourceVisibleIndex == -1 || sourceAllIndex == -1) {
                    return CANCEL_STATUS;
                }
                final Object currentTarget = getCommonDropAdapter().getCurrentTarget();
                if (currentTarget instanceof WorkingSet) {
                    int targetVisibleIndex = indexOfById((WorkingSet) currentTarget, visibleItems);
                    int targetAllIndex = indexOfById((WorkingSet) currentTarget, allItems);
                    if (targetVisibleIndex == -1 || targetAllIndex == -1) {
                        return CANCEL_STATUS;
                    }
                    if (getCommonDropAdapter().getCurrentLocation() == ViewerDropAdapter.LOCATION_AFTER) {
                        targetVisibleIndex++;
                        targetAllIndex++;
                    }
                    WorkingSet visibleRemoved = visibleItems.remove(sourceVisibleIndex);
                    visibleItems.add(sourceVisibleIndex >= targetVisibleIndex ? targetVisibleIndex : targetVisibleIndex - 1, visibleRemoved);
                    WorkingSet allRemoved = allItems.remove(sourceAllIndex);
                    allItems.add(sourceAllIndex >= targetAllIndex ? targetAllIndex : targetAllIndex - 1, allRemoved);
                } else {
                    return CANCEL_STATUS;
                }
            }
        }
    } else if (selection instanceof IStructuredSelection) {
        for (Object item : ((IStructuredSelection) selection).toArray()) {
            IProject project = ((IAdaptable) item).getAdapter(IProject.class);
            if (project != null && !workingSetContains(oldTarget, project) && !OTHERS_WORKING_SET_ID.equals(oldTarget.getId())) {
                Collection<String> projectNames = newHashSet(((ManualAssociationWorkingSet) oldTarget).getAssociatedProjectNames());
                projectNames.add(project.getName());
                ManualAssociationWorkingSet newTarget = new ManualAssociationWorkingSet(projectNames, oldTarget.getId(), manager);
                allItems.remove(oldTarget);
                allItems.add(newTarget);
                if (visibleItems.remove(oldTarget)) {
                    visibleItems.add(newTarget);
                }
                diffBuilder.edit(oldTarget, newTarget);
                oldTarget = newTarget;
            }
        }
    }
    WorkingSet[] newItems = Iterables.toArray(visibleItems, WorkingSet.class);
    WorkingSet[] newAllItems = Iterables.toArray(allItems, WorkingSet.class);
    Diff<WorkingSet> diff = diffBuilder.build(newItems, newAllItems);
    if (!diff.isEmpty()) {
        manager.updateState(diff);
        manager.saveState(new NullProgressMonitor());
        workingSetManagerBroker.refreshNavigator();
    }
    return OK_STATUS;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ManualAssociationAwareWorkingSetManager(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager) WorkingSetManager(org.eclipse.n4js.ui.workingsets.WorkingSetManager) IProject(org.eclipse.core.resources.IProject) ITreeSelection(org.eclipse.jface.viewers.ITreeSelection) WorkingSetDiffBuilder(org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder) TreePath(org.eclipse.jface.viewers.TreePath) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) ISelection(org.eclipse.jface.viewers.ISelection) Collection(java.util.Collection) WorkingSet(org.eclipse.n4js.ui.workingsets.WorkingSet) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet)

Example 5 with WorkingSetDiffBuilder

use of org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder in project n4js by eclipse.

the class GHOLD_101_WorkingSetsTest_PluginUITest method testDndSupport.

/**
 */
@Test
public void testDndSupport() throws CoreException {
    final Collection<String> projectNames = newArrayList("A", "B", "C", "D", "E");
    final IWorkspaceDescription workspaceDescription = ResourcesPlugin.getWorkspace().getDescription();
    final boolean autoBuild = workspaceDescription.isAutoBuilding();
    try {
        // No need for the build at all.
        workspaceDescription.setAutoBuilding(false);
        for (final String projectName : projectNames) {
            JavaProjectSetupUtil.createSimpleProject(projectName);
            assertTrue("Project " + projectName + " is not accessible.", getProjectByName(projectName).isAccessible());
        }
    } finally {
        workspaceDescription.setAutoBuilding(autoBuild);
    }
    activateWorkingSetManager(ManualAssociationAwareWorkingSetManager.class);
    WorkingSetManager manager = broker.getActiveManager();
    WorkingSetDiffBuilder builder = new WorkingSetDiffBuilder(manager);
    List<WorkingSet> workingSets = newArrayList();
    workingSets.add(new ManualAssociationWorkingSet(newArrayList(), "WS1", manager));
    workingSets.add(new ManualAssociationWorkingSet(newArrayList(), "WS2", manager));
    workingSets.add(new ManualAssociationWorkingSet(newArrayList(), "WS3", manager));
    for (WorkingSet workingSet : workingSets) {
        builder.add(workingSet);
    }
    workingSets.add(0, manager.getWorkingSets()[0]);
    Diff<WorkingSet> diff = builder.build(Iterables.toArray(workingSets, WorkingSet.class), Iterables.toArray(workingSets, WorkingSet.class));
    manager.updateState(diff);
    waitForIdleState();
    broker.refreshNavigator();
    waitForIdleState();
    commonViewer.expandToLevel(2);
    waitForIdleState();
    TreeItem[] treeItems = commonViewer.getTree().getItems();
    final int expectedItemCount = workingSets.size();
    assertTrue("Expected exactly " + expectedItemCount + " items in the Project Explorer. Input was: " + Arrays.toString(treeItems), treeItems.length == expectedItemCount);
    for (TreeItem item : treeItems) {
        Object data = item.getData();
        assertTrue("Expected instance of working set. Was: " + data, data instanceof WorkingSet);
        WorkingSet workingSet = (WorkingSet) data;
        if (WorkingSet.OTHERS_WORKING_SET_ID.equals(workingSet.getId())) {
            assertEquals("Expected " + projectNames.size() + " elements. Got: " + item.getItemCount(), projectNames.size(), item.getItemCount());
        } else {
            assertEquals("Expected 0 elements. Got: " + item.getItemCount(), 0, item.getItemCount());
        }
    }
    StructuredSelection selection = new StructuredSelection(getProjectsByName("A", "B", "C"));
    commonViewer.setSelection(selection);
    assertEquals(3, commonViewer.getTree().getSelection().length);
    INavigatorDnDService dnDService = projectExplorer.getNavigatorContentService().getDnDService();
    CommonDropAdapterAssistant[] dropAdapterAssistants = dnDService.findCommonDropAdapterAssistants(manager.getWorkingSets()[1], commonViewer.getStructuredSelection());
    assertTrue(!Arrays2.isEmpty(dropAdapterAssistants));
    N4JSProjectInWorkingSetDropAdapterAssistant[] n4DropAdapterAssistants = Arrays2.filter(dropAdapterAssistants, N4JSProjectInWorkingSetDropAdapterAssistant.class);
    assertTrue(!Arrays2.isEmpty(n4DropAdapterAssistants));
    N4JSProjectInWorkingSetDropAdapterAssistant assistant = n4DropAdapterAssistants[0];
    CommonDropAdapter adapter = assistant.getCommonDropAdapter();
    LocalSelectionTransfer.getTransfer().setSelection(commonViewer.getStructuredSelection());
    assistant.handleDrop(adapter, null, manager.getWorkingSets()[1]);
    waitForIdleState();
    broker.refreshNavigator();
    waitForIdleState();
    commonViewer.expandToLevel(2);
    waitForIdleState();
    treeItems = commonViewer.getTree().getItems();
    for (TreeItem item : treeItems) {
        Object data = item.getData();
        assertTrue("Expected instance of working set. Was: " + data, data instanceof WorkingSet);
        WorkingSet workingSet = (WorkingSet) data;
        if (WorkingSet.OTHERS_WORKING_SET_ID.equals(workingSet.getId())) {
            assertEquals("Expected " + (projectNames.size() - 3) + " elements. Got: " + item.getItemCount(), projectNames.size() - 3, item.getItemCount());
        } else if ("WS1".equals(workingSet.getId())) {
            assertEquals("Expected 3 elements. Got: " + item.getItemCount(), 3, item.getItemCount());
        } else {
            assertEquals("Expected 0 elements. Got: " + item.getItemCount(), 0, item.getItemCount());
        }
    }
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) CommonDropAdapter(org.eclipse.ui.navigator.CommonDropAdapter) N4JSProjectInWorkingSetDropAdapterAssistant(org.eclipse.n4js.ui.workingsets.internal.N4JSProjectInWorkingSetDropAdapterAssistant) ProjectTypeAwareWorkingSetManager(org.eclipse.n4js.ui.workingsets.ProjectTypeAwareWorkingSetManager) ProjectNameFilterAwareWorkingSetManager(org.eclipse.n4js.ui.workingsets.ProjectNameFilterAwareWorkingSetManager) ManualAssociationAwareWorkingSetManager(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager) WorkingSetManager(org.eclipse.n4js.ui.workingsets.WorkingSetManager) INavigatorDnDService(org.eclipse.ui.navigator.INavigatorDnDService) IWorkspaceDescription(org.eclipse.core.resources.IWorkspaceDescription) WorkingSetDiffBuilder(org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) CommonDropAdapterAssistant(org.eclipse.ui.navigator.CommonDropAdapterAssistant) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) ProjectNameFilterWorkingSet(org.eclipse.n4js.ui.workingsets.ProjectNameFilterAwareWorkingSetManager.ProjectNameFilterWorkingSet) ProjectTypeWorkingSet(org.eclipse.n4js.ui.workingsets.ProjectTypeAwareWorkingSetManager.ProjectTypeWorkingSet) WorkingSet(org.eclipse.n4js.ui.workingsets.WorkingSet) Test(org.junit.Test)

Aggregations

WorkingSet (org.eclipse.n4js.ui.workingsets.WorkingSet)6 WorkingSetDiffBuilder (org.eclipse.n4js.ui.workingsets.WorkingSetDiffBuilder)6 WorkingSetManager (org.eclipse.n4js.ui.workingsets.WorkingSetManager)5 ManualAssociationAwareWorkingSetManager (org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager)4 ManualAssociationWorkingSet (org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 IWorkspaceDescription (org.eclipse.core.resources.IWorkspaceDescription)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 ProjectNameFilterAwareWorkingSetManager (org.eclipse.n4js.ui.workingsets.ProjectNameFilterAwareWorkingSetManager)2 ProjectNameFilterWorkingSet (org.eclipse.n4js.ui.workingsets.ProjectNameFilterAwareWorkingSetManager.ProjectNameFilterWorkingSet)2 ProjectTypeAwareWorkingSetManager (org.eclipse.n4js.ui.workingsets.ProjectTypeAwareWorkingSetManager)2 ProjectTypeWorkingSet (org.eclipse.n4js.ui.workingsets.ProjectTypeAwareWorkingSetManager.ProjectTypeWorkingSet)2 TreeItem (org.eclipse.swt.widgets.TreeItem)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Pattern (java.util.regex.Pattern)1 IProject (org.eclipse.core.resources.IProject)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 ISelection (org.eclipse.jface.viewers.ISelection)1