KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > RubyConfigurationProvider


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.ruby.rubyproject;
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.ruby.rubyproject.ui.customizer.CustomizerProviderImpl;
35 import org.netbeans.modules.ruby.rubyproject.ui.customizer.RubyCompositePanelProvider;
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.modules.ruby.spi.project.support.rake.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 Ruby project.
51  * @author Jesse Glick
52  */

53 public final class RubyConfigurationProvider implements ProjectConfigurationProvider<RubyConfigurationProvider.Config> {
54
55     private static final Logger JavaDoc LOGGER = Logger.getLogger(RubyConfigurationProvider.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 "RubyConfigurationProvider.Config[" + name + "," + displayName + "]"; // NOI18N
85
}
86     }
87
88     private static final Config DEFAULT = new Config(null,
89             NbBundle.getMessage(RubyConfigurationProvider.class, "RubyConfigurationProvider.default.label"));
90
91     private final RubyProject 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
130     public RubyConfigurationProvider(RubyProject p) {
131         this.p = p;
132         fclWeak = FileUtil.weakFileChangeListener(fcl, null);
133         FileObject nbp = p.getProjectDirectory().getFileObject("nbproject"); // NOI18N
134
if (nbp != null) {
135             nbp.addFileChangeListener(fclWeak);
136             LOGGER.log(Level.FINEST, "Added listener to {0}", nbp);
137             configDir = nbp.getFileObject("configs"); // NOI18N
138
if (configDir != null) {
139                 configDir.addFileChangeListener(fclWeak);
140                 LOGGER.log(Level.FINEST, "Added listener to {0}", configDir);
141             }
142         }
143         p.evaluator().addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
144             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
145                 if (PROP_CONFIG.equals(evt.getPropertyName())) {
146                     LOGGER.log(Level.FINER, "Refiring " + PROP_CONFIG + " -> " + ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE);
147                     pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE, null, null);
148                 }
149             }
150         });
151     }
152
153     private void calculateConfigs() {
154         configs = new HashMap JavaDoc<String JavaDoc,Config>();
155         if (configDir != null) {
156             for (FileObject kid : configDir.getChildren()) {
157                 if (!kid.hasExt("properties")) {
158                     continue;
159                 }
160                 try {
161                     InputStream JavaDoc is = kid.getInputStream();
162                     try {
163                         Properties JavaDoc p = new Properties JavaDoc();
164                         p.load(is);
165                         String JavaDoc name = kid.getName();
166                         String JavaDoc label = p.getProperty("$label"); // NOI18N
167
configs.put(name, new Config(name, label != null ? label : name));
168                     } finally {
169                         is.close();
170                     }
171                 } catch (IOException JavaDoc x) {
172                     LOGGER.log(Level.INFO, null, x);
173                 }
174             }
175         }
176         LOGGER.log(Level.FINEST, "Calculated configurations: {0}", configs);
177     }
178
179     public Collection JavaDoc<Config> getConfigurations() {
180         calculateConfigs();
181         List JavaDoc<Config> l = new ArrayList JavaDoc<Config>();
182         l.addAll(configs.values());
183         Collections.sort(l, new Comparator JavaDoc<Config>() {
184             Collator JavaDoc c = Collator.getInstance();
185             public int compare(Config c1, Config c2) {
186                 return c.compare(c1.getDisplayName(), c2.getDisplayName());
187             }
188         });
189         l.add(0, DEFAULT);
190         return l;
191     }
192
193     public Config getActiveConfiguration() {
194         calculateConfigs();
195         String JavaDoc config = p.evaluator().getProperty(PROP_CONFIG);
196         if (config != null && configs.containsKey(config)) {
197             return configs.get(config);
198         } else {
199             return DEFAULT;
200         }
201     }
202
203     public void setActiveConfiguration(Config c) throws IllegalArgumentException JavaDoc, IOException JavaDoc {
204         if (c != DEFAULT && !configs.values().contains(c)) {
205             throw new IllegalArgumentException JavaDoc();
206         }
207         final String JavaDoc n = c.name;
208         EditableProperties ep = p.getUpdateHelper().getProperties(CONFIG_PROPS_PATH);
209         if (Utilities.compareObjects(n, ep.getProperty(PROP_CONFIG))) {
210             return;
211         }
212         if (n != null) {
213             ep.setProperty(PROP_CONFIG, n);
214         } else {
215             ep.remove(PROP_CONFIG);
216         }
217         p.getUpdateHelper().putProperties(CONFIG_PROPS_PATH, ep);
218         pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE, null, null);
219         ProjectManager.getDefault().saveProject(p);
220         assert p.getProjectDirectory().getFileObject(CONFIG_PROPS_PATH) != null;
221     }
222
223     public boolean hasCustomizer() {
224         return true;
225     }
226
227     public void customize() {
228         p.getLookup().lookup(CustomizerProviderImpl.class).showCustomizer(RubyCompositePanelProvider.RUN);
229     }
230
231     public boolean configurationsAffectAction(String JavaDoc command) {
232         return command.equals(ActionProvider.COMMAND_RUN) ||
233                command.equals(ActionProvider.COMMAND_DEBUG);
234     }
235
236     public void addPropertyChangeListener(PropertyChangeListener JavaDoc lst) {
237         pcs.addPropertyChangeListener(lst);
238     }
239
240     public void removePropertyChangeListener(PropertyChangeListener JavaDoc lst) {
241         pcs.removePropertyChangeListener(lst);
242     }
243
244 }
245
Popular Tags