KickJava   Java API By Example, From Geeks To Geeks.

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


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 DataViewTreeModel implements TreeModel{
70   private List dataViews;
71   private String JavaDoc root = "root";
72     /** Listeners. */
73   private Vector treeModelListeners;
74
75   public DataViewTreeModel() {
76   }
77
78   public void setDataViews(List dataViews){
79     this.dataViews = dataViews;
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 dataViews.get(index);
90     } else if (parent instanceof DataView){
91       DataView p = (DataView)parent;
92       return p.getObjEntityView(index);
93     } else if (parent instanceof ObjEntityView){
94       ObjEntityView p = (ObjEntityView)parent;
95       return p.getObjEntityViewField(index);
96     } else
97       return null;
98   }
99    /**
100    * Returns the number of children of parent.
101    */

102   public int getChildCount(Object JavaDoc parent) {
103     if (root.equals(parent)) {
104       return dataViews.size();
105     } else if (parent instanceof DataView){
106       DataView p = (DataView)parent;
107       return p.getObjEntityViewCount();
108     } else if (parent instanceof ObjEntityView){
109       ObjEntityView p = (ObjEntityView)parent;
110       return p.getObjEntityViewFieldCount();
111     } else if (parent instanceof ObjEntityViewField){
112       return 0;
113     } else
114       return -1;
115   }
116
117   /**
118    * Returns the index of child in parent.
119    */

120   public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
121     if (root.equals(parent)) {
122       return dataViews.indexOf(child);
123     } else if (parent instanceof DataView){
124       DataView p = (DataView)parent;
125       return p.getIndexOfObjEntityView((ObjEntityView)child);
126     } else if (parent instanceof ObjEntityView){
127       ObjEntityView p = (ObjEntityView)parent;
128       return p.getIndexOfObjEntityViewField((ObjEntityViewField)child);
129     } else
130       return -1;
131   }
132
133   /**
134    * Returns the root of the tree.
135    */

136   public Object JavaDoc getRoot() {
137     return root;
138   }
139
140   /**
141    * Returns true if node is a leaf.
142    */

143   public boolean isLeaf(Object JavaDoc node) {
144     if (root.equals(node)) {
145       return dataViews == null || dataViews.isEmpty();
146     } else if (node instanceof DataView){
147       DataView p = (DataView)node;
148       return p.getObjEntityViewCount() == 0;
149     } else if (node instanceof ObjEntityView){
150       ObjEntityView p = (ObjEntityView)node;
151       return p.getObjEntityViewFieldCount() == 0;
152     } else if (node instanceof ObjEntityViewField){
153       return true;
154     } else
155       return true;
156   }
157
158   public TreePath dataViewAdded(DataView dataView) {
159     int index = dataViews.indexOf(dataView);
160     if (index < 0) return null;
161     fireTreeNodesInserted(new TreeModelEvent(
162         this,
163         new TreePath(root),
164         new int[] {index},
165         new Object JavaDoc[] {dataView}));
166     return new TreePath(new Object JavaDoc[] {root, dataView});
167   }
168
169   public void dataViewRemoved(DataView dataView, int index) {
170
171     fireTreeNodesRemoved(new TreeModelEvent(
172         this,
173         new TreePath(root),
174         new int[] {index},
175         new Object JavaDoc[] {dataView}));
176   }
177
178   public TreePath dataViewChanged(DataView dataView) {
179     int index = dataViews.indexOf(dataView);
180     if (index < 0) return null;
181     fireTreeNodesChanged(new TreeModelEvent(
182         this,
183         new TreePath(root),
184         new int[] {index},
185         new Object JavaDoc[] {dataView}));
186     return new TreePath(new Object JavaDoc[] {root, dataView});
187   }
188
189   public TreePath objEntityViewAdded(ObjEntityView view) {
190     DataView owner = view.getDataView();
191     int index = owner.getIndexOfObjEntityView(view);
192     fireTreeNodesInserted(new TreeModelEvent(
193         this,
194         new Object JavaDoc[] {root, owner},
195         new int[] {index},
196         new Object JavaDoc[] {view}));
197     return new TreePath(new Object JavaDoc[] {root, owner, view});
198   }
199
200   public void objEntityViewRemoved(ObjEntityView view, int index) {
201     DataView owner = view.getDataView();
202     fireTreeNodesRemoved(new TreeModelEvent(
203         this,
204         new Object JavaDoc[] {root, owner},
205         new int[] {index},
206         new Object JavaDoc[] {view}));
207   }
208
209   public TreePath objEntityViewChanged(ObjEntityView view) {
210     DataView owner = view.getDataView();
211     int index = owner.getIndexOfObjEntityView(view);
212     fireTreeNodesChanged(new TreeModelEvent(
213         this,
214         new Object JavaDoc[] {root, owner},
215         new int[] {index},
216         new Object JavaDoc[] {view}));
217     return new TreePath(new Object JavaDoc[] {root, owner, view});
218   }
219
220   public TreePath fieldAdded(ObjEntityViewField field) {
221     ObjEntityView owner = field.getObjEntityView();
222     DataView dataView = owner.getDataView();
223     int index = owner.getIndexOfObjEntityViewField(field);
224     fireTreeNodesInserted(new TreeModelEvent(
225         this,
226         new Object JavaDoc[] {root, dataView, owner},
227         new int[] {index},
228         new Object JavaDoc[] {field}));
229     return new TreePath(new Object JavaDoc[] {root, dataView, owner, field});
230   }
231
232   public void fieldRemoved(ObjEntityViewField field, int index) {
233     ObjEntityView owner = field.getObjEntityView();
234     DataView dataView = owner.getDataView();
235     fireTreeNodesRemoved(new TreeModelEvent(
236         this,
237         new Object JavaDoc[] {root, dataView, owner},
238         new int[] {index},
239         new Object JavaDoc[] {field}));
240   }
241
242   public TreePath fieldChanged(ObjEntityViewField field) {
243     ObjEntityView owner = field.getObjEntityView();
244     DataView dataView = owner.getDataView();
245     int index = owner.getIndexOfObjEntityViewField(field);
246     fireTreeNodesChanged(new TreeModelEvent(
247         this,
248         new Object JavaDoc[] {root, dataView, owner},
249         new int[] {index},
250         new Object JavaDoc[] {field}));
251     return new TreePath(new Object JavaDoc[] {root, dataView, owner, field});
252   }
253
254   /**
255    * Messaged when the user has altered the value for the item
256    * identified by path to newValue. Not used by this model.
257    */

258   public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
259     System.out.println("*** valueForPathChanged : "
260                        + path + " --> " + newValue);
261   }
262
263   public synchronized void removeTreeModelListener(TreeModelListener l) {
264     if (treeModelListeners != null && treeModelListeners.contains(l)) {
265       Vector v = (Vector) treeModelListeners.clone();
266       v.removeElement(l);
267       treeModelListeners = v;
268     }
269   }
270   public synchronized void addTreeModelListener(TreeModelListener l) {
271     Vector v = treeModelListeners == null ? new Vector(2) : (Vector) treeModelListeners.clone();
272     if (!v.contains(l)) {
273       v.addElement(l);
274       treeModelListeners = v;
275     }
276   }
277   protected void fireTreeNodesChanged(TreeModelEvent e) {
278     if (treeModelListeners != null) {
279       Vector listeners = treeModelListeners;
280       int count = listeners.size();
281       for (int i = 0; i < count; i++) {
282         ((TreeModelListener) listeners.elementAt(i)).treeNodesChanged(e);
283       }
284     }
285   }
286   protected void fireTreeNodesInserted(TreeModelEvent e) {
287     if (treeModelListeners != null) {
288       Vector listeners = treeModelListeners;
289       int count = listeners.size();
290       for (int i = 0; i < count; i++) {
291         ((TreeModelListener) listeners.elementAt(i)).treeNodesInserted(e);
292       }
293     }
294   }
295   protected void fireTreeNodesRemoved(TreeModelEvent e) {
296     if (treeModelListeners != null) {
297       Vector listeners = treeModelListeners;
298       int count = listeners.size();
299       for (int i = 0; i < count; i++) {
300         ((TreeModelListener) listeners.elementAt(i)).treeNodesRemoved(e);
301       }
302     }
303   }
304   protected void fireTreeStructureChanged(TreeModelEvent e) {
305     if (treeModelListeners != null) {
306       Vector listeners = treeModelListeners;
307       int count = listeners.size();
308       for (int i = 0; i < count; i++) {
309         ((TreeModelListener) listeners.elementAt(i)).treeStructureChanged(e);
310       }
311     }
312   }
313 }
314
Popular Tags