KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > base > AbstractEditorPanel


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.base;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41
42 import com.genimen.djeneric.language.Messages;
43 import com.genimen.djeneric.repository.DjExtent;
44 import com.genimen.djeneric.repository.DjList;
45 import com.genimen.djeneric.repository.DjObject;
46 import com.genimen.djeneric.repository.DjOql;
47 import com.genimen.djeneric.repository.DjPersistenceManager;
48 import com.genimen.djeneric.repository.DjSession;
49 import com.genimen.djeneric.repository.exceptions.DjenericException;
50 import com.genimen.djeneric.structure.EditorDefinition;
51 import com.genimen.djeneric.structure.ExtentUsage;
52 import com.genimen.djeneric.structure.RelationUsage;
53 import com.genimen.djeneric.structure.ScriptDefinition;
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.EventDefinitionNode;
59 import com.genimen.djeneric.tools.scriptengine.core.nodes.ScriptNode;
60 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionContext;
61 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
62 import com.genimen.djeneric.tools.scriptengine.core.util.Variable;
63 import com.genimen.djeneric.tools.specifier.editor.DjenericContextManager;
64 import com.genimen.djeneric.tools.specifier.exceptions.VetoException;
65 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer;
66 import com.genimen.djeneric.tools.specifier.interfaces.EditorEventListener;
67 import com.genimen.djeneric.tools.specifier.interfaces.ObjectEditor;
68 import com.genimen.djeneric.tools.specifier.interfaces.ObjectModelListener;
69 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
70 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewerListener;
71 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer;
72 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeNode;
73
74 public abstract class AbstractEditorPanel extends BasePanel
75     implements
76       ObjectEditor,
77       ObjectModelListener,
78       ObjectViewerListener,
79       ScriptRunnerContainer,
80       EditorEventListener
81 {
82   private DjList _masterObjects;
83   private DjSession _session;
84   private DjenericTreeNode _associatedTreeNode = null;
85   private DjenericContextManager _contextManager;
86   private ArrayList JavaDoc _editorEventListeners = new ArrayList JavaDoc();
87   private String JavaDoc _eventContext = null;
88   private ScriptNode _embeddedScript = null;
89   private ArrayList JavaDoc _allViewers = new ArrayList JavaDoc();
90   private boolean _privateSessionCreated = false;
91
92   abstract public void registerEvent(String JavaDoc eventName, String JavaDoc eventTitle, String JavaDoc eventOption);
93
94   abstract public void registerAdditionalEvent(String JavaDoc eventName, String JavaDoc eventTitle, String JavaDoc eventOption);
95
96   abstract public void requestFocus();
97
98   abstract public void setSelectedIndexOfMaster(int index) throws DjenericException;
99
100   abstract public void construct(EditorDefinition editorDefinition, DjList objects) throws Exception JavaDoc;
101
102   public AbstractEditorPanel()
103   {
104   }
105
106   protected DjSession createDefaultSession(SpecifierPanelContainer editorContainer) throws DjenericException
107   {
108     DjSession session = editorContainer.getPersistenceManager().createSession();
109     session.setValidateObjectsAfterLoading(false);
110     return session;
111   }
112
113   public DjList getMasterObjects()
114   {
115     return _masterObjects;
116   }
117
118   public boolean canClose()
119   {
120     try
121     {
122       if (getSession().hasOutstandingChanges())
123       {
124         int result = JOptionPane.showOptionDialog(this, Messages.getString("global.Want2Discard"), Messages
125             .getString("global.CloseEditor"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
126                                                   new String JavaDoc[]{Messages.getString("global.Discard"),
127                                                       Messages.getString("global.Cancel")}, null);
128         if (result != 0)
129         {
130           return false;
131         }
132       }
133       return true;
134     }
135     catch (DjenericException e)
136     {
137       setStatusMessage(e);
138       return false;
139     }
140   }
141
142   public void beforeObjectCreated(ExtentUsage usage, DjObject obj) throws VetoException
143   {
144     fireScriptEvent(usage.getId() + ".beforecreate");
145   }
146
147   public void beforeObjectDeleted(ExtentUsage usage, DjObject obj) throws VetoException
148   {
149     fireScriptEvent(usage.getId() + ".beforedelete");
150   }
151
152   public void afterRowDeleted(ExtentUsage usage)
153   {
154     fireScriptEvent(usage.getId() + ".afterdelete");
155   }
156
157   public void afterRowInserted(ExtentUsage usage)
158   {
159     fireScriptEvent(usage.getId() + ".aftercreate");
160   }
161
162   public void beforePropertyChanged(ExtentUsage usage, DjObject obj, int propertyIdx, String JavaDoc newValue)
163   {
164     fireScriptEvent(usage.getId() + ".beforepropertychanged", usage.getId(), obj);
165   }
166
167   public void afterPropertyChanged(ExtentUsage usage, DjObject obj, int propertyIdx, String JavaDoc oldValue)
168   {
169     fireScriptEvent(usage.getId() + ".afterpropertychanged", usage.getId(), obj);
170   }
171
172   public void afterObjectCreated(ExtentUsage usage, DjObject obj)
173   {
174   }
175
176   public void afterObjectDeleted(ExtentUsage usage, DjObject obj)
177   {
178   }
179
180   protected void setScriptGlobals()
181   {
182     Iterator JavaDoc it = _allViewers.iterator();
183     while (it.hasNext())
184     {
185       ObjectViewer vw = (ObjectViewer) it.next();
186       DjObject obj = vw.getSelectedValue();
187       getEmbeddedScript().setExternalGlobal(vw.getExtentUsage().getId(), obj, vw.getExtent().getQualifiedObjectType());
188     }
189   }
190
191   protected void fireScriptEvent(String JavaDoc eventId)
192   {
193     fireScriptEvent(eventId, null, null);
194   }
195
196   protected void fireScriptEvent(String JavaDoc eventId, String JavaDoc varName, DjObject varValue)
197   {
198     if (_embeddedScript == null) return;
199
200     try
201     {
202       setScriptGlobals();
203       if (varName != null) _embeddedScript.setExternalGlobal(varName, varValue, varValue.getExtent().getObjectType());
204       _embeddedScript.handleEvent(this, eventId);
205     }
206     catch (DjScriptExecutionException e)
207     {
208       setStatusMessage(e);
209     }
210   }
211
212   protected DjenericContextManager getContextManager()
213   {
214     if (_contextManager == null) _contextManager = new DjenericContextManager(this);
215     return _contextManager;
216   }
217
218   protected void fireEditorEventAndHandleOption(ActionEvent JavaDoc e)
219   {
220     fireEditorEventAndHandleOption(e.getActionCommand());
221   }
222
223   protected void fireEditorEventAndHandleOption(String JavaDoc eventAndOption)
224   {
225     String JavaDoc event = eventAndOption;
226     String JavaDoc option = "none";
227     int idx = event.indexOf(",");
228     if (idx != -1)
229     {
230       option = event.substring(idx + 1);
231       event = event.substring(0, idx);
232     }
233     fireEditorEventAndHandleOption(event, option);
234   }
235
236   protected void fireEditorEventAndHandleOption(String JavaDoc event, String JavaDoc option)
237   {
238     try
239     {
240       if ("accept".equals(option))
241       {
242         apply();
243         close();
244         fireEditorEvent(event);
245       }
246       else if ("cancel".equals(option))
247       {
248         if (canClose())
249         {
250           close();
251           fireEditorEvent(event);
252         }
253       }
254       else fireEditorEvent(event);
255     }
256     catch (Exception JavaDoc x)
257     {
258       setStatusMessage(x);
259     }
260   }
261
262   protected void fireEditorEvent(String JavaDoc eventName) throws DjenericException
263   {
264     String JavaDoc fullEventName;
265
266     if (_eventContext == null) fullEventName = eventName;
267     else fullEventName = _eventContext + "." + eventName;
268
269     for (int i = 0; i < _editorEventListeners.size(); i++)
270     {
271       EditorEventListener eel = (EditorEventListener) _editorEventListeners.get(i);
272       eel.handleEvent(fullEventName);
273     }
274   }
275
276   public void addEditorEventListener(EditorEventListener lsnr)
277   {
278     _editorEventListeners.add(lsnr);
279   }
280
281   public void removeEditorEventListener(EditorEventListener lsnr)
282   {
283     _editorEventListeners.remove(lsnr);
284   }
285
286   public String JavaDoc getEventContext()
287   {
288     return _eventContext;
289   }
290
291   public void setEventContext(String JavaDoc string)
292   {
293     _eventContext = string;
294   }
295
296   public void choose(String JavaDoc className, DjExtent extent, Variable destVariable, DjSession session, DjOql oql,
297                      EditorEventDefinition[] events, String JavaDoc eventContext) throws DjScriptExecutionException
298   {
299     throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.ChooseNotImplemented"));
300   }
301
302   public void dispatch(String JavaDoc className, EditorEventDefinition[] eventDefs, String JavaDoc title, String JavaDoc eventContext)
303       throws DjScriptExecutionException
304   {
305     throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.DispatchNotImplemented"));
306   }
307
308   public boolean editorExists(String JavaDoc editorName)
309   {
310     return false;
311   }
312
313   public void edit(String JavaDoc editorName, DjSession session, DjList lst, EditorEventDefinition[] events, String JavaDoc eventContext)
314       throws DjScriptExecutionException
315   {
316     throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.EditNotImplemented"));
317
318   }
319
320   public void showPanel(String JavaDoc className, DjScriptExecutionContext scope) throws DjScriptExecutionException
321   {
322     throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.ShowPanelNotImplemented"));
323   }
324
325   public void terminate()
326   {
327     close();
328   }
329
330   protected ObjectViewer findObjectViewerChild(Component JavaDoc root)
331   {
332     if (root instanceof JComponent JavaDoc)
333     {
334       JComponent JavaDoc parent = (JComponent JavaDoc) root;
335       for (int i = 0; i < parent.getComponentCount(); i++)
336       {
337         Component JavaDoc c = parent.getComponent(i);
338         if (c instanceof ObjectViewer) return (ObjectViewer) c;
339       }
340
341       // Not found? Then go deeper
342
for (int i = 0; i < parent.getComponentCount(); i++)
343       {
344         ObjectViewer result = findObjectViewerChild(parent.getComponent(i));
345         if (result != null) return result;
346       }
347     }
348     return null;
349   }
350
351   public DjSession getSession() throws DjenericException
352   {
353     // If there is no session set, we create our editor private session here
354
// If it IS set, be sure not to close/commit it when the editor is done.
355
if (_session == null)
356     {
357       _session = createDefaultSession(getSpecifierPanelContainer());
358       _privateSessionCreated = true;
359     }
360
361     return _session;
362   }
363
364   public boolean shouldCommitAndCloseSession()
365   {
366     return _privateSessionCreated;
367   }
368
369   public void setSession(DjSession session)
370   {
371     _session = session;
372   }
373
374   protected void setMasterObjects(DjList list)
375   {
376     _masterObjects = list;
377   }
378
379   protected void setScript(ScriptDefinition script) throws UnsupportedEncodingException JavaDoc, ParseException,
380       DjScriptExecutionException, DjenericException
381   {
382     try
383     {
384       DjScriptParserEngine parser = new DjScriptParserEngine(script.getCode());
385       _embeddedScript = parser.getEmbeddedTree();
386       _embeddedScript.setSession(getSession());
387       setScriptGlobals();
388       getEmbeddedScript().initializeNow(this);
389       setScriptParameters();
390
391       EventDefinitionNode[] events = (EventDefinitionNode[]) getEmbeddedScript().getChildren(EventDefinitionNode.class)
392           .toArray(new EventDefinitionNode[0]);
393
394       for (int i = 0; i < events.length; i++)
395       {
396         registerAdditionalEvent(events[i].getEventName(), events[i].getEventTitle(), events[i].getOption());
397       }
398
399       addEditorEventListener(this);
400     }
401     catch (Exception JavaDoc pe)
402     {
403       JOptionPane.showMessageDialog(this, pe.getMessage(), "DjScript error", JOptionPane.ERROR_MESSAGE);
404     }
405   }
406
407   protected void setScriptParameters() throws DjenericException
408   {
409     DjenericTreeNode node = getAssociatedTreeNode();
410     if (node != null)
411     {
412       ScriptNode embeddedScript = getEmbeddedScript();
413       HashMap JavaDoc parameters = node.getParameters();
414       Iterator JavaDoc it = parameters.keySet().iterator();
415       while (it.hasNext())
416       {
417         String JavaDoc id = it.next().toString();
418         embeddedScript.setParameter(id, (DjObject) parameters.get(id));
419       }
420     }
421   }
422
423   protected ScriptNode getEmbeddedScript()
424   {
425     return _embeddedScript;
426   }
427
428   protected DjenericTreeNode getAssociatedTreeNode()
429   {
430     return _associatedTreeNode;
431   }
432
433   public void setAssociatedTreeNode(DjenericTreeNode node) throws DjenericException
434   {
435     DjObject contextObject = null;
436     if (node instanceof DjenericObjectContainer)
437     {
438       contextObject = ((DjenericObjectContainer) node).getObject();
439     }
440
441     _contextManager = new DjenericContextManager(this);
442     HashMap JavaDoc parameters = node.getParameters();
443     Iterator JavaDoc it = parameters.keySet().iterator();
444     while (it.hasNext())
445     {
446       String JavaDoc id = it.next().toString();
447       _contextManager.addParameter(id, (DjObject) parameters.get(id));
448     }
449
450     _associatedTreeNode = node;
451   }
452
453   public DjPersistenceManager getPersistenceManager() throws DjenericException
454   {
455     return getSession().getPersistenceManager();
456   }
457
458   public void registerViewer(ObjectViewer view)
459   {
460     _allViewers.add(view);
461     view.addModelListener(this);
462     view.addViewerListener(this);
463   }
464
465   public ObjectViewer getViewer(int idx)
466   {
467     return (ObjectViewer) _allViewers.get(idx);
468   }
469
470   public int getViewerCount()
471   {
472     return _allViewers.size();
473   }
474
475   public void notifyViewersDelete(DjExtent extent)
476   {
477     // avoid recursion, see below
478
notifyViewersOfDelete(extent, new ArrayList JavaDoc());
479   }
480
481   protected void notifyViewersOfDelete(DjExtent extent, ArrayList JavaDoc hitlist)
482   {
483     // avoid recursion and superfluous notifications:
484
if (hitlist.contains(extent)) return;
485
486     hitlist.add(extent);
487
488     for (int i = 0; i < getViewerCount(); i++)
489     {
490       // A cascade delete might have caused a delete elsewhere so we need
491
// to consider ALL viewers
492
try
493       {
494         getViewer(i).synchronizeUnderlyingAssociation();
495         // Also notify viewers of any children that might be deleted (cascade
496
// delete)
497
RelationUsage rels[] = getViewer(i).getExtentUsage().getDetailRelations();
498         for (int r = 0; r < rels.length; r++)
499         {
500           notifyViewersOfDelete(rels[r].getDetail().getExtent(), hitlist);
501         }
502       }
503       catch (Exception JavaDoc x)
504       {
505         setStatusMessage(x);
506       }
507     }
508   }
509
510   public void handleEvent(String JavaDoc eventName) throws DjenericException
511   {
512     try
513     {
514       if (_embeddedScript != null)
515       {
516         setScriptGlobals();
517         if (_embeddedScript.handleEvent(this, eventName))
518         {
519           synchronize();
520         }
521       }
522       setStatusMessage(Messages.getString("global.Ok"));
523
524     }
525     catch (DjScriptExecutionException se)
526     {
527       throw new DjenericException(se);
528     }
529   }
530
531   public void synchronize() throws DjenericException
532   {
533     for (int i = 0; i < getViewerCount(); i++)
534     {
535       getViewer(i).synchronize();
536     }
537   }
538
539 }
Popular Tags