Search in sources :

Example 51 with ListSelectionEvent

use of javax.swing.event.ListSelectionEvent in project JMRI by JMRI.

the class CombinedLocoSelListPane method layoutDecoderSelection.

/**
     * Create the panel used to select the decoder
     */
@Override
protected JPanel layoutDecoderSelection() {
    JPanel pane1a = new JPanel();
    pane1a.setLayout(new BoxLayout(pane1a, BoxLayout.X_AXIS));
    pane1a.add(new JLabel("Decoder installed: "));
    // create the list of manufacturers
    mMfgList = new JList<>();
    updateMfgListContents(null);
    mMfgList.clearSelection();
    mMfgList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    mMfgListener = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!mMfgList.isSelectionEmpty()) {
                // manufacturer selected, update decoder list
                String vMfg = mMfgList.getSelectedValue();
                try {
                    int vMfgID = Integer.parseInt(DecoderIndexFile.instance().mfgIdFromName(vMfg));
                    listDecodersFromMfg(vMfgID, vMfg);
                } catch (java.lang.NumberFormatException ex) {
                // mfg number lookup failed for some reason
                }
            } else {
            // no manufacturer selected, do nothing
            }
        }
    };
    mMfgList.addListSelectionListener(mMfgListener);
    mDecoderList = new JList<String>(DecoderIndexFile.instance().matchingComboBox(null, null, null, null, null, null).getModel());
    mDecoderList.clearSelection();
    mDecoderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    mDecoderListener = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!mDecoderList.isSelectionEmpty()) {
                // decoder selected - reset and disable loco selection
                locoBox.setSelectedIndex(0);
                go2.setEnabled(true);
                go2.setToolTipText("Click to open the programmer");
                updateMfgListToSelectedDecoder();
            } else {
                // decoder not selected - require one
                go2.setEnabled(false);
                go2.setToolTipText("Select a locomotive or decoder to enable");
            }
        }
    };
    mDecoderList.addListSelectionListener(mDecoderListener);
    pane1a.add(new JScrollPane(mMfgList));
    pane1a.add(new JScrollPane(mDecoderList));
    iddecoder = new JToggleButton("Ident");
    iddecoder.setToolTipText("Read the decoders mfg and version, then attempt to select its type");
    if (jmri.InstanceManager.getNullableDefault(jmri.ProgrammerManager.class) != null && jmri.InstanceManager.getDefault(jmri.ProgrammerManager.class).getGlobalProgrammer() != null && !jmri.InstanceManager.getDefault(jmri.ProgrammerManager.class).getGlobalProgrammer().getCanRead()) {
        // can't read, disable the button
        iddecoder.setEnabled(false);
        iddecoder.setToolTipText("Button disabled because configured command station can't read CVs");
    }
    iddecoder.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            if (log.isDebugEnabled()) {
                log.debug("identify decoder pressed");
            }
            startIdentifyDecoder();
        }
    });
    pane1a.add(iddecoder);
    pane1a.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
    return pane1a;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) BoxLayout(javax.swing.BoxLayout) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JLabel(javax.swing.JLabel) ListSelectionListener(javax.swing.event.ListSelectionListener) JToggleButton(javax.swing.JToggleButton) ActionListener(java.awt.event.ActionListener)

Example 52 with ListSelectionEvent

use of javax.swing.event.ListSelectionEvent in project JMRI by JMRI.

the class ManageBackupsDialog method initComponents.

private void initComponents() {
    setModalityType(ModalityType.DOCUMENT_MODAL);
    setModal(true);
    setTitle(Bundle.getMessage("ManageBackupsDialog.manageBackupSets"));
    setBounds(100, 100, 461, 431);
    BorderLayout borderLayout = new BorderLayout();
    borderLayout.setVgap(5);
    borderLayout.setHgap(5);
    getContentPane().setLayout(borderLayout);
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    GridBagLayout gbl_contentPanel = new GridBagLayout();
    gbl_contentPanel.columnWidths = new int[] { 0, 0 };
    gbl_contentPanel.rowHeights = new int[] { 0, 0, 0 };
    gbl_contentPanel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
    gbl_contentPanel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
    contentPanel.setLayout(gbl_contentPanel);
    {
        selectBackupSetsLabel = new JLabel(Bundle.getMessage("ManageBackupsDialog.instructionsLabel.text"));
        GridBagConstraints gbc_selectBackupSetsLabel = new GridBagConstraints();
        gbc_selectBackupSetsLabel.anchor = GridBagConstraints.WEST;
        gbc_selectBackupSetsLabel.insets = new Insets(0, 0, 5, 0);
        gbc_selectBackupSetsLabel.gridx = 0;
        gbc_selectBackupSetsLabel.gridy = 0;
        contentPanel.add(selectBackupSetsLabel, gbc_selectBackupSetsLabel);
    }
    {
        scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 1;
        contentPanel.add(scrollPane, gbc_scrollPane);
        {
            setList = new JList<BackupSet>();
            setList.setVisibleRowCount(20);
            model = new DefaultListModel<BackupSet>();
            // Load up the list control with the available BackupSets
            for (BackupSet bs : backup.getBackupSets()) {
                model.addElement(bs);
            }
            setList.setModel(model);
            // Update button states based on if anything is selected in the
            // list.
            setList.addListSelectionListener(new ListSelectionListener() {

                @Override
                public void valueChanged(ListSelectionEvent e) {
                    updateButtonStates();
                }
            });
            scrollPane.setViewportView(setList);
        }
    }
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            selectAllButton = new JButton(Bundle.getMessage("ManageBackupsDialog.selectAllButton.text"));
            selectAllButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    do_selectAllButton_actionPerformed(e);
                }
            });
            buttonPane.add(selectAllButton);
        }
        {
            horizontalStrut = Box.createHorizontalStrut(10);
            buttonPane.add(horizontalStrut);
        }
        {
            clearAllButton = new JButton(Bundle.getMessage("ManageBackupsDialog.clearAllButton.text"));
            clearAllButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    do_clearAllButton_actionPerformed(e);
                }
            });
            buttonPane.add(clearAllButton);
        }
        {
            horizontalGlue = Box.createHorizontalGlue();
            buttonPane.add(horizontalGlue);
        }
        {
            deleteButton = new JButton(Bundle.getMessage("ButtonDelete"));
            deleteButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    do_deleteButton_actionPerformed(e);
                }
            });
            deleteButton.setActionCommand("");
            buttonPane.add(deleteButton);
        }
        {
            horizontalStrut_1 = Box.createHorizontalStrut(10);
            buttonPane.add(horizontalStrut_1);
        }
        {
            JButton closeButton = new JButton(Bundle.getMessage("ButtonCancel"));
            closeButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    do_cancelButton_actionPerformed(e);
                }
            });
            // NOI18N
            closeButton.setActionCommand("Cancel");
            getRootPane().setDefaultButton(closeButton);
            buttonPane.add(closeButton);
        }
        {
            horizontalStrut_2 = Box.createHorizontalStrut(10);
            buttonPane.add(horizontalStrut_2);
        }
    // {
    // helpButton = new JButton(Bundle.getMessage("BackupDialog.helpButton.text"));
    // helpButton.addActionListener(new ActionListener() {
    // public void actionPerformed(ActionEvent e) {
    // do_helpButton_actionPerformed(e);
    // }
    // });
    // helpButton.setEnabled(false);
    // buttonPane.add(helpButton);
    // }
    }
    updateButtonStates();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) ListSelectionListener(javax.swing.event.ListSelectionListener) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) EmptyBorder(javax.swing.border.EmptyBorder)

Example 53 with ListSelectionEvent

use of javax.swing.event.ListSelectionEvent in project JMRI by JMRI.

the class RosterFrame method createTop.

JComponent createTop() {
    Object selectedRosterGroup = prefsMgr.getProperty(getWindowFrameRef(), SELECTED_ROSTER_GROUP);
    groups = new RosterGroupsPanel((selectedRosterGroup != null) ? selectedRosterGroup.toString() : null);
    groups.setNewWindowMenuAction(this.getNewWindowAction());
    setTitle(groups.getSelectedRosterGroup());
    final JPanel rosters = new JPanel();
    rosters.setLayout(new BorderLayout());
    // set up roster table
    rtable = new RosterTable(true, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    rtable.setRosterGroup(this.getSelectedRosterGroup());
    rtable.setRosterGroupSource(groups);
    rosters.add(rtable, BorderLayout.CENTER);
    // add selection listener
    rtable.getTable().getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
        JTable table = rtable.getTable();
        if (!e.getValueIsAdjusting()) {
            if (rtable.getSelectedRosterEntries().length == 1 && table.getSelectedRow() >= 0) {
                log.debug("Selected row ", table.getSelectedRow());
                locoSelected(rtable.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), RosterTableModel.IDCOL).toString());
            } else if (rtable.getSelectedRosterEntries().length > 1 || table.getSelectedRow() < 0) {
                locoSelected(null);
            }
        // leave last selected item visible if no selection
        }
    });
    //Set all the sort and width details of the table first.
    String rostertableref = getWindowFrameRef() + ":roster";
    rtable.getTable().setName(rostertableref);
    // Allow only one column to be sorted at a time - 
    // Java allows multiple column sorting, but to effectly persist that, we
    // need to be intelligent about which columns can be meaningfully sorted
    // with other columns; this bypasses the problem by only allowing the
    // last column sorted to affect sorting
    RowSorterUtil.addSingleSortableColumnListener(rtable.getTable().getRowSorter());
    // Reset and then persist the table's ui state
    JTablePersistenceManager tpm = InstanceManager.getNullableDefault(JTablePersistenceManager.class);
    if (tpm != null) {
        tpm.resetState(rtable.getTable());
        tpm.persist(rtable.getTable());
    }
    rtable.getTable().setDragEnabled(true);
    rtable.getTable().setTransferHandler(new TransferHandler() {

        @Override
        public int getSourceActions(JComponent c) {
            return TransferHandler.COPY;
        }

        @Override
        public Transferable createTransferable(JComponent c) {
            JTable table = rtable.getTable();
            ArrayList<String> Ids = new ArrayList<>(table.getSelectedRowCount());
            for (int i = 0; i < table.getSelectedRowCount(); i++) {
                Ids.add(rtable.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRows()[i]), RosterTableModel.IDCOL).toString());
            }
            return new RosterEntrySelection(Ids);
        }

        @Override
        public void exportDone(JComponent c, Transferable t, int action) {
        // nothing to do
        }
    });
    MouseListener rosterMouseListener = new rosterPopupListener();
    rtable.getTable().addMouseListener(rosterMouseListener);
    try {
        clickDelay = ((Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"));
    } catch (RuntimeException e) {
        clickDelay = 500;
        log.debug("Unable to get the double click speed, Using JMRI default of half a second" + e.toString());
    }
    // assemble roster/groups splitpane
    rosterGroupSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, groups, rosters);
    rosterGroupSplitPane.setOneTouchExpandable(true);
    // emphasis rosters
    rosterGroupSplitPane.setResizeWeight(0);
    Object w = prefsMgr.getProperty(getWindowFrameRef(), "rosterGroupPaneDividerLocation");
    if (w != null) {
        groupSplitPaneLocation = (Integer) w;
        rosterGroupSplitPane.setDividerLocation(groupSplitPaneLocation);
    }
    if (!Roster.getDefault().getRosterGroupList().isEmpty()) {
        if (prefsMgr.getSimplePreferenceState(this.getClass().getName() + ".hideGroups")) {
            hideGroupsPane(true);
        }
    } else {
        enableRosterGroupMenuItems(false);
    }
    PropertyChangeListener propertyChangeListener = (PropertyChangeEvent changeEvent) -> {
        JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
        String propertyName = changeEvent.getPropertyName();
        if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
            int current = sourceSplitPane.getDividerLocation();
            hideGroups = current <= 1;
            Integer last = (Integer) changeEvent.getNewValue();
            if (current >= 2) {
                groupSplitPaneLocation = current;
            } else if (last >= 2) {
                groupSplitPaneLocation = last;
            }
        }
    };
    groups.addPropertyChangeListener(SELECTED_ROSTER_GROUP, new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent pce) {
            prefsMgr.setProperty(this.getClass().getName(), SELECTED_ROSTER_GROUP, pce.getNewValue());
            setTitle((String) pce.getNewValue());
        }
    });
    rosterGroupSplitPane.addPropertyChangeListener(propertyChangeListener);
    Roster.getDefault().addPropertyChangeListener((PropertyChangeEvent e) -> {
        if (e.getPropertyName().equals("RosterGroupAdded") && Roster.getDefault().getRosterGroupList().size() == 1) {
            // if the pane is hidden, show it when 1st group is created
            hideGroupsPane(false);
            enableRosterGroupMenuItems(true);
        } else if (!rtable.isVisible() && (e.getPropertyName().equals("saved"))) {
            if (firstHelpLabel != null) {
                firstHelpLabel.setVisible(false);
            }
            rtable.setVisible(true);
            rtable.resetColumnWidths();
        }
    });
    if (Roster.getDefault().numEntries() == 0) {
        try {
            BufferedImage myPicture = ImageIO.read(FileUtil.findURL(("resources/" + Bundle.getMessage("ThrottleFirstUseImage")), FileUtil.Location.INSTALLED));
            //rosters.add(new JLabel(new ImageIcon( myPicture )), BorderLayout.CENTER);
            firstHelpLabel = new JLabel(new ImageIcon(myPicture));
            rtable.setVisible(false);
            rosters.add(firstHelpLabel, BorderLayout.NORTH);
            //tableArea.add(firstHelpLabel);
            rtable.setVisible(false);
        } catch (IOException ex) {
        // handle exception...
        }
    }
    return rosterGroupSplitPane;
}
Also used : RosterEntrySelection(jmri.util.datatransfer.RosterEntrySelection) JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) PropertyChangeListener(java.beans.PropertyChangeListener) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) PropertyChangeEvent(java.beans.PropertyChangeEvent) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) JLabel(javax.swing.JLabel) IOException(java.io.IOException) JTablePersistenceManager(jmri.swing.JTablePersistenceManager) JTable(javax.swing.JTable) TransferHandler(javax.swing.TransferHandler) JSplitPane(javax.swing.JSplitPane)

Example 54 with ListSelectionEvent

use of javax.swing.event.ListSelectionEvent in project processdash by dtuma.

the class TaskScheduleDialog method buildTaskButtons.

protected Component buildTaskButtons(boolean isRollup) {
    Box result = Box.createHorizontalBox();
    result.add(Box.createHorizontalGlue());
    addTaskAction = new TSAction("Buttons.Add_Schedule", "Buttons.Add_Task") {

        public void actionPerformed(ActionEvent e) {
            addTask();
        }
    };
    if (canEdit())
        result.add(new JButton(addTaskAction));
    result.add(Box.createHorizontalGlue());
    // button margins: 2 pixels top and bottom, 14 left and right.
    deleteTaskAction = new TSAction("Buttons.Remove_Schedule", "Buttons.Remove_Task") {

        public void actionPerformed(ActionEvent e) {
            deleteTask();
        }
    };
    deleteTaskAction.setEnabled(false);
    if (canEdit())
        result.add(new JButton(deleteTaskAction));
    result.add(Box.createHorizontalGlue());
    moveUpAction = new TSAction("Buttons.Move_Schedule_Up", "Buttons.Move_Task_Up") {

        public void actionPerformed(ActionEvent e) {
            moveTaskUp();
        }
    };
    moveUpAction.setEnabled(false);
    moveUpAction.setMnemonic('U');
    if (canEdit())
        result.add(new JButton(moveUpAction));
    result.add(Box.createHorizontalGlue());
    moveDownAction = new TSAction("Buttons.Move_Schedule_Down", "Buttons.Move_Task_Down") {

        public void actionPerformed(ActionEvent e) {
            moveTaskDown();
        }
    };
    moveDownAction.setEnabled(false);
    moveDownAction.setMnemonic('D');
    if (canEdit())
        result.add(new JButton(moveDownAction));
    result.add(Box.createHorizontalGlue());
    flatViewAction = mergedViewAction = null;
    if (!isRollup) {
        flatViewAction = new TSAction("Buttons.Flat_View") {

            public void actionPerformed(ActionEvent e) {
                toggleFlatView();
            }
        };
        JCheckBox flatViewCheckbox = new JCheckBox(flatViewAction);
        flatViewCheckbox.setFocusPainted(false);
        flatViewAction.useButtonModel(flatViewCheckbox);
        flatViewAction.setSelected(false);
        result.add(flatViewCheckbox);
        result.add(Box.createHorizontalGlue());
    } else {
        mergedViewAction = new TSAction("Buttons.Merged_View") {

            public void actionPerformed(ActionEvent e) {
                toggleMergedView();
            }
        };
        JCheckBox mergedViewCheckbox = new JCheckBox(mergedViewAction);
        mergedViewCheckbox.setFocusPainted(false);
        mergedViewAction.useButtonModel(mergedViewCheckbox);
        mergedViewAction.setSelected(false);
        result.add(mergedViewCheckbox);
        result.add(Box.createHorizontalGlue());
    }
    expandAllAction = new TSAction("Buttons.Expand_All") {

        public void actionPerformed(ActionEvent e) {
            expandAllForSelectedItems();
        }
    };
    treeTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            enableTaskButtons(e);
        }
    });
    return result;
}
Also used : JCheckBox(javax.swing.JCheckBox) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JComboBox(javax.swing.JComboBox) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) ListSelectionListener(javax.swing.event.ListSelectionListener)

Example 55 with ListSelectionEvent

use of javax.swing.event.ListSelectionEvent in project cytoscape-api by cytoscape.

the class CheckBoxJListTest method testValueChanged.

@Test
public void testValueChanged() {
    final List<String> selected = new ArrayList<String>();
    DefaultListModel model = new DefaultListModel();
    final String[] listItemStrings = new String[] { "test1", "test2", "test3" };
    for (String item : listItemStrings) model.addElement(item);
    list.setModel(model);
    for (String item : listItemStrings) selected.add(item);
    list.setSelectedItems(selected);
    ListSelectionEvent lse = mock(ListSelectionEvent.class);
    when(lse.getValueIsAdjusting()).thenReturn(false);
    list.valueChanged(lse);
    verify(lse, times(1)).getValueIsAdjusting();
}
Also used : ArrayList(java.util.ArrayList) ListSelectionEvent(javax.swing.event.ListSelectionEvent) DefaultListModel(javax.swing.DefaultListModel) Test(org.junit.Test)

Aggregations

ListSelectionEvent (javax.swing.event.ListSelectionEvent)129 ListSelectionListener (javax.swing.event.ListSelectionListener)120 ActionEvent (java.awt.event.ActionEvent)35 JScrollPane (javax.swing.JScrollPane)32 ActionListener (java.awt.event.ActionListener)29 JPanel (javax.swing.JPanel)28 MouseEvent (java.awt.event.MouseEvent)25 BorderLayout (java.awt.BorderLayout)21 JButton (javax.swing.JButton)20 Dimension (java.awt.Dimension)18 JLabel (javax.swing.JLabel)18 JBList (com.intellij.ui.components.JBList)16 MouseAdapter (java.awt.event.MouseAdapter)15 FlowLayout (java.awt.FlowLayout)11 Insets (java.awt.Insets)11 JList (javax.swing.JList)11 NotNull (org.jetbrains.annotations.NotNull)11 KeyAdapter (java.awt.event.KeyAdapter)10 KeyEvent (java.awt.event.KeyEvent)10 ArrayList (java.util.ArrayList)10