KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > ui > customizer > RailsProjectProperties


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.ruby.railsprojects.ui.customizer;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Map.Entry;
32 import java.util.Properties JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import java.util.Vector JavaDoc;
36 import javax.swing.ButtonModel JavaDoc;
37 import javax.swing.ComboBoxModel JavaDoc;
38 import javax.swing.DefaultListModel JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JToggleButton JavaDoc;
41 import javax.swing.ListCellRenderer JavaDoc;
42 import javax.swing.table.DefaultTableModel JavaDoc;
43 import javax.swing.text.BadLocationException JavaDoc;
44 import javax.swing.text.Document JavaDoc;
45 import org.netbeans.api.project.ProjectManager;
46 import org.netbeans.modules.ruby.railsprojects.RailsProject;
47 import org.netbeans.modules.ruby.railsprojects.RailsProjectUtil;
48 import org.netbeans.modules.ruby.railsprojects.SourceRoots;
49 import org.netbeans.modules.ruby.railsprojects.UpdateHelper;
50 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
51 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties;
52 import org.netbeans.modules.ruby.spi.project.support.rake.GeneratedFilesHelper;
53 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator;
54 import org.netbeans.modules.ruby.spi.project.support.rake.ReferenceHelper;
55 import org.netbeans.modules.ruby.spi.project.support.rake.ui.StoreGroup;
56 import org.openide.DialogDisplayer;
57 import org.openide.ErrorManager;
58 import org.openide.NotifyDescriptor;
59 import org.openide.filesystems.FileObject;
60 import org.openide.filesystems.FileUtil;
61 import org.openide.util.Mutex;
62 import org.openide.util.MutexException;
63 import org.openide.util.NbBundle;
64 import org.openide.util.Utilities;
65
66 /**
67  * @author Petr Hrebejk
68  */

69 public class RailsProjectProperties {
70     // Special properties of the project
71
public static final String JavaDoc Ruby_PROJECT_NAME = "j2se.project.name"; // NOI18N
72
public static final String JavaDoc JAVA_PLATFORM = "platform.active"; // NOI18N
73

74     // Properties stored in the PROJECT.PROPERTIES
75
// TODO - nuke me!
76
public static final String JavaDoc MAIN_CLASS = "main.file"; // NOI18N
77
public static final String JavaDoc JAVAC_COMPILER_ARG = "javac.compilerargs"; //NOI18N
78
public static final String JavaDoc RUN_JVM_ARGS = "run.jvmargs"; // NOI18N
79
public static final String JavaDoc RUN_WORK_DIR = "work.dir"; // NOI18N
80
public static final String JavaDoc DIST_DIR = "dist.dir"; // NOI18N
81
public static final String JavaDoc DIST_JAR = "dist.jar"; // NOI18N
82
public static final String JavaDoc JAVAC_CLASSPATH = "javac.classpath"; // NOI18N
83
public static final String JavaDoc RUN_CLASSPATH = "run.classpath"; // NOI18N
84
public static final String JavaDoc DEBUG_CLASSPATH = "debug.classpath"; // NOI18N
85
public static final String JavaDoc JAR_COMPRESS = "jar.compress"; // NOI18N
86
public static final String JavaDoc JAVAC_SOURCE = "javac.source"; // NOI18N
87
public static final String JavaDoc JAVAC_TARGET = "javac.target"; // NOI18N
88
public static final String JavaDoc JAVAC_TEST_CLASSPATH = "javac.test.classpath"; // NOI18N
89
public static final String JavaDoc RUN_TEST_CLASSPATH = "run.test.classpath"; // NOI18N
90
public static final String JavaDoc BUILD_DIR = "build.dir"; // NOI18N
91
public static final String JavaDoc BUILD_CLASSES_DIR = "build.classes.dir"; // NOI18N
92
public static final String JavaDoc BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
93
public static final String JavaDoc BUILD_TEST_RESULTS_DIR = "build.test.results.dir"; // NOI18N
94
public static final String JavaDoc BUILD_CLASSES_EXCLUDES = "build.classes.excludes"; // NOI18N
95
public static final String JavaDoc DIST_JAVADOC_DIR = "dist.javadoc.dir"; // NOI18N
96
public static final String JavaDoc NO_DEPENDENCIES="no.dependencies"; // NOI18N
97
public static final String JavaDoc DEBUG_TEST_CLASSPATH = "debug.test.classpath"; // NOI18N
98

99                     
100     // Properties stored in the PRIVATE.PROPERTIES
101
public static final String JavaDoc APPLICATION_ARGS = "application.args"; // NOI18N
102

103     // CustomizerCompile
104
// ButtonModel NO_DEPENDENCIES_MODEL;
105
Document JavaDoc JAVAC_COMPILER_ARG_MODEL;
106     
107     // CustomizerRun
108
Map JavaDoc<String JavaDoc/*|null*/,Map JavaDoc<String JavaDoc,String JavaDoc/*|null*/>/*|null*/> RUN_CONFIGS;
109     String JavaDoc activeConfig;
110
111     // CustomizerRunTest
112

113     // Private fields ----------------------------------------------------------------------
114
private RailsProject project;
115     private HashMap JavaDoc properties;
116     private UpdateHelper updateHelper;
117     private PropertyEvaluator evaluator;
118     private ReferenceHelper refHelper;
119     private GeneratedFilesHelper genFileHelper;
120     
121     private StoreGroup privateGroup;
122     private StoreGroup projectGroup;
123     
124     RailsProject getProject() {
125         return project;
126     }
127     
128     /** Creates a new instance of RubyUIProperties and initializes them */
129     public RailsProjectProperties( RailsProject project, UpdateHelper updateHelper, PropertyEvaluator evaluator, ReferenceHelper refHelper, GeneratedFilesHelper genFileHelper ) {
130         this.project = project;
131         this.updateHelper = updateHelper;
132         this.evaluator = evaluator;
133         this.refHelper = refHelper;
134         this.genFileHelper = genFileHelper;
135         //this.cs = new ClassPathSupport( evaluator, refHelper, updateHelper.getRakeProjectHelper(), WELL_KNOWN_PATHS, LIBRARY_PREFIX, LIBRARY_SUFFIX, ANT_ARTIFACT_PREFIX );
136

137         privateGroup = new StoreGroup();
138         projectGroup = new StoreGroup();
139         
140         init(); // Load known properties
141
}
142
143     /** Initializes the visual models
144      */

145     private void init() {
146         JAVAC_COMPILER_ARG_MODEL = projectGroup.createStringDocument( evaluator, JAVAC_COMPILER_ARG );
147         
148         // CustomizerRun
149
RUN_CONFIGS = readRunConfigs();
150         activeConfig = evaluator.getProperty("config");
151                 
152     }
153     
154     public void save() {
155         try {
156             // Store properties
157
Boolean JavaDoc result = (Boolean JavaDoc) ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
158                 final FileObject projectDir = updateHelper.getRakeProjectHelper().getProjectDirectory();
159                 public Object JavaDoc run() throws IOException JavaDoc {
160                     if ((genFileHelper.getBuildScriptState(GeneratedFilesHelper.BUILD_IMPL_XML_PATH,
161                         RailsProject.class.getResource("resources/build-impl.xsl") //NOI18N
162
)
163                         & GeneratedFilesHelper.FLAG_MODIFIED) == GeneratedFilesHelper.FLAG_MODIFIED) { //NOI18N
164
if (showModifiedMessage (NbBundle.getMessage(RailsProjectProperties.class,"TXT_ModifiedTitle"))) {
165                             //Delete user modified build-impl.xml
166
FileObject fo = projectDir.getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
167                             if (fo != null) {
168                                 fo.delete();
169                             }
170                         }
171                         else {
172                             return Boolean.FALSE;
173                         }
174                     }
175                     storeProperties();
176                     return Boolean.TRUE;
177                 }
178             });
179             // and save the project
180
if (result == Boolean.TRUE) {
181                 ProjectManager.getDefault().saveProject(project);
182             }
183         }
184         catch (MutexException e) {
185             ErrorManager.getDefault().notify((IOException JavaDoc)e.getException());
186         }
187         catch ( IOException JavaDoc ex ) {
188             ErrorManager.getDefault().notify( ex );
189         }
190     }
191     
192     
193         
194     private void storeProperties() throws IOException JavaDoc {
195         // Store standard properties
196
EditableProperties projectProperties = updateHelper.getProperties( RakeProjectHelper.PROJECT_PROPERTIES_PATH );
197         EditableProperties privateProperties = updateHelper.getProperties( RakeProjectHelper.PRIVATE_PROPERTIES_PATH );
198         
199         // Standard store of the properties
200
projectGroup.store( projectProperties );
201         privateGroup.store( privateProperties );
202         
203         storeRunConfigs(RUN_CONFIGS, projectProperties, privateProperties);
204         EditableProperties ep = updateHelper.getProperties("nbproject/private/config.properties");
205         if (activeConfig == null) {
206             ep.remove("config");
207         } else {
208             ep.setProperty("config", activeConfig);
209         }
210         updateHelper.putProperties("nbproject/private/config.properties", ep);
211
212         // Store the property changes into the project
213
updateHelper.putProperties( RakeProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties );
214         updateHelper.putProperties( RakeProjectHelper.PRIVATE_PROPERTIES_PATH, privateProperties );
215         
216     }
217     
218     private static boolean showModifiedMessage (String JavaDoc title) {
219         String JavaDoc message = NbBundle.getMessage(RailsProjectProperties.class,"TXT_Regenerate");
220         JButton JavaDoc regenerateButton = new JButton JavaDoc (NbBundle.getMessage(RailsProjectProperties.class,"CTL_RegenerateButton"));
221         regenerateButton.setDefaultCapable(true);
222         regenerateButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(RailsProjectProperties.class,"AD_RegenerateButton"));
223         NotifyDescriptor d = new NotifyDescriptor.Message (message, NotifyDescriptor.WARNING_MESSAGE);
224         d.setTitle(title);
225         d.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
226         d.setOptions(new Object JavaDoc[] {regenerateButton, NotifyDescriptor.CANCEL_OPTION});
227         return DialogDisplayer.getDefault().notify(d) == regenerateButton;
228     }
229     
230     /**
231      * A mess.
232      */

233     Map JavaDoc<String JavaDoc/*|null*/,Map JavaDoc<String JavaDoc,String JavaDoc>> readRunConfigs() {
234         Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>> m = new TreeMap JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>>(new Comparator JavaDoc<String JavaDoc>() {
235             public int compare(String JavaDoc s1, String JavaDoc s2) {
236                 return s1 != null ? (s2 != null ? s1.compareTo(s2) : 1) : (s2 != null ? -1 : 0);
237             }
238         });
239         Map JavaDoc<String JavaDoc,String JavaDoc> def = new TreeMap JavaDoc<String JavaDoc,String JavaDoc>();
240         for (String JavaDoc prop : new String JavaDoc[] {MAIN_CLASS, APPLICATION_ARGS, RUN_JVM_ARGS, RUN_WORK_DIR}) {
241             String JavaDoc v = updateHelper.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(prop);
242             if (v == null) {
243                 v = updateHelper.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(prop);
244             }
245             if (v != null) {
246                 def.put(prop, v);
247             }
248         }
249         m.put(null, def);
250         FileObject configs = project.getProjectDirectory().getFileObject("nbproject/configs");
251         if (configs != null) {
252             for (FileObject kid : configs.getChildren()) {
253                 if (!kid.hasExt("properties")) {
254                     continue;
255                 }
256                 m.put(kid.getName(), new TreeMap JavaDoc<String JavaDoc,String JavaDoc>(updateHelper.getProperties(FileUtil.getRelativePath(project.getProjectDirectory(), kid))));
257             }
258         }
259         configs = project.getProjectDirectory().getFileObject("nbproject/private/configs");
260         if (configs != null) {
261             for (FileObject kid : configs.getChildren()) {
262                 if (!kid.hasExt("properties")) {
263                     continue;
264                 }
265                 Map JavaDoc<String JavaDoc,String JavaDoc> c = m.get(kid.getName());
266                 if (c == null) {
267                     continue;
268                 }
269                 c.putAll(new HashMap JavaDoc<String JavaDoc,String JavaDoc>(updateHelper.getProperties(FileUtil.getRelativePath(project.getProjectDirectory(), kid))));
270             }
271         }
272         //System.err.println("readRunConfigs: " + m);
273
return m;
274     }
275
276     /**
277      * A royal mess.
278      */

279     void storeRunConfigs(Map JavaDoc<String JavaDoc/*|null*/,Map JavaDoc<String JavaDoc,String JavaDoc/*|null*/>/*|null*/> configs,
280             EditableProperties projectProperties, EditableProperties privateProperties) throws IOException JavaDoc {
281         //System.err.println("storeRunConfigs: " + configs);
282
Map JavaDoc<String JavaDoc,String JavaDoc> def = configs.get(null);
283         for (String JavaDoc prop : new String JavaDoc[] {MAIN_CLASS, APPLICATION_ARGS, RUN_JVM_ARGS, RUN_WORK_DIR}) {
284             String JavaDoc v = def.get(prop);
285             EditableProperties ep = (prop.equals(APPLICATION_ARGS) || prop.equals(RUN_WORK_DIR)) ?
286                 privateProperties : projectProperties;
287             if (!Utilities.compareObjects(v, ep.getProperty(prop))) {
288                 if (v != null && v.length() > 0) {
289                     ep.setProperty(prop, v);
290                 } else {
291                     ep.remove(prop);
292                 }
293             }
294         }
295         for (Map.Entry JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>> entry : configs.entrySet()) {
296             String JavaDoc config = entry.getKey();
297             if (config == null) {
298                 continue;
299             }
300             String JavaDoc sharedPath = "nbproject/configs/" + config + ".properties"; // NOI18N
301
String JavaDoc privatePath = "nbproject/private/configs/" + config + ".properties"; // NOI18N
302
Map JavaDoc<String JavaDoc,String JavaDoc> c = entry.getValue();
303             if (c == null) {
304                 updateHelper.putProperties(sharedPath, null);
305                 updateHelper.putProperties(privatePath, null);
306                 continue;
307             }
308             for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry2 : c.entrySet()) {
309                 String JavaDoc prop = entry2.getKey();
310                 String JavaDoc v = entry2.getValue();
311                 String JavaDoc path = (prop.equals(APPLICATION_ARGS) || prop.equals(RUN_WORK_DIR)) ?
312                     privatePath : sharedPath;
313                 EditableProperties ep = updateHelper.getProperties(path);
314                 if (!Utilities.compareObjects(v, ep.getProperty(prop))) {
315                     if (v != null && (v.length() > 0 || (def.get(prop) != null && def.get(prop).length() > 0))) {
316                         ep.setProperty(prop, v);
317                     } else {
318                         ep.remove(prop);
319                     }
320                     updateHelper.putProperties(path, ep);
321                 }
322             }
323             // Make sure the definition file is always created, even if it is empty.
324
updateHelper.putProperties(sharedPath, updateHelper.getProperties(sharedPath));
325         }
326     }
327 }
328
Popular Tags