Salome HOME
Remove the scenario functionality is implemented
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / StudyMenu.java
1 package org.splat.simer;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.splat.dal.bo.som.ProjectElement;
9 import org.splat.dal.bo.som.Scenario;
10 import org.splat.dal.bo.som.Study;
11 import org.splat.service.ProjectElementService;
12 import org.splat.service.ScenarioService;
13 import org.splat.som.Step;
14 import org.splat.wapp.Constants;
15 import org.splat.wapp.MenuItem;
16 import org.splat.wapp.SlidMenu;
17
18 /**
19  * Menu of a study.
20  */
21 public class StudyMenu extends SlidMenu {
22
23         /**
24          * Selection URL.
25          */
26         private static final String SELECTION_URL = "step-study?selection=";
27
28         /**
29          * Currently open study.
30          */
31         private transient Study _study = null;
32         /**
33          * Currently "open" scenario.
34          */
35         private transient Scenario _scopen = null;
36         /**
37          * Currently selected step.
38          */
39         private transient Step _stopen;
40
41         /**
42          * Scenario service.
43          */
44         private ScenarioService _scenarioService;
45         /**
46          * Project element service.
47          */
48         private ProjectElementService _projectElementService;
49
50         // ==============================================================================================================================
51         // Constructor
52         // ==============================================================================================================================
53
54         /**
55          * Default constructor.
56          */
57         public StudyMenu() {
58                 super("activities", Constants.STUDY_MENU);
59         }
60
61         /**
62          * Create a menu for the given study.
63          * @param context the study
64          */
65         public StudyMenu(final Study context) {
66                 super("activities", Constants.STUDY_MENU);
67                 _study = context;
68         }
69
70         /**
71          * Initialize the menu for the given study.
72          * @param context the study
73          * @return the study menu
74          */
75         public StudyMenu init(final Study context) {
76                 _study = context;
77                 _scopen = null;
78                 init();
79                 return this;
80         }
81
82         // ==============================================================================================================================
83         // Member functions
84         // ==============================================================================================================================
85
86         /** 
87          * {@inheritDoc}
88          * @see org.splat.wapp.Menu#selects(java.lang.String)
89          */
90         @Override
91         public void selects(final String name) {
92                 String[] parse = name.split("\\x2E");
93                 Scenario[] scenes = _study.getScenarii();
94                 Scenario scenew = _scopen;
95                 int askid = 0;
96
97                 // Initialization
98                 if (scenew == null && scenes.length == 1) {
99                         scenew = scenes[0];
100                 }
101                 try {
102                         int askdex = Integer.valueOf(parse[0]);
103                         if (askdex > 0) {
104                                 while (askid < scenes.length) {
105                                         if (scenes[askid].getIndex() == askdex) {
106                                                 break;
107                                         }
108                                         askid += 1;
109                                 }
110                                 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
111                         }
112                 } catch (Exception error) {
113                         return;
114                 }
115                 if (scenew == null) {
116                         openStudy(scenes);
117                 } else if (_scopen == null || !scenew.equals(_scopen)) {
118                         openScenario(scenew, scenes, askid, Integer.valueOf(parse[1]));
119                 } else {
120                         Step[] step = getProjectElementService().getSteps(_scopen);
121                         int selected = Integer.valueOf(parse[1]);
122                         for (int i = 0; i < step.length; i++) {
123                                 if (step[i].getNumber() == selected) {
124                                         _stopen = step[i];
125                                         break;
126                                 }
127                         }
128                 }
129                 super.selects(name);
130         }
131
132         /**
133          * Open a scenario.
134          * 
135          * @param scenew
136          *            the scenario to open
137          * @param scenes
138          *            study scenarii
139          * @param askid
140          *            the last scenario index
141          * @param stepIndex
142          *            the open step index
143          */
144         private void openScenario(final Scenario scenew, final Scenario[] scenes,
145                         final int askid, final int stepIndex) {
146
147                 // Opening a scenario
148                 this.clear();
149                 // Collection of steps to be displayed
150                 List<Step> steps = getStepsToDisplay(scenew, scenes, askid);
151                 // Creation of the menu
152                 boolean first = true; // For differentiating the first scenario step
153                 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
154                         Step step = i.next();
155                         int number = step.getNumber();
156                         String icon;
157                         icon = getIcon(step);
158                         if (number == stepIndex) {
159                                 _stopen = step;
160                         }
161                         if (step.getOwner() instanceof Study) {
162                                 addItem("0." + number, "menu.step." + number, icon,
163                                                 SELECTION_URL + "0." + number);
164                         } else {
165                                 Scenario group = (Scenario) step.getOwner();
166                                 long index = group.getIndex();
167                                 String value = index + "." + number;
168                                 if (index == scenew.getIndex()) {
169                                         if (first) {
170                                                 if (group.isCheckedout()) {
171                                                         icon = Constants.IMG_CHECKEDOUT;
172                                                 }
173                                                 addGroup(value, scenew.getTitle(), icon, SELECTION_URL
174                                                                 + value);
175                                                 first = false;
176                                         } else {
177                                                 addSubItem(value, "menu.step." + number, icon,
178                                                                 SELECTION_URL + value);
179                                         }
180                                 } else {
181                                         addItemsGroup(group, value);
182                                 }
183                         }
184                 }
185                 _scopen = scenew;
186         }
187
188         /**
189          * Get list of steps to display.
190          * 
191          * @param scenew
192          *            the scenario to open
193          * @param scenes
194          *            study scenarii
195          * @param askid
196          *            the last scenario index
197          * @return list of steps
198          */
199         private List<Step> getStepsToDisplay(final Scenario scenew,
200                         final Scenario[] scenes, final int askid) {
201                 List<Step> steps = new ArrayList<Step>();
202                 Step[] newstep = getProjectElementService().getSteps(scenew);
203
204                 int base = newstep[0].getNumber();
205                 int last = newstep[newstep.length - 1].getNumber();
206                 steps.addAll(Arrays.asList(newstep));
207                 for (int i = askid - 1; i > -1; i--) {
208                         steps.add(0, getProjectElementService().getFirstStep(scenes[i]));
209                 }
210                 newstep = getProjectElementService().getSteps(_study);
211                 for (int i = newstep.length - 1; i > -1; i--) {
212                         if (newstep[i].getNumber() < base) {
213                                 steps.add(0, newstep[i]);
214                         }
215                 }
216                 for (int i = askid + 1; i < scenes.length; i++) {
217                         steps.add(getProjectElementService().getFirstStep(scenes[i]));
218                 }
219                 for (int i = 0; i < newstep.length; i++) {
220                         if (newstep[i].getNumber() > last) {
221                                 steps.add(newstep[i]);
222                         }
223                 }
224                 return steps;
225         }
226
227         /**
228          * Add a group of menu items according to the scenario content.
229          * 
230          * @param group
231          *            the scenario
232          * @param value
233          *            the scenario name
234          */
235         private void addItemsGroup(final Scenario group, final String value) {
236                 String icon;
237                 if (group.isCheckedout()) {
238                         icon = Constants.IMG_CHECKEDOUT;
239                 } else if (getScenarioService().isEmpty(group)) {
240                         icon = Constants.IMG_EMPTY;
241                         // else if (group.isFinished()) icon = IMG_CHECKED;
242                 } else {
243                         icon = Constants.IMG_DONE;
244                 }
245                 addGroup(value, group.getTitle(), icon, SELECTION_URL + value);
246         }
247
248         /**
249          * Study with several scenarii, non of them open. Collection of steps to be displayed.
250          * 
251          * @param scenes
252          *            study scenarii
253          */
254         private void openStudy(final Scenario[] scenes) {
255                 List<Step> steps = new ArrayList<Step>();
256                 Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
257
258                 int base = newstep[0].getNumber();
259                 int last = newstep[newstep.length - 1].getNumber();
260                 for (int i = 0; i < scenes.length; i++) {
261                         steps.add(getProjectElementService().getFirstStep(scenes[i]));
262                 }
263
264                 newstep = getProjectElementService().getSteps(_study);
265                 _stopen = newstep[0]; // Default selected step
266                 for (int i = newstep.length - 1; i > -1; i--) {
267                         if (newstep[i].getNumber() < base) {
268                                 steps.add(0, newstep[i]);
269                         }
270                 }
271                 for (int i = 0; i < newstep.length; i++) {
272                         if (newstep[i].getNumber() > last) {
273                                 steps.add(newstep[i]);
274                         }
275                 }
276                 // Creation of the menu
277                 this.clear();
278                 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
279                         Step step = i.next();
280                         int number = step.getNumber();
281                         String icon;
282                         if (step.getOwner() instanceof Study) {
283                                 icon = getIcon(step);
284                                 addItem("0." + number, "menu.step." + number, icon,
285                                                 SELECTION_URL + "0." + number);
286                                 // WARNING: The selection number must end the action's parameters for the need of refreshGivenStepItem()
287                         } else {
288                                 Scenario group = (Scenario) step.getOwner();
289                                 long index = group.getIndex();
290                                 String value = index + "." + number;
291                                 addItemsGroup(group, value);
292                         }
293                 }
294         }
295
296         /**
297          * Get icon according to the step progress state.
298          * 
299          * @param step
300          *            the step
301          * @return the icon
302          */
303         private String getIcon(final Step step) {
304                 String icon;
305                 if (step.isStarted()) {
306                         if (step.isFinished()) {
307                                 icon = Constants.IMG_CHECKED;
308                         } else {
309                                 icon = Constants.IMG_DONE;
310                         }
311                 } else {
312                         icon = Constants.IMG_EMPTY;
313                 }
314                 return icon;
315         }
316
317         /**
318          * Refresh the given step's menu item.
319          * @param step the step to refresh
320          */
321         public void refreshGivenStepItem(final Step step) {
322                 String number = "." + step.getNumber();
323                 ProjectElement owner = step.getOwner();
324                 int range = 0;
325
326                 for (Iterator<MenuItem> action = _menu.iterator(); action.hasNext();) {
327                         MenuItem item = action.next();
328                         String index = item.getAction(); // Returns the above string ended by the selection number
329                         if (!index.endsWith(number)) {
330                                 continue;
331                         }
332
333                         String icon;
334                         if (owner instanceof Scenario) {
335                                 if (range == 0 && ((Scenario) owner).isCheckedout()) {
336                                         icon = Constants.IMG_CHECKEDOUT;
337                                 }
338                                 range += 1;
339                         }
340                         icon = getIcon(step);
341                         item.icon(icon);
342                 }
343         }
344
345         /**
346          * Refresh the icon of the currently selected item.
347          */
348         public void refreshSelectedItem() {
349                 MenuItem item = this.getSelectedItem();
350                 item.icon(getIcon(_stopen));
351         }
352
353         /**
354          * Get the scenarioService.
355          * 
356          * @return the scenarioService
357          */
358         public ScenarioService getScenarioService() {
359                 return _scenarioService;
360         }
361
362         /**
363          * Set the scenarioService.
364          * 
365          * @param scenarioService
366          *            the scenarioService to set
367          */
368         public void setScenarioService(final ScenarioService scenarioService) {
369                 _scenarioService = scenarioService;
370         }
371
372         /**
373          * Get the projectElementService.
374          * 
375          * @return the projectElementService
376          */
377         public ProjectElementService getProjectElementService() {
378                 return _projectElementService;
379         }
380
381         /**
382          * Set the projectElementService.
383          * 
384          * @param projectElementService
385          *            the projectElementService to set
386          */
387         public void setProjectElementService(
388                         final ProjectElementService projectElementService) {
389                 _projectElementService = projectElementService;
390         }
391 }