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 IMG_EMPTY = "icon.empty.png";
28 * Checked element icon.
30 private static final String IMG_CHECKED = "icon.checked.png";
32 * Finished element icon.
34 private static final String IMG_DONE = "icon.done.png";
36 * Checked out element icon.
38 private static final String IMG_CHECKEDOUT = "icon.checkedout.png";
42 private static final String SELECTION_URL = "step-study?selection=";
45 * Currently open study.
47 private transient Study _study = null;
49 * Currently "open" scenario.
51 private transient Scenario _scopen = null;
53 * Currently selected step.
55 private transient Step _stopen;
60 private ScenarioService _scenarioService;
62 * Project element service.
64 private ProjectElementService _projectElementService;
66 // ==============================================================================================================================
68 // ==============================================================================================================================
71 * Default constructor.
74 super("activities", Constants.STUDY_MENU);
78 * Create a menu for the given study.
79 * @param context the study
81 public StudyMenu(final Study context) {
82 super("activities", Constants.STUDY_MENU);
87 * Initialize the menu for the given study.
88 * @param context the study
89 * @return the study menu
91 public StudyMenu init(final Study context) {
98 // ==============================================================================================================================
100 // ==============================================================================================================================
104 * @see org.splat.wapp.Menu#selects(java.lang.String)
107 public void selects(final String name) {
108 String[] parse = name.split("\\x2E");
109 Scenario[] scenes = _study.getScenarii();
110 Scenario scenew = _scopen;
114 if (scenew == null && scenes.length == 1) {
118 int askdex = Integer.valueOf(parse[0]);
120 while (askid < scenes.length) {
121 if (scenes[askid].getIndex() == askdex) {
126 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
128 } catch (Exception error) {
131 if (scenew == null) {
133 } else if (_scopen == null || !scenew.equals(_scopen)) {
134 openScenario(scenew, scenes, askid, Integer.valueOf(parse[1]));
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) {
152 * the scenario to open
156 * the last scenario index
158 * the open step index
160 private void openScenario(final Scenario scenew, final Scenario[] scenes,
161 final int askid, final int stepIndex) {
163 // Opening a scenario
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();
173 icon = getIcon(step);
174 if (number == stepIndex) {
177 if (step.getOwner() instanceof Study) {
178 addItem("0." + number, "menu.step." + number, icon,
179 SELECTION_URL + "0." + number);
181 Scenario group = (Scenario) step.getOwner();
182 long index = group.getIndex();
183 String value = index + "." + number;
184 if (index == scenew.getIndex()) {
186 if (group.isCheckedout()) {
187 icon = IMG_CHECKEDOUT;
189 addGroup(value, scenew.getTitle(), icon, SELECTION_URL
193 addSubItem(value, "menu.step." + number, icon,
194 SELECTION_URL + value);
197 addItemsGroup(group, value);
205 * Get list of steps to display.
208 * the scenario to open
212 * the last scenario index
213 * @return list of steps
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);
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]));
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]);
232 for (int i = askid + 1; i < scenes.length; i++) {
233 steps.add(getProjectElementService().getFirstStep(scenes[i]));
235 for (int i = 0; i < newstep.length; i++) {
236 if (newstep[i].getNumber() > last) {
237 steps.add(newstep[i]);
244 * Add a group of menu items according to the scenario content.
251 private void addItemsGroup(final Scenario group, final String value) {
253 if (group.isCheckedout()) {
254 icon = IMG_CHECKEDOUT;
255 } else if (getScenarioService().isEmpty(group)) {
257 // else if (group.isFinished()) icon = IMG_CHECKED;
261 addGroup(value, group.getTitle(), icon, SELECTION_URL + value);
265 * Study with several scenarii, non of them open. Collection of steps to be displayed.
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
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]));
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]);
287 for (int i = 0; i < newstep.length; i++) {
288 if (newstep[i].getNumber() > last) {
289 steps.add(newstep[i]);
292 // Creation of the menu
294 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
295 Step step = i.next();
296 int number = step.getNumber();
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()
304 Scenario group = (Scenario) step.getOwner();
305 long index = group.getIndex();
306 String value = index + "." + number;
307 addItemsGroup(group, value);
313 * Get icon according to the step progress state.
319 private String getIcon(final Step step) {
321 if (step.isStarted()) {
322 if (step.isFinished()) {
334 * Refresh the given step's menu item.
335 * @param step the step to refresh
337 public void refreshGivenStepItem(final Step step) {
338 String number = "." + step.getNumber();
339 ProjectElement owner = step.getOwner();
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)) {
350 if (owner instanceof Scenario) {
351 if (range == 0 && ((Scenario) owner).isCheckedout()) {
352 icon = IMG_CHECKEDOUT;
356 icon = getIcon(step);
362 * Refresh the icon of the currently selected item.
364 public void refreshSelectedItem() {
365 MenuItem item = this.getSelectedItem();
366 item.icon(getIcon(_stopen));
370 * Get the scenarioService.
372 * @return the scenarioService
374 public ScenarioService getScenarioService() {
375 return _scenarioService;
379 * Set the scenarioService.
381 * @param scenarioService
382 * the scenarioService to set
384 public void setScenarioService(final ScenarioService scenarioService) {
385 _scenarioService = scenarioService;
389 * Get the projectElementService.
391 * @return the projectElementService
393 public ProjectElementService getProjectElementService() {
394 return _projectElementService;
398 * Set the projectElementService.
400 * @param projectElementService
401 * the projectElementService to set
403 public void setProjectElementService(
404 final ProjectElementService projectElementService) {
405 _projectElementService = projectElementService;