1 23 package org.objectweb.clif.scenario.util.isac.gui.action; 24 25 import java.io.FileOutputStream ; 26 import java.io.PrintStream ; 27 import java.net.URL ; 28 import java.util.Vector ; 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 50 public class SaveScenarioAction extends Action { 51 54 private ScenarioGUIEditor window; 55 static Category cat = Category.getInstance(SaveScenarioAction.class 56 .getName()); 57 58 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 ( 70 "file:" + FileName.SAVE_ICON))); 71 } catch (Exception e) { 72 cat.warn("Unable to find save icon file"); 73 } 74 } 75 76 82 public static void saveScenario(ScenarioGUIEditor window) { 83 String fileName = window.getFileName(); 85 String [] filterExtensions = {"*.xml"}; 86 if (fileName == null) { 87 FileDialog dialog = new FileDialog(window.getShell(), SWT.SAVE); 89 dialog.setText("Saving a CLIF scenario"); 90 dialog.setFilterExtensions(filterExtensions); 91 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 fileStream = new FileOutputStream (fileName); 100 PrintStream out = new PrintStream (fileStream); 101 TreeManager treeManager = TreeManager.getTreeManager(null); 102 switch (window.getEditorShowed()) { 103 case ScenarioGUIEditor.XML_EDITOR : 104 ScenarioTreeViewer treeViewer = window.getTreeViewer(); 105 String 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 LoadProfileManager.getInstance().createNewLoadProfile(); 119 Vector errors = new Vector (); 121 ScenarioNode tree = LoadScenario.loadScenario( 122 fileName, TreeManager 123 .getTreeManager(null).getNodes(), errors); 124 if (tree == null) { 125 ErrorWarningDialog dialog = new ErrorWarningDialog( 127 window.getShell(), errors); 128 dialog.open(); 129 LoadProfileManager.getInstance().createNewLoadProfile(); 131 } else { 132 if (errors.size() != 0) { 134 ErrorWarningDialog dialog = new ErrorWarningDialog( 136 window.getShell(), errors); 137 dialog.open(); 138 } 139 treeManager.setTree(tree); 141 treeViewer.setInput(tree); 142 treeManager.saveScenario(out); 143 window.setScenarioSavedState(true); 144 } 145 148 break; 149 case ScenarioGUIEditor.GUI_EDITOR : 150 treeManager.saveScenario(out); 151 window.setScenarioSavedState(true); 152 break; 153 default : 154 break; 155 } 156 } catch (Exception 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 |