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

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

Introduction

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

Prototype

@Override
    public Object[] toArray() 

Source Link

Usage

From source file:net.rim.ejde.internal.ui.launchers.LaunchUtils.java

License:Open Source License

/**
 * Get list of <code>IProject</code> from the selection.
 *
 * @param selection/*from  ww w . ja  v a  2 s.  c  om*/
 * @return The list of <code>IProject</code>
 */
public static List<IProject> getSelectedProjects(StructuredSelection selection) {
    Object[] items = selection.toArray();
    List<IProject> projects = new ArrayList<IProject>();
    IProject iproject;
    for (int i = 0; i < items.length; i++) {
        iproject = null;
        if (items[i] instanceof IAdaptable) {
            Object ires = ((IAdaptable) items[i]).getAdapter(IResource.class);
            if (ires != null) {
                iproject = ((IResource) ires).getProject();
            }
        }
        if (iproject != null) {
            if (NatureUtils.hasBBNature(iproject) && !projects.contains(iproject)) {
                projects.add(iproject);
            }
        }

    }
    return projects;
}

From source file:net.sourceforge.javahexeditor.plugin.editors.HexEditor.java

License:Open Source License

@Override
public void setSelection(ISelection selection) {
    if (selection.isEmpty()) {
        return;/* w  ww. java2  s. co m*/
    }
    StructuredSelection aSelection = (StructuredSelection) selection;
    long[] startEnd = (long[]) aSelection.getFirstElement();
    long start = startEnd[0];
    long end = start;
    if (startEnd.length > 1) {
        end = startEnd[1];
    }
    if (aSelection.size() > 1) {
        startEnd = (long[]) aSelection.toArray()[1];
        end = startEnd[0];
        if (startEnd.length > 1) {
            end = startEnd[1];
        }
    }
    getManager().setSelection(new RangeSelection(start, end));
}

From source file:net.tourbook.ui.views.TourBlogView.java

License:Open Source License

private void fireMarkerPosition(final StructuredSelection selection) {

    final Object[] selectedMarker = selection.toArray();

    if (selectedMarker.length > 0) {

        final ArrayList<TourMarker> allTourMarker = new ArrayList<TourMarker>();

        for (final Object object : selectedMarker) {
            allTourMarker.add((TourMarker) object);
        }//from  w  w w  . j av a  2  s . co  m

        _postSelectionProvider.setSelection(new SelectionTourMarker(_tourData, allTourMarker));
    }
}

From source file:net.tourbook.ui.views.TourBlogView.java

License:Open Source License

/**
 * Fire a selection for the selected marker(s).
 *//*w  w  w. ja  v  a  2 s.  c  om*/
private void hrefActionOpenMarker(final StructuredSelection selection) {

    // a chart must be available
    if (_tourChart == null) {

        final TourChart tourChart = TourManager.getInstance().getActiveTourChart();

        if ((tourChart == null) || tourChart.isDisposed()) {

            fireMarkerPosition(selection);

            return;

        } else {
            _tourChart = tourChart;
        }
    }

    final Object[] selectedMarker = selection.toArray();

    if (selectedMarker.length > 1) {

        // two or more markers are selected

        _postSelectionProvider.setSelection(
                new SelectionChartXSliderPosition(_tourChart, ((TourMarker) selectedMarker[0]).getSerieIndex(),
                        ((TourMarker) selectedMarker[selectedMarker.length - 1]).getSerieIndex()));

    } else if (selectedMarker.length > 0) {

        // one marker is selected

        _postSelectionProvider.setSelection(
                new SelectionChartXSliderPosition(_tourChart, ((TourMarker) selectedMarker[0]).getSerieIndex(),
                        SelectionChartXSliderPosition.IGNORE_SLIDER_POSITION));
    }
}

From source file:net.tourbook.ui.views.tourCatalog.ActionCheckTours.java

License:Open Source License

@Override
public void run() {

    // check all selected compared tours which are not yet stored

    final CheckboxTreeViewer viewer = fCompareResultView.getViewer();
    final StructuredSelection selection = (StructuredSelection) viewer.getSelection();
    if (selection.size() > 0) {

        for (final Object tour : selection.toArray()) {

            if (tour instanceof TVICompareResultComparedTour) {
                final TVICompareResultComparedTour comparedTour = (TVICompareResultComparedTour) tour;
                if (comparedTour.isSaved() == false) {
                    viewer.setChecked(tour, true);
                }//w w w . j a  va 2 s  .c  om
            }
        }
    }
}

From source file:net.tourbook.ui.views.tourDataEditor.TourDataEditorView.java

License:Open Source License

void actionCsvTimeSliceExport() {

    // get selected time slices
    final StructuredSelection selection = (StructuredSelection) _sliceViewer.getSelection();
    if (selection.size() == 0) {
        return;//from   w  w  w.j  a  v  a  2  s .c om
    }

    /*
     * get export filename
     */
    final FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
    dialog.setText(Messages.dialog_export_file_dialog_text);

    dialog.setFilterPath(_state.get(STATE_CSV_EXPORT_PATH));
    dialog.setFilterExtensions(new String[] { Util.CSV_FILE_EXTENSION });
    dialog.setFileName(
            net.tourbook.ui.UI.format_yyyymmdd_hhmmss(_tourData) + UI.SYMBOL_DOT + Util.CSV_FILE_EXTENSION);

    final String selectedFilePath = dialog.open();
    if (selectedFilePath == null) {
        return;
    }

    final File exportFilePath = new Path(selectedFilePath).toFile();

    // keep export path
    _state.put(STATE_CSV_EXPORT_PATH, exportFilePath.getPath());

    if (exportFilePath.exists()) {
        if (net.tourbook.ui.UI.confirmOverwrite(exportFilePath) == false) {
            // don't overwrite file, nothing more to do
            return;
        }
    }

    /*
     * write time slices into csv file
     */
    Writer exportWriter = null;
    try {

        exportWriter = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(selectedFilePath), UI.UTF_8));
        final StringBuilder sb = new StringBuilder();

        writeCSVHeader(exportWriter, sb);

        for (final Object selectedItem : selection.toArray()) {

            final int serieIndex = ((TimeSlice) selectedItem).serieIndex;

            // truncate buffer
            sb.setLength(0);

            // no.
            sb.append(Integer.toString(serieIndex + 0));
            sb.append(UI.TAB);

            // time hh:mm:ss
            if (_serieTime != null) {
                sb.append(net.tourbook.common.UI.format_hh_mm_ss(_serieTime[serieIndex]));
            }
            sb.append(UI.TAB);

            // time in seconds
            if (_serieTime != null) {
                sb.append(Integer.toString(_serieTime[serieIndex]));
            }
            sb.append(UI.TAB);

            // distance
            if (_serieDistance != null) {
                sb.append(_nf6.format(_serieDistance[serieIndex] / 1000 / _unitValueDistance));
            }
            sb.append(UI.TAB);

            // altitude
            if (_serieAltitude != null) {
                sb.append(_nf3.format(_serieAltitude[serieIndex] / _unitValueAltitude));
            }
            sb.append(UI.TAB);

            // gradient
            if (_serieGradient != null) {
                sb.append(_nf3.format(_serieGradient[serieIndex]));
            }
            sb.append(UI.TAB);

            // pulse
            if (_seriePulse != null) {
                sb.append(_nf3.format(_seriePulse[serieIndex]));
            }
            sb.append(UI.TAB);

            // marker
            final TourMarker tourMarker = _markerMap.get(serieIndex);
            if (tourMarker != null) {
                sb.append(tourMarker.getLabel());
            }
            sb.append(UI.TAB);

            // temperature
            if (_serieTemperature != null) {

                final float temperature = UI.convertTemperatureFromMetric(_serieTemperature[serieIndex]);

                sb.append(_nf3.format(temperature));
            }
            sb.append(UI.TAB);

            // cadence
            if (_serieCadence != null) {
                sb.append(_nf3.format(_serieCadence[serieIndex]));
            }
            sb.append(UI.TAB);

            // speed
            if (_serieSpeed != null) {
                sb.append(_nf3.format(_serieSpeed[serieIndex]));
            }
            sb.append(UI.TAB);

            // pace
            if (_seriePace != null) {
                sb.append(net.tourbook.common.UI.format_hhh_mm_ss((long) _seriePace[serieIndex]));
            }
            sb.append(UI.TAB);

            // power
            if (_seriePower != null) {
                sb.append(_nf3.format(_seriePower[serieIndex]));
            }
            sb.append(UI.TAB);

            // longitude
            if (_serieLongitude != null) {
                sb.append(Double.toString(_serieLongitude[serieIndex]));
            }
            sb.append(UI.TAB);

            // latitude
            if (_serieLatitude != null) {
                sb.append(Double.toString(_serieLatitude[serieIndex]));
            }
            sb.append(UI.TAB);

            // break time
            if (_serieBreakTime != null) {
                sb.append(_serieBreakTime[serieIndex] ? 1 : 0);
            }
            sb.append(UI.TAB);

            // end of line
            sb.append(net.tourbook.ui.UI.SYSTEM_NEW_LINE);
            exportWriter.write(sb.toString());
        }

    } catch (final IOException e) {
        e.printStackTrace();
    } finally {

        if (exportWriter != null) {
            try {
                exportWriter.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:net.tourbook.ui.views.tourDataEditor.TourDataEditorView.java

License:Open Source License

/**
 * delete selected time slices//from w w w  .j av a2s.  co  m
 * 
 * @param isRemoveTime
 */
void actionDeleteTimeSlices(final boolean isRemoveTime) {

    // a tour with reference tours is currently not supported
    if (_isReferenceTourAvailable) {
        MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
                Messages.tour_editor_dlg_delete_rows_title, Messages.tour_editor_dlg_delete_rows_message);
        return;
    }

    if (isRowSelectionMode() == false) {
        return;
    }

    // get selected time slices
    final StructuredSelection selection = (StructuredSelection) _sliceViewer.getSelection();
    if (selection.size() == 0) {
        return;
    }

    final Object[] selectedTimeSlices = selection.toArray();

    /*
     * check if time slices have a successive selection
     */
    int lastIndex = -1;
    int firstIndex = -1;

    for (final Object selectedItem : selectedTimeSlices) {

        final TimeSlice timeSlice = (TimeSlice) selectedItem;

        if (lastIndex == -1) {

            // first slice

            firstIndex = lastIndex = timeSlice.serieIndex;

        } else {

            // 2...n slices

            if (lastIndex - timeSlice.serieIndex == -1) {

                // successive selection

                lastIndex = timeSlice.serieIndex;

            } else {

                MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
                        Messages.tour_editor_dlg_delete_rows_title,
                        Messages.tour_editor_dlg_delete_rows_not_successive);
                return;
            }
        }
    }

    // check if markers are within the selection
    if (canDeleteMarkers(firstIndex, lastIndex) == false) {
        return;
    }

    /*
     * get first selection index to select a time slice after removal
     */
    final Table table = (Table) _sliceViewer.getControl();
    final int[] indices = table.getSelectionIndices();
    Arrays.sort(indices);
    int lastSelectionIndex = indices[0];

    TourManager.removeTimeSlices(_tourData, firstIndex, lastIndex, isRemoveTime);

    getDataSeriesFromTourData();

    // update UI
    updateUI_Tab_1_Tour();
    updateUI_Tab_3_Info();

    // update slice viewer
    _sliceViewerItems = getRemainingSliceItems(_sliceViewerItems, firstIndex, lastIndex);

    _sliceViewer.getControl().setRedraw(false);
    {
        // update viewer
        _sliceViewer.remove(selectedTimeSlices);

        // update serie index label
        _sliceViewer.refresh(true);
    }
    _sliceViewer.getControl().setRedraw(true);

    setTourDirty();

    // notify other viewers
    fireModifyNotification();

    /*
     * select next available time slice
     */
    final int itemCount = table.getItemCount();
    if (itemCount > 0) {

        // adjust to array bounds
        lastSelectionIndex = Math.max(0, Math.min(lastSelectionIndex, itemCount - 1));

        table.setSelection(lastSelectionIndex);
        table.showSelection();

        // fire selection position
        _sliceViewer.setSelection(_sliceViewer.getSelection());
    }
}

From source file:net.tourbook.ui.views.tourDataEditor.TourDataEditorView.java

License:Open Source License

/**
 * enable actions//w  ww  . jav a 2 s. c  o  m
 */
private void enableSliceActions() {

    final StructuredSelection sliceSelection = (StructuredSelection) _sliceViewer.getSelection();

    final int numberOfSelectedSlices = sliceSelection.size();

    final boolean isSliceSelected = numberOfSelectedSlices > 0;
    final boolean isOneSliceSelected = numberOfSelectedSlices == 1;
    final boolean isTourInDb = isTourInDb();

    // check if a marker can be created
    boolean canCreateMarker = false;
    if (isOneSliceSelected) {
        final TimeSlice oneTimeSlice = (TimeSlice) sliceSelection.getFirstElement();
        canCreateMarker = _markerMap.containsKey(oneTimeSlice.serieIndex) == false;
    }
    // get selected Marker
    TourMarker selectedMarker = null;
    for (final Iterator<?> iterator = sliceSelection.iterator(); iterator.hasNext();) {
        final TimeSlice timeSlice = (TimeSlice) iterator.next();
        if (_markerMap.containsKey(timeSlice.serieIndex)) {
            selectedMarker = _markerMap.get(timeSlice.serieIndex);
            break;
        }
    }

    _actionCreateTourMarker.setEnabled(_isEditMode && isTourInDb && isOneSliceSelected && canCreateMarker);
    _actionOpenMarkerDialog.setEnabled(_isEditMode && isTourInDb);

    // select marker
    _actionOpenMarkerDialog.setTourMarker(selectedMarker);

    _actionDeleteTimeSlicesRemoveTime.setEnabled(_isEditMode && isTourInDb && isSliceSelected);
    _actionDeleteTimeSlicesKeepTime.setEnabled(_isEditMode && isTourInDb && isSliceSelected);

    _actionExportTour.setEnabled(true);
    _actionCsvTimeSliceExport.setEnabled(isSliceSelected);

    _actionSplitTour.setEnabled(isOneSliceSelected);
    _actionExtractTour.setEnabled(numberOfSelectedSlices >= 2);

    // set start/end position into the actions
    if (isSliceSelected) {

        final Object[] selectedSliceArray = sliceSelection.toArray();
        final int lastSliceIndex = selectedSliceArray.length - 1;

        final TimeSlice firstSelectedTimeSlice = (TimeSlice) selectedSliceArray[0];
        final TimeSlice lastSelectedTimeSlice = (TimeSlice) selectedSliceArray[lastSliceIndex];

        final int firstSelectedSerieIndex = firstSelectedTimeSlice.serieIndex;
        final int lastSelectedSerieIndex = lastSelectedTimeSlice.serieIndex;

        _actionExportTour.setTourRange(firstSelectedSerieIndex, lastSelectedSerieIndex);

        _actionSplitTour.setTourRange(firstSelectedSerieIndex);
        _actionExtractTour.setTourRange(firstSelectedSerieIndex, lastSelectedSerieIndex);

        /*
         * prevent that the first and last slice is selected for the split which causes errors
         */
        final int numberOfAllSlices = _sliceViewerItems.length;

        final boolean isSplitValid = firstSelectedSerieIndex > 0
                && firstSelectedSerieIndex < numberOfAllSlices - 1;

        _actionSplitTour.setEnabled(isOneSliceSelected && isSplitValid);
    }
}

From source file:net.tourbook.ui.views.tourDataEditor.TourDataEditorView.java

License:Open Source License

/**
 * select the chart slider(s) according to the selected marker(s)
 * //from  w w  w.j a v a2  s.  c o  m
 * @return
 */
private ISelection fireSliderPosition(final StructuredSelection selection) {

    final Object[] selectedData = selection.toArray();
    if ((selectedData == null) || (selectedData.length == 0)) {
        return null;
    }

    if (_tourChart == null) {

        final TourChart tourChart = TourManager.getInstance().getActiveTourChart();

        if ((tourChart != null) && (tourChart.isDisposed() == false)) {
            _tourChart = tourChart;
        }
    }

    final Object firstItem = selectedData[0];

    int serieIndex0 = -1;
    int serieIndex1 = -1;
    int serieIndex2 = -1;

    if (selectedData.length > 1) {

        // two or more data are selected, set the 2 sliders to the first and last selected data

        if (firstItem instanceof TimeSlice) {

            final int serieIndexFirst = ((TimeSlice) firstItem).serieIndex;

            /*
             * position slider at the beginning of the first slice
             */
            serieIndex1 = serieIndexFirst > 0 ? serieIndexFirst - 1 : 0;
            serieIndex2 = ((TimeSlice) selectedData[selectedData.length - 1]).serieIndex;

        } else if (firstItem instanceof TourMarker) {

            serieIndex1 = ((TourMarker) firstItem).getSerieIndex();
            serieIndex2 = ((TourMarker) selectedData[selectedData.length - 1]).getSerieIndex();
        }

    } else if (selectedData.length > 0) {

        // one data is selected

        if (firstItem instanceof TimeSlice) {

            final int serieIndexFirst = ((TimeSlice) firstItem).serieIndex;

            /*
             * position slider at the beginning of the slice so that each slice borders has an
             * slider
             */
            serieIndex0 = serieIndexFirst > 0 ? serieIndexFirst - 1
                    : SelectionChartXSliderPosition.IGNORE_SLIDER_POSITION;

            serieIndex1 = serieIndexFirst;
            serieIndex2 = SelectionChartXSliderPosition.IGNORE_SLIDER_POSITION;

        } else if (firstItem instanceof TourMarker) {

            serieIndex1 = ((TourMarker) firstItem).getSerieIndex();
            serieIndex2 = SelectionChartXSliderPosition.IGNORE_SLIDER_POSITION;
        }
    }

    ISelection sliderSelection = null;
    if (serieIndex1 != -1) {

        if (_tourChart == null) {

            // chart is not available, fire a map position

            if ((_serieLatitude != null) && (_serieLatitude.length > 0)) {

                // map position is available

                sliderSelection = new SelectionMapPosition(_tourData, serieIndex1, serieIndex2, true);
            }

        } else {

            final SelectionChartXSliderPosition xSliderSelection = new SelectionChartXSliderPosition(_tourChart,
                    serieIndex0, serieIndex1, serieIndex2);

            xSliderSelection.setCenterSliderPosition(true);

            sliderSelection = xSliderSelection;
        }

        _postSelectionProvider.setSelection(sliderSelection);
    }

    return sliderSelection;
}

From source file:net.tourbook.ui.views.TourMarkerView.java

License:Open Source License

/**
 * select the chart slider(s) according to the selected marker(s)
 *//*w ww. ja  v  a2 s.com*/
private void fireSliderPosition(final StructuredSelection selection) {

    // a chart must be available
    if (_tourChart == null) {

        final TourChart tourChart = TourManager.getInstance().getActiveTourChart();

        if ((tourChart == null) || tourChart.isDisposed()) {
            return;
        } else {
            _tourChart = tourChart;
        }
    }

    final Object[] segments = selection.toArray();

    if (segments.length > 1) {

        // two or more markers are selected

        _postSelectionProvider.setSelection(
                new SelectionChartXSliderPosition(_tourChart, ((TourMarker) segments[0]).getSerieIndex(),
                        ((TourMarker) segments[segments.length - 1]).getSerieIndex()));

    } else if (segments.length > 0) {

        // one marker is selected

        _postSelectionProvider.setSelection(
                new SelectionChartXSliderPosition(_tourChart, ((TourMarker) segments[0]).getSerieIndex(),
                        SelectionChartXSliderPosition.IGNORE_SLIDER_POSITION));
    }
}