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.ddmuilib.AllocationPanel.java

License:Apache License

/**
 * Returns the current allocation selection or <code>null</code> if none is found.
 * If a {@link ISelection} object is specified, the first {@link AllocationInfo} from this
 * selection is returned, otherwise, the <code>ISelection</code> returned by
 * {@link TableViewer#getSelection()} is used.
 * @param selection the {@link ISelection} to use, or <code>null</code>
 *//*from ww  w  . j av  a  2s. c o  m*/
private AllocationInfo getAllocationSelection(ISelection selection) {
    if (selection == null) {
        selection = mAllocationViewer.getSelection();
    }

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Object object = structuredSelection.getFirstElement();
        if (object instanceof AllocationInfo) {
            return (AllocationInfo) object;
        }
    }

    return null;
}

From source file:com.android.ddmuilib.EmulatorControlPanel.java

License:Apache License

private void createGpxLocationControl(Composite gpxLocationComp) {
    GridData gd;/*from   w  w  w . ja  v a  2s  .co m*/

    IPreferenceStore store = DdmUiPreferences.getStore();

    gpxLocationComp.setLayout(new GridLayout(1, false));

    mGpxUploadButton = new Button(gpxLocationComp, SWT.PUSH);
    mGpxUploadButton.setText("Load GPX...");

    // Table for way point
    mGpxWayPointTable = new Table(gpxLocationComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
    mGpxWayPointTable.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
    gd.heightHint = 100;
    mGpxWayPointTable.setHeaderVisible(true);
    mGpxWayPointTable.setLinesVisible(true);

    TableHelper.createTableColumn(mGpxWayPointTable, "Name", SWT.LEFT, "Some Name", PREFS_WAYPOINT_COL_NAME,
            store);
    TableHelper.createTableColumn(mGpxWayPointTable, "Longitude", SWT.LEFT, "-199.999999",
            PREFS_WAYPOINT_COL_LONGITUDE, store);
    TableHelper.createTableColumn(mGpxWayPointTable, "Latitude", SWT.LEFT, "-199.999999",
            PREFS_WAYPOINT_COL_LATITUDE, store);
    TableHelper.createTableColumn(mGpxWayPointTable, "Elevation", SWT.LEFT, "99999.9",
            PREFS_WAYPOINT_COL_ELEVATION, store);
    TableHelper.createTableColumn(mGpxWayPointTable, "Description", SWT.LEFT, "Some Description",
            PREFS_WAYPOINT_COL_DESCRIPTION, store);

    final TableViewer gpxWayPointViewer = new TableViewer(mGpxWayPointTable);
    gpxWayPointViewer.setContentProvider(new WayPointContentProvider());
    gpxWayPointViewer.setLabelProvider(new WayPointLabelProvider());

    gpxWayPointViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object selectedObject = structuredSelection.getFirstElement();
                if (selectedObject instanceof WayPoint) {
                    WayPoint wayPoint = (WayPoint) selectedObject;

                    if (mEmulatorConsole != null && mPlayingTrack == false) {
                        processCommandResult(mEmulatorConsole.sendLocation(wayPoint.getLongitude(),
                                wayPoint.getLatitude(), wayPoint.getElevation()));
                    }
                }
            }
        }
    });

    // table for tracks.
    mGpxTrackTable = new Table(gpxLocationComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
    mGpxTrackTable.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
    gd.heightHint = 100;
    mGpxTrackTable.setHeaderVisible(true);
    mGpxTrackTable.setLinesVisible(true);

    TableHelper.createTableColumn(mGpxTrackTable, "Name", SWT.LEFT, "Some very long name", PREFS_TRACK_COL_NAME,
            store);
    TableHelper.createTableColumn(mGpxTrackTable, "Point Count", SWT.RIGHT, "9999", PREFS_TRACK_COL_COUNT,
            store);
    TableHelper.createTableColumn(mGpxTrackTable, "First Point Time", SWT.LEFT, "999-99-99T99:99:99Z",
            PREFS_TRACK_COL_FIRST, store);
    TableHelper.createTableColumn(mGpxTrackTable, "Last Point Time", SWT.LEFT, "999-99-99T99:99:99Z",
            PREFS_TRACK_COL_LAST, store);
    TableHelper.createTableColumn(mGpxTrackTable, "Comment", SWT.LEFT, "-199.999999", PREFS_TRACK_COL_COMMENT,
            store);

    final TableViewer gpxTrackViewer = new TableViewer(mGpxTrackTable);
    gpxTrackViewer.setContentProvider(new TrackContentProvider());
    gpxTrackViewer.setLabelProvider(new TrackLabelProvider());

    gpxTrackViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object selectedObject = structuredSelection.getFirstElement();
                if (selectedObject instanceof Track) {
                    Track track = (Track) selectedObject;

                    if (mEmulatorConsole != null && mPlayingTrack == false) {
                        TrackPoint[] points = track.getPoints();
                        processCommandResult(mEmulatorConsole.sendLocation(points[0].getLongitude(),
                                points[0].getLatitude(), points[0].getElevation()));
                    }

                    mPlayGpxButton.setEnabled(true);
                    mGpxBackwardButton.setEnabled(true);
                    mGpxForwardButton.setEnabled(true);
                    mGpxSpeedButton.setEnabled(true);

                    return;
                }
            }

            mPlayGpxButton.setEnabled(false);
            mGpxBackwardButton.setEnabled(false);
            mGpxForwardButton.setEnabled(false);
            mGpxSpeedButton.setEnabled(false);
        }
    });

    mGpxUploadButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            FileDialog fileDialog = new FileDialog(mParent.getShell(), SWT.OPEN);

            fileDialog.setText("Load GPX File");
            fileDialog.setFilterExtensions(new String[] { "*.gpx" });

            String fileName = fileDialog.open();
            if (fileName != null) {
                GpxParser parser = new GpxParser(fileName);
                if (parser.parse()) {
                    gpxWayPointViewer.setInput(parser.getWayPoints());
                    gpxTrackViewer.setInput(parser.getTracks());
                }
            }
        }
    });

    mGpxPlayControls = new Composite(gpxLocationComp, SWT.NONE);
    GridLayout gl;
    mGpxPlayControls.setLayout(gl = new GridLayout(5, false));
    gl.marginHeight = gl.marginWidth = 0;
    mGpxPlayControls.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    mPlayGpxButton = new Button(mGpxPlayControls, SWT.PUSH | SWT.FLAT);
    mPlayGpxButton.setImage(mPlayImage);
    mPlayGpxButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if (mPlayingTrack == false) {
                ISelection selection = gpxTrackViewer.getSelection();
                if (selection.isEmpty() == false && selection instanceof IStructuredSelection) {
                    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                    Object selectedObject = structuredSelection.getFirstElement();
                    if (selectedObject instanceof Track) {
                        Track track = (Track) selectedObject;
                        playTrack(track);
                    }
                }
            } else {
                // if we're playing, then we pause
                mPlayingTrack = false;
                if (mPlayingThread != null) {
                    mPlayingThread.interrupt();
                }
            }
        }
    });

    Label separator = new Label(mGpxPlayControls, SWT.SEPARATOR | SWT.VERTICAL);
    separator.setLayoutData(gd = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL));
    gd.heightHint = 0;

    ImageLoader loader = ImageLoader.getDdmUiLibLoader();
    mGpxBackwardButton = new Button(mGpxPlayControls, SWT.TOGGLE | SWT.FLAT);
    mGpxBackwardButton.setImage(loader.loadImage("backward.png", mParent.getDisplay())); //$NON-NLS-1$
    mGpxBackwardButton.setSelection(false);
    mGpxBackwardButton.addSelectionListener(mDirectionButtonAdapter);
    mGpxForwardButton = new Button(mGpxPlayControls, SWT.TOGGLE | SWT.FLAT);
    mGpxForwardButton.setImage(loader.loadImage("forward.png", mParent.getDisplay())); //$NON-NLS-1$
    mGpxForwardButton.setSelection(true);
    mGpxForwardButton.addSelectionListener(mDirectionButtonAdapter);

    mGpxSpeedButton = new Button(mGpxPlayControls, SWT.PUSH | SWT.FLAT);

    mSpeedIndex = 0;
    mSpeed = PLAY_SPEEDS[mSpeedIndex];

    mGpxSpeedButton.setText(String.format(SPEED_FORMAT, mSpeed));
    mGpxSpeedButton.addSelectionListener(mSpeedButtonAdapter);

    mPlayGpxButton.setEnabled(false);
    mGpxBackwardButton.setEnabled(false);
    mGpxForwardButton.setEnabled(false);
    mGpxSpeedButton.setEnabled(false);

}

From source file:com.android.ddmuilib.EmulatorControlPanel.java

License:Apache License

private void createKmlLocationControl(Composite kmlLocationComp) {
    GridData gd;//from   w w w. j  av a  2 s.c o  m

    IPreferenceStore store = DdmUiPreferences.getStore();

    kmlLocationComp.setLayout(new GridLayout(1, false));

    mKmlUploadButton = new Button(kmlLocationComp, SWT.PUSH);
    mKmlUploadButton.setText("Load KML...");

    // Table for way point
    mKmlWayPointTable = new Table(kmlLocationComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
    mKmlWayPointTable.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
    gd.heightHint = 200;
    mKmlWayPointTable.setHeaderVisible(true);
    mKmlWayPointTable.setLinesVisible(true);

    TableHelper.createTableColumn(mKmlWayPointTable, "Name", SWT.LEFT, "Some Name", PREFS_WAYPOINT_COL_NAME,
            store);
    TableHelper.createTableColumn(mKmlWayPointTable, "Longitude", SWT.LEFT, "-199.999999",
            PREFS_WAYPOINT_COL_LONGITUDE, store);
    TableHelper.createTableColumn(mKmlWayPointTable, "Latitude", SWT.LEFT, "-199.999999",
            PREFS_WAYPOINT_COL_LATITUDE, store);
    TableHelper.createTableColumn(mKmlWayPointTable, "Elevation", SWT.LEFT, "99999.9",
            PREFS_WAYPOINT_COL_ELEVATION, store);
    TableHelper.createTableColumn(mKmlWayPointTable, "Description", SWT.LEFT, "Some Description",
            PREFS_WAYPOINT_COL_DESCRIPTION, store);

    final TableViewer kmlWayPointViewer = new TableViewer(mKmlWayPointTable);
    kmlWayPointViewer.setContentProvider(new WayPointContentProvider());
    kmlWayPointViewer.setLabelProvider(new WayPointLabelProvider());

    mKmlUploadButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            FileDialog fileDialog = new FileDialog(mParent.getShell(), SWT.OPEN);

            fileDialog.setText("Load KML File");
            fileDialog.setFilterExtensions(new String[] { "*.kml" });

            String fileName = fileDialog.open();
            if (fileName != null) {
                KmlParser parser = new KmlParser(fileName);
                if (parser.parse()) {
                    kmlWayPointViewer.setInput(parser.getWayPoints());

                    mPlayKmlButton.setEnabled(true);
                    mKmlBackwardButton.setEnabled(true);
                    mKmlForwardButton.setEnabled(true);
                    mKmlSpeedButton.setEnabled(true);
                }
            }
        }
    });

    kmlWayPointViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object selectedObject = structuredSelection.getFirstElement();
                if (selectedObject instanceof WayPoint) {
                    WayPoint wayPoint = (WayPoint) selectedObject;

                    if (mEmulatorConsole != null && mPlayingTrack == false) {
                        processCommandResult(mEmulatorConsole.sendLocation(wayPoint.getLongitude(),
                                wayPoint.getLatitude(), wayPoint.getElevation()));
                    }
                }
            }
        }
    });

    mKmlPlayControls = new Composite(kmlLocationComp, SWT.NONE);
    GridLayout gl;
    mKmlPlayControls.setLayout(gl = new GridLayout(5, false));
    gl.marginHeight = gl.marginWidth = 0;
    mKmlPlayControls.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    mPlayKmlButton = new Button(mKmlPlayControls, SWT.PUSH | SWT.FLAT);
    mPlayKmlButton.setImage(mPlayImage);
    mPlayKmlButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if (mPlayingTrack == false) {
                Object input = kmlWayPointViewer.getInput();
                if (input instanceof WayPoint[]) {
                    playKml((WayPoint[]) input);
                }
            } else {
                // if we're playing, then we pause
                mPlayingTrack = false;
                if (mPlayingThread != null) {
                    mPlayingThread.interrupt();
                }
            }
        }
    });

    Label separator = new Label(mKmlPlayControls, SWT.SEPARATOR | SWT.VERTICAL);
    separator.setLayoutData(gd = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL));
    gd.heightHint = 0;

    ImageLoader loader = ImageLoader.getDdmUiLibLoader();
    mKmlBackwardButton = new Button(mKmlPlayControls, SWT.TOGGLE | SWT.FLAT);
    mKmlBackwardButton.setImage(loader.loadImage("backward.png", mParent.getDisplay())); //$NON-NLS-1$
    mKmlBackwardButton.setSelection(false);
    mKmlBackwardButton.addSelectionListener(mDirectionButtonAdapter);
    mKmlForwardButton = new Button(mKmlPlayControls, SWT.TOGGLE | SWT.FLAT);
    mKmlForwardButton.setImage(loader.loadImage("forward.png", mParent.getDisplay())); //$NON-NLS-1$
    mKmlForwardButton.setSelection(true);
    mKmlForwardButton.addSelectionListener(mDirectionButtonAdapter);

    mKmlSpeedButton = new Button(mKmlPlayControls, SWT.PUSH | SWT.FLAT);

    mSpeedIndex = 0;
    mSpeed = PLAY_SPEEDS[mSpeedIndex];

    mKmlSpeedButton.setText(String.format(SPEED_FORMAT, mSpeed));
    mKmlSpeedButton.addSelectionListener(mSpeedButtonAdapter);

    mPlayKmlButton.setEnabled(false);
    mKmlBackwardButton.setEnabled(false);
    mKmlForwardButton.setEnabled(false);
    mKmlSpeedButton.setEnabled(false);
}

From source file:com.android.ddmuilib.explorer.DeviceExplorer.java

License:Apache License

/**
 * Creates a control capable of displaying some information.  This is
 * called once, when the application is initializing, from the UI thread.
 *//*w ww  .  j ava 2s  .co  m*/
@Override
protected Control createControl(Composite parent) {
    mParent = parent;
    parent.setLayout(new FillLayout());

    ImageLoader loader = ImageLoader.getDdmUiLibLoader();
    if (mFileImage == null) {
        mFileImage = loader.loadImage("file.png", mParent.getDisplay());
    }
    if (mFolderImage == null) {
        mFolderImage = loader.loadImage("folder.png", mParent.getDisplay());
    }
    if (mPackageImage == null) {
        mPackageImage = loader.loadImage("android.png", mParent.getDisplay());
    }
    if (mOtherImage == null) {
        // TODO: find a default image for other.
    }

    mTree = new Tree(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
    mTree.setHeaderVisible(true);

    IPreferenceStore store = DdmUiPreferences.getStore();

    // create columns
    TableHelper.createTreeColumn(mTree, "Name", SWT.LEFT, "0000drwxrwxrwx", COLUMN_NAME, store); //$NON-NLS-2$
    TableHelper.createTreeColumn(mTree, "Size", SWT.RIGHT, "000000", COLUMN_SIZE, store); //$NON-NLS-2$
    TableHelper.createTreeColumn(mTree, "Date", SWT.LEFT, "2007-08-14", COLUMN_DATE, store); //$NON-NLS-2$
    TableHelper.createTreeColumn(mTree, "Time", SWT.LEFT, "20:54", COLUMN_TIME, store); //$NON-NLS-2$
    TableHelper.createTreeColumn(mTree, "Permissions", SWT.LEFT, "drwxrwxrwx", COLUMN_PERMISSIONS, store); //$NON-NLS-2$
    TableHelper.createTreeColumn(mTree, "Info", SWT.LEFT, "drwxrwxrwx", COLUMN_INFO, store); //$NON-NLS-2$

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

    // setup data provider
    mContentProvider = new DeviceContentProvider();
    mTreeViewer.setContentProvider(mContentProvider);
    mTreeViewer.setLabelProvider(new FileLabelProvider(mFileImage, mFolderImage, mPackageImage, mOtherImage));

    // setup a listener for selection
    mTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection sel = event.getSelection();
            if (sel.isEmpty()) {
                mPullAction.setEnabled(false);
                mPushAction.setEnabled(false);
                mDeleteAction.setEnabled(false);
                mCreateNewFolderAction.setEnabled(false);
                return;
            }
            if (sel instanceof IStructuredSelection) {
                IStructuredSelection selection = (IStructuredSelection) sel;
                Object element = selection.getFirstElement();
                if (element == null)
                    return;
                if (element instanceof FileEntry) {
                    mPullAction.setEnabled(true);
                    mPushAction.setEnabled(selection.size() == 1);
                    if (selection.size() == 1) {
                        FileEntry entry = (FileEntry) element;
                        setDeleteEnabledState(entry);
                        mCreateNewFolderAction.setEnabled(entry.isDirectory());
                    } else {
                        mDeleteAction.setEnabled(false);
                    }
                }
            }
        }
    });

    // 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) {
                    FileEntry entry = (FileEntry) selection.getFirstElement();
                    String name = entry.getName();

                    FileEntry parentEntry = entry.getParent();

                    // can't really do anything with no parent
                    if (parentEntry == null) {
                        return;
                    }

                    // check this is a file like we want.
                    Matcher m = mKeyFilePattern.matcher(name);
                    if (m.matches()) {
                        // get the name w/o the extension
                        String baseName = m.group(1);

                        // add the data extension
                        String dataName = baseName + TRACE_DATA_EXT;

                        FileEntry dataEntry = parentEntry.findChild(dataName);

                        handleTraceDoubleClick(baseName, entry, dataEntry);

                    } else {
                        m = mDataFilePattern.matcher(name);
                        if (m.matches()) {
                            // get the name w/o the extension
                            String baseName = m.group(1);

                            // add the key extension
                            String keyName = baseName + TRACE_KEY_EXT;

                            FileEntry keyEntry = parentEntry.findChild(keyName);

                            handleTraceDoubleClick(baseName, keyEntry, entry);
                        }
                    }
                }
            }
        }
    });

    // setup drop listener
    mTreeViewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { FileTransfer.getInstance() },
            new ViewerDropAdapter(mTreeViewer) {
                @Override
                public boolean performDrop(Object data) {
                    // get the item on which we dropped the item(s)
                    FileEntry target = (FileEntry) getCurrentTarget();

                    // in case we drop at the same level as root
                    if (target == null) {
                        return false;
                    }

                    // if the target is not a directory, we get the parent directory
                    if (target.isDirectory() == false) {
                        target = target.getParent();
                    }

                    if (target == null) {
                        return false;
                    }

                    // get the list of files to drop
                    String[] files = (String[]) data;

                    // do the drop
                    pushFiles(files, target);

                    // we need to finish with a refresh
                    refresh(target);

                    return true;
                }

                @Override
                public boolean validateDrop(Object target, int operation, TransferData transferType) {
                    if (target == null) {
                        return false;
                    }

                    // convert to the real item
                    FileEntry targetEntry = (FileEntry) target;

                    // if the target is not a directory, we get the parent directory
                    if (targetEntry.isDirectory() == false) {
                        target = targetEntry.getParent();
                    }

                    if (target == null) {
                        return false;
                    }

                    return true;
                }
            });

    // create and start the refresh thread
    new Thread("Device Ls refresher") {
        @Override
        public void run() {
            while (true) {
                try {
                    sleep(FileListingService.REFRESH_RATE);
                } catch (InterruptedException e) {
                    return;
                }

                if (mTree != null && mTree.isDisposed() == false) {
                    Display display = mTree.getDisplay();
                    if (display.isDisposed() == false) {
                        display.asyncExec(new Runnable() {
                            @Override
                            public void run() {
                                if (mTree.isDisposed() == false) {
                                    mTreeViewer.refresh(true);
                                }
                            }
                        });
                    } else {
                        return;
                    }
                } else {
                    return;
                }
            }

        }
    }.start();

    return mTree;
}

From source file:com.android.ddmuilib.StackTracePanel.java

License:Apache License

/**
 * Creates the controls for the StrackTrace display.
 * <p/>This method will set the parent {@link Composite} to use a {@link GridLayout} with
 * 2 columns./*  w  ww  .java2  s  .co  m*/
 * @param parent the parent composite.
 * @param prefs_stack_column
 * @param store
 */
public Table createPanel(Composite parent, String prefs_stack_column, IPreferenceStore store) {

    mStackTraceTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
    mStackTraceTable.setHeaderVisible(false);
    mStackTraceTable.setLinesVisible(false);

    TableHelper.createTableColumn(mStackTraceTable, "Info", SWT.LEFT,
            "SomeLongClassName.method(android/somepackage/someotherpackage/somefile.java:99999)", //$NON-NLS-1$
            prefs_stack_column, store);

    mStackTraceViewer = new TableViewer(mStackTraceTable);
    mStackTraceViewer.setContentProvider(new StackTraceContentProvider());
    mStackTraceViewer.setLabelProvider(new StackTraceLabelProvider());

    mStackTraceViewer.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            if (sSourceRevealer != null && mCurrentClient != null) {
                // get the selected stack trace element
                ISelection selection = mStackTraceViewer.getSelection();

                if (selection instanceof IStructuredSelection) {
                    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                    Object object = structuredSelection.getFirstElement();
                    if (object instanceof StackTraceElement) {
                        StackTraceElement traceElement = (StackTraceElement) object;

                        if (traceElement.isNativeMethod() == false) {
                            sSourceRevealer.reveal(mCurrentClient.getClientData().getClientDescription(),
                                    traceElement.getClassName(), traceElement.getLineNumber());
                        }
                    }
                }
            }
        }
    });

    return mStackTraceTable;
}

From source file:com.android.ddmuilib.ThreadPanel.java

License:Apache License

/**
 * Returns the current thread selection or <code>null</code> if none is found.
 * If a {@link ISelection} object is specified, the first {@link ThreadInfo} from this selection
 * is returned, otherwise, the <code>ISelection</code> returned by
 * {@link TableViewer#getSelection()} is used.
 * @param selection the {@link ISelection} to use, or <code>null</code>
 */// w  w  w .  j  av a 2  s  .c  o  m
private ThreadInfo getThreadSelection(ISelection selection) {
    if (selection == null) {
        selection = mThreadViewer.getSelection();
    }

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Object object = structuredSelection.getFirstElement();
        if (object instanceof ThreadInfo) {
            return (ThreadInfo) object;
        }
    }

    return null;
}

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

License:Open Source License

@Override
public void run(IAction action) {
    IProject project = null;/*www  .  j  a  v  a2  s  .c om*/
    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection ss = (IStructuredSelection) selection;
        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, part.getSite().getWorkbenchWindow());
        WizardDialog dialog = new WizardDialog(part.getSite().getShell(), wizard);
        dialog.open();
    }

}

From source file:com.android.ide.eclipse.adt.internal.launch.junit.AndroidJUnitLaunchConfigurationTab.java

License:Open Source License

/**
 * Returns the current Java element context from which to initialize
 * default settings, or <code>null</code> if none.
 *
 * @return Java element context.//from w w w  .  j  ava  2s . c  o m
 */
private IJavaElement getContext() {
    IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (activeWorkbenchWindow == null) {
        return null;
    }
    IWorkbenchPage page = activeWorkbenchWindow.getActivePage();
    if (page != null) {
        ISelection selection = page.getSelection();
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            if (!ss.isEmpty()) {
                Object obj = ss.getFirstElement();
                if (obj instanceof IJavaElement) {
                    return (IJavaElement) obj;
                }
                if (obj instanceof IResource) {
                    IJavaElement je = JavaCore.create((IResource) obj);
                    if (je == null) {
                        IProject pro = ((IResource) obj).getProject();
                        je = JavaCore.create(pro);
                    }
                    if (je != null) {
                        return je;
                    }
                }
            }
        }
        IEditorPart part = page.getActiveEditor();
        if (part != null) {
            IEditorInput input = part.getEditorInput();
            return (IJavaElement) input.getAdapter(IJavaElement.class);
        }
    }
    return null;
}

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

License:Open Source License

@Override
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) {
                    ProjectState state = Sdk.getProjectState(project);
                    if (state != null && state.isLibrary()) {

                        MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
                                "Android Launch", "Android library projects cannot be launched.");
                    } else {
                        // and launch
                        launch(project, mode);
                    }/*  w w  w .j a  va2 s. co m*/
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * Creates the selector./*from   w  ww. j  a  va  2 s  .co m*/
 * <p/>
 * The {@link SelectorMode} changes the behavior of the selector depending on what is being
 * edited (a device config, a resource config, a given configuration).
 *
 * @param parent the composite parent.
 * @param mode the mode for the selector.
 */
public ConfigurationSelector(Composite parent, SelectorMode mode) {
    super(parent, SWT.NONE);

    mMode = mode;
    mBaseConfiguration.createDefault();

    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());
    // the label provider must return the value of the label only if the mode is
    // CONFIG_ONLY
    mFullTableViewer.setLabelProvider(new QualifierLabelProvider(mMode == SelectorMode.CONFIG_ONLY));
    mFullTableViewer.setInput(mBaseConfiguration);
    mFullTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        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());
    // always show the qualifier value in this case.
    mSelectionTableViewer.setLabelProvider(new QualifierLabelProvider(true /* showQualifierValue */));
    mSelectionTableViewer.setInput(mSelectedConfiguration);
    mSelectionTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        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);

                        if (mMode != SelectorMode.CONFIG_ONLY) {
                            QualifierEditBase composite = mUiMap.get(first.getClass());

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

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

                        return;
                    }
                } else {
                    if (mMode != SelectorMode.CONFIG_ONLY) {
                        mStackLayout.topControl = null;
                        mQualifierEditParent.layout();
                    }
                }
            }

            mRemoveButton.setEnabled(false);
        }
    });

    if (mMode != SelectorMode.CONFIG_ONLY) {
        // 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(LocaleQualifier.class, new LocaleEdit(mQualifierEditParent));
        mUiMap.put(LayoutDirectionQualifier.class, new LayoutDirectionEdit(mQualifierEditParent));
        mUiMap.put(SmallestScreenWidthQualifier.class, new SmallestScreenWidthEdit(mQualifierEditParent));
        mUiMap.put(ScreenWidthQualifier.class, new ScreenWidthEdit(mQualifierEditParent));
        mUiMap.put(ScreenHeightQualifier.class, new ScreenHeightEdit(mQualifierEditParent));
        mUiMap.put(ScreenSizeQualifier.class, new ScreenSizeEdit(mQualifierEditParent));
        mUiMap.put(ScreenRatioQualifier.class, new ScreenRatioEdit(mQualifierEditParent));
        mUiMap.put(ScreenOrientationQualifier.class, new OrientationEdit(mQualifierEditParent));
        mUiMap.put(UiModeQualifier.class, new UiModeEdit(mQualifierEditParent));
        mUiMap.put(NightModeQualifier.class, new NightModeEdit(mQualifierEditParent));
        mUiMap.put(DensityQualifier.class, new DensityEdit(mQualifierEditParent));
        mUiMap.put(TouchScreenQualifier.class, new TouchEdit(mQualifierEditParent));
        mUiMap.put(KeyboardStateQualifier.class, new KeyboardEdit(mQualifierEditParent));
        mUiMap.put(TextInputMethodQualifier.class, new TextInputEdit(mQualifierEditParent));
        mUiMap.put(NavigationStateQualifier.class, new NavigationStateEdit(mQualifierEditParent));
        mUiMap.put(NavigationMethodQualifier.class, new NavigationEdit(mQualifierEditParent));
        mUiMap.put(ScreenDimensionQualifier.class, new ScreenDimensionEdit(mQualifierEditParent));
        mUiMap.put(VersionQualifier.class, new VersionEdit(mQualifierEditParent));
    }
}