Example usage for org.eclipse.jface.viewers IStructuredSelection isEmpty

List of usage examples for org.eclipse.jface.viewers IStructuredSelection isEmpty

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers IStructuredSelection isEmpty.

Prototype

public boolean isEmpty();

Source Link

Document

Returns whether this selection is empty.

Usage

From source file:com.liferay.ide.kaleo.ui.navigator.WorkflowDefinitionsActionProvider.java

License:Open Source License

public void fillContextMenu(IMenuManager menu) {
    // This is a temp workaround to clean up the default group that are provided by CNF
    menu.removeAll();// w w  w .  j  a v  a 2s. c  om

    ICommonViewerSite site = actionSite.getViewSite();
    IStructuredSelection selection = null;

    if (site instanceof ICommonViewerWorkbenchSite) {
        ICommonViewerWorkbenchSite wsSite = (ICommonViewerWorkbenchSite) site;
        selection = (IStructuredSelection) wsSite.getSelectionProvider().getSelection();
    }

    WorkflowDefinitionEntry definition = null;
    WorkflowDefinitionsFolder definitionsFolder = null;

    if (selection != null && !selection.isEmpty()) {
        Iterator<?> iterator = selection.iterator();
        Object obj = iterator.next();

        if (obj instanceof WorkflowDefinitionEntry) {
            definition = (WorkflowDefinitionEntry) obj;
        }

        if (obj instanceof WorkflowDefinitionsFolder) {
            definitionsFolder = (WorkflowDefinitionsFolder) obj;
        }

        if (iterator.hasNext()) {
            definition = null;
            definitionsFolder = null;
        }
    }

    menu.add(invisibleSeparator(TOP_SECTION_START_SEPARATOR));
    addTopSection(menu, definition, definitionsFolder);
    menu.add(invisibleSeparator(TOP_SECTION_END_SEPARATOR));
    menu.add(new Separator());

    menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end"));
}

From source file:com.liferay.ide.project.ui.migration.AutoCorrectAction.java

License:Open Source License

@Override
public void selectionChanged(IStructuredSelection selection) {
    if (selection.isEmpty()) {
        setEnabled(false);//from  ww  w  . jav a 2  s  . c  o m
    } else {
        boolean selectionCompatible = true;

        Iterator<?> items = selection.iterator();
        Object lastItem = null;

        while (items.hasNext()) {
            Object item = items.next();

            if (!(item instanceof Problem)) {
                selectionCompatible = false;
                break;
            }

            Problem problem = (Problem) item;

            if (problem.autoCorrectContext == null) {
                selectionCompatible = false;
                break;
            }
            if (lastItem != null) {
                String prCurrentKey = ((Problem) item).autoCorrectContext.substring(0,
                        problem.autoCorrectContext.indexOf(":"));
                ;
                String prLastKey = ((Problem) lastItem).autoCorrectContext.substring(0,
                        problem.autoCorrectContext.indexOf(":"));

                if (!(prCurrentKey.equals(prLastKey))) {
                    selectionCompatible = false;
                    break;
                }
            }

            lastItem = item;
        }

        Iterator<?> items2 = selection.iterator();

        List<String> autoCorrectContexts = new ArrayList<>();

        while (items2.hasNext()) {
            Object item = items2.next();

            if (item instanceof Problem && ((Problem) item).autoCorrectContext != null) {
                autoCorrectContexts.add(((Problem) item).autoCorrectContext);
            }
        }

        setEnabled(selectionCompatible);

        List<String> allAutoCorrectContexts = new ArrayList<>();

        if (_provider instanceof TableViewer) {
            TableViewer viewer = (TableViewer) _provider;
            Object obj = viewer.getInput();
            if (obj instanceof Object[]) {
                Object[] problems = (Object[]) obj;

                for (Object o : problems) {
                    if (o instanceof Problem && ((Problem) o).autoCorrectContext != null) {
                        allAutoCorrectContexts.add(((Problem) o).autoCorrectContext);
                    }
                }
            } else if (obj instanceof List<?>) {
                List<?> list = (List<?>) obj;

                for (Object p : list) {
                    if (p instanceof Problem) {
                        Problem problem = (Problem) p;

                        if (problem.autoCorrectContext != null) {
                            allAutoCorrectContexts.add(problem.autoCorrectContext);
                        }
                    }
                }
            }
        }

        setText("Correct automatically");
    }
}

From source file:com.liferay.ide.project.ui.modules.fragment.NewModuleFragmentFilesWizard.java

License:Open Source License

@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
    if (selection != null && !selection.isEmpty()) {
        final Object element = selection.getFirstElement();

        if (element instanceof IResource) {
            initialProject = ((IResource) element).getProject();
        } else if (element instanceof IJavaProject) {
            initialProject = ((IJavaProject) element).getProject();
        } else if (element instanceof IPackageFragment) {
            initialProject = ((IJavaElement) element).getResource().getProject();
        } else if (element instanceof IJavaElement) {
            initialProject = ((IJavaElement) element).getResource().getProject();
        }/*  w w w  .  j av  a 2 s  .c om*/

        if (initialProject != null) {
            final IBundleProject bundleProject = LiferayCore.create(IBundleProject.class, initialProject);

            if (bundleProject != null && bundleProject.isFragmentBundle()) {
                element().setProjectName(initialProject.getName());
            }
        }
    }
}

From source file:com.liferay.ide.project.ui.modules.NewLiferayComponentWizard.java

License:Open Source License

@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
    if (selection != null && !selection.isEmpty()) {
        final Object element = selection.getFirstElement();

        if (element instanceof IResource) {
            initialProject = ((IResource) element).getProject();
        } else if (element instanceof IJavaProject) {
            initialProject = ((IJavaProject) element).getProject();
        } else if (element instanceof IPackageFragment) {
            initialPackage = ((IPackageFragment) element);
            initialProject = ((IJavaElement) element).getResource().getProject();
        } else if (element instanceof IPackageFragmentRoot) {
            initialPackageRoot = (IPackageFragmentRoot) element;
            initialProject = initialPackageRoot.getJavaProject().getProject();
        } else if (element instanceof IJavaElement) {
            initialProject = ((IJavaElement) element).getResource().getProject();
        }/*from   ww w. j a v a  2  s.co m*/

        if (initialProject != null) {
            final IBundleProject bundleProject = LiferayCore.create(IBundleProject.class, initialProject);

            if (bundleProject != null && "jar".equals(bundleProject.getBundleShape())
                    && !bundleProject.isFragmentBundle()) {
                element().setProjectName(initialProject.getName());

                if (initialPackage != null) {
                    element().setPackageName(initialPackage.getElementName());
                }
            }
        }
    }
}

From source file:com.liferay.ide.project.ui.upgrade.animated.SummaryPage.java

License:Open Source License

public SummaryPage(Composite parent, int style, LiferayUpgradeDataModel dataModel) {
    super(parent, style, dataModel, SUMMARY_PAGE_ID, false);

    Composite container = new Composite(this, SWT.NONE);
    container.setLayout(new GridLayout(2, false));
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    tableViewer = new TableViewer(container);
    tableViewer.setContentProvider(new TableViewContentProvider());
    tableViewer.setLabelProvider(new TableViewLabelProvider());
    tableViewer.getControl().setBackground(this.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override/*from w w w  .j av  a  2  s  . com*/
        public void selectionChanged(SelectionChangedEvent event) {
            final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            if (!selection.isEmpty()) {
                if (selection.getFirstElement() instanceof TableViewElement) {
                    final TableViewElement tableViewElement = (TableViewElement) selection.getFirstElement();
                    final int pageIndex = tableViewElement.pageIndex;

                    PageNavigateEvent navEvent = new PageNavigateEvent();
                    navEvent.setTargetPage(pageIndex);

                    for (PageNavigatorListener listener : naviListeners) {
                        listener.onPageNavigate(navEvent);
                    }
                }
            }
        }
    });

    final Table table = tableViewer.getTable();
    final GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
    tableData.heightHint = 175;
    table.setLayoutData(tableData);
    table.setLinesVisible(false);

    createImages();
}

From source file:com.liferay.ide.server.ui.action.EditPropertiesFileAction.java

License:Open Source License

/**
 * Update the enabled state.//w w w  .j  av a 2 s. c o m
 *
 * @param sel
 *            a selection
 */
@SuppressWarnings("rawtypes")
public void selectionChanged(IStructuredSelection sel) {
    if (sel.isEmpty()) {
        setEnabled(false);
        return;
    }

    boolean enabled = false;
    Iterator iterator = sel.iterator();

    while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (obj instanceof PropertiesFile) {
            final PropertiesFile node = (PropertiesFile) obj;

            if (accept(node)) {
                enabled = true;
            }
        } else {
            setEnabled(false);
            return;
        }
    }

    setEnabled(enabled);
}

From source file:com.liferay.ide.server.ui.action.OpenLiferayHomeFolderAction.java

License:Open Source License

/**
 * Update the enabled state.//from  w  w w .  j av a  2s  .c  o m
 *
 * @param sel
 *            a selection
 */
@SuppressWarnings("rawtypes")
public void selectionChanged(IStructuredSelection sel) {
    if (sel.isEmpty()) {
        setEnabled(false);

        return;
    }

    boolean enabled = false;
    Iterator iterator = sel.iterator();

    while (iterator.hasNext()) {
        Object obj = iterator.next();

        if (obj instanceof PropertiesFile) {
            final PropertiesFile node = (PropertiesFile) obj;

            final String path = node.getPath();

            try {
                if (!CoreUtil.isNullOrEmpty(ServerUIUtil.getSystemExplorerCommand(new Path(path).toFile()))
                        && accept(node)) {
                    enabled = true;
                }
            } catch (IOException e) {
            }
        } else {
            setEnabled(false);

            return;
        }
    }

    setEnabled(enabled);
}

From source file:com.liferay.ide.server.ui.navigator.PropertiesActionProvider.java

License:Open Source License

public void fillContextMenu(IMenuManager menu) {
    // This is a temp workaround to clean up the default group that are provided by CNF
    menu.removeAll();//from   w w w  .j ava 2s  . c  om

    ICommonViewerSite site = actionSite.getViewSite();
    IStructuredSelection selection = null;

    if (site instanceof ICommonViewerWorkbenchSite) {
        ICommonViewerWorkbenchSite wsSite = (ICommonViewerWorkbenchSite) site;
        selection = (IStructuredSelection) wsSite.getSelectionProvider().getSelection();
    }

    PropertiesFile file = null;

    if (selection != null && !selection.isEmpty()) {
        Iterator<?> iterator = selection.iterator();
        Object obj = iterator.next();

        if (obj instanceof PropertiesFile) {
            file = (PropertiesFile) obj;
        }

        if (iterator.hasNext()) {
            file = null;
        }
    }

    menu.add(invisibleSeparator(TOP_SECTION_START_SEPARATOR));
    addTopSection(menu, file);
    menu.add(invisibleSeparator(TOP_SECTION_END_SEPARATOR));
    menu.add(new Separator());

    menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end"));
}

From source file:com.maccasoft.composer.MusicEditor.java

License:Open Source License

void createHeader(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout(12, false);
    gridLayout.marginWidth = gridLayout.marginHeight = 0;
    composite.setLayout(gridLayout);/* w w  w.ja va 2 s .com*/
    composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    Label label = new Label(composite, SWT.NONE);
    label.setText("Song");

    songsCombo = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.NO_FOCUS);
    songsCombo.getCombo().setVisibleItemCount(20);
    songsCombo.getCombo()
            .setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 30), SWT.DEFAULT));
    songsCombo.setContentProvider(new ObservableListContentProvider());
    songsCombo.setLabelProvider(new LabelProvider());

    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.NO_FOCUS);

    edit = new ToolItem(toolBar, SWT.PUSH);
    edit.setImage(ImageRegistry.getImageFromResources("application_edit.png"));
    edit.setToolTipText("Rename");
    edit.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IInputValidator validator = new IInputValidator() {

                @Override
                public String isValid(String newText) {
                    if (newText.length() == 0) {
                        return "";
                    }
                    for (Song song : project.getSongs()) {
                        if (song != currentSong && newText.equalsIgnoreCase(song.getName())) {
                            return "A song with the same title already exists";
                        }
                    }
                    return null;
                }
            };
            InputDialog dlg = new InputDialog(shell, "Rename Song", "Title:", currentSong.getName(), validator);
            if (dlg.open() == InputDialog.OK) {
                currentSong.setName(dlg.getValue());
                songsCombo.refresh();
            }
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    play = new ToolItem(toolBar, SWT.PUSH);
    play.setImage(ImageRegistry.getImageFromResources("control_play_blue.png"));
    play.setToolTipText("Play");
    play.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Runnable playThread = new Runnable() {

                @Override
                public void run() {
                    try {
                        ProjectCompiler compiler = new ProjectCompiler(project);
                        Music music = compiler.build(currentSong);
                        byte[] data = music.toArray();
                        try {
                            serialPort.writeInt('P');
                            serialPort.writeInt(data.length & 0xFF);
                            serialPort.writeInt((data.length >> 8) & 0xFF);
                            serialPort.writeBytes(data);
                        } catch (SerialPortException e) {
                            e.printStackTrace();
                        }
                    } catch (final ProjectException ex) {
                        Display.getDefault().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                if (shell.isDisposed()) {
                                    return;
                                }
                                MessageDialog.openError(shell, Main.APP_TITLE,
                                        "An error occurred while compiling song:\r\n\r\n" + ex.getMessage());
                                focusOnErrorCell(ex);
                            }
                        });
                    }
                }
            };
            new Thread(playThread).start();
        }
    });

    stop = new ToolItem(toolBar, SWT.PUSH);
    stop.setImage(ImageRegistry.getImageFromResources("control_stop_blue.png"));
    stop.setToolTipText("Stop");
    stop.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                serialPort.writeInt('0');
            } catch (SerialPortException ex) {
                ex.printStackTrace();
            }
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    delete = new ToolItem(toolBar, SWT.PUSH);
    delete.setImage(ImageRegistry.getImageFromResources("application_delete.png"));
    delete.setToolTipText("Delete song");
    delete.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = songsCombo.getStructuredSelection();
            if (selection.isEmpty()) {
                return;
            }
            if (!MessageDialog.openConfirm(shell, Main.APP_TITLE, "You really want to delete this song?")) {
                return;
            }
            int index = project.getSongs().indexOf(selection.getFirstElement());
            if (index > 0) {
                songsCombo.setSelection(new StructuredSelection(project.getSong(index - 1)));
            } else {
                songsCombo.setSelection(new StructuredSelection(project.getSong(index + 1)));
            }
            project.getObservableSongs().remove(selection.getFirstElement());
            delete.setEnabled(currentSong != null && project.getSongs().size() > 1);
        }
    });

    label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    label = new Label(composite, SWT.NONE);
    label.setText("BPM");

    bpm = new Spinner(composite, SWT.BORDER | SWT.NO_FOCUS);
    bpm.setValues(120, 0, 9999, 0, 1, 1);
    bpm.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 5), SWT.DEFAULT));
    bpm.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (currentSong != null) {
                currentSong.setBpm(bpm.getSelection());
            }
        }
    });

    label = new Label(composite, SWT.NONE);
    label.setText("Rows");

    rows = new Spinner(composite, SWT.BORDER | SWT.NO_FOCUS);
    rows.setValues(0, 0, 9999, 0, 1, 1);
    rows.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 5), SWT.DEFAULT));
    rows.setEnabled(false);
    rows.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (currentSong == null) {
                return;
            }
            int totalRows = rows.getSelection();
            while (currentSong.getObservableRows().size() > totalRows) {
                currentSong.getObservableRows().remove(currentSong.getObservableRows().size() - 1);
            }
            while (currentSong.getObservableRows().size() < totalRows) {
                currentSong.getObservableRows().add(new SongRow());
            }
        }
    });

    label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    label = new Label(composite, SWT.NONE);
    label.setText("Octave");

    octave = new Spinner(composite, SWT.BORDER | SWT.NO_FOCUS);
    octave.setValues(3, 1, 9, 0, 1, 1);
    octave.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 3), SWT.DEFAULT));
    octave.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            viewer.getControl().setFocus();
        }
    });

    instrumentToolBar = new InstrumentToolBar(composite);

    songsCombo.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            currentSong = (Song) selection.getFirstElement();
            updateSongView();
        }
    });
}

From source file:com.maccasoft.composer.NoteKeyListener.java

License:Open Source License

@Override
public void keyPressed(KeyEvent e) {
    IStructuredSelection selection = viewer.getStructuredSelection();
    if (selection.isEmpty()) {
        return;/*from  w ww.j a  v  a2  s . co m*/
    }

    SongRow model = (SongRow) selection.getFirstElement();

    final ViewerCell cell = viewer.getColumnViewerEditor().getFocusCell();
    int channel = cell.getColumnIndex() / 4;
    int columnIndex = cell.getColumnIndex() % 4;

    if (columnIndex == 0) {
        String s = noteMap.get(e.character);
        if (s != null) {
            model.setNote(channel, s + String.valueOf(getOctave()));
            if ("".equals(model.getInstrument(channel))) {
                model.setInstrument(channel, getInstrument());
            }
            viewer.update(model, null);

            final ViewerCell nextCell = cell.getNeighbor(ViewerCell.BELOW, false);
            if (nextCell != null) {
                final Event event1 = new Event();
                event1.type = SWT.KeyDown;
                event1.keyCode = SWT.ARROW_DOWN;
                event1.widget = e.widget;
                final Event event2 = new Event();
                event2.type = SWT.KeyUp;
                event2.keyCode = SWT.ARROW_DOWN;
                event2.widget = e.widget;

                final Display display = e.display;
                display.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (event1.widget.isDisposed()) {
                            return;
                        }
                        display.post(event1);
                        display.post(event2);
                    }
                });
            }
            e.doit = false;
            return;
        }
    }
    if (e.character == SWT.DEL) {
        if ((e.stateMask & SWT.MOD2) != 0) {
            removeEntryAndShiftUp(channel, model);
            viewer.refresh();
            e.doit = false;
            return;
        }
        switch (columnIndex) {
        case 0:
            model.setNote(channel, "");
            break;
        case 1:
            model.setInstrument(channel, "");
            break;
        case 2:
            model.setFx1(channel, "");
            break;
        case 3:
            model.setFx2(channel, "");
            break;
        }
        viewer.update(model, null);
        e.doit = false;
        return;
    }
    if (e.keyCode == SWT.INSERT) {
        insertBlankAndShiftDown(channel, model);
        viewer.refresh();
        e.doit = false;
        return;
    }
    if (e.character >= 0x20 && e.character <= 0x7F) {
        e.doit = false;
        return;
    }
}