Example usage for javax.swing ActionMap setParent

List of usage examples for javax.swing ActionMap setParent

Introduction

In this page you can find the example usage for javax.swing ActionMap setParent.

Prototype

public void setParent(ActionMap map) 

Source Link

Document

Sets this ActionMap's parent.

Usage

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from w w w  .  ja v  a2s. c om
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();

    ActionMap newMap = new ActionMap();
    newMap.setParent(actionMap);

    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from   ww w  . j a va2s  . c om
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();

    ActionMap newMap = new ActionMap();
    newMap.setParent(actionMap);
    System.out.println(newMap.getParent());

    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void uninstallActionMap(JComponent c, ActionMap map) {
    if (map == null) {
        return;//w ww .j  av a 2 s  .c o  m
    }

    ActionMap firstMap = c.getActionMap();
    ActionMap parent = firstMap;
    ActionMap child = null;
    ActionMap newMap = null;
    while (parent != null) {
        if (parent == map) {
            if (child != null) {
                child.setParent(parent.getParent());
                child = parent;
            } else {
                newMap = parent.getParent();
            }
        } else {
            child = parent;
        }
        parent = parent.getParent();
    }

    if (newMap != null) {
        c.setActionMap(newMap);
    }
}

From source file:Main.java

public static ActionMap installActionMap(JComponent c, ActionMap map) {
    if (map == null) {
        return null;
    }// w  ww . j  a  va  2s.  c  o m

    ActionMap currentMap = c.getActionMap();
    if (currentMap != null) {
        ActionMap parent = currentMap;
        while (parent.getParent() != null) {
            if (parent == map) {
                return map;
            }
            parent = parent.getParent();
        }
        parent.setParent(map);
    } else {
        c.setActionMap(map);
    }
    return map;
}

From source file:DigitalClock.java

public DigitalClock() {
    // Set default values for our properties
    setFormat(DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale()));
    setUpdateFrequency(1000); // Update once a second

    // Specify a Swing TransferHandler object to do the dirty work of
    // copy-and-paste and drag-and-drop for us. This one will transfer
    // the value of the "time" property. Since this property is read-only
    // it will allow drags but not drops.
    setTransferHandler(new TransferHandler("time"));

    // Since JLabel does not normally support drag-and-drop, we need an
    // event handler to detect a drag and start the transfer.
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            getTransferHandler().exportAsDrag(DigitalClock.this, e, TransferHandler.COPY);
        }/*w w  w.  j a va 2s  .  co m*/
    });

    // Before we can have a keyboard binding for a Copy command,
    // the component needs to be able to accept keyboard focus.
    setFocusable(true);
    // Request focus when we're clicked on
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            requestFocus();
        }
    });
    // Use a LineBorder to indicate when we've got the keyboard focus
    addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            setBorder(LineBorder.createBlackLineBorder());
        }

        public void focusLost(FocusEvent e) {
            setBorder(null);
        }
    });

    // Now bind the Ctrl-C keystroke to a "Copy" command.
    InputMap im = new InputMap();
    im.setParent(getInputMap(WHEN_FOCUSED));
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy");
    setInputMap(WHEN_FOCUSED, im);

    // And bind the "Copy" command to a pre-defined Action that performs
    // a copy using the TransferHandler we've installed.
    ActionMap am = new ActionMap();
    am.setParent(getActionMap());
    am.put("Copy", TransferHandler.getCopyAction());
    setActionMap(am);

    // Create a javax.swing.Timer object that will generate ActionEvents
    // to tell us when to update the displayed time. Every updateFrequency
    // milliseconds, this timer will cause the actionPerformed() method
    // to be invoked. (For non-GUI applications, see java.util.Timer.)
    timer = new Timer(updateFrequency, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setText(getTime()); // set label to current time string
        }
    });
    timer.setInitialDelay(0); // Do the first update immediately
    timer.start(); // Start timing now!
}

From source file:org.docx4all.swing.text.WordMLEditorKit.java

private void initKeyBindings(JEditorPane editor) {
    ActionMap myActionMap = new ActionMap();
    InputMap myInputMap = new InputMap();

    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK);
    myActionMap.put(insertSoftBreakAction, new InsertSoftBreakAction(insertSoftBreakAction));
    myInputMap.put(ks, insertSoftBreakAction);

    ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    myActionMap.put(enterKeyTypedAction, new EnterKeyTypedAction(enterKeyTypedAction));
    myInputMap.put(ks, enterKeyTypedAction);

    ks = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
    myActionMap.put(deleteNextCharAction, new DeleteNextCharAction(deleteNextCharAction));
    myInputMap.put(ks, deleteNextCharAction);

    ks = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0);
    myActionMap.put(deletePrevCharAction, new DeletePrevCharAction(deletePrevCharAction));
    myInputMap.put(ks, deletePrevCharAction);

    myActionMap.setParent(editor.getActionMap());
    myInputMap.setParent(editor.getInputMap());
    editor.setActionMap(myActionMap);//from   www .j  a  v  a 2s. c om
    editor.setInputMap(JComponent.WHEN_FOCUSED, myInputMap);
}

From source file:org.trianacode.gui.hci.ApplicationFrame.java

/**
 * Initialises the panels in the main window
 *//*from   w w  w .  j a  va  2s.c o m*/
private void initLayout() {

    GUIEnv.setApplicationFrame(this);
    ColorManager.setDefaultColorModel(new TrianaColorModel());
    ColorManager.registerColorModel(ScriptConstants.SCRIPT_RENDERING_HINT, new ScriptColorModel());

    // do this after all other color loading
    ColorTable.instance().loadUserPrefs();

    TaskGraphView defaultview = new TaskGraphView("Default View");
    TrianaComponentModel compmodel = new TrianaComponentModel(tools, this, this);

    defaultview.setDefaultToolModel(compmodel);
    defaultview.setDefaultOpenGroupModel(compmodel);
    defaultview.registerToolModel(ScriptConstants.SCRIPT_RENDERING_HINT, new ScriptComponentModel());
    defaultview.registerToolModel(TextToolConstants.TEXT_TOOL_RENDERING_HINT, new TextToolComponentModel());
    defaultview.registerToolModel(HiddenToolConstants.HIDDEN_RENDERING_HINT, new HiddenComponentModel());

    TaskGraphView mapview = new TaskGraphView("Map View", defaultview);
    mapview.registerOpenGroupModel(MapConstants.MAP_RENDERING_HINT, new MapComponentModel());
    mapview.registerToolModel(MapConstants.MAP_LOCATION_RENDERING_HINT, new MapLocationComponentModel());

    TaskGraphViewManager.setDefaultTaskGraphView(defaultview);
    TaskGraphViewManager.registerTaskGraphView(MapConstants.MAP_RENDERING_HINT, mapview);

    taskGraphFileHandler = new TaskGraphFileHandler();

    trianaMenuBar = new TrianaMainMenu(this, tools);
    this.setJMenuBar(trianaMenuBar);

    TrianaShutdownHook shutDownHook = new TrianaShutdownHook();
    Runtime.getRuntime().addShutdownHook(shutDownHook);
    getDesktopViewManager().addDesktopViewListener(this);
    this.workspace.add(getDesktopViewManager().getWorkspace(), BorderLayout.CENTER);

    ((TrianaMainMenu) trianaMenuBar).addHelp();

    ToolTreeModel treemodel = new ToolTreeModel(tools);
    toolboxTree = new JTree(treemodel);
    toolboxTree.addFocusListener(this);
    toolboxTree.setCellRenderer(new TrianaTreeRenderer());
    toolmonitor.setTree(toolboxTree);

    treemodel.addTreeModelListener(this);

    ToolTipManager.sharedInstance().registerComponent(toolboxTree);
    ToolTipManager.sharedInstance().setInitialDelay(TOOL_TIP_SHOW_DELAY);
    ToolTipManager.sharedInstance().setDismissDelay(TOOL_TIP_HIDE_DELAY);

    //set up key maps
    MainTrianaKeyMapFactory keymaps = new MainTrianaKeyMapFactory(this, ActionDisplayOptions.DISPLAY_NAME);
    InputMap inputMap = keymaps.getInputMap();
    inputMap.setParent(this.getRootPane().getInputMap());
    this.getRootPane().setInputMap(JComponent.WHEN_FOCUSED, inputMap);
    ActionMap actMap = keymaps.getActionMap();
    actMap.setParent(this.getRootPane().getActionMap());
    this.getRootPane().setActionMap(actMap);

    leaflistener = new LeafListener(toolboxTree, this, tools);

    keymaps = new MainTrianaKeyMapFactory(leaflistener, ActionDisplayOptions.DISPLAY_NAME);
    inputMap = keymaps.getInputMap();
    inputMap.setParent(toolboxTree.getInputMap());
    toolboxTree.setInputMap(JComponent.WHEN_FOCUSED, inputMap);
    actMap = keymaps.getActionMap();
    actMap.setParent(toolboxTree.getActionMap());
    toolboxTree.setActionMap(actMap);

    toolboxTree.addMouseListener(leaflistener);
    toolboxTree.addMouseMotionListener(leaflistener);
    //toolboxTree.setRootVisible(false);
    JPanel toolPanel = new JPanel(new BorderLayout());

    SearchToolBar searchtoolbar = new SearchToolBar("Search", toolboxTree, treemodel);
    searchtoolbar.setFloatable(false);

    toolPanel.add(searchtoolbar, BorderLayout.NORTH);
    JScrollPane scroll = new JScrollPane(toolboxTree);

    toolPanel.add(scroll, BorderLayout.CENTER);

    JSplitPane verticalSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, toolPanel, workspace);

    TrianaToolBar toolbar = new TrianaToolBar("Main ToolBar", this);
    TrianaUnitToolBar unitToolbar = new TrianaUnitToolBar("Unit ToolBar");
    toolbar.setRollover(true);
    unitToolbar.setRollover(true);

    JPanel innerpanel = new JPanel();
    innerpanel.setLayout(new BoxLayout(innerpanel, BoxLayout.X_AXIS));
    innerpanel.add(toolbar);
    innerpanel.add(Box.createHorizontalStrut(10));
    innerpanel.add(unitToolbar);
    innerpanel.add(Box.createHorizontalGlue());

    JPanel outerpanel = new JPanel(new BorderLayout());

    outerpanel.add(innerpanel, BorderLayout.NORTH);
    outerpanel.add(verticalSplit, BorderLayout.CENTER);
    getContentPane().add(outerpanel);
}