KickJava   Java API By Example, From Geeks To Geeks.

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


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.modeler.userperspective;
31
32 import java.util.HashMap JavaDoc;
33
34 import javax.swing.ImageIcon JavaDoc;
35 import javax.swing.JOptionPane JavaDoc;
36 import javax.swing.JTree JavaDoc;
37 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
38 import javax.swing.tree.DefaultTreeModel JavaDoc;
39 import javax.swing.tree.TreeNode JavaDoc;
40 import javax.swing.tree.TreePath JavaDoc;
41
42 import com.genimen.djeneric.language.Messages;
43 import com.genimen.djeneric.repository.DjExtent;
44 import com.genimen.djeneric.repository.DjPersistenceManager;
45 import com.genimen.djeneric.repository.exceptions.DjenericException;
46 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
47 import com.genimen.djeneric.repository.exceptions.QbeFilterException;
48 import com.genimen.djeneric.structure.EditorDefinition;
49 import com.genimen.djeneric.structure.ExtentUsage;
50 import com.genimen.djeneric.structure.RelationDescriptor;
51 import com.genimen.djeneric.structure.RelationUsage;
52 import com.genimen.djeneric.structure.ResourceDefinition;
53 import com.genimen.djeneric.tools.modeler.ModelEditor;
54
55 public class ExtentNode extends DefaultMutableTreeNode JavaDoc
56 {
57   private static final long serialVersionUID = 1L;
58   protected JTree JavaDoc _tree = null;
59   protected DefaultTreeModel JavaDoc _treeModel = null;
60   protected ExtentUsageNode _editorStruct = null;
61   protected ExtentUsage _extentUsage = null;
62
63   ImageIcon JavaDoc _recursiveIcon = ModelEditor.getImageIcon("recursivefolder.gif");
64   ImageIcon JavaDoc _defaultIcon = ModelEditor.getImageIcon("folder.gif");
65   ImageIcon JavaDoc _icon = _defaultIcon;
66
67   public ExtentNode(ExtentUsage usage)
68   {
69     _icon = ModelEditor.getImageIcon("modelview.gif");
70     _extentUsage = usage;
71     updateImageIcon();
72   }
73
74   public ExtentNode(JTree JavaDoc theTree, DefaultTreeModel JavaDoc theModel, DjExtent extent)
75   {
76     _tree = theTree;
77     _treeModel = theModel;
78     _extentUsage = new ExtentUsage(extent);
79     updateImageIcon();
80   }
81
82   public ExtentNode(JTree JavaDoc theTree, DefaultTreeModel JavaDoc theModel, RelationDescriptor detailVia, ExtentUsage usage)
83   {
84     this(theTree, theModel, detailVia.getDetailExtent());
85     _extentUsage = usage;
86     _extentUsage.setIsDetailVia(detailVia);
87     updateImageIcon();
88   }
89
90   public String JavaDoc getPathString()
91   {
92     if (getParent() != null) return getParent().toString() + "/" + toString();
93     return toString();
94   }
95
96   public String JavaDoc getRoleName()
97   {
98     return "Node";
99   }
100
101   public void setExtentUsage(ExtentUsage taus)
102   {
103     _extentUsage = taus;
104     updateImageIcon();
105   }
106
107   public void createSubTree(ExtentNode extentNode, ExtentUsage extentUsage)
108   {
109     extentNode.setExtentUsage(extentUsage);
110     // Beware of endless loop: do not recurse nodes that are marked recursive
111
if (!isRecursive())
112     {
113       for (int i = 0; i < extentUsage.getDetailRelationCount(); i++)
114       {
115         RelationUsage ru = extentUsage.getDetailRelation(i);
116         if (!ru.isValid()) continue;
117
118         RelationDescriptor rdesc = new RelationDescriptor(extentUsage.getDetailRelation(i).getRelation());
119
120         // The structure is already built, we are using the structure to build the tree.
121
// So: instruct addDetail to NOT update the usage structure
122

123         ExtentNode detail = extentNode.addDetail(rdesc, ru.getDetail(), false);
124         // recurse the entire tree here
125
createSubTree(detail, ru.getDetail());
126       }
127     }
128   }
129
130   public EditorDefinition getEditor()
131   {
132     return _extentUsage.getEditor();
133   }
134
135   public ExtentUsageNode getEditorExtentNode()
136   {
137     if (isRecursive()) return getParentExtentNode().getEditorExtentNode();
138
139     if (_editorStruct == null && _extentUsage.getEditor() != null)
140     {
141       EditorDefinition editor = _extentUsage.getEditor();
142       ExtentUsage editorStructure = editor.getUsages();
143
144       _editorStruct = new ExtentUsageNode(editorStructure);
145       _editorStruct.setModel(new DefaultTreeModel JavaDoc(_editorStruct));
146
147       createSubTree(_editorStruct, editorStructure);
148     }
149     return _editorStruct;
150   }
151
152   public void setEditor(EditorDefinition editor)
153   {
154     _extentUsage.setEditor(editor);
155   }
156
157   protected ExtentNode createNewNode(RelationDescriptor relDesc, ExtentUsage usage)
158   {
159     return new ExtentNode(getTree(), getModel(), relDesc, usage);
160   }
161
162   protected ExtentNode createNewNode(DjExtent extent)
163   {
164     return new ExtentNode(getTree(), getModel(), extent);
165   }
166
167   public ExtentNode addDetail(RelationDescriptor relDesc, ExtentUsage detail, boolean updateExtentusageStructure)
168   {
169     if (detail.getExtent() == null) detail.setExtent(relDesc.getDetailExtent());
170     ExtentNode newNode = createNewNode(relDesc, detail);
171     getModel().insertNodeInto(newNode, this, getChildCount());
172
173     try
174     {
175       if (updateExtentusageStructure) getExtentUsage().addDetail(detail, relDesc.getRelation());
176     }
177     catch (ObjectNotDefinedException e)
178     {
179       // If the relation does not exist, do not add it to the structure: ignore silently
180
}
181
182     return newNode;
183   }
184
185   public ExtentNode addDetail(DjExtent extent)
186   {
187     ExtentNode newNode = createNewNode(extent);
188     getModel().insertNodeInto(newNode, this, getChildCount());
189
190     return newNode;
191   }
192
193   public ExtentNode addDetail(ExtentNode node)
194   {
195     getModel().insertNodeInto(node, this, getChildCount());
196     return node;
197   }
198
199   public String JavaDoc getTitle()
200   {
201     return _extentUsage.getTitle();
202   }
203
204   public void setTitle(String JavaDoc title)
205   {
206     _extentUsage.setTitle(title);
207   }
208
209   public void setId(String JavaDoc alias)
210   {
211     _extentUsage.setId(alias.toLowerCase());
212   }
213
214   public String JavaDoc getId()
215   {
216     return _extentUsage.getId();
217   }
218
219   public boolean isEditAllowed()
220   {
221     return _extentUsage.isEditAllowed();
222   }
223
224   public void setEditAllowed(boolean allowed)
225   {
226     _extentUsage.setEditAllowed(allowed);
227   }
228
229   public String JavaDoc getEditorTarget()
230   {
231     return _extentUsage.getEditorTarget();
232   }
233
234   public void setEditorTarget(String JavaDoc editorTarget)
235   {
236     _extentUsage.setEditorTarget(editorTarget);
237   }
238
239   public boolean isInsertAllowed()
240   {
241     return _extentUsage.isInsertAllowed();
242   }
243
244   public void setInsertAllowed(boolean allowed)
245   {
246     _extentUsage.setInsertAllowed(allowed);
247   }
248
249   public boolean isDeleteAllowed()
250   {
251     return _extentUsage.isDeleteAllowed();
252   }
253
254   public void setDeleteAllowed(boolean allowed)
255   {
256     _extentUsage.setDeleteAllowed(allowed);
257   }
258
259   public boolean isRecursive()
260   {
261     return _extentUsage.isRecursive();
262   }
263
264   public void setRecursive(boolean recursive) throws DjenericException
265   {
266
267     if (getParent() == null || ((ExtentNode) getParent()).getExtent() == null) throw new DjenericException(Messages
268         .getString("ExtentNode.CannotRecurseNoParent"));
269
270     DjExtent parentExtent = ((ExtentNode) getParent()).getExtent();
271
272     if (!parentExtent.getInternalCode().equals(getExtent().getInternalCode())) throw new DjenericException(Messages
273         .getString("ExtentNode.CannotRecurseOtherParentType"));
274
275     _extentUsage.setRecursive(recursive);
276   }
277
278   public ExtentUsage getExtentUsage()
279   {
280     return _extentUsage;
281   }
282
283   public DjExtent getExtent()
284   {
285     return _extentUsage.getExtent();
286   }
287
288   public void setExtent(DjExtent extent)
289   {
290     _extentUsage.setExtent(extent);
291   }
292
293   public RelationDescriptor getVia()
294   {
295     return _extentUsage.getVia();
296   }
297
298   public void setModel(DefaultTreeModel JavaDoc mdl)
299   {
300     _treeModel = mdl;
301   }
302
303   public void setTree(JTree JavaDoc tree)
304   {
305     _tree = tree;
306   }
307
308   public JTree JavaDoc getTree()
309   {
310     if (_tree != null) return _tree;
311     ExtentNode parent = getParentExtentNode();
312     if (parent != null) return parent.getTree();
313     else return null;
314   }
315
316   public DefaultTreeModel JavaDoc getModel()
317   {
318     return _treeModel;
319   }
320
321   public ImageIcon JavaDoc getImageIcon()
322   {
323     if (isRecursive()) return _recursiveIcon;
324     return _icon;
325   }
326
327   public ExtentNode getParentExtentNode()
328   {
329     return (ExtentNode) getParent();
330   }
331
332   public void removeFromTree()
333   {
334     getModel().removeNodeFromParent(this);
335   }
336
337   public String JavaDoc toString()
338   {
339     if (getExtent() == null) return Messages.getString("global.View");
340     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(40);
341
342     String JavaDoc s = getTitle();
343
344     if (s != null)
345     {
346       sb.append(s);
347     }
348     s = getId();
349     if (s != null)
350     {
351       sb.append(" (");
352       sb.append(s);
353       sb.append(")");
354     }
355
356     if (sb.length() != 0) return sb.toString();
357
358     if (getVia() != null) return getVia().toString();
359     return getExtent().getName();
360   }
361
362   public void focusChild(ExtentNode focusThis)
363   {
364     for (int i = 0; i < getChildCount(); i++)
365     {
366       ExtentNode node = (ExtentNode) getChildAt(i);
367       TreeNode JavaDoc parentPath[] = getPath();
368       TreeNode JavaDoc childPath[] = new TreeNode JavaDoc[parentPath.length + 1];
369       System.arraycopy(parentPath, 0, childPath, 0, parentPath.length);
370
371       if (node.equals(focusThis))
372       {
373         childPath[parentPath.length] = node;
374         getTree().setSelectionPath(new TreePath JavaDoc(childPath));
375         break;
376       }
377     }
378   }
379
380   public boolean hasExtent(DjExtent tab)
381   {
382     for (int i = 0; i < getChildCount(); i++)
383     {
384       ExtentNode node = (ExtentNode) getChildAt(i);
385       if (node.getExtent() == tab) return true;
386     }
387     return false;
388   }
389
390   public boolean hasExtent(RelationDescriptor rd)
391   {
392     for (int i = 0; i < getChildCount(); i++)
393     {
394       ExtentNode node = (ExtentNode) getChildAt(i);
395       try
396       {
397         if ((node.getVia() != null) && node.getExtent() == rd.getDetailExtent()
398             && (node.getVia().getRelation() == rd.getRelation())) return true;
399       }
400       catch (ObjectNotDefinedException e)
401       {
402         // Silently ignore a missing relation
403
}
404     }
405     return false;
406   }
407
408   public ExtentNode[] getChildren()
409   {
410     ExtentNode[] result = new ExtentNode[getChildCount()];
411     for (int i = 0; i < result.length; i++)
412     {
413       result[i] = (ExtentNode) getChildAt(i);
414     }
415     return result;
416   }
417
418   public void setDescriptorExpression(String JavaDoc descriptor)
419   {
420     _extentUsage.setDescriptorExpression(descriptor);
421   }
422
423   public String JavaDoc getDescriptorExpression()
424   {
425     return _extentUsage.getDescriptorExpression();
426   }
427
428   public boolean shouldDeleteFromStructure(ModelEditor editor)
429   {
430     DjPersistenceManager mgr = editor.getPersistenceManager();
431
432     if (getVia() != null && !getVia().isvalid())
433     {
434       return true;
435     }
436
437     if (getExtent() != null)
438     {
439       if (mgr.findExtent(getExtent()) == null)
440       {
441         JOptionPane.showMessageDialog(editor, Messages.getString("ExtentNode.Warning", getExtent().getName(),
442                                                                  getPathString(), getRoleName()));
443
444         return true;
445       }
446     }
447     ExtentNode[] children = getChildren();
448     for (int i = 0; i < children.length; i++)
449     {
450       if (children[i].shouldDeleteFromStructure(editor)) getModel().removeNodeFromParent(children[i]);
451     }
452     if (getEditorExtentNode() != null)
453     {
454       if (getEditorExtentNode().shouldDeleteFromStructure(editor))
455       {
456         _editorStruct = null;
457       }
458     }
459     return false;
460   }
461
462   /**
463    * @return
464    */

465   public String JavaDoc getOqlFilter()
466   {
467     return _extentUsage.getOqlExpression();
468   }
469
470   public void setOqlFilter(String JavaDoc string)
471   {
472     _extentUsage.setOqlExpression(string);
473   }
474
475   public void validate(String JavaDoc path) throws DjenericException
476   {
477     String JavaDoc currentPath = path + "/" + toString();
478     try
479     {
480       ExtentUsage usg = getExtentUsage();
481       if (usg != null) usg.validate();
482       ExtentNode[] children = getChildren();
483       for (int i = 0; i < children.length; i++)
484       {
485         children[i].validate(currentPath);
486       }
487     }
488     catch (QbeFilterException qbef)
489     {
490       throw new DjenericException(Messages.getString("ExtentNode.InvalidConstantInFilter", currentPath, qbef
491           .getPropertyWithError()));
492     }
493     catch (ObjectNotDefinedException onde)
494     {
495       throw new DjenericException(Messages.getString("ExtentNode.InvalidPropInFilter", currentPath));
496     }
497   }
498
499   public ResourceDefinition getImageIconResource()
500   {
501     return getExtentUsage().getImageIconResource();
502   }
503
504   public void setImageIconResource(ResourceDefinition imageResource)
505   {
506     if (imageResource != null && imageResource.getBytes().length == 0) imageResource = null;
507
508     getExtentUsage().setImageIconResource(imageResource);
509     updateImageIcon();
510
511   }
512
513   protected void updateImageIcon()
514   {
515     ResourceDefinition imageResource = getImageIconResource();
516
517     if (imageResource != null)
518     {
519       _icon = new ImageIcon JavaDoc(imageResource.getBytes());
520     }
521     else
522     {
523       if (getExtentUsage() == null || getExtentUsage().getExtent() == null) _icon = ModelEditor
524           .getImageIcon("modelview.gif");
525       else _icon = _defaultIcon;
526     }
527   }
528
529   public String JavaDoc getCustomNodeClass()
530   {
531     return _extentUsage.getCustomNodeClass();
532   }
533
534   public void setCustomNodeClass(String JavaDoc customNodeClass)
535   {
536     _extentUsage.setCustomNodeClass(customNodeClass);
537   }
538
539   public void collectParameters(HashMap JavaDoc parameterMap)
540   {
541     if (!parameterMap.containsKey(getId()) && getId() != null) parameterMap.put(getId(), getExtent());
542     ExtentNode parent = getParentExtentNode();
543     if (parent != null) parent.collectParameters(parameterMap);
544   }
545
546 }
Popular Tags