1 30 package com.genimen.djeneric.tools.specifier.base; 31 32 import java.awt.Component ; 33 import java.awt.event.ActionEvent ; 34 import java.io.UnsupportedEncodingException ; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 import javax.swing.JComponent ; 40 import javax.swing.JOptionPane ; 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 _editorEventListeners = new ArrayList (); 87 private String _eventContext = null; 88 private ScriptNode _embeddedScript = null; 89 private ArrayList _allViewers = new ArrayList (); 90 private boolean _privateSessionCreated = false; 91 92 abstract public void registerEvent(String eventName, String eventTitle, String eventOption); 93 94 abstract public void registerAdditionalEvent(String eventName, String eventTitle, String 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 ; 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 []{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 newValue) 163 { 164 fireScriptEvent(usage.getId() + ".beforepropertychanged", usage.getId(), obj); 165 } 166 167 public void afterPropertyChanged(ExtentUsage usage, DjObject obj, int propertyIdx, String 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 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 eventId) 192 { 193 fireScriptEvent(eventId, null, null); 194 } 195 196 protected void fireScriptEvent(String eventId, String 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 e) 219 { 220 fireEditorEventAndHandleOption(e.getActionCommand()); 221 } 222 223 protected void fireEditorEventAndHandleOption(String eventAndOption) 224 { 225 String event = eventAndOption; 226 String 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 event, String 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 x) 257 { 258 setStatusMessage(x); 259 } 260 } 261 262 protected void fireEditorEvent(String eventName) throws DjenericException 263 { 264 String 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 getEventContext() 287 { 288 return _eventContext; 289 } 290 291 public void setEventContext(String string) 292 { 293 _eventContext = string; 294 } 295 296 public void choose(String className, DjExtent extent, Variable destVariable, DjSession session, DjOql oql, 297 EditorEventDefinition[] events, String eventContext) throws DjScriptExecutionException 298 { 299 throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.ChooseNotImplemented")); 300 } 301 302 public void dispatch(String className, EditorEventDefinition[] eventDefs, String title, String eventContext) 303 throws DjScriptExecutionException 304 { 305 throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.DispatchNotImplemented")); 306 } 307 308 public boolean editorExists(String editorName) 309 { 310 return false; 311 } 312 313 public void edit(String editorName, DjSession session, DjList lst, EditorEventDefinition[] events, String eventContext) 314 throws DjScriptExecutionException 315 { 316 throw new DjScriptExecutionException(Messages.getString("AbstractEditorPanel.EditNotImplemented")); 317 318 } 319 320 public void showPanel(String 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 root) 331 { 332 if (root instanceof JComponent ) 333 { 334 JComponent parent = (JComponent ) root; 335 for (int i = 0; i < parent.getComponentCount(); i++) 336 { 337 Component c = parent.getComponent(i); 338 if (c instanceof ObjectViewer) return (ObjectViewer) c; 339 } 340 341 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 (_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 , 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 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 parameters = node.getParameters(); 414 Iterator it = parameters.keySet().iterator(); 415 while (it.hasNext()) 416 { 417 String 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 parameters = node.getParameters(); 443 Iterator it = parameters.keySet().iterator(); 444 while (it.hasNext()) 445 { 446 String 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 notifyViewersOfDelete(extent, new ArrayList ()); 479 } 480 481 protected void notifyViewersOfDelete(DjExtent extent, ArrayList hitlist) 482 { 483 if (hitlist.contains(extent)) return; 485 486 hitlist.add(extent); 487 488 for (int i = 0; i < getViewerCount(); i++) 489 { 490 try 493 { 494 getViewer(i).synchronizeUnderlyingAssociation(); 495 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 x) 504 { 505 setStatusMessage(x); 506 } 507 } 508 } 509 510 public void handleEvent(String 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 |