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.modules.form.palette;
043:
044: import java.io.*;
045: import javax.swing.JFileChooser;
046: import javax.swing.BorderFactory;
047: import javax.swing.event.*;
048: import java.beans.*;
049: import java.util.*;
050:
051: import org.openide.WizardDescriptor;
052: import org.openide.ErrorManager;
053: import org.openide.filesystems.*;
054: import org.netbeans.api.project.ant.*;
055: import org.netbeans.api.project.*;
056: import org.netbeans.modules.form.project.ClassSource;
057: import org.netbeans.spi.project.ui.support.ProjectChooser;
058: import org.openide.util.ChangeSupport;
059:
060: /**
061: * The first panel in the wizard for adding new components to the palette from
062: * a project. In this panel the user chooses a project (as project folder
063: * using the project chooser UI).
064: *
065: * @author Tomas Pavek
066: */
067:
068: class ChooseProjectWizardPanel implements
069: WizardDescriptor.Panel<AddToPaletteWizard> {
070:
071: private JFileChooser projectChooser;
072: private static String lastDirectoryUsed;
073:
074: private final ChangeSupport cs = new ChangeSupport(this );
075:
076: // ----------
077: // WizardDescriptor.Panel implementation
078:
079: public java.awt.Component getComponent() {
080: if (projectChooser == null) { // create the UI component for the wizard step
081: projectChooser = ProjectChooser.projectChooser();
082: projectChooser.setBorder(BorderFactory.createEmptyBorder(0,
083: 0, 0, 0));
084:
085: // wizard API: set the caption and index of this panel
086: projectChooser.setName(PaletteUtils
087: .getBundleString("CTL_SelectProject_Caption")); // NOI18N
088: projectChooser.putClientProperty(
089: "WizardPanel_contentSelectedIndex", // NOI18N
090: new Integer(0));
091:
092: if (lastDirectoryUsed != null)
093: projectChooser.setCurrentDirectory(new File(
094: lastDirectoryUsed));
095: projectChooser.setControlButtonsAreShown(false);
096:
097: projectChooser
098: .addPropertyChangeListener(new PropertyChangeListener() {
099: public void propertyChange(
100: PropertyChangeEvent ev) {
101: String propName = ev.getPropertyName();
102: if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
103: .equals(propName)
104: || JFileChooser.DIRECTORY_CHANGED_PROPERTY
105: .equals(propName))
106: cs.fireChange();
107: }
108: });
109: }
110:
111: return projectChooser;
112: }
113:
114: public org.openide.util.HelpCtx getHelp() {
115: // PENDING
116: return new org.openide.util.HelpCtx("beans.adding"); // NOI18N
117: }
118:
119: public boolean isValid() {
120: if (projectChooser != null) {
121: File file = projectChooser.getSelectedFile();
122: if (file != null) {
123: FileObject projectDir = FileUtil.toFileObject(FileUtil
124: .normalizeFile(file));
125: if (projectDir != null) {
126: try {
127: Project project = ProjectManager.getDefault()
128: .findProject(projectDir);
129: if (project != null) { // it is a project directory
130: lastDirectoryUsed = projectChooser
131: .getCurrentDirectory()
132: .getAbsolutePath();
133: return true;
134: }
135: } catch (IOException ex) {
136: } // ignore
137: }
138: }
139: }
140: return false;
141: }
142:
143: public void readSettings(AddToPaletteWizard settings) {
144: }
145:
146: public void storeSettings(AddToPaletteWizard settings) {
147: if (projectChooser == null)
148: return;
149:
150: File file = projectChooser.getSelectedFile();
151: if (file == null) {
152: return;
153: }
154: FileObject projectDir = FileUtil.toFileObject(FileUtil
155: .normalizeFile(file));
156: if (projectDir == null) {
157: return;
158: }
159:
160: Project project = null;
161: try {
162: project = ProjectManager.getDefault().findProject(
163: projectDir);
164: } catch (IOException ex) {
165: ErrorManager.getDefault().notify(
166: ErrorManager.INFORMATIONAL, ex);
167: }
168: if (project == null)
169: return;
170:
171: List<ClassSource.ProjectEntry> entries = new ArrayList<ClassSource.ProjectEntry>();
172: for (AntArtifact aa : AntArtifactQuery.findArtifactsByType(
173: project, /* XXX JavaProjectConstants.ARTIFACT_TYPE_JAR */
174: "jar")) { // NOI18N
175: entries.add(new ClassSource.ProjectEntry(aa));
176: }
177: settings.setJARFiles(entries);
178: }
179:
180: public void addChangeListener(ChangeListener listener) {
181: cs.addChangeListener(listener);
182: }
183:
184: public void removeChangeListener(ChangeListener listener) {
185: cs.removeChangeListener(listener);
186: }
187:
188: }
|