PDA

View Full Version : Action support - like in Swing



troyg
10 Nov 2011, 7:15 PM
Recently I've had to do some swing development.
After having to develop in swing for a short while, I'm beginning to really appreciate the effort the GXT team has taken with their APIs, especially with what I've seen so far in GXT3.

Anyway, enough of the brown-nosing... something I do like from Swing is the Action stuff - http://download.oracle.com/javase/tutorial/uiswing/misc/action.html

Basically I define an action - say a delete action. As part of the action, I create the code that actually does the delete. But I can also define things like a label, mnemonic key, icon etc.

So then I can create a button, and also a menu item that simply take the same instance of my delete action. They don't need to be explicitly configured in terms of labels, listeners etc. The action also holds some state. So for example, I add a selection change listener to my JTable (similar to GXT grid), and disable the action when nothing is selected, the button, and the menu item are automatically disabled. Quite a nice feature.

Is there something like this already in your API, or as part of GWT that I'm ignorant of?
If not, I know you guys are working hard at getting GXT3 out the door, but perhaps this would be a nice enhancement, that can complement your existing ways of creating buttons and menu items etc.

Cheers
Troy

chalu
11 Nov 2011, 1:03 AM
Something like this :


// define the actions, put them in a place for easy management
actionsMap = new HashMap<String, Action>();
actionsMap.put(Perms.Refresh_Loan.encode(), new Action("Refresh", Resources.ICONS.refresh(), new ToolTipConfig("Refresh View")) {
@Override
public void actionPerformed(ActionEvent evt) {
refresh(true);
}
});
actionsMap.put(Perms.Add_Loan.encode(), new Action("Add", Resources.ICONS.addrecord(), new ToolTipConfig("Create Loan")) {
@Override
public void actionPerformed(ActionEvent evt) {
addModel();
}
});
actionsMap.put(Perms.Save_Loan.encode(), new Action("Save", Resources.ICONS.disk(), new ToolTipConfig("Save Changes")) {
@Override
public void actionPerformed(ActionEvent evt) {
saveModel();
}
});

// we can now start using them in a menu-bar, and tool-bar e.t.c
refreshBtn = new Button(actionsMap.get(Perms.Refresh_Loan.encode()));
topBar.add(refreshBtn);
topBar.add(new SeparatorToolItem());
ctxMenu.add(actionsMap.get(Perms.Refresh_Loan.encode()));
ctxMenu.add(new SeparatorMenuItem());

addBtn = new Button(actionsMap.get(Perms.Add_Loan.encode()));
topBar.add(addBtn);
ctxMenu.add(actionsMap.get(Perms.Add_Loan.encode()));

29227

I had to do some custom plumbing to build this feature.

troyg
13 Nov 2011, 12:50 PM
Yeah that sort of thing...but going one step further, where if you make changes to the action instance after adding it to various controls, it takes an immediate effect.

E.g. If you disable the action instance, it'll immediately gray out any visible controls etc.