Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / OpenObject.java
1 package org.splat.simer;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Vector;
7
8 import org.splat.kernel.User;
9 import org.splat.som.Document;
10 import org.splat.som.DocumentType;
11 import org.splat.som.KnowledgeElement;
12 import org.splat.som.KnowledgeElementType;
13 import org.splat.som.ProgressState;
14 import org.splat.som.Proxy;
15 import org.splat.som.Publication;
16 import org.splat.som.Scenario;
17 import org.splat.som.Step;
18 import org.splat.wapp.Menu;
19 import org.splat.wapp.PopupMenu;
20
21
22 public abstract class OpenObject implements Proxy {
23
24         protected  User                          cuser       = null;   // Connected user
25         protected  String                        selection   = null;   // Menu selected by the user
26         protected  Step                          ustep       = null;   // Corresponding selected step
27         protected  String                        description = null;   // Object description (rich text)
28         protected  List<Step>                    involving   = null;
29         protected  List<SimulationContextFacade> context     = null;   // Simulation Context display representations
30         protected  List<DocumentFacade>          contents    = null;   // Document display representations
31         protected  List<KnowledgeIterator>       knowledge   = null;   // Knowledge Element display representations structured by knowledge types
32         
33         protected  Menu                          menu        = null;   // Left pane menu of this object
34         protected  PopupMenu                     popup       = null;   // Pop-up menu of this object, if the user has write access
35
36     protected static HashMap<Integer, DocumentFacade>         docpres  = null;
37     protected static HashMap<Integer, KnowledgeElementFacade> knowpres = null;
38
39         public class KnowledgeIterator {
40 //  ------------------------------
41       protected KnowledgeElementType         type;
42       protected List<KnowledgeElementFacade> list;
43
44       public KnowledgeIterator (KnowledgeElementType type, List<KnowledgeElementFacade> list)
45       {
46         this.type = type;
47         this.list = list;
48       }
49       public String getIndex ()
50       {
51         return String.valueOf(type.getIndex());
52       }
53       public String getType ()
54       {
55         return type.getName();
56       }
57       public List<KnowledgeElementFacade> getKnowledgeElements ()
58       {
59         return list;
60       }
61         }
62     
63 //  ==============================================================================================================================
64 //  Constructor
65 //  ==============================================================================================================================
66
67     protected OpenObject () {
68 //  -----------------------
69 //    All member fields are supposed initialized by subclasses 
70       if (docpres == null)  docpres  = new HashMap<Integer, DocumentFacade>();
71       if (knowpres == null) knowpres = new HashMap<Integer, KnowledgeElementFacade>();
72     }
73
74 //  ==============================================================================================================================
75 //  Public member functions
76 //  ==============================================================================================================================
77
78     public void developDocument (String index) {
79 //  ------------------------------------------
80       for (Iterator<DocumentFacade> i=contents.iterator(); i.hasNext();) {
81         DocumentFacade doc = i.next();
82         if (!doc.getIndex().equals(index)) continue;
83         doc.develop();
84         return;
85       }
86     }
87
88     public void developKnowledge (String index) {
89 //  -------------------------------------------
90       for (Iterator<KnowledgeIterator> i=knowledge.iterator(); i.hasNext();) {
91         List<KnowledgeElementFacade> knowelms = i.next().getKnowledgeElements();
92         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j.hasNext(); ) {
93           KnowledgeElementFacade kelm = j.next();
94           if (!kelm.getIndex().equals(index)) continue;
95           kelm.develop();
96           return;
97         }
98       }
99     }
100
101     public void clearFacades () {
102 //  ---------------------------
103       docpres.clear();      // For eventually reopening the knowledge from a fresh context
104       knowpres.clear();     // For eventually reopening the knowledge from a fresh context
105     }
106
107     public List<Document> collectInvolvedDocuments (DocumentType type) {
108 //  ------------------------------------------------------------------
109       List<Document> found = new Vector<Document>();
110       for (Iterator<Step> i=involving.iterator(); i.hasNext();) {
111         Step              step  = i.next();
112         List<Publication> exist = step.getAllDocuments();
113         for (Iterator<Publication> j=exist.iterator(); j.hasNext();) {
114           Document doc = j.next().value();
115           if (doc.getType().equals(type)) found.add(doc);
116         }
117       }
118       return found;
119     }
120
121     public String getDisplayedDescription () {
122 //  ----------------------------------------
123       return description;
124     }
125
126     public List<DocumentFacade> getDisplayedDocuments () {
127 //  -----------------------------------------------------
128       return contents;
129     }
130
131     public List<SimulationContextFacade> getDisplayedSimulationContexts () {
132 //  ----------------------------------------------------------------------
133       return context;
134     }
135
136     public List<KnowledgeIterator> getDisplayedKnowledges () {
137 //  --------------------------------------------------------
138       return knowledge;
139     }
140
141     public List<Step> getInvolvedSteps () {
142 //  -------------------------------------
143       return involving;
144     }
145
146     public Menu getMenu () {
147 //  ----------------------
148       return menu;
149     }
150
151     public PopupMenu getPopup () {
152 //  ----------------------------
153       return popup;
154     }
155
156     public Step getSelectedStep () {
157 //  ------------------------------
158       return ustep;
159     }
160
161     public String getSelection () {
162 //  -----------------------------
163       return selection;
164     }
165
166     public User getUser () {
167 //  ----------------------
168       return cuser;
169     }
170
171     public boolean isOpenForWriting () {
172 //  ----------------------------------
173       return (popup != null);     // The pop-up is supposed existed when the user is staffed on the study
174     }
175
176     public void reduceDocument (String index) {
177 //  -----------------------------------------
178       for (Iterator<DocumentFacade> i=contents.iterator(); i.hasNext();) {
179         DocumentFacade doc = i.next();
180         if (!doc.getIndex().equals(index)) continue;
181         doc.reduceAll();
182         return;
183       }
184     }
185
186     public void reduceHistory (String index) {
187 //  ----------------------------------------
188       for (Iterator<DocumentFacade> i=contents.iterator(); i.hasNext();) {
189         DocumentFacade doc = i.next();
190         if (!doc.getIndex().equals(index)) continue;
191         doc.reduce();
192         return;
193       }
194     }
195
196     public void reduceKnowledge (String index) {
197 //  ------------------------------------------
198       for (Iterator<KnowledgeIterator> i=knowledge.iterator(); i.hasNext();) {
199         List<KnowledgeElementFacade> knowelms = i.next().getKnowledgeElements();
200         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j.hasNext(); ) {
201           KnowledgeElementFacade kelm = j.next();
202           if (!kelm.getIndex().equals(index)) continue;
203           kelm.reduce();
204           return;
205         }
206       }
207     }
208
209 //  ==============================================================================================================================
210 //  Protected services
211 //  ==============================================================================================================================
212
213     protected void setupContents () {
214 //  -------------------------------
215 //  Description
216 //    Initialized in subclasses
217
218 //  Knowledge elements supposed ordered by type
219       if (ustep.mayContain(KnowledgeElement.class)) {
220         Scenario                   scene   = (Scenario)ustep.getOwner();
221             List<KnowledgeElementType> types   = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
222         List<KnowledgeElement>     kelms   = scene.getAllKnowledgeElements();
223         Iterator<KnowledgeElement> more    = kelms.iterator();
224         KnowledgeElement           current = null;
225         if (more.hasNext())        current = more.next();
226
227         knowledge = new Vector<KnowledgeIterator>(types.size());
228             for (Iterator<KnowledgeElementType> i=types.iterator(); i.hasNext();) {
229           KnowledgeElementType         type    = i.next();
230           List<KnowledgeElementFacade> display = new Vector<KnowledgeElementFacade>(kelms.size());
231           while (current != null && current.getType().equals(type)) {
232                 KnowledgeElementFacade facade = knowpres.get(current.getIndex());
233                 if (facade == null) {
234               facade = new KnowledgeElementFacade(current);
235               knowpres.put(current.getIndex(), facade);
236                 }
237                 display.add(facade);
238                 if (more.hasNext()) current = more.next();
239                 else                current = null;
240           }
241           knowledge.add( new KnowledgeIterator(type, display) );
242         }
243       } else {
244         knowledge = null;
245       }
246 //  Documents
247           if (ustep.mayContain(Document.class)) {
248         List<Publication> list = ustep.getAllDocuments();
249           
250         contents = new Vector<DocumentFacade>(list.size());
251         for (Iterator<Publication> i=list.iterator(); i.hasNext();) {
252           Publication     present = i.next();
253           Integer         index   = present.getIndex();
254           DocumentFacade  facade  = docpres.get(index);
255           if (facade == null) {
256             facade = new DocumentFacade(this, present);
257             docpres.put(index, facade);
258           }
259           contents.add(facade);
260         }
261           } else {
262         contents = null;
263           }
264     }
265 }