Example usage for java.awt KeyboardFocusManager addKeyEventDispatcher

List of usage examples for java.awt KeyboardFocusManager addKeyEventDispatcher

Introduction

In this page you can find the example usage for java.awt KeyboardFocusManager addKeyEventDispatcher.

Prototype

public void addKeyEventDispatcher(KeyEventDispatcher dispatcher) 

Source Link

Document

Adds a KeyEventDispatcher to this KeyboardFocusManager's dispatcher chain.

Usage

From source file:ru.codemine.pos.ui.MainWindow.java

private void setupKeyboard() {
    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher(keyDispatcher);
}

From source file:org.nekorp.workflow.desktop.view.AppMainWindow.java

/**
 * call this somewhere in your GUI construction
 *//*  www . j a va  2  s  . com*/
private void setupKeyShortcut() {
    KeyStroke key1 = KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK);
    actionMap.put(key1, new AbstractAction("guardar") {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (editorMonitor.hasChange()) {
                try {
                    aplication.guardaServicio();
                } catch (IllegalArgumentException ex) {
                    //no lo guardo.
                }
            }
        }
    });
    //        key1 = KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK);
    //        actionMap.put(key1, new AbstractAction("deshacer") {
    //            @Override
    //            public void actionPerformed(ActionEvent e) {
    //                if (editorMonitor.hasChange()) {
    //                    editorMonitor.undo();
    //                }
    //            }
    //        });
    //        key1 = KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK);
    //        actionMap.put(key1, new AbstractAction("rehacer") {
    //            @Override
    //            public void actionPerformed(ActionEvent e) {
    //                editorMonitor.redo();
    //            }
    //        });
    // add more actions..

    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    kfm.addKeyEventDispatcher(new KeyEventDispatcher() {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(e);
            if (actionMap.containsKey(keyStroke)) {
                final Action a = actionMap.get(keyStroke);
                final ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), null);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        a.actionPerformed(ae);
                    }
                });
                return true;
            }
            return false;
        }
    });
}

From source file:com.intuit.tank.tools.debugger.AgentDebuggerFrame.java

/**
 * @throws HeadlessException/*from w w w .  j  av  a2 s  .co  m*/
 */
public AgentDebuggerFrame(final boolean isStandalone, String serviceUrl) throws HeadlessException {
    super("Intuit Tank Agent Debugger");
    workingDir = PanelBuilder.createWorkingDir(this, serviceUrl);
    setSize(new Dimension(1024, 800));
    setBounds(new Rectangle(getSize()));
    setPreferredSize(getSize());
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLayout(new BorderLayout());
    this.standalone = isStandalone;
    addWindowListener(new WindowAdapter() {
        public void windowClosed(WindowEvent e) {
            quit();
        }
    });
    errorIcon = ActionProducer.getIcon("bullet_error.png", IconSize.SMALL);
    modifiedIcon = ActionProducer.getIcon("bullet_code_change.png", IconSize.SMALL);
    skippedIcon = ActionProducer.getIcon("skip.png", IconSize.SMALL);

    this.glassPane = new InfiniteProgressPanel();
    setGlassPane(glassPane);
    debuggerActions = new ActionProducer(this, serviceUrl);
    requestResponsePanel = new RequestResponsePanel(this);
    requestResponsePanel.init();
    testPlanChooser = new JComboBox();
    testPlanChooser.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent event) {
            if (event.getItem() != null) {
                HDTestPlan selected = (HDTestPlan) event.getItem();
                if (!selected.equals(currentTestPlan)) {
                    setCurrentTestPlan(selected);
                }
            }

        }
    });

    tankClientChooser = new JComboBox<TankClientChoice>();
    debuggerActions.setChoiceComboBoxOptions(tankClientChooser);

    actionComponents = new ActionComponents(standalone, testPlanChooser, tankClientChooser, debuggerActions);
    addScriptChangedListener(actionComponents);
    setJMenuBar(actionComponents.getMenuBar());

    Component topPanel = PanelBuilder.createTopPanel(actionComponents);
    Component bottomPanel = PanelBuilder.createBottomPanel(this);
    Component contentPanel = PanelBuilder.createContentPanel(this);

    final JPopupMenu popup = actionComponents.getPopupMenu();
    scriptEditorTA.setPopupMenu(null);

    scriptEditorTA.addMouseListener(new MouseAdapter() {
        int lastHash;

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

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

        private void maybeShow(MouseEvent e) {
            if (lastHash == getHash(e)) {
                return;
            }
            if (e.isPopupTrigger()) {
                // select the line
                try {
                    int offset = scriptEditorTA.viewToModel(e.getPoint());
                    Rectangle modelToView = scriptEditorTA.modelToView(offset);
                    Point point = new Point(modelToView.x + 1, e.getPoint().y);
                    if (modelToView.contains(point)) {
                        if (!multiSelect) {
                            int line = scriptEditorTA.getLineOfOffset(offset);
                            scriptEditorTA.setCurrentLine(line);
                        }
                        popup.show(e.getComponent(), e.getX(), e.getY());
                    }
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
            } else if (e.isShiftDown()) {
                int line = scriptEditorTA.getCaretLineNumber();
                int start = Math.min(line, lastLine);
                int end = Math.max(line, lastLine);
                multiSelect = end - start > 1;
                if (multiSelect) {
                    multiSelectStart = start;
                    multiSelectEnd = end;
                    try {
                        scriptEditorTA.setEnabled(true);
                        scriptEditorTA.select(scriptEditorTA.getLineStartOffset(start),
                                scriptEditorTA.getLineEndOffset(end));
                        scriptEditorTA.setEnabled(false);
                    } catch (BadLocationException e1) {
                        e1.printStackTrace();
                        multiSelect = false;
                    }
                }
            } else {
                multiSelect = false;
                lastLine = scriptEditorTA.getCaretLineNumber();
            }
            lastHash = getHash(e);
        }

        private int getHash(MouseEvent e) {
            return new HashCodeBuilder().append(e.getButton()).append(e.getSource().hashCode())
                    .append(e.getPoint()).toHashCode();
        }

    });

    JSplitPane mainSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    mainSplit.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    mainSplit.setTopComponent(contentPanel);
    mainSplit.setBottomComponent(bottomPanel);
    mainSplit.setDividerLocation(600);
    mainSplit.setResizeWeight(0.8D);
    mainSplit.setDividerSize(5);

    add(topPanel, BorderLayout.NORTH);
    add(mainSplit, BorderLayout.CENTER);

    WindowUtil.centerOnScreen(this);
    pack();

    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();

    manager.addKeyEventDispatcher(new KeyEventDispatcher() {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                handleKeyEvent(e);
            }
            return false;
        }
    });
}

From source file:com.igormaznitsa.zxpoly.MainForm.java

public MainForm(final String title, final String romPath) throws IOException {
    initComponents();/*  w ww  .  j  a va  2  s  . c  o  m*/
    this.setTitle(title);

    this.getInputContext().selectInputMethod(Locale.ENGLISH);

    setIconImage(Utils.loadIcon("appico.png"));

    final RomData rom = loadRom(romPath);

    this.board = new Motherboard(rom);
    this.board.setZXPolyMode(true);
    this.menuOptionsZX128Mode.setSelected(!this.board.isZXPolyMode());
    this.menuOptionsTurbo.setSelected(this.turboMode);

    log.info("Main form completed");
    this.board.reset();

    this.scrollPanel.getViewport().add(this.board.getVideoController());
    this.keyboardAndTapeModule = this.board.findIODevice(KeyboardKempstonAndTapeIn.class);
    this.kempstonMouse = this.board.findIODevice(KempstonMouse.class);

    final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher(new KeyboardDispatcher(this.keyboardAndTapeModule));

    final GridBagConstraints cpuIndicatorConstraint = new GridBagConstraints();
    cpuIndicatorConstraint.ipadx = 5;

    this.panelIndicators.add(this.indicatorCPU0, cpuIndicatorConstraint, 0);
    this.panelIndicators.add(this.indicatorCPU1, cpuIndicatorConstraint, 1);
    this.panelIndicators.add(this.indicatorCPU2, cpuIndicatorConstraint, 2);
    this.panelIndicators.add(this.indicatorCPU3, cpuIndicatorConstraint, 3);

    updateTapeMenu();

    final Thread daemon = new Thread(this, "ZXPolyThread");
    daemon.setDaemon(true);
    daemon.start();

    updateInfoPanel();

    pack();

    this.setLocationRelativeTo(null);
}

From source file:com.freedomotic.jfrontend.MainWindow.java

/**
 *
 * @param master/*from  w w  w .  j  ava 2 s  .  c  om*/
 */
public MainWindow(final JavaDesktopFrontend master) {
    this.i18n = master.getApi().getI18n();
    UIManager.put("OptionPane.yesButtonText", i18n.msg("yes"));
    UIManager.put("OptionPane.noButtonText", i18n.msg("no"));
    UIManager.put("OptionPane.cancelButtonText", i18n.msg("cancel"));
    this.master = master;
    this.api = master.getApi();
    this.auth = api.getAuth();
    ObjectEditor.setAPI(api);

    setWindowedMode();
    updateMenusPermissions();

    String defEnv = master.getApi().getConfig().getProperty("KEY_ROOM_XML_PATH");
    EnvironmentLogic env = api.environments()
            .findOne(defEnv.substring(defEnv.length() - 41, defEnv.length() - 5));
    setEnvironment(env);

    checkDeletableEnvironments();

    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher(new MyDispatcher());

}

From source file:org.datavyu.views.DatavyuView.java

/**
 * Constructor./*from   w w w. j a  va 2  s.co m*/
 *
 * @param app
 *            The SingleFrameApplication that invoked this main FrameView.
 */
public DatavyuView(final SingleFrameApplication app) {
    super(app);

    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();

    manager.addKeyEventDispatcher(new KeyEventDispatcher() {

        /**
         * Dispatches the keystroke to the correct action.
         *
         * @param evt The event that triggered this action.
         * @return true if the KeyboardFocusManager should take no further
         * action with regard to the KeyEvent; false otherwise.
         */
        @Override
        public boolean dispatchKeyEvent(final KeyEvent evt) {

            // Pass the keyevent onto the keyswitchboard so that it can
            // route it to the correct action.
            //                    spreadsheetMenuSelected(null);

            return Datavyu.getApplication().dispatchKeyEvent(evt);
        }
    });

    // generated GUI builder code
    initComponents();

    // BugzID:492 - Set the shortcut for new cell, so a keystroke that won't
    // get confused for the "carriage return". The shortcut for new cells
    // is handled in Datavyu.java
    newCellMenuItem.setAccelerator(KeyStroke.getKeyStroke('\u21A9'));

    // BugzID:521 + 468 - Define accelerator keys based on Operating system.
    int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    weakTemporalOrderMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, keyMask));

    strongTemporalOrderMenuItem
            .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.SHIFT_MASK | keyMask));

    // Set zoom in to keyMask + '+'
    zoomInMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, keyMask));

    // Set zoom out to keyMask + '-'
    zoomOutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, keyMask));

    // Set reset zoom to keyMask + '0'
    resetZoomMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, keyMask));

    // Set the save accelerator to keyMask + 'S'
    saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, keyMask));

    // Set the save as accelerator to keyMask + shift + 'S'
    saveAsMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, keyMask | InputEvent.SHIFT_MASK));

    // Set the open accelerator to keyMask + 'o';
    openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, keyMask));

    // Set the new accelerator to keyMask + 'N';
    newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, keyMask));

    // Set the new accelerator to keyMask + 'L';
    newCellLeftMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, keyMask));

    // Set the new accelerator to keyMask + 'R';
    newCellRightMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, keyMask));

    // Set the show spreadsheet accelrator to F5.
    showSpreadsheetMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));

    // Set the undo accelerator to keyMask + 'Z';
    undoSpreadSheetMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, keyMask));

    // Set the redo accelerator to keyMask + 'Y'
    redoSpreadSheetMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, keyMask));

    if (panel != null) {
        panel.deregisterListeners();
        panel.removeFileDropEventListener(this);
    }

    panel = new SpreadsheetPanel(Datavyu.getProjectController().getDB(), null);
    panel.registerListeners();
    panel.addFileDropEventListener(this);
    setComponent(panel);

    System.out.println(getComponent());

    // initialize the undo/redo system
    spreadsheetUndoManager = new SpreadsheetUndoManager();
    undoSupport = new UndoableEditSupport();
    undoSupport.addUndoableEditListener(new UndoAdapter());
    refreshUndoRedo();
    //////

    //Jakrabbit Menu
    pushMenuItem.setVisible(false);
    pullMenuItem.setVisible(false);
    jSeparator10.setVisible(false);

}

From source file:view.MainWindow.java

/**
 * Creates new form MainWindow//from   w  ww.  j a  va  2s . c om
 */
public MainWindow() {
    initComponents();

    updateText();

    List<Image> icons = new ArrayList<>();
    icons.add(new ImageIcon(getClass().getResource("/image/aCCinaPDF_logo_icon32.png")).getImage());
    this.setIconImages(icons);

    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
    workspacePanel.setParent((MainWindow) this);
    populateDriveComboBox();

    dmtn = new DefaultMutableTreeNode("0 " + Bundle.getBundle().getString("tn.documentsLoaded"));
    TreeModel tm = new DefaultTreeModel(dmtn);
    jtOpenedDocuments.setModel(tm);
    setupTreePopups();
    setupDropListener();

    DefaultTreeCellRenderer renderer1 = (DefaultTreeCellRenderer) jtOpenedDocuments.getCellRenderer();
    Icon closedIcon = new ImageIcon(MainWindow.class.getResource("/image/pdf_ico.jpg"));
    renderer1.setLeafIcon(closedIcon);
    DefaultTreeCellRenderer renderer2 = (DefaultTreeCellRenderer) jtExplorer.getCellRenderer();
    renderer2.setLeafIcon(closedIcon);
    tfProcurar.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            refreshTree(tfProcurar.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            refreshTree(tfProcurar.getText());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {

        }
    });

    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher((MainWindow) this);
    refreshTree(null);

    ctrl.setPrintMenuItem(menuItemPrint);
}

From source file:vista.ventas.DiagOrdenesDeCompra.java

private void eventosDeTeclas() {
    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher(new KeyEventDispatcher() {

        public boolean dispatchKeyEvent(KeyEvent e) {
            if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow().getClass()
                    .getSimpleName().equals("DiagOrdenesDeCompra")) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    if (e.getKeyCode() == KeyEvent.VK_F3) {
                        agregarArticulo();
                        calcularTotal();
                    }/*from ww  w  .  ja  v a2  s  .  c o  m*/
                    if (e.getKeyCode() == KeyEvent.VK_F7) {
                        realizarVenta();
                    }
                    if (e.getKeyCode() == KeyEvent.VK_F4) {
                        cerrar();
                    }
                    if (e.getKeyCode() == KeyEvent.VK_DELETE && tblArticulos.getSelectedRow() != -1) {
                        quitarArticuloLista();
                    }

                }
            }

            return false;
        }
    });
}

From source file:vista.ventas.DialogEntregas.java

private void eventosDeTeclas() {
    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher(new KeyEventDispatcher() {
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow().getClass()
                    .getSimpleName().equals("DialogEntregas")) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    if (e.getKeyCode() == KeyEvent.VK_F3) {
                        agregarArticulo();
                        calcularSubtotalGral();
                    }/*from  w w w . j  a v  a 2  s.c  o m*/
                    if (e.getKeyCode() == KeyEvent.VK_F7) {
                        realizarVenta();
                    }
                    if (e.getKeyCode() == KeyEvent.VK_F4) {
                        cerrar();
                    }

                }
            }

            return false;
        }
    });
}