KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > editor > ScriptRunnerPanel


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.editor;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.FlowLayout JavaDoc;
36 import java.io.UnsupportedEncodingException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 import javax.swing.BorderFactory JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.border.BevelBorder JavaDoc;
44 import javax.swing.border.Border JavaDoc;
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 JavaDoc borderLayout1 = new BorderLayout JavaDoc();
80   DjSession _session;
81   public final static String JavaDoc OK_MSG = Messages.getString("global.Ok");
82   ScriptNode _currentScript = null;
83   Border JavaDoc border1;
84   JPanel JavaDoc _panelHeader = new JPanel JavaDoc();
85   JLabel JavaDoc _labelStep = new JLabel JavaDoc();
86   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
87
88   public ScriptRunnerPanel(SpecifierPanelContainer editorContainer)
89   {
90     try
91     {
92       setSpecifierPanelContainer(editorContainer);
93
94       jbInit();
95     }
96     catch (Exception JavaDoc ex)
97     {
98       DjLogger.log(ex);
99     }
100   }
101
102   public void setScript(String JavaDoc script) throws UnsupportedEncodingException JavaDoc, 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 JavaDoc 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 JavaDoc) 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 JavaDoc)) throw new DjenericException(Messages
152         .getString("ScriptRunnerPanel.SpecifierPanelNotComp"));
153     add((Component JavaDoc) panel, BorderLayout.CENTER);
154     revalidate();
155   }
156
157   public void choose(String JavaDoc className, DjExtent extent, Variable destVariable, DjSession session, DjOql qbe,
158                      EditorEventDefinition[] events, String JavaDoc 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 JavaDoc());
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 JavaDoc x)
182     {
183       throw new DjScriptExecutionException(x);
184     }
185   }
186
187   public void dispatch(String JavaDoc className, EditorEventDefinition[] eventDefs, String JavaDoc title, String JavaDoc 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 JavaDoc x)
211     {
212       throw new DjScriptExecutionException(x);
213     }
214   }
215
216   public void edit(String JavaDoc editorName, DjSession session, DjList objects, EditorEventDefinition[] events,
217                    String JavaDoc 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 JavaDoc x)
248     {
249       throw new DjScriptExecutionException(x);
250     }
251   }
252
253   public void showPanel(String JavaDoc className, DjScriptExecutionContext scope) throws DjScriptExecutionException
254   {
255     try
256     {
257       Object JavaDoc 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 JavaDoc x)
269     {
270       throw new DjScriptExecutionException(x);
271     }
272   }
273
274   public void handleEvent(String JavaDoc 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 JavaDoc
287   {
288     border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color JavaDoc(109, 109, 110),
289                                               new Color JavaDoc(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     // TBI:
297
// this.add(_panelHeader, BorderLayout.NORTH);
298
}
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 JavaDoc[]{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