Search in sources :

Example 1 with TableViewerBuilder

use of org.eclipse.n4js.ui.viewer.TableViewerBuilder 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;
        }
    });
}
Also used : Arrays(java.util.Arrays) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) TableViewer(org.eclipse.jface.viewers.TableViewer) Inject(com.google.inject.Inject) Rectangle(org.eclipse.swt.graphics.Rectangle) Arrays2(org.eclipse.n4js.utils.collections.Arrays2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Point(org.eclipse.swt.graphics.Point) Collections.singletonList(java.util.Collections.singletonList) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) IProject(org.eclipse.core.resources.IProject) Composite(org.eclipse.swt.widgets.Composite) Optional(com.google.common.base.Optional) FluentIterable.from(com.google.common.collect.FluentIterable.from) WizardPage(org.eclipse.jface.wizard.WizardPage) N4JSProjectExplorerLabelProvider(org.eclipse.n4js.ui.navigator.N4JSProjectExplorerLabelProvider) BORDER(org.eclipse.swt.SWT.BORDER) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) PUSH(org.eclipse.swt.SWT.PUSH) Button(org.eclipse.swt.widgets.Button) UIUtils(org.eclipse.n4js.ui.utils.UIUtils) TableViewerBuilder(org.eclipse.n4js.ui.viewer.TableViewerBuilder) Collection(java.util.Collection) Group(org.eclipse.swt.widgets.Group) Ints(com.google.common.primitives.Ints) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) CENTER(org.eclipse.swt.SWT.CENTER) FILL(org.eclipse.swt.SWT.FILL) GridDataFactory.fillDefaults(org.eclipse.jface.layout.GridDataFactory.fillDefaults) SHADOW_IN(org.eclipse.swt.SWT.SHADOW_IN) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Comparator(java.util.Comparator) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ImageRef(org.eclipse.n4js.ui.ImageDescriptorCache.ImageRef) Label(org.eclipse.swt.widgets.Label) Group(org.eclipse.swt.widgets.Group) Composite(org.eclipse.swt.widgets.Composite) Optional(com.google.common.base.Optional) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) Rectangle(org.eclipse.swt.graphics.Rectangle) Text(org.eclipse.swt.widgets.Text) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IProject(org.eclipse.core.resources.IProject) Button(org.eclipse.swt.widgets.Button) ManualAssociationWorkingSet(org.eclipse.n4js.ui.workingsets.ManualAssociationAwareWorkingSetManager.ManualAssociationWorkingSet) WizardPage(org.eclipse.jface.wizard.WizardPage) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) TableViewerBuilder(org.eclipse.n4js.ui.viewer.TableViewerBuilder)

Aggregations

Optional (com.google.common.base.Optional)1 FluentIterable.from (com.google.common.collect.FluentIterable.from)1 Sets.newTreeSet (com.google.common.collect.Sets.newTreeSet)1 Ints (com.google.common.primitives.Ints)1 Inject (com.google.inject.Inject)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 Comparator (java.util.Comparator)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IProject (org.eclipse.core.resources.IProject)1 ResourcesPlugin (org.eclipse.core.resources.ResourcesPlugin)1 GridDataFactory.fillDefaults (org.eclipse.jface.layout.GridDataFactory.fillDefaults)1 GridLayoutFactory (org.eclipse.jface.layout.GridLayoutFactory)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 WizardPage (org.eclipse.jface.wizard.WizardPage)1 ImageRef (org.eclipse.n4js.ui.ImageDescriptorCache.ImageRef)1 N4JSProjectExplorerLabelProvider (org.eclipse.n4js.ui.navigator.N4JSProjectExplorerLabelProvider)1 UIUtils (org.eclipse.n4js.ui.utils.UIUtils)1