Search in sources :

Example 16 with MouseListener

use of java.awt.event.MouseListener 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 17 with MouseListener

use of java.awt.event.MouseListener in project JMRI by JMRI.

the class ThrottlesListPanel method initGUI.

private void initGUI() {
    throttleFrames = new JTable(throttleFramesLM);
    throttleFrames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    throttleFrames.setRowHeight(ThrottlesTableCellRenderer.height);
    throttleFrames.setTableHeader(null);
    throttleFrames.setDefaultRenderer(Object.class, new ThrottlesTableCellRenderer());
    throttleFrames.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            int row = throttleFrames.rowAtPoint(e.getPoint());
            throttleFrames.getSelectionModel().setSelectionInterval(row, row);
            ((ThrottleFrame) throttleFramesLM.getValueAt(row, 0)).toFront();
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }
    });
    JScrollPane scrollPane1 = new JScrollPane(throttleFrames);
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(320, 200));
    JToolBar throttleToolBar = new JToolBar("Throttles list toolbar");
    JButton jbNew = new JButton();
    jbNew.setIcon(new NamedIcon("resources/icons/throttles/new.png", "resources/icons/throttles/new.png"));
    jbNew.setToolTipText(Bundle.getMessage("ThrottleToolBarNewWindowToolTip"));
    jbNew.setVerticalTextPosition(JButton.BOTTOM);
    jbNew.setHorizontalTextPosition(JButton.CENTER);
    jbNew.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ThrottleFrame tf = ThrottleFrameManager.instance().createThrottleFrame();
            tf.toFront();
        }
    });
    throttleToolBar.add(jbNew);
    throttleToolBar.addSeparator();
    throttleToolBar.add(new StopAllButton());
    throttleToolBar.add(new LargePowerManagerButton());
    add(throttleToolBar, BorderLayout.PAGE_START);
    add(scrollPane1, BorderLayout.CENTER);
    throttleToolBar.addSeparator();
    JButton jbPreferences = new JButton();
    jbPreferences.setIcon(new NamedIcon("resources/icons/throttles/preferences.png", "resources/icons/throttles/Preferences24.png"));
    jbPreferences.setToolTipText(Bundle.getMessage("ThrottleToolBarPreferencesToolTip"));
    jbPreferences.setVerticalTextPosition(JButton.BOTTOM);
    jbPreferences.setHorizontalTextPosition(JButton.CENTER);
    jbPreferences.addActionListener(new ThrottlesPreferencesAction());
    throttleToolBar.add(jbPreferences);
}
Also used : JScrollPane(javax.swing.JScrollPane) NamedIcon(jmri.jmrit.catalog.NamedIcon) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Dimension(java.awt.Dimension) JToolBar(javax.swing.JToolBar) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable)

Example 18 with MouseListener

use of java.awt.event.MouseListener in project android by JetBrains.

the class LayoutTestUtilities method pressMouse.

public static void pressMouse(InteractionManager manager, int button, int x, int y, int modifiers) {
    Object listener = manager.getListener();
    assertTrue(listener instanceof MouseListener);
    MouseListener mouseListener = (MouseListener) listener;
    JComponent layeredPane = manager.getSurface().getLayeredPane();
    mouseListener.mousePressed(new MouseEventBuilder(x, y).withSource(layeredPane).withMask(modifiers).build());
}
Also used : MouseListener(java.awt.event.MouseListener) MouseEventBuilder(com.android.tools.idea.uibuilder.fixtures.MouseEventBuilder)

Example 19 with MouseListener

use of java.awt.event.MouseListener in project android by JetBrains.

the class LayoutTestUtilities method releaseMouse.

public static void releaseMouse(InteractionManager manager, int button, int x, int y, int modifiers) {
    Object listener = manager.getListener();
    assertTrue(listener instanceof MouseListener);
    MouseListener mouseListener = (MouseListener) listener;
    JComponent layeredPane = manager.getSurface().getLayeredPane();
    mouseListener.mousePressed(new MouseEventBuilder(x, y).withSource(layeredPane).withMask(modifiers).build());
}
Also used : MouseListener(java.awt.event.MouseListener) MouseEventBuilder(com.android.tools.idea.uibuilder.fixtures.MouseEventBuilder)

Example 20 with MouseListener

use of java.awt.event.MouseListener in project jdk8u_jdk by JetBrains.

the class bug7146377 method main.

public static void main(String[] args) throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            frame = new JFrame();
            label = new JLabel("A label");
            label.addMouseListener(new MouseListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    checkEvent(e);
                }

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

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

                @Override
                public void mouseEntered(MouseEvent e) {
                    checkEvent(e);
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    checkEvent(e);
                }
            });
            frame.add(label);
            frame.setSize(200, 100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
    SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
    toolkit.realSync();
    // On Linux platforms realSync doesn't guaranties setSize completion
    Thread.sleep(1000);
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            point = label.getLocationOnScreen();
        }
    });
    Robot robot = new Robot();
    robot.setAutoDelay(200);
    // Move mouse
    for (int i = 0; i < 20; i++) {
        robot.mouseMove(point.x + i, point.y + i);
    }
    for (int button : new int[] { InputEvent.BUTTON1_MASK, InputEvent.BUTTON2_MASK, InputEvent.BUTTON3_MASK }) {
        robot.mouseMove(point.x, point.y);
        // Mouse Drag
        robot.mousePress(button);
        for (int i = 0; i < 20; i++) {
            robot.mouseMove(point.x + i, point.y + i);
        }
        robot.mouseRelease(button);
    }
    toolkit.realSync();
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            frame.dispose();
        }
    });
    System.out.println("Test passed");
}
Also used : MouseListener(java.awt.event.MouseListener) MouseEvent(java.awt.event.MouseEvent) SunToolkit(sun.awt.SunToolkit)

Aggregations

MouseListener (java.awt.event.MouseListener)104 MouseEvent (java.awt.event.MouseEvent)67 Dimension (java.awt.Dimension)21 JLabel (javax.swing.JLabel)20 MouseAdapter (java.awt.event.MouseAdapter)19 JPanel (javax.swing.JPanel)16 Component (java.awt.Component)14 JComponent (javax.swing.JComponent)14 ActionEvent (java.awt.event.ActionEvent)13 ActionListener (java.awt.event.ActionListener)11 MouseMotionListener (java.awt.event.MouseMotionListener)11 JButton (javax.swing.JButton)11 JScrollPane (javax.swing.JScrollPane)11 Insets (java.awt.Insets)8 BorderLayout (java.awt.BorderLayout)7 Color (java.awt.Color)7 Point (java.awt.Point)7 File (java.io.File)7 ImageIcon (javax.swing.ImageIcon)6 JCheckBox (javax.swing.JCheckBox)6