Example usage for javax.swing ListSelectionModel isSelectedIndex

List of usage examples for javax.swing ListSelectionModel isSelectedIndex

Introduction

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

Prototype

boolean isSelectedIndex(int index);

Source Link

Document

Returns true if the specified index is selected.

Usage

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

protected void initComponent() {
    layout = new MigLayout("flowy, fill, insets 0", "", "[min!][fill]");
    panel = new JPanel(layout);

    topPanel = new JPanel(new BorderLayout());
    topPanel.setVisible(false);//from w w  w.  jav  a 2s .c o m
    panel.add(topPanel, "growx");

    scrollPane = new JScrollPane(impl);
    impl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    impl.setFillsViewportHeight(true);
    panel.add(scrollPane, "grow");

    impl.setShowGrid(true);
    impl.setGridColor(Color.lightGray);

    impl.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                handleClickAction();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            showPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            showPopup(e);
        }

        protected void showPopup(MouseEvent e) {
            if (e.isPopupTrigger() && contextMenuEnabled) {
                // select row
                Point p = e.getPoint();
                int viewRowIndex = impl.rowAtPoint(p);

                int rowNumber;
                if (viewRowIndex >= 0) {
                    rowNumber = impl.convertRowIndexToModel(viewRowIndex);
                } else {
                    rowNumber = -1;
                }
                ListSelectionModel model = impl.getSelectionModel();

                if (!model.isSelectedIndex(rowNumber)) {
                    model.setSelectionInterval(rowNumber, rowNumber);
                }

                // show popup menu
                JPopupMenu popupMenu = createPopupMenu();
                if (popupMenu.getComponentCount() > 0) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
    });

    ColumnControlButton columnControlButton = new ColumnControlButton(impl) {
        @Override
        protected ColumnVisibilityAction createColumnVisibilityAction(TableColumn column) {
            ColumnVisibilityAction columnVisibilityAction = super.createColumnVisibilityAction(column);

            columnVisibilityAction.addPropertyChangeListener(evt -> {
                if ("SwingSelectedKey".equals(evt.getPropertyName()) && evt.getNewValue() instanceof Boolean) {
                    ColumnVisibilityAction action = (ColumnVisibilityAction) evt.getSource();

                    String columnName = action.getActionCommand();
                    boolean collapsed = !((boolean) evt.getNewValue());

                    Column col = getColumn(columnName);
                    if (col != null) {
                        col.setCollapsed(collapsed);
                    }
                }
            });

            return columnVisibilityAction;
        }
    };
    impl.setColumnControl(columnControlButton);

    impl.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
    impl.getActionMap().put("enter", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (enterPressAction != null) {
                enterPressAction.actionPerform(DesktopAbstractTable.this);
            } else {
                handleClickAction();
            }
        }
    });

    Messages messages = AppBeans.get(Messages.NAME);
    // localize default column control actions
    for (Object actionKey : impl.getActionMap().allKeys()) {
        if ("column.packAll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packAll"));
        } else if ("column.packSelected".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packSelected"));
        } else if ("column.horizontalScroll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.horizontalScroll"));
        }
    }

    // Ability to configure fonts in table
    // Add action to column control
    String configureFontsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.configureFontsLabel");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "fonts",
            new AbstractAction(configureFontsLabel) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Component rootComponent = SwingUtilities.getRoot(impl);
                    final FontDialog fontDialog = FontDialog.show(rootComponent, impl.getFont());
                    fontDialog.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosed(WindowEvent e) {
                            Font result = fontDialog.getResult();
                            if (result != null) {
                                impl.setFont(result);
                                packRows();
                            }
                        }
                    });
                    fontDialog.open();
                }
            });

    // Ability to reset settings
    String resetSettingsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.resetSettings");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "resetSettings",
            new AbstractAction(resetSettingsLabel) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    resetPresentation();
                }
            });

    scrollPane.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            if (!columnsInitialized) {
                adjustColumnHeaders();
            }
            columnsInitialized = true;
        }
    });

    // init default row height
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            if (!fontInitialized) {
                applyFont(impl, impl.getFont());
            }
        }
    });
}

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

/**
 * Inverts the current selection status of the table containing the process
 * instances. This means that instances that have been selected will result
 * being unselected and the other way round.
 *//*  w  w  w. j  a v  a 2s.co m*/
private void invertSelectionStatus() {
    ListSelectionModel selectionModel = processInstanceIDsTable.getSelectionModel();
    // step through the table
    for (int index = 0; index < extendedLog.getSizeOfLog(); index++) {
        if (selectionModel.isSelectedIndex(index) == true) {
            // if entry is currently selected --> deselect
            selectionModel.removeSelectionInterval(index, index);
        } else {
            // if entry is currently not selected --> select
            selectionModel.addSelectionInterval(index, index);
        }
    }
    hideAllMetrics();
    updateResults();
}

From source file:org.ut.biolab.medsavant.client.view.genetics.charts.SummaryChart.java

private JPopupMenu createPopup(final Chart chart) {

    JPopupMenu menu = new JPopupMenu();

    //Filter by selections
    JMenuItem filter1Item = new JMenuItem("Filter by Selection" + (mapGenerator.isNumeric() ? "" : "(s)"));
    filter1Item.addActionListener(new ActionListener() {
        @Override/*  w w w  .j av a 2  s.  c  o  m*/
        public void actionPerformed(ActionEvent e) {

            ThreadController.getInstance().cancelWorkers(pageName);

            boolean isGender = mapGenerator.getName().equalsIgnoreCase(BasicPatientColumns.GENDER.getAlias());
            try {
                List<String> values = new ArrayList<String>();
                ListSelectionModel selectionModel = chart.getSelectionsForModel(chart.getModel(0));
                QueryViewController qvc = SearchBar.getInstance().getQueryViewController();
                for (int i = selectionModel.getMinSelectionIndex(); i <= selectionModel
                        .getMaxSelectionIndex(); i++) {
                    if (selectionModel.isSelectedIndex(i)) {
                        String v = ((ChartPoint) chart.getModel().getPoint(i)).getHighlight().name();
                        values.add(v);
                        if (mapGenerator.isNumeric() && !isGender) {
                            double low = 0;
                            double high = 0;

                            String[] s = v.split(" - ");
                            if (s.length < 2) {
                                LOG.error("Invalid range detected for numeric condition "
                                        + mapGenerator.getName() + " val=" + v);
                            }
                            NumberFormat format = NumberFormat.getInstance();
                            low = format.parse(s[0]).doubleValue();
                            high = format.parse(s[1]).doubleValue();

                            QueryUtils.addNumericQuery(chartName, low, high, false);
                        }
                    }
                }
                if (values.isEmpty()) {
                    return;
                }

                if (!mapGenerator.isNumeric() || isGender) {
                    QueryUtils.addMultiStringQuery(chartName, values);
                }

            } catch (Exception ex) {
                ClientMiscUtils.reportError("Error filtering by selection: %s", ex);
            }
        }
    });
    menu.add(filter1Item);

    return menu;
}

From source file:org.wings.SList.java

/**
 * Return an array of all of the selected indices.
 *
 * @return all selected indices./*  w  w w .j a v  a 2s . c o  m*/
 * @see #removeSelectionInterval
 * @see #addListSelectionListener
 */
public int[] getSelectedIndices() {
    ListSelectionModel sm = getSelectionModel();
    int iMin = sm.getMinSelectionIndex();
    int iMax = sm.getMaxSelectionIndex();

    if ((iMin < 0) || (iMax < 0)) {
        return new int[0];
    }

    int[] rvTmp = new int[1 + (iMax - iMin)];
    int n = 0;
    for (int i = iMin; i <= iMax; i++) {
        if (sm.isSelectedIndex(i)) {
            rvTmp[n++] = i;
        }
    }
    int[] rv = new int[n];
    System.arraycopy(rvTmp, 0, rv, 0, n);
    return rv;
}

From source file:org.wings.SList.java

/**
 * Return the values of the selected cells.
 * Returns only the selected elements which are in the model.
 * If the selection model indices a selection outside the the datamodel it is ignored
 *
 * @return the selected values/*from   ww w.  j a  v  a 2  s . com*/
 * @see #isSelectedIndex
 * @see #getModel
 * @see #addListSelectionListener
 */
public Object[] getSelectedValues() {
    ListSelectionModel sm = getSelectionModel();
    ListModel dm = getModel();

    int iMin = sm.getMinSelectionIndex();
    int iMax = sm.getMaxSelectionIndex();

    if ((iMin < 0) || (iMax < 0)) {
        return new Object[0];
    }

    Object[] rvTmp = new Object[1 + (iMax - iMin)];
    int n = 0;
    for (int i = iMin; i <= iMax; i++) {
        if (sm.isSelectedIndex(i) && i < dm.getSize()) {
            rvTmp[n++] = dm.getElementAt(i);
        }
    }
    Object[] rv = new Object[n];
    System.arraycopy(rvTmp, 0, rv, 0, n);
    return rv;
}