1 package org.splat.simer;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
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;
21 public class StudyMenu extends SlidMenu {
26 private static final String SELECTION_URL = "step-study?selection=";
29 * Currently open study.
31 private transient Study _study = null;
33 * Currently "open" scenario.
35 private transient Scenario _scopen = null;
37 * Currently selected step.
39 private transient Step _stopen;
44 private ScenarioService _scenarioService;
46 * Project element service.
48 private ProjectElementService _projectElementService;
50 // ==============================================================================================================================
52 // ==============================================================================================================================
55 * Default constructor.
58 super("activities", Constants.STUDY_MENU);
62 * Create a menu for the given study.
63 * @param context the study
65 public StudyMenu(final Study context) {
66 super("activities", Constants.STUDY_MENU);
71 * Initialize the menu for the given study.
72 * @param context the study
73 * @return the study menu
75 public StudyMenu init(final Study context) {
82 // ==============================================================================================================================
84 // ==============================================================================================================================
88 * @see org.splat.wapp.Menu#selects(java.lang.String)
91 public void selects(final String name) {
92 String[] parse = name.split("\\x2E");
93 Scenario[] scenes = _study.getScenarii();
94 Scenario scenew = _scopen;
98 if (scenew == null && scenes.length == 1) {
102 int askdex = Integer.valueOf(parse[0]);
104 while (askid < scenes.length) {
105 if (scenes[askid].getIndex() == askdex) {
110 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
112 } catch (Exception error) {
115 if (scenew == null) {
117 } else if (_scopen == null || !scenew.equals(_scopen)) {
118 openScenario(scenew, scenes, askid, Integer.valueOf(parse[1]));
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) {
136 * the scenario to open
140 * the last scenario index
142 * the open step index
144 private void openScenario(final Scenario scenew, final Scenario[] scenes,
145 final int askid, final int stepIndex) {
147 // Opening a scenario
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();
157 icon = getIcon(step);
158 if (number == stepIndex) {
161 if (step.getOwner() instanceof Study) {
162 addItem("0." + number, "menu.step." + number, icon,
163 SELECTION_URL + "0." + number);
165 Scenario group = (Scenario) step.getOwner();
166 long index = group.getIndex();
167 String value = index + "." + number;
168 if (index == scenew.getIndex()) {
170 if (group.isCheckedout()) {
171 icon = Constants.IMG_CHECKEDOUT;
173 addGroup(value, scenew.getTitle(), icon, SELECTION_URL
177 addSubItem(value, "menu.step." + number, icon,
178 SELECTION_URL + value);
181 addItemsGroup(group, value);
189 * Get list of steps to display.
192 * the scenario to open
196 * the last scenario index
197 * @return list of steps
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);
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]));
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]);
216 for (int i = askid + 1; i < scenes.length; i++) {
217 steps.add(getProjectElementService().getFirstStep(scenes[i]));
219 for (int i = 0; i < newstep.length; i++) {
220 if (newstep[i].getNumber() > last) {
221 steps.add(newstep[i]);
228 * Add a group of menu items according to the scenario content.
235 private void addItemsGroup(final Scenario group, final String value) {
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;
243 icon = Constants.IMG_DONE;
245 addGroup(value, group.getTitle(), icon, SELECTION_URL + value);
249 * Study with several scenarii, non of them open. Collection of steps to be displayed.
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
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]));
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]);
271 for (int i = 0; i < newstep.length; i++) {
272 if (newstep[i].getNumber() > last) {
273 steps.add(newstep[i]);
276 // Creation of the menu
278 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
279 Step step = i.next();
280 int number = step.getNumber();
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()
288 Scenario group = (Scenario) step.getOwner();
289 long index = group.getIndex();
290 String value = index + "." + number;
291 addItemsGroup(group, value);
297 * Get icon according to the step progress state.
303 private String getIcon(final Step step) {
305 if (step.isStarted()) {
306 if (step.isFinished()) {
307 icon = Constants.IMG_CHECKED;
309 icon = Constants.IMG_DONE;
312 icon = Constants.IMG_EMPTY;
318 * Refresh the given step's menu item.
319 * @param step the step to refresh
321 public void refreshGivenStepItem(final Step step) {
322 String number = "." + step.getNumber();
323 ProjectElement owner = step.getOwner();
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)) {
334 if (owner instanceof Scenario) {
335 if (range == 0 && ((Scenario) owner).isCheckedout()) {
336 icon = Constants.IMG_CHECKEDOUT;
340 icon = getIcon(step);
346 * Refresh the icon of the currently selected item.
348 public void refreshSelectedItem() {
349 MenuItem item = this.getSelectedItem();
350 item.icon(getIcon(_stopen));
354 * Get the scenarioService.
356 * @return the scenarioService
358 public ScenarioService getScenarioService() {
359 return _scenarioService;
363 * Set the scenarioService.
365 * @param scenarioService
366 * the scenarioService to set
368 public void setScenarioService(final ScenarioService scenarioService) {
369 _scenarioService = scenarioService;
373 * Get the projectElementService.
375 * @return the projectElementService
377 public ProjectElementService getProjectElementService() {
378 return _projectElementService;
382 * Set the projectElementService.
384 * @param projectElementService
385 * the projectElementService to set
387 public void setProjectElementService(
388 final ProjectElementService projectElementService) {
389 _projectElementService = projectElementService;