Reputation: 1634
I was just wondering if there are any callback methods or if we can create some call backs for action listener in java (lets say: beforeAction(), afterAction() etc...), just like we have some of the call backs in Javascript, like:
beforeFilter(), afterFilter() etc...
I've Googled the topic, but didn't found out what exactly I'm looking for. I've also thought of something like:
public void actionPerformed(ActionEvent e) {
beforeAction();
actuallAction();
afterAction();
}
Shall we consider those methods as callbacks???
Any Help would be appreciated. Thanks.
Edit: Yeah I'm asking about java swing.
Upvotes: 0
Views: 532
Reputation: 24444
First of all, the need for ordered listener execution might be an indicator for a design problem/mistake, as usualy listeners should not have any causal dependencies. So review your design first.
If you still insist on staged listeners, all you need is some kind of 'staged' action listener adapter that delegates ActionEvent
s to three different groups of listeners (the 'stages'):
public class StagedActionEventSource implements ActionListener {
public static StagedActionEventSource forButton(Button button) {
StagedActionEventSource l = find(button.getActionListeners());
if (l == null) {
l = new StagedActionEventSource();
button.addActionListener(l);
}
return l;
}
public static StagedActionEventSource forList(List list) {
StagedActionEventSource l = find(list.getActionListeners());
if (l == null) {
l = new StagedActionEventSource();
list.addActionListener(l);
}
return l;
}
// ... add more widget-based implementations here ...
private static StagedActionEventSource find(ActionListener[] listeners) {
for (ActionListener l : listeners) {
if (l instanceof StagedActionEventSource)
return (StagedActionEventSource) l;
}
return null;
}
private ActionListener beforeActionRoot;
private ActionListener onActionRoot;
private ActionListener afterActionRoot;
public void addBeforeActionListener(ActionListener l) {
beforeActionRoot = AWTEventMulticaster.add(beforeActionRoot, l);
}
public void removeBeforeActionListener(ActionListener l) {
beforeActionRoot = AWTEventMulticaster.remove(beforeActionRoot, l);
}
public void addActionListener(ActionListener l) {
onActionRoot = AWTEventMulticaster.add(onActionRoot, l);
}
public void removeActionListener(ActionListener l) {
onActionRoot = AWTEventMulticaster.remove(onActionRoot, l);
}
public void addAfterActionListener(ActionListener l) {
afterActionRoot = AWTEventMulticaster.add(afterActionRoot, l);
}
public void removeAfterActionListener(ActionListener l) {
afterActionRoot = AWTEventMulticaster.remove(afterActionRoot, l);
}
@Override
public void actionPerformed(ActionEvent e) {
fireEvent(beforeActionRoot, e);
fireEvent(onActionRoot, e);
fireEvent(afterActionRoot, e);
}
private void fireEvent(ActionListener root, ActionEvent e) {
if (root != null) {
root.actionPerformed(e);
}
}
}
Note that this concept only works if all your listeners are registered via this adapter. Of course it is still possible to directly register a listener at the component, but this listener will then not take part in the staged event dispatching - instead it will be called before or after the listeners registered at the StagedEventSource
.
Usage:
Button button = new Button(...);
StagedActionEventSource.forButton(button)
.addBeforeActionListener(new MyActionListener());
StagedActionEventSource.forButton(button)
.addActionListener(new MyActionListener());
Upvotes: 1
Reputation: 1571
I dont know if Java has this kind of feature (rather not), but you can always chech on Aspect Oriented Programing and it's implementations in Java like Spring-AOP .
Upvotes: 0