Salome HOME
41eb3b4df4efb1e9d87273b14a12ee469751e820
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewScenarioMenu.java
1 package org.splat.simer;
2
3 import java.util.Iterator;
4 import java.util.Vector;
5
6 import org.splat.dal.bo.som.Scenario;
7 import org.splat.service.ProjectElementService;
8 import org.splat.service.ScenarioService;
9 import org.splat.som.Step;
10 import org.splat.dal.bo.som.Study;
11 import org.splat.wapp.MenuItem;
12 import org.splat.wapp.SlidMenu;
13
14 public class NewScenarioMenu extends SlidMenu {
15
16         private Study study;
17         private Scenario scopen; // Currently "open" scenario
18         private Step stopen; // Currently selected step
19         private ProjectElementService _projectElementService;
20         private ScenarioService _scenarioService;
21
22         // ==============================================================================================================================
23         // Constructor
24         // ==============================================================================================================================
25
26         public NewScenarioMenu() {
27                 super("scenarii", "study");
28                 scopen = null;
29         }
30
31         public NewScenarioMenu init(Study context) {
32                 study = context;
33                 scopen = null;
34                 init();
35                 return this;
36         }
37
38         public NewScenarioMenu(Study context) {
39                 // -----------------------------------
40                 super("scenarii", "study");
41                 study = context;
42                 scopen = null;
43         }
44
45         // ==============================================================================================================================
46         // Member functions
47         // ==============================================================================================================================
48
49         public void selects(String name) {
50                 // ---------------------------------
51                 String[] parse = name.split("\\x2E");
52                 Scenario[] scenes = study.getScenarii();
53                 Scenario scenew = scopen;
54                 int askid = 0;
55
56                 // Initialization
57                 if (scenew == null && scenes.length == 1)
58                         scenew = scenes[0];
59                 try {
60                         int askdex = Integer.valueOf(parse[0]);
61                         if (askdex > 0) {
62                                 while (askid < scenes.length) {
63                                         if (scenes[askid].getIndex() == askdex)
64                                                 break;
65                                         askid += 1;
66                                 }
67                                 scenew = scenes[askid]; // Throws an exception if the scenario does not exist (that is, if name is not correct)
68                         }
69                 } catch (Exception error) {
70                         return;
71                 }
72                 if (scenew == null) {
73
74                         // Study with several scenarii, non of them open
75
76                         // Collection of steps to be displayed
77                         Vector<Step> steps = new Vector<Step>();
78                         Step[] newstep = getProjectElementService().getSteps(scenes[0]); // All scenarii have the same steps
79
80                         for (int i = 0; i < scenes.length; i++)
81                                 steps.add(getProjectElementService().getFirstStep(scenes[i]));
82                         newstep = getProjectElementService().getSteps(study);
83                         stopen = newstep[0]; // Default selected step
84
85                         // Creation of the menu
86                         for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
87                                 Step step = i.next();
88                                 int number = step.getNumber();
89                                 Scenario group = (Scenario) step.getOwner(); // The menu includes first scenario steps only
90                                 long index = group.getIndex();
91                                 String value = index + "." + number;
92                                 String icon;
93                                 if (getScenarioService().isEmpty(group))
94                                         icon = "icon.empty.png";
95                                 // else if (group.isFinished()) icon = "icon.checked.png";
96                                 else
97                                         icon = "icon.done.png";
98                                 addGroup(value, group.getTitle(), icon,
99                                                 "select-step?selection=" + value + "&title=%{title}");
100                         }
101                 } else if (scopen == null || !scenew.equals(scopen)) {
102
103                         // Opening a scenario
104                         this.clear();
105                         // Collection of steps to be displayed
106                         Vector<Step> steps = new Vector<Step>();
107                         Step[] newstep = getProjectElementService().getSteps(scenew);
108
109                         for (int i = 0; i < newstep.length; i++) {
110                                 steps.add(newstep[i]);
111                         }
112                         for (int i = askid - 1; i > -1; i--)
113                                 steps.add(0, getProjectElementService().getFirstStep(scenes[i]));
114                         newstep = getProjectElementService().getSteps(study);
115                         for (int i = askid + 1; i < scenes.length; i++)
116                                 steps.add(getProjectElementService().getFirstStep(scenes[i]));
117
118                         // Creation of the menu
119                         boolean first = true; // For differentiating the first scenario step
120                         int askdex = Integer.valueOf(parse[1]);
121                         for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
122                                 Step step = i.next();
123                                 int number = step.getNumber();
124                                 String icon;
125                                 if (!step.isStarted())
126                                         icon = "icon.empty.png";
127                                 else if (step.isFinished())
128                                         icon = "icon.checked.png";
129                                 else
130                                         icon = "icon.done.png";
131                                 if (number == askdex)
132                                         stopen = step;
133                                 Scenario group = (Scenario) step.getOwner();
134                                 long index = group.getIndex();
135                                 String value = index + "." + number;
136                                 if (index != scenew.getIndex()) {
137                                         if (getScenarioService().isEmpty(group))
138                                                 icon = "icon.empty.png";
139                                         // else if (group.isFinished()) icon = "icon.checked.png";
140                                         else
141                                                 icon = "icon.done.png";
142                                         addGroup(value, group.getTitle(), icon,
143                                                         "select-step?selection=" + value
144                                                                         + "&title=%{title}");
145                                 } else if (first) {
146                                         addGroup(value, scenew.getTitle(), icon,
147                                                         "select-step?selection=" + value
148                                                                         + "&title=%{title}");
149                                         first = false;
150                                 } else {
151                                         addSubItem(value, "menu.step." + number, icon,
152                                                         "select-step?selection=" + value
153                                                                         + "&title=%{title}");
154                                 }
155                         }
156                         scopen = scenew;
157                 }
158                 super.selects(name);
159         }
160
161         public void refreshSelectedItem() {
162                 // ----------------------------------
163                 MenuItem item = this.getSelectedItem();
164                 String icon;
165                 if (!stopen.isStarted())
166                         icon = "icon.empty.png";
167                 else if (stopen.isFinished())
168                         icon = "icon.checked.png";
169                 else
170                         icon = "icon.done.png";
171                 item.icon(icon);
172         }
173
174         /**
175          * Get the scenarioService.
176          * 
177          * @return the scenarioService
178          */
179         public ScenarioService getScenarioService() {
180                 return _scenarioService;
181         }
182
183         /**
184          * Set the scenarioService.
185          * 
186          * @param scenarioService
187          *            the scenarioService to set
188          */
189         public void setScenarioService(ScenarioService scenarioService) {
190                 _scenarioService = scenarioService;
191         }
192
193         /**
194          * Get the projectElementService.
195          * 
196          * @return the projectElementService
197          */
198         public ProjectElementService getProjectElementService() {
199                 return _projectElementService;
200         }
201
202         /**
203          * Set the projectElementService.
204          * 
205          * @param projectElementService
206          *            the projectElementService to set
207          */
208         public void setProjectElementService(
209                         ProjectElementService projectElementService) {
210                 _projectElementService = projectElementService;
211         }
212 }