KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > ui > SourceFoldersWizardPanel


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.java.freeform.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.api.java.project.JavaProjectConstants;
34 import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
35 import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
36 import org.netbeans.modules.java.freeform.JavaProjectGenerator;
37 import org.netbeans.modules.java.freeform.JavaProjectNature;
38 import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport;
39 import org.openide.WizardDescriptor;
40 import org.openide.util.HelpCtx;
41 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
42 import org.netbeans.spi.project.support.ant.PropertyProvider;
43 import org.netbeans.spi.project.support.ant.PropertyUtils;
44 import org.openide.util.NbBundle;
45
46 /**
47  * @author David Konecny
48  */

49 public class SourceFoldersWizardPanel implements WizardDescriptor.Panel, ChangeListener JavaDoc, WizardDescriptor.FinishablePanel {
50
51     private SourceFoldersPanel component;
52     private WizardDescriptor wizardDescriptor;
53
54     public SourceFoldersWizardPanel() {
55         getComponent().setName(NbBundle.getMessage (NewJ2SEFreeformProjectWizardIterator.class, "TXT_NewJ2SEFreeformProjectWizardIterator_SourcePackageFolders"));
56     }
57     
58     public Component JavaDoc getComponent() {
59         if (component == null) {
60             component = new SourceFoldersPanel();
61             component.setChangeListener(this);
62             ((JComponent JavaDoc)component).getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(SourceFoldersWizardPanel.class, "ACSD_SourceFoldersWizardPanel")); // NOI18N
63
}
64         return component;
65     }
66     
67     public HelpCtx getHelp() {
68         return new HelpCtx( SourceFoldersWizardPanel.class );
69     }
70     
71     public boolean isValid() {
72         getComponent();
73         // Panel is valid without any source folder specified, but
74
// Next button is enabled only when there is some soruce
75
// folder specified -> see NewJ2SEFreeformProjectWizardIterator
76
// which enables/disables Next button
77
wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); // NOI18N
78
return true;
79     }
80
81     public boolean isFinishPanel() {
82         return true;
83     }
84     
85     private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
86     public final void addChangeListener(ChangeListener JavaDoc l) {
87         synchronized (listeners) {
88             listeners.add(l);
89         }
90     }
91     public final void removeChangeListener(ChangeListener JavaDoc l) {
92         synchronized (listeners) {
93             listeners.remove(l);
94         }
95     }
96     protected final void fireChangeEvent() {
97         Collection JavaDoc<ChangeListener JavaDoc> ls;
98         synchronized (listeners) {
99             ls = new HashSet JavaDoc<ChangeListener JavaDoc>(listeners);
100         }
101         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
102         for (ChangeListener JavaDoc l : ls) {
103             l.stateChanged(ev);
104         }
105     }
106     
107     public void readSettings(Object JavaDoc settings) {
108         wizardDescriptor = (WizardDescriptor)settings;
109         File JavaDoc projectLocation = (File JavaDoc)wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_LOCATION);
110         File JavaDoc projectFolder = (File JavaDoc)wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER);
111         PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
112             PropertyUtils.fixedPropertyProvider(
113             Collections.singletonMap(ProjectConstants.PROP_PROJECT_LOCATION, projectLocation.getAbsolutePath()))});
114
115         ProjectModel pm = (ProjectModel)wizardDescriptor.getProperty(NewJ2SEFreeformProjectWizardIterator.PROP_PROJECT_MODEL);
116         if (pm == null ||
117                 !pm.getBaseFolder().equals(projectLocation) ||
118                 !pm.getNBProjectFolder().equals(projectFolder)) {
119             pm = ProjectModel.createEmptyModel(projectLocation, projectFolder, evaluator);
120             wizardDescriptor.putProperty(NewJ2SEFreeformProjectWizardIterator.PROP_PROJECT_MODEL, pm);
121         }
122         List JavaDoc l = (List JavaDoc)wizardDescriptor.getProperty(NewJavaFreeformProjectSupport.PROP_EXTRA_JAVA_SOURCE_FOLDERS);
123         if (l != null) {
124             Iterator JavaDoc it = l.iterator();
125             while (it.hasNext()) {
126                 String JavaDoc path = (String JavaDoc)it.next();
127                 assert it.hasNext();
128                 String JavaDoc label = (String JavaDoc)it.next();
129                 // try to find if the model already contains this source folder
130
boolean found = false;
131                 for (int i = 0; i < pm.getSourceFoldersCount(); i++) {
132                     JavaProjectGenerator.SourceFolder existingSf = pm.getSourceFolder(i);
133                     if (existingSf.location.equals(path)) {
134                         found = true;
135                         break;
136                     }
137                 }
138                 // don't add the folder if it is already in the model
139
if (!found) {
140                     JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
141                     sf.location = path;
142                     sf.label = label;
143                     sf.type = JavaProjectConstants.SOURCES_TYPE_JAVA;
144                     sf.style = JavaProjectNature.STYLE_PACKAGES;
145                     pm.addSourceFolder(sf, false);
146                 }
147             }
148         }
149         
150         wizardDescriptor.putProperty("NewProjectWizard_Title", component.getClientProperty("NewProjectWizard_Title")); // NOI18N
151
component.setModel(pm, null);
152     }
153     
154     public void storeSettings(Object JavaDoc settings) {
155         wizardDescriptor = (WizardDescriptor)settings;
156         wizardDescriptor.putProperty("NewProjectWizard_Title", null); // NOI18N
157
}
158     
159     public void stateChanged(ChangeEvent JavaDoc e) {
160         fireChangeEvent();
161     }
162     
163 }
164
Popular Tags