X

Geertjan's Blog

  • December 25, 2013

Region-Specific CutAction

Geertjan Wielenga
Product Manager

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.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.