During the course of learning eclipse RCP, I had been wondering how to display the modified data to different views. Later I checked the data online for a long time and learned the observer's design pattern, apply it to this aspect and find that the effect is ideal and elegant.
1. What is the observer Mode 1. Concept
The observer mode defines one-to-many dependencies between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated. The implementation of the observer design pattern provided by JDK is composed of Java. util. observable class and Java. util. Observer interface. From the name, we can clearly see the roles played by the two in the Observer Design Mode: the observer is the observer role, and the observable is the subject role.
2. Use Time
1. When an object needs to change other objects at the same time, and it does not know how many objects need to be changed, the observer mode should be considered;
2. An abstract model has two aspects, one of which depends on the other. In this case, the observer mode can be used to close the two aspects in an independent object so that they can be changed and reused independently;
3. The work done by the observer mode is actually decoupling. Let the two sides of coupling depend on abstraction, rather than on specifics, so that their changes will not affect the changes on the other side.
3. Example
We will use a practical example to illustrate: in daily life, the most common example is the weather forecast. Here we observe the earth, however, we detect meteorological changes on the earth by launching the Meteorological Satellite observer.
This example involves three objects:
Earth: observed object
Satellite: Observer
Weather Service: client call
Observed object: Earth)
Import Java. util. observable; </P> <p> public class Earth extends observable {<br/> private string weather = "Sunny"; </P> <p> Public String getweather () {<br/> return weather; <br/>}< br/> Public void setweather (string weather) {<br/> This. weather = weather; </P> <p> // set the change point <br/> setchanged (); <br/> policyobservers (weather ); <br/>}</P> <p >}< br/>
[Note] Before detecting an object, you must set the change point setchanged () and notify the observer yyobservers (). These two functions are implemented by the observable class, encapsulates the implementation details of the observer mode.
Observer: Satellite)
Import Java. util. observable; </P> <p> Import Java. util. observer; </P> <p> public class satellite implements observer {</P> <p> private string weather; </P> <p> Public void Update (observable OBJ, object Arg) {</P> <p> weather = (string) ARG; </P> <p> // capture weather changes, feedback to the tester </P> <p> system. out. println ("recent weather changes:" + weather); </P> <p >}</P> <p>}
Client call: Weather Service)
Public class weatherservice {</P> <p> Public static void main (string [] ARGs) {<br/> earth Earth = New Earth (); <br/> satellite = new satellite (); </P> <p> // register, be sure to add <br/> Earth. addobserver (satellite); </P> <p> system. out. println ("Weather Forecast:"); <br/> system. out. println ("------------"); <br/> Earth. setweather ("Typhoon 'Pearl 'approaching"); <br/> Earth. setweather ("rainstorm"); <br/> Earth. setweather ("Hot Weather"); <br/>}</P> <p >}< br/>
[Running result]
Weather Forecast:
------------
Recent weather changes: typhoon 'Pearl 'approaching
Recent weather changes: heavy rain
Recent weather changes: hot weather
Ii. Design Concept 1. situation description
We also use an example to describe how to use the observer mode to elegantly transfer data between views and dialogs of eclipse RCP. Assume that we want to develop a module for information management of individual personnel. The personnel information must be saved with names and Gender attributes. This module requires the addition, modification, and deletion of personnel information, A table is used to list all personnel information.
2. Concept
In this personnel information management module, we first construct the Personnel Information Model (EO) and save the personnel information data to be displayed through a dataset (Personnel Information Data factory, this dataset is our observation object, and the view used to display personnel information in eclipse RCP is the observer. When you modify a dataset in a dialog box or other form, the RCP view automatically refreshes the dataset.
Iii. Source Code Implementation 1. Source Code Composition
2. program source code (1) dialogs. firstdialog. Java
Package dialogs; </P> <p> Import Org. eclipse. jface. dialogs. dialog; <br/> Import Org. eclipse. jface. dialogs. idialogconstants; <br/> Import Org. eclipse. SWT. SWT; <br/> Import Org. eclipse. SWT. graphics. point; <br/> Import Org. eclipse. SWT. layout. filllayout; <br/> Import Org. eclipse. SWT. layout. griddata; <br/> Import Org. eclipse. SWT. layout. gridlayout; <br/> Import Org. eclipse. SWT. widgets. composite; <br/> Import Org. eclipse. SWT. widgets. control; <br/> Import Org. eclipse. SWT. widgets. label; <br/> Import Org. eclipse. SWT. widgets. shell; <br/> Import Org. eclipse. SWT. widgets. text; <br/> Import Org. eclipse. UI. platformui; </P> <p> Import pojos. person; <br/> Import rcptest. view; <br/> Import rcptest. personfactory; </P> <p> public class firstdialog extends dialog {</P> <p> private text text_1; <br/> private text; <br/>/** <br/> * Create the dialog <br/> * @ Param parentshell <br/> */<br/> Public firstdialog (shell parentshell) {<br/> super (parentshell ); <br/>}</P> <p>/** <br/> * Create contents of the dialog <br/> * @ Param parent <br/> */< br/> protected control createdialogarea (composite parent) {<br/> composite container = (composite) super. createdialogarea (parent); <br/> container. setlayout (New filllayout (); </P> <p> final composite composite_1 = new composite (container, SWT. none); <br/> final gridlayout = new gridlayout (); <br/> gridlayout. numcolumns = 2; <br/> composite_1.setlayout (gridlayout); </P> <p> final label Label = new label (composite_1, SWT. none); <br/> label. setlayoutdata (New griddata (); <br/> label. settext ("name:"); </P> <p> text = new text (composite_1, SWT. border); <br/> text. setlayoutdata (New griddata (SWT. fill, SWT. center, true, false); </P> <p> final label label_1 = new label (composite_1, SWT. none); <br/> label_1.settext ("Gender:"); </P> <p> text_1 = new text (composite_1, SWT. border); <br/> text_1.setlayoutdata (New griddata (SWT. fill, SWT. center, true, false); <br/> // <br/> return container; <br/>}</P> <p>/** <br/> * Create contents of the button bar <br/> * @ Param parent <br/> */ <br/> protected void createbuttonsforbuttonbar (composite parent) {<br/> createbutton (parent, idialogconstants. OK _id, "OK", <br/> true); <br/> createbutton (parent, idialogconstants. cancel_id, <br/> "cancel", false ); <br/>}</P> <p>/** <br/> * return the initial size of the dialog <br/> */<br/> protected point getinitialsize () {<br/> return new point (275,184); <br/>}< br/> protected void buttonpressed (INT buttonid) {<br/> If (buttonid = idialogconstants. yes_id) {<br/> personfactory pF = (View) platformui. getworkbench (). getactiveworkbenchwindow (). getactivepage (). findview (view. ID )). getfactory (); <br/> PF. delperson (new person (text. gettext (), text_1.gettext (); <br/>}< br/> If (buttonid = idialogconstants. OK _id) {<br/> personfactory pF = (View) platformui. getworkbench (). getactiveworkbenchwindow (). getactivepage (). findview (view. ID )). getfactory (); <br/> PF. addperson (new person (text. gettext (), text_1.gettext (); <br/>}< br/> super. buttonpressed (buttonid); <br/> This. close (); </P> <p >}</P> <p>}
(2) pojos. Person. Java
Package pojos; </P> <p> public class person {<br/> private string name; <br/> private string sex; </P> <p> Public Person () {<br/> This. name = ""; <br/> This. sex = ""; <br/>}< br/> Public Person (string name, string sex) {<br/> This. name = Name; <br/> This. sex = sex; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String getsex () {<br/> return sex; <br/>}< br/> Public void setsex (string sex) {<br/> This. sex = sex; <br/>}</P> <p >}< br/>
(3) rcptest. personfactory. Java
Package rcptest; </P> <p> Import Java. util. hashmap; <br/> Import Java. util. map; <br/> Import Java. util. observable; </P> <p> Import pojos. person; </P> <p> public class personfactory extends observable {<br/> // Private list <person> data; <br/> Private Static personfactory PF; <br/> Private Static hashmap <string, person> data; </P> <p> private personfactory () {<br/>}</P> <p> Public static personfactory getsinglepersonfactory () {<br/> If (pF = NULL) {<br/> pF = new personfactory (); <br/> DATA = new hashmap <string, person> (); <br/> data. put ("Zhang San", new person ("Zhang San", "male"); <br/> data. put ("", new person ("", ""); <br/> data. put ("Chen 2", new person ("Chen 2", ""); <br/>}< br/> return PF; <br/>}</P> <p> Public hashmap <string, person> getdata () {<br/> return data; <br/>}</P> <p> Public void addperson (person p) {<br/> data. put (P. getname (), P); </P> <p>/** write to database */<br/> // actions </P> <p> setchanged (); <br/> policyobservers (p); <br/>}</P> <p> Public void delperson (person p) {<br/> data. remove (P. getname (); </P> <p> setchanged (); <br/> policyobservers (P ); <br/>}</P> <p> Public void modifyperson (person p) {<br/> data. remove (P. getname (); <br/> data. put (P. getname (), P); </P> <p> setchanged (); <br/> policyobservers (P ); <br/>}</P> <p >}< br/>
(4) view. Java
Package rcptest; </P> <p> Import Java. util. iterator; <br/> Import Java. util. observable; <br/> Import Java. util. observer; </P> <p> Import Org. eclipse. jface. action. action; <br/> Import Org. eclipse. jface. action. imenumanager; <br/> Import Org. eclipse. jface. action. itoolbarmanager; <br/> Import Org. eclipse. jface. viewers. structuredselection; <br/> Import Org. eclipse. jface. viewers. tableviewer; <br/> Import Org. eclipse. SWT. SWT; <br/> Import Org. eclipse. SWT. events. selectionadapter; <br/> Import Org. eclipse. SWT. events. selectionevent; <br/> Import Org. eclipse. SWT. widgets. button; <br/> Import Org. eclipse. SWT. widgets. composite; <br/> Import Org. eclipse. SWT. widgets. label; <br/> Import Org. eclipse. SWT. widgets. shell; <br/> Import Org. eclipse. SWT. widgets. table; <br/> Import Org. eclipse. SWT. widgets. tablecolumn; <br/> Import Org. eclipse. SWT. widgets. text; <br/> Import Org. eclipse. UI. platformui; <br/> Import Org. eclipse. UI. part. viewpart; </P> <p> Import COM. swtdesigner. swtresourcemanager; </P> <p> Import pojos. person; </P> <p> Import dialogs. firstdialog; </P> <p> Public Class View extends viewpart implements observer {</P> <p> private table; <br/> private personfactory PF; <br/> private tableviewer; <br/> Public static final string id = "rcptest. view "; // $ NON-NLS-1 $ </P> <p>/*** <br/> * Create contents of the view part <br/> * @ Param parent <br/> */ <br/> Public void createpartcontrol (composite parent) {<br/> composite container = new composite (parent, SWT. none); </P> <p> pF = personfactory. getsinglepersonfactory (); <br/> PF. addobserver (this); </P> <p> tableviewer = new tableviewer (container, SWT. border | SWT. selected); <br/> table = tableviewer. gettable (); <br/> table. setlinesvisible (true); <br/> table. setheadervisible (true); <br/> table. setbounds (0, 54,494,292); <br/> for (INT I = 0; I <firsttableargs. lable. length; I ++) {<br/> tablecolumn Tc = new tablecolumn (table, SWT. center); <br/> TC. settext (firsttableargs. lable [I]); <br/> TC. setmoveable (true); <br/> table. getcolumn (I ). pack (); <br/>}</P> <p> tableviewer. setsorter (New firsttablesorter (); <br/> tablecolumn column = table. getcolumn (0); <br/> column. addselectionlistener (New selectionadapter () {<br/> Public void widgetselected (selectionevent e) {<br/> (firsttablesorter) (tableviewer. getsorter ())). dosort (firsttableargs. name); <br/> tableviewer. refresh (); <br/>}< br/>}); <br/> column = table. getcolumn (1); <br/> column. addselectionlistener (New selectionadapter () {<br/> Public void widgetselected (selectionevent e) {<br/> (firsttablesorter) tableviewer. getsorter ()). dosort (firsttableargs. sex); <br/> tableviewer. refresh (); <br/>}< br/>}); </P> <p> tableviewer. setcontentprovider (New firstcontentprovider (); <br/> tableviewer. setlabelprovider (New firsttablelabelprovider (); <br/> tableviewer. setinput (PF); </P> <p> final button = new button (container, SWT. none); <br/> button. addselectionlistener (New selectionadapter () {<br/> Public void widgetselected (final selectionevent e) {<br/> firstdialog FD = new firstdialog (new shell ()); <br/> FD. open (); <br/>}< br/>}); <br/> button. settext ("add"); <br/> button. setbounds (25, 15, 48, 22); </P> <p> final button button_1 = new button (container, SWT. none); <br/> button_1.addselectionlistener (New selectionadapter () {<br/> Public void widgetselected (final selectionevent e) {<br/> View FV = (View) platformui. getworkbench (). getactiveworkbenchwindow (). getactivepage (). findview (view. ID); <br/> tableviewer TV = FV. gettableviewer (); <br/> structuredselection Ss = (structuredselection) TV. getselection (); <br/> iterator it = ss. iterator (); <br/> while (it. hasnext () {<br/> FV. getfactory (). delperson (person) it. next (); <br/>}< br/>}); <br/> button_1.settext ("delete "); <br/> button_1.setbounds (90, 15, 48, 22); <br/> // <br/> createactions (); <br/> initializatelbar (); <br/> initializemenu (); <br/>}</P> <p>/** <br/> * Create the actions <br/> */<br/> private void createactions () {<br/> // create the actions <br/>}</P> <p>/** <br/> * initialize the toolbar <br/> */< br/> private void initializetoolbar () {<br/> itoolbarmanager toolbarmanager = getviewsite (). getactionbars () <br/>. gettoolbarmanager (); </P> <p >}</P> <p>/** <br/> * initialize the menu <br/> */<br/> private void initializemenu () {<br/> imenumanager menumanager = getviewsite (). getactionbars () <br/>. getmenumanager (); <br/>}</P> <p> Public void setfocus () {<br/> // set the focus <br/>}</P> <p> Public void settext (string st) {<br/>}</P> <p> Public tableviewer gettableviewer () {<br/> return tableviewer; <br/>}</P> <p> Public personfactory getfactory () {<br/> return PF; <br/>}</P> <p> Public void Update (observable OBJ, object Arg) {<br/> tableviewer. refresh (); <br/>}< br/>
(5) firstcontentprovider. Java
Package rcptest; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. iterator; <br/> Import Java. util. list; <br/> Import Java. util. set; </P> <p> Import Org. eclipse. jface. viewers. istructuredcontentprovider; <br/> Import Org. eclipse. jface. viewers. viewer; </P> <p> Import pojos. person; </P> <p> public class firstcontentprovider implements istructuredcontentprovider {</P> <p> Public object [] getelements (Object inputelement) {</P> <p> personfactory pF = (personfactory) inputelement; <br/> List list = new arraylist (); </P> <p> hashmap <string, person> map = PF. getdata (); <br/> set = PF. getdata (). keyset (); <br/> iterator = set. iterator (); <br/> while (iterator. hasnext () {<br/> list. add (map. get (iterator. next (); <br/>}< br/> return list. toarray (); <br/>}</P> <p> Public void dispose () {<br/> // todo auto-generated method stub </P> <p >}</P> <p> Public void inputchanged (viewer, object oldinput, object newinput) {<br/> // todo auto-generated method stub </P> <p >}< br/>
(6) firstlabelprovider. Java
Package rcptest; </P> <p> Import Org. eclipse. jface. viewers. ilabelproviderlistener; <br/> Import Org. eclipse. jface. viewers. itablelabelprovider; <br/> Import Org. eclipse. SWT. graphics. image; </P> <p> Import pojos. person; </P> <p> public class firsttablelabelprovider implements itablelabelprovider {</P> <p> public image getcolumnimage (object element, int columnindex) {<br/> // todo auto-generated method stub <br/> return NULL; <br/>}</P> <p> Public String getcolumntext (object element, int columnindex) {<br/> person PS = (person) element; <br/> If (columnindex = firsttableargs. id [0]) {<br/> return ps. getname () + ""; <br/>} else if (columnindex = firsttableargs. id [1]) {<br/> return ps. getsex () + ""; <br/>}< br/> return ""; <br/>}</P> <p> Public void addlistener (ilabelproviderlistener listener) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> Public void dispose () {<br/> // todo auto-generated method stub </P> <p >}</P> <p> Public Boolean islabelproperty (object element, string Property) {<br/> // todo auto-generated method stub <br/> return false; <br/>}</P> <p> Public void removelistener (ilabelproviderlistener listener) {<br/> // todo auto-generated method stub </P> <p >}< br/>
(7) firsttablesorter. Java
Package rcptest; </P> <p> Import Org. eclipse. jface. viewers. viewer; <br/> Import Org. eclipse. jface. viewers. viewercomparator; <br/> Import Org. eclipse. jface. viewers. viewersorter; </P> <p> Import pojos. person; </P> <p> public class firsttablesorter extends viewersorter {</P> <p> Private Static final int ascending = 0; <br/> Private Static final int descending = 1; </P> <p> private int order; <br/> private int column; </P> <p> Public void dosort (INT column) {<br/> If (column = This. column) {<br/> order = 1-order; <br/>}else {<br/> This. column = column; <br/> order = ascending; <br/>}</P> <p> Public int compare (viewer, object E1, object E2) {<br/> int result = 0; <br/> viewercomparator Vc = new viewercomparator (); </P> <p> person p1 = (person) E1; <br/> person P2 = (person) E2; </P> <p> switch (column) {<br/> case firsttableargs. name: <br/> result = VC. compare (viewer, p1.getname (), p2.getname (); <br/> break; <br/> case firsttableargs. sex: <br/> result = VC. compare (viewer, p1.getsex (), p2.getsex (); <br/> break; <br/>}; <br/> If (Order = descending) {<br/> result =-result; <br/>}< br/> return result; <br/>}</P> <p >}< br/>
(8) firsttableargs. Java
Package rcptest; </P> <p> public class firsttableargs {<br/> Public final static int ID [] = {0, 1 }; <br/> Public final static int name = 0; <br/> Public final static int sex = 1; <br/> Public final static string lable [] = {"name", "gender" };< br/>}< br/>
Iv. References
Http://koskoswh1.blog.163.com/blog/static/126433078200972402933608/
Http://blog.csdn.net/wsh622827/archive/2009/11/01/4755140.aspx