package org.splat.simer;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Iterator;
-import java.util.Vector;
+import java.util.List;
import org.splat.dal.bo.som.ProjectElement;
import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.Study;
import org.splat.service.ProjectElementService;
import org.splat.service.ScenarioService;
import org.splat.som.Step;
-import org.splat.dal.bo.som.Study;
import org.splat.wapp.MenuItem;
import org.splat.wapp.SlidMenu;
+/**
+ * Menu of a study.
+ */
public class StudyMenu extends SlidMenu {
- private Study study;
- private Scenario scopen; // Currently "open" scenario
- private Step stopen; // Currently selected step
+ /**
+ * Empty element icon.
+ */
+ private static final String IMG_EMPTY = "icon.empty.png";
+ /**
+ * Checked element icon.
+ */
+ private static final String IMG_CHECKED = "icon.checked.png";
+ /**
+ * Finished element icon.
+ */
+ private static final String IMG_DONE = "icon.done.png";
+ /**
+ * Checked out element icon.
+ */
+ private static final String IMG_CHECKEDOUT = "icon.checkedout.png";
+ /**
+ * Selection URL.
+ */
+ private static final String SELECTION_URL = "step-study?selection=";
+
+ /**
+ * Currently open study.
+ */
+ private transient Study _study = null;
+ /**
+ * Currently "open" scenario.
+ */
+ private transient Scenario _scopen = null;
+ /**
+ * Currently selected step.
+ */
+ private transient Step _stopen;
+
+ /**
+ * Scenario service.
+ */
private ScenarioService _scenarioService;
+ /**
+ * Project element service.
+ */
private ProjectElementService _projectElementService;
// ==============================================================================================================================
// Constructor
// ==============================================================================================================================
+ /**
+ * Default constructor.
+ */
public StudyMenu() {
super("activities", "study");
- scopen = null;
}
- public StudyMenu init(Study context) {
- study = context;
- scopen = null;
- init();
- return this;
+ /**
+ * Create a menu for the given study.
+ * @param context the study
+ */
+ public StudyMenu(final Study context) {
+ super("activities", "study");
+ _study = context;
}
- public StudyMenu(Study context) {
- // --------------------------------
- super("activities", "study");
- study = context;
- scopen = null;
+ /**
+ * Initialize the menu for the given study.
+ * @param context the study
+ * @return the study menu
+ */
+ public StudyMenu init(final Study context) {
+ _study = context;
+ super.init();
+ return this;
}
// ==============================================================================================================================
// Member functions
// ==============================================================================================================================
- public void selects(String name) {
- // ---------------------------------
+ /**
+ * {@inheritDoc}
+ * @see org.splat.wapp.Menu#selects(java.lang.String)
+ */
+ @Override
+ public void selects(final String name) {
String[] parse = name.split("\\x2E");
- Scenario[] scenes = study.getScenarii();
- Scenario scenew = scopen;
+ Scenario[] scenes = _study.getScenarii();
+ Scenario scenew = _scopen;
int askid = 0;
// Initialization
- if (scenew == null && scenes.length == 1)
+ if (scenew == null && scenes.length == 1) {
scenew = scenes[0];
+ }
try {
int askdex = Integer.valueOf(parse[0]);
if (askdex > 0) {
while (askid < scenes.length) {
- if (scenes[askid].getIndex() == askdex)
+ if (scenes[askid].getIndex() == askdex) {
break;
+ }
askid += 1;
}
scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
return;
}
if (scenew == null) {
-
- // Study with several scenarii, non of them open
- // Collection of steps to be displayed
- Vector<Step> steps = new Vector<Step>();
- Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
-
- int base = newstep[0].getNumber();
- int last = newstep[newstep.length - 1].getNumber();
- for (int i = 0; i < scenes.length; i++)
- steps.add(getProjectElementService().getFirstStep(scenes[i]));
-
- newstep = getProjectElementService().getSteps(study);
- stopen = newstep[0]; // Default selected step
- for (int i = newstep.length - 1; i > -1; i--) {
- if (newstep[i].getNumber() >= base)
- continue;
- steps.add(0, newstep[i]);
- }
- for (int i = 0; i < newstep.length; i++) {
- if (newstep[i].getNumber() <= last)
+ openStudy(scenes);
+ } else if (_scopen == null || !scenew.equals(_scopen)) {
+ openScenario(scenew, scenes, askid, Integer.valueOf(parse[1]));
+ } else {
+ Step[] step = getProjectElementService().getSteps(_scopen);
+ int selected = Integer.valueOf(parse[1]);
+ for (int i = 0; i < step.length; i++) {
+ if (step[i].getNumber() != selected) {
continue;
- steps.add(newstep[i]);
- }
- // Creation of the menu
- for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
- Step step = i.next();
- int number = step.getNumber();
- String icon;
- if (step.getOwner() instanceof Study) {
- if (!step.isStarted())
- icon = "icon.empty.png";
- else if (step.isFinished())
- icon = "icon.checked.png";
- else
- icon = "icon.done.png";
- addItem("0." + number, "menu.step." + number, icon,
- "step-study?selection=0." + number);
- // WARNING: The selection number must end the action's parameters for the need of refreshGivenStepItem()
- } else {
- Scenario group = (Scenario) step.getOwner();
- long index = group.getIndex();
- String value = index + "." + number;
- if (group.isCheckedout())
- icon = "icon.checkedout.png";
- else if (getScenarioService().isEmpty(group))
- icon = "icon.empty.png";
- // else if (group.isFinished()) icon = "icon.checked.png";
- else
- icon = "icon.done.png";
- addGroup(value, group.getTitle(), icon,
- "step-study?selection=" + value);
}
+ _stopen = step[i];
+ break;
}
- } else if (scopen == null || !scenew.equals(scopen)) {
+ }
+ super.selects(name);
+ }
- // Opening a scenario
- this.clear();
- // Collection of steps to be displayed
- Vector<Step> steps = new Vector<Step>();
- Step[] newstep = getProjectElementService().getSteps(scenew);
+ /**
+ * Open a scenario.
+ *
+ * @param scenew
+ * the scenario to open
+ * @param scenes
+ * study scenarii
+ * @param askid
+ * the last scenario index
+ * @param stepIndex
+ * the open step index
+ */
+ private void openScenario(final Scenario scenew, final Scenario[] scenes,
+ final int askid, final int stepIndex) {
- int base = newstep[0].getNumber();
- int last = newstep[newstep.length - 1].getNumber();
- for (int i = 0; i < newstep.length; i++) {
- steps.add(newstep[i]);
- }
- for (int i = askid - 1; i > -1; i--)
- steps
- .add(0, getProjectElementService().getFirstStep(
- scenes[i]));
- newstep = getProjectElementService().getSteps(study);
- for (int i = newstep.length - 1; i > -1; i--) {
- if (newstep[i].getNumber() >= base)
- continue;
- steps.add(0, newstep[i]);
- }
- for (int i = askid + 1; i < scenes.length; i++)
- steps.add(getProjectElementService().getFirstStep(scenes[i]));
- for (int i = 0; i < newstep.length; i++) {
- if (newstep[i].getNumber() <= last)
- continue;
- steps.add(newstep[i]);
+ // Opening a scenario
+ this.clear();
+ // Collection of steps to be displayed
+ List<Step> steps = getStepsToDisplay(scenew, scenes, askid);
+ // Creation of the menu
+ boolean first = true; // For differentiating the first scenario step
+ for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
+ Step step = i.next();
+ int number = step.getNumber();
+ String icon;
+ icon = getIcon(step);
+ if (number == stepIndex) {
+ _stopen = step;
}
- // Creation of the menu
- boolean first = true; // For differentiating the first scenario step
- int askdex = Integer.valueOf(parse[1]);
- for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
- Step step = i.next();
- int number = step.getNumber();
- String icon;
- if (!step.isStarted())
- icon = "icon.empty.png";
- else if (step.isFinished())
- icon = "icon.checked.png";
- else
- icon = "icon.done.png";
- if (number == askdex)
- stopen = step;
- if (step.getOwner() instanceof Study) {
- addItem("0." + number, "menu.step." + number, icon,
- "step-study?selection=0." + number);
- } else {
- Scenario group = (Scenario) step.getOwner();
- long index = group.getIndex();
- String value = index + "." + number;
- if (index != scenew.getIndex()) {
- if (group.isCheckedout())
- icon = "icon.checkedout.png";
- else if (getScenarioService().isEmpty(group))
- icon = "icon.empty.png";
- // else if (group.isFinished()) icon = "icon.checked.png";
- else
- icon = "icon.done.png";
- addGroup(value, group.getTitle(), icon,
- "step-study?selection=" + value);
- } else if (first) {
- if (group.isCheckedout())
- icon = "icon.checkedout.png";
- addGroup(value, scenew.getTitle(), icon,
- "step-study?selection=" + value);
+ if (step.getOwner() instanceof Study) {
+ addItem("0." + number, "menu.step." + number, icon,
+ SELECTION_URL + "0." + number);
+ } else {
+ Scenario group = (Scenario) step.getOwner();
+ long index = group.getIndex();
+ String value = index + "." + number;
+ if (index == scenew.getIndex()) {
+ if (first) {
+ if (group.isCheckedout()) {
+ icon = IMG_CHECKEDOUT;
+ }
+ addGroup(value, scenew.getTitle(), icon, SELECTION_URL
+ + value);
first = false;
} else {
addSubItem(value, "menu.step." + number, icon,
- "step-study?selection=" + value);
+ SELECTION_URL + value);
}
+ } else {
+ addItemsGroup(group, value);
}
}
- scopen = scenew;
+ }
+ _scopen = scenew;
+ }
+
+ /**
+ * Get list of steps to display.
+ *
+ * @param scenew
+ * the scenario to open
+ * @param scenes
+ * study scenarii
+ * @param askid
+ * the last scenario index
+ * @return list of steps
+ */
+ private List<Step> getStepsToDisplay(final Scenario scenew,
+ final Scenario[] scenes, final int askid) {
+ List<Step> steps = new ArrayList<Step>();
+ Step[] newstep = getProjectElementService().getSteps(scenew);
+
+ int base = newstep[0].getNumber();
+ int last = newstep[newstep.length - 1].getNumber();
+ steps.addAll(Arrays.asList(newstep));
+ for (int i = askid - 1; i > -1; i--) {
+ steps.add(0, getProjectElementService().getFirstStep(scenes[i]));
+ }
+ newstep = getProjectElementService().getSteps(_study);
+ for (int i = newstep.length - 1; i > -1; i--) {
+ if (newstep[i].getNumber() >= base) {
+ continue;
+ }
+ steps.add(0, newstep[i]);
+ }
+ for (int i = askid + 1; i < scenes.length; i++) {
+ steps.add(getProjectElementService().getFirstStep(scenes[i]));
+ }
+ for (int i = 0; i < newstep.length; i++) {
+ if (newstep[i].getNumber() <= last) {
+ continue;
+ }
+ steps.add(newstep[i]);
+ }
+ return steps;
+ }
+
+ /**
+ * Add a group of menu items according to the scenario content.
+ *
+ * @param group
+ * the scenario
+ * @param value
+ * the scenario name
+ */
+ private void addItemsGroup(final Scenario group, final String value) {
+ String icon;
+ if (group.isCheckedout()) {
+ icon = IMG_CHECKEDOUT;
+ } else if (getScenarioService().isEmpty(group)) {
+ icon = IMG_EMPTY;
+ // else if (group.isFinished()) icon = IMG_CHECKED;
} else {
- Step[] step = getProjectElementService().getSteps(scopen);
- int selected = Integer.valueOf(parse[1]);
- for (int i = 0; i < step.length; i++) {
- if (step[i].getNumber() != selected)
- continue;
- stopen = step[i];
- break;
+ icon = IMG_DONE;
+ }
+ addGroup(value, group.getTitle(), icon, SELECTION_URL + value);
+ }
+
+ /**
+ * Study with several scenarii, non of them open. Collection of steps to be displayed.
+ *
+ * @param scenes
+ * study scenarii
+ */
+ private void openStudy(final Scenario[] scenes) {
+ List<Step> steps = new ArrayList<Step>();
+ Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
+
+ int base = newstep[0].getNumber();
+ int last = newstep[newstep.length - 1].getNumber();
+ for (int i = 0; i < scenes.length; i++) {
+ steps.add(getProjectElementService().getFirstStep(scenes[i]));
+ }
+
+ newstep = getProjectElementService().getSteps(_study);
+ _stopen = newstep[0]; // Default selected step
+ for (int i = newstep.length - 1; i > -1; i--) {
+ if (newstep[i].getNumber() >= base) {
+ continue;
+ }
+ steps.add(0, newstep[i]);
+ }
+ for (int i = 0; i < newstep.length; i++) {
+ if (newstep[i].getNumber() <= last) {
+ continue;
+ }
+ steps.add(newstep[i]);
+ }
+ // Creation of the menu
+ for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
+ Step step = i.next();
+ int number = step.getNumber();
+ String icon;
+ if (step.getOwner() instanceof Study) {
+ icon = getIcon(step);
+ addItem("0." + number, "menu.step." + number, icon,
+ SELECTION_URL + "0." + number);
+ // WARNING: The selection number must end the action's parameters for the need of refreshGivenStepItem()
+ } else {
+ Scenario group = (Scenario) step.getOwner();
+ long index = group.getIndex();
+ String value = index + "." + number;
+ addItemsGroup(group, value);
}
}
- super.selects(name);
}
- public void refreshGivenStepItem(Step step) {
- // --------------------------------------------
+ /**
+ * Get icon according to the step progress state.
+ *
+ * @param step
+ * the step
+ * @return the icon
+ */
+ private String getIcon(final Step step) {
+ String icon;
+ if (step.isStarted()) {
+ if (step.isFinished()) {
+ icon = IMG_CHECKED;
+ } else {
+ icon = IMG_DONE;
+ }
+ } else {
+ icon = IMG_EMPTY;
+ }
+ return icon;
+ }
+
+ /**
+ * Refresh the given step's menu item.
+ * @param step the step to refresh
+ */
+ public void refreshGivenStepItem(final Step step) {
String number = "." + step.getNumber();
ProjectElement owner = step.getOwner();
int range = 0;
for (Iterator<MenuItem> action = _menu.iterator(); action.hasNext();) {
MenuItem item = action.next();
String index = item.getAction(); // Returns the above string ended by the selection number
- if (!index.endsWith(number))
+ if (!index.endsWith(number)) {
continue;
+ }
String icon;
if (owner instanceof Scenario) {
- if (range == 0 && ((Scenario) owner).isCheckedout())
- icon = "icon.checkedout.png";
+ if (range == 0 && ((Scenario) owner).isCheckedout()) {
+ icon = IMG_CHECKEDOUT;
+ }
range += 1;
}
- if (!step.isStarted())
- icon = "icon.empty.png";
- else if (step.isFinished())
- icon = "icon.checked.png";
- else
- icon = "icon.done.png";
+ icon = getIcon(step);
item.icon(icon);
}
}
+ /**
+ * Refresh the icon of the currently selected item.
+ */
public void refreshSelectedItem() {
- // ----------------------------------
MenuItem item = this.getSelectedItem();
- String icon;
- if (!stopen.isStarted())
- icon = "icon.empty.png";
- else if (stopen.isFinished())
- icon = "icon.checked.png";
- else
- icon = "icon.done.png";
- item.icon(icon);
+ item.icon(getIcon(_stopen));
}
/**
* @param scenarioService
* the scenarioService to set
*/
- public void setScenarioService(ScenarioService scenarioService) {
+ public void setScenarioService(final ScenarioService scenarioService) {
_scenarioService = scenarioService;
}
* the projectElementService to set
*/
public void setProjectElementService(
- ProjectElementService projectElementService) {
+ final ProjectElementService projectElementService) {
_projectElementService = projectElementService;
}
}
\ No newline at end of file