> Now, since the PostJournalToGL action class is set up as conditional

What do you mean by "conditional"? I assume you mean context-sensitive?

> shouldn't it enable the menu item whenever there is a change to the data in 
> the FuelPurchaseManager class?

Not whenever there's a change to the data _in_ the FuelPurchasingManager class, 
but when a FuelPurchasingManager instance appears in the lookup of the 
currently selected Node or similar.

It's a little odd to have an action be context-sensitive with respect to a 
singleton; it could just as well be written as a global action that always 
works. Usually actions are sensitive wrt. some object that can change based on 
the user's Node selection.

-- Eirik

-----Original Message-----
From: Sean Carrick <[email protected]> 
Sent: Monday, March 30, 2020 10:23 PM
To: [email protected]
Subject: Auto Enabling/Disabling of Menu Items

Hello, Dev List!

I am somewhat new at designing RCPs on the NetBeans Platform and have run into 
a small issue. If anyone can point me in the right direction, I will be very 
grateful.

I am working on a singleton class, FuelPurchaseManager, which stores the data 
for the Proof of Concept for my project for making fuel purchases. I have 
created a menu, Maintenance/Post Journals to Ledger. On this menu, I have 
created the following Action:

package com.northwind.fuel.controller;
import com.northwind.fuel.model.FuelPurchaseManager;import
java.awt.event.ActionEvent;import java.awt.event.ActionListener;import
org.openide.loaders.DataObject;import org.openide.awt.ActionID;import 
org.openide.awt.ActionReference;import
org.openide.awt.ActionRegistration;import
org.openide.util.NbBundle.Messages;

@ActionID(
        category = "Maintenance",
        id = "com.northwind.fuel.controller.PostJournalToGL"
)@ActionRegistration(
        iconBase = "com/northwind/fuel/postToGL.png",
        displayName = "#CTL_PostJournalToGL"
)@ActionReference(path = "Menu/Maintenance/Journals", position = 
3233)@Messages("CTL_PostJournalToGL=Post Fuel Journal")public final class 
PostJournalToGL implements ActionListener {

    private final FuelPurchaseManager context;

    public PostJournalToGL(FuelPurchaseManager context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        FuelPurchaseManager.getInstance().actionPerformed(ev);
    }
}


I have set up the FuelPurchaseManager singleton as follows:

public class FuelPurchaseManager extends AbstractAction
        implements LookupListener, ContextAwareAction {
    public static final long serialVersionUID = 6597346658963L;

    private static FuelPurchaseManager mgr = null;
    private Lookup context;
    Lookup.Result<FuelPurchaseManager> lkpInfo;

    private final ArrayList<FuelPurchase> journal;

    private FuelPurchaseManager() {
        this.journal = new ArrayList<>();

        /* *********************************************************************
          * DEBUGGING CODE: The following code, until the "END
DEBUGGING"     *            * comment is for the Proof of Concept
only. It will need to be      *           * updated to ***actual***
business logic before we create a release *           * build.
                                                   * 
********************************************************************/
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd, yyyy");
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 14, 2020", fmt), // Tx date
                540312, // Odometer
                102.890, // # of gallons
                2.765,  // Price per gallon
                284.49)); // Total price
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 17, 2020", fmt),
                541990,
                134.880,
                2.493,
                336.26));
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 20, 2020", fmt),
                542766,
                151.460,
                2.420,
                366.46));
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 21, 2020", fmt),
                543088,
                92.730,
                2.306,
                213.80));
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 23, 2020", fmt),
                545317,
                161.060,
                2.620,
                421.99));
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 25, 2020", fmt),
                546525,
                127.360,
                2.706,
                344.66));
        this.journal.add(new FuelPurchase(
                LocalDate.parse("Jan 29, 2020", fmt),
                547184,
                168.880,
                2.315,
                390.87));
        /*  ********************************************************************
           * END OF DEBUGGING CODE: Remove the above before release
build.    *
*******************************************************************/
    }

    private FuelPurchaseManager(Lookup context) {
        this();

        putValue(Action.NAME, NbBundle.getMessage(FuelPurchaseManager.class,
                "LBL_Action"));
        this.context = context;
    }

    void init() {
        assert SwingUtilities.isEventDispatchThread()
                : "this shall be called just from AWT thread";

        if ( lkpInfo != null ) {
            return;
        }
    }

    public static FuelPurchaseManager getInstance() {
        if ( mgr == null )
            mgr = new FuelPurchaseManager();

        return mgr;
    }

    @Override
    public boolean isEnabled() {
        init();
        return super.isEnabled();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        init();


        throw new UnsupportedOperationException("`FuelPurchaseManager."
                + "actionPerformed(ActionEvent e)` Not supported yet.");
    }

    @Override
    public void resultChanged(LookupEvent le) {
        setEnabled(!lkpInfo.allInstances().isEmpty());
    }

    @Override
    public Action createContextAwareInstance(Lookup lkp) {
        return FuelPurchaseManager.getInstance();
    }

    /* Many other data storage, retrieval, and manipulation methods below here 
*/ }

Now, since the PostJournalToGL action class is set up as conditional, shouldn't 
it enable the menu item whenever there is a change to the data in the 
FuelPurchaseManager class?

I have Googled this using every possible set of keywords that I am able to 
think up, but to no avail. I cannot seem to find anything that points me in the 
correct direction for making this work. Everything that I have found is dealing 
with visual elements in the application, mostly using Nodes to conditionally 
enable menu items and toolbar buttons.

If you could possibly point me in the correct direction for solving this, I 
will be so grateful! However, if this is not possible, I can go with my only 
hope (which I hoped would just be temporary), which is creating a visual 
journal window for each module that contains a journal of financial 
transactions that will need to be posted to the General Ledger.

Thanks for any help that you are able to provide with this issue.

Sincerely,

Sean Carrick
Owner - PekinSOFT Systems
[email protected]
(309) 989-0672

Reply via email to