Hide/show a Save button in Netbeans Platform 7

When we incorporate NetBEANS Platform 7 into our application, we usually employ Action Annotations rather than layer.xml file for registering actions into a system.

For example, the code below is an implementation of closing a TopComponent. (Notes: we create a PaymentEditorTopComponent.class in org/editor/payment package, and CloseWindowAction.class in the org/action/payment package.)


@ActionID(category = "MyPaymentEditorToolbars_WindowActions",
id = "org.action.payment.CloseWindowAction")
@ActionRegistration(iconBase = "org/action/payment/cross.png",
displayName = "#CTL_CloseWindowAction")
@ActionReferences({
@ActionReference(path = "Toolbars/MyPaymentEditorToolbars_WindowActions", position = 300)
})
@Messages("CTL_CloseWindowAction=Close Window")
public final class CloseWindowAction implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
// TODO implement action body
PaymentEditorTopComponent tc = (PaymentEditorTopComponent)WindowManager.getDefault().findTopComponent("PaymentEditorTopComponent");
if (tc.canClose()) {
tc.close();
}
}
}

Then, we add the following code snippets into componentActivated() and componentDeactivated() of the PaymentEditorTopComponent class.


@Override
protected void componentActivated() {
Toolbar toolbar = ToolbarPool.getDefault().findToolbar("MyPaymentEditorToolbars_WindowActions");
if (!toolbar.isVisible()) {
toolbar.setVisible(true);
}
}

@Override
protected void componentDeactivated() {
Toolbar toolbar = ToolbarPool.getDefault().findToolbar("MyPaymentEditorToolbars_WindowActions");
if (toolbar.isVisible()) {
toolbar.setVisible(false);
}
}

By the above means, the Close Window toolbar will be displayed when the PaymentEditorTopComponent is active, and hidden when the PaymentEditorTopComponent is not active.

There is catch. From my understanding, to be able to have Action Annotation as above, the action class must implement ActionListener.

However, the Save button implements SaveCookie, not ActionListener. So, what could we do to hide and show the Save button?

What I have done is to go back to the layer.xml approach.

  1. I create a class file named MySavePaymentCookieImpl which implements SaveCookie as usual.
  2. I have InstanceContent, and fire(boolean){} as shown in the CRUD tutorial
  3. I add the following code into layer.xml

  4. <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
    <filesystem>
    <folder name="Toolbars">
    <folder name="MyPaymentEditorToolbars_WindowActions">
    <file name="org-openide-actions-SaveAction.shadow">
    <attr name="originalFile" stringvalue="Actions/System/org-openide-actions-SaveAction.instance"/>
    <attr name="position" intvalue="300"/>
    </file>

    <file name="org-openide-actions-SaveAllAction.shadow_hidden"/>
    </folder>
    </folder>
    </filesystem>

  5. Now, the key step is to inform the system to use layer.xml file by adding the following line into Module Manifest file

    OpenIDE-Module-Layer: org/editor/payment/layer.xml
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment