Salome HOME
Addition of a new scenario to a study is fixed. StudyMenu and NewScenarioMenu are...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / StudyMenu.java
1 package org.splat.simer;
2
3 import java.util.Iterator;
4 import java.util.Vector;
5
6 import org.splat.dal.bo.som.ProjectElement;
7 import org.splat.dal.bo.som.Scenario;
8 import org.splat.service.ProjectElementService;
9 import org.splat.service.ScenarioService;
10 import org.splat.som.Step;
11 import org.splat.dal.bo.som.Study;
12 import org.splat.wapp.MenuItem;
13 import org.splat.wapp.SlidMenu;
14
15 public class StudyMenu extends SlidMenu {
16
17         private Study study;
18         private Scenario scopen; // Currently "open" scenario
19         private Step stopen; // Currently selected step
20         private ScenarioService _scenarioService;
21         private ProjectElementService _projectElementService;
22
23         // ==============================================================================================================================
24         // Constructor
25         // ==============================================================================================================================
26
27         public StudyMenu() {
28                 super("activities", "study");
29                 scopen = null;
30         }
31
32         public StudyMenu init(Study context) {
33                 study = context;
34                 scopen = null;
35                 return this;
36         }
37
38         public StudyMenu(Study context) {
39                 // --------------------------------
40                 super("activities", "study");
41                 study = context;
42                 scopen = null;
43         }
44
45         // ==============================================================================================================================
46         // Member functions
47         // ==============================================================================================================================
48
49         public void selects(String name) {
50                 // ---------------------------------
51                 String[] parse = name.split("\\x2E");
52                 Scenario[] scenes = study.getScenarii();
53                 Scenario scenew = scopen;
54                 int askid = 0;
55
56                 // Initialization
57                 if (scenew == null && scenes.length == 1)
58                         scenew = scenes[0];
59                 try {
60                         int askdex = Integer.valueOf(parse[0]);
61                         if (askdex > 0) {
62                                 while (askid < scenes.length) {
63                                         if (scenes[askid].getIndex() == askdex)
64                                                 break;
65                                         askid += 1;
66                                 }
67                                 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
68                         }
69                 } catch (Exception error) {
70                         return;
71                 }
72                 if (scenew == null) {
73
74                         // Study with several scenarii, non of them open
75                         // Collection of steps to be displayed
76                         Vector<Step> steps = new Vector<Step>();
77                         Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
78
79                         int base = newstep[0].getNumber();
80                         int last = newstep[newstep.length - 1].getNumber();
81                         for (int i = 0; i < scenes.length; i++)
82                                 steps.add(getProjectElementService().getFirstStep(scenes[i]));
83
84                         newstep = getProjectElementService().getSteps(study);
85                         stopen = newstep[0]; // Default selected step
86                         for (int i = newstep.length - 1; i > -1; i--) {
87                                 if (newstep[i].getNumber() >= base)
88                                         continue;
89                                 steps.add(0, newstep[i]);
90                         }
91                         for (int i = 0; i < newstep.length; i++) {
92                                 if (newstep[i].getNumber() <= last)
93                                         continue;
94                                 steps.add(newstep[i]);
95                         }
96                         // Creation of the menu
97                         for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
98                                 Step step = i.next();
99                                 int number = step.getNumber();
100                                 String icon;
101                                 if (step.getOwner() instanceof Study) {
102                                         if (!step.isStarted())
103                                                 icon = "icon.empty.png";
104                                         else if (step.isFinished())
105                                                 icon = "icon.checked.png";
106                                         else
107                                                 icon = "icon.done.png";
108                                         addItem("0." + number, "menu.step." + number, icon,
109                                                         "step-study?selection=0." + number);
110                                         // WARNING: The selection number must end the action's parameters for the need of refreshGivenStepItem()
111                                 } else {
112                                         Scenario group = (Scenario) step.getOwner();
113                                         long index = group.getIndex();
114                                         String value = index + "." + number;
115                                         if (group.isCheckedout())
116                                                 icon = "icon.checkedout.png";
117                                         else if (getScenarioService().isEmpty(group))
118                                                 icon = "icon.empty.png";
119                                         // else if (group.isFinished()) icon = "icon.checked.png";
120                                         else
121                                                 icon = "icon.done.png";
122                                         addGroup(value, group.getTitle(), icon,
123                                                         "step-study?selection=" + value);
124                                 }
125                         }
126                 } else if (scopen == null || !scenew.equals(scopen)) {
127
128                         // Opening a scenario
129                         this.clear();
130                         // Collection of steps to be displayed
131                         Vector<Step> steps = new Vector<Step>();
132                         Step[] newstep = getProjectElementService().getSteps(scenew);
133
134                         int base = newstep[0].getNumber();
135                         int last = newstep[newstep.length - 1].getNumber();
136                         for (int i = 0; i < newstep.length; i++) {
137                                 steps.add(newstep[i]);
138                         }
139                         for (int i = askid - 1; i > -1; i--)
140                                 steps
141                                                 .add(0, getProjectElementService().getFirstStep(
142                                                                 scenes[i]));
143                         newstep = getProjectElementService().getSteps(study);
144                         for (int i = newstep.length - 1; i > -1; i--) {
145                                 if (newstep[i].getNumber() >= base)
146                                         continue;
147                                 steps.add(0, newstep[i]);
148                         }
149                         for (int i = askid + 1; i < scenes.length; i++)
150                                 steps.add(getProjectElementService().getFirstStep(scenes[i]));
151                         for (int i = 0; i < newstep.length; i++) {
152                                 if (newstep[i].getNumber() <= last)
153                                         continue;
154                                 steps.add(newstep[i]);
155                         }
156                         // Creation of the menu
157                         boolean first = true; // For differentiating the first scenario step
158                         int askdex = Integer.valueOf(parse[1]);
159                         for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
160                                 Step step = i.next();
161                                 int number = step.getNumber();
162                                 String icon;
163                                 if (!step.isStarted())
164                                         icon = "icon.empty.png";
165                                 else if (step.isFinished())
166                                         icon = "icon.checked.png";
167                                 else
168                                         icon = "icon.done.png";
169                                 if (number == askdex)
170                                         stopen = step;
171                                 if (step.getOwner() instanceof Study) {
172                                         addItem("0." + number, "menu.step." + number, icon,
173                                                         "step-study?selection=0." + number);
174                                 } else {
175                                         Scenario group = (Scenario) step.getOwner();
176                                         long index = group.getIndex();
177                                         String value = index + "." + number;
178                                         if (index != scenew.getIndex()) {
179                                                 if (group.isCheckedout())
180                                                         icon = "icon.checkedout.png";
181                                                 else if (getScenarioService().isEmpty(group))
182                                                         icon = "icon.empty.png";
183                                                 // else if (group.isFinished()) icon = "icon.checked.png";
184                                                 else
185                                                         icon = "icon.done.png";
186                                                 addGroup(value, group.getTitle(), icon,
187                                                                 "step-study?selection=" + value);
188                                         } else if (first) {
189                                                 if (group.isCheckedout())
190                                                         icon = "icon.checkedout.png";
191                                                 addGroup(value, scenew.getTitle(), icon,
192                                                                 "step-study?selection=" + value);
193                                                 first = false;
194                                         } else {
195                                                 addSubItem(value, "menu.step." + number, icon,
196                                                                 "step-study?selection=" + value);
197                                         }
198                                 }
199                         }
200                         scopen = scenew;
201                 } else {
202                         Step[] step = getProjectElementService().getSteps(scopen);
203                         int selected = Integer.valueOf(parse[1]);
204                         for (int i = 0; i < step.length; i++) {
205                                 if (step[i].getNumber() != selected)
206                                         continue;
207                                 stopen = step[i];
208                                 break;
209                         }
210                 }
211                 super.selects(name);
212         }
213
214         public void refreshGivenStepItem(Step step) {
215                 // --------------------------------------------
216                 String number = "." + step.getNumber();
217                 ProjectElement owner = step.getOwner();
218                 int range = 0;
219
220                 for (Iterator<MenuItem> action = menu.iterator(); action.hasNext();) {
221                         MenuItem item = action.next();
222                         String index = item.getAction(); // Returns the above string ended by the selection number
223                         if (!index.endsWith(number))
224                                 continue;
225
226                         String icon;
227                         if (owner instanceof Scenario) {
228                                 if (range == 0 && ((Scenario) owner).isCheckedout())
229                                         icon = "icon.checkedout.png";
230                                 range += 1;
231                         }
232                         if (!step.isStarted())
233                                 icon = "icon.empty.png";
234                         else if (step.isFinished())
235                                 icon = "icon.checked.png";
236                         else
237                                 icon = "icon.done.png";
238                         item.icon(icon);
239                 }
240         }
241
242         public void refreshSelectedItem() {
243                 // ----------------------------------
244                 MenuItem item = this.getSelectedItem();
245                 String icon;
246                 if (!stopen.isStarted())
247                         icon = "icon.empty.png";
248                 else if (stopen.isFinished())
249                         icon = "icon.checked.png";
250                 else
251                         icon = "icon.done.png";
252                 item.icon(icon);
253         }
254
255         /**
256          * Get the scenarioService.
257          * 
258          * @return the scenarioService
259          */
260         public ScenarioService getScenarioService() {
261                 return _scenarioService;
262         }
263
264         /**
265          * Set the scenarioService.
266          * 
267          * @param scenarioService
268          *            the scenarioService to set
269          */
270         public void setScenarioService(ScenarioService scenarioService) {
271                 _scenarioService = scenarioService;
272         }
273
274         /**
275          * Get the projectElementService.
276          * 
277          * @return the projectElementService
278          */
279         public ProjectElementService getProjectElementService() {
280                 return _projectElementService;
281         }
282
283         /**
284          * Set the projectElementService.
285          * 
286          * @param projectElementService
287          *            the projectElementService to set
288          */
289         public void setProjectElementService(
290                         ProjectElementService projectElementService) {
291                 _projectElementService = projectElementService;
292         }
293 }