java.beans.PropertyChangeSupport pee.
Hello everyone. Finally learned a period of RCP, but for the exam, a few weeks did not write code, learn to forget almost. Take advantage of the summer vacation time, learn a good lesson. I have long heard of GEF's name, it seems that I can not exception, but also to start GEF up. Do not know is not before the foundation did not learn, the first question is about the Propertychangesupport support class.
Briefly, Java.beans.PropertyChangeSupport is a utility class that supports the bean that binds the property to use the class. Still not much nonsense, with the code to speak.
Import Java.beans.PropertyChangeListener;
Import Java.beans.PropertyChangeSupport;
public class Propertychangesupporttest {
Transient protected Propertychangesupport listeners = new Propertychangesupport (this);
public void Addpropertychangelistener (PropertyChangeListener listener) {
Listeners.addpropertychangelistener (listener);
}
protected void Firepropertychange (String prop, Object old, object NewValue) {
Listeners.firepropertychange (prop, old, newvalue);
}
public void Removepropertychangelistener (PropertyChangeListener l) {
Listeners.removepropertychangelistener (l);
}
}
You can see how the Propertychangesupport is constructed by binding the class itself to that property so that his subclasses can also use the tool class. Mainly look at his 2nd method, triggering a property change event (Prop is the property name of the binding, which can be obtained from the event handler in subsequent event handling)
And look at the subclass of it:
public class Beantest extends Propertychangesupporttest {
public static final String test = "Test";
Private String name = "";
public void SetName (String name) {
if (this. name.equals (name)) {
System.out.println ("Beantest's Name property does not change.) " );
return;
}
this. Name = name;
Firepropertychange (TEST, NULL, name);
}
Public String GetName () {
return this. Name;
}
The Beantest class has a Name property that triggers the Propertychangeevent event when the value of name is changed. Let's take a look at event monitoring for extreme processing:
Import java.beans.PropertyChangeEvent;
Import Java.beans.PropertyChangeListener;
public class Beantestlistener implements propertychangelistener{
public void PropertyChange (Propertychangeevent evt) {
if (Evt.getpropertyname (). Equals (Beantest.test))
System.out.println ("Beantest's Name property changes.) " );
}
public static void Main (string[] args) {
Beantest test = new Beantest ();
Test.addpropertychangelistener (New Beantestlistener ());
Test.setname ("Fuxueliang");
Test.setname ("Fuxueliang");
System.out.println (Test.getname ());
}
You can see that the event source is determined by evt.getpropertyname (). Equals (Beantest.test). The results of the operation are as follows:
The Name property of the beantest changes.
The Beantest Name property does not change.
Fuxueliang
/**
* When the setname operation is performed, the Firepropertychange event is triggered.
* Because Propertychangesupporttest and propertychangesupport bind
* So in Beantestlistener, because the listener in the support class is added, the PropertyChange event should be triggered.
* and will be based on Evt.getpropertyname () to get the name of the property you are binding to trigger the event in Beantest
*/
This is only an analysis of the Propertychangesupport method, the rest of the method can be discussed together.
As for the listener mechanism, you can refer to Chen's "eclipse from Getting Started to mastery", which has a login monitor implementation.
The above is only a point of my rough view, there are wrong place also please correct me. Because this is my first time to write this article, in the expression of several wording may be deficient in the post in the future must try to correct, so please forgive. * _ *