1 30 package com.genimen.djeneric.tools.specifier.editor; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Color ; 34 import java.awt.Component ; 35 import java.awt.FlowLayout ; 36 import java.io.UnsupportedEncodingException ; 37 import java.util.ArrayList ; 38 39 import javax.swing.BorderFactory ; 40 import javax.swing.JLabel ; 41 import javax.swing.JOptionPane ; 42 import javax.swing.JPanel ; 43 import javax.swing.border.BevelBorder ; 44 import javax.swing.border.Border ; 45 46 import com.genimen.djeneric.language.Messages; 47 import com.genimen.djeneric.repository.DjExtent; 48 import com.genimen.djeneric.repository.DjList; 49 import com.genimen.djeneric.repository.DjOql; 50 import com.genimen.djeneric.repository.DjPersistenceManager; 51 import com.genimen.djeneric.repository.DjSession; 52 import com.genimen.djeneric.repository.exceptions.DjenericException; 53 import com.genimen.djeneric.structure.EditorDefinition; 54 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine; 55 import com.genimen.djeneric.tools.scriptengine.core.EditorEventDefinition; 56 import com.genimen.djeneric.tools.scriptengine.core.ParseException; 57 import com.genimen.djeneric.tools.scriptengine.core.ScriptRunnerContainer; 58 import com.genimen.djeneric.tools.scriptengine.core.nodes.ScriptNode; 59 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionContext; 60 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException; 61 import com.genimen.djeneric.tools.scriptengine.core.util.Variable; 62 import com.genimen.djeneric.tools.specifier.base.BasePanel; 63 import com.genimen.djeneric.tools.specifier.interfaces.Chooser; 64 import com.genimen.djeneric.tools.specifier.interfaces.CustomPanel; 65 import com.genimen.djeneric.tools.specifier.interfaces.EditorEventListener; 66 import com.genimen.djeneric.tools.specifier.interfaces.ObjectEditor; 67 import com.genimen.djeneric.tools.specifier.interfaces.ScriptEventDispatcher; 68 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanel; 69 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer; 70 import com.genimen.djeneric.util.DjLogger; 71 72 public class ScriptRunnerPanel extends BasePanel 73 implements 74 SpecifierPanelContainer, 75 ScriptRunnerContainer, 76 EditorEventListener 77 { 78 private static final long serialVersionUID = 1L; 79 BorderLayout borderLayout1 = new BorderLayout (); 80 DjSession _session; 81 public final static String OK_MSG = Messages.getString("global.Ok"); 82 ScriptNode _currentScript = null; 83 Border border1; 84 JPanel _panelHeader = new JPanel (); 85 JLabel _labelStep = new JLabel (); 86 FlowLayout flowLayout1 = new FlowLayout (); 87 88 public ScriptRunnerPanel(SpecifierPanelContainer editorContainer) 89 { 90 try 91 { 92 setSpecifierPanelContainer(editorContainer); 93 94 jbInit(); 95 } 96 catch (Exception ex) 97 { 98 DjLogger.log(ex); 99 } 100 } 101 102 public void setScript(String script) throws UnsupportedEncodingException , ParseException 103 { 104 if (_session != null) 105 { 106 _session.close(); 107 _session = null; 108 } 109 110 DjScriptParserEngine parser = new DjScriptParserEngine(script); 111 _currentScript = parser.getTree(); 112 } 113 114 public void runScript() throws DjenericException, DjScriptExecutionException 115 { 116 _session = getPersistenceManager().createSession(); 117 _currentScript.execute(this); 118 } 119 120 public String getTitle() 121 { 122 if (_currentScript == null) return Messages.getString("ScriptRunnerPanel.UnknownScript"); 123 124 return _currentScript.getTitle(); 125 } 126 127 public void editorApplied(ObjectEditor editor) 128 { 129 130 } 131 132 public void notifyClosed(SpecifierPanel editor) 133 { 134 this.remove((Component ) editor); 135 setStatusMessage(OK_MSG); 136 repaint(); 137 } 138 139 public DjPersistenceManager getPersistenceManager() 140 { 141 return getSpecifierPanelContainer().getPersistenceManager(); 142 } 143 144 public DjSession getSession() 145 { 146 return _session; 147 } 148 149 protected void setActivePanel(SpecifierPanel panel) throws DjenericException 150 { 151 if (!(panel instanceof Component )) throw new DjenericException(Messages 152 .getString("ScriptRunnerPanel.SpecifierPanelNotComp")); 153 add((Component ) panel, BorderLayout.CENTER); 154 revalidate(); 155 } 156 157 public void choose(String className, DjExtent extent, Variable destVariable, DjSession session, DjOql qbe, 158 EditorEventDefinition[] events, String eventContext) throws DjScriptExecutionException 159 { 160 try 161 { 162 Chooser chooser; 163 if (className == null) chooser = new ChooserPanel(); 164 else chooser = (Chooser) loadClass(className).newInstance(); 165 166 chooser.setSpecifierPanelContainer(this); 167 chooser.construct(this, qbe, session, extent, new ArrayList ()); 168 chooser.addEditorEventListener(this); 169 chooser.setEventContext(eventContext); 170 chooser.setDestinationVariable(destVariable); 171 for (int i = 0; i < events.length; i++) 172 { 173 chooser.registerEvent(events[i].getEventName(), events[i].getEventTitle(), events[i].getOption()); 174 } 175 176 setActivePanel(chooser); 177 178 chooser.requestFocus(); 179 setStatusMessage(OK_MSG); 180 } 181 catch (Exception x) 182 { 183 throw new DjScriptExecutionException(x); 184 } 185 } 186 187 public void dispatch(String className, EditorEventDefinition[] eventDefs, String title, String eventContext) 188 throws DjScriptExecutionException 189 { 190 try 191 { 192 ScriptEventDispatcher dispatcher; 193 194 if (className == null) dispatcher = new DispatchPanel(); 195 else dispatcher = (ScriptEventDispatcher) loadClass(className).newInstance(); 196 197 dispatcher.construct(this); 198 199 dispatcher.setSpecifierPanelContainer(this); 200 dispatcher.setEventContext(eventContext); 201 dispatcher.addEditorEventListener(this); 202 for (int i = 0; i < eventDefs.length; i++) 203 { 204 dispatcher.registerEvent(eventDefs[i].getEventName(), eventDefs[i].getEventTitle(), eventDefs[i].getOption()); 205 } 206 setActivePanel(dispatcher); 207 dispatcher.requestFocus(); 208 setStatusMessage(OK_MSG); 209 } 210 catch (Exception x) 211 { 212 throw new DjScriptExecutionException(x); 213 } 214 } 215 216 public void edit(String editorName, DjSession session, DjList objects, EditorEventDefinition[] events, 217 String eventContext) throws DjScriptExecutionException 218 { 219 try 220 { 221 EditorDefinition editorDef = getEditorDefinition(editorName); 222 223 ObjectEditor editor; 224 if (editorDef.getCustomEditorClass() == null) 225 { 226 editor = new EditorPanel(); 227 } 228 else 229 { 230 editor = (ObjectEditor) loadClass(editorDef.getCustomEditorClass()).newInstance(); 231 } 232 editor.setSession(session); 233 editor.setSpecifierPanelContainer(this); 234 editor.construct(editorDef, objects); 235 236 editor.addEditorEventListener(this); 237 editor.setEventContext(eventContext); 238 for (int i = 0; i < events.length; i++) 239 { 240 editor.registerEvent(events[i].getEventName(), events[i].getEventTitle(), events[i].getOption()); 241 } 242 243 setActivePanel(editor); 244 editor.requestFocus(); 245 setStatusMessage(OK_MSG); 246 } 247 catch (Exception x) 248 { 249 throw new DjScriptExecutionException(x); 250 } 251 } 252 253 public void showPanel(String className, DjScriptExecutionContext scope) throws DjScriptExecutionException 254 { 255 try 256 { 257 Object o = loadClass(className).newInstance(); 258 if (!(o instanceof CustomPanel)) throw new DjScriptExecutionException(Messages 259 .getString("ScriptRunnerPanel.WrongPanelType", className, CustomPanel.class.getName())); 260 261 CustomPanel panel = (CustomPanel) o; 262 panel.setExecutionScope(scope); 263 setActivePanel(panel); 264 panel.requestFocus(); 265 setStatusMessage(OK_MSG); 266 panel.execute(); 267 } 268 catch (Exception x) 269 { 270 throw new DjScriptExecutionException(x); 271 } 272 } 273 274 public void handleEvent(String eventName) throws DjenericException 275 { 276 try 277 { 278 _currentScript.handleEvent(this, eventName); 279 } 280 catch (DjScriptExecutionException se) 281 { 282 throw new DjenericException(se); 283 } 284 } 285 286 void jbInit() throws Exception 287 { 288 border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color (109, 109, 110), 289 new Color (156, 156, 158)); 290 this.setLayout(borderLayout1); 291 _labelStep.setText(Messages.getString("ScriptRunnerPanel.Steps")); 292 _panelHeader.setLayout(flowLayout1); 293 flowLayout1.setAlignment(FlowLayout.LEFT); 294 _panelHeader.add(_labelStep, null); 295 296 } 299 300 public boolean canClose() 301 { 302 303 if (_session.hasOutstandingChanges()) 304 { 305 int result = JOptionPane.showOptionDialog(this, Messages.getString("global.Want2Discard"), Messages 306 .getString("ScriptRunnerPanel.ExitScript"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, 307 null, new String []{Messages.getString("global.Discard"), 308 Messages.getString("global.Cancel")}, null); 309 if (result != 0) 310 { 311 return false; 312 } 313 } 314 315 return true; 316 } 317 318 protected void cleanup() 319 { 320 getSession().close(); 321 } 322 323 public void terminate() 324 { 325 close(); 326 } 327 } | Popular Tags |