1 19 20 21 package org.netbeans.modules.i18n.form; 22 23 24 import java.awt.Component ; 25 import java.beans.PropertyEditorSupport ; 26 import java.io.IOException ; 27 import java.text.MessageFormat ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.ResourceBundle ; 31 import org.netbeans.api.java.classpath.ClassPath; 32 33 import org.netbeans.modules.form.FormModel; 34 import org.netbeans.modules.form.FormAwareEditor; 35 import org.netbeans.modules.form.FormDataObject; 36 import org.netbeans.modules.form.NamedPropertyEditor; 37 import org.netbeans.modules.form.FormEditorSupport; 38 import org.netbeans.modules.i18n.I18nPanel; 39 import org.netbeans.modules.i18n.I18nString; 40 import org.netbeans.modules.i18n.I18nUtil; 41 import org.netbeans.modules.i18n.java.JavaI18nSupport; 42 43 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 44 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.Repository; 47 import org.openide.loaders.DataObject; 48 import org.openide.NotifyDescriptor; 49 50 import org.openide.ErrorManager; 51 import org.openide.util.HelpCtx; 52 import org.openide.util.MapFormat; 53 import org.openide.util.NbBundle; 54 55 import org.w3c.dom.Element ; 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.DOMException ; 58 import org.w3c.dom.NamedNodeMap ; 59 import org.w3c.dom.Node ; 60 import org.w3c.dom.NodeList ; 61 import org.netbeans.api.project.Project; 62 63 64 77 public class FormI18nMnemonicEditor extends PropertyEditorSupport implements FormAwareEditor, NamedPropertyEditor, XMLPropertyEditor { 78 79 80 private FormI18nMnemonic formI18nMnemonic; 81 82 85 private FormDataObject sourceDataObject; 86 87 private final ResourceBundle bundle; 88 89 90 public static final String XML_RESOURCESTRING = "ResourceString"; 92 public static final String XML_ARGUMENT = "Argument"; 94 public static final String ATTR_BUNDLE = "bundle"; 96 public static final String ATTR_KEY = "key"; 98 public static final String ATTR_IDENTIFIER = "identifier"; 100 public static final String ATTR_REPLACE_FORMAT = "replaceFormat"; 102 public static final String ATTR_INDEX = "index"; 104 public static final String ATTR_JAVACODE = "javacode"; 106 107 private static final int MAX_INDEX = 1000; 108 109 110 public FormI18nMnemonicEditor() { 111 bundle = NbBundle.getBundle(FormI18nMnemonicEditor.class); 112 } 113 114 private Project getProject() { 115 return org.netbeans.modules.i18n.Util.getProjectFor(sourceDataObject); 116 } 117 118 120 public String [] getTags() { 121 return null; 122 } 123 124 126 public void setAsText(String text) {} 127 128 129 131 public String getAsText() { 132 FormI18nMnemonic formI18nMnemonic = (FormI18nMnemonic)getValue(); 133 DataObject dataObject = formI18nMnemonic.getSupport().getResourceHolder().getResource(); 134 135 if (dataObject == null || formI18nMnemonic.getKey() == null) { 136 return bundle.getString("TXT_InvalidValue"); 137 } else { 138 String resourceName = org.netbeans.modules.i18n.Util. 139 getResourceName(formI18nMnemonic.getSupport().getSourceDataObject().getPrimaryFile(), 140 dataObject.getPrimaryFile(), 141 '/', false); return MessageFormat.format( 143 bundle.getString("TXT_Key"), 144 new Object [] { 145 formI18nMnemonic.getKey(), 146 resourceName , 147 } 148 ); 149 } 150 } 151 152 160 public String getJavaInitializationString() { 161 return ((FormI18nMnemonic)getValue()).getReplaceString(); 162 } 163 164 166 public Component getCustomEditor() { 167 return new CustomEditor(new FormI18nMnemonic((FormI18nMnemonic)getValue()), getProject(), sourceDataObject.getPrimaryFile()); 168 } 169 170 172 public boolean supportsCustomEditor() { 173 return true; 174 } 175 176 178 public Object getValue() { 179 if(formI18nMnemonic == null) { 180 formI18nMnemonic = createFormI18nMnemonic(); 181 182 if(I18nUtil.getOptions().getLastResource2() != null) 183 formI18nMnemonic.getSupport().getResourceHolder().setResource(I18nUtil.getOptions().getLastResource2()); 184 } 185 186 return formI18nMnemonic; 187 } 188 189 191 public void setValue(Object object) { 192 if(object instanceof FormI18nMnemonic) 193 formI18nMnemonic = (FormI18nMnemonic)object; 194 else { 195 formI18nMnemonic = createFormI18nMnemonic(); 196 197 if(I18nUtil.getOptions().getLastResource2() != null) 198 formI18nMnemonic.getSupport().getResourceHolder().setResource(I18nUtil.getOptions().getLastResource2()); 199 } 200 } 201 202 203 private FormI18nMnemonic createFormI18nMnemonic() { 204 return new FormI18nMnemonic(new FormI18nSupport.Factory().createI18nSupport(sourceDataObject)); 206 } 207 208 214 public void setFormModel(FormModel model) { 215 sourceDataObject = FormEditorSupport.getFormDataObject(model); 216 } 217 218 222 public String getDisplayName () { 223 return bundle.getString("PROP_MenmonicEditor_name"); 224 } 225 226 227 236 public void readFromXML(Node domNode) throws IOException { 237 if(!XML_RESOURCESTRING.equals (domNode.getNodeName ())) { 238 throw new IOException (); 239 } 240 241 FormI18nMnemonic formI18nMnemonic = createFormI18nMnemonic(); 242 243 NamedNodeMap namedNodes = domNode.getAttributes (); 244 245 try { 246 Node node; 247 node = namedNodes.getNamedItem(ATTR_BUNDLE); 249 String bundleName = (node == null) ? null : node.getNodeValue(); 250 251 node = namedNodes.getNamedItem(ATTR_KEY); 253 String key = (node == null) ? null : node.getNodeValue(); 254 255 if(bundleName != null) { 257 FileObject sourceFo = sourceDataObject.getPrimaryFile(); 258 if ( sourceFo != null ) { 259 FileObject fileObject = org.netbeans.modules.i18n.Util. 260 getResource(sourceFo, bundleName); 261 262 if(fileObject != null) { 263 try { 264 DataObject dataObject = DataObject.find(fileObject); 265 if(dataObject.getClass().equals(formI18nMnemonic.getSupport().getResourceHolder().getResourceClasses()[0])) formI18nMnemonic.getSupport().getResourceHolder().setResource(dataObject); 267 } 268 catch (IOException e) { 269 } 270 } 271 } 272 273 } 274 275 if(key != null && key.length() > 0) { 277 formI18nMnemonic.setKey(key); 278 279 formI18nMnemonic.setValue(formI18nMnemonic.getSupport().getResourceHolder().getValueForKey(key)); 281 formI18nMnemonic.setComment(formI18nMnemonic.getSupport().getResourceHolder().getCommentForKey(key)); 282 } 283 284 ((JavaI18nSupport)formI18nMnemonic.getSupport()).createIdentifier(); 286 287 node = namedNodes.getNamedItem(ATTR_IDENTIFIER); 288 if(node != null) { 289 String identifier = (node == null) ? null : node.getNodeValue(); 290 291 if(identifier != null) 292 ((JavaI18nSupport)formI18nMnemonic.getSupport()).setIdentifier(identifier); 293 } 294 295 node = namedNodes.getNamedItem(ATTR_REPLACE_FORMAT); 297 if(node != null) { 298 String replaceFormat = node.getNodeValue(); 299 300 if(replaceFormat != null && replaceFormat.length() > 0) { 301 302 Map map = new HashMap (6); 309 map.put("0", "{identifier}"); map.put("1", "{key}"); map.put("2", "{bundleNameSlashes}"); map.put("3", "{bundleNameDots}"); map.put("4", "{sourceFileName}"); map.put("fileName", "{sourceFileName}"); 316 String newReplaceFormat = MapFormat.format(replaceFormat, map); 317 318 formI18nMnemonic.setReplaceFormat(newReplaceFormat); 319 } 320 } else { 321 formI18nMnemonic.setReplaceFormat((String )I18nUtil.getDefaultReplaceFormat(false)); 323 } 324 } catch(NullPointerException npe) { 325 throw new IOException (); 326 } 327 328 if(domNode instanceof Element ) { 330 Element domElement = (Element )domNode; 331 NodeList argNodeList = domElement.getElementsByTagName(XML_ARGUMENT); 332 333 int highest = -1; 335 for(int i = 0; i < argNodeList.getLength(); i++) { 336 NamedNodeMap attributes = argNodeList.item(i).getAttributes(); 337 338 Node indexNode = attributes.getNamedItem (ATTR_INDEX); 339 String indexString = (indexNode == null) ? null : indexNode.getNodeValue (); 340 341 if(indexString != null) { 342 try { 343 int index = Integer.parseInt(indexString); 344 if (index > highest && index < MAX_INDEX) 345 highest = index; 346 } catch (Exception e) {} 347 } 348 } 349 350 String [] parameters = new String [highest + 1]; 352 353 for (int i = 0; i < argNodeList.getLength(); i++) { 355 NamedNodeMap attr = argNodeList.item(i).getAttributes (); 356 357 Node indexNode = attr.getNamedItem (ATTR_INDEX); 358 String indexString = (indexNode == null) ? null : indexNode.getNodeValue (); 359 if (indexString != null) { 360 try { 361 int index = Integer.parseInt(indexString); 362 if (index < MAX_INDEX) { 363 Node javaCodeNode = attr.getNamedItem (ATTR_JAVACODE); 364 String javaCode = (javaCodeNode == null) ? null : javaCodeNode.getNodeValue (); 365 parameters[index] = javaCode; 366 } 367 } catch (Exception e) {} 368 } 369 } 370 371 for (int i = 0; i < parameters.length; i++) 373 if (parameters[i] == null) 374 parameters[i] = ""; 376 formI18nMnemonic.setArguments(parameters); 378 } 379 380 setValue(formI18nMnemonic); 382 } 383 384 391 public Node storeToXML(Document doc) { 392 Element element = doc.createElement (XML_RESOURCESTRING); 393 394 String bundleName; 395 if (formI18nMnemonic.getSupport().getResourceHolder().getResource() == null) { 396 bundleName = ""; 397 } else { 398 bundleName = org.netbeans.modules.i18n.Util. 399 getResourceName(formI18nMnemonic.getSupport().getSourceDataObject().getPrimaryFile(), 400 formI18nMnemonic.getSupport().getResourceHolder().getResource().getPrimaryFile(),'/', true); 401 if (bundleName == null) bundleName = ""; 402 } 403 404 405 element.setAttribute(ATTR_BUNDLE, bundleName); 407 element.setAttribute(ATTR_KEY, (formI18nMnemonic.getKey() == null) ? "" : formI18nMnemonic.getKey()); JavaI18nSupport support = (JavaI18nSupport)formI18nMnemonic.getSupport(); 410 if(support.getIdentifier() == null) 411 support.createIdentifier(); 412 Map map = new HashMap (1); 413 map.put("identifier", support.getIdentifier()); element.setAttribute(ATTR_REPLACE_FORMAT, formI18nMnemonic.getReplaceFormat() == null ? "" : MapFormat.format(formI18nMnemonic.getReplaceFormat(), map) ); 416 String [] arguments = formI18nMnemonic.getArguments(); 418 for (int i = 0; i < arguments.length; i++) { 419 Element childElement = doc.createElement (XML_ARGUMENT); 420 childElement.setAttribute (ATTR_INDEX, "" + i); childElement.setAttribute (ATTR_JAVACODE, arguments[i]); 422 try { 423 element.appendChild(childElement); 424 } catch (DOMException de) {} 425 } 426 427 return element; 428 } 429 430 431 private static class CustomEditor extends I18nPanel implements EnhancedCustomPropertyEditor { 432 433 private final ResourceBundle bundle; 434 435 436 public CustomEditor(I18nString i18nString, Project project, FileObject file) { 437 super(i18nString.getSupport().getPropertyPanel(), false, project, file); 438 setI18nString(i18nString); 439 bundle = NbBundle.getBundle(FormI18nMnemonicEditor.class); 440 HelpCtx.setHelpIDString(this, I18nUtil.HELP_ID_FORMED); 441 } 442 443 444 public Object getPropertyValue() throws IllegalStateException { 445 I18nString i18nString = getI18nString(); 446 447 if(i18nString == null 448 || !(i18nString instanceof FormI18nMnemonic) 449 || i18nString.getSupport().getResourceHolder().getResource() == null 450 || i18nString.getKey() == null) { 451 452 IllegalStateException ise = new IllegalStateException (); 454 String message = bundle.getString("MSG_InvalidValue"); 455 ErrorManager.getDefault().annotate( 456 ise, ErrorManager.WARNING, message, 457 message, null, null); 458 throw ise; 459 } 460 461 i18nString.getSupport().getResourceHolder().addProperty( 463 i18nString.getKey(), 464 i18nString.getValue(), 465 i18nString.getComment(), 466 false ); 468 469 return i18nString; 470 } 471 472 } 473 474 475 } 476 | Popular Tags |