Example usage for javax.swing Action putValue

List of usage examples for javax.swing Action putValue

Introduction

In this page you can find the example usage for javax.swing Action putValue.

Prototype

public void putValue(String key, Object value);

Source Link

Document

Sets one of this object's properties using the associated key.

Usage

From source file:MainClass.java

public MainClass() {
    JFrame frame = new JFrame();
    JTextPane textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    JPanel north = new JPanel();

    JMenuBar menu = new JMenuBar();
    JMenu styleMenu = new JMenu();
    styleMenu.setText("Style");

    Action boldAction = new BoldAction();
    boldAction.putValue(Action.NAME, "Bold");
    styleMenu.add(boldAction);//from   w  w  w.j a  v a  2 s .c  o  m

    Action italicAction = new ItalicAction();
    italicAction.putValue(Action.NAME, "Italic");
    styleMenu.add(italicAction);

    Action foregroundAction = new ForegroundAction();
    foregroundAction.putValue(Action.NAME, "Color");
    styleMenu.add(foregroundAction);

    Action formatTextAction = new FontAndSizeAction();
    formatTextAction.putValue(Action.NAME, "Font and Size");
    styleMenu.add(formatTextAction);

    menu.add(styleMenu);

    north.add(menu);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(800, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:net.pandoragames.far.ui.swing.component.UndoHistoryPopupMenu.java

private void init(SwingConfig config, ComponentRepository componentRepository) {
    // COPY//from  www  .  j  a v a 2 s.  c  o  m
    JMenuItem copy = new JMenuItem(config.getLocalizer().localize("label.copy"));
    copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selection = textComponent.getSelectedText();
            if (selection != null) {
                StringSelection textTransfer = new StringSelection(selection);
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(textTransfer, null);
            }
        }
    });
    this.add(copy);
    // PASTE
    JMenuItem paste = new JMenuItem(config.getLocalizer().localize("label.paste"));
    paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (transfer != null && transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String) transfer.getTransferData(DataFlavor.stringFlavor);
                    String selected = textComponent.getSelectedText();
                    if (selected == null) {
                        int insertAt = textComponent.getCaretPosition();
                        textComponent.getDocument().insertString(insertAt, text, null);
                    } else {
                        int start = textComponent.getSelectionStart();
                        int end = textComponent.getSelectionEnd();
                        textComponent.getDocument().remove(start, end - start);
                        textComponent.getDocument().insertString(start, text, null);
                    }
                }
            } catch (UnsupportedFlavorException e) {
                LogFactory.getLog(this.getClass()).error("UnsupportedFlavorException reading from clipboard",
                        e);
            } catch (IOException iox) {
                LogFactory.getLog(this.getClass()).error("IOException reading from clipboard", iox);
            } catch (BadLocationException blx) {
                LogFactory.getLog(this.getClass()).error("BadLocationException reading from clipboard", blx);
            }
        }
    });
    this.add(paste);
    // UNDO
    Action undoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_UNDO);
    if (undoAction != null) {
        undoAction.putValue(Action.NAME, config.getLocalizer().localize("label.undo"));
        this.add(undoAction);
    }
    // REDO
    Action redoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_REDO);
    if (redoAction != null) {
        redoAction.putValue(Action.NAME, config.getLocalizer().localize("label.redo"));
        this.add(redoAction);
    }
    // PREVIOUS
    Action prevAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_PREVIOUS);
    if (prevAction != null) {
        prevAction.putValue(Action.NAME, config.getLocalizer().localize("label.previous"));
        this.add(prevAction);
    }
    // NEXT
    Action nextAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_NEXT);
    if (nextAction != null) {
        nextAction.putValue(Action.NAME, config.getLocalizer().localize("label.next"));
        this.add(nextAction);
    }
}

From source file:TextComponentDemo.java

protected JMenu createStyleMenu() {
    JMenu menu = new JMenu("Style");

    Action action = new StyledEditorKit.BoldAction();
    action.putValue(Action.NAME, "Bold");
    menu.add(action);//from  ww w . j  a va  2s  . c o m

    action = new StyledEditorKit.ItalicAction();
    action.putValue(Action.NAME, "Italic");
    menu.add(action);

    action = new StyledEditorKit.UnderlineAction();
    action.putValue(Action.NAME, "Underline");
    menu.add(action);

    menu.addSeparator();

    menu.add(new StyledEditorKit.FontSizeAction("12", 12));
    menu.add(new StyledEditorKit.FontSizeAction("14", 14));
    menu.add(new StyledEditorKit.FontSizeAction("18", 18));

    menu.addSeparator();

    menu.add(new StyledEditorKit.FontFamilyAction("Serif", "Serif"));
    menu.add(new StyledEditorKit.FontFamilyAction("SansSerif", "SansSerif"));

    menu.addSeparator();

    menu.add(new StyledEditorKit.ForegroundAction("Red", Color.red));
    menu.add(new StyledEditorKit.ForegroundAction("Green", Color.green));
    menu.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue));
    menu.add(new StyledEditorKit.ForegroundAction("Black", Color.black));

    return menu;
}

From source file:burlov.ultracipher.swing.MainPanel.java

public Action getDeleteEntryAction() {
    Action ret = new AbstractAction("deleteEntryAction") {

        /**/*from  w ww. j av  a 2 s . c om*/
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            deleteCurrentEntry();
        }
    };
    ret.putValue(Action.NAME, "Delete entry");
    ret.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control D"));
    return ret;
}

From source file:burlov.ultracipher.swing.MainPanel.java

public Action getNewEntryAction() {
    Action ret = new AbstractAction("deleteEntryAction") {

        /**/*w w w. j  a  va  2  s . c o  m*/
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            getEditDataPanel().setEditable(true);
            newEntry();
        }
    };
    ret.putValue(Action.NAME, "New entry");
    ret.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control N"));
    return ret;
}

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

private void setupActions() {

    wordList.addListSelectionListener(e -> {
        wordEditField.setText(wordList.getSelectedValue());
        wordEditField.selectAll();//from   w  w  w.  ja  v a2s.co  m
        new FocusRequester(wordEditField);
    });

    newWord.addActionListener(e -> newWordAction());

    ActionListener wordEditFieldListener = e -> actOnWordEdit();
    wordEditField.addActionListener(wordEditFieldListener);

    removeWord.addActionListener(e -> {
        int index = wordList.getSelectedIndex();
        if (index == -1) {
            return;
        }
        wordListModel.remove(index);
        wordEditField.setText("");
        if (!wordListModel.isEmpty()) {
            wordList.setSelectedIndex(Math.min(index, wordListModel.size() - 1));
        }
    });

    fieldList.addListSelectionListener(e -> {
        currentField = fieldList.getSelectedValue();
        fieldNameField.setText("");
        setupWordSelector();
    });

    newField.addActionListener(e -> {
        if (!fieldListModel.get(0).equals(FIELD_FIRST_LINE)) {
            // only add <field name> once
            fieldListModel.add(0, FIELD_FIRST_LINE);
        }
        fieldList.setSelectedIndex(0);
        fPane.getVerticalScrollBar().setValue(0);
        fieldNameField.setEnabled(true);
        fieldNameField.setText(currentField);
        fieldNameField.selectAll();

        new FocusRequester(fieldNameField);
    });

    fieldNameField.addActionListener(e -> fieldNameField.transferFocus());

    fieldNameField.addFocusListener(new FieldNameFocusAdapter());

    removeField.addActionListener(e -> {
        int index = fieldList.getSelectedIndex();
        if (index == -1) {
            return;
        }
        String fieldName = fieldListModel.get(index);
        removedFields.add(fieldName);
        fieldListModel.remove(index);
        wordListModels.remove(fieldName);
        fieldNameField.setText("");
        if (!fieldListModel.isEmpty()) {
            fieldList.setSelectedIndex(Math.min(index, wordListModel.size() - 1));
        }
    });

    ok.addActionListener(e -> {
        try {
            applyChanges();
            dispose();
        } catch (Exception ex) {
            LOGGER.info("Could not apply changes in \"Manage content selectors\"", ex);
            JOptionPane.showMessageDialog(frame, Localization.lang("Could not apply changes."));
        }
    });

    apply.addActionListener(e -> {
        // Store if an entry is currently being edited:
        if (!"".equals(wordEditField.getText())) {
            wordEditFieldListener.actionPerformed(null);
        }
        try {
            applyChanges();
        } catch (Exception ex) {
            LOGGER.info("Could not apply changes in \"Manage content selectors\"", ex);
            JOptionPane.showMessageDialog(frame, Localization.lang("Could not apply changes."));
        }
    });

    Action cancelAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    };
    cancelAction.putValue(Action.NAME, Localization.lang("Cancel"));
    cancel.setAction(cancelAction);
}

From source file:com.eviware.soapui.impl.wsdl.panels.request.AbstractWsdlRequestDesktopPanel.java

protected JButton createActionButton(Action action, boolean enabled) {
    JButton button = UISupport.createToolbarButton(action, enabled);
    action.putValue(Action.NAME, null);
    return button;
}

From source file:com.diversityarrays.kdxplore.field.FieldViewDialog.java

private void initialiseAction(Action action, String resourceName, String tooltip) {
    action.putValue(Action.SHORT_DESCRIPTION, tooltip);

    InputStream is = getClass().getResourceAsStream(resourceName);
    if (is != null) {
        try {//  ww  w  .ja v  a 2 s  .  c  o m
            BufferedImage img = ImageIO.read(is);
            action.putValue(Action.SMALL_ICON, new ImageIcon(img));
        } catch (IOException ignore) {
        }
    }
}

From source file:captureplugin.CapturePlugin.java

public ActionMenu getContextMenuActions(final Program program) {

    final DeviceIf[] devices = mConfig.getDeviceArray();

    final Window parent = UiUtilities.getLastModalChildOf(getParentFrame());

    Action mainaction = new devplugin.ContextMenuAction();
    mainaction.putValue(Action.NAME, mLocalizer.msg("record", "record Program"));
    mainaction.putValue(Action.SMALL_ICON, createImageIcon("mimetypes", "video-x-generic", 16));

    ArrayList<ActionMenu> actionList = new ArrayList<ActionMenu>();

    for (final DeviceIf dev : devices) {
        Action action = new ContextMenuAction();
        action.putValue(Action.NAME, dev.getName());

        ArrayList<AbstractAction> commandList = new ArrayList<AbstractAction>();

        if (dev.isAbleToAddAndRemovePrograms()) {
            final Program test = dev.getProgramForProgramInList(program);

            if (test != null) {
                AbstractAction caction = new AbstractAction() {
                    public void actionPerformed(ActionEvent evt) {
                        dev.remove(parent, test);
                        updateMarkedPrograms();
                    }//w  w w . j  a v  a 2  s  .co m
                };
                caction.putValue(Action.NAME, Localizer.getLocalization(Localizer.I18N_DELETE));
                commandList.add(caction);
            } else {
                AbstractAction caction = new AbstractAction() {
                    public void actionPerformed(ActionEvent evt) {
                        if (dev.add(parent, program)) {
                            dev.sendProgramsToReceiveTargets(new Program[] { program });
                        }
                        updateMarkedPrograms();
                    }
                };
                caction.putValue(Action.NAME, mLocalizer.msg("record", "record"));
                commandList.add(caction);
            }

        }

        String[] commands = dev.getAdditionalCommands();

        if (commands != null) {
            for (int y = 0; y < commands.length; y++) {

                final int num = y;

                AbstractAction caction = new AbstractAction() {

                    public void actionPerformed(ActionEvent evt) {
                        dev.executeAdditionalCommand(parent, num, program);
                    }
                };
                caction.putValue(Action.NAME, commands[y]);
                commandList.add(caction);
            }
        }

        if (!commandList.isEmpty()) {
            actionList.add(new ActionMenu(action, commandList.toArray(new Action[commandList.size()])));
        }
    }

    if (actionList.size() == 1) {
        ActionMenu menu = actionList.get(0);

        if (menu.getSubItems().length == 0) {
            return null;
        }

        if (menu.getSubItems().length == 1) {
            Action action = menu.getSubItems()[0].getAction();
            action.putValue(Action.SMALL_ICON, createImageIcon("mimetypes", "video-x-generic", 16));
            return new ActionMenu(action);
        } else {
            mainaction.putValue(Action.NAME, menu.getTitle());
            return new ActionMenu(mainaction, menu.getSubItems());
        }

    }

    ActionMenu[] actions = new ActionMenu[actionList.size()];
    actionList.toArray(actions);

    if (actions.length == 0) {
        return null;
    }

    return new ActionMenu(mainaction, actions);
}

From source file:com.willwinder.ugs.nbp.lib.services.ActionRegistrationService.java

/**
 * Registers an action with the platform along with optional shortcuts and
 * menu items.//from   w ww .  j  ava2 s .  c  o m
 *
 * @param id The unique id of the action
 * @param name Display name of the action.
 * @param category Category in the Keymap tool.
 * @param shortcut Default shortcut, use an empty string or null for none.
 * @param menuPath Menu location starting with "Menu", like "Menu/Head/Hats"
 * @param localMenu Localized menu location starting with "Menu", like "Menu/Cabeza/Sombreros"
 * @param action an action object to attach to the action entry.
 * @throws IOException 
 */
public void registerAction(String id, String name, String category, String localCategory, String shortcut,
        String menuPath, String localMenu, Action action) throws IOException {
    ///////////////////////
    // Add/Update Action //
    ///////////////////////
    String originalFile = "Actions/" + category + "/" + id + ".instance";
    FileObject root = FileUtil.getConfigRoot();
    FileObject in = FileUtil.createFolder(root, "Actions/" + category);
    //in.setAttribute("displayName", localCategory);
    //in.setAttribute("SystemFileSystem.localizingBundle", localCategory + "lkhaglk");
    //in.setAttribute("SystemFileSystem.localizingBundle", localCategory);
    in.refresh();

    FileObject obj = in.getFileObject(id, "instance");
    if (obj == null) {
        obj = in.createData(id, "instance");
    }
    action.putValue(Action.NAME, name);
    obj.setAttribute("instanceCreate", action);
    obj.setAttribute("instanceClass", action.getClass().getName());

    /////////////////////
    // Add/Update Menu //
    /////////////////////
    if (StringUtils.isNotEmpty(menuPath) && StringUtils.isNotEmpty(id)) {
        in = createAndLocalizeFullMenu(menuPath, localMenu);

        obj = in.getFileObject(id, SHADOW);
        // Create if missing.
        if (obj == null) {
            obj = in.createData(id, SHADOW);
            obj.setAttribute("originalFile", originalFile);
        }
    }

    /////////////////////////
    // Add/Update Shortcut //
    /////////////////////////
    if (shortcut != null && shortcut.length() > 0) {
        in = FileUtil.createFolder(root, "Shortcuts");
        obj = in.getFileObject(shortcut, SHADOW);
        if (obj == null) {
            obj = in.createData(shortcut, SHADOW);
            obj.setAttribute("originalFile", originalFile);
        }
    }
}