Example usage for javax.swing InputMap put

List of usage examples for javax.swing InputMap put

Introduction

In this page you can find the example usage for javax.swing InputMap put.

Prototype

public void put(KeyStroke keyStroke, Object actionMapKey) 

Source Link

Document

Adds a binding for keyStroke to actionMapKey .

Usage

From source file:net.java.sip.communicator.plugin.propertieseditor.SearchField.java

/**
 * Creates an instance <tt>SearchField</tt>.
 * //from w  w w .j a  v a2 s  .  c om
 * @param text the text we would like to enter by default
 * @param sorter the sorter which will be used for filtering.
 */
public SearchField(String text, JTable table) {
    super(text);

    this.table = table;

    ComponentUI ui = getUI();

    if (ui instanceof SearchFieldUI) {
        SearchFieldUI searchFieldUI = (SearchFieldUI) ui;

        searchFieldUI.setCallButtonEnabled(false);
        searchFieldUI.setDeleteButtonEnabled(true);
    }

    setBorder(null);
    setDragEnabled(true);
    setOpaque(false);
    setPreferredSize(new Dimension(100, 25));

    InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");

    ActionMap amap = getActionMap();
    @SuppressWarnings("serial")
    AbstractAction escapeAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            setText("");
        }
    };

    amap.put("escape", escapeAction);

    getDocument().addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
        }

        public void insertUpdate(DocumentEvent e) {
            filter(getText());
        }

        public void removeUpdate(DocumentEvent e) {
            filter(getText());
        }
    });

    loadSkin();
}

From source file:org.pgptool.gui.ui.keyslist.KeysTableView.java

@SuppressWarnings("serial")
private void initTableKeyListener() {
    int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
    InputMap inputMap = table.getInputMap(condition);
    ActionMap actionMap = table.getActionMap();
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), DELETE);
    actionMap.put(DELETE, new AbstractAction() {
        @Override// www.  j a  va 2  s.  c  o  m
        public void actionPerformed(ActionEvent e) {
            if (pm == null) {
                return;
            }
            safePerformAction(pm.getActionDelete(), null);
        }
    });
}

From source file:net.erdfelt.android.sdkfido.ui.SdkFidoFrame.java

private void enableExitKey() {
    InputMap rootInput = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap rootAction = getRootPane().getActionMap();

    if (SystemUtils.IS_OS_UNIX || SystemUtils.IS_OS_WINDOWS) {
        rootInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.CTRL_DOWN_MASK), "exit");
        rootInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK), "exit");
    }/*from w  ww. j  a va2 s  . co  m*/

    if (SystemUtils.IS_OS_MAC) {
        rootInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.META_DOWN_MASK), "exit");
    }

    rootAction.put("exit", new KeyAction(actionMapper, "exit"));
}

From source file:net.sf.jabref.wizard.auximport.gui.FromAuxDialog.java

private void jbInit() {
    JPanel panel1 = new JPanel();

    panel1.setLayout(new BorderLayout());
    selectInDBButton.setText(Localization.lang("Select"));
    selectInDBButton.setEnabled(false);// w w  w .j  ava 2  s.c  om
    selectInDBButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            FromAuxDialog.this.select_actionPerformed();
        }
    });
    generateButton.setText(Localization.lang("Generate"));
    generateButton.setEnabled(false);
    generateButton.addActionListener(new FromAuxDialog_generate_actionAdapter(this));
    cancelButton.setText(Localization.lang("Cancel"));
    cancelButton.addActionListener(new FromAuxDialog_Cancel_actionAdapter(this));
    parseButton.setText(Localization.lang("Parse"));
    parseButton.addActionListener(new FromAuxDialog_parse_actionAdapter(this));

    initPanels();

    // insert the buttons
    ButtonBarBuilder bb = new ButtonBarBuilder();
    JPanel buttonPanel = bb.getPanel();
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    bb.addGlue();
    bb.addButton(parseButton);
    bb.addRelatedGap();
    bb.addButton(selectInDBButton);
    bb.addButton(generateButton);
    bb.addButton(cancelButton);
    bb.addGlue();
    this.setModal(true);
    this.setResizable(true);
    this.setTitle(Localization.lang("AUX file import"));
    JLabel desc = new JLabel("<html><h3>" + Localization.lang("AUX file import") + "</h3><p>"
            + Localization.lang("This feature generates a new database based on which entries "
                    + "are needed in an existing LaTeX document.")
            + "</p>" + "<p>"
            + Localization.lang("You need to select one of your open databases from which to choose "
                    + "entries, as well as the AUX file produced by LaTeX when compiling your document.")
            + "</p></html>");
    desc.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    panel1.add(desc, BorderLayout.NORTH);

    JPanel centerPane = new JPanel(new BorderLayout());
    centerPane.add(buttons, BorderLayout.NORTH);
    centerPane.add(statusPanel, BorderLayout.CENTER);

    getContentPane().add(panel1, BorderLayout.NORTH);
    getContentPane().add(centerPane, BorderLayout.CENTER);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    // Key bindings:
    ActionMap am = statusPanel.getActionMap();
    InputMap im = statusPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });

}

From source file:net.sf.jabref.gui.PreviewPanel.java

private void createKeyBindings() {
    ActionMap actionMap = this.getActionMap();
    InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

    final String close = "close";
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), close);
    actionMap.put(close, this.closeAction);

    final String copy = "copy";
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.COPY_PREVIEW), copy);
    actionMap.put(copy, this.copyPreviewAction);

    final String print = "print";
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.PRINT_ENTRY_PREVIEW), print);
    actionMap.put(print, this.printAction);
}

From source file:DragPictureDemo2.java

public DTPicture(Image image) {
    super(image);
    addMouseMotionListener(this);

    //Add the cut/copy/paste key bindings to the input map.
    //Note that this step is redundant if you are installing
    //menu accelerators that cause these actions to be invoked.
    //DragPictureDemo does not use menu accelerators and, since
    //the default value of installInputMapBindings is true,
    //the bindings are installed. DragPictureDemo2 does use
    //menu accelerators and so calls setInstallInputMapBindings
    //with a value of false. Your program would do one or the
    //other, but not both.
    if (installInputMapBindings) {
        InputMap imap = this.getInputMap();
        imap.put(KeyStroke.getKeyStroke("ctrl X"), TransferHandler.getCutAction().getValue(Action.NAME));
        imap.put(KeyStroke.getKeyStroke("ctrl C"), TransferHandler.getCopyAction().getValue(Action.NAME));
        imap.put(KeyStroke.getKeyStroke("ctrl V"), TransferHandler.getPasteAction().getValue(Action.NAME));
    }/*from   w w w  .ja va  2  s . c o  m*/

    //Add the cut/copy/paste actions to the action map.
    //This step is necessary because the menu's action listener
    //looks for these actions to fire.
    ActionMap map = this.getActionMap();
    map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
    map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
    map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
}

From source file:net.sf.jabref.importer.ImportCustomizationDialog.java

/**
 *
 * @param frame/*w ww. j  a va 2  s.c o  m*/
 */
public ImportCustomizationDialog(final JabRefFrame frame) {
    super(frame, Localization.lang("Manage custom imports"), false);

    ImportTableModel tableModel = new ImportTableModel();
    customImporterTable = new JTable(tableModel);
    TableColumnModel cm = customImporterTable.getColumnModel();
    cm.getColumn(0).setPreferredWidth(COL_0_WIDTH);
    cm.getColumn(1).setPreferredWidth(COL_1_WIDTH);
    cm.getColumn(2).setPreferredWidth(COL_2_WIDTH);
    cm.getColumn(3).setPreferredWidth(COL_3_WIDTH);
    JScrollPane sp = new JScrollPane(customImporterTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    customImporterTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    customImporterTable.setPreferredScrollableViewportSize(getSize());
    if (customImporterTable.getRowCount() > 0) {
        customImporterTable.setRowSelectionInterval(0, 0);
    }

    JButton addFromFolderButton = new JButton(Localization.lang("Add from folder"));
    addFromFolderButton.addActionListener(e -> {
        CustomImporter importer = new CustomImporter();
        importer.setBasePath(FileDialogs.getNewDir(frame,
                new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Collections.emptyList(),
                Localization.lang("Select Classpath of New Importer"), JFileChooser.CUSTOM_DIALOG, false));
        String chosenFileStr = null;
        if (importer.getBasePath() != null) {
            chosenFileStr = FileDialogs.getNewFile(frame, importer.getFileFromBasePath(),
                    Collections.singletonList(".class"), Localization.lang("Select new ImportFormat subclass"),
                    JFileChooser.CUSTOM_DIALOG, false);
        }
        if (chosenFileStr != null) {
            try {
                importer.setClassName(pathToClass(importer.getFileFromBasePath(), new File(chosenFileStr)));
                importer.setName(importer.getInstance().getFormatName());
                importer.setCliId(importer.getInstance().getId());
                addOrReplaceImporter(importer);
                customImporterTable.revalidate();
                customImporterTable.repaint();
            } catch (Exception exc) {
                JOptionPane.showMessageDialog(frame,
                        Localization.lang("Could not instantiate %0", chosenFileStr));
            } catch (NoClassDefFoundError exc) {
                JOptionPane.showMessageDialog(frame, Localization.lang(
                        "Could not instantiate %0. Have you chosen the correct package path?", chosenFileStr));
            }

        }
    });
    addFromFolderButton
            .setToolTipText(Localization.lang("Add a (compiled) custom ImportFormat class from a class path.")
                    + "\n" + Localization.lang("The path need not be on the classpath of JabRef."));

    JButton addFromJarButton = new JButton(Localization.lang("Add from jar"));
    addFromJarButton.addActionListener(e -> {
        String basePath = FileDialogs.getNewFile(frame,
                new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Arrays.asList(".zip", ".jar"),
                Localization.lang("Select a Zip-archive"), JFileChooser.CUSTOM_DIALOG, false);

        if (basePath != null) {
            try (ZipFile zipFile = new ZipFile(new File(basePath), ZipFile.OPEN_READ)) {
                ZipFileChooser zipFileChooser = new ZipFileChooser(this, zipFile);
                zipFileChooser.setVisible(true);
                customImporterTable.revalidate();
                customImporterTable.repaint(10);
            } catch (IOException exc) {
                LOGGER.info("Could not open Zip-archive.", exc);
                JOptionPane.showMessageDialog(frame, Localization.lang("Could not open %0", basePath) + "\n"
                        + Localization.lang("Have you chosen the correct package path?"));
            } catch (NoClassDefFoundError exc) {
                LOGGER.info("Could not instantiate Zip-archive reader.", exc);
                JOptionPane.showMessageDialog(frame, Localization.lang("Could not instantiate %0", basePath)
                        + "\n" + Localization.lang("Have you chosen the correct package path?"));
            }
        }
    });
    addFromJarButton
            .setToolTipText(Localization.lang("Add a (compiled) custom ImportFormat class from a Zip-archive.")
                    + "\n" + Localization.lang("The Zip-archive need not be on the classpath of JabRef."));

    JButton showDescButton = new JButton(Localization.lang("Show description"));
    showDescButton.addActionListener(e -> {
        int row = customImporterTable.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(frame, Localization.lang("Please select an importer."));
        } else {
            CustomImporter importer = ((ImportTableModel) customImporterTable.getModel()).getImporter(row);
            try {
                ImportFormat importFormat = importer.getInstance();
                JOptionPane.showMessageDialog(frame, importFormat.getDescription());
            } catch (IOException | ClassNotFoundException | InstantiationException
                    | IllegalAccessException exc) {
                LOGGER.warn("Could not instantiate importer " + importer.getName(), exc);
                JOptionPane.showMessageDialog(frame, Localization.lang("Could not instantiate %0 %1",
                        importer.getName() + ":\n", exc.getMessage()));
            }
        }
    });

    JButton removeButton = new JButton(Localization.lang("Remove"));
    removeButton.addActionListener(e -> {
        int row = customImporterTable.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(frame, Localization.lang("Please select an importer."));
        } else {
            customImporterTable.removeRowSelectionInterval(row, row);
            Globals.prefs.customImports
                    .remove(((ImportTableModel) customImporterTable.getModel()).getImporter(row));
            Globals.IMPORT_FORMAT_READER.resetImportFormats();
            customImporterTable.revalidate();
            customImporterTable.repaint();
        }
    });

    Action closeAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    };

    JButton closeButton = new JButton(Localization.lang("Close"));
    closeButton.addActionListener(closeAction);

    JButton helpButton = new HelpAction(HelpFile.CUSTOM_IMPORTS).getHelpButton();

    // Key bindings:
    JPanel mainPanel = new JPanel();
    ActionMap am = mainPanel.getActionMap();
    InputMap im = mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", closeAction);
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(sp, BorderLayout.CENTER);
    JPanel buttons = new JPanel();
    ButtonBarBuilder bb = new ButtonBarBuilder(buttons);
    buttons.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    bb.addGlue();
    bb.addButton(addFromFolderButton);
    bb.addButton(addFromJarButton);
    bb.addButton(showDescButton);
    bb.addButton(removeButton);
    bb.addButton(closeButton);
    bb.addUnrelatedGap();
    bb.addButton(helpButton);
    bb.addGlue();

    getContentPane().add(mainPanel, BorderLayout.CENTER);
    getContentPane().add(buttons, BorderLayout.SOUTH);
    this.setSize(getSize());
    pack();
    this.setLocationRelativeTo(frame);
    new FocusRequester(customImporterTable);
}

From source file:TextComponentDemo.java

protected void addBindings() {
    InputMap inputMap = textPane.getInputMap();

    // Ctrl-b to go backward one character
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.backwardAction);

    // Ctrl-f to go forward one character
    key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.forwardAction);

    // Ctrl-p to go up one line
    key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.upAction);

    // Ctrl-n to go down one line
    key = KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.downAction);
}

From source file:net.sf.jabref.gui.FileListEntryEditor.java

public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar,
        boolean showOpenButton, BibDatabaseContext databaseContext) {
    this.entry = entry;
    this.databaseContext = databaseContext;

    ActionListener okAction = e -> {
        // If OK button is disabled, ignore this event:
        if (!ok.isEnabled()) {
            return;
        }/*from  w  w  w . jav  a2 s  .  c  o  m*/
        // If necessary, ask the external confirm object whether we are ready to close.
        if (externalConfirm != null) {
            // Construct an updated FileListEntry:
            storeSettings(entry);
            if (!externalConfirm.confirmClose(entry)) {
                return;
            }
        }
        diag.dispose();
        storeSettings(FileListEntryEditor.this.entry);
        okPressed = true;
    };
    types = new JComboBox<>();
    types.addItemListener(itemEvent -> {
        if (!okDisabledExternally) {
            ok.setEnabled(types.getSelectedItem() != null);
        }
    });

    FormBuilder builder = FormBuilder.create().layout(new FormLayout(
            "left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p"));
    builder.add(Localization.lang("Link")).xy(1, 1);
    builder.add(link).xy(3, 1);
    final BrowseListener browse = new BrowseListener(frame, link);
    final JButton browseBut = new JButton(Localization.lang("Browse"));
    browseBut.addActionListener(browse);
    builder.add(browseBut).xy(5, 1);
    JButton open = new JButton(Localization.lang("Open"));
    if (showOpenButton) {
        builder.add(open).xy(7, 1);
    }
    builder.add(Localization.lang("Description")).xy(1, 3);
    builder.add(description).xyw(3, 3, 3);
    builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    builder.add(Localization.lang("File type")).xy(1, 5);
    builder.add(types).xyw(3, 5, 3);
    if (showProgressBar) {
        builder.appendRows("2dlu, p");
        builder.add(downloadLabel).xy(1, 7);
        builder.add(prog).xyw(3, 7, 3);
    }

    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    bb.addRelatedGap();
    bb.addButton(ok);
    JButton cancel = new JButton(Localization.lang("Cancel"));
    bb.addButton(cancel);
    bb.addGlue();
    bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    ok.addActionListener(okAction);
    // Add OK action to the two text fields to simplify entering:
    link.addActionListener(okAction);
    description.addActionListener(okAction);

    open.addActionListener(e -> openFile());

    AbstractAction cancelAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            diag.dispose();
        }
    };
    cancel.addActionListener(cancelAction);

    // Key bindings:
    ActionMap am = builder.getPanel().getActionMap();
    InputMap im = builder.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", cancelAction);

    link.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            checkExtension();
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            // Do nothing
        }

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            checkExtension();
        }

    });

    diag = new JDialog(frame, Localization.lang("Save file"), true);
    diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
    diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
    diag.pack();
    diag.setLocationRelativeTo(frame);
    diag.addWindowListener(new WindowAdapter() {

        @Override
        public void windowActivated(WindowEvent event) {
            if (openBrowseWhenShown && !dontOpenBrowseUntilDisposed) {
                dontOpenBrowseUntilDisposed = true;
                SwingUtilities.invokeLater(() -> browse.actionPerformed(new ActionEvent(browseBut, 0, "")));
            }
        }

        @Override
        public void windowClosed(WindowEvent event) {
            dontOpenBrowseUntilDisposed = false;
        }
    });
    setValues(entry);
}

From source file:TextComponentDemo.java

protected void addBindings() {
    InputMap inputMap = textPane.getInputMap();

    //Ctrl-b to go backward one character
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.backwardAction);

    //Ctrl-f to go forward one character
    key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.forwardAction);

    //Ctrl-p to go up one line
    key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.upAction);

    //Ctrl-n to go down one line
    key = KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.downAction);
}