KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > gui > action > GenerateAction


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.gui.action;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.PrintStream JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.apache.log4j.Category;
35 import org.eclipse.jface.action.Action;
36 import org.eclipse.jface.dialogs.MessageDialog;
37 import org.eclipse.jface.resource.ImageDescriptor;
38 import org.eclipse.jface.window.ApplicationWindow;
39 import org.objectweb.clif.scenario.util.isac.FileName;
40 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
41 import org.objectweb.clif.scenario.util.isac.gui.tree.ScenarioTreeViewer;
42 import org.objectweb.clif.scenario.util.isac.loadprofile.LoadProfileManager;
43 import org.objectweb.clif.scenario.util.isac.util.tree.ErrorWarningDialog;
44 import org.objectweb.clif.scenario.util.isac.util.tree.LoadScenario;
45 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
46 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
47 /**
48  * Action which genrate either XML or tree in the other editor view
49  *
50  * @author JC Meillaud
51  * @author A Peyrard
52  */

53 public class GenerateAction extends Action {
54     /**
55      * The application window, which launch this action
56      */

57     ScenarioGUIEditor window;
58     static Category cat = Category.getInstance(GenerateAction.class.getName());
59     private int editorViewed;
60
61     /**
62      * Build a new 'Generate' action
63      *
64      * @param w
65      * The application window which launch the action
66      */

67     public GenerateAction(ApplicationWindow w, int editorViewed) {
68         cat.debug("-> constructor");
69         this.window = (ScenarioGUIEditor) w;
70         this.editorViewed = editorViewed;
71         switchGenerationMethod(editorViewed);
72         try {
73             this.setImageDescriptor(ImageDescriptor.createFromURL(new URL JavaDoc(
74                     "file:" + FileName.GENERATE_ICON)));
75         } catch (Exception JavaDoc e) {
76             cat.warn("Unable to find generate icon file");
77         }
78     }
79
80     /**
81      * Change the generation method (only the text and the tooltiptext)
82      *
83      * @param EditorViewed
84      * The int representing an editor
85      */

86     public void switchGenerationMethod(int EditorViewed) {
87         switch (EditorViewed) {
88             case ScenarioGUIEditor.XML_EDITOR :
89                 this.setText("Generate Tree@Ctrl+G");
90                 this
91                         .setToolTipText("Generate the tree describing in the XML editor");
92                 break;
93             case ScenarioGUIEditor.GUI_EDITOR :
94                 this.setText("Generate XML@Ctrl+G");
95                 this
96                         .setToolTipText("Generate the XML representing by the tree");
97                 break;
98             default :
99         // never append, none other choices
100
}
101     }
102
103     public void run() {
104         cat.debug("-> run");
105         TreeManager treeManager = TreeManager.getTreeManager(null);
106         ScenarioTreeViewer treeViewer = this.window.getTreeViewer();
107         switch (editorViewed) {
108             case ScenarioGUIEditor.GUI_EDITOR :
109                 if (!this.window.getStyledText().getText().equals("")) {
110                     if (!MessageDialog
111                             .openConfirm(this.window.getShell(),
112                                     "Continue XML generation process ?",
113                                     "You will loose the XML code previously edited in the XML Editor"))
114                         return;
115                 }
116                 // switch the view
117
this.window.switchEditorViewer(ScenarioGUIEditor.XML_EDITOR);
118                 // save the tree into a temp file
119
try {
120                     FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(
121                             FileName.TEMP_BEHAVIORS);
122                     PrintStream JavaDoc out = new PrintStream JavaDoc(fileStream);
123                     treeManager.saveScenario(out);
124                     out.flush();
125                     out.close();
126
127                     // load the temp file
128
FileInputStream JavaDoc file = new FileInputStream JavaDoc(
129                             FileName.TEMP_BEHAVIORS);
130                     InputStreamReader JavaDoc input = new InputStreamReader JavaDoc(file);
131                     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(input);
132                     // create the result string
133
String JavaDoc result = "";
134                     String JavaDoc line = reader.readLine();
135                     while (line != null) {
136                         result = result.concat(line + "\n");
137                         line = reader.readLine();
138                     }
139                     // set the text into the editor
140
this.window.getStyledText().setText(result);
141                 } catch (Exception JavaDoc e) {
142                     cat.warn("Unable to save the scenario in "
143                             + FileName.TEMP_BEHAVIORS + ", " + e);
144                 }
145                 break;
146             case ScenarioGUIEditor.XML_EDITOR :
147                 try {
148                     if (this.window.isNotNullTree()) {
149                         if (!MessageDialog
150                                 .openConfirm(this.window.getShell(),
151                                         "Continue tree generation process ?",
152                                         "You will lose the tree previously edited in the GUI Editor"))
153                             return;
154                     }
155                     // switch the view
156
this.window
157                             .switchEditorViewer(ScenarioGUIEditor.GUI_EDITOR);
158                     // init a new tree
159
treeManager.createNewTree();
160                     treeViewer.setInput(treeManager.getTree());
161                     treeViewer.refresh();
162                     this.window.notifySelectionChanged(null) ;
163                     // get the text of the xml editor
164
String JavaDoc text = this.window.getStyledText().getText();
165                     if ((text == null) || (text.equals(""))) {
166                         return;
167                     }
168                     OutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(
169                             FileName.TEMP_BEHAVIORS);
170                     PrintStream JavaDoc out = new PrintStream JavaDoc(fileStream);
171                     out.println(text);
172                     out.flush();
173                     out.close();
174                     ScenarioNode treeEmpty = treeManager.createNewTree();
175                     treeViewer.setInput(treeEmpty);
176                     // reinitialize the load profile
177
LoadProfileManager.getInstance().createNewLoadProfile();
178                     // initialise a errors vector
179
Vector JavaDoc errors = new Vector JavaDoc();
180                     ScenarioNode tree = LoadScenario.loadScenario(
181                             FileName.TEMP_BEHAVIORS, TreeManager
182                                     .getTreeManager(null).getNodes(), errors);
183                     if (tree == null) {
184                         // open a errors-warning dialog
185
ErrorWarningDialog dialog = new ErrorWarningDialog(
186                                 this.window.getShell(), errors);
187                         dialog.open();
188                         // erase the edited profile
189
LoadProfileManager.getInstance().createNewLoadProfile();
190                     } else {
191                         // we only have warnings, because the tree is not null
192
if (errors.size() != 0) {
193                             // open a errors-warning dialog
194
ErrorWarningDialog dialog = new ErrorWarningDialog(
195                                     this.window.getShell(), errors);
196                             dialog.open();
197                         }
198                         // if there is no errors add the tree loaded
199
treeManager.setTree(tree);
200                         treeViewer.setInput(tree);
201                     }
202                     // refresh the tree view
203
treeViewer.refresh();
204                 } catch (Exception JavaDoc e) {
205                     cat.warn("Unable to save the scenario in "
206                             + FileName.TEMP_BEHAVIORS + ", " + e);
207                 }
208                 break;
209             default :
210         // no other choice
211
}
212     }
213
214 }
Popular Tags