use of org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet in project n4js by eclipse.
the class WorkingSetManualAssociationWizard method addPages.
@Override
public void addPages() {
addPage(new WizardPage("") {
private Text nameText;
private final Collection<IProject> workspaceProjects = newTreeSet(PROJECT_NAME_COMPARATOR);
private final Collection<IProject> workingSetProjects = newTreeSet(PROJECT_NAME_COMPARATOR);
@Override
public void createControl(Composite parent) {
final Composite composite = new Composite(parent, NONE);
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).create());
composite.setLayoutData(fillDefaults().align(FILL, FILL).grab(true, true).create());
new Label(composite, NONE).setText("Working set name:");
nameText = new Text(composite, BORDER);
nameText.setLayoutData(fillDefaults().align(FILL, CENTER).grab(true, false).create());
Composite tableComposite = new Composite(composite, NONE);
tableComposite.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).create());
tableComposite.setLayoutData(fillDefaults().align(FILL, FILL).grab(true, true).span(2, 1).create());
Group workspaceGroup = new Group(tableComposite, SHADOW_IN);
workspaceGroup.setText("Available workspace projects");
workspaceGroup.setLayout(GridLayoutFactory.fillDefaults().create());
workspaceGroup.setLayoutData(fillDefaults().align(FILL, FILL).grab(true, true).create());
final TableViewer allProjectsViewer = new TableViewerBuilder(singletonList("")).setHasBorder(true).setHeaderVisible(false).setLinesVisible(false).setMultipleSelection(true).setColumnWidthsInPixel(Ints.asList(350)).setLabelProvider(labelProvider).build(workspaceGroup);
Composite buttonComposite = new Composite(tableComposite, NONE);
buttonComposite.setLayout(GridLayoutFactory.fillDefaults().create());
// buttonComposite.setLayoutData(fillDefaults().align(CENTER, CENTER).grab(false, false).create());
final Button addButton = new Button(buttonComposite, PUSH);
addButton.setImage(ImageRef.RIGHT_ARROW.asImage().orNull());
addButton.setToolTipText("Add all selected workspace projects");
addButton.setEnabled(false);
final Button removeButton = new Button(buttonComposite, PUSH);
removeButton.setImage(ImageRef.LEFT_ARROW.asImage().orNull());
removeButton.setToolTipText("Remove all selected working set element projects");
removeButton.setEnabled(false);
Group workingSetGroup = new Group(tableComposite, SHADOW_IN);
workingSetGroup.setText("Associated working set projects");
workingSetGroup.setLayout(GridLayoutFactory.fillDefaults().create());
workingSetGroup.setLayoutData(fillDefaults().align(FILL, FILL).grab(true, true).create());
final TableViewer associatedProjectsViewer = new TableViewerBuilder(singletonList("")).setHasBorder(true).setHeaderVisible(false).setLinesVisible(false).setMultipleSelection(true).setColumnWidthsInPixel(Ints.asList(350)).setLabelProvider(labelProvider).build(workingSetGroup);
addButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = allProjectsViewer.getStructuredSelection();
if (selection != null && !selection.isEmpty()) {
final IProject[] projects = Arrays2.filter(selection.toArray(), IProject.class);
allProjectsViewer.remove(projects);
associatedProjectsViewer.add(projects);
workspaceProjects.removeAll(Arrays.asList(projects));
workingSetProjects.addAll(Arrays.asList(projects));
setPageComplete(validatePage());
}
}
});
removeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = associatedProjectsViewer.getStructuredSelection();
if (selection != null && !selection.isEmpty()) {
final IProject[] projects = Arrays2.filter(selection.toArray(), IProject.class);
associatedProjectsViewer.remove(projects);
allProjectsViewer.add(projects);
workingSetProjects.removeAll(Arrays.asList(projects));
workspaceProjects.addAll(Arrays.asList(projects));
setPageComplete(validatePage());
}
}
});
associatedProjectsViewer.addSelectionChangedListener(event -> {
final IStructuredSelection selection = associatedProjectsViewer.getStructuredSelection();
removeButton.setEnabled(null != selection && !selection.isEmpty());
});
allProjectsViewer.addSelectionChangedListener(event -> {
final IStructuredSelection selection = allProjectsViewer.getStructuredSelection();
addButton.setEnabled(null != selection && !selection.isEmpty());
});
setPageComplete(false);
setControl(composite);
final Optional<WorkingSet> editedWorkingSet = getEditedWorkingSet();
workspaceProjects.addAll(Arrays.asList(ResourcesPlugin.getWorkspace().getRoot().getProjects()));
if (editedWorkingSet.isPresent()) {
final ManualAssociationWorkingSet workingSet = (ManualAssociationWorkingSet) editedWorkingSet.get();
workingSetRef.set(workingSet);
nameText.setText(workingSet.getName());
nameText.selectAll();
workingSetProjects.addAll(workingSet.getAssociatedProjects());
workspaceProjects.removeAll(workingSetProjects);
originalName.set(workingSet.getName());
}
composite.getDisplay().asyncExec(() -> {
setTitle(TITLE);
setDescription(DESCRIPTION);
allProjectsViewer.setInput(workspaceProjects);
associatedProjectsViewer.setInput(workingSetProjects);
});
nameText.addModifyListener(e -> setPageComplete(validatePage()));
}
@Override
public void setVisible(boolean visible) {
if (visible) {
Rectangle location = UIUtils.getConstrainedShellBounds(getShell(), SHELL_SIZE);
getShell().setBounds(location);
}
super.setVisible(visible);
}
@SuppressWarnings("null")
private boolean validatePage() {
String errorMessage = null;
final String name = nameText.getText();
final WorkingSetManager manager = getManager();
if (manager == null) {
errorMessage = "No active working set manager is available.";
}
if (errorMessage == null) {
if (name == null || name.trim().length() == 0) {
errorMessage = "Working set name should be specified.";
}
}
if (errorMessage == null) {
if (!name.equals(originalName.get()) && // This case ID and name are equal. Intentionally name.
getAllWorkingSets().stream().anyMatch(ws -> ws.getName().equals(name))) {
errorMessage = "A working set already exists with name '" + name + "'.";
}
}
if (errorMessage != null) {
workingSetRef.set(null);
} else {
final Iterable<String> projectNames = from(workingSetProjects).transform(p -> p.getName());
workingSetRef.set(new ManualAssociationWorkingSet(projectNames, name, manager));
}
setMessage(errorMessage, ERROR);
return errorMessage == null;
}
});
}
use of org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet 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;
}
use of org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet in project n4js by eclipse.
the class AddWorkingSetModificationStrategy method execute.
@Override
public void execute(final WorkingSetManager manager) {
if (toAdd != null && RESOURCE_WORKING_SET_ID.equals(toAdd.getId())) {
final String name = toAdd.getName();
// Working set with the given name already exists, nothing to do.
if (getWorkingSetByName(manager, name) != null) {
return;
}
final IProject[] projects = Arrays2.filter(toAdd.getElements(), IProject.class);
final List<String> projectNames = Arrays2.transform(projects, p -> p.getName());
final WorkingSet newWorkingSet = new ManualAssociationWorkingSet(projectNames, name, manager);
final WorkingSetDiffBuilder builder = new WorkingSetDiffBuilder(manager);
builder.add(newWorkingSet);
final List<WorkingSet> newItems = newArrayList(manager.getWorkingSets());
if (newItemsIndex == APPEND_TO_END_INDEX) {
newItems.add(newWorkingSet);
} else if (newItemsIndex >= 0) {
newItems.add(newItemsIndex, newWorkingSet);
}
final List<WorkingSet> newAllItems = newArrayList(manager.getAllWorkingSets());
if (newAllItemsIndex == APPEND_TO_END_INDEX) {
newAllItems.add(newWorkingSet);
} else if (newAllItemsIndex >= 0) {
newAllItems.add(newAllItemsIndex, newWorkingSet);
}
final Diff<WorkingSet> diff = builder.build(newItems, newAllItems);
updateAndSaveState(manager, diff);
}
}
use of org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet 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;
}
use of org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet 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());
}
}
}
Aggregations