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.project;
020:
021: import java.awt.Toolkit;
022: import java.awt.image.BufferedImage;
023: import java.io.File;
024: import java.util.Collection;
025: import java.util.Hashtable;
026: import java.util.Iterator;
027: import java.util.LinkedList;
028:
029: import javax.swing.ImageIcon;
030:
031: import com.jeta.forms.gui.common.FormUtils;
032: import com.jeta.forms.logger.FormsLogger;
033: import com.jeta.forms.project.ProjectManager;
034: import com.jeta.forms.project.RuntimeProjectManager;
035: import com.jeta.swingbuilder.gui.project.UserPreferencesNames;
036: import com.jeta.swingbuilder.interfaces.userprops.TSUserPropertiesUtils;
037: import com.jeta.swingbuilder.store.ProjectModel;
038:
039: /**
040: * Concrete implementation of ProjectManager
041: *
042: * @author Jeff Tassin
043: */
044: public class DefaultProjectManager implements ProjectManager {
045: /**
046: * A list of source directories for this project. We use source directories
047: * to locate forms when given a relative path.
048: */
049: private LinkedList m_source_dirs = new LinkedList();
050:
051: /** an empty icon if a resource cannot be loaded */
052: private static ImageIcon m_empty_icon;
053:
054: /** cache of images */
055: private Hashtable m_image_cache = new Hashtable();
056:
057: /**
058: * This is used to locate resources for forms used by the Abeille Form
059: * Builder itself.
060: */
061: private RuntimeProjectManager m_runtime = new RuntimeProjectManager();
062:
063: /**
064: * The current project
065: */
066: private ProjectModel m_project;
067:
068: /**
069: * ctor
070: */
071: public DefaultProjectManager() {
072:
073: }
074:
075: /**
076: * ctor
077: */
078: public DefaultProjectManager(ProjectModel pmodel) {
079: setProject(pmodel);
080: }
081:
082: /**
083: * Clears any cached resources
084: */
085: public void clearResourceCache() {
086: m_image_cache.clear();
087: }
088:
089: /**
090: * @return a valid absolute path given a relative path. Searches the
091: * registered source paths and determines if the relativePath
092: * (includes filename) exists within one of the source paths.
093: */
094: public String getAbsolutePath(String relativePath) {
095: // FormsLogger.debug( "DefaultProjectManager.getAbsoultePath: " +
096: // relativePath );
097: if (relativePath == null)
098: relativePath = "";
099:
100: relativePath = FormUtils.fixPath(relativePath);
101:
102: if (isJETAResource(relativePath))
103: return m_runtime.getAbsolutePath(relativePath);
104:
105: Iterator iter = m_source_dirs.iterator();
106: while (iter.hasNext()) {
107: String path = (String) iter.next();
108: File f = new File(path + File.separatorChar + relativePath);
109: if (f.exists()) {
110: if (f.isFile()) {
111: return f.getPath();
112: } else {
113: // should never happen
114: assert (false);
115: }
116: }
117: }
118: return null;
119: }
120:
121: /**
122: * @return the current project
123: */
124: public ProjectModel getProject() {
125: return m_project;
126: }
127:
128: /**
129: * @return a valid relative package/filename given an absolute path. The
130: * relative path is determined using the source directories. If the
131: * path does not lie within a source directory, null is returned.
132: */
133: public String getRelativePath(String absPath) {
134: if (isJETAResource(absPath))
135: return m_runtime.getRelativePath(absPath);
136:
137: if (absPath == null)
138: return null;
139:
140: Iterator iter = m_source_dirs.iterator();
141: while (iter.hasNext()) {
142: String src_path = (String) iter.next();
143: if (absPath.indexOf(src_path) == 0) {
144: return absPath.substring(src_path.length() + 1, absPath
145: .length());
146: }
147: }
148: return null;
149: }
150:
151: /**
152: * @return true if the given resource is a JETA resource for the Abeille
153: * Forms Builder. JETA resources begin with "com.jeta" or
154: * "jeta.resources".
155: */
156: private boolean isJETAResource(String resource) {
157: if (FormUtils.isDesignMode())
158: return false;
159: else if (resource == null)
160: return false;
161: else if (resource.indexOf("com/jeta") >= 0)
162: return true;
163: else if (resource.indexOf("jeta.resources") >= 0)
164: return true;
165: else
166: return false;
167: }
168:
169: /**
170: * @return true if the given absolute path lies within one of the source
171: * directories.
172: */
173: public boolean isValidAbsolutePath(String path) {
174: if (isJETAResource(path))
175: return m_runtime.isValidAbsolutePath(path);
176:
177: Iterator iter = m_source_dirs.iterator();
178: while (iter.hasNext()) {
179: String src_path = (String) iter.next();
180: if (path.indexOf(src_path) == 0) {
181: return true;
182: }
183: }
184: return false;
185: }
186:
187: /**
188: * @return true if the given relative resource exists and is a file
189: */
190: public boolean isValidResource(String relpath) {
191: // System.out.println( "DefaultProjectmanager.isValidResource: " +
192: // relpath
193: // );
194: if (relpath == null)
195: return false;
196:
197: if (relpath.length() > 0) {
198: if (relpath.charAt(0) == '\\' || relpath.charAt(0) == '/') {
199: relpath = relpath.substring(1, relpath.length());
200: }
201: }
202:
203: Iterator iter = m_source_dirs.iterator();
204: while (iter.hasNext()) {
205: String src_path = (String) iter.next();
206: String full_path = src_path + File.separatorChar + relpath;
207:
208: // System.out.println( "DefaultProjectmanager.isValidResource:
209: // full_path: " + full_path );
210:
211: File f = new File(full_path);
212: if (f.isFile())
213: return true;
214: }
215: return false;
216: }
217:
218: /**
219: * Utility method that loads an image from the CLASSPATH.
220: *
221: * @param imageName
222: * the subdirectory and name of image file (i.e.
223: * images/edit16.gif )
224: */
225: public ImageIcon loadImage(String imageName) {
226: ImageIcon result = null;
227: boolean cache_images = TSUserPropertiesUtils.getBoolean(
228: UserPreferencesNames.ID_CACHE_IMAGES, true);
229: if (cache_images) {
230: synchronized (this ) {
231: result = (ImageIcon) m_image_cache.get(imageName);
232: if (result != null)
233: return result;
234: }
235: }
236:
237: if (isJETAResource(imageName)) {
238: result = m_runtime.loadImage(imageName);
239: } else {
240: try {
241: String path = getAbsolutePath(imageName);
242: if (path != null) {
243: /**
244: * The toolkit should be used to create the image, otherwise
245: * it may be pulled from the toolkit image cache.
246: */
247: Toolkit toolkit = Toolkit.getDefaultToolkit();
248: ImageIcon image = new ImageIcon(toolkit
249: .createImage(path));
250: result = image;
251: }
252: } catch (Exception e) {
253: e.printStackTrace();
254: }
255:
256: if (result == null && m_empty_icon == null) {
257: int width = 16;
258: int height = 16;
259: BufferedImage img = new BufferedImage(width, height,
260: BufferedImage.TYPE_INT_RGB);
261: java.awt.Graphics2D bg = img.createGraphics();
262: bg.setColor(javax.swing.UIManager.getColor("control"));
263: bg.fillRect(0, 0, width, height);
264: bg.setColor(java.awt.Color.red);
265: bg.drawRect(0, 0, width - 1, height - 1);
266: bg.drawLine(0, 0, width - 1, height - 1);
267: bg.drawLine(0, height - 1, width - 1, 0);
268: bg.dispose();
269: m_empty_icon = new ImageIcon(img);
270: }
271: }
272:
273: if (result == null)
274: result = m_empty_icon;
275: else {
276: if (cache_images) {
277: synchronized (this ) {
278: m_image_cache.put(imageName, result);
279: }
280: }
281: }
282: return result;
283: }
284:
285: /**
286: * Sets the current project
287: */
288: public void setProject(ProjectModel pmodel) {
289: m_project = pmodel;
290: m_source_dirs.clear();
291: if (pmodel != null) {
292: File rootDir = pmodel.getProjectRootDir();
293:
294: Collection src_paths = pmodel.getSourcePaths();
295: Iterator iter = src_paths.iterator();
296: while (iter.hasNext()) {
297: String src_path = (String) iter.next();
298: if (src_path.equals("."))
299: src_path = rootDir.getPath();
300:
301: if (File.separatorChar == '/')
302: src_path = src_path.replace('\\',
303: File.separatorChar);
304: else if (File.separatorChar == '\\')
305: src_path = src_path
306: .replace('/', File.separatorChar);
307:
308: File f = new File(rootDir, src_path);
309: try {
310: if (f.isDirectory()) {
311: FormsLogger
312: .debug("DefaultProjectManager loading (rel)project path: "
313: + f.getCanonicalPath());
314: m_source_dirs.add(f.getCanonicalPath());
315: } else {
316: f = new File(src_path);
317: if (f.isDirectory()) {
318: FormsLogger
319: .debug("DefaultProjectManager loading (abs)project path: "
320: + src_path);
321: m_source_dirs.add(src_path);
322: } else {
323: System.err
324: .println("unable to load project directory: "
325: + src_path);
326: }
327: }
328: } catch (Exception e) {
329: FormsLogger.severe(e);
330: }
331: }
332: }
333: }
334:
335: }
|