home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JRUNTIME.Z
/
NewEventBean.snippet
< prev
next >
Wrap
Text File
|
1998-05-08
|
3KB
|
81 lines
// This snippet takes advantage of JBuilder's ability to define
// multiple public classes in a single source file. This is not
// recommended and is only used here as an example.
// Each class should be pasted into its own .java file.
// You will see warnings to remind you when you compile.
// Snippet Note: Use the parameter button to change the name of the event class.
//<Target text="Name for new event class" pattern=%CustomEvent% default="CustomEvent">
//<Package>
import java.util.*;
// This defines a new event, with minimum state.
public class %CustomEvent% extends EventObject {
static final int EVENT1=1;
static final int EVENT2=2;
static final int EVENT3=3;
private int id=0;
public int getID() {return id;};
%CustomEvent%(Object source,int i) {
super(source);
id=i;
}
}
// This defines a listener interface for the set of events that
// are generated by %CustomEvent%
public interface %CustomEvent%Listener extends EventListener {
public void event1(%CustomEvent% e);
public void event2(%CustomEvent% e);
public void event3(%CustomEvent% e);
}
// This is an example of a non-visual Bean that fires the new events
public class %CustomEvent%ExampleBean {
public %CustomEvent%ExampleBean() {
}
// The add/remove methods provide the signature for the IDE to recognize
// these events and show them in the event list
public synchronized void add%CustomEvent%Listener(%CustomEvent%Listener l) {
listenerList.addElement(l);
}
public synchronized void remove%CustomEvent%Listener(%CustomEvent%Listener l){
listenerList.removeElement(l);
}
// A single process method keeps all event dispatching in one place.
// Separate processEVENT1, processEVENT2, etc methods could also be used.
protected void process%CustomEvent%(%CustomEvent% e) {
switch (e.getID()) {
case %CustomEvent%.EVENT1:
for (int i=0; i<listenerList.size(); i++)
//Send event to all registered listeners
((%CustomEvent%Listener)listenerList.elementAt(i)).event1(e);
break;
case %CustomEvent%.EVENT2:
for (int i=0; i<listenerList.size(); i++)
((%CustomEvent%Listener)listenerList.elementAt(i)).event2(e);
break;
case %CustomEvent%.EVENT3:
for (int i=0; i<listenerList.size(); i++)
((%CustomEvent%Listener)listenerList.elementAt(i)).event3(e);
break;
}
}
// A test method to fire all 3 example events
public void test%CustomEvent% () {
process%CustomEvent% (new %CustomEvent% (this, %CustomEvent%.EVENT1));
process%CustomEvent% (new %CustomEvent% (this, %CustomEvent%.EVENT2));
process%CustomEvent% (new %CustomEvent% (this, %CustomEvent%.EVENT3));
}
private Vector listenerList = new Vector();
}