1 23 package org.objectweb.clif.scenario.util.isac.gui.action; 24 25 import java.io.BufferedReader ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.OutputStream ; 30 import java.io.PrintStream ; 31 import java.net.URL ; 32 import java.util.Vector ; 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 53 public class GenerateAction extends Action { 54 57 ScenarioGUIEditor window; 58 static Category cat = Category.getInstance(GenerateAction.class.getName()); 59 private int editorViewed; 60 61 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 ( 74 "file:" + FileName.GENERATE_ICON))); 75 } catch (Exception e) { 76 cat.warn("Unable to find generate icon file"); 77 } 78 } 79 80 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 } 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 this.window.switchEditorViewer(ScenarioGUIEditor.XML_EDITOR); 118 try { 120 FileOutputStream fileStream = new FileOutputStream ( 121 FileName.TEMP_BEHAVIORS); 122 PrintStream out = new PrintStream (fileStream); 123 treeManager.saveScenario(out); 124 out.flush(); 125 out.close(); 126 127 FileInputStream file = new FileInputStream ( 129 FileName.TEMP_BEHAVIORS); 130 InputStreamReader input = new InputStreamReader (file); 131 BufferedReader reader = new BufferedReader (input); 132 String result = ""; 134 String line = reader.readLine(); 135 while (line != null) { 136 result = result.concat(line + "\n"); 137 line = reader.readLine(); 138 } 139 this.window.getStyledText().setText(result); 141 } catch (Exception 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 this.window 157 .switchEditorViewer(ScenarioGUIEditor.GUI_EDITOR); 158 treeManager.createNewTree(); 160 treeViewer.setInput(treeManager.getTree()); 161 treeViewer.refresh(); 162 this.window.notifySelectionChanged(null) ; 163 String text = this.window.getStyledText().getText(); 165 if ((text == null) || (text.equals(""))) { 166 return; 167 } 168 OutputStream fileStream = new FileOutputStream ( 169 FileName.TEMP_BEHAVIORS); 170 PrintStream out = new PrintStream (fileStream); 171 out.println(text); 172 out.flush(); 173 out.close(); 174 ScenarioNode treeEmpty = treeManager.createNewTree(); 175 treeViewer.setInput(treeEmpty); 176 LoadProfileManager.getInstance().createNewLoadProfile(); 178 Vector errors = new Vector (); 180 ScenarioNode tree = LoadScenario.loadScenario( 181 FileName.TEMP_BEHAVIORS, TreeManager 182 .getTreeManager(null).getNodes(), errors); 183 if (tree == null) { 184 ErrorWarningDialog dialog = new ErrorWarningDialog( 186 this.window.getShell(), errors); 187 dialog.open(); 188 LoadProfileManager.getInstance().createNewLoadProfile(); 190 } else { 191 if (errors.size() != 0) { 193 ErrorWarningDialog dialog = new ErrorWarningDialog( 195 this.window.getShell(), errors); 196 dialog.open(); 197 } 198 treeManager.setTree(tree); 200 treeViewer.setInput(tree); 201 } 202 treeViewer.refresh(); 204 } catch (Exception e) { 205 cat.warn("Unable to save the scenario in " 206 + FileName.TEMP_BEHAVIORS + ", " + e); 207 } 208 break; 209 default : 210 } 212 } 213 214 } | Popular Tags |