001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * BackgroundImageUrlDialog.java
044: * Created on November 5, 2004, 5:08 PM
045: */
046: package org.netbeans.modules.css.visual.ui;
047:
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import org.netbeans.modules.css.Utilities; //import org.netbeans.modules.css.visual.model.CssMetaModel;
051: import java.awt.Component;
052: import java.io.File;
053: import java.io.IOException;
054: import javax.swing.JFileChooser;
055: import javax.swing.filechooser.FileFilter; //import org.netbeans.modules.web.api.webmodule.WebModule;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * Dialog to select a Image URL
062: * @author Winston Prakash
063: * @version 1.0
064: */
065: public class BackgroundImageUrlDialog { //extends URLPanel{
066:
067: private static final Logger LOGGER = Logger
068: .getLogger(BackgroundImageUrlDialog.class.getName());
069: private FileObject base;
070:
071: public BackgroundImageUrlDialog(FileObject base) {
072: this .base = base;
073: }
074:
075: private String imageUrl = null;
076: private FileFilter imgFilter = new ImageFilter();
077:
078: // XXX Get URL also not just local file
079: public boolean show(Component parent) {
080: boolean retValue = false;
081: JFileChooser fileChooser = Utilities.getJFileChooser();
082: try {
083: //try to find a web module for the edited css
084:
085: // WebModule webModule = WebModule.getWebModule(base);
086: // FileObject webModuleDocumentBase = webModule == null ? null : webModule.getDocumentBase();
087: FileObject webModuleDocumentBase = null;
088:
089: //identify a starting directory for the file chooser
090: FileObject currDir = base.getParent();
091:
092: if (currDir != null) {
093: fileChooser.setCurrentDirectory(FileUtil
094: .toFile(currDir));
095: }
096:
097: fileChooser.addChoosableFileFilter(new ImageFilter());
098: if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
099: File imageFile = fileChooser.getSelectedFile();
100: FileObject imageFO = FileUtil.toFileObject(imageFile);
101: if (imageFO == null) {
102: //should not happen with Master FS
103: //assert imageFile != null;
104: LOGGER.log(Level.WARNING, null,
105: new IllegalStateException(
106: "Cannot find FileObject for file "
107: + imageFile.toURL()));
108: return false;
109: }
110: FileObject imageFolder = imageFO.getParent();
111:
112: //count relative path to the base dir
113: //check if we do not overlap the web module root is thre is any
114: //if we are out of the web module, copy the file to the css file
115: //directory
116:
117: //assert webModuleDocumentBase is parent/ancestor of base
118:
119: String path = null;
120: FileObject dir = base;
121: int level = -1;
122: do {
123: level++;
124:
125: dir = dir.getParent();
126:
127: if (dir == null) {
128: break;
129: }
130:
131: path = FileUtil.getRelativePath(dir, imageFolder);
132:
133: if (dir.equals(webModuleDocumentBase)) {
134: //we reached the web module document base boundary
135: //use relative path to this point
136: if (path == null) {
137: //the file is out of webmodule, copy it inside
138: FileUtil.copyFile(imageFO, currDir, imageFO
139: .getName(), imageFO.getExt());
140: imageUrl = imageFO.getNameExt();
141: return true;
142: } //else just continue trying to find the relative path
143: }
144: } while (path == null);
145:
146: if (path == null) {
147: //shoutldn't happen - a common path should always exist !?!?!
148: return false;
149: }
150:
151: StringBuilder pathBuilder = new StringBuilder();
152: pathBuilder.append(makePrefix(level));
153: pathBuilder.append(path);
154: if (path.length() > 0) {
155: pathBuilder.append('/');
156: }
157: pathBuilder.append(imageFO.getNameExt());
158: imageUrl = encodeURL(pathBuilder.toString());
159:
160: retValue = true;
161:
162: }
163: } catch (IOException exc) {
164: LOGGER.log(Level.WARNING, null, exc);
165: }
166: return retValue;
167: }
168:
169: private String makePrefix(int level) {
170: StringBuilder sb = new StringBuilder();
171: for (int i = 0; i < level; i++) {
172: sb.append("../");
173: }
174: return sb.toString();
175: }
176:
177: private String encodeURL(String imageUrl) {
178: StringBuffer sb = new StringBuffer();
179: int len = imageUrl.length();
180: for (int i = 0; i < len; i++) {
181: char chr = imageUrl.charAt(i);
182: if (chr == ' ') {
183: sb.append("%20");
184: } else {
185: sb.append(chr);
186: }
187: }
188: return sb.toString();
189: }
190:
191: public String getImageUrl() {
192: return imageUrl;
193: }
194:
195: public static class ImageFilter extends FileFilter {
196: //Accept all directories and image files.
197: public boolean accept(File f) {
198: if (f.isDirectory()) {
199: return true;
200: }
201: String extension = null;
202: String s = f.getName();
203: int i = s.lastIndexOf('.');
204:
205: if (i > 0 && i < s.length() - 1) {
206: extension = s.substring(i + 1).toLowerCase();
207: }
208:
209: if (extension != null) {
210: if (extension.toLowerCase().equals("gif") || //NOI18N
211: extension.toLowerCase().equals("jpg") || //NOI18N
212: extension.toLowerCase().equals("png")) { //NOI18N
213: return true;
214: } else {
215: return false;
216: }
217: }
218: return false;
219: }
220:
221: //The description of this filter
222: public String getDescription() {
223: return NbBundle.getMessage(BackgroundImageUrlDialog.class,
224: "IMAGE_FILTER"); //NOI18N
225: }
226: }
227: }
|