KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import org.apache.log4j.Category;
30 import org.eclipse.jface.action.Action;
31 import org.eclipse.jface.resource.ImageDescriptor;
32 import org.eclipse.jface.window.ApplicationWindow;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.widgets.FileDialog;
35 import org.objectweb.clif.scenario.util.isac.FileName;
36 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
37 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
38 /**
39  * Action which save a scenario
40  *
41  * @author JC Meillaud
42  * @author A Peyrard
43  */

44 public class SaveScenarioAsAction extends Action {
45     static Category cat = Category.getInstance(SaveScenarioAsAction.class.getName());
46     /**
47      * The application window, which launch this action
48      */

49     private ScenarioGUIEditor window;
50
51     /**
52      * Build a new 'save scenario' action
53      * @param w The application window, in wich we will use this action
54      */

55     public SaveScenarioAsAction(ApplicationWindow w) {
56         cat.debug("-> constructor") ;
57         this.window = (ScenarioGUIEditor) w;
58         this.setText("Save &As...@Ctrl+Alt+S");
59         this.setToolTipText("Save the current Scenario in a specified file");
60         try {
61             this.setImageDescriptor(
62                 ImageDescriptor.createFromURL(
63                     new URL JavaDoc("file:" + FileName.SAVE_AS_ICON)));
64         } catch (Exception JavaDoc e) {
65             cat.warn("Unable to find save_as icon file");
66         }
67     }
68
69     public void run() {
70         cat.debug("-> run") ;
71         // create a new file dialog
72
FileDialog dialog = new FileDialog(this.window.getShell(), SWT.SAVE);
73         // open it and save the filename
74
String JavaDoc fileName = dialog.open();
75         if (fileName == null) {
76             this.window.getStatusLine().getProgressMonitor().done() ;
77             return ;
78         }
79         
80         // set the filename to the gui
81
cat.debug("Save into the file "+fileName);
82         this.window.setFileName(fileName);
83         // Save the scenario into the file
84
try {
85             FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(fileName) ;
86             PrintStream JavaDoc out = new PrintStream JavaDoc(fileStream) ;
87             TreeManager treeManager = TreeManager.getTreeManager(null) ;
88             //treeManager.saveBehaviors(fileStream) ;
89
treeManager.saveScenario(out);
90             fileStream.flush();
91             fileStream.close() ;
92             // set the saved state
93
this.window.setScenarioSavedState(true) ;
94         }
95         catch (Exception JavaDoc e) {
96             cat.warn("Unable to save the scenario in "+ fileName +", " + e) ;
97         }
98     }
99 }
100
Popular Tags