Salome HOME
Modifications to respect PMD rules.
[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                 super.init();
94                 return this;
95         }
96
97         // ==============================================================================================================================
98         // Member functions
99         // ==============================================================================================================================
100
101         /** 
102          * {@inheritDoc}
103          * @see org.splat.wapp.Menu#selects(java.lang.String)
104          */
105         @Override
106         public void selects(final String name) {
107                 String[] parse = name.split("\\x2E");
108                 Scenario[] scenes = _study.getScenarii();
109                 Scenario scenew = _scopen;
110                 int askid = 0;
111
112                 // Initialization
113                 if (scenew == null && scenes.length == 1) {
114                         scenew = scenes[0];
115                 }
116                 try {
117                         int askdex = Integer.valueOf(parse[0]);
118                         if (askdex > 0) {
119                                 while (askid < scenes.length) {
120                                         if (scenes[askid].getIndex() == askdex) {
121                                                 break;
122                                         }
123                                         askid += 1;
124                                 }
125                                 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
126                         }
127                 } catch (Exception error) {
128                         return;
129                 }
130                 if (scenew == null) {
131                         openStudy(scenes);
132                 } else if (_scopen == null || !scenew.equals(_scopen)) {
133                         openScenario(scenew, scenes, askid, Integer.valueOf(parse[1]));
134                 } else {
135                         Step[] step = getProjectElementService().getSteps(_scopen);
136                         int selected = Integer.valueOf(parse[1]);
137                         for (int i = 0; i < step.length; i++) {
138                                 if (step[i].getNumber() != selected) {
139                                         continue;
140                                 }
141                                 _stopen = step[i];
142                                 break;
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                                 continue;
230                         }
231                         steps.add(0, newstep[i]);
232                 }
233                 for (int i = askid + 1; i < scenes.length; i++) {
234                         steps.add(getProjectElementService().getFirstStep(scenes[i]));
235                 }
236                 for (int i = 0; i < newstep.length; i++) {
237                         if (newstep[i].getNumber() <= last) {
238                                 continue;
239                         }
240                         steps.add(newstep[i]);
241                 }
242                 return steps;
243         }
244
245         /**
246          * Add a group of menu items according to the scenario content.
247          * 
248          * @param group
249          *            the scenario
250          * @param value
251          *            the scenario name
252          */
253         private void addItemsGroup(final Scenario group, final String value) {
254                 String icon;
255                 if (group.isCheckedout()) {
256                         icon = IMG_CHECKEDOUT;
257                 } else if (getScenarioService().isEmpty(group)) {
258                         icon = IMG_EMPTY;
259                         // else if (group.isFinished()) icon = IMG_CHECKED;
260                 } else {
261                         icon = IMG_DONE;
262                 }
263                 addGroup(value, group.getTitle(), icon, SELECTION_URL + value);
264         }
265
266         /**
267          * Study with several scenarii, non of them open. Collection of steps to be displayed.
268          * 
269          * @param scenes
270          *            study scenarii
271          */
272         private void openStudy(final Scenario[] scenes) {
273                 List<Step> steps = new ArrayList<Step>();
274                 Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
275
276                 int base = newstep[0].getNumber();
277                 int last = newstep[newstep.length - 1].getNumber();
278                 for (int i = 0; i < scenes.length; i++) {
279                         steps.add(getProjectElementService().getFirstStep(scenes[i]));
280                 }
281
282                 newstep = getProjectElementService().getSteps(_study);
283                 _stopen = newstep[0]; // Default selected step
284                 for (int i = newstep.length - 1; i > -1; i--) {
285                         if (newstep[i].getNumber() >= base) {
286                                 continue;
287                         }
288                         steps.add(0, newstep[i]);
289                 }
290                 for (int i = 0; i < newstep.length; i++) {
291                         if (newstep[i].getNumber() <= last) {
292                                 continue;
293                         }
294                         steps.add(newstep[i]);
295                 }
296                 // Creation of the menu
297                 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
298                         Step step = i.next();
299                         int number = step.getNumber();
300                         String icon;
301                         if (step.getOwner() instanceof Study) {
302                                 icon = getIcon(step);
303                                 addItem("0." + number, "menu.step." + number, icon,
304                                                 SELECTION_URL + "0." + number);
305                                 // WARNING: The selection number must end the action's parameters for the need of refreshGivenStepItem()
306                         } else {
307                                 Scenario group = (Scenario) step.getOwner();
308                                 long index = group.getIndex();
309                                 String value = index + "." + number;
310                                 addItemsGroup(group, value);
311                         }
312                 }
313         }
314
315         /**
316          * Get icon according to the step progress state.
317          * 
318          * @param step
319          *            the step
320          * @return the icon
321          */
322         private String getIcon(final Step step) {
323                 String icon;
324                 if (step.isStarted()) {
325                         if (step.isFinished()) {
326                                 icon = IMG_CHECKED;
327                         } else {
328                                 icon = IMG_DONE;
329                         }
330                 } else {
331                         icon = IMG_EMPTY;
332                 }
333                 return icon;
334         }
335
336         /**
337          * Refresh the given step's menu item.
338          * @param step the step to refresh
339          */
340         public void refreshGivenStepItem(final Step step) {
341                 String number = "." + step.getNumber();
342                 ProjectElement owner = step.getOwner();
343                 int range = 0;
344
345                 for (Iterator<MenuItem> action = _menu.iterator(); action.hasNext();) {
346                         MenuItem item = action.next();
347                         String index = item.getAction(); // Returns the above string ended by the selection number
348                         if (!index.endsWith(number)) {
349                                 continue;
350                         }
351
352                         String icon;
353                         if (owner instanceof Scenario) {
354                                 if (range == 0 && ((Scenario) owner).isCheckedout()) {
355                                         icon = IMG_CHECKEDOUT;
356                                 }
357                                 range += 1;
358                         }
359                         icon = getIcon(step);
360                         item.icon(icon);
361                 }
362         }
363
364         /**
365          * Refresh the icon of the currently selected item.
366          */
367         public void refreshSelectedItem() {
368                 MenuItem item = this.getSelectedItem();
369                 item.icon(getIcon(_stopen));
370         }
371
372         /**
373          * Get the scenarioService.
374          * 
375          * @return the scenarioService
376          */
377         public ScenarioService getScenarioService() {
378                 return _scenarioService;
379         }
380
381         /**
382          * Set the scenarioService.
383          * 
384          * @param scenarioService
385          *            the scenarioService to set
386          */
387         public void setScenarioService(final ScenarioService scenarioService) {
388                 _scenarioService = scenarioService;
389         }
390
391         /**
392          * Get the projectElementService.
393          * 
394          * @return the projectElementService
395          */
396         public ProjectElementService getProjectElementService() {
397                 return _projectElementService;
398         }
399
400         /**
401          * Set the projectElementService.
402          * 
403          * @param projectElementService
404          *            the projectElementService to set
405          */
406         public void setProjectElementService(
407                         final ProjectElementService projectElementService) {
408                 _projectElementService = projectElementService;
409         }
410 }