KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.Collections JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Set JavaDoc;
65 import java.util.TreeSet JavaDoc;
66
67 import org.jdom.Element;
68
69 /**
70  *
71  * @author Nataliya Kholodna
72  * @version 1.0
73  */

74
75 public class DataView extends DVObject {
76   private CayenneProject cayenneProject;
77   private List JavaDoc objEntityViews = new ArrayList JavaDoc();
78   private File JavaDoc file;
79
80   private List JavaDoc saveErrors = new ArrayList JavaDoc();
81
82   public DataView(CayenneProject cayenneProject, String JavaDoc name, File JavaDoc file){
83     setName(name);
84     this.cayenneProject = cayenneProject;
85     this.file = file;
86   }
87
88   public DataView(CayenneProject cayenneProject, File JavaDoc file){
89     setName(file.getName());
90     this.cayenneProject = cayenneProject;
91     this.file = file;
92   }
93
94   public DataView(CayenneProject cayenneProject){
95     this.cayenneProject = cayenneProject;
96     setName("DataView");
97     file = new File JavaDoc(cayenneProject.getProjectDirectory(), "DataView.view.xml");
98   }
99
100   public List JavaDoc getSaveErrors(){
101     return Collections.unmodifiableList(saveErrors);
102   }
103
104   public CayenneProject getCayenneProject(){
105     return cayenneProject;
106   }
107
108   public void setFile(File JavaDoc file){
109     this.file = file;
110   }
111
112   public File JavaDoc getFile(){
113     return file;
114   }
115
116   public List JavaDoc getObjEntityViews() {
117     return Collections.unmodifiableList(objEntityViews);
118   }
119
120   public ObjEntityView createObjEntityView(List JavaDoc allObjEntityViews){
121     String JavaDoc nameRoot = "ObjEntityView";
122     String JavaDoc name = nameRoot;
123     for (int i = 0; i < Integer.MAX_VALUE; i++) {
124       name = nameRoot + i;
125       boolean nameExists = false;
126       for (int j = 0; j < allObjEntityViews.size(); j++) {
127         ObjEntityView objEntityView = (ObjEntityView)allObjEntityViews.get(j);
128         nameExists = name.equalsIgnoreCase(objEntityView.getName());
129         if (nameExists)
130           break;
131       }
132       if (!nameExists)
133         break;
134     }
135     ObjEntityView objEntityView = new ObjEntityView(this);
136     objEntityView.setName(name);
137     return objEntityView;
138   }
139
140   public void addObjEntityView(ObjEntityView objEntityView){
141     objEntityViews.add(objEntityView);
142   }
143
144   public void removeObjEntityView(ObjEntityView objEntityView){
145     ObjEntity entity = objEntityView.getObjEntity();
146     if (entity != null){
147       entity.removeObjEntityView(objEntityView);
148     }
149     objEntityViews.remove(objEntityView);
150   }
151
152   public ObjEntityView getObjEntityView(String JavaDoc name){
153     Iterator JavaDoc itr = objEntityViews.iterator();
154     while (itr.hasNext()){
155       Object JavaDoc o = itr.next();
156       ObjEntityView objEnt = (ObjEntityView)o;
157       if (objEnt.getName().equals(name)){
158         return objEnt;
159       }
160     }
161     return null;
162   }
163
164   public ObjEntityView getObjEntityView(int index){
165     return (ObjEntityView)(objEntityViews.get(index));
166   }
167
168   public int getIndexOfObjEntityView(ObjEntityView objEntityView){
169     return objEntityViews.indexOf(objEntityView);
170   }
171
172
173   public int getObjEntityViewCount(){
174     return objEntityViews.size();
175   }
176
177   public void clear(){
178     if (objEntityViews.size() != 0){
179       for (Iterator JavaDoc j = this.getObjEntityViews().iterator(); j.hasNext();){
180         ObjEntityView view = (ObjEntityView)j.next();
181         ObjEntity entity = view.getObjEntity();
182         entity.removeObjEntityView(view);
183       }
184       objEntityViews.clear();
185     }
186   }
187
188   public String JavaDoc toString(){
189     return this.getName();
190   }
191   public Element getDataViewElement(){
192     Element dataViewElement = new Element("data-view");
193
194     if (saveErrors.size() != 0){
195       saveErrors.clear();
196     }
197
198     List JavaDoc objEntityViewElements = new ArrayList JavaDoc();
199     Set JavaDoc objEntityViewsSort = new TreeSet JavaDoc(new ObjEntityViewsComparator());
200     objEntityViewsSort.addAll(objEntityViews);
201
202     Iterator JavaDoc itr = objEntityViewsSort.iterator();
203     while (itr.hasNext()){
204       Object JavaDoc o = itr.next();
205       ObjEntityView view = (ObjEntityView)o;
206       Element objEntityViewElement =
207                      view.getObjEntityViewElement();
208       saveErrors.addAll(view.getSaveErrors());
209       objEntityViewElements.add(objEntityViewElement);
210     }
211     dataViewElement.setContent(objEntityViewElements);
212
213     return dataViewElement;
214   }
215 }
Popular Tags