Re: Auto Enabling/Disabling of Menu Items
Jaroslav, Thank you for your input. I checked out your slideshow and I believe that this is going to help me a great deal! I guess my problem is that this is my first foray into designing on the NetBeans Platform, so some old habits are dying rather hard. I will get this though, because I have loved NetBeans since somewhere around version 3 and am bound and determined to use the Platform for my application. Sincerely, Sean Carrick Owner - PekinSOFT Systems s...@pekinsoft.com (309) 989-0672 On Sat, Apr 4, 2020 at 12:51 AM Jaroslav Tulach wrote: > út 31. 3. 2020 v 4:22 odesílatel Sean Carrick napsal: > > > 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); > > > > Wrong. Don't use singleton. Use `context`. > > > > } > > } > > > > > > I have set up the FuelPurchaseManager singleton as follows: > > > > public class FuelPurchaseManager extends AbstractAction > > implements LookupListener, ContextAwareAction { > > > > Remove `extends AbstractAction implements LookupListener, > ContextAwareAction` keep just the business logic. > > Then just make sure a TopComponent with instance of `FuelPurchaceManager` > in its lookup is opened - on startup - on some other action - etc.: > ``` > TopComponent tc = new TopComponent() { > { > associateLookup(Lookups.singleton(new > FuelPurchaceManager())); > } > }; > tc.open(); > tc.requestActive(); > ``` > > Your `PostJournalToGL` action is going to be enabled when your TopComponent > is focused and disabled otherwise. > -jt > > PS: > > https://www.slideshare.net/guestb9a4bf/mvcdci-in-netbeans-by-jaroslav-tulach > PPS: http://wiki.apidesign.org/wiki/DCI > > > > > public static final long serialVersionUID = 6597346658963L; > > > > private static FuelPurchaseManager mgr = null; > > private Lookup context; > > Lookup.Result lkpInfo; > > > > private final ArrayList 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, > > "); > > 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.par
Re: Auto Enabling/Disabling of Menu Items
út 31. 3. 2020 v 4:22 odesílatel Sean Carrick napsal: > 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); > Wrong. Don't use singleton. Use `context`. > } > } > > > I have set up the FuelPurchaseManager singleton as follows: > > public class FuelPurchaseManager extends AbstractAction > implements LookupListener, ContextAwareAction { > Remove `extends AbstractAction implements LookupListener, ContextAwareAction` keep just the business logic. Then just make sure a TopComponent with instance of `FuelPurchaceManager` in its lookup is opened - on startup - on some other action - etc.: ``` TopComponent tc = new TopComponent() { { associateLookup(Lookups.singleton(new FuelPurchaceManager())); } }; tc.open(); tc.requestActive(); ``` Your `PostJournalToGL` action is going to be enabled when your TopComponent is focused and disabled otherwise. -jt PS: https://www.slideshare.net/guestb9a4bf/mvcdci-in-netbeans-by-jaroslav-tulach PPS: http://wiki.apidesign.org/wiki/DCI > public static final long serialVersionUID = 6597346658963L; > > private static FuelPurchaseManager mgr = null; > private Lookup context; > Lookup.Result lkpInfo; > > private final ArrayList 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, > "); > 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)); > /* > ***
Re: Auto Enabling/Disabling of Menu Items
Eirik, I appreciate your response to my question. Thank you for letting me know that the class has to be tied to a Node. Now I know which way I have to go to make it work the way I am wanting. Just so you better understand why I have the menu item conditional (as stated in the Action Wizard), or context-sensitive, it is a menu for posting journal entries to the General Ledger in an accounting system. There is no need for the menu to be active if there are no journal entries to post. I *could* have it be globally available and just pop a message that tells the user there are no entries to post, if none have been added since the last post, but I thought that only have the ability to post available when there are journal entries would be better. Again, thank you very much for giving me direction. Blessings! Sincerely, Sean Carrick Owner - PekinSOFT Systems s...@pekinsoft.com (309) 989-0672 On Tue, Mar 31, 2020 at 8:31 AM Eirik Bakke wrote: > > 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 > Sent: Monday, March 30, 2020 10:23 PM > To: dev@netbeans.apache.org > 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 lkpInfo; > > private final ArrayList 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, > "); > this.journal.add(new FuelPurchase( > LocalDate.parse("
RE: Auto Enabling/Disabling of Menu Items
> 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 Sent: Monday, March 30, 2020 10:23 PM To: dev@netbeans.apache.org 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 lkpInfo; private final ArrayList 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, "); 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,
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 lkpInfo; private final ArrayList 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, "); 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("`FuelPurchaseManage