KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > loadprofile > LoadProfileManager


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.loadprofile;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.log4j.Category;
30 import org.eclipse.jface.wizard.WizardDialog;
31 import org.eclipse.swt.graphics.Color;
32 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
33 import org.objectweb.clif.scenario.util.isac.loadprofile.gui.GroupDescriptionWizard;
34 import org.objectweb.clif.scenario.util.isac.loadprofile.gui.LoadDrawingEditor;
35 import org.objectweb.clif.scenario.util.isac.loadprofile.gui.Size;
36 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
37
38 /**
39  * This class implements the load profile manager
40  *
41  * @author JC Meillaud
42  * @author A Peyrard
43  */

44 public class LoadProfileManager {
45     // logger
46
static Category cat = Category.getInstance(LoadProfileManager.class
47             .getName());
48     // unique instance
49
private static LoadProfileManager instance = null;
50     // table storing load profiles
51
private Hashtable JavaDoc groups;
52     // LoadDrawing Editor
53
private LoadDrawingEditor loadDrawingEditor;
54     // ScenarioGUIEditor
55
private ScenarioGUIEditor window;
56
57     /**
58      * Build a new load profile manager
59      */

60     private LoadProfileManager() {
61         this.groups = new Hashtable JavaDoc();
62     }
63
64     /**
65      * Instance of the manager getter
66      *
67      * @return the unique instance of this manager
68      */

69     public static LoadProfileManager getInstance() {
70         if (instance == null)
71             instance = new LoadProfileManager();
72         return instance;
73     }
74
75     /**
76      * Create a new load profile
77      */

78     public void createNewLoadProfile() {
79         // remove all groups int the group description table
80
this.groups.clear();
81         // unselect the groups
82
this.loadDrawingEditor.setSelectedGroup(null) ;
83     }
84
85     ///////////////////////////////////////////////////////////////////////////////
86
// GROUP and RAMP Descriptions
87
//////////////////////////////////////////////////////////////////////////////
88

89     /**
90      * Search in all groups the one which have the oldId for behavior id
91      * and replace this id by the new one
92      * @param oldId The oldId to be searched
93      * @param newId The id to be set
94      */

95     public void changeBehaviorId(String JavaDoc oldId, String JavaDoc newId) {
96         // put a flag to know if changes have been done
97
boolean flag = false ;
98         Enumeration JavaDoc e = this.getElements() ;
99         // for each element test if the behaviorId is the oldId
100
while (e.hasMoreElements()) {
101             // get the group description
102
GroupDescription gd = (GroupDescription)e.nextElement() ;
103             if (gd.getBehaviorId().equals(oldId)) {
104                 gd.setBehaviorId(newId) ;
105                 flag = true ;
106             }
107         }
108         // if there was change
109
if (flag) {
110             if (loadDrawingEditor != null) {
111                 // update the selection menu item
112
this.loadDrawingEditor.setSelectedGroup(this.loadDrawingEditor.getGroupDescriptionSelected()) ;
113             }
114         }
115     }
116     
117     /**
118      * This method return an enumeration of all groups existing in the load profile manager
119      * @return The elements
120      */

121     public Enumeration JavaDoc getElements() {
122         return this.groups.elements() ;
123     }
124     
125     /**
126      * This method adds a new group description to the load profile
127      *
128      * @param gd
129      * The group description to add
130      */

131     public void addGroupDescription(GroupDescription gd) {
132         // if key previously exist in the load profile
133
if (this.groups.containsKey(gd.getGroupId()))
134             return;
135         // add the group
136
this.groups.put(gd.getGroupId(), gd);
137     }
138
139     /**
140      * This method adds a new ramp description to a group specified by it id
141      * @param rd The ramp description
142      * @param groupId The group id
143      */

144     public void addRampDescriptionToGroup(RampDescription rd, String JavaDoc groupId) {
145         // check if the group table contains the id
146
if (!this.groups.containsKey(groupId)) {
147             // do nothing
148
return ;
149         }
150         // add the ramp description
151
((GroupDescription)this.groups.get(groupId)).addRamp(rd) ;
152     }
153     
154     /**
155      * This method remove a selected group description of this load profile
156      *
157      * @param gd
158      * The group description to remove
159      */

160     public void removeGroupDescription(GroupDescription gd) {
161         // if the id exist in the group table
162
if (this.groups.containsKey(gd.getGroupId())) {
163             this.groups.remove(gd.getGroupId());
164         }
165     }
166         
167     /**
168      * Ramp description getter
169      *
170      * @param rampId
171      * The id of the ramp description searched
172      * @param groupId
173      * The id of the group containing the ramp
174      * @return The ramp description we are looking for
175      */

176     public RampDescription getRampDescriptionOfGroup(String JavaDoc rampId,
177             String JavaDoc groupId) {
178         // if groupid exist
179
if (this.groups.containsKey(groupId))
180             return ((GroupDescription) this.groups.get(groupId)).getRampDescription(rampId);
181         return null;
182     }
183
184     /**
185      * Get the elements ramp description of a given group
186      *
187      * @param groupId the id of the group
188      * @return The elements which are in the table
189      */

190     public Enumeration JavaDoc getElementsOfGroup(String JavaDoc groupId) {
191         // if groupid exist
192
if (this.groups.containsKey(groupId))
193             return ((GroupDescription) this.groups.get(groupId)).getElements();
194         return null;
195     }
196
197     /**
198      * Remove a rampdescription
199      *
200      * @param rampId
201      * The id of the ramp to remove
202      */

203     public void removeRampDescription(String JavaDoc rampId) {
204         if (rampId == null)
205             return;
206         // analyse all groups in order to find the ramp to be removed
207
Enumeration JavaDoc e = this.groups.elements();
208         while (e.hasMoreElements()) {
209             ((GroupDescription) e.nextElement()).removeRampDescription(rampId);
210         }
211     }
212
213     /**
214      * Remove a ramp description of a group
215      *
216      * @param rd
217      * The ramp description to remove
218      * @param groupId
219      * The group id to remove the element
220      */

221     public void removeRampDescriptionOfGroup(RampDescription rd, String JavaDoc groupId) {
222         cat.warn("REMOVE RAMP DESCRIPTION");
223         // if ramp exist
224
if (this.groups.containsKey(groupId)) {
225             ((GroupDescription) this.groups.get(groupId))
226                     .removeRampDescription(rd);
227         }
228     }
229
230     /**
231      * Remove a ramp description of a group
232      *
233      * @param rampId The ramp id of the description to remove
234      * @param groupId The group id to remove the element
235      */

236     public void removeRampDescriptionOfGroup(String JavaDoc rampId, String JavaDoc groupId) {
237         // if ramp exist
238
if (this.groups.containsKey(groupId)) {
239             ((GroupDescription) this.groups.get(groupId))
240                     .removeRampDescription(rampId);
241         }
242     }
243
244     /**
245      * Generate a new unused id for a ramp
246      *
247      * @return The new created id
248      */

249     public String JavaDoc rampIdGenerator() {
250         // get all the ids of the groups
251
Vector JavaDoc ids = new Vector JavaDoc();
252         Enumeration JavaDoc e = this.groups.elements();
253         while (e.hasMoreElements())
254             ids.addAll(((GroupDescription) e.nextElement())
255                     .getRampDescriptionIds());
256         // search a new unique id
257
int intId = ids.size();
258         while (ids.contains("r" + Integer.toString(intId)))
259             intId++;
260         return "r" + Integer.toString(intId);
261     }
262
263     /**
264      * Generate a new unused id for a group
265      *
266      * @return The new created id
267      */

268     public String JavaDoc groupIdGenerator() {
269         int intId = this.groups.size();
270         while (this.groups.containsKey("g" + Integer.toString(intId)))
271             intId++;
272         return "g" + Integer.toString(intId);
273     }
274
275     /////////////////////////////////////////////////////////////////////////////
276
// Drawable editor methods
277
/////////////////////////////////////////////////////////////////////////////
278

279     /**
280      * Return the default color defined in the drawable editor
281      * @return The default color
282      */

283     public Color getDefaultColor() {
284         return LoadDrawingEditor.defaultColor ;
285     }
286     
287     /**
288      * Edit ramp selected properties
289      */

290     public void editGroupSelectedDescription() {
291         // get the behaviors ids
292
Vector JavaDoc ids = TreeManager.getTreeManager(null).getBehaviorsIds();
293         // open the wizard for editing current ramp values
294
GroupDescriptionWizard wizard = new GroupDescriptionWizard(
295                 this.loadDrawingEditor.getGroupDescriptionSelected(), ids);
296         WizardDialog dialog = new WizardDialog(this.loadDrawingEditor
297                 .getParent().getShell(), wizard);
298         int erno = dialog.open();
299         // reload the description of the selected toolbar item
300
this.loadDrawingEditor.setSelectedGroup(this.loadDrawingEditor
301                 .getGroupDescriptionSelected());
302     }
303     
304     public void setScale(Size scaleSize) {
305         this.loadDrawingEditor.setScale(scaleSize) ;
306     }
307
308     /**
309      * Notify when the group selected changing
310      * @param gd The group description selected
311      */

312     public void notifySelectionChangedInDrawable(GroupDescription gd) {
313         // do things
314
}
315     
316     /**
317      * Set the load drawing editor
318      *
319      * @param lde
320      * The load drawing editor
321      */

322     public void setDrawablePart(LoadDrawingEditor lde) {
323         this.loadDrawingEditor = lde;
324     }
325 }
Popular Tags