import cern.stopmi.OPGeneric.OPSkel;
import java.util.Vector;
import java.beans.*;
import cern.stopmi.beans.*;
import java.awt.*;
import java.awt.event.*;
class Motor extends Thread{
transient private Vector listeners = new Vector();
private Integer motorPosition = new Integer(0);
public Motor(){
this.start();
}
public void run(){
while(true){
try{sleep(1000);}catch(InterruptedException ie){}
Integer newValue = new Integer(motorPosition.intValue()+1);
firePropertyChange("MOTOR_MOVED",motorPosition,newValue);
motorPosition = newValue;
}
}
public void reset(){
motorPosition = new Integer(0);
}
public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
listeners.addElement(listener);
}
public void firePropertyChange(String propertyName,
Object oldValue, Object newValue) {
synchronized (this) {
Vector targs = (Vector) listeners.clone();
PropertyChangeEvent evt =
new PropertyChangeEvent(this, propertyName, oldValue, newValue);
for (int i = 0; i < targs.size(); i++) {
PropertyChangeListener t = (PropertyChangeListener)targs.elementAt(i);
t.propertyChange(evt);
}
}
}
}
class MyPanel extends OPPanel{
public OPTextField text = new OPTextField();
public OPButton button = new OPButton();
public MyPanel(){
button.setText("Click to reset");
this.setLayout(new BorderLayout());
add("Center",button);
add("South",text);
}
}
class MyController extends cern.stopmi.OPGeneric.OPController
implements PropertyChangeListener, ActionListener{
public void propertyChange(PropertyChangeEvent evt){
if(evt.getSource().getClass() == Motor.class){
if(evt.getPropertyName().compareTo("MOTOR_MOVED")==0){
MyPanel pan = (MyPanel)(this.getOPPanel(MyPanel.class));
pan.text.setText("Current motor position :: "+evt.getNewValue());
}
}
}
public void actionPerformed(ActionEvent evt){
OPPanel caller = this.getOPPanel(evt.getSource());
if(caller instanceof MyPanel){
MyPanel pan = (MyPanel)caller;
if(evt.getSource() == pan.button){
Motor motor = (Motor)(this.getObject(Motor.class));
motor.reset();
}
}
}
}
public class Main extends OPSkel {
public Main() {
MyPanel pan = new MyPanel();
MyController contr = new MyController();
Motor motor = new Motor();
this.getContentPane().add("Center",pan);
this.validate();
contr.addListenersNeeded(pan);
contr.addObjectReference(motor);
motor.addPropertyChangeListener(contr);
}
public static void main(String[] args) {
Main main1 = new Main();
}
}