Salome HOME
Show URLs for previous versions of the document's files.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewScenarioMenu.java
1 package org.splat.simer;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.splat.dal.bo.som.Scenario;
8 import org.splat.dal.bo.som.Study;
9 import org.splat.service.ProjectElementService;
10 import org.splat.service.ScenarioService;
11 import org.splat.som.Step;
12 import org.splat.wapp.Constants;
13 import org.splat.wapp.MenuItem;
14 import org.splat.wapp.SlidMenu;
15
16 public class NewScenarioMenu extends SlidMenu {
17
18         /**
19          * Currently "open" study.
20          */
21         private transient Study _study;
22         /**
23          * Currently "open" scenario.
24          */
25         private transient Scenario _scopen;
26         /**
27          * Currently selected step.
28          */
29         private transient Step _stopen;
30         /**
31          * Injected project element service.
32          */
33         private ProjectElementService _projectElementService;
34         /**
35          * Injected scenario service.
36          */
37         private ScenarioService _scenarioService;
38
39         // ==============================================================================================================================
40         // Constructor
41         // ==============================================================================================================================
42
43         public NewScenarioMenu() {
44                 super("scenarii", "study");
45                 _scopen = null;
46         }
47
48         public NewScenarioMenu init(final Study context) {
49                 _study = context;
50                 _scopen = null;
51                 init();
52                 return this;
53         }
54
55         public NewScenarioMenu(final Study context) {
56                 super("scenarii", "study");
57                 _study = context;
58                 _scopen = null;
59         }
60
61         // ==============================================================================================================================
62         // Member functions
63         // ==============================================================================================================================
64
65         @Override
66         public void selects(final String name) {
67                 String[] parse = name.split("\\x2E");
68                 Scenario[] scenes = _study.getScenarii();
69                 Scenario scenew = _scopen;
70                 int askid = 0;
71
72                 // Initialization
73                 if (scenew == null && scenes.length == 1) {
74                         scenew = scenes[0];
75                 }
76                 try {
77                         int askdex = Integer.valueOf(parse[0]);
78                         if (askdex > 0) {
79                                 while (askid < scenes.length) {
80                                         if (scenes[askid].getIndex() == askdex) {
81                                                 break;
82                                         }
83                                         askid += 1;
84                                 }
85                                 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
86                         }
87                 } catch (Exception error) {
88                         return;
89                 }
90                 if (scenew == null) {
91
92                         // Study with several scenarii, non of them open
93
94                         // Collection of steps to be displayed
95                         List<Step> steps = new ArrayList<Step>();
96                         Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
97
98                         for (int i = 0; i < scenes.length; i++) {
99                                 steps.add(getProjectElementService().getFirstStep(scenes[i]));
100                         }
101                         newstep = getProjectElementService().getSteps(_study);
102                         _stopen = newstep[0]; // Default selected step
103
104                         // Creation of the menu
105                         for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
106                                 Step step = i.next();
107                                 int number = step.getNumber();
108                                 Scenario group = (Scenario) step.getOwner(); // The menu includes first scenario steps only
109                                 long index = group.getIndex();
110                                 String value = index + "." + number;
111                                 String icon;
112                                 if (getScenarioService().isEmpty(group)) {
113                                         icon = Constants.IMG_EMPTY;
114                                         // else if (group.isFinished()) icon = Constants.IMG_CHECKED;
115                                 } else {
116                                         icon = Constants.IMG_DONE;
117                                 }
118                                 addGroup(value, group.getTitle(), icon, getSelectStepUrl(value));
119                         }
120                 } else if (_scopen == null || !scenew.equals(_scopen)) {
121
122                         // Opening a scenario
123                         this.clear();
124                         // Collection of steps to be displayed
125                         List<Step> steps = new ArrayList<Step>();
126                         Step[] newstep = getProjectElementService().getSteps(scenew);
127
128                         for (int i = 0; i < newstep.length; i++) {
129                                 steps.add(newstep[i]);
130                         }
131                         for (int i = askid - 1; i > -1; i--) {
132                                 steps
133                                                 .add(0, getProjectElementService().getFirstStep(
134                                                                 scenes[i]));
135                         }
136                         newstep = getProjectElementService().getSteps(_study);
137                         for (int i = askid + 1; i < scenes.length; i++) {
138                                 steps.add(getProjectElementService().getFirstStep(scenes[i]));
139                         }
140
141                         // Creation of the menu
142                         boolean first = true; // For differentiating the first scenario step
143                         int askdex = Integer.valueOf(parse[1]);
144                         for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
145                                 Step step = i.next();
146                                 int number = step.getNumber();
147                                 String icon;
148                                 if (step.isStarted()) {
149                                         if (step.isFinished()) {
150                                                 icon = Constants.IMG_CHECKED;
151                                         } else {
152                                                 icon = Constants.IMG_DONE;
153                                         }
154                                 } else {
155                                         icon = Constants.IMG_EMPTY;
156                                 }
157                                 if (number == askdex) {
158                                         _stopen = step;
159                                 }
160                                 Scenario group = (Scenario) step.getOwner();
161                                 long index = group.getIndex();
162                                 String value = index + "." + number;
163                                 if (index == scenew.getIndex()) {
164                                         if (first) {
165                                                 addGroup(value, scenew.getTitle(), icon,
166                                                                 getSelectStepUrl(value));
167                                                 first = false;
168                                         } else {
169                                                 addSubItem(value, "menu.step." + number, icon,
170                                                                 getSelectStepUrl(value));
171                                         }
172                                 } else {
173                                         if (getScenarioService().isEmpty(group)) {
174                                                 icon = Constants.IMG_EMPTY;
175                                                 // else if (group.isFinished()) icon = Constants.IMG_CHECKED;
176                                         } else {
177                                                 icon = Constants.IMG_DONE;
178                                         }
179                                         addGroup(value, group.getTitle(), icon,
180                                                         getSelectStepUrl(value));
181                                 }
182                         }
183                         _scopen = scenew;
184                 }
185                 super.selects(name);
186         }
187
188         /**
189          * Build URL for selecting a study step.
190          * 
191          * @param value
192          *            selection
193          * @return URL relative to the web application context, i.e. the part after http://host/siman/
194          */
195         private String getSelectStepUrl(final String value) {
196                 return "select-step?selection=" + value + "&title=%{title}";
197         }
198
199         /**
200          * Refresh the icon of the selected menu item.
201          */
202         public void refreshSelectedItem() {
203                 MenuItem item = this.getSelectedItem();
204                 String icon;
205                 if (_stopen.isStarted()) {
206                         if (_stopen.isFinished()) {
207                                 icon = Constants.IMG_CHECKED;
208                         } else {
209                                 icon = Constants.IMG_DONE;
210                         }
211                 } else {
212                         icon = Constants.IMG_EMPTY;
213                 }
214                 item.icon(icon);
215         }
216
217         /**
218          * Get the scenarioService.
219          * 
220          * @return the scenarioService
221          */
222         public ScenarioService getScenarioService() {
223                 return _scenarioService;
224         }
225
226         /**
227          * Set the scenarioService.
228          * 
229          * @param scenarioService
230          *            the scenarioService to set
231          */
232         public void setScenarioService(final ScenarioService scenarioService) {
233                 _scenarioService = scenarioService;
234         }
235
236         /**
237          * Get the projectElementService.
238          * 
239          * @return the projectElementService
240          */
241         public ProjectElementService getProjectElementService() {
242                 return _projectElementService;
243         }
244
245         /**
246          * Set the projectElementService.
247          * 
248          * @param projectElementService
249          *            the projectElementService to set
250          */
251         public void setProjectElementService(
252                         final ProjectElementService projectElementService) {
253                 _projectElementService = projectElementService;
254         }
255 }