Example usage for javax.swing ListSelectionModel addSelectionInterval

List of usage examples for javax.swing ListSelectionModel addSelectionInterval

Introduction

In this page you can find the example usage for javax.swing ListSelectionModel addSelectionInterval.

Prototype

void addSelectionInterval(int index0, int index1);

Source Link

Document

Changes the selection to be the set union of the current selection and the indices between index0 and index1 inclusive.

Usage

From source file:Main.java

/**
 * Set the lead and anchor without affecting selection.
 *///from   ww w  .j ava 2  s . co  m
public static void setLeadAnchorWithoutSelection(ListSelectionModel model, int lead, int anchor) {
    if (anchor == -1) {
        anchor = lead;
    }
    if (lead == -1) {
        model.setAnchorSelectionIndex(-1);
        model.setLeadSelectionIndex(-1);
    } else {
        if (model.isSelectedIndex(lead)) {
            model.addSelectionInterval(lead, lead);
        } else {
            model.removeSelectionInterval(lead, lead);
        }
        model.setAnchorSelectionIndex(anchor);
    }
}

From source file:com.diversityarrays.kdxplore.trials.TrialOverviewPanel.java

private void selectTrial(Trial trial) {
    ListSelectionModel lsm = trialsTable.getSelectionModel();
    lsm.setValueIsAdjusting(true);//from ww  w  . j  a v a2s  . c  o  m
    try {
        lsm.clearSelection();
        int mrow = trialTableModel.indexOfTrial(trial);
        if (mrow >= 0) {
            int vrow = trialsTable.convertRowIndexToView(mrow);
            if (vrow >= 0) {
                lsm.addSelectionInterval(vrow, vrow);
            }
        }
    } finally {
        lsm.setValueIsAdjusting(false);
    }
}

From source file:com.diversityarrays.kdxplore.trialmgr.trait.TraitExplorerPanel.java

@Override
public void setSelectedTraits(List<Trait> traits) {
    List<Integer> traitIndices = traits.stream().map(traitTableModel::indexOf).filter(index -> index != null) // null if not found in model
            .map(modelIndex -> traitsTable.convertRowIndexToView(modelIndex)).filter(index -> index >= 0) // -1 if not visible
            .collect(Collectors.toList());

    if (!traitIndices.isEmpty()) {
        Collections.sort(traitIndices);
        ListSelectionModel lsm = traitsTable.getSelectionModel();
        lsm.setValueIsAdjusting(true);//from   www  . j a v  a2s . c o  m
        lsm.clearSelection();
        try {
            for (Integer index : traitIndices) {
                lsm.addSelectionInterval(index, index);
            }
        } finally {
            lsm.setValueIsAdjusting(false);
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void applySelectionIndexes(List<Integer> indexes) {
    Collections.sort(indexes);/*from   ww w.ja v a2  s. c o  m*/
    ListSelectionModel model = impl.getSelectionModel();
    model.setValueIsAdjusting(true);
    int lastOpened = indexes.get(0);
    int current = indexes.get(0);
    for (Integer index : indexes) {
        if (index > current + 1) {
            model.addSelectionInterval(lastOpened, current);
            lastOpened = index;
        }
        current = index;
    }
    model.addSelectionInterval(lastOpened, current);
    model.setValueIsAdjusting(false);
}

From source file:net.sf.dvstar.transmission.TransmissionView.java

private void restoreRowsSelections(int srows[]) {
    lockValueChanged = true;/*from  w  ww  .  ja  v a 2 s . co  m*/

    /*
    int rcs = tblTorrentList.getRowSorter().getViewRowCount();
    int rca = tblTorrentList.getRowSorter().getModelRowCount();
            
    if(rcs != rca) return;
            
     */

    tracePrint(true, "[restoreRowsSelections] " + Tools.printArray(srows));
    //tracePrint(true, "[restoreRowsSelections] " + Tools.showCallStack());

    ListSelectionModel model = tblTorrentList.getSelectionModel();

    if (srows != null && srows.length > 0) {
        for (int i = 0; i < srows.length; i++) {
            int j = srows[i];
            //j = tblTorrentList.convertRowIndexToModel(srows[i]);
            //j = tblTorrentList.getRowSorter().convertRowIndexToModel(srows[i]);
            model.addSelectionInterval(j, j);
            {
                //tblTorrentList.getSelectionModel().removeListSelectionListener(tblTorrentList);
                //enableListSelectionEvent = false;
                //tblTorrentList.setSelectionModel(model);
                //enableListSelectionEvent = true;
                //tblTorrentList.getSelectionModel().addListSelectionListener(torrentsSelectionListener);
            }
        }
        //tblTorrentList.setSelectionModel(model);
    }
    lockValueChanged = false;
}

From source file:com.diversityarrays.kdxplore.curate.TrialDataEditor.java

private void reapplySelection(List<PlotOrSpecimen> selectedPlotSpecimens,
        Set<TraitInstance> selectedTraitInstances) {
    boolean anySubPlot = false;
    List<Integer> selectedModelColumns = new ArrayList<>();
    for (TraitInstance ti : selectedTraitInstances) {
        if (TraitLevel.PLOT != ti.trait.getTraitLevel()) {
            anySubPlot = true;//  ww w. j  a v  a 2 s . com
        }
        Integer columnIndex = curationTableModel.getColumnIndexForTraitInstance(ti);
        if (columnIndex != null) {
            selectedModelColumns.add(columnIndex);
        }
    }

    boolean nonTraitColumns = false;
    if (selectedModelColumns.isEmpty()) {
        nonTraitColumns = true;
        selectedModelColumns.addAll(curationTableModel.getNonTraitInstanceColumns());
    }

    ListSelectionModel lsm = curationTable.getSelectionModel();

    try {
        curationTable.clearSelection();
        lsm.setValueIsAdjusting(true);

        // lsm.clearSelection(); above line already does this

        // For the curation table ...
        int countRowsFound = 0;

        if (anySubPlot) {
            // get all of the rows?
            Set<Integer> plotIds = selectedPlotSpecimens.stream().map(PlotOrSpecimen::getPlotId)
                    .collect(Collectors.toSet());
            for (Integer plotId : plotIds) {
                Integer[] modelRows = curationTableModel.getRowsForPlotId(plotId, TraitLevel.PLOT);
                for (Integer modelRow : modelRows) {
                    int viewRow = curationTable.convertRowIndexToView(modelRow);
                    if (viewRow >= 0) {
                        lsm.addSelectionInterval(viewRow, viewRow);
                    }
                }
            }
        } else {
            for (PlotOrSpecimen pos : selectedPlotSpecimens) {
                Optional<Integer> opt = curationTableModel.getRowForPlotOrSpecimen(pos);
                if (opt.isPresent()) {
                    Integer modelRow = opt.get();
                    int viewRow = curationTable.convertRowIndexToView(modelRow);
                    if (viewRow >= 0) {
                        ++countRowsFound;
                        lsm.addSelectionInterval(viewRow, viewRow);
                    }
                }
            }
        }
        //            Set<Integer> plotIds = selectedPlotSpecimens.stream().map(PlotOrSpecimen::getPlotId)
        //                    .collect(Collectors.toSet());
        //            for (Integer plotId : plotIds) {
        //                Integer[] modelRows = curationTableModel.getRowsForPlotId(plotId);
        //                for (Integer modelRow : modelRows) {
        //                    int viewRow = curationTable.convertRowIndexToView(modelRow);
        //                    if (viewRow >= 0) {
        //                        lsm.addSelectionInterval(viewRow, viewRow);
        //                    }
        //                }
        //            }

        StringBuilder msg = new StringBuilder();
        if (!selectedModelColumns.isEmpty()) {
            List<Integer> sorted = new ArrayList<>(selectedModelColumns);
            Collections.sort(sorted);
            for (Integer mcol : sorted) {
                int vcol = curationTable.convertColumnIndexToView(mcol);
                if (vcol >= 0) {
                    curationTable.addColumnSelectionInterval(vcol, vcol);
                }
            }

            if (!nonTraitColumns) {
                if (countRowsFound <= 0) {
                    msg.append("\tSamples Table: selected ").append(selectedModelColumns.size())
                            .append(" Traits");
                } else {
                    msg.append("\tSamples Table: selected ").append(countRowsFound).append(" for ")
                            .append(selectedModelColumns.size()).append(" Traits");
                }
                //                    msg.append("\tSamples Table: selected ").append(selectedModelColumns.size())
                //                            .append(" Traits");
                //                    if (!plotIds.isEmpty()) {
                //                        msg.append(" in ").append(plotIds.size()).append(" Plots");
                //                    }
            } else if (countRowsFound > 0) {
                msg.append("\tSamples Table: selected ").append(countRowsFound);
            }
            //                else if (!plotIds.isEmpty()) {
            //                    msg.append("\tSamples Table: selected ").append(plotIds.size())
            //                            .append(" Plots");
            //                }
        }

        //            if (!plotIds.isEmpty()) {
        //                if (msg.length() > 0)
        //                    msg.append('\n');
        //                msg.append("\tField View: selected ").append(plotIds.size()).append(" Plots");
        //            }

        if (msg.length() > 0) {
            messages.println(msg);
        }

        Set<Plot> plots = selectedPlotSpecimens.stream()
                .map(pos -> curationData.getPlotByPlotId(pos.getPlotId())).collect(Collectors.toSet());
        fieldLayoutView.setSelectedPlots(new ArrayList<>(plots));
    } finally {
        lsm.setValueIsAdjusting(false);
        curationTable.repaint();
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.WorkbenchPaneSS.java

/**
 * Converts the column contents from on format of Lat/Lon to another
 * @param columnIndex the index of the column being converted
 * @param converter the converter to use
 * @param outputFormat the format string
 * //from   w  w  w. j av  a 2  s.  c om
 * return number of non-blank cells that were NOT converted
 */
protected List<CellPosition> convertColumnContents(int columnIndex, int[] rows, GeoRefConverter converter,
        String outputFormat, LatLonConverter.LATLON latOrLon, LatLonConverter.DEGREES_FORMAT degFmt) {
    List<CellPosition> unconverted = new Vector<CellPosition>();

    if (columnIndex == -1) {
        return unconverted;
    }

    final int[] selectedRows = spreadSheet.getSelectedRows();
    final int[] selectedCols = spreadSheet.getSelectedColumns();
    for (int index = 0; index < rows.length; ++index) {
        int rowIndex = rows[index];
        String currentValue = null;
        //check backup col for original value before any conversions...
        currentValue = getLatLonSrc(columnIndex, rowIndex);
        if (currentValue != null) {
            currentValue = currentValue.replace("  ", " ");
        }
        if (StringUtils.isBlank(currentValue)) {
            currentValue = (String) model.getValueAt(rowIndex, columnIndex);
        }

        if (StringUtils.isBlank(currentValue)) {
            continue;
        }

        String convertedValue;
        try {
            convertedValue = converter.convert(StringUtils.stripToNull(currentValue), outputFormat, latOrLon,
                    degFmt);

        } catch (Exception e) {
            //UsageTracker.incrHandledUsageCount();
            //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(WorkbenchPaneSS.class, e);
            // this value didn't convert correctly
            // it would be nice to highlight that cell, but I don't know how we could do that
            log.warn("Could not convert contents of cell (" + (rowIndex + 1) + "," + (columnIndex + 1) + ")");
            unconverted.add(new CellPosition(rowIndex, columnIndex));
            continue;
        }

        model.setValueAt(convertedValue, rowIndex, columnIndex, !(converter instanceof GeoRefConverter));
        if (!currentValue.equals(convertedValue)) {
            setChanged(true);
        }
    }

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            ListSelectionModel selModel = spreadSheet.getSelectionModel();
            for (int rowIndex : selectedRows) {
                selModel.addSelectionInterval(rowIndex, rowIndex);
            }
            ListSelectionModel colSelModel = spreadSheet.getColumnModel().getSelectionModel();
            for (int colIndex : selectedCols) {
                colSelModel.addSelectionInterval(colIndex, colIndex);
            }
        }
    });
    return unconverted;
}

From source file:org.orbisgis.core.ui.plugins.views.geocatalog.Catalog.java

public void setActiveFiltersId(String[] filterIds) {
    ListSelectionModel selectionModel = lstFilters.getSelectionModel();
    ListModel filterModel = lstFilters.getModel();
    selectionModel.clearSelection();/*ww w.j ava  2s  . c  o  m*/
    selectionModel.setValueIsAdjusting(true);
    for (String filterId : filterIds) {
        GeocatalogFilterDecorator filter = new GeocatalogFilterDecorator(filterId, null, null);
        int index = -1;
        for (int i = 0; i < filterModel.getSize(); i++) {
            if (filterModel.getElementAt(i).equals(filter)) {
                index = i;
            }
        }
        if (index != -1) {
            selectionModel.addSelectionInterval(index, index);
        }
    }
    selectionModel.setValueIsAdjusting(false);
}

From source file:org.orbisgis.core.ui.plugins.views.geocatalog.Catalog.java

public void setActiveLabels(String[] labels) {
    ListSelectionModel selectionModel = lstTags.getSelectionModel();
    selectionModel.clearSelection();//from   w  w w .  jav  a2  s  . co m
    for (String label : labels) {
        selectionModel.setValueIsAdjusting(true);
        int index = -1;
        for (int i = 0; i < tagListModel.getSize(); i++) {
            if (tagListModel.getElementAt(i).equals(label)) {
                index = i;
            }
        }
        if (index != -1) {
            selectionModel.addSelectionInterval(index, index);
        }
    }
    selectionModel.setValueIsAdjusting(false);
}

From source file:org.processmining.analysis.performance.PerformanceAnalysisGUI.java

/**
 * Updates both the visualization and the performance metrics by taking only
 * the selected instances into account. If all instances have been selected,
 * the result corresponds to the initial state.
 *//*from ww w  . j ava  2  s . c  o  m*/
private void updateResults() {
    try {
        // check if selection is empty --> if so select whole log
        ListSelectionModel selectionModel = processInstanceIDsTable.getSelectionModel();
        if (selectionModel.isSelectionEmpty()) {
            selectionModel.addSelectionInterval(0, extendedLog.getSizeOfLog() - 1);
        }
        // invoke redraw of vizualization for selected process instances
        // only..
        ArrayList selectedInstanceIDs = getSelectedInstanceIDs();
        ArrayList selectedInstances = getSelectedInstances();

        grappaPanel = replayResult.getVisualization(selectedInstanceIDs);
        grappaPanel.addGrappaListener(new ExtendedGrappaAdapter());
        mapping = new HashMap();
        buildGraphMapping(mapping, grappaPanel.getSubgraph());
        modelContainer = new JScrollPane(grappaPanel);
        centerPanel.removeAll();
        centerPanel.add(modelContainer, BorderLayout.CENTER);
        centerPanel.validate();
        centerPanel.repaint();
        // update process metrics and display them
        displayProcessMetrics(selectedInstances);
        // calculate the values for the simulation model
        calculateValuesForSimulationModel(getSelectedInstances(), getSelectedInstanceIDs());
        // update the provided object (visualization might have changed)
        extendedPetriNet = (ExtendedPetriNet) replayResult.replayedPetriNet;
        // Make sure that what was selected before the update, is now
        // selected again.
        if (elt1 instanceof ExtendedTransition) {
            // transition was selected, select it again in sb1
            ExtendedTransition trans = (ExtendedTransition) elt1;
            sb1.setSelectedItem("Transition - " + trans.getLogEvent().getModelElementName() + " "
                    + trans.getLogEvent().getEventType());
            if (elt2 instanceof ExtendedTransition) {
                // another transition was selected too, select it again in
                // sb2
                trans = (ExtendedTransition) elt2;
                sb2.setSelectedItem("Transition - " + trans.getLogEvent().getModelElementName() + " "
                        + trans.getLogEvent().getEventType());
            }
        } else if (elt1 instanceof ExtendedPlace) {
            // place was selected, select it in sb1
            ExtendedPlace place = (ExtendedPlace) elt1;
            sb1.setSelectedItem("Place - " + place.getIdentifier());
        } else {
            // nothing selected, set sb1 and sb2 to index 0
            sb1.setSelectedIndex(0);
            sb2.setSelectedIndex(0);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "An internal error occured while\nupdating the visualization.");
        Message.add("Probably not found.\n" + ex.toString(), 2);
        ex.printStackTrace();
    }
}