1 19 20 package org.netbeans.modules.apisupport.project.universe; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.text.BreakIterator ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Locale ; 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 50 public final class LocalizedBundleInfo { 51 52 public static final String NAME = "OpenIDE-Module-Name"; public static final String DISPLAY_CATEGORY = "OpenIDE-Module-Display-Category"; public static final String SHORT_DESCRIPTION = "OpenIDE-Module-Short-Description"; public static final String LONG_DESCRIPTION = "OpenIDE-Module-Long-Description"; 57 static final LocalizedBundleInfo EMPTY = new LocalizedBundleInfo(new EditableProperties[] {new EditableProperties(true)}); 58 59 private final EditableProperties[] props; 60 private final File [] paths; 61 62 private final PropertyChangeSupport changeSupport = new PropertyChangeSupport (this); 63 64 65 private boolean modified; 66 67 77 public static LocalizedBundleInfo load(FileObject[] bundleFOs) throws IOException { 78 return new LocalizedBundleInfo(bundleFOs); 79 } 80 81 90 public static LocalizedBundleInfo load(InputStream [] bundleISs) throws IOException { 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 100 private LocalizedBundleInfo(EditableProperties[] props) { 101 this.props = props; 102 paths = new File [props.length]; 103 } 104 105 106 private LocalizedBundleInfo(FileObject[] bundleFOs) throws IOException { 107 if (bundleFOs == null || bundleFOs.length == 0) { 108 throw new IllegalArgumentException (); 109 } 110 props = new EditableProperties[bundleFOs.length]; 111 paths = new File [bundleFOs.length]; 112 for (int i = 0; i < bundleFOs.length; i++) { 113 InputStream 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 e) { 126 Util.err.log(ErrorManager.WARNING, 127 "Cannot reload localized bundle info " + FileUtil.getFileDisplayName(fe.getFile())); 129 } 130 } 131 }); 132 } 133 } 134 135 142 public void reload() throws IOException { 143 String 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 160 public void store() throws IOException { 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 178 public EditableProperties toEditableProperties() { 179 return props[0]; 180 } 181 182 private String getProperty(String 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 195 public boolean isModified() { 196 return modified; 197 } 198 199 public String getDisplayName() { 200 return getProperty(NAME); 201 } 202 203 public void setDisplayName(String name) { 204 String oldDisplayName = getDisplayName(); 205 this.setProperty(NAME, name, false); 206 firePropertyChange(ProjectInformation.PROP_DISPLAY_NAME, oldDisplayName, getDisplayName()); 207 } 208 209 public String getCategory() { 210 return getProperty(DISPLAY_CATEGORY); 211 } 212 213 public void setCategory(String category) { 214 this.setProperty(DISPLAY_CATEGORY, category, false); 215 } 216 217 public String getShortDescription() { 218 return getProperty(SHORT_DESCRIPTION); 219 } 220 221 public void setShortDescription(String shortDescription) { 222 this.setProperty(SHORT_DESCRIPTION, shortDescription, false); 223 } 224 225 public String getLongDescription() { 226 return getProperty(LONG_DESCRIPTION); 227 } 228 229 public void setLongDescription(String longDescription) { 230 this.setProperty(LONG_DESCRIPTION, longDescription, true); 231 } 232 233 public File [] getPaths() { 234 return paths; 235 } 236 237 private void setProperty(String name, String 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 [] splitBySentence(String text) { 259 List <String > sentences = new ArrayList (); 260 BreakIterator 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 []) sentences.toArray(new String [sentences.size()]); 270 } 271 272 public void addPropertyChangeListener(PropertyChangeListener pchl) { 273 changeSupport.addPropertyChangeListener(pchl); 274 } 275 276 public void removePropertyChangeListener(PropertyChangeListener pchl) { 277 changeSupport.removePropertyChangeListener(pchl); 278 } 279 280 private void firePropertyChange(String propName, Object oldValue, Object newValue) { 281 changeSupport.firePropertyChange(propName, oldValue, newValue); 282 } 283 284 285 public String toString() { 286 return "LocalizedBundleInfo[" + getDisplayName() + "; " + getCategory() + "; " + getShortDescription() + "; " + getLongDescription() + "]"; } 291 292 public static interface Provider { 293 294 LocalizedBundleInfo getLocalizedBundleInfo(); 295 } 296 297 } 298 | Popular Tags |