1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.io.IOException ; 25 import java.text.MessageFormat ; 26 import java.util.Locale ; 27 import org.openide.DialogDisplayer; 28 import org.openide.NotifyDescriptor; 29 import org.openide.cookies.SaveCookie; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileSystem; 32 import org.openide.filesystems.Repository; 33 import org.openide.loaders.FileEntry; 34 35 import org.openide.loaders.MultiDataObject; 36 import org.openide.util.NbBundle; 37 38 39 44 public final class Util extends Object { 45 46 47 public static final String HELP_ID_PROPERTIES = "propfiles.prop"; 49 public static final String HELP_ID_CREATING = "propfiles.creating"; 51 public static final String HELP_ID_ADDING = "propfiles.adding"; 53 public static final String HELP_ID_MODIFYING 54 = "propfiles.modifying"; 56 public static final String HELP_ID_ADDLOCALE 57 = "propfiles.addlocale"; 59 public static final String HELP_ID_EDITLOCALE 60 = "propfiles.editlocale"; 62 63 public static final char PRB_SEPARATOR_CHAR 64 = PropertiesDataLoader.PRB_SEPARATOR_CHAR; 65 66 public static final int LABEL_FIRST_PART_LENGTH = 10; 67 68 69 public static String stringToKey(String source) { 70 StringBuffer result = new StringBuffer (); 71 for (int i = 0; i < source.length(); i++) { 72 char x = source.charAt(i); 73 switch (x) { 74 case '=': 75 case ':': 76 case '\t': 77 case '\r': 78 case '\n': 79 case '\f': 80 case ' ': 81 result.append('_'); break; 82 default: 83 result.append(x); 84 } 85 } 86 return result.toString(); 87 } 88 89 95 public static String assembleName (String baseName, String lang) { 96 if (lang.length() == 0) { 97 return baseName; 98 } else { 99 if (lang.charAt(0) != PRB_SEPARATOR_CHAR) { 100 StringBuffer res = new StringBuffer ().append(baseName) 101 .append(PRB_SEPARATOR_CHAR) 102 .append(lang); 103 return res.toString(); 104 } else { 105 return baseName + lang; 106 } 107 } 108 } 109 110 125 public static String getLocaleSuffix(MultiDataObject.Entry fe) { 126 MultiDataObject.Entry pe = fe.getDataObject().getPrimaryEntry(); 127 if (fe == pe) { 128 return ""; } 130 String myName = fe.getFile().getName(); 131 String baseName = pe.getFile().getName(); 132 assert myName.startsWith(baseName); 133 return myName.substring(baseName.length()); 134 } 135 136 155 public static String getLanguage(final String localeSuffix) { 156 return getFirstPart(localeSuffix); 157 } 158 159 178 public static String getCountry(final String localeSuffix) { 179 if (localeSuffix.length() == 0) { 180 return null; 181 } 182 int start = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, 1); 183 return (start != -1) 184 ? getFirstPart(localeSuffix.substring(start)) 185 : ""; } 187 188 207 public static String getVariant(final String localeSuffix) { 208 if (localeSuffix.length() == 0) { 209 return null; 210 } 211 int start = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, 1); 212 if (start == -1) { 213 return ""; } 215 start = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, start + 1); 216 return (start != -1) ? localeSuffix.substring(start + 1) : ""; } 218 219 229 private static String getFirstPart(String localeSuffix) { 230 if (localeSuffix.length() == 0) { 231 return null; 232 } 233 234 assert localeSuffix.charAt(0) == PRB_SEPARATOR_CHAR; 235 236 int end = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, 1); 237 return (end != -1) ? localeSuffix.substring(1, end) 238 : localeSuffix.substring(1); 239 } 240 241 242 public static String getLocaleLabel(MultiDataObject.Entry fe) { 243 244 String localeSuffix = getLocaleSuffix(fe); 245 String language; 246 String country; 247 String variant; 248 249 254 if (localeSuffix.length() == 0) { 255 language = ""; country = ""; variant = ""; } else { 259 language = getLanguage(localeSuffix); 260 country = getCountry(localeSuffix); 261 variant = getVariant(localeSuffix); 262 263 language = language.length() != 0 ? language : ""; country = country.length() != 0 ? country : ""; variant = variant.length() != 0 ? variant : ""; } 268 269 String defaultLangName = null; 270 if (language == "") { defaultLangName = NbBundle.getMessage( 272 Util.class, 273 "LAB_defaultLanguage"); } 275 276 277 if (language == "" && country == "" && variant == "") { return defaultLangName; 279 } 280 281 String localeSpec = localeSuffix.substring(1); 282 Locale locale = new Locale (language, country, variant); 283 284 285 String langName; 286 if (language == "") { langName = defaultLangName; 288 } else { 289 langName = locale.getDisplayLanguage(); 290 if (langName.equals(language)) { 291 langName = NbBundle.getMessage(Util.class, 292 "LAB_unknownLanguage", language); 294 } 295 } 296 297 298 if (country == "" && variant == "") { return NbBundle.getMessage(Util.class, 300 "LAB_localeSpecLang", localeSpec, 302 langName); 303 } 304 305 306 String countryName = ""; if (country != "") { countryName = locale.getDisplayCountry(); 309 if (countryName.equals(country)) { 310 countryName = NbBundle.getMessage(Util.class, 311 "LAB_unknownCountry", country); 313 } 314 } 315 316 317 String variantName = variant == "" ? "" : locale.getDisplayVariant(); 319 320 321 String countryAndVariant; 322 if (variantName == "") { countryAndVariant = countryName; 324 } else if (countryName == "") { countryAndVariant = variantName; 326 } else { 327 countryAndVariant = countryName + ", " + variantName; } 329 return NbBundle.getMessage(Util.class, 330 "LAB_localeSpecLangCountry", localeSpec, 332 langName, 333 countryAndVariant); 334 335 } 336 337 339 private static void notifyError(String locale) { 340 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 341 MessageFormat.format( 342 NbBundle.getBundle(PropertiesDataNode.class).getString("MSG_LangExists"), 343 new Object [] {locale}), NotifyDescriptor.ERROR_MESSAGE); 344 DialogDisplayer.getDefault().notify(msg); 345 } 346 347 public static void createLocaleFile(PropertiesDataObject propertiesDataObject, 348 String locale, 349 boolean copyInitialContent) 350 { 351 try { 352 if(locale.length() == 0) { 353 notifyError(locale); 355 return; 356 } 357 358 if(propertiesDataObject != null) { 359 FileObject file = propertiesDataObject.getPrimaryFile(); 360 final String newName = file.getName() + PropertiesDataLoader.PRB_SEPARATOR_CHAR + locale; 361 final FileObject folder = file.getParent(); 362 java.util.Iterator it = propertiesDataObject.secondaryEntries().iterator(); 364 while (it.hasNext()) { 365 FileObject f = ((FileEntry)it.next()).getFile(); 366 if (newName.startsWith(f.getName()) && f.getName().length() > file.getName().length()) 367 file = f; 368 } 369 if (file.getName().equals(newName)) 370 return; 372 if (!copyInitialContent) { FileSystem defaultFS = Repository.getDefault().getDefaultFileSystem(); 374 file = defaultFS.findResource("Templates/Other/properties.properties"); } 376 377 final FileObject templateFile = file; 378 379 SaveCookie save = (SaveCookie) propertiesDataObject.getCookie(SaveCookie.class); 380 if (save != null) 381 save.save(); 382 383 421 422 if(folder.getFileObject(newName, PropertiesDataLoader.PROPERTIES_EXTENSION) == null) { 424 folder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 425 public void run() throws IOException { 426 templateFile.copy(folder, newName, PropertiesDataLoader.PROPERTIES_EXTENSION); 427 } 428 }); } 430 } 431 } catch(IOException ioe) { 432 if(Boolean.getBoolean("netbeans.debug.exceptions")) ioe.printStackTrace(); 434 435 notifyError(locale); 436 } 437 } 438 } 439 | Popular Tags |