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 import org.openide.ErrorManager; 50 import org.openide.util.HelpCtx; 51 import org.openide.util.MapFormat; 52 import org.openide.util.NbBundle; 53 54 import org.w3c.dom.Element ; 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.DOMException ; 57 import org.w3c.dom.NamedNodeMap ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 import org.netbeans.api.project.Project; 61 62 63 76 public class FormI18nIntegerEditor extends PropertyEditorSupport implements FormAwareEditor, NamedPropertyEditor, XMLPropertyEditor { 77 78 79 private FormI18nInteger formI18nInteger; 80 81 84 private FormDataObject sourceDataObject; 85 86 87 public static final String XML_RESOURCESTRING = "ResourceString"; 89 public static final String XML_ARGUMENT = "Argument"; 91 public static final String ATTR_BUNDLE = "bundle"; 93 public static final String ATTR_KEY = "key"; 95 public static final String ATTR_IDENTIFIER = "identifier"; 97 public static final String ATTR_REPLACE_FORMAT = "replaceFormat"; 99 public static final String ATTR_INDEX = "index"; 101 public static final String ATTR_JAVACODE = "javacode"; 103 104 private static final int MAX_INDEX = 1000; 105 106 private final ResourceBundle bundle; 107 108 109 110 public FormI18nIntegerEditor() { 111 bundle = NbBundle.getBundle(FormI18nIntegerEditor.class); 112 } 113 114 115 117 public String [] getTags() { 118 return null; 119 } 120 121 123 public void setAsText(String text) {} 124 125 126 128 public String getAsText() { 129 FormI18nInteger formI18nInteger = (FormI18nInteger)getValue(); 130 DataObject dataObject = formI18nInteger.getSupport().getResourceHolder().getResource(); 131 132 if (dataObject == null || formI18nInteger.getKey() == null) { 133 return bundle.getString("TXT_InvalidValue"); 134 } else { 135 136 String resourceName = org.netbeans.modules.i18n.Util. 137 getResourceName(formI18nInteger.getSupport().getSourceDataObject().getPrimaryFile(), 138 dataObject.getPrimaryFile(), 139 '/', false); 141 return MessageFormat.format( 142 bundle.getString("TXT_Key"), 143 new Object [] { 144 formI18nInteger.getKey(), 145 resourceName, } 147 ); 148 } 149 } 150 151 159 public String getJavaInitializationString() { 160 return ((FormI18nInteger)getValue()).getReplaceString(); 161 } 162 163 165 public Component getCustomEditor() { 166 return new CustomEditor(new FormI18nInteger((FormI18nInteger)getValue()), getProject(), sourceDataObject.getPrimaryFile()); 167 } 168 169 private Project getProject() { 170 return org.netbeans.modules.i18n.Util.getProjectFor(sourceDataObject); 171 } 172 173 174 176 public boolean supportsCustomEditor() { 177 return true; 178 } 179 180 182 public Object getValue() { 183 if(formI18nInteger == null) { 184 formI18nInteger = createFormI18nInteger(); 185 186 if(I18nUtil.getOptions().getLastResource2() != null) 187 formI18nInteger.getSupport().getResourceHolder().setResource(I18nUtil.getOptions().getLastResource2()); 188 } 189 190 return formI18nInteger; 191 } 192 193 195 public void setValue(Object object) { 196 if(object instanceof FormI18nInteger) 197 formI18nInteger = (FormI18nInteger)object; 198 else { 199 formI18nInteger = createFormI18nInteger(); 200 201 if(I18nUtil.getOptions().getLastResource2() != null) 202 formI18nInteger.getSupport().getResourceHolder().setResource(I18nUtil.getOptions().getLastResource2()); 203 } 204 } 205 206 207 private FormI18nInteger createFormI18nInteger() { 208 return new FormI18nInteger(new FormI18nSupport.Factory().createI18nSupport(sourceDataObject)); 210 } 211 212 218 public void setFormModel(FormModel model) { 219 sourceDataObject = FormEditorSupport.getFormDataObject(model); 220 } 221 222 226 public String getDisplayName () { 227 return NbBundle.getMessage(FormI18nIntegerEditor.class, "PROP_IntegerEditor_name"); } 229 230 231 240 public void readFromXML(Node domNode) throws IOException { 241 if(!XML_RESOURCESTRING.equals (domNode.getNodeName ())) { 242 throw new IOException (); 243 } 244 245 FormI18nInteger formI18nInteger = createFormI18nInteger(); 246 247 NamedNodeMap namedNodes = domNode.getAttributes (); 248 249 try { 250 Node node; 251 node = namedNodes.getNamedItem(ATTR_BUNDLE); 253 String bundleName = (node == null) ? null : node.getNodeValue(); 254 255 node = namedNodes.getNamedItem(ATTR_KEY); 257 String key = (node == null) ? null : node.getNodeValue(); 258 259 if(bundleName != null) { 261 262 FileObject sourceFo = sourceDataObject.getPrimaryFile(); 263 if ( sourceFo != null ) { 264 FileObject fileObject = org.netbeans.modules.i18n.Util. 265 getResource(sourceFo, bundleName); 266 267 if(fileObject != null) { 268 try { 269 DataObject dataObject = DataObject.find(fileObject); 270 if(dataObject.getClass().equals(formI18nInteger.getSupport().getResourceHolder().getResourceClasses()[0])) formI18nInteger.getSupport().getResourceHolder().setResource(dataObject); 272 } 273 catch (IOException e) { 274 } 275 } 276 } 277 } 278 279 if(key != null && key.length() > 0) { 281 formI18nInteger.setKey(key); 282 283 formI18nInteger.setValue(formI18nInteger.getSupport().getResourceHolder().getValueForKey(key)); 285 formI18nInteger.setComment(formI18nInteger.getSupport().getResourceHolder().getCommentForKey(key)); 286 } 287 288 ((JavaI18nSupport)formI18nInteger.getSupport()).createIdentifier(); 290 291 node = namedNodes.getNamedItem(ATTR_IDENTIFIER); 292 if(node != null) { 293 String identifier = (node == null) ? null : node.getNodeValue(); 294 295 if(identifier != null) 296 ((JavaI18nSupport)formI18nInteger.getSupport()).setIdentifier(identifier); 297 } 298 299 node = namedNodes.getNamedItem(ATTR_REPLACE_FORMAT); 301 if(node != null) { 302 String replaceFormat = node.getNodeValue(); 303 304 if(replaceFormat != null && replaceFormat.length() > 0) { 305 306 Map map = new HashMap (6); 313 map.put("0", "{identifier}"); map.put("1", "{key}"); map.put("2", "{bundleNameSlashes}"); map.put("3", "{bundleNameDots}"); map.put("4", "{sourceFileName}"); map.put("fileName", "{sourceFileName}"); 320 String newReplaceFormat = MapFormat.format(replaceFormat, map); 321 322 formI18nInteger.setReplaceFormat(newReplaceFormat); 323 } 324 } else { 325 formI18nInteger.setReplaceFormat((String )I18nUtil.getDefaultReplaceFormat(false)); 327 } 328 } catch(NullPointerException npe) { 329 throw new IOException (); 330 } 331 332 if(domNode instanceof Element ) { 334 Element domElement = (Element )domNode; 335 NodeList argNodeList = domElement.getElementsByTagName(XML_ARGUMENT); 336 337 int highest = -1; 339 for(int i = 0; i < argNodeList.getLength(); i++) { 340 NamedNodeMap attributes = argNodeList.item(i).getAttributes(); 341 342 Node indexNode = attributes.getNamedItem (ATTR_INDEX); 343 String indexString = (indexNode == null) ? null : indexNode.getNodeValue (); 344 345 if(indexString != null) { 346 try { 347 int index = Integer.parseInt(indexString); 348 if (index > highest && index < MAX_INDEX) 349 highest = index; 350 } catch (Exception e) {} 351 } 352 } 353 354 String [] parameters = new String [highest + 1]; 356 357 for (int i = 0; i < argNodeList.getLength(); i++) { 359 NamedNodeMap attr = argNodeList.item(i).getAttributes (); 360 361 Node indexNode = attr.getNamedItem (ATTR_INDEX); 362 String indexString = (indexNode == null) ? null : indexNode.getNodeValue (); 363 if (indexString != null) { 364 try { 365 int index = Integer.parseInt(indexString); 366 if (index < MAX_INDEX) { 367 Node javaCodeNode = attr.getNamedItem (ATTR_JAVACODE); 368 String javaCode = (javaCodeNode == null) ? null : javaCodeNode.getNodeValue (); 369 parameters[index] = javaCode; 370 } 371 } catch (Exception e) {} 372 } 373 } 374 375 for (int i = 0; i < parameters.length; i++) 377 if (parameters[i] == null) 378 parameters[i] = ""; 380 formI18nInteger.setArguments(parameters); 382 } 383 384 setValue(formI18nInteger); 386 } 387 388 395 public Node storeToXML(Document doc) { 396 Element element = doc.createElement (XML_RESOURCESTRING); 397 398 String bundleName; 399 if (formI18nInteger.getSupport().getResourceHolder().getResource() == null) { 400 bundleName = ""; 401 } else { 402 bundleName = org.netbeans.modules.i18n.Util. 403 getResourceName(formI18nInteger.getSupport().getSourceDataObject().getPrimaryFile(), 404 formI18nInteger.getSupport().getResourceHolder().getResource().getPrimaryFile(),'/', true); 405 if (bundleName == null) bundleName = ""; 406 } 407 408 409 element.setAttribute(ATTR_BUNDLE, bundleName); 411 element.setAttribute(ATTR_KEY, (formI18nInteger.getKey() == null) ? "" : formI18nInteger.getKey()); JavaI18nSupport support = (JavaI18nSupport)formI18nInteger.getSupport(); 414 if(support.getIdentifier() == null) 415 support.createIdentifier(); 416 Map map = new HashMap (1); 417 map.put("identifier", support.getIdentifier()); element.setAttribute(ATTR_REPLACE_FORMAT, formI18nInteger.getReplaceFormat() == null ? "" : MapFormat.format(formI18nInteger.getReplaceFormat(), map) ); 420 String [] arguments = formI18nInteger.getArguments(); 422 for (int i = 0; i < arguments.length; i++) { 423 Element childElement = doc.createElement (XML_ARGUMENT); 424 childElement.setAttribute (ATTR_INDEX, "" + i); childElement.setAttribute (ATTR_JAVACODE, arguments[i]); 426 try { 427 element.appendChild(childElement); 428 } catch (DOMException de) {} 429 } 430 431 return element; 432 } 433 434 435 private static class CustomEditor extends I18nPanel implements EnhancedCustomPropertyEditor { 436 437 private final ResourceBundle bundle; 438 439 440 public CustomEditor(I18nString i18nString, Project project, FileObject file) { 441 super(i18nString.getSupport().getPropertyPanel(), false, project, file); 442 bundle = NbBundle.getBundle(FormI18nIntegerEditor.class); 443 setI18nString(i18nString); 444 445 HelpCtx.setHelpIDString(this, I18nUtil.HELP_ID_FORMED); 446 } 447 448 449 public Object getPropertyValue() throws IllegalStateException { 450 I18nString i18nString = getI18nString(); 451 452 if(i18nString == null 453 || !(i18nString instanceof FormI18nInteger) 454 || i18nString.getSupport().getResourceHolder().getResource() == null 455 || i18nString.getKey() == null) { 456 457 IllegalStateException ise = new IllegalStateException (); 459 String message = bundle.getString("MSG_InvalidValue"); 460 ErrorManager.getDefault().annotate( 461 ise, ErrorManager.WARNING, message, 462 message, null, null); 463 throw ise; 464 } 465 466 i18nString.getSupport().getResourceHolder().addProperty( 468 i18nString.getKey(), 469 i18nString.getValue(), 470 i18nString.getComment(), 471 false ); 473 474 return i18nString; 475 } 476 477 } 478 479 480 } 481 | Popular Tags |