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

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

Introduction

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

Prototype

public Object[] toArray();

Source Link

Document

Returns the elements in this selection as an array.

Usage

From source file:net.bioclipse.opentox.prefs.ServicesPreferencePage.java

License:Open Source License

public Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    setSize(new Point(600, 420));
    container.setSize(600, 420);//from  ww  w.  ja v a 2s. co m
    container.setLayout(new FormLayout());

    //      checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER);
    checkboxTableViewer = new TableViewer(container, SWT.BORDER | SWT.SINGLE);
    checkboxTableViewer.setContentProvider(new ApplicationsContentProvider());
    checkboxTableViewer.setLabelProvider(new ApplicationsLabelProvider());
    final Table table = checkboxTableViewer.getTable();
    FormData formData = new FormData(700, 300);
    formData.left = new FormAttachment(0, 11);
    formData.top = new FormAttachment(0, 10);
    table.setLayoutData(formData);

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
    tableColumn.setText("Name");
    tableColumn.setWidth(100);
    TableColumn tableColumn2 = new TableColumn(table, SWT.LEFT);
    tableColumn2.setText("Service");
    tableColumn2.setWidth(300);
    TableColumn tableColumn3 = new TableColumn(table, SWT.LEFT);
    tableColumn3.setText("ServiceSPARQL");
    tableColumn3.setWidth(300);

    appList = getPreferencesFromStore();
    checkboxTableViewer.setInput(appList);

    final Button addButton = new Button(container, SWT.NONE);
    formData.right = new FormAttachment(100, -89);
    FormData formData_1 = new FormData();
    formData_1.right = new FormAttachment(100, -9);
    formData_1.left = new FormAttachment(table, 6);
    addButton.setLayoutData(formData_1);
    addButton.setText("Add");
    addButton.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {

            ServicesEditDialog dlg = new ServicesEditDialog(getShell());
            dlg.open();

            String[] ret = dlg.getServiceInfo();
            if (ret.length == 3) {
                appList.add(ret);
                checkboxTableViewer.refresh();
            }
        }
    });

    final Button editButton = new Button(container, SWT.NONE);
    FormData formData_2 = new FormData();
    formData_2.top = new FormAttachment(table, 0, SWT.TOP);
    formData_2.left = new FormAttachment(table, 6);
    formData_2.right = new FormAttachment(100, -9);
    editButton.setLayoutData(formData_2);
    editButton.setText("Edit");
    editButton.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {

            //Get selection from viewer
            ISelection sel = checkboxTableViewer.getSelection();
            if (!(sel instanceof IStructuredSelection)) {
                logger.debug("Item of wrong type selected.");
                showMessage("Please select an entry to edit first.");
                return;
            }

            IStructuredSelection ssel = (IStructuredSelection) sel;
            Object obj = ssel.getFirstElement();

            if (!(obj instanceof String[])) {
                logger.debug("Object of wrong type selected.");
                showMessage("Please select an entry to edit first.");
                return;
            }

            String[] chosen = (String[]) obj;
            /* TODO Fix this in a better way. Without the if-statement below
             * we'll get an unexpected ArrayIndexOutOfBoundsException in 
             * some cases, 'cos the last element is missing in the selection */
            if (chosen.length < 3) {
                for (int i = chosen.length; i < 3; i++) {
                    chosen[i] = "NA";
                }
            }

            ServicesEditDialog dlg = new ServicesEditDialog(getShell(), chosen[0], chosen[1], chosen[2]);

            dlg.open();

            String[] ret = dlg.getServiceInfo();
            //If OK pressed
            if (dlg.getReturnCode() == 0) {
                if (ret.length == 3) {
                    chosen[0] = ret[0]; //name
                    chosen[1] = ret[1]; //service
                    chosen[2] = ret[2]; //serviceSPARQL
                    checkboxTableViewer.refresh();
                } else {
                    logger.debug("Error getting result from dialog!");
                    showMessage("Error getting result from dialog.");
                }
            }

        }
    });

    final Button removeButton = new Button(container, SWT.NONE);
    formData_1.top = new FormAttachment(removeButton, 6);
    FormData formData_3 = new FormData();
    formData_3.right = new FormAttachment(100, -9);
    formData_3.left = new FormAttachment(table, 6);
    formData_3.top = new FormAttachment(0, 46);
    removeButton.setLayoutData(formData_3);
    removeButton.setText("Remove");
    removeButton.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {

            //Get selection from viewer
            if (checkboxTableViewer.getSelection() instanceof IStructuredSelection) {
                IStructuredSelection selection = (IStructuredSelection) checkboxTableViewer.getSelection();
                Object[] objSelection = selection.toArray();

                for (int i = 0; i < objSelection.length; i++) {
                    if (objSelection[i] instanceof String[]) {
                        String[] row = (String[]) objSelection[i];
                        if (appList.contains(row)) {
                            appList.remove(row);
                        }
                    }
                }
                checkboxTableViewer.refresh();
            }

        }
    });

    if (table.getItemCount() > 0)
        table.setSelection(0);
    container.pack();
    parent.pack();
    return container;
}

From source file:net.bioclipse.ui.prefs.UpdateSitesPreferencePage.java

License:Open Source License

public Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    setSize(new Point(600, 420));
    container.setSize(600, 420);/*from  www  .ja  v  a 2  s.c  o m*/
    container.setLayout(new FormLayout());

    //      checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER);
    checkboxTableViewer = new TableViewer(container, SWT.BORDER | SWT.SINGLE);
    checkboxTableViewer.setContentProvider(new ApplicationsContentProvider());
    checkboxTableViewer.setLabelProvider(new ApplicationsLabelProvider());
    final Table table = checkboxTableViewer.getTable();
    FormData formData = new FormData();
    formData.left = new FormAttachment(0, 11);
    formData.top = new FormAttachment(0, 10);
    table.setLayoutData(formData);

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
    tableColumn.setText("Name");
    tableColumn.setWidth(180);
    TableColumn tableColumn2 = new TableColumn(table, SWT.LEFT);
    tableColumn2.setText("URL");
    tableColumn2.setWidth(270);

    appList = getPreferencesFromStore();
    checkboxTableViewer.setInput(appList);

    final Button addButton = new Button(container, SWT.NONE);
    formData.right = new FormAttachment(100, -89);
    FormData formData_1 = new FormData();
    formData_1.right = new FormAttachment(100, -9);
    formData_1.left = new FormAttachment(table, 6);
    addButton.setLayoutData(formData_1);
    addButton.setText("Add");
    addButton.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {

            UpdateSitesEditDialog dlg = new UpdateSitesEditDialog(getShell());
            dlg.open();

            String[] ret = dlg.getUpdateSites();
            if (ret.length == 2) {
                appList.add(ret);
                checkboxTableViewer.refresh();
            }
        }
    });

    final Button editButton = new Button(container, SWT.NONE);
    FormData formData_2 = new FormData();
    formData_2.top = new FormAttachment(table, 0, SWT.TOP);
    formData_2.left = new FormAttachment(table, 6);
    formData_2.right = new FormAttachment(100, -9);
    editButton.setLayoutData(formData_2);
    editButton.setText("Edit");
    editButton.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {

            //Get selection from viewer
            ISelection sel = checkboxTableViewer.getSelection();
            if (!(sel instanceof IStructuredSelection)) {
                logger.debug("Item of wrong type selected.");
                showMessage("Please select an entry to edit first.");
                return;
            }

            IStructuredSelection ssel = (IStructuredSelection) sel;
            Object obj = ssel.getFirstElement();

            if (!(obj instanceof String[])) {
                logger.debug("Object of wrong type selected.");
                showMessage("Please select an entry to edit first.");
                return;
            }

            String[] chosen = (String[]) obj;
            //            logger.debug("(:) " + chosen[0]);

            UpdateSitesEditDialog dlg = new UpdateSitesEditDialog(getShell(), chosen[0], chosen[1]);

            dlg.open();

            String[] ret = dlg.getUpdateSites();
            //If OK pressed
            if (dlg.getReturnCode() == 0) {
                if (ret.length == 2) {
                    chosen[0] = ret[0]; //ext
                    chosen[1] = ret[1]; //url
                    checkboxTableViewer.refresh();
                } else {
                    logger.debug("Error getting result from dialog!");
                    showMessage("Error getting result from dialog.");
                }
            }

        }
    });

    final Button removeButton = new Button(container, SWT.NONE);
    formData_1.top = new FormAttachment(removeButton, 6);
    FormData formData_3 = new FormData();
    formData_3.right = new FormAttachment(100, -9);
    formData_3.left = new FormAttachment(table, 6);
    formData_3.top = new FormAttachment(0, 46);
    removeButton.setLayoutData(formData_3);
    removeButton.setText("Remove");
    removeButton.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {

            //Get selection from viewer
            if (checkboxTableViewer.getSelection() instanceof IStructuredSelection) {
                IStructuredSelection selection = (IStructuredSelection) checkboxTableViewer.getSelection();
                Object[] objSelection = selection.toArray();

                for (int i = 0; i < objSelection.length; i++) {
                    if (objSelection[i] instanceof String[]) {
                        String[] row = (String[]) objSelection[i];
                        if (appList.contains(row)) {
                            appList.remove(row);
                        }
                    }
                }
                checkboxTableViewer.refresh();
            }

        }
    });

    //Add remember button
    rememberButton = new Button(container, SWT.CHECK);
    formData.bottom = new FormAttachment(rememberButton, -6);
    FormData formData_4 = new FormData();
    formData_4.left = new FormAttachment(0, 10);
    rememberButton.setLayoutData(formData_4);
    rememberButton.setText("Run automatic updates on startup");

    //Add remember button
    openDialogButton = new Button(container, SWT.CHECK);
    formData_4.bottom = new FormAttachment(openDialogButton, -6);
    FormData formData_5 = new FormData();
    formData_5.bottom = new FormAttachment(100, -10);
    formData_5.left = new FormAttachment(0, 10);
    openDialogButton.setLayoutData(formData_5);
    openDialogButton.setText("Install automatic updates in background");

    //Sync with preference

    boolean skipUpdate = prefsStore.getBoolean(IPreferenceConstants.SKIP_UPDATE_ON_STARTUP);
    //        boolean skipUpdate=Activator.getDefault().getDialogSettings().getBoolean( 
    //              net.bioclipse.ui.dialogs.IDialogConstants.SKIP_UPDATE_ON_STARTUP );

    boolean skipDialog = prefsStore.getBoolean(IPreferenceConstants.SKIP_UPDATE_DIALOG_ON_STARTUP);
    //        boolean skipDialog=Activator.getDefault().getDialogSettings().getBoolean( 
    //                 net.bioclipse.ui.dialogs.IDialogConstants.SKIP_UPDATE_DIALOG_ON_STARTUP );

    logger.debug("Preference said SKIP_UPDATE_ON_STARTUP: " + skipUpdate);
    logger.debug("Preference said SKIP_UPDATE_DIALOG_ON_STARTUP: " + skipDialog);

    //Meaning is inverted
    rememberButton.setSelection(!skipUpdate);
    //Meaning is not inverted here :)
    openDialogButton.setSelection(skipDialog);

    if (table.getItemCount() > 0)
        table.setSelection(0);
    container.pack();
    parent.pack();
    return container;
}

From source file:net.enilink.komma.edit.ui.wizards.ObjectTypeSelectionPage.java

License:Open Source License

public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);

    composite.setLayout(new GridLayout(1, false));
    Tree tree = new Tree(composite, SWT.BORDER);

    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    tree.setLayoutData(gridData);/*w w  w  .  ja  v a2  s.c o m*/

    treeViewer = new TreeViewer(tree);
    treeViewer.setLabelProvider(treeLabelProvider);
    treeViewer.setContentProvider(treeContentProvider);
    treeViewer.setInput(treeInput);

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent e) {
            IStructuredSelection selection = (IStructuredSelection) e.getSelection();
            types = selection.toArray();
            setPageComplete(!selection.isEmpty());
        }
    });

    selectTypes();

    setControl(composite);
}

From source file:net.enilink.komma.edit.ui.wizards.RefactorRenameWizard.java

License:Open Source License

public RefactorRenameWizard(IEditingDomain domain, IWorkbench workbench,
        IStructuredSelection currentSelection) {
    this.domain = domain;
    this.workbench = workbench;
    this.currentSelection = currentSelection;

    this.renameMap = new LinkedHashMap<IObject, IReference>();
    for (Object object : currentSelection.toArray()) {
        if (object instanceof IObject) {
            renameMap.put((IObject) object, null);
        }// w w  w .j  av  a2 s .c o  m
    }

    setWindowTitle("Refactor - Rename content within models");
    setNeedsProgressMonitor(true);

    createPages();
}

From source file:net.sf.eclipsensis.template.AbstractTemplateSettings.java

License:Open Source License

@SuppressWarnings("unchecked")
private void edit() {
    IStructuredSelection selection = (IStructuredSelection) mTableViewer.getSelection();

    Object[] objects = selection.toArray();
    if (objects == null || objects.length != 1) {
        return;//from w ww.  j  a  va 2 s . com
    }

    T oldTemplate = (T) selection.getFirstElement();
    edit(oldTemplate);
}

From source file:net.sf.fjep.fatjar.wizards.export.FJExpWizard.java

License:Open Source License

public void init(IWorkbench workbench, IStructuredSelection selection) {
    Object[] jprojects = selection.toArray();
    selectedJavaProjects = new String[jprojects.length];
    for (int i = 0; i < jprojects.length; i++) {
        selectedJavaProjects[i] = ((JavaProject) jprojects[i]).getProject().getName();
    }//from  w w w . j  a va 2  s  . c o  m
}

From source file:net.sf.j2s.ui.property.J2SConfigPage.java

License:Open Source License

Object[] getSelection() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.size() >= 1) {
        return selection.toArray();
    } else {//from   w  ww  .  ja va2  s  . com
        return new Object[0];
    }
}

From source file:net.sf.sveditor.ui.explorer.AddNdocsHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection sel = HandlerUtil.getCurrentSelection(event);

    if (sel instanceof IStructuredSelection) {
        IStructuredSelection sel_s = (IStructuredSelection) sel;

        IFile file = null;//from w  w w. j  a  va2  s  .c om
        for (Object s : sel_s.toArray()) {

            if (s instanceof IFile) {
                file = (IFile) s;
            } else if (s instanceof IAdaptable) {
                file = (IFile) ((IAdaptable) s).getAdapter(IFile.class);
            }

            if (file == null) {
                continue;
            }
            try {
                // For better or worse... right now we are getting what is on the disk, not what is in the editor
                // TODO: Check if the editor is open and dirty.  If so, prompt for a save, and allow DB to rebuild before
                //       continuing
                InputStream is = file.getContents();
                StringBuilder sb = new StringBuilder();
                int ch;
                do {
                    ch = is.read();
                    if (ch != -1) {
                        sb.append((char) ch);
                    }
                } while (ch != -1);
                String contents = sb.toString();

                // Get the SVDB for this file
                SVDBFile svdbf = SVDBIndexUtil.findIndexFile(file);

                IDocCommentAdder dca = new DocCommentAdder(svdbf, SVDBIndexUtil.findIndexIterator(file), true);
                ArrayList<Tuple<Object, String>> fComments = dca.AddComments(-1);
                ArrayList<String> lines = new ArrayList<String>(Arrays.asList(contents.split("\n")));
                // Restore \n's
                for (int i = 0; i < lines.size(); i++) {
                    lines.set(i, lines.get(i) + "\n");
                }

                // Insert the comments as appropriate
                // Start at the last comment to be inserted and work backwards as this will change subsequent line numbers
                while (fComments.size() > 0) {
                    int highest_comment = -1;
                    int highest_index = -1;
                    String highest_string = "";

                    // Find the comment closest to the end of the file
                    for (int i = 0; i < fComments.size(); i++) {
                        Integer offset = (Integer) fComments.get(i).first();
                        if (offset > highest_comment) {
                            // Get comment and line number
                            highest_comment = offset;
                            highest_string = fComments.get(i).second();
                            highest_index = i;
                        }
                    }

                    // Check if we have a mismatch between what we currently have in the editor and what 
                    // we had on our last compile... if things don't match 
                    if (highest_comment > lines.size()) {
                        // TODO: The file has shrunk for some reason... need to re-compile here
                        break;
                    }
                    if (highest_string != "") {
                        // Figure out leading whitespace where we are going to insert comment
                        String leadingWS = lines.get(highest_comment - 1);
                        leadingWS = leadingWS.replaceAll("[a-zA-Z0-9_].*", "");
                        leadingWS = leadingWS.replaceAll("\n", "");
                        highest_string = leadingWS + highest_string.replaceAll("\n", "\n" + leadingWS);
                        highest_string = highest_string.substring(0,
                                highest_string.length() - leadingWS.length()); // Remove trailing whitespace
                        lines.add(highest_comment - 1, highest_string);
                    }
                    fComments.remove(highest_index);

                }

                sb.setLength(0);
                for (String line : lines) {
                    sb.append(line);
                }
                InputStream in_s = new StringInputStream(sb.toString());
                file.setContents(in_s, true, false, new NullProgressMonitor());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

From source file:net.sf.sveditor.ui.explorer.IndentHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection sel = HandlerUtil.getCurrentSelection(event);

    if (sel instanceof IStructuredSelection) {
        IStructuredSelection sel_s = (IStructuredSelection) sel;

        IFile file = null;// w  ww  .  j a v  a 2 s. c  om
        for (Object s : sel_s.toArray()) {

            if (s instanceof IFile) {
                file = (IFile) s;
            } else if (s instanceof IAdaptable) {
                file = (IFile) ((IAdaptable) s).getAdapter(IFile.class);
            }

            if (file == null) {
                break;
            }
            try {
                InputStream is = file.getContents();
                StringBuilder sb = new StringBuilder();
                int ch;
                do {
                    ch = is.read();
                    if (ch != -1) {
                        sb.append((char) ch);
                    }
                } while (ch != -1);
                String contents = sb.toString();
                SVIndentScanner scanner = new SVIndentScanner(new StringTextScanner(contents));
                ISVIndenter indenter = SVCorePlugin.getDefault().createIndenter();
                indenter.init(scanner);

                String result = indenter.indent(-1, -1);
                InputStream in_s = new StringInputStream(result);
                file.setContents(in_s, true, false, new NullProgressMonitor());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

From source file:net.sf.versiontree.views.CompareRevisionsAction.java

License:Open Source License

@Override
protected boolean updateSelection(IStructuredSelection selection) {
    this.selection = selection;
    Object[] selArray = selection.toArray();
    if (selection.size() == 1) {
        if (selArray[0] instanceof IRevision) {
            this.setText(NLS.bind(TeamUIMessages.CompareRevisionAction_Revision,
                    ((IRevision) selArray[0]).getRevision()));
        } else {/*from  w  w w . ja  v  a  2s.  c o m*/
            this.setText(TeamUIMessages.LocalHistoryPage_CompareAction);
        }
        return selArray[0] instanceof IRevision && localFile != null;
    } else if (selection.size() == 2) {
        setText(TeamUIMessages.CompareRevisionAction_CompareWithOther);
        return selArray[0] instanceof IRevision && selArray[1] instanceof IRevision;
    }

    return false;
}