KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.log4j.Category;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.jface.window.ApplicationWindow;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.widgets.FileDialog;
36 import org.objectweb.clif.scenario.util.isac.FileName;
37 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
38 import org.objectweb.clif.scenario.util.isac.gui.tree.ScenarioTreeViewer;
39 import org.objectweb.clif.scenario.util.isac.loadprofile.LoadProfileManager;
40 import org.objectweb.clif.scenario.util.isac.util.tree.ErrorWarningDialog;
41 import org.objectweb.clif.scenario.util.isac.util.tree.LoadScenario;
42 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
43 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
44 /**
45  * Action which save a scenario
46  *
47  * @author JC Meillaud
48  * @author A Peyrard
49  */

50 public class SaveScenarioAction extends Action {
51     /**
52      * The application window, which launch this action
53      */

54     private ScenarioGUIEditor window;
55     static Category cat = Category.getInstance(SaveScenarioAction.class
56             .getName());
57
58     /**
59      * Build a new 'save scenario' action
60      *
61      * @param w
62      * The application window, in wich we will use this action
63      */

64     public SaveScenarioAction(ApplicationWindow w) {
65         this.window = (ScenarioGUIEditor) w;
66         this.setText("&Save@Ctrl+S");
67         this.setToolTipText("Save the current Scenario");
68         try {
69             this.setImageDescriptor(ImageDescriptor.createFromURL(new URL JavaDoc(
70                     "file:" + FileName.SAVE_ICON)));
71         } catch (Exception JavaDoc e) {
72             cat.warn("Unable to find save icon file");
73         }
74     }
75
76     /**
77      * Save a scenario in the filename if it is previously defined
78      *
79      * @param window
80      * The scenarioGUIEditor
81      */

82     public static void saveScenario(ScenarioGUIEditor window) {
83         // get the current fileName
84
String JavaDoc fileName = window.getFileName();
85         String JavaDoc[] filterExtensions = {"*.xml"};
86         if (fileName == null) {
87             // create a new file dialog
88
FileDialog dialog = new FileDialog(window.getShell(), SWT.SAVE);
89             dialog.setText("Saving a CLIF scenario");
90             dialog.setFilterExtensions(filterExtensions);
91             // open it and save the filename
92
fileName = dialog.open();
93             if (fileName == null)
94                 return;
95             window.setFileName(fileName);
96             cat.debug("Save into the file " + fileName);
97         }
98         try {
99             FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(fileName);
100             PrintStream JavaDoc out = new PrintStream JavaDoc(fileStream);
101             TreeManager treeManager = TreeManager.getTreeManager(null);
102             switch (window.getEditorShowed()) {
103                 case ScenarioGUIEditor.XML_EDITOR :
104                     ScenarioTreeViewer treeViewer = window.getTreeViewer();
105                     String JavaDoc text = window.getStyledText().getText();
106                     if ((text == null) || (text.equals(""))) {
107                         treeManager.createNewTree();
108                         treeViewer.setInput(treeManager.getTree());
109                         treeViewer.refresh();
110                         return;
111                     }
112                     out.println(text);
113                     out.flush();
114                     out.close();
115                     ScenarioNode treeEmpty = treeManager.createNewTree();
116                     treeViewer.setInput(treeEmpty);
117                     // reinitialize the load profile
118
LoadProfileManager.getInstance().createNewLoadProfile();
119                     // initialise a errors vector
120
Vector JavaDoc errors = new Vector JavaDoc();
121                     ScenarioNode tree = LoadScenario.loadScenario(
122                             fileName, TreeManager
123                                     .getTreeManager(null).getNodes(), errors);
124                     if (tree == null) {
125                         // open a errors-warning dialog
126
ErrorWarningDialog dialog = new ErrorWarningDialog(
127                                 window.getShell(), errors);
128                         dialog.open();
129                         // erase the dited profile
130
LoadProfileManager.getInstance().createNewLoadProfile();
131                     } else {
132                         // we only have warnings, because the tree is not null
133
if (errors.size() != 0) {
134                             // open a errors-warning dialog
135
ErrorWarningDialog dialog = new ErrorWarningDialog(
136                                     window.getShell(), errors);
137                             dialog.open();
138                         }
139                         // if there is no errors add the tree loaded
140
treeManager.setTree(tree);
141                         treeViewer.setInput(tree);
142                         treeManager.saveScenario(out);
143                         window.setScenarioSavedState(true);
144                     }
145                     // refresh the tree view
146
//treeViewer.refresh();
147

148                     break;
149                 case ScenarioGUIEditor.GUI_EDITOR :
150                     treeManager.saveScenario(out);
151                     window.setScenarioSavedState(true);
152                     break;
153                 default :
154                     break;
155             }
156             //treeManager.saveScenario(out) ;
157
// set the saved state
158
//window.setBehaviorsSavedState(true) ;
159
} catch (Exception JavaDoc e) {
160             cat.warn("Unable to save the scenario in " + fileName + ", " + e);
161         }
162     }
163
164     public void run() {
165         cat.debug("-> run");
166         saveScenario(this.window);
167     }
168 }
Popular Tags