Example usage for java.awt Component addKeyListener

List of usage examples for java.awt Component addKeyListener

Introduction

In this page you can find the example usage for java.awt Component addKeyListener.

Prototype

public synchronized void addKeyListener(KeyListener l) 

Source Link

Document

Adds the specified key listener to receive key events from this component.

Usage

From source file:Main.java

private static void addComponentListeners(Component c, Object... objs) {
    if (c == null)
        return;//from  w w w. jav  a2  s .co  m
    ComponentListener componentListener = search(objs, ComponentListener.class);
    FocusListener focusListener = search(objs, FocusListener.class);
    HierarchyBoundsListener hierarchyBoundsListener = search(objs, HierarchyBoundsListener.class);
    HierarchyListener hierarchyListener = search(objs, HierarchyListener.class);
    InputMethodListener inputMethodListener = search(objs, InputMethodListener.class);
    KeyListener keyListener = search(objs, KeyListener.class);
    MouseListener mouseListener = search(objs, MouseListener.class);
    MouseMotionListener mouseMotionListener = search(objs, MouseMotionListener.class);
    MouseWheelListener mouseWheelListener = search(objs, MouseWheelListener.class);
    if (componentListener != null)
        c.addComponentListener(componentListener);
    if (focusListener != null)
        c.addFocusListener(focusListener);
    if (hierarchyBoundsListener != null)
        c.addHierarchyBoundsListener(hierarchyBoundsListener);
    if (hierarchyListener != null)
        c.addHierarchyListener(hierarchyListener);
    if (inputMethodListener != null)
        c.addInputMethodListener(inputMethodListener);
    if (keyListener != null)
        c.addKeyListener(keyListener);
    if (mouseListener != null)
        c.addMouseListener(mouseListener);
    if (mouseMotionListener != null)
        c.addMouseMotionListener(mouseMotionListener);
    if (mouseWheelListener != null)
        c.addMouseWheelListener(mouseWheelListener);
}

From source file:edu.ku.brc.specify.datamodel.busrules.DeterminationBusRules.java

/**
 * @param kl//from   w  w  w .j  a va2 s.co  m
 * @param comp
 * 
 * Adds KeyListener to comp if it is not already a listener.
 * (This method is overkill given the current way listeners are set up.)
 */
protected void addListenerIfNecessary(final KeyListener kl, final Component comp) {
    boolean fnd = false;
    for (KeyListener existingKl : comp.getKeyListeners()) {
        if (existingKl == kl) {
            fnd = true;
            break;
        }
    }
    if (!fnd) {
        comp.addKeyListener(kl);
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopSearchField.java

public DesktopSearchField() {
    composition = new JPanel();
    composition.setLayout(new BorderLayout());
    composition.setFocusable(false);//  w  w w . j  ava2 s.co m

    comboBox = new SearchComboBox() {
        @Override
        public void setPopupVisible(boolean v) {
            if (!items.isEmpty()) {
                super.setPopupVisible(v);
            } else if (!v) {
                super.setPopupVisible(false);
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (SearchAutoCompleteSupport.SEARCH_ENTER_COMMAND.equals(e.getActionCommand())) {
                enterHandling = true;
            }

            super.actionPerformed(e);
        }
    };
    comboBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (settingValue || disableActionListener)
                return;

            if ("comboBoxEdited".equals(e.getActionCommand())) {
                Object selectedItem = comboBox.getSelectedItem();

                if (popupItemSelectionHandling) {
                    if (selectedItem instanceof ValueWrapper) {
                        Object selectedValue = ((ValueWrapper) selectedItem).getValue();
                        setValue(selectedValue);
                        updateOptionsDsItem();
                    } else if (selectedItem instanceof String) {
                        handleSearch((String) selectedItem);
                    }
                    popupItemSelectionHandling = false;
                } else if (enterHandling) {
                    if (selectedItem instanceof String) {
                        boolean found = false;
                        String newFilter = (String) selectedItem;
                        if (prevValue != null) {
                            if (Objects.equals(getDisplayString((Entity) prevValue), newFilter)) {
                                found = true;
                            }
                        }
                        if (!found) {
                            handleSearch(newFilter);
                        } else {
                            updateComponent(prevValue);
                            clearSearchVariants();
                        }
                    } else {
                        // Disable variants after select
                        clearSearchVariants();
                    }
                    enterHandling = false;
                }
            }

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    updateEditState();
                }
            });
        }
    });

    Component editorComponent = comboBox.getEditor().getEditorComponent();
    editorComponent.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    updateEditState();
                }
            });
        }
    });

    comboBox.setEditable(true);
    comboBox.setPrototypeDisplayValue("AAAAAAAAAAAA");
    autoComplete = SearchAutoCompleteSupport.install(comboBox, items);
    autoComplete.setFilterEnabled(false);

    for (int i = 0; i < comboBox.getComponentCount(); i++) {
        java.awt.Component component = comboBox.getComponent(i);
        component.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                clearSearchVariants();
                // Reset invalid value

                checkSelectedValue();
            }
        });
    }
    // set value only on PopupMenu closing to avoid firing listeners on keyboard navigation
    comboBox.addPopupMenuListener(new PopupMenuListener() {
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            comboBox.updatePopupWidth();
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            if (!autoComplete.isEditableState()) {
                // Only if realy item changed
                if (!enterHandling) {
                    Object selectedItem = comboBox.getSelectedItem();
                    if (selectedItem instanceof ValueWrapper) {
                        Object selectedValue = ((ValueWrapper) selectedItem).getValue();
                        setValue(selectedValue);
                        updateOptionsDsItem();
                    } else if (selectedItem instanceof String) {
                        handleSearch((String) selectedItem);
                    }
                } else {
                    popupItemSelectionHandling = true;
                }

                updateMissingValueState();
            }
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            clearSearchVariants();
        }
    });

    setFilterMode(DEFAULT_FILTER_MODE);

    textField = new JTextField();
    textField.setEditable(false);
    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
    valueFormatter = new DefaultValueFormatter(sessionSource.getLocale());

    composition.add(comboBox, BorderLayout.CENTER);
    impl = comboBox;

    DesktopComponentsHelper.adjustSize(comboBox);
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopSuggestionField.java

public DesktopSuggestionField() {
    composition = new JPanel();
    composition.setLayout(new BorderLayout());
    composition.setFocusable(false);// w  w  w .j a  v a2  s  .c  o  m

    comboBox = new SearchComboBox() {
        @Override
        public void setPopupVisible(boolean v) {
            if (!items.isEmpty()) {
                super.setPopupVisible(v);
            } else if (!v) {
                super.setPopupVisible(false);
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (SearchAutoCompleteSupport.SEARCH_ENTER_COMMAND.equals(e.getActionCommand())) {
                enterHandling = true;
            }

            super.actionPerformed(e);
        }
    };

    comboBox.addActionListener(e -> {
        if (settingValue || disableActionListener) {
            return;
        }

        if ("comboBoxEdited".equals(e.getActionCommand())) {
            Object selectedItem = comboBox.getSelectedItem();

            if (popupItemSelectionHandling) {
                if (selectedItem instanceof ValueWrapper) {
                    Object selectedValue = ((ValueWrapper) selectedItem).getValue();
                    setValue(selectedValue);
                }
            } else if (enterHandling) {
                if (selectedItem instanceof String) {
                    boolean found = false;
                    String newFilter = (String) selectedItem;
                    if (prevValue != null) {
                        if (Objects.equals(getDisplayString(prevValue), newFilter)) {
                            found = true;
                        }
                    }

                    final boolean searchStringEqualsToCurrentValue = found;
                    // we need to do it later
                    // unable to change current text from ActionListener
                    SwingUtilities.invokeLater(() -> {
                        updateComponent(prevValue);

                        if (!searchStringEqualsToCurrentValue) {
                            handleOnEnterAction(((String) selectedItem));
                        }
                    });
                } else if (currentSearchComponentText != null) {
                    // Disable variants after select
                    final String enterActionString = currentSearchComponentText;
                    SwingUtilities.invokeLater(() -> {
                        updateComponent(prevValue);

                        handleOnEnterAction(enterActionString);
                    });

                    currentSearchComponentText = null;
                }
            }

            clearSearchVariants();

            popupItemSelectionHandling = false;
            enterHandling = false;
        }

        SwingUtilities.invokeLater(this::updateEditState);
    });

    Component editorComponent = comboBox.getEditor().getEditorComponent();
    editorComponent.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            SwingUtilities.invokeLater(() -> {
                updateEditState();

                if (e.getKeyChar() != '\n') {
                    handleSearchInput();
                }
            });
        }

        @Override
        public void keyPressed(KeyEvent e) {
            SwingUtilities.invokeLater(() -> {
                if (e.getKeyCode() == KeyEvent.VK_DOWN && arrowDownActionHandler != null
                        && !comboBox.isPopupVisible()) {
                    arrowDownActionHandler.onArrowDownKeyPressed(getComboBoxEditorField().getText());
                }
            });
        }
    });

    comboBox.setEditable(true);
    comboBox.setPrototypeDisplayValue("AAAAAAAAAAAA");

    autoComplete = SearchAutoCompleteSupport.install(comboBox, items);
    autoComplete.setFilterEnabled(false);

    for (int i = 0; i < comboBox.getComponentCount(); i++) {
        Component component = comboBox.getComponent(i);
        component.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                clearSearchVariants();
                // Reset invalid value

                checkSelectedValue();
            }
        });
    }

    final JTextField searchEditorComponent = getComboBoxEditorField();
    searchEditorComponent.addActionListener(e -> currentSearchComponentText = searchEditorComponent.getText());

    // set value only on PopupMenu closing to avoid firing listeners on keyboard navigation
    comboBox.addPopupMenuListener(new PopupMenuListener() {
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            comboBox.updatePopupWidth();
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            if (!autoComplete.isEditableState()) {
                popupItemSelectionHandling = comboBox.getSelectedIndex() >= 0;

                // Only if really item changed
                if (!enterHandling) {
                    Object selectedItem = comboBox.getSelectedItem();
                    if (selectedItem instanceof ValueWrapper) {
                        Object selectedValue = ((ValueWrapper) selectedItem).getValue();
                        setValue(selectedValue);

                        clearSearchVariants();
                    }
                }

                updateMissingValueState();
            }
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            clearSearchVariants();
        }
    });

    textField = new JTextField();
    textField.setEditable(false);
    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
    valueFormatter = new DefaultValueFormatter(sessionSource.getLocale());

    composition.add(comboBox, BorderLayout.CENTER);
    impl = comboBox;

    DesktopComponentsHelper.adjustSize(comboBox);

    Configuration configuration = AppBeans.get(Configuration.NAME);
    asyncSearchDelayMs = configuration.getConfig(ClientConfig.class).getSuggestionFieldAsyncSearchDelayMs();
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ??????/*from  w  w  w.  j  av  a2s .  c  o m*/
 * 
 * @param target ?
 * @param action ?????????????
 * @param property ???????????? 
 * @return ??
 * @see KeyListener#keyPressed(java.awt.event.KeyEvent)
 */
public EventQuery keydown(Object target, String action, String property) {
    KeyListener keyPressed = EventHandler.create(KeyListener.class, target, action, property, "keyPressed");
    for (Component child : list) {
        child.addKeyListener(keyPressed);
    }
    return this;
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ?????//from w  w w  .  j  a v a 2s.c om
 * 
 * @param target ?
 * @param action ?????????????
 * @param property ???????????? 
 * @return ??
 * @see KeyListener#keyReleased(java.awt.event.KeyEvent)
 */
public EventQuery keyup(Object target, String action, String property) {
    KeyListener keyReleased = EventHandler.create(KeyListener.class, target, action, property, "keyReleased");
    for (Component child : list) {
        child.addKeyListener(keyReleased);
    }
    return this;
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ?????/*from   w  w  w .  j  a  va  2s . c om*/
 * 
 * @param target ?
 * @param action ?????????????
 * @param property ???????????? 
 * @return ??
 * @see KeyListener#keyTyped(java.awt.event.KeyEvent)
 */
public EventQuery keypress(Object target, String action, String property) {
    KeyListener keyTyped = EventHandler.create(KeyListener.class, target, action, property, "keyTyped");
    for (Component child : list) {
        child.addKeyListener(keyTyped);
    }
    return this;
}

From source file:savant.view.swing.NavigationBar.java

NavigationBar() {

    this.setOpaque(false);
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

    String buttonStyle = "segmentedCapsule";

    String shortcutMod = MiscUtils.MAC ? "Cmd" : "Ctrl";

    add(getRigidPadding());//from w  w w.ja v  a2 s  .  co m

    JButton loadGenomeButton = (JButton) add(new JButton(""));
    loadGenomeButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.GENOME));
    loadGenomeButton.setToolTipText("Load or change genome");
    loadGenomeButton.setFocusable(false);
    loadGenomeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Savant.getInstance().showOpenGenomeDialog();
        }
    });
    loadGenomeButton.putClientProperty("JButton.buttonType", buttonStyle);
    loadGenomeButton.putClientProperty("JButton.segmentPosition", "first");
    loadGenomeButton.setPreferredSize(ICON_SIZE);
    loadGenomeButton.setMinimumSize(ICON_SIZE);
    loadGenomeButton.setMaximumSize(ICON_SIZE);

    JButton loadTrackButton = (JButton) add(new JButton(""));
    loadTrackButton.setFocusable(false);
    loadTrackButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.TRACK));
    loadTrackButton.setToolTipText("Load a track");
    loadTrackButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Savant.getInstance().openTrack();
        }
    });
    loadTrackButton.putClientProperty("JButton.buttonType", buttonStyle);
    loadTrackButton.putClientProperty("JButton.segmentPosition", "last");
    loadTrackButton.setPreferredSize(ICON_SIZE);
    loadTrackButton.setMinimumSize(ICON_SIZE);
    loadTrackButton.setMaximumSize(ICON_SIZE);

    if (!Savant.getInstance().isStandalone()) {
        add(loadGenomeButton);
        add(loadTrackButton);
        add(getRigidPadding());
        add(getRigidPadding());
    } else {
        loadGenomeButton.setVisible(false);
        loadTrackButton.setVisible(false);
    }

    JLabel rangeText = new JLabel("Location ");
    add(rangeText);

    String[] a = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    locationField = new JComboBox(a);
    locationField.setEditable(true);
    locationField.setRenderer(new ReferenceListRenderer());

    // When the item is chosen from the menu, navigate to the given feature/reference.
    locationField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (!currentlyPopulating) {
                if (ae.getActionCommand().equals("comboBoxChanged")) {
                    // Assumes that combo-box items created by populateCombo() are of the form "GENE (chrX:1-1000)".
                    String itemText = locationField.getSelectedItem().toString();
                    int lastBracketPos = itemText.lastIndexOf('(');
                    if (lastBracketPos > 0) {
                        itemText = itemText.substring(lastBracketPos + 1, itemText.length() - 1);
                    }
                    setRangeFromText(itemText);

                }
            }
        }
    });

    // When the combo-box is popped open, we may want to repopulate the menu.
    locationField.addPopupMenuListener(new PopupMenuListener() {
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent pme) {
            String text = (String) locationField.getEditor().getItem();
            if (!text.equals(lastPoppedUp)) {
                try {
                    // Building the menu could take a while.
                    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    populateCombo();
                } finally {
                    setCursor(Cursor.getDefaultCursor());
                }
            }
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent pme) {
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent pme) {
        }
    });

    // Add our special keystroke-handling to the JComboBox' text-field.
    // We have to turn off default tab-handling so that tab can pop up our list.
    Component textField = locationField.getEditor().getEditorComponent();
    textField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
    textField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent evt) {
            if (evt.getKeyCode() == KeyEvent.VK_TAB) {
                locationField.showPopup();
            } else if (evt.getModifiers() == KeyEvent.SHIFT_MASK) {
                switch (evt.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    locationController.shiftRangeLeft();
                    evt.consume();
                    break;
                case KeyEvent.VK_RIGHT:
                    locationController.shiftRangeRight();
                    evt.consume();
                    break;
                case KeyEvent.VK_UP:
                    locationController.zoomIn();
                    evt.consume();
                    break;
                case KeyEvent.VK_DOWN:
                    locationController.zoomOut();
                    evt.consume();
                    break;
                case KeyEvent.VK_HOME:
                    locationController.shiftRangeFarLeft();
                    evt.consume();
                    break;
                case KeyEvent.VK_END:
                    locationController.shiftRangeFarRight();
                    evt.consume();
                    break;
                }
            }
        }
    });
    add(locationField);
    locationField.setToolTipText("Current display range");
    locationField.setPreferredSize(LOCATION_SIZE);
    locationField.setMaximumSize(LOCATION_SIZE);
    locationField.setMinimumSize(LOCATION_SIZE);

    add(getRigidPadding());

    JButton goButton = (JButton) add(new JButton("  Go  "));
    goButton.putClientProperty("JButton.buttonType", buttonStyle);
    goButton.putClientProperty("JButton.segmentPosition", "only");
    goButton.setToolTipText("Go to specified range (Enter)");
    goButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setRangeFromText(locationField.getEditor().getItem().toString());
        }
    });

    add(getRigidPadding());

    JLabel l = new JLabel("Length: ");
    add(l);

    lengthLabel = (JLabel) add(new JLabel());
    lengthLabel.setToolTipText("Length of the current range");
    lengthLabel.setPreferredSize(LENGTH_SIZE);
    lengthLabel.setMaximumSize(LENGTH_SIZE);
    lengthLabel.setMinimumSize(LENGTH_SIZE);

    add(Box.createGlue());

    double screenwidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth();

    JButton afterGo = null;
    //if (screenwidth > 800) {
    final JButton undoButton = (JButton) add(new JButton(""));
    afterGo = undoButton;
    undoButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.UNDO));
    undoButton.setToolTipText("Undo range change (" + shortcutMod + "+Z)");
    undoButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.undoLocationChange();
        }
    });
    undoButton.putClientProperty("JButton.buttonType", buttonStyle);
    undoButton.putClientProperty("JButton.segmentPosition", "first");
    undoButton.setPreferredSize(ICON_SIZE);
    undoButton.setMinimumSize(ICON_SIZE);
    undoButton.setMaximumSize(ICON_SIZE);

    final JButton redo = (JButton) add(new JButton(""));
    redo.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.REDO));
    redo.setToolTipText("Redo range change (" + shortcutMod + "+Y)");
    redo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.redoLocationChange();
        }
    });
    redo.putClientProperty("JButton.buttonType", buttonStyle);
    redo.putClientProperty("JButton.segmentPosition", "last");
    redo.setPreferredSize(ICON_SIZE);
    redo.setMinimumSize(ICON_SIZE);
    redo.setMaximumSize(ICON_SIZE);
    //}

    add(getRigidPadding());
    add(getRigidPadding());

    final JButton zoomInButton = (JButton) add(new JButton());
    if (afterGo == null) {
        afterGo = zoomInButton;
    }
    zoomInButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.ZOOMIN));
    zoomInButton.putClientProperty("JButton.buttonType", buttonStyle);
    zoomInButton.putClientProperty("JButton.segmentPosition", "first");
    zoomInButton.setPreferredSize(ICON_SIZE);
    zoomInButton.setMinimumSize(ICON_SIZE);
    zoomInButton.setMaximumSize(ICON_SIZE);
    zoomInButton.setToolTipText("Zoom in (Shift+Up)");
    zoomInButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.zoomIn();
            AnalyticsAgent.log(new NameValuePair[] { new NameValuePair("navigation-event", "zoomed"),
                    new NameValuePair("navigation-direction", "in"),
                    new NameValuePair("navigation-modality", "navbar") });
        }
    });

    final JButton zoomOut = (JButton) add(new JButton(""));
    zoomOut.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.ZOOMOUT));
    zoomOut.setToolTipText("Zoom out (Shift+Down)");
    zoomOut.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.zoomOut();
            AnalyticsAgent.log(new NameValuePair[] { new NameValuePair("navigation-event", "zoomed"),
                    new NameValuePair("navigation-direction", "out"),
                    new NameValuePair("navigation-modality", "navbar") });
        }
    });
    zoomOut.putClientProperty("JButton.buttonType", buttonStyle);
    zoomOut.putClientProperty("JButton.segmentPosition", "last");
    zoomOut.setPreferredSize(ICON_SIZE);
    zoomOut.setMinimumSize(ICON_SIZE);
    zoomOut.setMaximumSize(ICON_SIZE);

    add(getRigidPadding());
    add(getRigidPadding());

    final JButton shiftFarLeft = (JButton) add(new JButton());
    shiftFarLeft.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.SHIFT_FARLEFT));
    shiftFarLeft.putClientProperty("JButton.buttonType", buttonStyle);
    shiftFarLeft.putClientProperty("JButton.segmentPosition", "first");
    shiftFarLeft.setToolTipText("Move to the beginning of the genome (Shift+Home)");
    shiftFarLeft.setPreferredSize(ICON_SIZE);
    shiftFarLeft.setMinimumSize(ICON_SIZE);
    shiftFarLeft.setMaximumSize(ICON_SIZE);
    shiftFarLeft.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.shiftRangeFarLeft();
            AnalyticsAgent.log(new NameValuePair[] { new NameValuePair("navigation-event", "panned"),
                    new NameValuePair("navigation-direction", "left"),
                    new NameValuePair("navigation-modality", "navbar") });
        }
    });

    final JButton shiftLeft = (JButton) add(new JButton());
    shiftLeft.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.SHIFT_LEFT));
    shiftLeft.putClientProperty("JButton.buttonType", buttonStyle);
    shiftLeft.putClientProperty("JButton.segmentPosition", "middle");
    shiftLeft.setToolTipText("Move left (Shift+Left)");
    shiftLeft.setPreferredSize(ICON_SIZE);
    shiftLeft.setMinimumSize(ICON_SIZE);
    shiftLeft.setMaximumSize(ICON_SIZE);
    shiftLeft.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.shiftRangeLeft();
            AnalyticsAgent.log(new NameValuePair[] { new NameValuePair("navigation-event", "panned"),
                    new NameValuePair("navigation-direction", "left"),
                    new NameValuePair("navigation-modality", "navbar") });
        }
    });

    final JButton shiftRight = (JButton) add(new JButton());
    shiftRight.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.SHIFT_RIGHT));
    shiftRight.putClientProperty("JButton.buttonType", buttonStyle);
    shiftRight.putClientProperty("JButton.segmentPosition", "middle");
    shiftRight.setToolTipText("Move right (Shift+Right)");
    shiftRight.setPreferredSize(ICON_SIZE);
    shiftRight.setMinimumSize(ICON_SIZE);
    shiftRight.setMaximumSize(ICON_SIZE);
    shiftRight.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.shiftRangeRight();
            AnalyticsAgent.log(new NameValuePair[] { new NameValuePair("navigation-event", "panned"),
                    new NameValuePair("navigation-direction", "right"),
                    new NameValuePair("navigation-modality", "navbar") });
        }
    });

    final JButton shiftFarRight = (JButton) add(new JButton());
    shiftFarRight
            .setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.SHIFT_FARRIGHT));
    shiftFarRight.putClientProperty("JButton.buttonType", buttonStyle);
    shiftFarRight.putClientProperty("JButton.segmentPosition", "last");
    shiftFarRight.setToolTipText("Move to the end of the genome (Shift+End)");
    shiftFarRight.setPreferredSize(ICON_SIZE);
    shiftFarRight.setMinimumSize(ICON_SIZE);
    shiftFarRight.setMaximumSize(ICON_SIZE);
    shiftFarRight.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            locationController.shiftRangeFarRight();
            AnalyticsAgent.log(new NameValuePair[] { new NameValuePair("navigation-event", "panned"),
                    new NameValuePair("navigation-direction", "right"),
                    new NameValuePair("navigation-modality", "navbar") });
        }
    });

    add(getRigidPadding());

    locationController.addListener(new Listener<LocationChangedEvent>() {
        @Override
        public void handleEvent(LocationChangedEvent event) {
            updateLocation(event.getReference(), (Range) event.getRange());
        }
    });

    // When the genome changes, we may need to invalidate our menu.
    GenomeController.getInstance().addListener(new Listener<GenomeChangedEvent>() {
        @Override
        public void handleEvent(GenomeChangedEvent event) {
            lastPoppedUp = "INVALID";
        }
    });

    this.addComponentListener(new ComponentListener() {
        @Override
        public void componentResized(ComponentEvent ce) {
            int width = ce.getComponent().getWidth();

            undoButton.setVisible(true);
            redo.setVisible(true);
            zoomInButton.setVisible(true);
            zoomOut.setVisible(true);
            shiftFarLeft.setVisible(true);
            shiftLeft.setVisible(true);
            shiftRight.setVisible(true);
            shiftFarRight.setVisible(true);

            // hide some components if the window isn't wide enough
            if (width < 1200) {
                undoButton.setVisible(false);
                redo.setVisible(false);
            }
            if (width < 1000) {
                shiftFarLeft.setVisible(false);
                shiftFarRight.setVisible(false);

                shiftRight.putClientProperty("JButton.segmentPosition", "last");
                shiftLeft.putClientProperty("JButton.segmentPosition", "first");
            } else {
                shiftRight.putClientProperty("JButton.segmentPosition", "middle");
                shiftLeft.putClientProperty("JButton.segmentPosition", "middle");
            }
        }

        public void componentMoved(ComponentEvent ce) {
        }

        @Override
        public void componentShown(ComponentEvent ce) {
        }

        @Override
        public void componentHidden(ComponentEvent ce) {
        }
    });
}

From source file:Filter3dTest.java

/**
 * Creates a new InputManager that listens to input from the specified
 * component./*from   w ww.j  av  a  2s  .  co m*/
 */
public InputManager(Component comp) {
    this.comp = comp;
    mouseLocation = new Point();
    centerLocation = new Point();

    // register key and mouse listeners
    comp.addKeyListener(this);
    comp.addMouseListener(this);
    comp.addMouseMotionListener(this);
    comp.addMouseWheelListener(this);

    // allow input of the TAB key and other keys normally
    // used for focus traversal
    comp.setFocusTraversalKeysEnabled(false);
}

From source file:self.philbrown.javaQuery.$.java

/**
 * Refreshes the listeners for key events
 *//*from  ww w.  j a  v a  2  s  .  co  m*/
private void setupKeyListener() {
    for (final Component view : views) {
        view.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent event) {
                if (keyDown != null)
                    keyDown.invoke($.with(view), event.getKeyCode(), event);
            }

            @Override
            public void keyReleased(KeyEvent event) {
                if (keyUp != null)
                    keyUp.invoke($.with(view), event.getKeyCode(), event);
            }

            @Override
            public void keyTyped(KeyEvent event) {
                if (keyPress != null)
                    keyPress.invoke($.with(view), event.getKeyCode(), event);
            }

        });
    }
}