• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Separate Actions and GUI

 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I've been battling with this problem for a while now, and I'm wondering what the people here usually do.

I have a GUI, and I want to separate the Actions from it. I'm not quite sure how to go about it.
The actions will be added to a JMenuBar, and some of them to a JToolBar as well. Performing some of these actions may disable/enable other actions, so they need to be accessible in some way. Also, a user might customize which actions appear in the toolbar.

Let's say my GUI (MainForm) is in the package infinity.view. What I'm currently thinking about is accessing my actions through a static factory (MainFormActionFactory) in infinity.view.event. The factory will create/return singleton instances of these actions using either enum constants or static final Strings or something as an argument.
Amongst others, one of the things that troubles me about this setup is that MainFormActionFactory will have to be public, as well as its factory method. Arguably, this is exactly what you want, in a setup that separates the controller from the view.
Also, should I make a factory like this for every form/dialog that I make?

I would like to know your opinions on the matter, or similar situations.
 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also just bumped into an interesting looking class named ActionMap. What is the purpose of this class exactly? Do you propose I use it instead of the factory?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am trying to understand your requirement, correct me if its wrong:
So you have this- GUI and you need to set the Action (Event handlers?) for the GUI Components. But you dont want them to be within the GUI code?

If what I have understood is correct- You might have EventHandler classes for each Action- Say for action- "Save" you can have SaveListener which can implement required AWT Event Listeners.
 
Maneesh Godbole
Saloon Keeper
Posts: 10976
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually, to share a common "action" one would extends AbstractAction and share the instance between all action sources.
 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed, I'm not sure if I can use EventHandler to create instances of Action? I would like them to include icons as well.

Maneesh, this is essentially what I'm doing, but I'm just wondering what the best way is of sharing these instances.
I don't have a problem getting it to work. I'm just trying to organize it all in such a way that I separate my view and my controller effectively, and that my GUI class doesn't become too cluttered.

Let's say my GUI looks roughly like this:

Now, it's not too bad for two actions. But I'm going to add a lot more. How would you normally do it?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah sorry, I didnt know that Java had an EventHandler class as well. I meant- "Event Handler" (2 words) Something like- a Class implementing ActionListener.

You can always have a package for event handlers. As you said- You can have a factory for returning Singleton for each action- Say SaveAction, OpenAction and so on.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I also just bumped into an interesting looking class named ActionMap. What is the purpose of this class exactly? Do you propose I use it instead of the factory?

I believe ActionMap is originally meant to be used with InputMap. When a user presses one or more keys, the matching key stroke is looked up in the InputMap. This will retrieve a value. This value is then used as the key of the ActionMap to find the matching Action. This is then executed.

Of course nothing prevents you from adding your own entries in the ActionMap, just be careful on what to use as keys. You don't want to overwrite any existing Actions or the behaviour of the component will change. Of course, if that's what you want that's not a problem.
 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, this shouldn't be a problem if I just create a new ActionMap, right?

Thanks all. Now I'm curious what everyone usually does. Do you personally use internal classes in your form, separate classes, something else?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: ...Now I'm curious what everyone usually does. Do you personally use internal classes in your form, separate classes, something else?


I mean separating would mean that lot of other parts of the code can reuse it. Also if we separate them then it can also create unnecessary noise in the code- If they are not used by other parts of the code. As long as all my GUI components are at one place- the action classes need not move out- they might as well reside along with the GUI code.

If at all I was using Netbeans or something else for building the GUI and then adding listeners- I wouldn't think much about how the code is being generated behind the scenes.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Rob, this shouldn't be a problem if I just create a new ActionMap, right?

Nope. You can even give it a parent (e.g. an existing component's ActionMap) as fall-back.
 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mohamed. I have a good fresh perspective on the matter.

Rob, I ended up using an EnumMap of Actions. I'm surprised the designers didn't just use some standard Map themselves, but it might have to do with the fact that ActionMap was around before Generics.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic