001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.store;
020:
021: import java.io.Externalizable;
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.Arrays;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.LinkedList;
028:
029: import com.jeta.forms.gui.common.FormUtils;
030: import com.jeta.open.support.EmptyCollection;
031: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
032:
033: /**
034: * Defines the settings for a project.
035: *
036: * @author Jeff Tassin
037: */
038: public class ProjectModel implements Externalizable {
039: static final long serialVersionUID = 3273178622430479212L;
040:
041: public static final int VERSION = 3;
042:
043: /**
044: * Whether the project file is to be used in a "shared" context. (Across all
045: * team members on a development project)
046: */
047: private boolean m_shared;
048:
049: /**
050: * For shared project files this is the base directory of the project
051: * itself. All project level java beans will be relative to this base
052: * directory.
053: */
054: private String m_project_env_variable;
055:
056: /**
057: * Whether the project file is to be used in a "shared" context. (Across all
058: * team members on a development project)
059: */
060: private ProjectLevelImportedBeansModel m_imported_beans;
061:
062: /**
063: * The list of source paths for the application
064: */
065: private LinkedList m_source_paths;
066:
067: /**
068: * This is the path to the project file. This is transient because the user
069: * can rename/move the file.
070: */
071: private transient String m_project_path;
072:
073: /**
074: * The path where classes are stored (can be null).
075: */
076: private String m_class_path;
077:
078: /**
079: * ctor
080: */
081: public ProjectModel() {
082: }
083:
084: public boolean equals(Object obj) {
085: if (this == obj)
086: return true;
087:
088: if (null == obj)
089: return false;
090:
091: if (!(obj instanceof ProjectModel))
092: return false;
093:
094: ProjectModel mObj = (ProjectModel) obj;
095:
096: if ((m_shared == mObj.m_shared)
097: && ((null == m_project_env_variable && null == mObj.m_project_env_variable)
098: || (m_project_env_variable != null && m_project_env_variable
099: .equals(mObj.m_project_env_variable)) || (mObj.m_project_env_variable != null && mObj.m_project_env_variable
100: .equals(m_project_env_variable)))
101: && ((null == m_class_path && null == mObj.m_class_path)
102: || (m_class_path != null && m_class_path
103: .equals(mObj.m_class_path)) || (mObj.m_class_path != null && mObj.m_class_path
104: .equals(m_class_path)))
105: && ((null == m_imported_beans && null == mObj.m_imported_beans)
106: || (m_imported_beans != null && m_imported_beans
107: .equals(mObj.m_imported_beans)) || (mObj.m_imported_beans != null && mObj.m_imported_beans
108: .equals(m_imported_beans)))) {
109: //
110: // Thus far the two projects are equal...final step is to examine
111: // the sets of source paths...
112: //
113: if ((null == m_source_paths && null != mObj.m_source_paths)
114: || (null != m_source_paths && null == mObj.m_source_paths)
115: || (null != m_source_paths
116: && null != mObj.m_source_paths && mObj.m_source_paths
117: .size() != m_source_paths.size())) {
118: return false;
119: } else {
120: Object[] paths1 = m_source_paths.toArray();
121: Arrays.sort(paths1);
122:
123: Object[] paths2 = mObj.m_source_paths.toArray();
124: Arrays.sort(paths2);
125:
126: for (int i = 0; i < paths1.length; i++) {
127: if (!paths1[i].equals(paths2[i]))
128: return false;
129: }
130: }
131: } else {
132: return false;
133: }
134:
135: return true;
136: }
137:
138: /**
139: * Adds a source path to this model
140: */
141: public void addSourcePath(String path) {
142: if (m_source_paths == null) {
143: m_source_paths = new LinkedList();
144: }
145: if ((path != null) && (!"".equals(path))) {
146: m_source_paths.add(path);
147: }
148: }
149:
150: public boolean isShared() {
151: return m_shared;
152: }
153:
154: public String getClassPath() {
155: return m_class_path;
156: }
157:
158: public String getProjectEnvVariable() {
159: return m_project_env_variable;
160: }
161:
162: /**
163: * @return a collection of String objects that are the paths where source
164: * and image files can be found.
165: */
166: public Collection getSourcePaths() {
167: if (m_source_paths == null)
168: return EmptyCollection.getInstance();
169: else
170: return m_source_paths;
171: }
172:
173: /**
174: * @return the path to the project file
175: */
176: public String getProjectPath() {
177: return m_project_path;
178: }
179:
180: public File getProjectRootDir() {
181: String tempPath = null;
182:
183: if (m_shared) {
184: if (m_project_env_variable != null) {
185: String directory = System
186: .getenv(m_project_env_variable);
187: try {
188: File file = new File(directory);
189: tempPath = file.getAbsolutePath();
190: } catch (Exception e) {
191: }
192: }
193: } else {
194: tempPath = m_project_path;
195: File file = new File(tempPath);
196: if (!file.isDirectory()) {
197: tempPath = file.getParent();
198: }
199: }
200:
201: File rootDir = null;
202:
203: if ((tempPath != null) && (!"".equals(tempPath))) {
204: String path = FormDesignerUtils.fastTrim(tempPath);
205: char c = File.separatorChar;
206: if (c == '\\')
207: path = path.replace('/', File.separatorChar);
208: else
209: path = path.replace('\\', File.separatorChar);
210:
211: try {
212: rootDir = new File(path);
213: } catch (Exception e) {
214: rootDir = null;
215: }
216: }
217:
218: return rootDir;
219: }
220:
221: /**
222: * @return the project level imported beans model
223: */
224: public ProjectLevelImportedBeansModel getProjectLevelImportedBeansModel() {
225: if (this .m_imported_beans == null) {
226: this .m_imported_beans = new ProjectLevelImportedBeansModel(
227: this .m_project_env_variable);
228: }
229: return this .m_imported_beans;
230: }
231:
232: /**
233: * Set the path to the project file
234: */
235: public void setProjectPath(String path) {
236: this .m_project_path = path;
237: }
238:
239: public void setClassPath(String classPath) {
240: this .m_class_path = classPath;
241: }
242:
243: public void setProjectEnvVariable(String envVariable) {
244: getProjectLevelImportedBeansModel().setEnvVar(envVariable);
245: this .m_project_env_variable = envVariable;
246: }
247:
248: public void setShared(boolean m_shared) {
249: this .m_shared = m_shared;
250:
251: }
252:
253: public void setProjectLevelImportedBeans(
254: ProjectLevelImportedBeansModel imported_beans) {
255: if (imported_beans != null) {
256: this .m_imported_beans = imported_beans;
257: this .m_imported_beans
258: .setEnvVar(this .m_project_env_variable);
259: }
260: }
261:
262: /**
263: * Externalizable Implementation
264: */
265: public void readExternal(java.io.ObjectInput in)
266: throws ClassNotFoundException, IOException {
267: int version = in.readInt();
268: m_source_paths = (LinkedList) in.readObject();
269: if (version >= 2) {
270: m_class_path = (String) in.readObject();
271: }
272:
273: LinkedList fixed_src_paths = new LinkedList();
274: if (m_source_paths != null) {
275: Iterator iter = m_source_paths.iterator();
276: while (iter.hasNext()) {
277: String path = (String) iter.next();
278: path = FormUtils.fixPath(path);
279: fixed_src_paths.add(path);
280: }
281: }
282: m_source_paths = fixed_src_paths;
283: m_class_path = FormUtils.fixPath(m_class_path);
284:
285: if (version >= 3) {
286: m_shared = in.readBoolean();
287: m_project_env_variable = (String) in.readObject();
288: m_imported_beans = (ProjectLevelImportedBeansModel) in
289: .readObject();
290: if (m_imported_beans == null) {
291: m_imported_beans = new ProjectLevelImportedBeansModel(
292: m_project_env_variable);
293: }
294: }
295: }
296:
297: /**
298: * Externalizable Implementation
299: */
300: public void writeExternal(java.io.ObjectOutput out)
301: throws IOException {
302: out.writeInt(VERSION);
303: out.writeObject(m_source_paths);
304: out.writeObject(m_class_path);
305: out.writeBoolean(m_shared);
306: out.writeObject(m_project_env_variable);
307: out.writeObject(m_imported_beans);
308: }
309: }
|