Previous article review:
1 Plugin Learning Chapter
2 easy to build plug-in engineering and model file analysis
3 using extension points to develop perspective view
4 SWT Programming Notes
5 SWT simple control using and layout collocation
6 SWT complex space and layout collocation
7 SWT Layout Detailed
8 Ipreferencestore use of the detailed
9 Editor Code coloring
Ten JFace Development
Event monitoring is an important part of plug-in development, each time the click or the key can trigger some kind of event response, then how to achieve it?
For some type of listening model, it is usually necessary to add a listening queue .
Listeners need to be added to this listening queue in some way.
When the model triggers a listener event in a specific situation, it generates an event response that causes each listener in the listener queue to trigger the action of the response .
For example, here's a small example:
classfocusedcountry{List<IListener> listener =NewArrayList (); Public voidAddListener (Ilistener lis) {listener.add (LIS); } //Remove Listener Public voidRemoveListener (Ilistener lis) {listener.remove (LIS); } //Triggering listener Events protected voidfirechange (String message) { for(Ilistener lis:listener) {lis.noticedchange (message); } }}
The listener has a listening queue, and anyone interested in it will be added to the listener queue. So there are basically three functions, which are added to the queue, left from the queue, and a trigger function of itself.
Interface ilistener{ publicvoid noticedchange (String message); class Developedcountry implements ilistener{ publicvoid Noticedchange (String Message) { System. out. println ("noticed the change:"+message);} }
The above implementation of a listening interface, as long as the implementation of the interface of the class, can be added to the queue.
Public classListentest { Public Static voidMain (string[] args) {developedcountry America=NewDevelopedcountry (); Focusedcountry China=NewFocusedcountry (); Focusedcountry Northkorea=NewFocusedcountry (); China.addlistener (America); Northkorea.addlistener (America); China.firechange ("To the Moon! "); Northkorea.firechange ("The atom bomb was made, where to throw it! "); }}
The result of the call is as follows, all events are received by the listener.
Noticed the change: Moon landing! Noticed the change: The atom bomb was made, where to throw it!
So how is it used in the GEF?
GEF is an MVC-standard architecture whose model is responsible for implementing this listening queue, and control is responsible for receiving and responding to changes to the view's model .
Therefore, the generic model inherits a custom virtual class that contains a listener queue and the three functions mentioned above.
Public classAbstractmodel implements serializable{PrivatePropertychangesupport listeners =NewPropertychangesupport ( This); Public voidAddpropertychangelistener (PropertyChangeListener listener) {Listeners.addpropertychangelistener (listener);} Public voidFirepropertychange (String propname, Object oldvalue,object newvalue) {Listeners.firepropertychange (PropName, OldValue, NewValue); } Public voidRemovepropertychangelistener (PropertyChangeListener listener) {Listeners.removepropertychangelistener ( Listener); }}
After inheriting this class, some events are required to trigger the listener, in general, the model will correspond to some attribute views, and the attribute view needs to inherit the Ipropertysource interface. and override the following method.
Publicipropertydescriptor[] Getpropertydescriptors () {return Newipropertydescriptor[] {NewPropertyDescriptor (P_table_name,"table_name"), } PublicObject GetPropertyValue (object ID) {if(id = =p_table_name) { returnGetphysicalname (); } return NULL; } PublicBoolean ispropertyset (Object ID) {if(id = =p_table_name) { return true; } return false; } Public voidsetPropertyValue (Object ID, object value) {if(id = =p_table_name) {seName (String) value); } }
When a property on a property view changes, the Firechange is usually triggered at the set value, and then the Firepropertychange function inside the listners is triggered.
Public void setxxxlname (String xxxname) { this. xxxname = xxxname; NULL , Xxxname); }
This is a legacy of plugin development, which is to mark each event with a static string. The function generates a PropertyChange event.
So the model part of the monitoring is done, the following is to do is the listener added.
Here the listener needs to implement the PropertyChangeListener interface and add it to the listening queue at the right time, as this part of the code in Editpart,each editpart of the GEF corresponds to a model, Therefore, it is OK to get the corresponding model object by the simple Getmodel method, then call the AddListener of the model object and add it to the listening queue .
public void Activate () { if (IsActive ()) { ; } super.activate (); ((TableModel) Getmodel ()). Addpropertychangelistener ( this ); public void Deactive () { if (! IsActive ()) { return ; } super.deactivate (); ((TableModel) Getmodel ()). Removepropertychangelistener ( this ); }
In general, these are both functions, because these functions are equivalent to the constructors of the general function and the execution position of the destructor.
After adding the listening queue, you need to implement the PropertyChange method inside the PropertyChangeListener, this method passes a parameter, which can be used to get the first set of strings above, so as to determine which time the model has responded.
Public void PropertyChange (propertychangeevent evt) { if (Evt.getpropertyname (). Equals (TABLEMODEL.P _table_name)) refreshvisuals (); ... }
"Plug-in development"--11 eavesdropping (Java event Monitoring principle-GEF example explained)