Example usage for javax.swing JList isSelectedIndex

List of usage examples for javax.swing JList isSelectedIndex

Introduction

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

Prototype

public boolean isSelectedIndex(int index) 

Source Link

Document

Returns true if the specified index is selected, else false .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "A", "B", "C", "D" };
    JList list = new JList(items);

    int index = 2;
    boolean isSel = list.isSelectedIndex(index);
}

From source file:corelyzer.ui.CorelyzerGLCanvas.java

private void updateMainFrameListSelection(final int track, final int section, final MouseEvent event) {
    CorelyzerApp app = CorelyzerApp.getApp();
    if (app == null) {
        return;/*  w ww  .j  av a 2s .  c o m*/
    }

    if (track >= 0) {
        // Now, we need to traverse app's list model
        // to find match of native id
        // index conversion (native to java list)

        CRDefaultListModel sessionModel = app.getSessionListModel();
        int sessionIndex = -1;
        TrackSceneNode trackNode = null;
        for (int i = 0; i < sessionModel.size(); i++) {
            Session session = (Session) sessionModel.elementAt(i);
            trackNode = session.getTrackSceneNodeWithTrackId(track);

            if (trackNode != null) {
                sessionIndex = i;
            }
        }

        if (sessionIndex < 0) {
            return;
        }

        // Set selected session
        app.getSessionList().setSelectedIndex(sessionIndex);

        // Track
        int ssize;
        boolean found = false;
        CRDefaultListModel tmodel = app.getTrackListModel();
        // tsize = tmodel.getSize();
        TrackSceneNode tt;
        CoreSection cs = null;

        for (int i = 0; i < tmodel.size() && !found; i++) {
            tt = (TrackSceneNode) tmodel.elementAt(i);

            if (track == tt.getId()) {
                selectedTrackIndex = i;
                ssize = tt.getNumCores();

                for (int j = 0; j < ssize; j++) {
                    cs = tt.getCoreSection(j);
                    if (section == cs.getId()) {
                        selectedTrackSectionIndex = j;
                        found = true;
                        break;
                    }
                }
            }
        }

        if (!found || cs == null) {
            return;
        }

        // update ui
        CorelyzerApp.getApp().getTrackList().setSelectedIndex(selectedTrackIndex);
        JList secList = CorelyzerApp.getApp().getSectionList();
        boolean selected = secList.isSelectedIndex(selectedTrackSectionIndex);
        List<Integer> indices = new ArrayList<Integer>();
        indices.addAll(Arrays.asList(ArrayUtils.toObject(secList.getSelectedIndices())));
        if (event.isControlDown() || (event.isMetaDown() && CorelyzerApp.MAC_OS_X)) { // toggle selection
            if (indices.contains(selectedTrackSectionIndex))
                indices.remove(new Integer(selectedTrackSectionIndex));
            else
                indices.add(selectedTrackSectionIndex);

            int[] newSelArray = ArrayUtils.toPrimitive(indices.toArray(new Integer[0]));
            secList.setSelectedIndices(newSelArray);
        } else if (event.isShiftDown()) { // select range
            int[] toSel = null;
            if (indices.size() == 0) {
                toSel = makeRangeArray(0, selectedTrackSectionIndex);
            } else {
                final int minSel = Collections.min(indices);
                final int maxSel = Collections.max(indices);
                if (selectedTrackSectionIndex < minSel) {
                    toSel = makeRangeArray(selectedTrackSectionIndex, minSel);
                } else if (selectedTrackSectionIndex > maxSel) {
                    toSel = makeRangeArray(maxSel, selectedTrackSectionIndex);
                }
            }
            secList.setSelectedIndices(toSel);
        } else if (!(event.isAltDown() && selected)) {
            // don't modify selection if Alt is down and section was already
            // selected...user is presumably trying to move it
            secList.setSelectedIndex(selectedTrackSectionIndex);
        }

        CRDefaultListModel lm = CorelyzerApp.getApp().getSectionListModel();
        String secName = null;
        if (lm != null) {
            Object selSec = lm.getElementAt(selectedTrackSectionIndex);
            if (selSec != null) {
                secName = selSec.toString();
            } else {
                System.out.println("no object at index");
            }
        } else {
            System.out.println("no list model");
        }

        JMenuItem title = (JMenuItem) this.scenePopupMenu.getComponent(0);
        String trackName = CorelyzerApp.getApp().getTrackListModel().getElementAt(selectedTrackIndex)
                .toString();
        title.setText("Track: " + trackName);
        JMenuItem stitle = (JMenuItem) this.scenePopupMenu.getComponent(1);
        stitle.setText("Section: " + secName);

        // Enable section-based popupMenu options
        this.setEnableSectionBasedPopupMenuOptions(true);

        // 2/5/2012 brg: check Stagger Sections menu item if necessary
        final boolean trackIsStaggered = SceneGraph.trackIsStaggered(selectedTrack);
        AbstractButton ab = (AbstractButton) this.scenePopupMenu.getComponent(14);
        ab.getModel().setSelected(trackIsStaggered);

        // check section and graph lock menu items
        final boolean sectionIsLocked = !SceneGraph.isSectionMovable(selectedTrack, selectedTrackSection);
        ab = (AbstractButton) this.scenePopupMenu.getComponent(7);
        ab.getModel().setSelected(sectionIsLocked);
        final boolean sectionGraphIsLocked = !SceneGraph.isSectionGraphMovable(selectedTrack,
                selectedTrackSection);
        ab = (AbstractButton) this.scenePopupMenu.getComponent(8);
        ab.getModel().setSelected(sectionGraphIsLocked);

        CoreSectionImage csImg = cs.getCoreSectionImage();
        if (csImg != null && csImg.getId() != -1) {
            this.propertyMenuItem.setEnabled(true);
            this.splitMenuItem.setEnabled(true);
        } else {
            this.propertyMenuItem.setEnabled(false);
            this.splitMenuItem.setEnabled(false);
        }

        // System.out.println("---> [in Java DS] Picked Track " + track +
        // " and Track Section " + section);
        // String secname = CorelyzerApp.getApp().getSectionListModel().
        // getElementAt(section).toString();
        // System.out.println("---> [INFO] Section " + secname +
        // " is selected");
    } else {
        JMenuItem title = (JMenuItem) this.scenePopupMenu.getComponent(0);
        title.setText("Track: N/A");
        JMenuItem stitle = (JMenuItem) this.scenePopupMenu.getComponent(1);
        stitle.setText("Section: N/A");

        // disable section based items
        this.setEnableSectionBasedPopupMenuOptions(false);
    }
}