KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEConfigurationProvider


1 /*
2  * Sun Public License Notice
3  *
4  * The contents of this file are subject to the Sun Public License
5  * Version 1.0 (the "License"). You may not use this file except in
6  * compliance with the License. A copy of the License is available at
7  * http://www.sun.com/
8  *
9  * The Original Code is NetBeans. The Initial Developer of the Original
10  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
11  * Microsystems, Inc. All Rights Reserved.
12  */

13
14 package org.netbeans.modules.java.j2seproject;
15
16 import java.beans.PropertyChangeEvent JavaDoc;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.beans.PropertyChangeSupport JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.text.Collator JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import org.netbeans.api.project.ProjectManager;
34 import org.netbeans.modules.java.j2seproject.ui.customizer.CustomizerProviderImpl;
35 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SECompositePanelProvider;
36 import org.netbeans.spi.project.ActionProvider;
37 import org.netbeans.spi.project.ProjectConfiguration;
38 import org.netbeans.spi.project.ProjectConfigurationProvider;
39 import org.netbeans.spi.project.support.ant.EditableProperties;
40 import org.openide.filesystems.FileChangeAdapter;
41 import org.openide.filesystems.FileChangeListener;
42 import org.openide.filesystems.FileEvent;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileRenameEvent;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.util.NbBundle;
47 import org.openide.util.Utilities;
48
49 /**
50  * Manages configurations for a Java SE project.
51  * @author Jesse Glick
52  */

53 final class J2SEConfigurationProvider implements ProjectConfigurationProvider<J2SEConfigurationProvider.Config> {
54
55     private static final Logger JavaDoc LOGGER = Logger.getLogger(J2SEConfigurationProvider.class.getName());
56
57     /**
58      * Ant property name for active config.
59      */

60     public static final String JavaDoc PROP_CONFIG = "config"; // NOI18N
61
/**
62      * Ant property file which specified active config.
63      */

64     public static final String JavaDoc CONFIG_PROPS_PATH = "nbproject/private/config.properties"; // NOI18N
65

66     public static final class Config implements ProjectConfiguration {
67         /** file basename, or null for default config */
68         public final String JavaDoc name;
69         private final String JavaDoc displayName;
70         public Config(String JavaDoc name, String JavaDoc displayName) {
71             this.name = name;
72             this.displayName = displayName;
73         }
74         public String JavaDoc getDisplayName() {
75             return displayName;
76         }
77         public int hashCode() {
78             return name != null ? name.hashCode() : 0;
79         }
80         public boolean equals(Object JavaDoc o) {
81             return (o instanceof Config) && Utilities.compareObjects(name, ((Config) o).name);
82         }
83         public String JavaDoc toString() {
84             return "J2SEConfigurationProvider.Config[" + name + "," + displayName + "]"; // NOI18N
85
}
86     }
87
88     private static final Config DEFAULT = new Config(null,
89             NbBundle.getMessage(J2SEConfigurationProvider.class, "J2SEConfigurationProvider.default.label"));
90
91     private final J2SEProject p;
92     private final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
93     private final FileChangeListener fcl = new FileChangeAdapter() {
94         public void fileFolderCreated(FileEvent fe) {
95             update(fe);
96         }
97         public void fileDataCreated(FileEvent fe) {
98             update(fe);
99         }
100         public void fileDeleted(FileEvent fe) {
101             update(fe);
102         }
103         public void fileRenamed(FileRenameEvent fe) {
104             update(fe);
105         }
106         private void update(FileEvent ev) {
107             LOGGER.log(Level.FINEST, "Received {0}", ev);
108             Set JavaDoc<String JavaDoc> oldConfigs = configs != null ? configs.keySet() : Collections.<String JavaDoc>emptySet();
109             configDir = p.getProjectDirectory().getFileObject("nbproject/configs"); // NOI18N
110
if (configDir != null) {
111                 configDir.removeFileChangeListener(fclWeak);
112                 configDir.addFileChangeListener(fclWeak);
113                 LOGGER.log(Level.FINEST, "(Re-)added listener to {0}", configDir);
114             } else {
115                 LOGGER.log(Level.FINEST, "No nbproject/configs exists");
116             }
117             calculateConfigs();
118             Set JavaDoc<String JavaDoc> newConfigs = configs.keySet();
119             if (!oldConfigs.equals(newConfigs)) {
120                 LOGGER.log(Level.FINER, "Firing " + ProjectConfigurationProvider.PROP_CONFIGURATIONS + ": {0} -> {1}", new Object JavaDoc[] {oldConfigs, newConfigs});
121                 pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATIONS, null, null);
122                 // XXX also fire PROP_ACTIVE_CONFIGURATION?
123
}
124         }
125     };
126     private final FileChangeListener fclWeak;
127     private FileObject configDir;
128     private Map JavaDoc<String JavaDoc,Config> configs;
129     private FileObject nbp;
130     
131     public J2SEConfigurationProvider(J2SEProject p) {
132         this.p = p;
133         fclWeak = FileUtil.weakFileChangeListener(fcl, null);
134         nbp = p.getProjectDirectory().getFileObject("nbproject"); // NOI18N
135
if (nbp != null) {
136             nbp.addFileChangeListener(fclWeak);
137             LOGGER.log(Level.FINEST, "Added listener to {0}", nbp);
138             configDir = nbp.getFileObject("configs"); // NOI18N
139
if (configDir != null) {
140                 configDir.addFileChangeListener(fclWeak);
141                 LOGGER.log(Level.FINEST, "Added listener to {0}", configDir);
142             }
143         }
144         p.evaluator().addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
145             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
146                 if (PROP_CONFIG.equals(evt.getPropertyName())) {
147                     LOGGER.log(Level.FINER, "Refiring " + PROP_CONFIG + " -> " + ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE);
148                     pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE, null, null);
149                 }
150             }
151         });
152     }
153
154     private void calculateConfigs() {
155         configs = new HashMap JavaDoc<String JavaDoc,Config>();
156         if (configDir != null) {
157             for (FileObject kid : configDir.getChildren()) {
158                 if (!kid.hasExt("properties")) {
159                     continue;
160                 }
161                 try {
162                     InputStream JavaDoc is = kid.getInputStream();
163                     try {
164                         Properties JavaDoc p = new Properties JavaDoc();
165                         p.load(is);
166                         String JavaDoc name = kid.getName();
167                         String JavaDoc label = p.getProperty("$label"); // NOI18N
168
configs.put(name, new Config(name, label != null ? label : name));
169                     } finally {
170                         is.close();
171                     }
172                 } catch (IOException JavaDoc x) {
173                     LOGGER.log(Level.INFO, null, x);
174                 }
175             }
176         }
177         LOGGER.log(Level.FINEST, "Calculated configurations: {0}", configs);
178     }
179
180     public Collection JavaDoc<Config> getConfigurations() {
181         calculateConfigs();
182         List JavaDoc<Config> l = new ArrayList JavaDoc<Config>();
183         l.addAll(configs.values());
184         Collections.sort(l, new Comparator JavaDoc<Config>() {
185             Collator JavaDoc c = Collator.getInstance();
186             public int compare(Config c1, Config c2) {
187                 return c.compare(c1.getDisplayName(), c2.getDisplayName());
188             }
189         });
190         l.add(0, DEFAULT);
191         return l;
192     }
193
194     public Config getActiveConfiguration() {
195         calculateConfigs();
196         String JavaDoc config = p.evaluator().getProperty(PROP_CONFIG);
197         if (config != null && configs.containsKey(config)) {
198             return configs.get(config);
199         } else {
200             return DEFAULT;
201         }
202     }
203
204     public void setActiveConfiguration(Config c) throws IllegalArgumentException JavaDoc, IOException JavaDoc {
205         if (c != DEFAULT && !configs.values().contains(c)) {
206             throw new IllegalArgumentException JavaDoc();
207         }
208         final String JavaDoc n = c.name;
209         EditableProperties ep = p.getUpdateHelper().getProperties(CONFIG_PROPS_PATH);
210         if (Utilities.compareObjects(n, ep.getProperty(PROP_CONFIG))) {
211             return;
212         }
213         if (n != null) {
214             ep.setProperty(PROP_CONFIG, n);
215         } else {
216             ep.remove(PROP_CONFIG);
217         }
218         p.getUpdateHelper().putProperties(CONFIG_PROPS_PATH, ep);
219         pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE, null, null);
220         ProjectManager.getDefault().saveProject(p);
221         assert p.getProjectDirectory().getFileObject(CONFIG_PROPS_PATH) != null;
222     }
223
224     public boolean hasCustomizer() {
225         return true;
226     }
227
228     public void customize() {
229         p.getLookup().lookup(CustomizerProviderImpl.class).showCustomizer(J2SECompositePanelProvider.RUN);
230     }
231
232     public boolean configurationsAffectAction(String JavaDoc command) {
233         return command.equals(ActionProvider.COMMAND_RUN) ||
234                command.equals(ActionProvider.COMMAND_DEBUG);
235     }
236
237     public void addPropertyChangeListener(PropertyChangeListener JavaDoc lst) {
238         pcs.addPropertyChangeListener(lst);
239     }
240
241     public void removePropertyChangeListener(PropertyChangeListener JavaDoc lst) {
242         pcs.removePropertyChangeListener(lst);
243     }
244
245 }
246
Popular Tags