1 19 20 package org.netbeans.modules.i18n.form; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.beans.PropertyEditor ; 27 import java.io.IOException ; 28 import java.util.*; 29 import org.openide.ErrorManager; 30 import org.openide.DialogDisplayer; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.loaders.*; 34 import org.openide.cookies.SaveCookie; 35 import org.openide.cookies.EditorCookie; 36 import org.openide.DialogDescriptor; 37 import org.openide.util.NbBundle; 38 import org.netbeans.api.java.classpath.ClassPath; 39 import org.netbeans.api.java.project.JavaProjectConstants; 40 import org.netbeans.api.project.FileOwnerQuery; 41 import org.netbeans.api.project.Project; 42 import org.netbeans.api.project.ProjectUtils; 43 import org.netbeans.api.project.SourceGroup; 44 import org.netbeans.api.project.Sources; 45 import org.netbeans.modules.properties.*; 46 import org.netbeans.modules.i18n.*; 47 import org.netbeans.modules.i18n.java.JavaResourceHolder; 48 import org.netbeans.modules.form.I18nService; 49 import org.netbeans.modules.form.I18nValue; 50 51 56 public class I18nServiceImpl implements I18nService { 57 58 private Map changesMap = new HashMap(); 61 62 private static class ChangeInfo { 63 Map changed = new HashMap(); Set added = new HashSet(); } 66 67 71 public I18nValue create(String key, String value, DataObject srcDataObject) { 72 FormI18nString i18nString = new FormI18nString(srcDataObject); 73 i18nString.setKey(key); 74 i18nString.setValue(value); 75 return i18nString; 76 } 77 78 82 public I18nValue changeKey(I18nValue prev, String newKey) { 83 FormI18nString oldI18nString = (FormI18nString) prev; 84 FormI18nString changedI18nString; 85 if (oldI18nString.getKey() == I18nValue.COMPUTE_AUTO_KEY) { 86 changedI18nString = oldI18nString; 88 } 89 else { changedI18nString = new FormI18nString(oldI18nString); 91 changedI18nString.allData = oldI18nString.allData; 92 } 93 changedI18nString.setKey(newKey); 94 return changedI18nString; 95 } 96 97 101 public I18nValue changeValue(I18nValue prev, String value) { 102 FormI18nString i18nString = new FormI18nString((FormI18nString)prev); 103 i18nString.setValue(value); 104 return i18nString; 105 } 106 107 111 public I18nValue switchLocale(I18nValue value, String localeSuffix) { 112 if (value == null || value.getKey() == null) 113 return value; 114 115 FormI18nString i18nString = new FormI18nString((FormI18nString)value); 116 JavaResourceHolder rh = (JavaResourceHolder) i18nString.getSupport().getResourceHolder(); 117 rh.setLocalization(localeSuffix); 118 i18nString.setValue(rh.getValueForKey(i18nString.getKey())); 119 i18nString.setComment(rh.getCommentForKey(i18nString.getKey())); 120 return i18nString; 121 } 122 123 129 public void update(I18nValue oldValue, I18nValue newValue, 130 DataObject srcDataObject, String bundleName, String localeSuffix, 131 boolean canRemove) 132 throws IOException 133 { 134 FormI18nString oldI18nString = (FormI18nString) oldValue; 135 FormI18nString newI18nString = (FormI18nString) newValue; 136 137 if (oldI18nString != null) { 138 ResourceHolder oldRH = oldI18nString.getSupport().getResourceHolder(); 139 DataObject oldRes = oldRH.getResource(); 140 DataObject newRes = null; 141 if (newI18nString != null) { 142 ResourceHolder newRH = newI18nString.getSupport().getResourceHolder(); 143 newRes = newRH.getResource(); 144 if (newRes == null) { newRH.setResource(oldRes); 146 newRes = oldRes; 147 } 148 } 149 150 String oldKey = oldI18nString.getKey(); 151 if (canRemove && oldKey != null) { 152 JavaResourceHolder jrh = (JavaResourceHolder) oldRH; 153 Object allData = jrh.getAllData(oldKey); 154 registerChange(srcDataObject, oldRes, oldKey, allData); 155 156 if (newI18nString == null 157 || newI18nString.getKey() == null 158 || !newI18nString.getKey().equals(oldKey) 159 || newRes != oldRes) 160 { oldI18nString.allData = allData; 162 jrh.removeProperty(oldKey); 163 if (newI18nString != null) 164 newI18nString.allData = oldI18nString.allData; 165 } 166 else if (localeSuffix != null && !localeSuffix.equals("")) { oldI18nString.allData = allData; 169 } 170 171 if (newI18nString == null 172 && oldRes == getPropertiesDataObject(srcDataObject, bundleName)) 173 { oldRH.setResource(null); 175 } 176 } 177 } 178 179 if (newI18nString != null && newI18nString.getKey() != null 180 && newI18nString.getKey() != I18nValue.NOI18N_KEY) 181 { JavaResourceHolder rh = (JavaResourceHolder) newI18nString.getSupport().getResourceHolder(); 183 184 if (rh.getResource() == null) { DataObject propertiesDO = getPropertiesDataObject(srcDataObject, bundleName); 186 if (propertiesDO == null) { propertiesDO = createPropertiesDataObject(srcDataObject, bundleName); 188 if (propertiesDO == null) 189 return; 190 } 191 rh.setResource(propertiesDO); 192 193 newI18nString.setKey(rh.findFreeKey(newI18nString.getKey())); 195 } 196 197 rh.setLocalization(localeSuffix); 198 String key = newI18nString.getKey(); 199 if (!isValueUpToDate(rh, newI18nString)) { 200 if (newI18nString.allData != null) { rh.setAllData(key, newI18nString.allData); 202 newI18nString.allData = null; 203 newI18nString.setValue(rh.getValueForKey(key)); 205 newI18nString.setComment(rh.getCommentForKey(key)); 206 } 207 else { 208 rh.addProperty(key, newI18nString.getValue(), newI18nString.getComment(), true); 209 } 210 registerChange(srcDataObject, rh.getResource(), newI18nString.getKey(), null); 211 } 212 } 213 } 214 215 private static boolean isValueUpToDate(ResourceHolder rh, I18nString i18nString) { 216 String storedValue = rh.getValueForKey(i18nString.getKey()); 217 String storedComment = rh.getCommentForKey(i18nString.getKey()); 218 if ("".equals(storedComment)) storedComment = null; 220 String newValue = i18nString.getValue(); 221 String newComment = i18nString.getComment(); 222 if ("".equals(newComment)) newComment = null; 224 return (storedValue == newValue || (storedValue != null && storedValue.equals(newValue))) 225 && (storedComment == newComment || (storedComment != null && storedComment.equals(newComment))); 226 } 227 228 233 public PropertyEditor getPropertyEditor(Class type, PropertyEditor existing) { 234 return existing instanceof FormI18nStringEditor ? existing : new FormI18nStringEditor(); 235 } 236 237 244 public int analyzePropertyEditorChange(PropertyEditor oldPE, PropertyEditor newPE) { 245 if (oldPE instanceof FormI18nStringEditor) 246 return isPlainStringEditor(newPE) ? -1 : 0; 247 if (isPlainStringEditor(oldPE)) 248 return newPE instanceof FormI18nStringEditor ? 1 : 0; 249 return 0; 250 } 251 252 private static boolean isPlainStringEditor(PropertyEditor pe) { 253 return pe != null && pe.getClass().getName().endsWith(".StringEditor"); } 255 256 263 public Component getBundleSelectionComponent(final PropertyEditor prEd, DataObject srcDataObject) { 264 try { 265 final FileSelector fs = new FileSelector(srcDataObject.getPrimaryFile(), JavaResourceHolder.getTemplate()); 266 return fs.getDialog(NbBundle.getMessage(I18nServiceImpl.class, "CTL_SELECT_BUNDLE_TITLE"), new ActionListener () 268 { 269 public void actionPerformed(ActionEvent ev) { 270 DataObject bundleDO = fs.getSelectedDataObject(); 271 if (bundleDO != null) { 272 ClassPath cp = ClassPath.getClassPath(bundleDO.getPrimaryFile(), ClassPath.SOURCE); 273 if (cp != null) { 274 String bundleName = cp.getResourceName(bundleDO.getPrimaryFile(), '/', false); 275 prEd.setValue(bundleName); 276 } 277 } 278 } 279 }); 280 } 281 catch (IOException ex) { 282 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 284 } 285 return null; 286 } 287 288 293 public String [][] getAvailableLocales(DataObject srcDataObject, String bundleName) { 294 PropertiesDataObject dobj = null; 295 try { 296 dobj = getPropertiesDataObject(srcDataObject, bundleName); 297 } 298 catch (IOException ex) { 299 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 300 } 301 if (dobj == null) 302 return null; 303 304 List list = new ArrayList(); 305 list.add(dobj.getPrimaryEntry()); 306 list.addAll(dobj.secondaryEntries()); 307 Collections.sort(list, new Comparator() { 308 public int compare(Object o1, Object o2) { 309 MultiDataObject.Entry e1 = (MultiDataObject.Entry) o1; 310 MultiDataObject.Entry e2 = (MultiDataObject.Entry) o2; 311 return e1.getFile().getName().compareTo(e2.getFile().getName()); 312 } 313 }); 314 315 String [] locales = new String [list.size()]; 316 String [] displays = new String [list.size()]; 317 for (int i=0; i < list.size(); i++) { 318 MultiDataObject.Entry entry = (MultiDataObject.Entry) list.get(i); 319 locales[i] = org.netbeans.modules.properties.Util.getLocaleSuffix(entry); 320 displays[i] = org.netbeans.modules.properties.Util.getLocaleLabel(entry); 321 } 322 return new String [][] { locales, displays }; 323 } 324 325 331 public Component getCreateLocaleComponent(final PropertyEditor prEd, DataObject srcDataObject, String bundleName) { 332 final PropertiesDataObject propertiesDO; 333 try { 334 propertiesDO = getPropertiesDataObject(srcDataObject, bundleName); 335 } 336 catch (DataObjectNotFoundException ex) { 337 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 338 return null; 339 } 340 final Dialog [] dialog = new Dialog [1]; 341 final LocalePanel localePanel = new LocalePanel(); 342 343 DialogDescriptor dialogDescriptor = new DialogDescriptor( 344 localePanel, 345 NbBundle.getBundle(PropertiesDataObject.class).getString("CTL_NewLocaleTitle"), true, 347 DialogDescriptor.OK_CANCEL_OPTION, 348 DialogDescriptor.OK_OPTION, 349 new ActionListener () { 350 public void actionPerformed(ActionEvent evt) { 351 if (evt.getSource() == DialogDescriptor.OK_OPTION) { 352 String locale = localePanel.getLocale().toString(); 353 org.netbeans.modules.properties.Util.createLocaleFile( 354 propertiesDO, locale, false); 355 prEd.setValue("_" + locale); } 357 dialog[0].setVisible(false); 358 dialog[0].dispose(); 359 } 360 } 361 ); 362 dialog[0] = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 363 return dialog[0]; 364 } 365 366 371 public void autoSave(DataObject srcDataObject) { 372 Map relatedMap = (Map) changesMap.remove(srcDataObject); 373 if (relatedMap != null) { 374 for (Iterator it=relatedMap.keySet().iterator(); it.hasNext(); ) { 375 DataObject propertiesDO = (DataObject) it.next(); 376 SaveCookie save = (SaveCookie) propertiesDO.getCookie(SaveCookie.class); 379 if (save != null) { 380 try { 381 save.save(); 382 } 383 catch (IOException ex) { 384 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 385 } 386 } 387 } 388 } 389 } 390 391 395 public void close(DataObject srcDataObject) { 396 Map relatedMap = (Map) changesMap.remove(srcDataObject); 397 if (relatedMap != null) { 398 for (Iterator it=relatedMap.entrySet().iterator(); it.hasNext(); ) { 399 Map.Entry e = (Map.Entry) it.next(); 400 PropertiesDataObject propertiesDO = (PropertiesDataObject) e.getKey(); 401 ChangeInfo changes = (ChangeInfo) e.getValue(); 402 JavaResourceHolder rh = new JavaResourceHolder(); 403 rh.setResource(propertiesDO); 404 for (Iterator it2=changes.changed.entrySet().iterator(); it2.hasNext(); ) { 405 Map.Entry e2 = (Map.Entry) it2.next(); 406 String key = (String ) e2.getKey(); 407 Object allData = e2.getValue(); 408 rh.setAllData(key, allData); 409 } 410 for (Iterator it2=changes.added.iterator(); it2.hasNext(); ) { 411 String key = (String ) it2.next(); 412 rh.removeProperty(key); 413 } 414 } 426 } 427 } 428 429 436 public boolean isDefaultInternationalizableProject(DataObject srcDataObject) { 437 return org.netbeans.modules.i18n.Util.isNbBundleAvailable(srcDataObject); 438 } 439 440 442 private static PropertiesDataObject getPropertiesDataObject(DataObject srcDataObject, String bundleName) 443 throws DataObjectNotFoundException 444 { 445 if (bundleName.startsWith("/")) bundleName = bundleName.substring(1); 447 if (!bundleName.toLowerCase().endsWith(".properties")) bundleName = bundleName + ".properties"; FileObject bundleFile = org.netbeans.modules.i18n.Util 450 .getResource(srcDataObject.getPrimaryFile(), bundleName); 451 if (bundleFile != null) { 452 DataObject dobj = DataObject.find(bundleFile); 453 if (dobj instanceof PropertiesDataObject) 454 return (PropertiesDataObject) dobj; 455 } 456 return null; 457 } 458 459 private static DataObject createPropertiesDataObject(DataObject srcDataObject, 460 String filePath) 461 throws IOException 462 { 463 if (filePath == null) 464 return null; 465 466 FileObject folder; 467 ClassPath cp = ClassPath.getClassPath(srcDataObject.getPrimaryFile(), ClassPath.SOURCE); 468 FileObject root = cp.getRoots()[0]; 469 String fileName; 470 int idx = filePath.lastIndexOf('/'); 471 boolean hasCustomRes = false; 472 Project owner = FileOwnerQuery.getOwner(srcDataObject.getPrimaryFile()); 473 if (owner != null) { 474 Sources srcs = ProjectUtils.getSources(owner); 476 SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_RESOURCES); 477 if (grps != null && grps.length > 0) { 478 hasCustomRes = true; 479 root = grps[0].getRootFolder(); 480 } 481 } 482 if (idx < 0) { folder = root; 484 fileName = filePath; 485 } 486 else { 487 String folderPath = filePath.substring(0, idx); 488 folder = cp.findResource(folderPath); 489 if (folder == null || hasCustomRes) { 490 folder = FileUtil.createFolder(root, folderPath); 491 } 492 fileName = filePath.substring(idx + 1); 493 } 494 if (folder != null) { 495 DataObject template = JavaResourceHolder.getTemplate(); 496 return template.createFromTemplate(DataFolder.findFolder(folder), fileName); 497 } 499 return null; } 501 502 507 private void registerChange(DataObject srcDO, DataObject propertiesDO, String key, Object allData) { 508 Map relatedMap = (Map) changesMap.get(srcDO); 509 if (relatedMap == null) { 510 relatedMap = new HashMap(); 511 changesMap.put(srcDO, relatedMap); 512 } 513 ChangeInfo changes = (ChangeInfo) relatedMap.get(propertiesDO); 514 if (changes == null) { 515 changes = new ChangeInfo(); 516 relatedMap.put(propertiesDO, changes); 517 } 518 if (!changes.changed.containsKey(key) && !changes.added.contains(key)) { 519 if (allData != null) changes.changed.put(key, allData); else changes.added.add(key); 524 } 525 } 526 } 527 | Popular Tags |