KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.log4j.Category;
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.dialogs.MessageDialog;
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.Display;
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.loadprofile.LoadProfileManager;
39 import org.objectweb.clif.scenario.util.isac.util.tree.ErrorWarningDialog;
40 import org.objectweb.clif.scenario.util.isac.util.tree.LoadScenario;
41 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
42 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
43 /**
44  * Action which open a scenario
45  *
46  * @author JC Meillaud
47  * @author A Peyrard
48  */

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

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

60     public OpenScenarioAction(ApplicationWindow w) {
61         this.window = (ScenarioGUIEditor) w;
62         this.setText("&Open@Ctrl+O");
63         this.setToolTipText("Open a scenario file");
64         try {
65             this.setImageDescriptor(
66                 ImageDescriptor.createFromURL(
67                     new URL JavaDoc("file:" + FileName.OPEN_ICON)));
68         } catch (Exception JavaDoc e) {
69             cat.warn("Unable to find load icon file");
70         }
71     }
72
73     public void run() {
74         cat.debug("-> run") ;
75         // check if the scenario is saved
76
if (!this.window.isBehaviorsSavedState() && this.window.isNotNullTree()) {
77             // ask the user if he wants to save the scenario
78
if (MessageDialog
79                     .openQuestion(
80                             Display.getCurrent().getActiveShell(),
81                             "Scenario is not saved",
82                             "The current scenario is not saved,\n"
83                                     + "you will loose changes. Would you like to save it ?")) {
84                 SaveScenarioAction.saveScenario(this.window) ;
85             }
86         }
87         // get the current fileName
88
String JavaDoc[] filterExtensions={"*.xml"};
89         // create a new file dialog
90
FileDialog dialog =
91                 new FileDialog(this.window.getShell(), SWT.OPEN);
92         dialog.setText("Open a CLIF scenario");
93         dialog.setFilterExtensions(filterExtensions);
94         // open it and save the filename
95
String JavaDoc fileName = dialog.open();
96         if (fileName == null) return ;
97         this.window.setFileName(fileName);
98         // load the scenario
99
cat.warn("LOAD THE SCENARIO IN THE FILE : " + fileName) ;
100         // initialise a errors vector
101
Vector JavaDoc errors = new Vector JavaDoc() ;
102         ScenarioNode tree = LoadScenario.loadScenario(fileName, TreeManager.getTreeManager(null).getNodes(), errors) ;
103         if (tree == null) {
104             // open a errors-warning dialog
105
ErrorWarningDialog dialogError = new ErrorWarningDialog(this.window.getShell(), errors) ;
106             dialogError.open() ;
107             // erase the dited profile
108
LoadProfileManager.getInstance().createNewLoadProfile();
109         }
110         else {
111             // we only have warnings, because the tree is not null
112
if (errors.size() != 0) {
113             // open a errors-warning dialog
114
ErrorWarningDialog dialogError = new ErrorWarningDialog(this.window.getShell(), errors) ;
115             dialogError.open() ;
116         }
117             // if there is no errors add the tree loaded
118
TreeManager.getTreeManager(null).setTree(tree);
119             this.window.getTreeViewer().setInput(tree);
120         }
121         this.window.getTreeViewer().refresh() ;
122         // set the saved state
123
this.window.setScenarioSavedState(true) ;
124     }
125 }
126
Popular Tags