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