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) {
97 // ==============================================================================================================================
99 // ==============================================================================================================================
103 * @see org.splat.wapp.Menu#selects(java.lang.String)
106 public void selects(final String name) {
107 String[] parse = name.split("\\x2E");
108 Scenario[] scenes = _study.getScenarii();
109 Scenario scenew = _scopen;
113 if (scenew == null && scenes.length == 1) {
117 int askdex = Integer.valueOf(parse[0]);
119 while (askid < scenes.length) {
120 if (scenes[askid].getIndex() == askdex) {
125 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
127 } catch (Exception error) {
130 if (scenew == null) {
132 } else if (_scopen == null || !scenew.equals(_scopen)) {
133 openScenario(scenew, scenes, askid, Integer.valueOf(parse[1]));
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) {
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) {
231 steps.add(0, newstep[i]);
233 for (int i = askid + 1; i < scenes.length; i++) {
234 steps.add(getProjectElementService().getFirstStep(scenes[i]));
236 for (int i = 0; i < newstep.length; i++) {
237 if (newstep[i].getNumber() <= last) {
240 steps.add(newstep[i]);
246 * Add a group of menu items according to the scenario content.
253 private void addItemsGroup(final Scenario group, final String value) {
255 if (group.isCheckedout()) {
256 icon = IMG_CHECKEDOUT;
257 } else if (getScenarioService().isEmpty(group)) {
259 // else if (group.isFinished()) icon = IMG_CHECKED;
263 addGroup(value, group.getTitle(), icon, SELECTION_URL + value);
267 * Study with several scenarii, non of them open. Collection of steps to be displayed.
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
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]));
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) {
288 steps.add(0, newstep[i]);
290 for (int i = 0; i < newstep.length; i++) {
291 if (newstep[i].getNumber() <= last) {
294 steps.add(newstep[i]);
296 // Creation of the menu
297 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
298 Step step = i.next();
299 int number = step.getNumber();
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()
307 Scenario group = (Scenario) step.getOwner();
308 long index = group.getIndex();
309 String value = index + "." + number;
310 addItemsGroup(group, value);
316 * Get icon according to the step progress state.
322 private String getIcon(final Step step) {
324 if (step.isStarted()) {
325 if (step.isFinished()) {
337 * Refresh the given step's menu item.
338 * @param step the step to refresh
340 public void refreshGivenStepItem(final Step step) {
341 String number = "." + step.getNumber();
342 ProjectElement owner = step.getOwner();
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)) {
353 if (owner instanceof Scenario) {
354 if (range == 0 && ((Scenario) owner).isCheckedout()) {
355 icon = IMG_CHECKEDOUT;
359 icon = getIcon(step);
365 * Refresh the icon of the currently selected item.
367 public void refreshSelectedItem() {
368 MenuItem item = this.getSelectedItem();
369 item.icon(getIcon(_stopen));
373 * Get the scenarioService.
375 * @return the scenarioService
377 public ScenarioService getScenarioService() {
378 return _scenarioService;
382 * Set the scenarioService.
384 * @param scenarioService
385 * the scenarioService to set
387 public void setScenarioService(final ScenarioService scenarioService) {
388 _scenarioService = scenarioService;
392 * Get the projectElementService.
394 * @return the projectElementService
396 public ProjectElementService getProjectElementService() {
397 return _projectElementService;
401 * Set the projectElementService.
403 * @param projectElementService
404 * the projectElementService to set
406 public void setProjectElementService(
407 final ProjectElementService projectElementService) {
408 _projectElementService = projectElementService;