View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2007, 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-2007</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.bpmo.model;
26  
27  import java.util.*;
28  
29  import org.eclipse.draw2d.geometry.Dimension;
30  import org.eclipse.draw2d.geometry.Point;
31  import org.omwg.ontology.Ontology;
32  import org.wsmo.common.Namespace;
33  import org.wsmo.common.TopEntity;
34  import org.wsmostudio.bpmo.ui.editor.BpmoEditor;
35  import org.wsmostudio.runtime.WSMORuntime;
36  
37  import com.ontotext.sbpm.bpmo.common.Constants;
38  
39  public class BpmoModel extends WorkflowEntitiesContainer {
40  
41      private List<ProcessNode> processes;
42      private BpmoEditor editorPart = null;
43      private Ontology nsHolder = null;
44      
45  	public BpmoModel() {
46  		this.processes = new ArrayList<ProcessNode>();
47  	}
48  	
49  	public void setContainingEditor(BpmoEditor editor) {
50  		this.editorPart = editor;
51  	}
52  	
53  	public void notifyContentChanged() {
54  		if (this.editorPart != null) {
55  		    this.editorPart.markDirty();
56  		}
57  	}
58  	
59  	public List<ProcessNode> listProcesses() {
60  		return Collections.unmodifiableList(this.processes);
61  	}
62      
63      public void addProcess(ProcessNode process) {
64          this.processes.add(process);
65          if (process.getOwner() != this) {
66              process.setOwner(this);
67          }
68          firePropertyChange("content", null, process);
69      }
70  
71      public void removeProcess(ProcessNode process) {
72          this.processes.remove(process);
73          process.setOwner(null);
74          firePropertyChange("content", process, null);
75      }
76      
77      public Dimension getPreferredSize() {
78          return null; // not really needed
79      }
80  
81      @Override
82      public WorkflowEntityNode cloneNode() {
83          throw new UnsupportedOperationException();
84      }
85      
86      public List<WorkflowProperty> listSupportedProperties() {
87          return new LinkedList<WorkflowProperty>();
88      }
89  
90      public TopEntity getNSHolder() {
91          return this.nsHolder;
92      }
93      
94      public void setNamespaces(Ontology onto) {
95          this.nsHolder = onto;
96      }
97  
98      public void updateNamespacesTo(Ontology onto) {
99          if (this.nsHolder == null) {
100             return;
101         }
102         if (this.nsHolder.getDefaultNamespace() != null) {
103             onto.setDefaultNamespace(this.nsHolder.getDefaultNamespace());
104         }
105         else {
106             onto.setDefaultNamespace(
107                     WSMORuntime.getRuntime().getWsmoFactory().createIRI(
108                             Constants.DEFAULT_NS));
109         }
110         for(Namespace ns : this.nsHolder.listNamespaces()) {
111             boolean isUnique = true;
112             for(Namespace existing : onto.listNamespaces()) {
113                 if (existing.getIRI().equals(ns.getIRI())) {
114                     isUnique = false;
115                     break;
116                 }
117             }
118             if (isUnique) {
119                 onto.addNamespace(ns); // add only the new ones and preserve any existing
120             }
121         }
122     }
123     
124     public void reorderProcessNodes() {
125         if (processes.size() < 2) {
126             return;
127         }
128         ProcessNode[] childNodes = 
129             processes.toArray(new ProcessNode[processes.size()]);
130         Arrays.sort(childNodes, new Comparator<ProcessNode>() {
131             public int compare(ProcessNode o1, ProcessNode o2) {
132                 return o1.getLocation().y - o2.getLocation().y;
133             }
134         });
135         int nextAvailableY = 10;
136         for(int i = 0; i < childNodes.length; i++) {
137             ProcessNode prevNode = (i == 0) ? childNodes[i] : childNodes[i-1];
138             shiftNodesAfter(i, childNodes, nextAvailableY, prevNode.getLocation().x, prevNode.getSize().width);
139             nextAvailableY = 
140                 childNodes[i].getLocation().y + childNodes[i].getSize().height + 10;
141         }
142     }
143 
144     private void shiftNodesAfter(int index, ProcessNode[] all, int minY, int prevX, int prevW) {
145         for(int i = index; i < all.length; i++) {
146             Point loc = all[i].getLocation();
147             if (loc.y < minY) {
148                 boolean mustMove = i == 0;
149                 if (i > 0) {
150                     mustMove = (prevX + prevW > loc.x - 5 
151                             && prevX < loc.x + all[i].getSize().width + 5); 
152                 }
153                 if (mustMove) {
154                     all[i].moveToLocation(loc.x, minY);
155                 }
156             }
157         }
158     }
159 
160     
161 }
162 
163 /*
164  * $Log$
165  * Revision 1.4  2007/08/01 15:47:23  alex_simov
166  * ui model refactoring: classnames are suffixed by "Node" to avoid name
167  * clashes with BPMO-API entities
168  *
169  * Revision 1.3  2007/06/26 17:44:31  alex_simov
170  * integrated composer
171  *
172  * Revision 1.2  2007/04/05 16:50:34  alex_simov
173  * loop block patterns added
174  *
175  * Revision 1.1  2007/03/20 10:05:46  alex_simov
176  * SBP modeller initial version
177  *
178  */