KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > universe > LocalizedBundleInfo


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.apisupport.project.universe;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.text.BreakIterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import org.netbeans.api.project.ProjectInformation;
32 import org.netbeans.modules.apisupport.project.Util;
33 import org.netbeans.spi.project.support.ant.EditableProperties;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileChangeAdapter;
36 import org.openide.filesystems.FileEvent;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.util.Utilities;
40
41 /**
42  * Represents localized information for a NetBeans module usually loaded from a
43  * <em>Bundle.properties</em> specified in a module's manifest. It is actaully
44  * back up by {@link EditableProperties} so any changes to this instance will
45  * behave exactly as specified in {@link EditableProperties} javadoc during
46  * storing.
47  *
48  * @author Martin Krauskopf
49  */

50 public final class LocalizedBundleInfo {
51     
52     public static final String JavaDoc NAME = "OpenIDE-Module-Name"; // NOI18N
53
public static final String JavaDoc DISPLAY_CATEGORY = "OpenIDE-Module-Display-Category"; // NOI18N
54
public static final String JavaDoc SHORT_DESCRIPTION = "OpenIDE-Module-Short-Description"; // NOI18N
55
public static final String JavaDoc LONG_DESCRIPTION = "OpenIDE-Module-Long-Description"; // NOI18N
56

57     static final LocalizedBundleInfo EMPTY = new LocalizedBundleInfo(new EditableProperties[] {new EditableProperties(true)});
58     
59     private final EditableProperties[] props;
60     private final File JavaDoc[] paths;
61     
62     private final PropertyChangeSupport JavaDoc changeSupport = new PropertyChangeSupport JavaDoc(this);
63
64     /** Whether this instance was modified since it was loaded or saved. */
65     private boolean modified;
66     
67     /**
68      * Returns instances initialized by data in the given {@link FileObject}.
69      * Note that instances created by this factory method are automatically
70      * storable (i.e. {@link #store} and {@link #reload} can be called) if the
71      * given object represents a regular {@link java.io.File}.
72      * @param bundleFO {@link FileObject} representing localizing bundle.
73      * Usually <em>bundle.properties</em> or its branded version.
74      * Given as an array so you can pass multiple locale variants (most specific last).
75      * @return instance representing data in the given bundle
76      */

77     public static LocalizedBundleInfo load(FileObject[] bundleFOs) throws IOException JavaDoc {
78         return new LocalizedBundleInfo(bundleFOs);
79     }
80     
81     /**
82      * Returns instances initialized by data in the given {@link FileObject}.
83      * Instances created by this factory method are not storable (i.e. {@link
84      * #store} and {@link #reload} cannot be called) until the {@link #setPath}
85      * is called upon this object.
86      * @param bundleIS input stream representing localizing bundle. Usually
87      * <em>bundle.properties</em> or its branded version.
88      * @return instance representing data in the given bundle
89      */

90     public static LocalizedBundleInfo load(InputStream JavaDoc[] bundleISs) throws IOException JavaDoc {
91         EditableProperties[] props = new EditableProperties[bundleISs.length];
92         for (int i = 0; i < props.length; i++) {
93             props[i] = new EditableProperties(true);
94             props[i].load(bundleISs[i]);
95         }
96         return new LocalizedBundleInfo(props);
97     }
98     
99     /** Use factory method instead. */
100     private LocalizedBundleInfo(EditableProperties[] props) {
101         this.props = props;
102         paths = new File JavaDoc[props.length];
103     }
104     
105     /** Use factory method instead. */
106     private LocalizedBundleInfo(FileObject[] bundleFOs) throws IOException JavaDoc {
107         if (bundleFOs == null || bundleFOs.length == 0) {
108             throw new IllegalArgumentException JavaDoc();
109         }
110         props = new EditableProperties[bundleFOs.length];
111         paths = new File JavaDoc[bundleFOs.length];
112         for (int i = 0; i < bundleFOs.length; i++) {
113             InputStream JavaDoc bundleIS = bundleFOs[i].getInputStream();
114             try {
115                 props[i] = new EditableProperties(true);
116                 props[i].load(bundleIS);
117             } finally {
118                 bundleIS.close();
119             }
120             paths[i] = FileUtil.toFile(bundleFOs[i]);
121             bundleFOs[i].addFileChangeListener(new FileChangeAdapter() {
122                 public void fileChanged(FileEvent fe) {
123                     try {
124                         LocalizedBundleInfo.this.reload();
125                     } catch (IOException JavaDoc e) {
126                         Util.err.log(ErrorManager.WARNING,
127                                 "Cannot reload localized bundle info " + // NOI18N
128
FileUtil.getFileDisplayName(fe.getFile()));
129                     }
130                 }
131             });
132         }
133     }
134     
135     /**
136      * Reload data of this localizing bundle info from the file represented by
137      * previously set path. Note that this instance already listens to the
138      * bundle properties file (or files if localized). So it just gives a
139      * possibility to force reloading in the case the properties were e.g.
140      * changed outside of IDE or using {@link java.io.File}.
141      */

142     public void reload() throws IOException JavaDoc {
143         String JavaDoc oldDisplayName = getDisplayName();
144         for (int i = 0; i < paths.length; i++) {
145             if (paths[i] == null) {
146                 props[i] = new EditableProperties(true);
147                 continue;
148             }
149             FileObject bundleFO = FileUtil.toFileObject(paths[i]);
150             props[i] = bundleFO != null ? Util.loadProperties(bundleFO) : new EditableProperties(true);
151         }
152         modified = false;
153         firePropertyChange(ProjectInformation.PROP_DISPLAY_NAME, oldDisplayName, getDisplayName());
154     }
155     
156     /**
157      * Reload this localizing bundle from the file specified by previously set
158      * path.
159      */

160     public void store() throws IOException JavaDoc {
161         for (int i = 0; i < paths.length; i++) {
162             if (paths[i] == null) {
163                 continue;
164             }
165             FileObject bundleFO = FileUtil.toFileObject(paths[i]);
166             if (bundleFO == null) {
167                 return;
168             }
169             Util.storeProperties(bundleFO, props[i]);
170         }
171         modified = false;
172     }
173     
174     /**
175      * Converts entries this instance represents into {@link
176      * EditableProperties}.
177      */

178     public EditableProperties toEditableProperties() {
179         return props[0];
180     }
181     
182     private String JavaDoc getProperty(String JavaDoc key) {
183         for (int i = props.length - 1; i >= 0; i--) {
184             if (props[i].containsKey(key)) {
185                 return props[i].getProperty(key);
186             }
187         }
188         return null;
189     }
190     
191     /**
192      * Tells whether this instance was modified since it was loaded or saved.
193      * I.e. if it needs to be saved or not.
194      */

195     public boolean isModified() {
196         return modified;
197     }
198     
199     public String JavaDoc getDisplayName() {
200         return getProperty(NAME);
201     }
202     
203     public void setDisplayName(String JavaDoc name) {
204         String JavaDoc oldDisplayName = getDisplayName();
205         this.setProperty(NAME, name, false);
206         firePropertyChange(ProjectInformation.PROP_DISPLAY_NAME, oldDisplayName, getDisplayName());
207     }
208     
209     public String JavaDoc getCategory() {
210         return getProperty(DISPLAY_CATEGORY);
211     }
212     
213     public void setCategory(String JavaDoc category) {
214         this.setProperty(DISPLAY_CATEGORY, category, false);
215     }
216     
217     public String JavaDoc getShortDescription() {
218         return getProperty(SHORT_DESCRIPTION);
219     }
220     
221     public void setShortDescription(String JavaDoc shortDescription) {
222         this.setProperty(SHORT_DESCRIPTION, shortDescription, false);
223     }
224     
225     public String JavaDoc getLongDescription() {
226         return getProperty(LONG_DESCRIPTION);
227     }
228     
229     public void setLongDescription(String JavaDoc longDescription) {
230         this.setProperty(LONG_DESCRIPTION, longDescription, true);
231     }
232     
233     public File JavaDoc[] getPaths() {
234         return paths;
235     }
236     
237     private void setProperty(String JavaDoc name, String JavaDoc value, boolean split) {
238         if (Utilities.compareObjects(value, getProperty(name))) {
239             return;
240         }
241         modified = true;
242         if (value != null) {
243             value = value.trim();
244         }
245         if (value != null && value.length() > 0) {
246             if (split) {
247                 props[props.length - 1].setProperty(name, splitBySentence(value));
248             } else {
249                 props[props.length - 1].setProperty(name, value);
250             }
251         } else {
252             for (int i = 0; i < props.length; i++) {
253                 props[i].remove(name);
254             }
255         }
256     }
257     
258     private static String JavaDoc[] splitBySentence(String JavaDoc text) {
259         List JavaDoc<String JavaDoc> sentences = new ArrayList JavaDoc();
260         // Use Locale.US since the customizer is setting the default (US) locale text only:
261
BreakIterator JavaDoc it = BreakIterator.getSentenceInstance(Locale.US);
262         it.setText(text);
263         int start = it.first();
264         int end;
265         while ((end = it.next()) != BreakIterator.DONE) {
266             sentences.add(text.substring(start, end));
267             start = end;
268         }
269         return (String JavaDoc[]) sentences.toArray(new String JavaDoc[sentences.size()]);
270     }
271     
272     public void addPropertyChangeListener(PropertyChangeListener JavaDoc pchl) {
273         changeSupport.addPropertyChangeListener(pchl);
274     }
275     
276     public void removePropertyChangeListener(PropertyChangeListener JavaDoc pchl) {
277         changeSupport.removePropertyChangeListener(pchl);
278     }
279     
280     private void firePropertyChange(String JavaDoc propName, Object JavaDoc oldValue, Object JavaDoc newValue) {
281         changeSupport.firePropertyChange(propName, oldValue, newValue);
282     }
283     
284     
285     public String JavaDoc toString() {
286         return "LocalizedBundleInfo[" + getDisplayName() + "; " + // NOI18N
287
getCategory() + "; " + // NOI18N
288
getShortDescription() + "; " + // NOI18N
289
getLongDescription() + "]"; // NOI18N
290
}
291     
292     public static interface Provider {
293         /** May return <code>null</code>. */
294         LocalizedBundleInfo getLocalizedBundleInfo();
295     }
296     
297 }
298
Popular Tags