View Javadoc
1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2006</p>
22   * <p>Company: OntoText Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.ui.editors.model;
26  
27  import java.util.*;
28  import java.util.Observable;
29  
30  import org.eclipse.core.runtime.IAdaptable;
31  
32  public class ObservableModel extends Observable implements Observer, IAdaptable {
33      
34      protected boolean dirty = false;
35      protected ObservableModel masterModel = null;
36      protected List<ObservableModel> subModels = new LinkedList<ObservableModel>();
37      
38      protected Object wsmoDelegate = null;
39      
40      private boolean disabled = false;
41      
42      private ObservableModel() {
43      }
44      public ObservableModel(Object wsmoObject) {
45          super();
46          this.wsmoDelegate = wsmoObject;
47      }
48      
49      public void disableNotification() {
50          disabled = true;
51      }
52      public void enableNotification() {
53          disabled = false;
54      }
55      
56      public Object getAdapter(Class adapter) {
57          if (wsmoDelegate == null) {
58              return null;
59          }
60          if (adapter.isInstance(wsmoDelegate)) {
61              return wsmoDelegate;
62          }
63          return null;
64      }
65      
66      public void setChanged() {
67          if (disabled) {
68              return;
69          }
70          dirty = true;
71          super.setChanged();
72          notifyObservers(wsmoDelegate);
73          if (masterModel != null) {
74              masterModel.setChanged();
75          }
76      }
77      
78      public void notifyObservers(Object arg) {
79          if (disabled) {
80              return;
81          }
82          super.setChanged();
83          super.notifyObservers(arg);
84      }
85      
86      public boolean isDirty() {
87          return dirty;
88      }
89      
90      public void clearDirty() {
91          if (false == dirty) {
92              return;
93          }
94          dirty = false;
95          for(ObservableModel emodel : subModels) {
96              emodel.clearDirty();
97          }
98          notifyObservers(wsmoDelegate);
99      }
100     
101     public void update(Observable o, Object arg) {
102         if (disabled) {
103             return;
104         }
105         this.notifyObservers(arg);
106         for(ObservableModel model : subModels) {
107             if (false == model.equals(o)) {
108                 model.setChanged();
109                 model.notifyObservers(arg);
110             }
111         }
112     }
113     
114     public void setMasterModel(ObservableModel newMasterModel) {
115         if (masterModel != null) {
116             masterModel.removeSubModel(this);
117         }
118         this.masterModel = newMasterModel;
119     }
120     
121     public ObservableModel getMasterModel() {
122         return this.masterModel;
123     }
124     
125     public void addSubModel(ObservableModel submodel) {
126         if (subModels.indexOf(submodel) == -1) {
127             subModels.add(submodel);
128             submodel.setMasterModel(this);
129         }
130     }
131     
132     public void removeSubModel(ObservableModel submodel) {
133         if (subModels.indexOf(submodel) != -1) {
134             subModels.remove(submodel);
135             submodel.setMasterModel(null);
136         }
137     }
138     
139     public Set<ObservableModel> listSubModels() {
140         return new HashSet<ObservableModel>(subModels);
141     }
142     
143     public int countObservers() {
144         return super.countObservers();
145     }
146 
147 }
148 
149 
150 /*
151  * $Log: ObservableModel.java,v $
152  * Revision 1.3  2006/01/09 12:51:14  alex_simov
153  * Copyright message in header updated
154  *
155  */