Region-Specific CutAction

The CutAction, and other global Actions, can be shared between multiple TopComponents. Depending on what needs cutting, an Action together with the "cut-to-clipboard" key is added to the ActionMap of any TopComponent that needs to integrate with the CutAction.

But what about if you have different regions within a single TopComponent? Each region should have the CutAction delegate to a region-specific CutAction. Here's how to do that, with two simple JPanels representing two regions in the TopComponent:

public class DemoTopComponent extends TopComponent {

    private final ExplorerManager em = new ExplorerManager();
    private final JPanel subPanel1 = new SubPanel1();
    private final JPanel subPanel2 = new SubPanel2();
    private final ActionMap actionMap;

    public DemoTopComponent() {
        setName("Demo");
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        actionMap = getActionMap();
        add(subPanel1);
        add(subPanel2);
        associateLookup(ExplorerUtils.createLookup(em, actionMap));
    }

    private class SubPanel1 extends JPanel {
        public SubPanel1() {
            setBorder(new TitledBorder(new LineBorder(Color.BLACK), "one"));
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    requestActive();
                    setBorder(new TitledBorder(new LineBorder(Color.RED), "one"));
                    subPanel2.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "two"));                   
                    actionMap.put("cut-to-clipboard", new AbstractAction() {
                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            JOptionPane.showMessageDialog(null, "cut one");
                        }
                    });
                }
            });
        }
    }

    private class SubPanel2 extends JPanel {
        public SubPanel2() {
            setBorder(new TitledBorder(new LineBorder(Color.BLACK), "two"));
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    requestActive();
                    setBorder(new TitledBorder(new LineBorder(Color.RED), "two"));
                    subPanel1.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "one"));                   
                    actionMap.put("cut-to-clipboard", new AbstractAction() {
                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            JOptionPane.showMessageDialog(null, "cut two");
                        }
                    });
                }
            });
        }
    }

    @Override
    protected void componentDeactivated() {
        subPanel1.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "one"));
        subPanel2.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "two"));
    }

}

Here you can see the result. In the image below, a panel is selected and the border is red, as defined above, and the CutAction is enabled, proven by the Cut button being enabled:

If the other panel were selected, the CutAction would also be enabled, but delegate to a different Action, as shown in the code above. If the Properties window or Output window were to be selected, there'd be no "cut-to-clipboard" key in the ActionMap of the TopComponent and hence the CutAction would not be enabled.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today