Salome HOME
Some PMD rules are satisfied.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewScenarioMenu.java
index a741099a1ef12e6191397993cd558584aa2b62e5..572335063b5a3fe491cdaac07a2cdc458af85e01 100644 (file)
@@ -1,55 +1,85 @@
 package org.splat.simer;
 
+import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.Vector;
+import java.util.List;
 
 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.Constants;
 import org.splat.wapp.MenuItem;
 import org.splat.wapp.SlidMenu;
 
 public class NewScenarioMenu extends SlidMenu {
 
-       private Study study;
-       private Scenario scopen; // Currently "open" scenario
-       private Step stopen; // Currently selected step
+       /**
+        * Currently "open" study.
+        */
+       private transient Study _study;
+       /**
+        * Currently "open" scenario.
+        */
+       private transient Scenario _scopen;
+       /**
+        * Currently selected step.
+        */
+       private transient Step _stopen;
+       /**
+        * Injected project element service.
+        */
        private ProjectElementService _projectElementService;
+       /**
+        * Injected scenario service.
+        */
        private ScenarioService _scenarioService;
 
        // ==============================================================================================================================
        // Constructor
        // ==============================================================================================================================
 
-       public NewScenarioMenu(Study context) {
-               // -----------------------------------
+       public NewScenarioMenu() {
                super("scenarii", "study");
-               study = context;
-               scopen = null;
+               _scopen = null;
+       }
+
+       public NewScenarioMenu init(final Study context) {
+               _study = context;
+               _scopen = null;
+               init();
+               return this;
+       }
+
+       public NewScenarioMenu(final Study context) {
+               super("scenarii", "study");
+               _study = context;
+               _scopen = null;
        }
 
        // ==============================================================================================================================
        // Member functions
        // ==============================================================================================================================
 
-       public void selects(String name) {
-               // ---------------------------------
+       @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)
@@ -62,13 +92,14 @@ public class NewScenarioMenu extends SlidMenu {
                        // Study with several scenarii, non of them open
 
                        // Collection of steps to be displayed
-                       Vector<Step> steps = new Vector<Step>();
+                       List<Step> steps = new ArrayList<Step>();
                        Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
 
-                       for (int i = 0; i < scenes.length; i++)
+                       for (int i = 0; i < scenes.length; i++) {
                                steps.add(getProjectElementService().getFirstStep(scenes[i]));
-                       newstep = getProjectElementService().getSteps(study);
-                       stopen = newstep[0]; // Default selected step
+                       }
+                       newstep = getProjectElementService().getSteps(_study);
+                       _stopen = newstep[0]; // Default selected step
 
                        // Creation of the menu
                        for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
@@ -78,30 +109,34 @@ public class NewScenarioMenu extends SlidMenu {
                                long index = group.getIndex();
                                String value = index + "." + number;
                                String icon;
-                               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,
-                                               "select-step?selection=" + value + "&title=%{title}");
+                               if (getScenarioService().isEmpty(group)) {
+                                       icon = Constants.IMG_EMPTY;
+                                       // else if (group.isFinished()) icon = Constants.IMG_CHECKED;
+                               } else {
+                                       icon = Constants.IMG_DONE;
+                               }
+                               addGroup(value, group.getTitle(), icon, getSelectStepUrl(value));
                        }
-               } else if (scopen == null || !scenew.equals(scopen)) {
+               } else if (_scopen == null || !scenew.equals(_scopen)) {
 
                        // Opening a scenario
                        this.clear();
                        // Collection of steps to be displayed
-                       Vector<Step> steps = new Vector<Step>();
+                       List<Step> steps = new ArrayList<Step>();
                        Step[] newstep = getProjectElementService().getSteps(scenew);
 
                        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 = askid + 1; i < scenes.length; i++)
+                       for (int i = askid - 1; i > -1; i--) {
+                               steps
+                                               .add(0, getProjectElementService().getFirstStep(
+                                                               scenes[i]));
+                       }
+                       newstep = getProjectElementService().getSteps(_study);
+                       for (int i = askid + 1; i < scenes.length; i++) {
                                steps.add(getProjectElementService().getFirstStep(scenes[i]));
+                       }
 
                        // Creation of the menu
                        boolean first = true; // For differentiating the first scenario step
@@ -110,52 +145,72 @@ public class NewScenarioMenu extends SlidMenu {
                                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.isStarted()) {
+                                       if (step.isFinished()) {
+                                               icon = Constants.IMG_CHECKED;
+                                       } else {
+                                               icon = Constants.IMG_DONE;
+                                       }
+                               } else {
+                                       icon = Constants.IMG_EMPTY;
+                               }
+                               if (number == askdex) {
+                                       _stopen = step;
+                               }
                                Scenario group = (Scenario) step.getOwner();
                                long index = group.getIndex();
                                String value = index + "." + number;
-                               if (index != scenew.getIndex()) {
-                                       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,
-                                                       "select-step?selection=" + value
-                                                                       + "&title=%{title}");
-                               } else if (first) {
-                                       addGroup(value, scenew.getTitle(), icon,
-                                                       "select-step?selection=" + value
-                                                                       + "&title=%{title}");
-                                       first = false;
+                               if (index == scenew.getIndex()) {
+                                       if (first) {
+                                               addGroup(value, scenew.getTitle(), icon,
+                                                               getSelectStepUrl(value));
+                                               first = false;
+                                       } else {
+                                               addSubItem(value, "menu.step." + number, icon,
+                                                               getSelectStepUrl(value));
+                                       }
                                } else {
-                                       addSubItem(value, "menu.step." + number, icon,
-                                                       "select-step?selection=" + value
-                                                                       + "&title=%{title}");
+                                       if (getScenarioService().isEmpty(group)) {
+                                               icon = Constants.IMG_EMPTY;
+                                               // else if (group.isFinished()) icon = Constants.IMG_CHECKED;
+                                       } else {
+                                               icon = Constants.IMG_DONE;
+                                       }
+                                       addGroup(value, group.getTitle(), icon,
+                                                       getSelectStepUrl(value));
                                }
                        }
-                       scopen = scenew;
+                       _scopen = scenew;
                }
                super.selects(name);
        }
 
+       /**
+        * Build URL for selecting a study step.
+        * 
+        * @param value
+        *            selection
+        * @return URL relative to the web application context, i.e. the part after http://host/siman/
+        */
+       private String getSelectStepUrl(final String value) {
+               return "select-step?selection=" + value + "&title=%{title}";
+       }
+
+       /**
+        * Refresh the icon of the selected menu 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";
+               if (_stopen.isStarted()) {
+                       if (_stopen.isFinished()) {
+                               icon = Constants.IMG_CHECKED;
+                       } else {
+                               icon = Constants.IMG_DONE;
+                       }
+               } else {
+                       icon = Constants.IMG_EMPTY;
+               }
                item.icon(icon);
        }
 
@@ -174,7 +229,7 @@ public class NewScenarioMenu extends SlidMenu {
         * @param scenarioService
         *            the scenarioService to set
         */
-       public void setScenarioService(ScenarioService scenarioService) {
+       public void setScenarioService(final ScenarioService scenarioService) {
                _scenarioService = scenarioService;
        }
 
@@ -194,7 +249,7 @@ public class NewScenarioMenu extends SlidMenu {
         *            the projectElementService to set
         */
        public void setProjectElementService(
-                       ProjectElementService projectElementService) {
+                       final ProjectElementService projectElementService) {
                _projectElementService = projectElementService;
        }
 }
\ No newline at end of file