开发者

How to invoke Actions from other Actions? (if that's wise at all)

开发者 https://www.devze.com 2022-12-19 00:48 出处:网络
I am about to refactor a Swing application from using ActionListeners to Action classes because I realized that a lot of my menu items are going to be used in a toolbar as well.

I am about to refactor a Swing application from using ActionListeners to Action classes because I realized that a lot of my menu items are going to be used in a toolbar as well.

Right now I have a class called ImportExport which deals with the state of the underlying model and then displays the appropriate user dialogs. ImportExport has the functions save(), saveAs() and open(). When a user clicks on the "Open" menu item, the action listener calls open(), open() first checks if the model has changes and if that's the case displays a dialog asking the user if he wants to save first. Now if the user clicks "Yes" open() calls save() which again performs some checks and displays user dialogs. save() is pretty persistent: The only way to get out of this action is either by saving successfully or the user deciding that he wants to cancel. I strongly rely on the feedback that save() provides and if a user wants to cancel I also cancel the calling open() function.

I wanted to split up the ImportExport class into three classes (OpenAction, SaveAction & SaveAsAction), each of them subclassing AbstractAction and eventually get rid of the ImportExport class. And this is where my problem arises: How do I tell the SaveAction to execute if the user wants to save the model before openi开发者_如何学JAVAng another one? And how do I get feedback if the user decides to cancel?

Is this even the right approach? I don't like to have duplicated code in the save and open action and I have already put as much functionality as possible into my underlying model but user dialogs are obiously out of place there so this is no option. Was Action designed at all to hold this kind of functionality or sould I just keep my ImportExport class and simply delegate all action calls to the appropriate functions in ImportExport. How do you use Actions?


I sometimes add a 'controller' action when there is a decision tree involved. OpenAction, SaveAction and SaveAsAction would subclass this controller Action. The controller action would determine the state of the model and request more info from the user if appropriate and then call the correct subclass.


I would create an ImportExportController class, having these methods : open, save, saveAs. This controller would know the business logic.

My Action classes would not contain a lot of business logic. They would only call the controller.

But these Actions would contain all the labels, icons, accelerator... that must be displayed in JButton, JMenuItem, etc.

For me, Actions are GUI oriented classes, used for convenience (and they are very convenient!), but no classes dedicated to handle business logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消