Search in sources :

Example 6 with ListSelectionModel

use of javax.swing.ListSelectionModel in project adempiere by adempiere.

the class MiniTable method toggleRowCheckedBySelection.

/**
     * Toggles the ID selection of the selected rows.  Multi-selection only.
     */
private void toggleRowCheckedBySelection() {
    if (isMultiSelection()) {
        ListSelectionModel rsm = this.getSelectionModel();
        int leadRow = rsm.getLeadSelectionIndex();
        int[] selectedRows = this.getSelectedRows();
        // Limit the number of row selection events to once per change
        if (selectedRows.length > 1)
            m_changingMultipleRows = true;
        else
            m_changingMultipleRows = false;
        for (int row : selectedRows) {
            toggleRowChecked(row);
        }
        //  Return the lead to its original location
        rsm.setLeadSelectionIndex(leadRow);
        if (m_changingMultipleRows) {
            fireRowSelectionEvent();
            m_changingMultipleRows = false;
        }
    }
}
Also used : DefaultListSelectionModel(javax.swing.DefaultListSelectionModel) ListSelectionModel(javax.swing.ListSelectionModel)

Example 7 with ListSelectionModel

use of javax.swing.ListSelectionModel in project adempiere by adempiere.

the class MiniTable method prepareRenderer.

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component c = super.prepareRenderer(renderer, row, column);
    JComponent jc = (JComponent) c;
    if (c == null)
        return c;
    //  Row is selected
    Color selectedColor = AdempierePLAF.getFieldBackground_Selected();
    //  Even row
    Color normalColor = AdempierePLAF.getFieldBackground_Normal();
    //  Odd row
    Color backColor = AdempierePLAF.getInfoBackground();
    //  Lead row border
    Color borderColor = AdempierePLAF.getFieldBackground_Mandatory();
    CompoundBorder cb = null;
    ListSelectionModel rsm = this.getSelectionModel();
    boolean readOnly = !this.isCellEditable(row, column);
    if (!(row == rsm.getLeadSelectionIndex())) {
        if (//  Highlighted but not the lead
        rsm.isSelectedIndex(row)) {
            c.setBackground(selectedColor);
            jc.setBorder(new MatteBorder(1, 1, 1, 1, selectedColor));
        } else if (//  Not selected but even in number
        row % 2 == 0) {
            c.setBackground(normalColor);
            jc.setBorder(new MatteBorder(1, 1, 1, 1, normalColor));
        } else //  Not selected and odd in number
        {
            // If not shaded, match the table's background
            c.setBackground(backColor);
            jc.setBorder(new MatteBorder(1, 1, 1, 1, backColor));
        }
        //  Buttons and checkboxes need to have the border turned on
        if (c.getClass().equals(JCheckBox.class)) {
            ((JCheckBox) c).setBorderPainted(false);
        } else if (c.getClass().equals(JButton.class)) {
            ((JButton) c).setBorderPainted(false);
        }
    } else {
        if (c.getClass().equals(JCheckBox.class)) {
            ((JCheckBox) c).setBorderPainted(true);
        } else if (c.getClass().equals(JButton.class)) {
            ((JButton) c).setBorderPainted(true);
        }
        //  Define border - compond border maintains the spacing of 1px around the field
        if (column == 0) {
            cb = new CompoundBorder(new EmptyBorder(new Insets(0, 0, 0, 1)), new MatteBorder(1, 1, 1, 0, borderColor));
        } else if (column == this.getColumnCount() - 1) {
            cb = new CompoundBorder(new EmptyBorder(new Insets(0, 1, 0, 0)), new MatteBorder(1, 0, 1, 1, borderColor));
        } else {
            cb = new CompoundBorder(new EmptyBorder(new Insets(0, 1, 0, 1)), new MatteBorder(1, 0, 1, 0, borderColor));
        }
        //  Set border
        jc.setBorder(cb);
        //  Set background color
        if (!readOnly && this.isRowChecked(row))
            c.setBackground(normalColor);
        else
            c.setBackground(selectedColor);
    }
    return c;
}
Also used : JCheckBox(javax.swing.JCheckBox) MatteBorder(javax.swing.border.MatteBorder) Insets(java.awt.Insets) Color(java.awt.Color) JComponent(javax.swing.JComponent) JButton(javax.swing.JButton) DefaultListSelectionModel(javax.swing.DefaultListSelectionModel) ListSelectionModel(javax.swing.ListSelectionModel) CompoundBorder(javax.swing.border.CompoundBorder) Component(java.awt.Component) JComponent(javax.swing.JComponent) EmptyBorder(javax.swing.border.EmptyBorder)

Example 8 with ListSelectionModel

use of javax.swing.ListSelectionModel in project adempiere by adempiere.

the class MiniTable method matchCheckWithSelectedRows.

/**
     * Match the row check with the row selection (highlight) in the table
     */
public void matchCheckWithSelectedRows() {
    int rows = this.getRowCount();
    if (this.getShowTotals())
        rows = rows - 1;
    if (rows <= 0)
        return;
    // Check if the lead row is selected, if not, select it
    ListSelectionModel rsm = this.getSelectionModel();
    int leadRow = rsm.getLeadSelectionIndex();
    if ((leadRow >= 0 && leadRow < rows && !rsm.isSelectedIndex(leadRow))) {
        if (isMultiSelection())
            rsm.addSelectionInterval(leadRow, leadRow);
        else
            rsm.setSelectionInterval(leadRow, leadRow);
    }
    //  In case
    if (this.getShowTotals()) {
        int totalRow = this.getRowCount() - 1;
        if (rsm.isSelectedIndex(totalRow))
            rsm.removeIndexInterval(totalRow, totalRow);
    }
    //  Set the id column to match the selection
    int[] selectedRows = this.getSelectedRows();
    m_changingMultipleRows = true;
    if (//  Not everything is selected
    selectedRows.length < rows) {
        //  Deselect everything
        for (int row = 0; row < rows; row++) {
            setRowChecked(row, false);
        }
    }
    //  Set the selected rows
    for (int row : selectedRows) {
        setRowChecked(row, true);
    }
    fireRowSelectionEvent();
    m_changingMultipleRows = false;
}
Also used : DefaultListSelectionModel(javax.swing.DefaultListSelectionModel) ListSelectionModel(javax.swing.ListSelectionModel)

Example 9 with ListSelectionModel

use of javax.swing.ListSelectionModel in project jabref by JabRef.

the class PreviewPrefsTab method setupLogic.

private void setupLogic() {
    chosen.getSelectionModel().addListSelectionListener(event -> {
        boolean selectionEmpty = ((ListSelectionModel) event.getSource()).isSelectionEmpty();
        btnLeft.setEnabled(!selectionEmpty);
        btnDown.setEnabled(!selectionEmpty);
        btnUp.setEnabled(!selectionEmpty);
    });
    available.getSelectionModel().addListSelectionListener(e -> btnRight.setEnabled(!((ListSelectionModel) e.getSource()).isSelectionEmpty()));
    btnRight.addActionListener(event -> {
        for (Object object : available.getSelectedValuesList()) {
            availableModel.removeElement(object);
            chosenModel.addElement(object);
        }
    });
    btnLeft.addActionListener(event -> {
        for (Object object : chosen.getSelectedValuesList()) {
            availableModel.addElement(object);
            chosenModel.removeElement(object);
        }
    });
    btnUp.addActionListener(event -> {
        List<Integer> newSelectedIndices = new ArrayList<>();
        for (int oldIndex : chosen.getSelectedIndices()) {
            boolean alreadyTaken = newSelectedIndices.contains(oldIndex - 1);
            int newIndex = (oldIndex > 0 && !alreadyTaken) ? oldIndex - 1 : oldIndex;
            chosenModel.add(newIndex, chosenModel.remove(oldIndex));
            newSelectedIndices.add(newIndex);
        }
        chosen.setSelectedIndices(ArrayUtils.toPrimitive(newSelectedIndices.toArray(new Integer[newSelectedIndices.size()])));
    });
    btnDown.addActionListener(event -> {
        List<Integer> newSelectedIndices = new ArrayList<>();
        int[] selectedIndices = chosen.getSelectedIndices();
        for (int i = selectedIndices.length - 1; i >= 0; i--) {
            int oldIndex = selectedIndices[i];
            boolean alreadyTaken = newSelectedIndices.contains(oldIndex + 1);
            int newIndex = (oldIndex < chosenModel.getSize() - 1 && !alreadyTaken) ? oldIndex + 1 : oldIndex;
            chosenModel.add(newIndex, chosenModel.remove(oldIndex));
            newSelectedIndices.add(newIndex);
        }
        chosen.setSelectedIndices(ArrayUtils.toPrimitive(newSelectedIndices.toArray(new Integer[newSelectedIndices.size()])));
    });
    btnDefault.addActionListener(event -> layout.setText(Globals.prefs.getPreviewPreferences().getPreviewStyleDefault().replace("__NEWLINE__", "\n")));
    btnTest.addActionListener(event -> {
        try {
            PreviewPanel testPane = new PreviewPanel(null, TestEntry.getTestEntry(), null).setFixedLayout(layout.getText());
            testPane.setPreferredSize(new Dimension(800, 350));
            JOptionPane.showMessageDialog(PreviewPrefsTab.this, new JScrollPane(testPane), Localization.lang("Preview"), JOptionPane.PLAIN_MESSAGE);
        } catch (StringIndexOutOfBoundsException exception) {
            LOGGER.warn("Parsing error.", exception);
            JOptionPane.showMessageDialog(null, Localization.lang("Parsing error") + ": " + Localization.lang("illegal backslash expression") + ".\n" + exception.getMessage(), Localization.lang("Parsing error"), JOptionPane.ERROR_MESSAGE);
        }
    });
}
Also used : JScrollPane(javax.swing.JScrollPane) ArrayList(java.util.ArrayList) ListSelectionModel(javax.swing.ListSelectionModel) PreviewPanel(org.jabref.gui.PreviewPanel) Dimension(java.awt.Dimension)

Example 10 with ListSelectionModel

use of javax.swing.ListSelectionModel in project sling by apache.

the class RequestListSelectionListener method valueChanged.

public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
        int idx = lsm.getMinSelectionIndex();
        if (idx >= 0) {
            try {
                idx = table.getRowSorter().convertRowIndexToModel(idx);
                TableModel tm = ((RequestTrackerFile) table.getModel()).getData(idx);
                if (dataField == null) {
                    dataField = new JTable();
                    dataField.setAutoCreateRowSorter(true);
                    dataField.setGridColor(Color.GRAY);
                    dataField.setShowGrid(true);
                    dataField.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    dataField.setRowSelectionAllowed(true);
                    dataField.setTableHeader(new JTableHeader(dataField.getColumnModel()));
                    dataField.setFont(new Font("Monospaced", dataField.getFont().getStyle(), dataField.getFont().getSize()));
                    dataField.setShowHorizontalLines(false);
                    //                        dataField.setIntercellSpacing(new Dimension(3, 5));
                    JDialog d = new JDialog(this.parent);
                    d.add(new JScrollPane(dataField));
                    d.addWindowListener(new WindowAdapter() {

                        @Override
                        public void windowClosing(WindowEvent e) {
                            dataField = null;
                        }
                    });
                    // setup location and size and ensure updating preferences
                    Util.setupComponentLocationSize(d, REQUEST_X, REQUEST_Y, REQUEST_WIDTH, REQUEST_HEIGHT, (int) screenSize.getWidth() / 4, (int) screenSize.getHeight() / 4, (int) screenSize.getWidth() / 2, (int) screenSize.getHeight() / 2);
                    d.setVisible(true);
                }
                dataField.setModel(tm);
                Util.setupColumnWidths(dataField.getColumnModel(), REQUEST_COLS);
            } catch (IOException e1) {
            // ignore
            }
        }
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) ListSelectionModel(javax.swing.ListSelectionModel) JTableHeader(javax.swing.table.JTableHeader) WindowAdapter(java.awt.event.WindowAdapter) IOException(java.io.IOException) Font(java.awt.Font) JTable(javax.swing.JTable) WindowEvent(java.awt.event.WindowEvent) TableModel(javax.swing.table.TableModel) JDialog(javax.swing.JDialog)

Aggregations

ListSelectionModel (javax.swing.ListSelectionModel)21 DefaultListSelectionModel (javax.swing.DefaultListSelectionModel)10 JScrollPane (javax.swing.JScrollPane)5 BorderLayout (java.awt.BorderLayout)4 Dimension (java.awt.Dimension)4 JButton (javax.swing.JButton)4 JLabel (javax.swing.JLabel)3 JTable (javax.swing.JTable)3 FlowLayout (java.awt.FlowLayout)2 ArrayList (java.util.ArrayList)2 JDialog (javax.swing.JDialog)2 JPanel (javax.swing.JPanel)2 TableColumnModel (javax.swing.table.TableColumnModel)2 TableModel (javax.swing.table.TableModel)2 FormBuilder (com.jgoodies.forms.builder.FormBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 Color (java.awt.Color)1 Component (java.awt.Component)1 Container (java.awt.Container)1 Font (java.awt.Font)1