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

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

Introduction

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

Prototype

public Object getFirstElement();

Source Link

Document

Returns the first element in this selection, or null if the selection is empty.

Usage

From source file:com.android.ide.eclipse.adt.internal.ui.ResourceExplorerView.java

License:Open Source License

@Override
public void createPartControl(Composite parent) {
    mTree = new Tree(parent, SWT.SINGLE | SWT.VIRTUAL);
    mTree.setLayoutData(new GridData(GridData.FILL_BOTH));
    mTree.setHeaderVisible(true);//  ww w. ja va 2  s  .c  om
    mTree.setLinesVisible(true);

    final IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore();

    // create 2 columns. The main one with the resources, and an "info" column.
    createTreeColumn(mTree, "Resources", SWT.LEFT, "abcdefghijklmnopqrstuvwxz", -1, PREFS_COLUMN_RES, store); //$NON-NLS-2$
    createTreeColumn(mTree, "Info", SWT.LEFT, "0123456789", -1, PREFS_COLUMN_2, store); //$NON-NLS-2$

    // create the jface wrapper
    mTreeViewer = new TreeViewer(mTree);

    mTreeViewer.setContentProvider(new ResourceContentProvider(true /* fullLevels */));
    mTreeViewer.setLabelProvider(new ResourceLabelProvider());

    // listen to selection change in the workbench.
    IWorkbenchPage page = getSite().getPage();

    page.addSelectionListener(this);

    // init with current selection
    selectionChanged(getSite().getPart(), page.getSelection());

    // add support for double click.
    mTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            ISelection sel = event.getSelection();

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

                if (selection.size() == 1) {
                    Object element = selection.getFirstElement();

                    // if it's a resourceFile, we directly open it.
                    if (element instanceof ResourceFile) {
                        try {
                            IAbstractFile iAbstractFile = ((ResourceFile) element).getFile();
                            if (iAbstractFile instanceof IFileWrapper) {
                                IDE.openEditor(getSite().getWorkbenchWindow().getActivePage(),
                                        ((IFileWrapper) iAbstractFile).getIFile());
                            }
                        } catch (PartInitException e) {
                        }
                    } else if (element instanceof ResourceItem) {
                        // if it's a ResourceItem, we open the first file, but only if
                        // there's no alternate files.
                        ResourceItem item = (ResourceItem) element;

                        if (item.isEditableDirectly()) {
                            ResourceFile[] files = item.getSourceFileArray();
                            if (files[0] != null) {
                                try {
                                    IAbstractFile iAbstractFile = files[0].getFile();
                                    if (iAbstractFile instanceof IFileWrapper) {
                                        IDE.openEditor(getSite().getWorkbenchWindow().getActivePage(),
                                                ((IFileWrapper) iAbstractFile).getIFile());
                                    }
                                } catch (PartInitException e) {
                                }
                            }
                        }
                    }
                }
            }
        }
    });

    // set up the resource manager to send us resource change notification
    AdtPlugin.getDefault().getResourceMonitor().addResourceEventListener(this);
}

From source file:com.android.ide.eclipse.adt.internal.wizards.actions.ExportAction.java

License:Open Source License

@Override
public void run(IAction action) {
    if (mSelection instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) mSelection;
        // get the unique selected item.
        if (selection.size() == 1) {
            Object element = selection.getFirstElement();

            // get the project object from it.
            IProject project = null;//from w  w  w.  j  a v a  2 s .  c o  m
            if (element instanceof IProject) {
                project = (IProject) element;
            } else if (element instanceof IAdaptable) {
                project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
            }

            // and finally do the action
            if (project != null) {
                if (!EclipseLintRunner.runLintOnExport(mShell, project)) {
                    return;
                }

                ProjectState state = Sdk.getProjectState(project);
                if (state.isLibrary()) {
                    MessageDialog.openError(mShell, "Android Export",
                            "Android library projects cannot be exported.");
                } else {
                    ExportHelper.exportUnsignedReleaseApk(project);
                }
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.internal.wizards.actions.ExportWizardAction.java

License:Open Source License

@Override
public void run(IAction action) {
    if (mSelection instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) mSelection;

        // get the unique selected item.
        if (selection.size() == 1) {
            Object element = selection.getFirstElement();

            // get the project object from it.
            IProject project = null;/*from  w w w . j  a  va 2s  .c  om*/
            if (element instanceof IProject) {
                project = (IProject) element;
            } else if (element instanceof IAdaptable) {
                project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
            }

            // and finally do the action
            if (project != null) {
                if (!EclipseLintRunner.runLintOnExport(mWorkbench.getActiveWorkbenchWindow().getShell(),
                        project)) {
                    return;
                }

                ProjectState state = Sdk.getProjectState(project);
                if (state.isLibrary()) {
                    MessageDialog.openError(mWorkbench.getDisplay().getActiveShell(), "Android Export",
                            "Android library projects cannot be exported.");
                } else {
                    // call the export wizard on the current selection.
                    ExportWizard wizard = new ExportWizard();
                    wizard.init(mWorkbench, selection);
                    WizardDialog dialog = new WizardDialog(mWorkbench.getDisplay().getActiveShell(), wizard);
                    dialog.open();
                }
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard.java

License:Open Source License

@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
    // get the project from the selection
    Object selected = selection.getFirstElement();

    if (selected instanceof IProject) {
        mProject = (IProject) selected;/*  w w w  .j a  v a  2  s  .c  o  m*/
    } else if (selected instanceof IAdaptable) {
        IResource r = (IResource) ((IAdaptable) selected).getAdapter(IResource.class);
        if (r != null) {
            mProject = r.getProject();
        }
    }
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.ConfirmationPage.java

License:Open Source License

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);//from  w  w w .  ja  v  a  2  s .c o m
    GridData data;

    Composite workArea = new Composite(parent, SWT.NONE);
    setControl(workArea);

    workArea.setLayout(new GridLayout());
    workArea.setLayoutData(
            new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));

    Label title = new Label(workArea, SWT.NONE);
    title.setText("Please review the export options.");

    Group group = new Group(workArea, SWT.NONE);
    group.setText("Project root");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    mProjectRootLabel = new Label(group, SWT.NONE);
    mProjectRootLabel.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    mProjectRootWarning = new Label(group, SWT.NONE);
    mProjectRootWarning.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    Group group2 = new Group(workArea, SWT.NONE);
    group2.setText("Exported Modules");
    group2.setLayout(new GridLayout());
    group2.setLayoutData(data = new GridData(SWT.FILL, SWT.FILL, true, true));
    data.heightHint = 300;

    Table table = new Table(group2, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    mTableViewer = new TableViewer(table);
    table.setLayout(new TableLayout());
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    mTableViewer.setContentProvider(new IStructuredContentProvider() {
        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof ProjectSetupBuilder) {
                ProjectSetupBuilder builder = (ProjectSetupBuilder) inputElement;
                Collection<GradleModule> modules = builder.getModules();
                Object[] array = new Object[modules.size()];
                int i = 0;
                for (GradleModule module : modules) {
                    array[i++] = module.getJavaProject();
                }

                return array;
            }

            return null;
        }

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

    });
    mTableViewer.setLabelProvider(new WorkbenchLabelProvider() {
        @Override
        protected String decorateText(String input, Object element) {
            if (element instanceof IJavaProject) {
                IJavaProject javaProject = (IJavaProject) element;
                StringBuilder sb = new StringBuilder(input);
                if (!mBuilder.isOriginalProject(javaProject)) {
                    sb.append('*');
                }
                // TODO: decorate icon instead?
                if (mOverrideProjects.contains(javaProject)) {
                    sb.append(" (1 warning)");
                }

                return sb.toString();
            }

            return input;
        }
    });
    mTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            Object firstElement = selection.getFirstElement();
            if (firstElement instanceof IJavaProject) {
                GradleModule module = mBuilder.getModule((IJavaProject) firstElement);
                if (mBuilder.getOriginalModules().contains(module)) {
                    mModuleDescription1.setText("Exported because selected in previous page.");
                } else {
                    List<GradleModule> list = mBuilder.getShortestDependencyTo(module);
                    StringBuilder sb = new StringBuilder();
                    for (GradleModule m : list) {
                        if (sb.length() > 0) {
                            sb.append(" > ");
                        }
                        sb.append(m.getJavaProject().getProject().getName());
                    }
                    mModuleDescription1.setText("Dependency chain: " + sb);
                }
                mModuleDescription2.setText("Path: " + module.getPath());

                if (mOverrideProjects.contains(module.getJavaProject())) {
                    mModuleDescription3.setText("WARNING: build.gradle already exists for this project");
                } else {
                    mModuleDescription3.setText("");
                }
            } else {
                mModuleDescription1.setText("");
                mModuleDescription2.setText("");
                mModuleDescription3.setText("");
            }
        }
    });

    mModuleDescription1 = new Label(group2, SWT.NONE);
    mModuleDescription1.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
    mModuleDescription2 = new Label(group2, SWT.NONE);
    mModuleDescription2.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
    mModuleDescription3 = new Label(group2, SWT.NONE);
    mModuleDescription3.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    mForceOverride = new Button(workArea, SWT.CHECK);
    mForceOverride.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
    mForceOverride.setText("Force overriding of existing files");
    mForceOverride.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            updateEnablement();
        }
    });

    setControl(workArea);
    Dialog.applyDialogFont(parent);
}

From source file:com.android.ide.eclipse.adt.launch.LaunchShortcut.java

License:Open Source License

public void launch(ISelection selection, String mode) {
    if (selection instanceof IStructuredSelection) {

        // get the object and the project from it
        IStructuredSelection structSelect = (IStructuredSelection) selection;
        Object o = structSelect.getFirstElement();

        // get the first (and normally only) element
        if (o instanceof IAdaptable) {
            IResource r = (IResource) ((IAdaptable) o).getAdapter(IResource.class);

            // get the project from the resource
            if (r != null) {
                IProject project = r.getProject();

                if (project != null) {
                    // and launch
                    launch(project, mode);
                }/*  w  w  w  .  ja  va 2 s  .  c  o m*/
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.ndk.internal.actions.AddNativeAction.java

License:Open Source License

public void run(IAction action) {
    IProject project = null;/*from   w ww .j ava  2  s . c  o  m*/
    if (mSelection instanceof IStructuredSelection) {
        IStructuredSelection ss = (IStructuredSelection) mSelection;
        if (ss.size() == 1) {
            Object obj = ss.getFirstElement();
            if (obj instanceof IProject) {
                project = (IProject) obj;
            } else if (obj instanceof PlatformObject) {
                project = (IProject) ((PlatformObject) obj).getAdapter(IProject.class);
            }
        }
    }

    if (project != null) {
        AddNativeWizard wizard = new AddNativeWizard(project, mPart.getSite().getWorkbenchWindow());
        WizardDialog dialog = new WizardDialog(mPart.getSite().getShell(), wizard);
        dialog.open();
    }

}

From source file:com.android.ide.eclipse.adt.project.export.ExportWizard.java

License:Open Source License

public void init(IWorkbench workbench, IStructuredSelection selection) {
    // get the project from the selection
    Object selected = selection.getFirstElement();

    if (selected instanceof IProject) {
        mProject = (IProject) selected;//from   www .  j av a2  s.c  om
    } else if (selected instanceof IAdaptable) {
        IResource r = (IResource) ((IAdaptable) selected).getAdapter(IResource.class);
        if (r != null) {
            mProject = r.getProject();
        }
    }
}

From source file:com.android.ide.eclipse.adt.project.ExportAction.java

License:Open Source License

public void run(IAction action) {
    if (mSelection instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) mSelection;
        // get the unique selected item.
        if (selection.size() == 1) {
            Object element = selection.getFirstElement();

            // get the project object from it.
            IProject project = null;/*from w  w w.  ja  v a2  s  .com*/
            if (element instanceof IProject) {
                project = (IProject) element;
            } else if (element instanceof IAdaptable) {
                project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
            }

            // and finally do the action
            if (project != null) {
                ExportHelper.exportProject(project);
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.ui.ConfigurationSelector.java

License:Open Source License

public ConfigurationSelector(Composite parent) {
    super(parent, SWT.NONE);

    mBaseConfiguration.createDefault();/*from   www.  j  a  va  2 s  .  c o m*/

    GridLayout gl = new GridLayout(4, false);
    gl.marginWidth = gl.marginHeight = 0;
    setLayout(gl);

    // first column is the first table
    final Table fullTable = new Table(this, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
    fullTable.setLayoutData(new GridData(GridData.FILL_BOTH));
    fullTable.setHeaderVisible(true);
    fullTable.setLinesVisible(true);

    // create the column
    final TableColumn fullTableColumn = new TableColumn(fullTable, SWT.LEFT);
    // set the header
    fullTableColumn.setText("Available Qualifiers");

    fullTable.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(ControlEvent e) {
            Rectangle r = fullTable.getClientArea();
            fullTableColumn.setWidth(r.width);
        }
    });

    mFullTableViewer = new TableViewer(fullTable);
    mFullTableViewer.setContentProvider(new QualifierContentProvider());
    mFullTableViewer.setLabelProvider(new QualifierLabelProvider(false /* showQualifierValue */));
    mFullTableViewer.setInput(mBaseConfiguration);
    mFullTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection structSelection = (IStructuredSelection) selection;
                Object first = structSelection.getFirstElement();

                if (first instanceof ResourceQualifier) {
                    mAddButton.setEnabled(true);
                    return;
                }
            }

            mAddButton.setEnabled(false);
        }
    });

    // 2nd column is the left/right arrow button
    Composite buttonComposite = new Composite(this, SWT.NONE);
    gl = new GridLayout(1, false);
    gl.marginWidth = gl.marginHeight = 0;
    buttonComposite.setLayout(gl);
    buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    new Composite(buttonComposite, SWT.NONE);
    mAddButton = new Button(buttonComposite, SWT.BORDER | SWT.PUSH);
    mAddButton.setText("->");
    mAddButton.setEnabled(false);
    mAddButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) mFullTableViewer.getSelection();

            Object first = selection.getFirstElement();
            if (first instanceof ResourceQualifier) {
                ResourceQualifier qualifier = (ResourceQualifier) first;

                mBaseConfiguration.removeQualifier(qualifier);
                mSelectedConfiguration.addQualifier(qualifier);

                mFullTableViewer.refresh();
                mSelectionTableViewer.refresh();
                mSelectionTableViewer.setSelection(new StructuredSelection(qualifier), true);

                onChange(false /* keepSelection */);
            }
        }
    });

    mRemoveButton = new Button(buttonComposite, SWT.BORDER | SWT.PUSH);
    mRemoveButton.setText("<-");
    mRemoveButton.setEnabled(false);
    mRemoveButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) mSelectionTableViewer.getSelection();

            Object first = selection.getFirstElement();
            if (first instanceof ResourceQualifier) {
                ResourceQualifier qualifier = (ResourceQualifier) first;

                mSelectedConfiguration.removeQualifier(qualifier);
                mBaseConfiguration.addQualifier(qualifier);

                mFullTableViewer.refresh();
                mSelectionTableViewer.refresh();

                onChange(false /* keepSelection */);
            }
        }
    });

    // 3rd column is the selected config table
    final Table selectionTable = new Table(this, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
    selectionTable.setLayoutData(new GridData(GridData.FILL_BOTH));
    selectionTable.setHeaderVisible(true);
    selectionTable.setLinesVisible(true);

    // create the column
    final TableColumn selectionTableColumn = new TableColumn(selectionTable, SWT.LEFT);
    // set the header
    selectionTableColumn.setText("Chosen Qualifiers");

    selectionTable.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(ControlEvent e) {
            Rectangle r = selectionTable.getClientArea();
            selectionTableColumn.setWidth(r.width);
        }
    });
    mSelectionTableViewer = new TableViewer(selectionTable);
    mSelectionTableViewer.setContentProvider(new QualifierContentProvider());
    mSelectionTableViewer.setLabelProvider(new QualifierLabelProvider(true /* showQualifierValue */));
    mSelectionTableViewer.setInput(mSelectedConfiguration);
    mSelectionTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            // ignore selection changes during resfreshes in some cases.
            if (mOnRefresh) {
                return;
            }

            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection structSelection = (IStructuredSelection) selection;

                if (structSelection.isEmpty() == false) {
                    Object first = structSelection.getFirstElement();

                    if (first instanceof ResourceQualifier) {
                        mRemoveButton.setEnabled(true);

                        QualifierEditBase composite = mUiMap.get(first.getClass());

                        if (composite != null) {
                            composite.setQualifier((ResourceQualifier) first);
                        }

                        mStackLayout.topControl = composite;
                        mQualifierEditParent.layout();

                        return;
                    }
                } else {
                    mStackLayout.topControl = null;
                    mQualifierEditParent.layout();
                }
            }

            mRemoveButton.setEnabled(false);
        }
    });

    // 4th column is the detail of the selected qualifier 
    mQualifierEditParent = new Composite(this, SWT.NONE);
    mQualifierEditParent.setLayout(mStackLayout = new StackLayout());
    mQualifierEditParent.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    // create the UI for all the qualifiers, and associate them to the ResourceQualifer class.
    mUiMap.put(CountryCodeQualifier.class, new MCCEdit(mQualifierEditParent));
    mUiMap.put(NetworkCodeQualifier.class, new MNCEdit(mQualifierEditParent));
    mUiMap.put(LanguageQualifier.class, new LanguageEdit(mQualifierEditParent));
    mUiMap.put(RegionQualifier.class, new RegionEdit(mQualifierEditParent));
    mUiMap.put(ScreenOrientationQualifier.class, new OrientationEdit(mQualifierEditParent));
    mUiMap.put(PixelDensityQualifier.class, new PixelDensityEdit(mQualifierEditParent));
    mUiMap.put(TouchScreenQualifier.class, new TouchEdit(mQualifierEditParent));
    mUiMap.put(KeyboardStateQualifier.class, new KeyboardEdit(mQualifierEditParent));
    mUiMap.put(TextInputMethodQualifier.class, new TextInputEdit(mQualifierEditParent));
    mUiMap.put(NavigationMethodQualifier.class, new NavigationEdit(mQualifierEditParent));
    mUiMap.put(ScreenDimensionQualifier.class, new ScreenDimensionEdit(mQualifierEditParent));
}