KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > dvmodeler > DataMapTreeModel


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.dataview.dvmodeler;
58
59 import javax.swing.tree.*;
60 import javax.swing.event.*;
61 import java.util.*;
62
63 /**
64  *
65  * @author Nataliya Kholodna
66  * @version 1.0
67  */

68
69 public class DataMapTreeModel implements TreeModel{
70   private List dataMaps;
71   private String JavaDoc root = "root";
72     /** Listeners. */
73   private Vector treeModelListeners;
74
75   public DataMapTreeModel() {
76   }
77
78   public void setDataMaps(List dataMaps){
79     this.dataMaps = dataMaps;
80     fireTreeStructureChanged(new TreeModelEvent(this, new TreePath(root)));
81   }
82
83
84   /**
85    * Returns the child of parent at index index in the parent's child array.
86    */

87   public Object JavaDoc getChild(Object JavaDoc parent, int index) {
88     if (root.equals(parent)) {
89       return dataMaps.get(index);
90     } else if (parent instanceof DataMap){
91       DataMap p = (DataMap)parent;
92       return p.getObjEntity(index);
93     } else if (parent instanceof ObjEntity){
94       ObjEntity p = (ObjEntity)parent;
95       return p.getObjEntityView(index);
96     } else if (parent instanceof ObjEntityView){
97       ObjEntityView p = (ObjEntityView)parent;
98       return p.getObjEntityViewField(index);
99     } else
100       return null;
101   }
102    /**
103    * Returns the number of children of parent.
104    */

105   public int getChildCount(Object JavaDoc parent) {
106     if (root.equals(parent)) {
107       return dataMaps.size();
108     } else if (parent instanceof DataMap){
109       DataMap p = (DataMap)parent;
110       return p.getObjEntityCount();
111     } else if (parent instanceof ObjEntity){
112       ObjEntity p = (ObjEntity)parent;
113       return p.getObjEntityViewCount();
114     } else if (parent instanceof ObjEntityView){
115       ObjEntityView p = (ObjEntityView)parent;
116       return p.getObjEntityViewFieldCount();
117     } else
118       return 0;
119   }
120
121   /**
122    * Returns the index of child in parent.
123    */

124   public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
125     if (root.equals(parent)) {
126       return dataMaps.indexOf(child);
127     } else if (parent instanceof DataMap){
128       DataMap p = (DataMap)parent;
129       return p.getIndexOfObjEntity((ObjEntity)child);
130     } else if (parent instanceof ObjEntity){
131       ObjEntity p = (ObjEntity)parent;
132       return p.getIndexOfObjEntityView((ObjEntityView)child);
133     } else if (parent instanceof ObjEntityView){
134       ObjEntityView p = (ObjEntityView)parent;
135       return p.getIndexOfObjEntityViewField((ObjEntityViewField)child);
136     } else
137       return 0;
138   }
139
140   /**
141    * Returns the root of the tree.
142    */

143   public Object JavaDoc getRoot() {
144     return root;
145   }
146
147   /**
148    * Returns true if node is a leaf.
149    */

150   public boolean isLeaf(Object JavaDoc node) {
151     if (root.equals(node)) {
152       return dataMaps == null || dataMaps.isEmpty();
153     } else if (node instanceof DataMap){
154       DataMap p = (DataMap)node;
155       return p.getObjEntityCount() == 0;
156     } else if (node instanceof ObjEntity){
157       ObjEntity p = (ObjEntity)node;
158       return p.getObjEntityViewCount() == 0;
159     } else if (node instanceof ObjEntityView){
160       ObjEntityView p = (ObjEntityView)node;
161       return p.getObjEntityViewFieldCount() == 0;
162     } else if (node instanceof ObjEntityViewField){
163       return true;
164     } else
165       return true;
166   }
167
168
169
170   /**
171    * Messaged when the user has altered the value for the item
172    * identified by path to newValue. Not used by this model.
173    */

174   public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
175     System.out.println("*** valueForPathChanged : "
176                        + path + " --> " + newValue);
177   }
178
179   public synchronized void removeTreeModelListener(TreeModelListener l) {
180     if (treeModelListeners != null && treeModelListeners.contains(l)) {
181       Vector v = (Vector) treeModelListeners.clone();
182       v.removeElement(l);
183       treeModelListeners = v;
184     }
185   }
186   public synchronized void addTreeModelListener(TreeModelListener l) {
187     Vector v = treeModelListeners == null ? new Vector(2) : (Vector) treeModelListeners.clone();
188     if (!v.contains(l)) {
189       v.addElement(l);
190       treeModelListeners = v;
191     }
192   }
193   protected void fireTreeNodesChanged(TreeModelEvent e) {
194     if (treeModelListeners != null) {
195       Vector listeners = treeModelListeners;
196       int count = listeners.size();
197       for (int i = 0; i < count; i++) {
198         ((TreeModelListener) listeners.elementAt(i)).treeNodesChanged(e);
199       }
200     }
201   }
202   protected void fireTreeNodesInserted(TreeModelEvent e) {
203     if (treeModelListeners != null) {
204       Vector listeners = treeModelListeners;
205       int count = listeners.size();
206       for (int i = 0; i < count; i++) {
207         ((TreeModelListener) listeners.elementAt(i)).treeNodesInserted(e);
208       }
209     }
210   }
211   protected void fireTreeNodesRemoved(TreeModelEvent e) {
212     if (treeModelListeners != null) {
213       Vector listeners = treeModelListeners;
214       int count = listeners.size();
215       for (int i = 0; i < count; i++) {
216         ((TreeModelListener) listeners.elementAt(i)).treeNodesRemoved(e);
217       }
218     }
219   }
220   protected void fireTreeStructureChanged(TreeModelEvent e) {
221     if (treeModelListeners != null) {
222       Vector listeners = treeModelListeners;
223       int count = listeners.size();
224       for (int i = 0; i < count; i++) {
225         ((TreeModelListener) listeners.elementAt(i)).treeStructureChanged(e);
226       }
227     }
228   }
229
230   public void replaceObjEntityView(ObjEntity oldObjEntity,
231                                    int oldIndex,
232                                    ObjEntity newObjEntity,
233                                    ObjEntityView objEntityView) {
234
235     DataMap oldDataMap = oldObjEntity.getDataMap();
236     fireTreeNodesRemoved(new TreeModelEvent(
237         this,
238         new Object JavaDoc[] {root, oldDataMap, oldObjEntity},
239         new int[] {oldIndex},
240         new Object JavaDoc[] {objEntityView}));
241
242     DataMap newDataMap = newObjEntity.getDataMap();
243
244     int newIndex = newObjEntity.getIndexOfObjEntityView(objEntityView);
245     fireTreeNodesInserted(new TreeModelEvent(
246         this,
247         new Object JavaDoc[] {root, newDataMap, newObjEntity},
248         new int[] {newIndex},
249         new Object JavaDoc[] {objEntityView}));
250
251   }
252
253   public TreePath objEntityViewAdded(ObjEntityView view) {
254     ObjEntity entity = view.getObjEntity();
255     if (entity == null)
256       return null;
257     DataMap dataMap = entity.getDataMap();
258     int index = entity.getIndexOfObjEntityView(view);
259     fireTreeNodesInserted(new TreeModelEvent(
260         this,
261         new Object JavaDoc[] {root, dataMap, entity},
262         new int[] {index},
263         new Object JavaDoc[] {view}));
264     return new TreePath(new Object JavaDoc[] {root, dataMap, entity, view});
265   }
266
267   public void objEntityViewRemoved(ObjEntity entity, ObjEntityView view, int index) {
268     DataMap dataMap = entity.getDataMap();
269     fireTreeNodesRemoved(new TreeModelEvent(
270         this,
271         new Object JavaDoc[] {root, dataMap, entity},
272         new int[] {index},
273         new Object JavaDoc[] {view}));
274   }
275
276   public void objEntityViewsRemoved(Map removingViews) {
277     HashMap views = (HashMap)removingViews;
278     for(Iterator j = views.keySet().iterator(); j.hasNext();){
279       ObjEntityView view = (ObjEntityView)j.next();
280       ObjEntity entity = view.getObjEntity();
281       Integer JavaDoc index = (Integer JavaDoc)views.get(view);
282       DataMap dataMap = entity.getDataMap();
283
284       fireTreeNodesRemoved(new TreeModelEvent(
285           this,
286           new Object JavaDoc[] {root, dataMap, entity},
287           new int[] {index.intValue()},
288           new Object JavaDoc[] {view}));
289     }
290   }
291
292
293   public TreePath objEntityViewChanged(ObjEntityView view) {
294     ObjEntity entity = view.getObjEntity();
295     if (entity == null)
296       return null;
297     DataMap dataMap = entity.getDataMap();
298     int index = entity.getIndexOfObjEntityView(view);
299     fireTreeNodesChanged(new TreeModelEvent(
300         this,
301         new Object JavaDoc[] {root, dataMap, entity},
302         new int[] {index},
303         new Object JavaDoc[] {view}));
304     return new TreePath(new Object JavaDoc[] {root, dataMap, entity, view});
305   }
306
307   public TreePath fieldAdded(ObjEntityViewField field) {
308     ObjEntityView owner = field.getObjEntityView();
309     ObjEntity entity = owner.getObjEntity();
310     if (entity == null)
311       return null;
312     DataMap dataMap = entity.getDataMap();
313     int index = owner.getIndexOfObjEntityViewField(field);
314     fireTreeNodesInserted(new TreeModelEvent(
315         this,
316         new Object JavaDoc[] {root, dataMap, entity, owner},
317         new int[] {index},
318         new Object JavaDoc[] {field}));
319     return new TreePath(new Object JavaDoc[] {root, dataMap, entity, owner, field});
320   }
321
322   public void fieldRemoved(ObjEntityViewField field, int index) {
323     ObjEntityView owner = field.getObjEntityView();
324     ObjEntity entity = owner.getObjEntity();
325
326     DataMap dataMap = entity.getDataMap();
327     fireTreeNodesRemoved(new TreeModelEvent(
328         this,
329         new Object JavaDoc[] {root, dataMap, entity, owner},
330         new int[] {index},
331         new Object JavaDoc[] {field}));
332
333   }
334
335   public TreePath fieldChanged(ObjEntityViewField field) {
336     ObjEntityView owner = field.getObjEntityView();
337     ObjEntity entity = owner.getObjEntity();
338     if (entity == null)
339       return null;
340     DataMap dataMap = entity.getDataMap();
341     int index = owner.getIndexOfObjEntityViewField(field);
342     fireTreeNodesChanged(new TreeModelEvent(
343         this,
344         new Object JavaDoc[] {root, dataMap, entity, owner},
345         new int[] {index},
346         new Object JavaDoc[] {field}));
347     return new TreePath(new Object JavaDoc[] {root, dataMap, entity, owner, field});
348   }
349 }
350
Popular Tags