KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > userperspective > ScriptEditorPanel


1 package com.genimen.djeneric.tools.modeler.userperspective;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.FlowLayout JavaDoc;
6 import java.awt.Point JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.event.InputEvent JavaDoc;
10 import java.awt.event.InputMethodEvent JavaDoc;
11 import java.awt.event.KeyEvent JavaDoc;
12 import java.awt.event.MouseEvent JavaDoc;
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import javax.swing.BorderFactory JavaDoc;
19 import javax.swing.JButton JavaDoc;
20 import javax.swing.JOptionPane JavaDoc;
21 import javax.swing.JPanel JavaDoc;
22 import javax.swing.JScrollPane JavaDoc;
23 import javax.swing.JSplitPane JavaDoc;
24 import javax.swing.JTree JavaDoc;
25 import javax.swing.border.BevelBorder JavaDoc;
26 import javax.swing.border.Border JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
29 import javax.swing.tree.DefaultTreeModel JavaDoc;
30 import javax.swing.tree.TreePath JavaDoc;
31
32 import com.genimen.djeneric.language.Messages;
33 import com.genimen.djeneric.repository.DjExtent;
34 import com.genimen.djeneric.repository.DjList;
35 import com.genimen.djeneric.repository.DjOql;
36 import com.genimen.djeneric.repository.DjPersistenceManager;
37 import com.genimen.djeneric.repository.DjProperty;
38 import com.genimen.djeneric.repository.DjRelation;
39 import com.genimen.djeneric.repository.DjSession;
40 import com.genimen.djeneric.structure.EditorDefinition;
41 import com.genimen.djeneric.structure.ResourceDefinition;
42 import com.genimen.djeneric.structure.ScriptDefinition;
43 import com.genimen.djeneric.tools.modeler.ModelEditor;
44 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine;
45 import com.genimen.djeneric.tools.scriptengine.core.EditorEventDefinition;
46 import com.genimen.djeneric.tools.scriptengine.core.Node;
47 import com.genimen.djeneric.tools.scriptengine.core.ParseException;
48 import com.genimen.djeneric.tools.scriptengine.core.ScriptRunnerContainer;
49 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
50 import com.genimen.djeneric.tools.scriptengine.core.nodes.ActionNode;
51 import com.genimen.djeneric.tools.scriptengine.core.nodes.ConstructorNode;
52 import com.genimen.djeneric.tools.scriptengine.core.nodes.ControllerNode;
53 import com.genimen.djeneric.tools.scriptengine.core.nodes.EventMappingNode;
54 import com.genimen.djeneric.tools.scriptengine.core.nodes.ScriptNode;
55 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope;
56 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionContext;
57 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
58 import com.genimen.djeneric.tools.scriptengine.core.util.TreeNormalizer;
59 import com.genimen.djeneric.tools.scriptengine.core.util.Variable;
60 import com.genimen.djeneric.ui.DjCloseable;
61 import com.genimen.djeneric.ui.DjCloseableCloser;
62 import com.genimen.djeneric.ui.DjCodeCompleter;
63 import com.genimen.djeneric.ui.DjCodeEditor;
64 import com.genimen.djeneric.ui.DjEditorPositionInfo;
65 import com.genimen.djeneric.ui.Util;
66 import com.genimen.djeneric.util.DjLogger;
67
68 public class ScriptEditorPanel extends JPanel JavaDoc implements DjCloseable, ScriptRunnerContainer
69 {
70   private static final long serialVersionUID = 1L;
71   public static final int SCRIPT_TYPE_NORMAL = 0;
72   public static final int SCRIPT_TYPE_EMBEDDED = 1;
73
74   BorderLayout JavaDoc borderLayoutMain = new BorderLayout JavaDoc();
75   JSplitPane JavaDoc _splMain = new JSplitPane JavaDoc();
76   JScrollPane JavaDoc _scrTree = new JScrollPane JavaDoc();
77   JTree JavaDoc _tree = new JTree JavaDoc();
78   JPanel JavaDoc _pnlSouth = new JPanel JavaDoc();
79   BorderLayout JavaDoc borderLayoutSouth = new BorderLayout JavaDoc();
80   JPanel JavaDoc _pnlButtons = new JPanel JavaDoc();
81   FlowLayout JavaDoc flowLayoutButtons = new FlowLayout JavaDoc();
82   JButton JavaDoc _butOk = new JButton JavaDoc();
83   JButton JavaDoc _butCheckSyntax = new JButton JavaDoc();
84   ScriptDefinition _scriptDefinition;
85   String JavaDoc _orgCode = "";
86   DjCloseableCloser _closer;
87   Border JavaDoc border1;
88
89   ScriptStructureTreeNode _treeRoot = null;
90   DefaultTreeModel JavaDoc _treeModel = null;
91   DjPersistenceManager _mgr;
92   Border JavaDoc border2;
93   int _scriptType = SCRIPT_TYPE_NORMAL;
94   JButton JavaDoc _butCancel = new JButton JavaDoc();
95   DjCodeEditor _edtCode = new DjCodeEditor()
96                                                  {
97                                                    private static final long serialVersionUID = 1L;
98
99                                                    protected void editorKeypressed(KeyEvent JavaDoc e)
100                                                    {
101                                                      super.editorKeypressed(e);
102                                                      ScriptEditorPanel.this.editorKeypressed(e);
103                                                    }
104                                                  };
105   ModelEditor _modelEditor;
106   // For embedded scripts:
107
EditorDefinition _editorDefinition = null;
108   JButton JavaDoc _butDefaultScript = new JButton JavaDoc();
109
110   public ScriptEditorPanel()
111   {
112     try
113     {
114       jbInit();
115       _butDefaultScript.setVisible(false);
116       _edtCode.setCaretPosition(0);
117       _edtCode.requestFocus();
118       refreshCodeTree();
119     }
120     catch (Exception JavaDoc ex)
121     {
122       DjLogger.log(ex);
123     }
124   }
125
126   public ScriptEditorPanel(ScriptDefinition sd, DjCloseableCloser closer, ModelEditor editor)
127   {
128     this();
129     _modelEditor = editor;
130     setPersistenceManager(editor.getPersistenceManager());
131     setCloser(closer);
132     setScriptDefinition(sd);
133     _edtCode.setCaretPosition(0);
134     _edtCode.requestFocus();
135     refreshCodeTree();
136   }
137
138   public void setScriptDefinition(ScriptDefinition sd)
139   {
140     _scriptDefinition = sd;
141     _edtCode.setText(sd.getCode());
142     _edtCode.setCaretPosition(0);
143     _orgCode = sd.getCode();
144     refreshCodeTree();
145   }
146
147   public void setCloser(DjCloseableCloser closer)
148   {
149     _closer = closer;
150   }
151
152   public void setPersistenceManager(DjPersistenceManager mgr)
153   {
154     _mgr = mgr;
155   }
156
157   void jbInit() throws Exception JavaDoc
158   {
159     border2 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color JavaDoc(109, 109, 110),
160                                               new Color JavaDoc(156, 156, 158));
161     _tree.setToggleClickCount(0);
162     border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color JavaDoc(142, 142, 142),
163                                               new Color JavaDoc(99, 99, 99));
164     this.setLayout(borderLayoutMain);
165     _pnlSouth.setLayout(borderLayoutSouth);
166     _pnlButtons.setLayout(flowLayoutButtons);
167     _butOk.setText(Messages.getString("global.Ok"));
168     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
169     {
170       public void actionPerformed(ActionEvent JavaDoc e)
171       {
172         _butOk_actionPerformed(e);
173       }
174     });
175     _butCheckSyntax.setText(Messages.getString("ScriptEditorPanel.CheckSyntax"));
176     _butCheckSyntax.addActionListener(new java.awt.event.ActionListener JavaDoc()
177     {
178       public void actionPerformed(ActionEvent JavaDoc e)
179       {
180         _butCheckSyntax_actionPerformed(e);
181       }
182     });
183     _tree.setShowsRootHandles(true);
184     _tree.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
185     {
186       public void mouseClicked(MouseEvent JavaDoc e)
187       {
188         _tree_mouseClicked(e);
189       }
190     });
191     flowLayoutButtons.setAlignment(FlowLayout.RIGHT);
192     _butCancel.setText(Messages.getString("global.Cancel"));
193     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
194     {
195       public void actionPerformed(ActionEvent JavaDoc e)
196       {
197         _butCancel_actionPerformed(e);
198       }
199     });
200     _edtCode.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
201     _butDefaultScript.setText(Messages.getString("ScriptEditorPanel.DefaultScript"));
202     _butDefaultScript.addActionListener(new ActionListener JavaDoc()
203     {
204       public void actionPerformed(ActionEvent JavaDoc e)
205       {
206         _butDefaultScript_actionPerformed(e);
207       }
208     });
209     this.add(_splMain, BorderLayout.CENTER);
210     _splMain.add(_scrTree, JSplitPane.LEFT);
211     _splMain.add(_edtCode, JSplitPane.RIGHT);
212     _splMain.setDividerLocation(150);
213     _scrTree.getViewport().add(_tree, null);
214     this.add(_pnlSouth, BorderLayout.SOUTH);
215     _pnlSouth.add(_pnlButtons, BorderLayout.EAST);
216     _pnlButtons.add(_butDefaultScript);
217     _pnlButtons.add(_butCheckSyntax, null);
218     _pnlButtons.add(_butCancel, null);
219     _pnlButtons.add(_butOk, null);
220     Util.sizeButtons(_pnlSouth);
221   }
222
223   void _butOk_actionPerformed(ActionEvent JavaDoc e)
224   {
225     if (apply(false)) close();
226   }
227
228   public boolean apply(boolean ignoreErrors)
229   {
230     String JavaDoc code = _edtCode.getText().trim();
231     ScriptNode script = null;
232     if (code.length() != 0)
233     {
234       try
235       {
236         script = validateCode(code);
237       }
238       catch (UnsupportedEncodingException JavaDoc e1)
239       {
240         DjLogger.log(e1);
241       }
242       catch (ParseException e1)
243       {
244         if (ignoreErrors)
245         {
246           _scriptDefinition.setCode(_edtCode.getText());
247           return true;
248         }
249         handleScriptError(e1);
250         int result = JOptionPane.showOptionDialog(this, Messages.getString("ScriptEditorPanel.ScriptHasError", e1
251             .getMessage()), Messages.getString("ScriptEditorPanel.ScriptHasErrorTitle"), JOptionPane.DEFAULT_OPTION,
252                                                   JOptionPane.INFORMATION_MESSAGE, null, new String JavaDoc[]{
253                                                       Messages.getString("global.Save"),
254                                                       Messages.getString("ScriptEditorPanel.Back2Editor")}, null);
255         if (result != 0) return false;
256       }
257     }
258     _scriptDefinition.setCode(_edtCode.getText());
259     if (script != null) _scriptDefinition.setTitle(script.getTitle());
260     return true;
261   }
262
263   private ScriptNode validateCode(String JavaDoc code) throws UnsupportedEncodingException JavaDoc, ParseException,
264       DjScriptExecutionException
265   {
266     ScriptNode script;
267     if (getScriptType() == SCRIPT_TYPE_NORMAL)
268     {
269       DjScriptParserEngine parser = new DjScriptParserEngine(code);
270       script = parser.getTree();
271       script.validate(this);
272     }
273     else
274     {
275       DjScriptParserEngine parser = new DjScriptParserEngine(code);
276       script = parser.getEmbeddedTree();
277       HashMap JavaDoc variables = new HashMap JavaDoc();
278
279       if (_editorDefinition != null) _editorDefinition.getUsageIdAndTypes(variables);
280
281       Iterator JavaDoc it = variables.keySet().iterator();
282       while (it.hasNext())
283       {
284         String JavaDoc name = it.next().toString();
285         script.setExternalGlobal(name, null, variables.get(name).toString());
286       }
287       script.validate(this);
288     }
289     return script;
290   }
291
292   void _butCheckSyntax_actionPerformed(ActionEvent JavaDoc e)
293   {
294     refreshCodeTree();
295   }
296
297   protected void refreshCodeTree()
298   {
299     try
300     {
301       String JavaDoc code = _edtCode.getText();
302       if (code.trim().length() != 0)
303       {
304         ScriptNode script = validateCode(code);
305         script.validate(this);
306         rebuildTree(script);
307         setStatusMessage(Messages.getString("ScriptEditorPanel.ParsedOk"));
308       }
309       else
310       {
311         clearTree();
312         setStatusMessage(Messages.getString("ScriptEditorPanel.NoScriptDefined"));
313       }
314     }
315     catch (UnsupportedEncodingException JavaDoc e)
316     {
317       _edtCode.setStatusMessage(e);
318     }
319     catch (ParseException e)
320     {
321       handleScriptError(e);
322     }
323   }
324
325   private void handleScriptError(ParseException e)
326   {
327     createErrorNode(e);
328
329     _edtCode.setStatusMessage(e.getMessage(), false);
330     if (e.getLine() > 0 && e.getColumn() > 0)
331     {
332       try
333       {
334         _edtCode.setCaretPosition(_edtCode.getLineStartOffset(e.getLine() - 1) + e.getColumn() - 1);
335         _edtCode.requestFocus();
336       }
337       catch (BadLocationException JavaDoc e1)
338       {
339         DjLogger.log(e1);
340       }
341     }
342   }
343
344   protected void createErrorNode(ParseException parseException)
345   {
346     _treeRoot = new ScriptStructureTreeNode(Messages.getString("ScriptEditorPanel.SyntaxError"));
347     _treeModel = new DefaultTreeModel JavaDoc(_treeRoot);
348     _tree.setModel(_treeModel);
349     _treeRoot.setTree(_tree);
350
351     SimpleNode sn = new SimpleNode(0);
352     sn.setLineInfo(parseException.getLine(), parseException.getColumn());
353     sn.setName(parseException.getMessage());
354
355     ScriptStructureTreeNode en = new ScriptStructureTreeNode(_tree, sn);
356
357     _treeRoot.add(en);
358     expandAll(_treeRoot);
359   }
360
361   public void setStatusMessage(String JavaDoc msg)
362   {
363     _edtCode.setStatusMessage(msg, true);
364   }
365
366   void clearTree()
367   {
368     _treeRoot = new ScriptStructureTreeNode(_tree, null);
369     _treeModel = new DefaultTreeModel JavaDoc(_treeRoot);
370     _tree.setModel(_treeModel);
371     _treeModel.nodeStructureChanged(_treeRoot);
372   }
373
374   void rebuildTree(SimpleNode root)
375   {
376     _treeRoot = new ScriptStructureTreeNode(_tree, root);
377     _treeModel = new DefaultTreeModel JavaDoc(_treeRoot);
378     buildTree(_tree, _treeRoot);
379
380     _tree.setModel(_treeModel);
381
382     _treeModel.nodeStructureChanged(_treeRoot);
383     expandAll(_treeRoot);
384   }
385
386   public void expandAll(ScriptStructureTreeNode node)
387   {
388     _tree.expandPath(new TreePath JavaDoc(node.getPath()));
389     int cc = node.getChildCount();
390     for (int i = 0; i < cc; i++)
391     {
392       ScriptStructureTreeNode child = (ScriptStructureTreeNode) node.getChildAt(i);
393       expandAll(child);
394     }
395   }
396
397   void buildTree(JTree JavaDoc tree, ScriptStructureTreeNode treeNode)
398   {
399
400     SimpleNode parseNode = treeNode.getNode();
401     for (int i = 0; i < parseNode.getChildCount(); i++)
402     {
403       SimpleNode child = parseNode.getChild(i);
404       if ((child instanceof ScriptNode) || (child instanceof ControllerNode) || (child instanceof ActionNode)
405           || (child instanceof EventMappingNode) || (child instanceof ConstructorNode))
406       {
407         ScriptStructureTreeNode ptn = new ScriptStructureTreeNode(tree, child);
408         treeNode.insertAsChild(ptn);
409
410         buildTree(tree, ptn);
411       }
412     }
413   }
414
415   public ScriptDefinition getScriptDefinition()
416   {
417     return _scriptDefinition;
418   }
419
420   public boolean canClose()
421   {
422     if (_edtCode.getText().equals(_orgCode)) return true;
423
424     int result = JOptionPane.showOptionDialog(this, Messages.getString("ScriptEditorPanel.DiscardChanges1"), Messages
425         .getString("global.DiscardChanges"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
426                                               new String JavaDoc[]{Messages.getString("global.Discard"),
427                                                   Messages.getString("global.Cancel")}, null);
428     return result == 0;
429   }
430
431   public boolean close()
432   {
433     if (_closer != null) _closer.closeCloseable(this);
434     return true;
435   }
436
437   void _edtCode_caretPositionChanged(InputMethodEvent JavaDoc e)
438   {
439
440   }
441
442   public void choose(String JavaDoc className, DjExtent extent, Variable destVariable, DjSession session, DjOql oql,
443                      EditorEventDefinition[] events, String JavaDoc eventContext)
444   {
445     // dummy method
446
}
447
448   public void dispatch(String JavaDoc className, EditorEventDefinition[] eventDefs, String JavaDoc title, String JavaDoc eventContext)
449   {
450     // dummy method
451
}
452
453   public void edit(String JavaDoc editorName, DjSession session, DjList lst, EditorEventDefinition[] events, String JavaDoc eventContext)
454   {
455     // dummy method
456
}
457
458   public DjPersistenceManager getPersistenceManager()
459   {
460     return _mgr;
461   }
462
463   public void terminate()
464   {
465     // dummy method
466
}
467
468   public ScriptStructureTreeNode getSelectedNode()
469   {
470     if (_tree.getSelectionPath() == null) return null;
471
472     TreePath JavaDoc[] tp = _tree.getSelectionPaths();
473
474     for (int qq = 0; qq < tp.length; qq++)
475     {
476       DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) (tp[qq].getLastPathComponent());
477       if (node instanceof ScriptStructureTreeNode) return (ScriptStructureTreeNode) node;
478     }
479     return null;
480   }
481
482   void _tree_mouseClicked(MouseEvent JavaDoc e)
483   {
484     if (e.getClickCount() > 1)
485     {
486       ScriptStructureTreeNode node = getSelectedNode();
487       if (node != null)
488       {
489         int line = node.getNode().getLine();
490         int column = node.getNode().getColumn();
491
492         try
493         {
494           int idx = _edtCode.getLineStartOffset(line - 1) + column - 1;
495           _edtCode.setCaretPosition(idx);
496           _edtCode.requestFocus();
497         }
498         catch (BadLocationException JavaDoc e1)
499         {
500           // Deliberate ignore
501
}
502       }
503     }
504   }
505
506   public void requestFocus()
507   {
508     _edtCode.requestFocus();
509   }
510
511   public void setOkCancelButtonVisible(boolean b)
512   {
513     _butOk.setVisible(b);
514     _butCancel.setVisible(b);
515   }
516
517   /**
518    * @return
519    */

520   public int getScriptType()
521   {
522     return _scriptType;
523   }
524
525   /**
526    * @param i
527    */

528   public void setScriptType(int i)
529   {
530     _scriptType = i;
531   }
532
533   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
534   {
535     if (canClose()) close();
536   }
537
538   public ResourceDefinition getResourceDefinition(String JavaDoc name)
539   {
540     // This is just a dummy function
541
return null;
542   }
543
544   public Class JavaDoc loadClass(String JavaDoc className)
545   {
546     // This is just a dummy function
547
return null;
548   }
549
550   public void showPanel(String JavaDoc className, DjScriptExecutionContext scope) throws DjScriptExecutionException
551   {
552     // This is just a dummy function
553
}
554
555   public boolean editorExists(String JavaDoc editorName)
556   {
557     return _modelEditor.getViewEditor().getEditorByName(editorName) != null;
558   }
559
560   public EditorDefinition getEditorDefinition(String JavaDoc editorName)
561   {
562     return _modelEditor.getViewEditor().getEditorByName(editorName);
563   }
564
565   protected void editorKeypressed(KeyEvent JavaDoc e)
566   {
567     if ((e.getKeyCode() == KeyEvent.VK_SPACE) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
568     {
569       handlePropertyCompletion(e);
570     }
571   }
572
573   private void handlePropertyCompletion(KeyEvent JavaDoc e)
574   {
575     DjEditorPositionInfo wi = _edtCode.getEditorPositionInfo();
576     String JavaDoc path = wi.getWord();
577
578     DjScriptParserEngine parser = null;
579     ScriptNode scriptNode = null;
580     try
581     {
582       String JavaDoc code = _edtCode.getText();
583       parser = new DjScriptParserEngine(code);
584       if (getScriptType() == SCRIPT_TYPE_NORMAL) scriptNode = parser.getTree();
585       else scriptNode = parser.getEmbeddedTree();
586     }
587     catch (UnsupportedEncodingException JavaDoc uee)
588     {
589       DjLogger.log(uee);
590       setStatusMessage(uee.getMessage());
591       return;
592     }
593     catch (Throwable JavaDoc t)
594     {
595       // try to recover
596
if (parser != null)
597       {
598         Node JavaDoc node = parser.getRootNode();
599         if (node instanceof ScriptNode)
600         {
601           scriptNode = (ScriptNode) node;
602           TreeNormalizer norm = new TreeNormalizer();
603           try
604           {
605             norm.normalize(scriptNode);
606           }
607           catch (ParseException xx)
608           {
609             // ignore this 0ne; this is a go at a script with syntax errors. Lets make the best of it..
610
}
611         }
612       }
613     }
614
615     if (scriptNode == null) return;
616
617     try
618     {
619       DjScriptCompileTimeScope ctxt = new DjScriptCompileTimeScope(scriptNode, getPersistenceManager());
620       scriptNode.declareGlobalsFromConstructor(ctxt);
621       HashMap JavaDoc variables = ctxt.getVariableTypes();
622       variables.put(ScriptNode.EVENT_VARIABLE_NAME, String JavaDoc.class.getName());
623
624       ArrayList JavaDoc events = new ArrayList JavaDoc();
625       if (_editorDefinition != null)
626       {
627         _editorDefinition.getUsageIdAndTypes(variables);
628         String JavaDoc[] ev = _editorDefinition.getEvents();
629         for (int i = 0; i < ev.length; i++)
630           events.add(ev[i]);
631       }
632
633       ctxt.declareGlobals(variables);
634       int cursorLine = _edtCode.getCaretLine();
635
636       try
637       {
638         scriptNode.collectVariables(ctxt, variables, cursorLine);
639         scriptNode.collectEvents(ctxt, events);
640       }
641       catch (DjScriptExecutionException se)
642       {
643         DjLogger.log(se);
644       }
645       showCompletions(variables, wi, path);
646     }
647     catch (Exception JavaDoc x)
648     {
649       DjLogger.log(x);
650       setStatusMessage("Sorry, unable to perform code completion");
651       return;
652     }
653
654   }
655
656   private void showCompletions(HashMap JavaDoc variables, DjEditorPositionInfo wi, String JavaDoc path)
657   {
658     HashMap JavaDoc possibles = new HashMap JavaDoc();
659     int dotIdx = path.indexOf(".");
660     if (dotIdx != -1)
661     {
662       String JavaDoc varName = path.substring(0, dotIdx);
663       wi.setStart(wi.getStart() + varName.length() + 1);
664       String JavaDoc subPath = "";
665       String JavaDoc propStart = path.substring(dotIdx + 1);
666       dotIdx = propStart.lastIndexOf(".");
667       if (dotIdx != -1)
668       {
669         subPath = propStart.substring(0, dotIdx);
670         propStart = propStart.substring(dotIdx + 1);
671         wi.setStart(wi.getStart() + subPath.length() + 1);
672       }
673       String JavaDoc varType = (String JavaDoc) variables.get(varName);
674       if (varType == null)
675       {
676         setStatusMessage("Variabele " + varName + " niet gedefinieerd");
677         return;
678       }
679       try
680       {
681         DjExtent extent = getPersistenceManager().getExtentByObjectType(varType);
682         if (subPath.trim().length() > 0)
683         {
684           extent = getPersistenceManager().getExtentByObjectType(extent.getPropertyType(subPath));
685         }
686         DjProperty idprop = extent.getIdProperty();
687         for (int i = 0; i < extent.getPropertyCount(); i++)
688         {
689           if (extent.getProperty(i).getName().toLowerCase().startsWith(propStart.toLowerCase()))
690           {
691             if (idprop != extent.getProperty(i)) possibles.put(extent.getProperty(i).getName(), extent.getProperty(i)
692                 .getName());
693           }
694         }
695         for (int i = 0; i < extent.getDetailRelationCount(); i++)
696         {
697           DjRelation rel = extent.getDetailRelation(i);
698
699           if (rel.getName().toLowerCase().startsWith(propStart.toLowerCase()))
700           {
701             possibles.put(rel.getName(), rel.getName());
702           }
703         }
704       }
705       catch (Exception JavaDoc onde)
706       {
707         setStatusMessage(onde.getMessage());
708         return;
709       }
710     }
711     else
712     {
713       Iterator JavaDoc it = variables.keySet().iterator();
714       while (it.hasNext())
715       {
716         String JavaDoc key = it.next().toString();
717         if (key.toLowerCase().startsWith(path.toLowerCase()))
718         {
719           possibles.put(key, key);
720         }
721       }
722     }
723     showPopup(possibles, wi);
724   }
725
726   private void showPopup(HashMap JavaDoc possibles, DjEditorPositionInfo wi)
727   {
728     if (possibles.size() > 0)
729     {
730       DjCodeCompleter sel = new DjCodeCompleter(possibles, _edtCode.getEditor(), wi);
731       Point JavaDoc p = _edtCode.getEditor().getCaret().getMagicCaretPosition();
732       int x = 0;
733       int y = 0;
734       if (p != null)
735       {
736         x = p.x;
737         y = p.y;
738       }
739
740       sel.show(_edtCode.getEditor(), x, y);
741     }
742   }
743
744   public void setEditorDefinition(EditorDefinition editorDef)
745   {
746     _editorDefinition = editorDef;
747     _butDefaultScript.setVisible(_editorDefinition != null);
748   }
749
750   public void _butDefaultScript_actionPerformed(ActionEvent JavaDoc e)
751   {
752     if (_editorDefinition == null) return;
753
754     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(5000);
755     sb.append("// Use the events section to add custom buttons to the editor\n");
756     sb.append("events\n{\n myEvent \"My Button\" none; // One of accept, cancel and none\n}\n\n");
757     sb.append("constructor\n{\n // body here\n}\n\n");
758     sb.append("controller\n{\n myEvent -> myAction;\n");
759
760     String JavaDoc[] eventNames = _editorDefinition.getEvents();
761     for (int i = 0; i < eventNames.length; i++)
762     {
763       sb.append(" " + eventNames[i] + " -> traceAction;\n");
764     }
765
766     sb.append("}\n\n");
767     sb.append("action myAction \"Title for myAction\"\n{\n trace(\"My button pressed\"); // body\n}\n\n");
768     sb.append("action traceAction \"Generic trace\"\n{\n trace(\"event: \" + $event); \n}\n");
769
770     int pos = _edtCode.getCaretPosition();
771     _edtCode.insert(sb.toString(), pos);
772   }
773 }
774
Popular Tags