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:Main.java

/**
 * Set the lead and anchor without affecting selection.
 *//*from  w w w. ja  va  2s.c om*/
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:SharedListSelectionHandler.java

public void valueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();

    int firstIndex = e.getFirstIndex();
    int lastIndex = e.getLastIndex();
    boolean isAdjusting = e.getValueIsAdjusting();
    System.out.println("Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting
            + "; selected indexes:");

    if (lsm.isSelectionEmpty()) {
        System.out.println(" <none>");
    } else {/*from   w ww .jav  a2 s. com*/
        // Find out which indexes are selected.
        int minIndex = lsm.getMinSelectionIndex();
        int maxIndex = lsm.getMaxSelectionIndex();
        for (int i = minIndex; i <= maxIndex; i++) {
            if (lsm.isSelectedIndex(i)) {
                System.out.println(" " + i);
            }
        }
    }
}

From source file:com.kstenschke.copypastestack.ToolWindow.java

/**
 * Copy selected items back into clipboard
 *//*from  ww w .ja v  a  2s  .  c  o  m*/
public void copySelectedItems() {
    boolean hasSelection = !this.form.listClipItems.isSelectionEmpty();
    if (hasSelection) {
        ListModel<String> listModel = this.form.listClipItems.getModel();
        ListSelectionModel selectionModel = this.form.listClipItems.getSelectionModel();

        int amountItems = listModel.getSize();

        for (int i = 0; i < amountItems; i++) {
            if (selectionModel.isSelectedIndex(i)) {
                String currentItemText = listModel.getElementAt(i);
                UtilsEnvironment.copyToClipboard(currentItemText);
            }
        }
    }
}

From source file:com.kstenschke.copypastestack.ToolWindow.java

/**
 * Paste all / selected items into editor
 *///from  w  ww.j a  v a 2s . c om
public void pasteItems() {
    boolean hasSelection = !this.form.listClipItems.isSelectionEmpty();
    int amountSelected = !hasSelection ? 0 : this.form.listClipItems.getSelectedIndices().length;
    boolean focusEditor = this.form.checkboxFocusOnPaste.isSelected();

    String wrapBefore = "";
    String wrapAfter = "";
    String wrapDelimiter = "";

    if (this.form.checkBoxWrap.isSelected()) {
        wrapBefore = this.form.textFieldWrapBefore.getText();
        wrapAfter = this.form.textFieldWrapAfter.getText();
        wrapDelimiter = this.form.textFieldWrapDelimiter.getText();

        if (this.form.checkboxWrapExtended.isSelected()) {
            wrapBefore = UtilsString.convertWhitespace(wrapBefore);
            wrapAfter = UtilsString.convertWhitespace(wrapAfter);
            wrapDelimiter = UtilsString.convertWhitespace(wrapDelimiter);
        }
    }

    if (!hasSelection || amountSelected > 1) {
        // Insert multiple items
        ListModel<String> listModel = this.form.listClipItems.getModel();
        ListSelectionModel selectionModel = this.form.listClipItems.getSelectionModel();

        int amountItems = listModel.getSize();
        int amountInserted = 0;
        for (int i = 0; i < amountItems; i++) {
            if (selectionModel.isSelectedIndex(i)) {
                String currentItemText = listModel.getElementAt(i);
                UtilsEnvironment.insertInEditor(wrapBefore + currentItemText + wrapAfter
                        + (amountInserted + 1 < amountSelected ? wrapDelimiter : ""), focusEditor);
                amountInserted++;
            }
        }
    } else {
        // Insert selected item
        Object itemValue = this.form.listClipItems.getSelectedValue();
        if (itemValue != null) {
            String itemText = itemValue.toString();
            if (this.form.checkBoxWrap.isSelected()) {
                itemText = wrapBefore + itemText + wrapAfter;
            }
            UtilsEnvironment.insertInEditor(itemText, focusEditor);
        }
    }
}

From source file:gdt.jgui.tool.JEntityEditor.java

private void deleteRows() {
    try {//from   w  w w  .ja  va  2s . c o  m
        JScrollPane scrollPane = (JScrollPane) tabbedPane.getSelectedComponent();
        JTable table = (JTable) scrollPane.getViewport().getView();
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        ListSelectionModel listModel = table.getSelectionModel();
        int rCnt = table.getRowCount();
        ArrayList<Integer> srl = new ArrayList<Integer>();
        for (int i = 0; i < rCnt; i++)
            if (listModel.isSelectedIndex(i)) {
                srl.add(new Integer(i));
            }
        Integer[] sra = srl.toArray(new Integer[0]);
        ArrayList<Core> ol = new ArrayList<Core>();
        Core row;
        boolean skip;
        for (int i = 0; i < rCnt; i++) {
            skip = false;
            for (int aSra : sra) {
                if (i == aSra) {
                    skip = true;
                    break;
                }
            }
            if (!skip) {
                row = new Core((String) tableModel.getValueAt(i, 0), (String) tableModel.getValueAt(i, 1),
                        (String) tableModel.getValueAt(i, 2));
                ol.add(row);
            }
        }
        Core[] ra = ol.toArray(new Core[0]);
        while (tableModel.getRowCount() > 0)
            tableModel.removeRow(0);
        for (Core aRa : ra) {
            tableModel.addRow(new String[] { aRa.type, aRa.name, aRa.value });
        }
    } catch (Exception e) {
        LOGGER.severe(e.toString());
    }
}

From source file:com.kstenschke.copypastestack.ToolWindow.java

/**
 * @return  String[]//from ww w  .  j a va  2 s. c o m
 */
private String[] getUnselectedItems() {
    ListModel<String> listModel = this.form.listClipItems.getModel();
    ListSelectionModel selectionModel = this.form.listClipItems.getSelectionModel();
    int[] selectedIndices = this.form.listClipItems.getSelectedIndices();

    int amountItems = listModel.getSize();
    String[] unselectedItems = new String[amountItems - selectedIndices.length];

    int index = 0;
    for (int i = 0; i < amountItems; i++) {
        if (!selectionModel.isSelectedIndex(i)) {
            String item = listModel.getElementAt(i);
            if (!item.trim().isEmpty()) {
                unselectedItems[index] = item;
                index++;
            }
        }
    }

    return unselectedItems;
}

From source file:gdt.jgui.tool.JEntityEditor.java

private Core[] getContent(boolean selected) {
    try {/*w w  w  . j  a  v  a 2 s .c  om*/
        JScrollPane scrollPane = (JScrollPane) tabbedPane.getSelectedComponent();
        JTable table = (JTable) scrollPane.getViewport().getView();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        ListSelectionModel listModel = table.getSelectionModel();
        int cnt = model.getRowCount();
        if (cnt < 1)
            return null;
        ArrayList<Core> cl = new ArrayList<Core>();
        for (int i = 0; i < cnt; i++) {
            if (selected)
                if (!listModel.isSelectedIndex(i))
                    continue;
            cl.add(new Core((String) model.getValueAt(i, 0), (String) model.getValueAt(i, 1),
                    (String) model.getValueAt(i, 2)));
        }
        return cl.toArray(new Core[0]);
    } catch (Exception e) {
        LOGGER.severe(e.toString());
    }
    return null;
}

From source file:gdt.jgui.entity.fields.JFieldsEditor.java

private void deleteRows() {
    try {/*from www  . jav a 2s  .  com*/
        JTable table = (JTable) scrollPane.getViewport().getView();
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        ListSelectionModel listModel = table.getSelectionModel();
        int rCnt = table.getRowCount();
        ArrayList<Integer> srl = new ArrayList<Integer>();
        for (int i = 0; i < rCnt; i++)
            if (listModel.isSelectedIndex(i)) {
                srl.add(new Integer(i));
            }
        Integer[] sra = srl.toArray(new Integer[0]);
        ArrayList<Core> ol = new ArrayList<Core>();
        Core row;
        boolean skip;
        for (int i = 0; i < rCnt; i++) {
            skip = false;
            for (int aSra : sra) {
                if (i == aSra) {
                    skip = true;
                    break;
                }
            }
            if (!skip) {
                row = new Core(null, (String) tableModel.getValueAt(i, 0),
                        (String) tableModel.getValueAt(i, 1));
                ol.add(row);
            }
        }
        Core[] ra = ol.toArray(new Core[0]);
        while (tableModel.getRowCount() > 0)
            tableModel.removeRow(0);
        for (Core aRa : ra) {
            tableModel.addRow(new String[] { aRa.name, aRa.value });
        }
    } catch (Exception e) {
        LOGGER.severe(e.toString());
    }
}

From source file:gdt.jgui.entity.fields.JFieldsEditor.java

private void copy(boolean cut) {
    try {//  w  w w  .  ja v a2  s  . c o  m
        JTable table = (JTable) scrollPane.getViewport().getView();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        ListSelectionModel listModel = table.getSelectionModel();
        int rCnt = table.getRowCount();
        ArrayList<Integer> srl = new ArrayList<Integer>();
        for (int i = 0; i < rCnt; i++)
            if (listModel.isSelectedIndex(i)) {
                srl.add(new Integer(i));
            }
        Integer[] sra = srl.toArray(new Integer[0]);
        ArrayList<String> sl = new ArrayList<String>();
        Properties locator = new Properties();
        locator.setProperty(Locator.LOCATOR_TYPE, LOCATOR_TYPE_FIELD);
        if (cut)
            locator.setProperty(JRequester.REQUESTER_ACTION, ACTION_CUT_FIELDS);
        else
            locator.setProperty(JRequester.REQUESTER_ACTION, ACTION_COPY_FIELDS);
        locator.setProperty(Entigrator.ENTIHOME, entihome$);
        locator.setProperty(EntityHandler.ENTITY_KEY, entityKey$);
        String locator$;
        console.clipboard.clear();
        for (int i = 0; i < rCnt; i++) {
            for (int aSra : sra) {
                if (i == aSra) {
                    locator.setProperty(CELL_FIELD_NAME, (String) model.getValueAt(i, 0));
                    locator.setProperty(Locator.LOCATOR_TITLE, (String) model.getValueAt(i, 0));
                    locator.setProperty(CELL_FIELD_VALUE, (String) model.getValueAt(i, 1));
                    locator$ = Locator.toString(locator);
                    //                System.out.println("FieldsEditor:copy:locator="+locator$);
                    console.clipboard.putString(locator$);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.severe(e.toString());
    }
}

From source file:gdt.jgui.entity.query.JQueryPanel.java

/**
 * Get the context menu./*from  w ww  .ja  v  a  2s  . com*/
 * @return the context menu.
 */
@Override
public JMenu getContextMenu() {
    menu = new JMenu("Context");
    menu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            menu.removeAll();
            //            System.out.println("BookmarksEditor:getConextMenu:menu selected");
            JMenuItem selectItem = new JMenuItem("Select");
            selectItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showHeader();
                    showContent();
                }
            });
            menu.add(selectItem);
            JMenuItem clearHeader = new JMenuItem("Clear all");
            clearHeader.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    clearHeader();
                    showHeader();
                    showContent();
                }
            });
            menu.add(clearHeader);
            Entigrator entigrator = console.getEntigrator(entihome$);
            Sack query = entigrator.getEntityAtKey(entityKey$);
            if (query.getElementItem("parameter", "noreset") == null) {
                JMenuItem resetItem = new JMenuItem("Reset");
                resetItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int response = JOptionPane.showConfirmDialog(console.getContentPanel(),
                                "Reset source to default ?", "Confirm", JOptionPane.YES_NO_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        if (response == JOptionPane.YES_OPTION)
                            reset();
                    }
                });
                menu.add(resetItem);
            }
            JMenuItem folderItem = new JMenuItem("Open folder");
            folderItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        File file = new File(entihome$ + "/" + entityKey$);
                        Desktop.getDesktop().open(file);
                    } catch (Exception ee) {
                        Logger.getLogger(getClass().getName()).info(ee.toString());
                    }
                }
            });
            menu.add(folderItem);

            menu.addSeparator();
            JMenuItem addHeader = new JMenuItem("Add column");
            addHeader.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    addHeader();
                }
            });
            menu.add(addHeader);
            JMenuItem removeColumn = new JMenuItem("Remove column ");
            removeColumn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    removeColumn();
                }
            });
            menu.add(removeColumn);
            ListSelectionModel lsm = table.getSelectionModel();
            if (!lsm.isSelectionEmpty()) {
                JMenuItem excludeRows = new JMenuItem("Exclude rows ");
                excludeRows.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Entigrator entigrator = console.getEntigrator(entihome$);
                        Sack query = entigrator.getEntityAtKey(entityKey$);
                        if (!query.existsElement("exclude"))
                            query.createElement("exclude");
                        //else
                        //   query.clearElement("exclude");
                        ListSelectionModel lsm = table.getSelectionModel();
                        int minIndex = lsm.getMinSelectionIndex();
                        int maxIndex = lsm.getMaxSelectionIndex();
                        for (int i = minIndex; i <= maxIndex; i++) {
                            if (lsm.isSelectedIndex(i)) {
                                System.out.println("JQueryPanel:exclude rows:label=" + table.getValueAt(i, 1));
                                query.putElementItem("exclude",
                                        new Core(null, (String) table.getValueAt(i, 1), null));
                            }
                        }
                        entigrator.save(query);
                        showHeader();
                        showContent();
                    }
                });
                menu.add(excludeRows);
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {
        }

        @Override
        public void menuCanceled(MenuEvent e) {
        }
    });
    return menu;
}