1 package org.splat.simer;
3 import java.util.ArrayList;
4 import java.util.Iterator;
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;
16 public class NewScenarioMenu extends SlidMenu {
19 * Currently "open" study.
21 private transient Study _study;
23 * Currently "open" scenario.
25 private transient Scenario _scopen;
27 * Currently selected step.
29 private transient Step _stopen;
31 * Injected project element service.
33 private ProjectElementService _projectElementService;
35 * Injected scenario service.
37 private ScenarioService _scenarioService;
39 // ==============================================================================================================================
41 // ==============================================================================================================================
43 public NewScenarioMenu() {
44 super("scenarii", "study");
48 public NewScenarioMenu init(final Study context) {
55 public NewScenarioMenu(final Study context) {
56 super("scenarii", "study");
61 // ==============================================================================================================================
63 // ==============================================================================================================================
66 public void selects(final String name) {
67 String[] parse = name.split("\\x2E");
68 Scenario[] scenes = _study.getScenarii();
69 Scenario scenew = _scopen;
73 if (scenew == null && scenes.length == 1) {
77 int askdex = Integer.valueOf(parse[0]);
79 while (askid < scenes.length) {
80 if (scenes[askid].getIndex() == askdex) {
85 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
87 } catch (Exception error) {
92 // Study with several scenarii, non of them open
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
98 for (int i = 0; i < scenes.length; i++) {
99 steps.add(getProjectElementService().getFirstStep(scenes[i]));
101 newstep = getProjectElementService().getSteps(_study);
102 _stopen = newstep[0]; // Default selected step
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;
112 if (getScenarioService().isEmpty(group)) {
113 icon = Constants.IMG_EMPTY;
114 // else if (group.isFinished()) icon = Constants.IMG_CHECKED;
116 icon = Constants.IMG_DONE;
118 addGroup(value, group.getTitle(), icon, getSelectStepUrl(value));
120 } else if (_scopen == null || !scenew.equals(_scopen)) {
122 // Opening a scenario
124 // Collection of steps to be displayed
125 List<Step> steps = new ArrayList<Step>();
126 Step[] newstep = getProjectElementService().getSteps(scenew);
128 for (int i = 0; i < newstep.length; i++) {
129 steps.add(newstep[i]);
131 for (int i = askid - 1; i > -1; i--) {
133 .add(0, getProjectElementService().getFirstStep(
136 newstep = getProjectElementService().getSteps(_study);
137 for (int i = askid + 1; i < scenes.length; i++) {
138 steps.add(getProjectElementService().getFirstStep(scenes[i]));
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();
148 if (step.isStarted()) {
149 if (step.isFinished()) {
150 icon = Constants.IMG_CHECKED;
152 icon = Constants.IMG_DONE;
155 icon = Constants.IMG_EMPTY;
157 if (number == askdex) {
160 Scenario group = (Scenario) step.getOwner();
161 long index = group.getIndex();
162 String value = index + "." + number;
163 if (index == scenew.getIndex()) {
165 addGroup(value, scenew.getTitle(), icon,
166 getSelectStepUrl(value));
169 addSubItem(value, "menu.step." + number, icon,
170 getSelectStepUrl(value));
173 if (getScenarioService().isEmpty(group)) {
174 icon = Constants.IMG_EMPTY;
175 // else if (group.isFinished()) icon = Constants.IMG_CHECKED;
177 icon = Constants.IMG_DONE;
179 addGroup(value, group.getTitle(), icon,
180 getSelectStepUrl(value));
189 * Build URL for selecting a study step.
193 * @return URL relative to the web application context, i.e. the part after http://host/siman/
195 private String getSelectStepUrl(final String value) {
196 return "select-step?selection=" + value + "&title=%{title}";
200 * Refresh the icon of the selected menu item.
202 public void refreshSelectedItem() {
203 MenuItem item = this.getSelectedItem();
205 if (_stopen.isStarted()) {
206 if (_stopen.isFinished()) {
207 icon = Constants.IMG_CHECKED;
209 icon = Constants.IMG_DONE;
212 icon = Constants.IMG_EMPTY;
218 * Get the scenarioService.
220 * @return the scenarioService
222 public ScenarioService getScenarioService() {
223 return _scenarioService;
227 * Set the scenarioService.
229 * @param scenarioService
230 * the scenarioService to set
232 public void setScenarioService(final ScenarioService scenarioService) {
233 _scenarioService = scenarioService;
237 * Get the projectElementService.
239 * @return the projectElementService
241 public ProjectElementService getProjectElementService() {
242 return _projectElementService;
246 * Set the projectElementService.
248 * @param projectElementService
249 * the projectElementService to set
251 public void setProjectElementService(
252 final ProjectElementService projectElementService) {
253 _projectElementService = projectElementService;