KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > palette > ChooseProjectWizardPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.palette;
21
22 import java.io.*;
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.BorderFactory JavaDoc;
25 import javax.swing.event.*;
26 import java.beans.*;
27 import java.util.*;
28 import java.net.URI JavaDoc;
29
30 import org.openide.WizardDescriptor;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.*;
33 import org.netbeans.api.project.ant.*;
34 import org.netbeans.api.project.*;
35 import org.netbeans.spi.project.ui.support.ProjectChooser;
36
37 /**
38  * The first panel in the wizard for adding new components to the palette from
39  * a project. In this panel the user chooses a project (as project folder
40  * using the project chooser UI).
41  *
42  * @author Tomas Pavek
43  */

44
45 class ChooseProjectWizardPanel implements WizardDescriptor.Panel {
46
47     private JFileChooser JavaDoc projectChooser;
48     private static String JavaDoc lastDirectoryUsed;
49
50     private EventListenerList listenerList;
51
52     // ----------
53
// WizardDescriptor.Panel implementation
54

55     public java.awt.Component JavaDoc getComponent() {
56         if (projectChooser == null) { // create the UI component for the wizard step
57
projectChooser = ProjectChooser.projectChooser();
58             projectChooser.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
59
60             // wizard API: set the caption and index of this panel
61
projectChooser.setName(PaletteUtils.getBundleString("CTL_SelectProject_Caption")); // NOI18N
62
projectChooser.putClientProperty("WizardPanel_contentSelectedIndex", // NOI18N
63
new Integer JavaDoc(0));
64
65             if (lastDirectoryUsed != null)
66                 projectChooser.setCurrentDirectory(new File(lastDirectoryUsed));
67             projectChooser.setControlButtonsAreShown(false);
68
69             projectChooser.addPropertyChangeListener(new PropertyChangeListener() {
70                 public void propertyChange(PropertyChangeEvent ev) {
71                     String JavaDoc propName = ev.getPropertyName();
72                     if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(propName)
73                          || JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(propName))
74                         fireStateChanged();
75                 }
76             });
77         }
78
79         return projectChooser;
80     }
81
82     public org.openide.util.HelpCtx getHelp() {
83         // PENDING
84
return new org.openide.util.HelpCtx("beans.adding"); // NOI18N
85
}
86
87     public boolean isValid() {
88         if (projectChooser != null) {
89             File file = projectChooser.getSelectedFile();
90             if (file != null) {
91                 file = FileUtil.normalizeFile(file);
92                 FileObject projectDir = FileUtil.toFileObject(file);
93                 try {
94                     Project project = ProjectManager.getDefault()
95                                                .findProject(projectDir);
96                     if (project != null) { // it is a project directory
97
lastDirectoryUsed = projectChooser.getCurrentDirectory()
98                                                            .getAbsolutePath();
99                         return true;
100                     }
101                 }
102                 catch (IOException ex) {} // ignore
103
}
104         }
105         return false;
106     }
107
108     public void readSettings(Object JavaDoc settings) {
109     }
110
111     public void storeSettings(Object JavaDoc settings) {
112         if (projectChooser == null)
113             return;
114
115         File file = projectChooser.getSelectedFile();
116         if (file == null)
117             return;
118         file = FileUtil.normalizeFile(file);
119
120         Project project = null;
121         try {
122             project = ProjectManager.getDefault().findProject(
123                                     FileUtil.toFileObject(file));
124         }
125         catch (IOException ex) {
126             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
127         }
128         if (project == null)
129             return;
130
131         List fileList = new ArrayList();
132         AntArtifact[] artifacts =
133             AntArtifactQuery.findArtifactsByType(project, "jar"); // NOI18N
134

135         for (int i=0; i < artifacts.length; i++) {
136             URI JavaDoc scriptLocation = artifacts[i].getScriptLocation().toURI();
137             URI JavaDoc[] artifactLocations = artifacts[i].getArtifactLocations();
138             for (int j=0; j < artifactLocations.length; j++) {
139                 File outputFile = new File(scriptLocation.resolve(artifactLocations[j]).normalize());
140                 fileList.add(outputFile);
141             }
142         }
143
144         File[] outputFiles = new File[fileList.size()];
145         fileList.toArray(outputFiles);
146         ((AddToPaletteWizard)settings).setJARFiles(outputFiles);
147     }
148
149     public void addChangeListener(ChangeListener listener) {
150         if (listenerList == null)
151             listenerList = new EventListenerList();
152         listenerList.add(ChangeListener.class, listener);
153     }
154
155     public void removeChangeListener(ChangeListener listener) {
156         if (listenerList != null)
157             listenerList.remove(ChangeListener.class, listener);
158     }
159
160     // -----
161

162     void fireStateChanged() {
163         if (listenerList == null)
164             return;
165
166         ChangeEvent e = null;
167         Object JavaDoc[] listeners = listenerList.getListenerList();
168         for (int i=listeners.length-2; i >= 0; i-=2) {
169             if (listeners[i] == ChangeListener.class) {
170                 if (e == null)
171                     e = new ChangeEvent(this);
172                 ((ChangeListener)listeners[i+1]).stateChanged(e);
173             }
174         }
175     }
176 }
177
Popular Tags