1 19 20 21 package org.netbeans.modules.i18n.form; 22 23 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.beans.PropertyEditorSupport ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.ResourceBundle ; 31 import javax.swing.*; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 import org.jdesktop.layout.GroupLayout; 35 import org.netbeans.api.java.classpath.ClassPath; 36 37 import org.netbeans.modules.form.FormModel; 38 import org.netbeans.modules.form.FormAwareEditor; 39 import org.netbeans.modules.form.FormDataObject; 40 import org.netbeans.modules.form.I18nValue; 41 import org.netbeans.modules.form.NamedPropertyEditor; 42 import org.netbeans.modules.form.FormEditorSupport; 43 import org.netbeans.modules.i18n.I18nPanel; 44 import org.netbeans.modules.i18n.I18nString; 45 import org.netbeans.modules.i18n.I18nUtil; 46 import org.netbeans.modules.i18n.java.JavaI18nSupport; 47 48 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 49 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 50 import org.openide.filesystems.FileObject; 51 import org.openide.loaders.DataObject; 52 import org.openide.ErrorManager; 53 import org.openide.util.HelpCtx; 54 import org.openide.util.MapFormat; 55 import org.openide.util.NbBundle; 56 import org.openide.awt.Mnemonics; 57 import org.netbeans.api.project.Project; 58 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.DOMException ; 62 import org.w3c.dom.NamedNodeMap ; 63 import org.w3c.dom.Node ; 64 import org.w3c.dom.NodeList ; 65 66 67 80 public class FormI18nStringEditor extends PropertyEditorSupport implements FormAwareEditor, NamedPropertyEditor, XMLPropertyEditor { 81 82 83 85 88 private FormDataObject sourceDataObject; 89 90 91 public static final String XML_RESOURCESTRING = "ResourceString"; 93 private static final String XML_PLAINSTRING = "PlainString"; 95 public static final String XML_ARGUMENT = "Argument"; 97 public static final String ATTR_BUNDLE = "bundle"; 99 public static final String ATTR_KEY = "key"; 101 private static final String ATTR_VALUE = "value"; 103 public static final String ATTR_IDENTIFIER = "identifier"; 105 public static final String ATTR_REPLACE_FORMAT = "replaceFormat"; 107 public static final String ATTR_INDEX = "index"; 109 public static final String ATTR_JAVACODE = "javacode"; 111 112 private static final int MAX_INDEX = 1000; 113 114 private final ResourceBundle bundle; 115 116 117 public FormI18nStringEditor() { 118 bundle = NbBundle.getBundle(FormI18nStringEditor.class); 119 } 120 121 122 124 public String [] getTags() { 125 return null; 126 } 127 128 130 public void setAsText(String text) { 131 setValue(text); 132 } 133 134 135 137 public String getAsText() { 138 Object value = getValue(); 139 if (value instanceof String ) 140 return (String ) value; 141 142 FormI18nString i18nString = (FormI18nString) value; 143 return i18nString.getValue(); 144 } 161 162 170 public String getJavaInitializationString() { 171 FormI18nString i18nString = (FormI18nString) getValue(); 172 String javaString; 173 if (i18nString.getKey() != I18nValue.NOI18N_KEY) { 174 javaString = i18nString.getReplaceString(); 175 if (javaString == null) { return "\"" + FormI18nSupport.toAscii(i18nString.getValue()) + "\""; } 178 } 179 else { javaString = "\"" + FormI18nSupport.toAscii(i18nString.getValue()) + "\""; } 182 183 return "*/\n\\1NOI18N*/\n\\0" + javaString; } 187 188 190 public Component getCustomEditor() { 191 FormI18nString formI18nString; 192 Object value = getValue(); 193 boolean noI18n; 194 if (value instanceof FormI18nString) { 195 formI18nString = new FormI18nString((FormI18nString)value); 196 if (formI18nString.getKey() == I18nValue.NOI18N_KEY) { 197 formI18nString.setKey(null); 198 noI18n = true; 199 } 200 else noI18n = false; 201 } 202 else { 203 formI18nString = createFormI18nString(); 204 if (value instanceof String ) 205 formI18nString.setValue((String )value); 206 DataObject lastResource = I18nUtil.getOptions().getLastResource2(); 207 if (lastResource != null) { 208 FileObject sourceFile = sourceDataObject.getPrimaryFile(); 209 FileObject bundleFile = lastResource.getPrimaryFile(); 210 ClassPath sourceClassPath = ClassPath.getClassPath( 211 sourceFile, ClassPath.SOURCE); 212 if (sourceClassPath.contains(bundleFile)) { 213 formI18nString.getSupport().getResourceHolder().setResource( 214 lastResource); 215 } 216 } 217 noI18n = false; 218 } 219 return new CustomEditor(formI18nString, getProject(), sourceDataObject.getPrimaryFile(), noI18n); 220 } 222 223 private Project getProject() { 224 return org.netbeans.modules.i18n.Util.getProjectFor(sourceDataObject); 225 } 226 227 228 230 public boolean supportsCustomEditor() { 231 return true; 232 } 233 234 236 247 249 public void setValue(Object object) { 250 if (object instanceof String && getValue() instanceof FormI18nString) { 251 FormI18nString i18nString = new FormI18nString((FormI18nString)getValue()); 252 i18nString.setValue((String )object); 253 object = i18nString; 254 } 255 super.setValue(object); 256 } 267 268 269 private FormI18nString createFormI18nString() { 270 return new FormI18nString(sourceDataObject); 272 } 273 274 280 public void setFormModel(FormModel model) { 281 sourceDataObject = FormEditorSupport.getFormDataObject(model); 282 } 283 284 288 public String getDisplayName () { 289 return NbBundle.getMessage(FormI18nStringEditor.class, "CTL_PropertyEditorName"); } 291 292 293 302 public void readFromXML(Node domNode) throws IOException { 303 FormI18nString formI18nString = createFormI18nString(); 304 NamedNodeMap namedNodes = domNode.getAttributes (); 305 306 if (XML_PLAINSTRING.equals(domNode.getNodeName())) { 307 Node node = namedNodes.getNamedItem(ATTR_VALUE); 309 if (node != null) { 310 formI18nString.setKey(I18nValue.NOI18N_KEY); 311 formI18nString.setValue(node.getNodeValue()); 312 setValue(formI18nString); 313 } 314 return; 315 } 316 317 if(!XML_RESOURCESTRING.equals (domNode.getNodeName ())) { 318 throw new IOException (); 319 } 320 321 try { 322 Node node; 323 node = namedNodes.getNamedItem(ATTR_BUNDLE); 325 String bundleName = (node == null) ? null : node.getNodeValue(); 326 327 node = namedNodes.getNamedItem(ATTR_KEY); 329 String key = (node == null) ? null : node.getNodeValue(); 330 331 if(bundleName != null) { 333 DataObject resourceDO = null; 334 FileObject sourceFo = sourceDataObject.getPrimaryFile(); 335 if ( sourceFo != null ) { 336 FileObject fileObject = org.netbeans.modules.i18n.Util. 337 getResource(sourceFo, bundleName); 338 339 if(fileObject != null) { 340 try { 341 resourceDO = DataObject.find(fileObject); 342 if( resourceDO.getClass().equals(formI18nString.getSupport().getResourceHolder().getResourceClasses()[0])) formI18nString.getSupport().getResourceHolder().setResource(resourceDO); 344 } 345 catch (IOException e) { 346 } 347 } 348 } 349 if (resourceDO == null) 350 formI18nString.bundleName = bundleName; 351 } 352 353 if(key != null && key.length() > 0) { 355 formI18nString.setKey(key); 356 357 formI18nString.setValue(formI18nString.getSupport().getResourceHolder().getValueForKey(key)); 359 formI18nString.setComment(formI18nString.getSupport().getResourceHolder().getCommentForKey(key)); 360 } 361 362 ((JavaI18nSupport)formI18nString.getSupport()).createIdentifier(); 364 365 node = namedNodes.getNamedItem(ATTR_IDENTIFIER); 366 if(node != null) { 367 String identifier = (node == null) ? null : node.getNodeValue(); 368 369 if(identifier != null) 370 ((JavaI18nSupport)formI18nString.getSupport()).setIdentifier(identifier); 371 } 372 373 node = namedNodes.getNamedItem(ATTR_REPLACE_FORMAT); 375 if(node != null) { 376 String replaceFormat = node.getNodeValue(); 377 378 if(replaceFormat != null && replaceFormat.length() > 0) { 379 380 Map map = new HashMap (6); 387 map.put("0", "{identifier}"); map.put("1", "{key}"); map.put("2", "{bundleNameSlashes}"); map.put("3", "{bundleNameDots}"); map.put("4", "{sourceFileName}"); map.put("fileName", "{sourceFileName}"); 394 String newReplaceFormat = MapFormat.format(replaceFormat, map); 395 396 formI18nString.setReplaceFormat(newReplaceFormat); 397 } 398 } else { 399 formI18nString.setReplaceFormat((String )I18nUtil.getDefaultReplaceFormat(false)); 401 } 402 } catch(NullPointerException npe) { 403 throw new IOException (); 404 } 405 406 if(domNode instanceof Element ) { 408 Element domElement = (Element )domNode; 409 NodeList argNodeList = domElement.getElementsByTagName(XML_ARGUMENT); 410 411 int highest = -1; 413 for(int i = 0; i < argNodeList.getLength(); i++) { 414 NamedNodeMap attributes = argNodeList.item(i).getAttributes(); 415 416 Node indexNode = attributes.getNamedItem (ATTR_INDEX); 417 String indexString = (indexNode == null) ? null : indexNode.getNodeValue (); 418 419 if(indexString != null) { 420 try { 421 int index = Integer.parseInt(indexString); 422 if (index > highest && index < MAX_INDEX) 423 highest = index; 424 } catch (Exception e) {} 425 } 426 } 427 428 String [] parameters = new String [highest + 1]; 430 431 for (int i = 0; i < argNodeList.getLength(); i++) { 433 NamedNodeMap attr = argNodeList.item(i).getAttributes (); 434 435 Node indexNode = attr.getNamedItem (ATTR_INDEX); 436 String indexString = (indexNode == null) ? null : indexNode.getNodeValue (); 437 if (indexString != null) { 438 try { 439 int index = Integer.parseInt(indexString); 440 if (index < MAX_INDEX) { 441 Node javaCodeNode = attr.getNamedItem (ATTR_JAVACODE); 442 String javaCode = (javaCodeNode == null) ? null : javaCodeNode.getNodeValue (); 443 parameters[index] = javaCode; 444 } 445 } catch (Exception e) {} 446 } 447 } 448 449 for (int i = 0; i < parameters.length; i++) 451 if (parameters[i] == null) 452 parameters[i] = ""; 454 formI18nString.setArguments(parameters); 456 } 457 458 setValue(formI18nString); 460 } 461 462 469 public Node storeToXML(Document doc) { 470 FormI18nString formI18nString = (FormI18nString) getValue(); 471 Element element; 472 if (formI18nString.getKey() != I18nValue.NOI18N_KEY) { 473 element = doc.createElement (XML_RESOURCESTRING); 474 475 String bundleName; 476 if (formI18nString.getSupport().getResourceHolder().getResource() == null) { 477 bundleName = formI18nString.bundleName; 478 } else { 479 bundleName = org.netbeans.modules.i18n.Util. 480 getResourceName(formI18nString.getSupport().getSourceDataObject().getPrimaryFile(), 481 formI18nString.getSupport().getResourceHolder().getResource().getPrimaryFile(),'/', true); 482 if (bundleName == null) bundleName = ""; } 484 485 element.setAttribute(ATTR_BUNDLE, bundleName); 487 element.setAttribute(ATTR_KEY, (formI18nString.getKey() == null) ? "" : formI18nString.getKey()); JavaI18nSupport support = (JavaI18nSupport)formI18nString.getSupport(); 490 if(support.getIdentifier() == null) 491 support.createIdentifier(); 492 Map map = new HashMap (1); 493 map.put("identifier", support.getIdentifier()); element.setAttribute(ATTR_REPLACE_FORMAT, formI18nString.getReplaceFormat() == null ? "" : MapFormat.format(formI18nString.getReplaceFormat(), map) ); 496 String [] arguments = formI18nString.getArguments(); 498 for (int i = 0; i < arguments.length; i++) { 499 Element childElement = doc.createElement (XML_ARGUMENT); 500 childElement.setAttribute (ATTR_INDEX, "" + i); childElement.setAttribute (ATTR_JAVACODE, arguments[i]); 502 try { 503 element.appendChild(childElement); 504 } catch (DOMException de) {} 505 } 506 } 507 else { element = doc.createElement (XML_PLAINSTRING); 509 element.setAttribute(ATTR_VALUE, formI18nString.getValue()); 510 } 511 512 return element; 513 } 514 515 516 private static class CustomEditor extends JPanel implements EnhancedCustomPropertyEditor, ChangeListener { 517 private I18nPanel i18nPanel; 518 private StringPanel stringPanel; 519 private JCheckBox noI18nCheckBox; 520 521 522 public CustomEditor(I18nString i18nString, Project project, FileObject file, boolean noI18n) { 523 i18nPanel = new I18nPanel(i18nString.getSupport().getPropertyPanel(), false, project, file); 524 noI18nCheckBox = new JCheckBox(); 525 Mnemonics.setLocalizedText(noI18nCheckBox, 526 NbBundle.getMessage(FormI18nStringEditor.class, "CTL_NOI18NCheckBox")); 528 GroupLayout layout = new GroupLayout(this); 529 setLayout(layout); 530 layout.setHorizontalGroup(layout.createParallelGroup() 531 .add(i18nPanel) 532 .add(layout.createSequentialGroup() 533 .addContainerGap() 534 .add(noI18nCheckBox) 535 .addContainerGap())); 536 layout.setVerticalGroup(layout.createSequentialGroup() 537 .add(i18nPanel) 538 .add(noI18nCheckBox)); 539 540 i18nPanel.setI18nString(i18nString); 541 setNoI18n(noI18n); 542 noI18nCheckBox.setSelected(noI18n); 543 noI18nCheckBox.addChangeListener(this); 544 545 HelpCtx.setHelpIDString(this, I18nUtil.HELP_ID_FORMED); 546 } 547 548 549 public Object getPropertyValue() throws IllegalStateException { 550 I18nString i18nString = i18nPanel.getI18nString(); 551 552 if (isNoI18n() && i18nString != null) { 553 i18nString.setKey(I18nValue.NOI18N_KEY); 554 i18nString.setValue(stringPanel.getString()); 555 return i18nString; 556 } 557 558 if(i18nString == null 559 || !(i18nString instanceof FormI18nString) 560 || i18nString.getSupport().getResourceHolder().getResource() == null 561 || i18nString.getKey() == null) { 562 563 IllegalStateException ise = new IllegalStateException (); 565 String message = NbBundle.getMessage(FormI18nStringEditor.class, "MSG_InvalidValue"); ErrorManager.getDefault().annotate( 567 ise, ErrorManager.WARNING, message, 568 message, null, null); 569 throw ise; 570 } 571 572 return i18nString; 573 } 574 575 public void stateChanged(ChangeEvent e) { 576 setNoI18n(noI18nCheckBox.isSelected()); 577 } 578 579 private boolean isNoI18n() { 580 return noI18nCheckBox.isSelected(); 581 } 582 583 private void setNoI18n(boolean noI18n) { 584 if (noI18n) { 585 if (getPlainStringPanel().getParent() == null) { 586 ((GroupLayout)getLayout()).replace(i18nPanel, stringPanel); 587 revalidate(); 588 repaint(); 589 } 590 } 591 else if (i18nPanel.getParent() == null) { 592 ((GroupLayout)getLayout()).replace(stringPanel, i18nPanel); 593 revalidate(); 594 repaint(); 595 } 596 } 597 598 private JComponent getPlainStringPanel() { 599 if (stringPanel == null) { 600 stringPanel = new StringPanel(); 601 stringPanel.setString(i18nPanel.getI18nString().getValue()); 602 } 603 return stringPanel; 604 } 605 } 606 607 611 private static class StringPanel extends JPanel { 612 private JTextArea textArea; 613 614 private StringPanel() { 615 textArea = new JTextArea(); 616 JScrollPane scroll = new JScrollPane(textArea); 617 GroupLayout layout = new GroupLayout(this); 618 layout.setAutocreateContainerGaps(true); 619 setLayout(layout); 620 layout.setHorizontalGroup(layout.createSequentialGroup().add(scroll)); 621 layout.setVerticalGroup(layout.createSequentialGroup().add(scroll)); 622 } 623 624 String getString() { 625 return textArea.getText(); 626 } 627 628 void setString(String str) { 629 textArea.setText(str); 630 } 631 } 632 633 } 634 | Popular Tags |