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-2006 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: package org.netbeans.api.project.ant;
043:
044: import java.awt.Dialog;
045: import java.io.File;
046: import java.io.IOException;
047: import java.net.URI;
048: import java.net.URL;
049: import javax.swing.JFileChooser;
050: import org.netbeans.api.project.FileOwnerQuery;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.libraries.LibraryManager;
053: import org.netbeans.modules.project.ant.FileChooserAccessory;
054: import org.netbeans.modules.project.ant.RelativizeFilePathCustomizer;
055: import org.netbeans.modules.project.ant.ProjectLibraryProvider;
056: import org.netbeans.spi.project.AuxiliaryConfiguration;
057: import org.netbeans.spi.project.support.ant.AntProjectHelper;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.util.NbBundle;
063:
064: /**
065: * Custom file chooser allowing user to choose how a file will be referenced
066: * from a project - via absolute path, or relative path. Make sure you call
067: * {@link #getSelectedPaths} instead of {@link #getSelectedFiles} as it returns relative
068: * files and performs file copying if necessary.
069: *
070: * @author David Konecny
071: * @since org.netbeans.modules.project.ant/1 1.19
072: */
073: public final class FileChooser extends JFileChooser {
074:
075: private FileChooserAccessory accessory;
076:
077: /**
078: * Create chooser for given AntProjectHelper. Standard file chooser is shown
079: * if project is not sharable.
080: *
081: * @param helper ant project helper; cannot be null
082: * @param copyAllowed is file copying allowed
083: */
084: public FileChooser(AntProjectHelper helper, boolean copyAllowed) {
085: super ();
086: LibraryManager lm = ProjectLibraryProvider
087: .getProjectLibraryManager(helper);
088: if (lm != null) {
089: URL u = lm.getLocation();
090: if (u != null) {
091: File libBase = new File(URI.create(u.toExternalForm()))
092: .getParentFile();
093: accessory = new FileChooserAccessory(this , FileUtil
094: .toFile(helper.getProjectDirectory()), libBase,
095: copyAllowed);
096: setAccessory(accessory);
097: }
098: }
099: }
100:
101: /**
102: * Create chooser for given base folder and shared libraries folder.
103: *
104: * @param baseFolder base folder to which all selected files will be relativized;
105: * can be null in which case regular file chooser is shown
106: * @param sharedLibrariesFolder providing shared libraries folder enables option
107: * of copying selected files there; can be null in which case copying option
108: * is disabled
109: */
110: public FileChooser(File baseFolder, File sharedLibrariesFolder) {
111: super ();
112: if (baseFolder != null) {
113: accessory = new FileChooserAccessory(this , baseFolder,
114: sharedLibrariesFolder,
115: sharedLibrariesFolder != null);
116: setAccessory(accessory);
117: }
118: }
119:
120: /**
121: * Returns array of paths selected. The difference from
122: * {@link #getSelectedFiles} is that depends on user's choice the files
123: * may be relative and they may have been copied to different location.
124: *
125: * @return array of files which may be relative to base folder this chooser
126: * was created for; e.g. project folder in case of AntProjectHelper
127: * constructor; never null; can be empty array
128: * @throws java.io.IOException any IO problem; for example during
129: * file copying
130: */
131: public String[] getSelectedPaths() throws IOException {
132: if (accessory != null) {
133: accessory.copyFilesIfNecessary();
134: if (accessory.isRelative()) {
135: return accessory.getFiles();
136: }
137: }
138: if (isMultiSelectionEnabled()) {
139: File[] sels = getSelectedFiles();
140: String[] toRet = new String[sels.length];
141: int index = 0;
142: for (File fil : sels) {
143: toRet[index] = fil.getAbsolutePath();
144: index++;
145: }
146: return toRet;
147: } else {
148: if (getSelectedFile() != null) {
149: return new String[] { getSelectedFile()
150: .getAbsolutePath() };
151: } else {
152: return new String[0];
153: }
154: }
155: }
156:
157: @Override
158: public void approveSelection() {
159: if (accessory != null && !accessory.canApprove()) {
160: return;
161: }
162: super.approveSelection();
163: }
164:
165: }
|