1 19 20 21 package org.netbeans.modules.i18n; 22 23 24 import java.awt.Dialog ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.io.IOException ; 28 import java.util.Arrays ; 29 import javax.swing.DefaultComboBoxModel ; 30 import javax.swing.JPanel ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 33 34 import org.openide.DialogDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileObject; 38 import org.openide.loaders.DataObject; 39 40 41 49 public class PropertyPanel extends JPanel { 50 51 54 public static final String PROP_STRING = "propString"; 56 57 public static final String PROP_RESOURCE = "property_resource"; 59 60 private static final String DUMMY_ACTION = "dont_proceed"; 62 63 protected I18nString i18nString; 64 65 66 private FileObject file; 67 68 73 private int internalTextChange = 0; 74 75 76 77 public PropertyPanel() { 78 initComponents(); 79 myInitComponents(); 80 initAccessibility(); 81 } 82 83 public void setEnabled(boolean ena) { 84 super.setEnabled(ena); 85 commentText.setEnabled(ena); 86 commentLabel.setEnabled(ena); 87 commentScroll.setEnabled(ena); 88 89 keyBundleCombo.setEnabled(ena); 90 keyLabel.setEnabled(ena); 91 92 replaceFormatButton.setEnabled(ena); 93 replaceFormatLabel.setEnabled(ena); 94 replaceFormatTextField.setEnabled(ena); 95 96 valueLabel.setEnabled(ena); 97 valueText.setEnabled(ena); 98 valueScroll.setEnabled(ena); 99 } 100 101 102 public void setI18nString(I18nString i18nString) { 103 this.i18nString = i18nString; 104 105 updateAllValues(); 106 firePropertyChange(PROP_STRING, null,null); 107 } 108 109 110 public void setFile(FileObject fo) { 111 this.file = fo; 112 } 113 114 public FileObject getFile() { 115 return file; 116 } 117 118 119 void updateAllValues() { 120 resourceText.setText(getResourceName(i18nString.getSupport().getResourceHolder().getResource())); 121 updateBundleKeys(); 122 updateKey(); 123 updateValue(); 124 updateComment(); 125 } 126 127 129 private void updateKey() { 130 String key = i18nString.getKey(); 131 132 if(key == null || !key.equals(keyBundleCombo.getSelectedItem()) ) { 133 String oldActionCommand = keyBundleCombo.getActionCommand(); 135 keyBundleCombo.setActionCommand(DUMMY_ACTION); 136 137 internalTextChange++; 138 keyBundleCombo.setSelectedItem(key == null ? "" : key); internalTextChange--; 140 141 keyBundleCombo.setActionCommand(oldActionCommand); 142 } 143 144 updateReplaceText(); 145 } 146 147 149 private void updateValue() { 150 String value = i18nString.getValue(); 151 152 if(!valueText.getText().equals(value)) { 153 valueText.setText(value == null ? "" : value); } 155 156 updateReplaceText(); 157 } 158 159 160 private void updateComment() { 161 String comment = i18nString.getComment(); 162 163 if(!commentText.getText().equals(comment)) { 164 commentText.setText(comment == null ? "" : comment); } 166 } 167 168 169 protected void updateReplaceText() { 170 replaceFormatTextField.setText(i18nString.getReplaceString()); 171 } 172 173 174 void updateBundleKeys() { 175 String oldActionCommand = keyBundleCombo.getActionCommand(); 177 keyBundleCombo.setActionCommand(DUMMY_ACTION); 178 179 internalTextChange++; 180 keyBundleCombo.setModel(new DefaultComboBoxModel (i18nString.getSupport().getResourceHolder().getAllKeys())); 181 internalTextChange--; 182 183 keyBundleCombo.setActionCommand(oldActionCommand); 184 185 updateKey(); 186 } 187 188 189 private void changeResource(DataObject resource) { 190 if(resource == null) 191 throw new IllegalArgumentException (); 192 193 DataObject oldValue = i18nString.getSupport().getResourceHolder().getResource(); 194 195 if(oldValue != null && oldValue.equals(resource)) 196 return; 197 198 i18nString.getSupport().getResourceHolder().setResource(resource); 199 String newResourceValue = i18nString.getSupport().getResourceHolder() 200 .getValueForKey(i18nString.getKey()); 201 if (newResourceValue != null) { 202 i18nString.setValue(newResourceValue); 203 } 204 updateAllValues(); 205 206 firePropertyChange(PROP_RESOURCE, oldValue, resource); 207 208 I18nUtil.getOptions().setLastResource2(resource); 209 } 210 211 public void setResource(DataObject resource) { 212 if (isResourceClass(resource.getClass())) { 213 changeResource(resource); 214 } 215 } 216 217 private boolean isResourceClass(Class clazz) { 218 return Arrays.asList(i18nString.getSupport().getResourceHolder().getResourceClasses()).contains(clazz); 219 } 220 221 private String getResourceName(DataObject resource) { 222 if(resource == null) { 223 return ""; } 225 else { 226 String name = Util.getResourceName(file, resource.getPrimaryFile(), '.', false ); 227 return name != null ? name : ""; } 229 } 230 231 private void initAccessibility() { 232 this.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_PropertyPanel")); valueText.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_valueText")); commentText.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_commentText")); replaceFormatButton.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_CTL_Format")); replaceFormatTextField.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_replaceFormatTextField")); browseButton.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_CTL_BrowseButton")); resourceText.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_ResourceText")); } 240 241 private void myInitComponents() { 242 argumentsButton.setVisible(false); 243 ((javax.swing.JTextField )keyBundleCombo.getEditor().getEditorComponent()). 245 getDocument().addDocumentListener(new DocumentListener () { 246 public void changedUpdate(DocumentEvent e) { keyBundleTextChanged();} 247 public void insertUpdate(DocumentEvent e) {keyBundleTextChanged();} 248 public void removeUpdate(DocumentEvent e) {keyBundleTextChanged();} 249 } 250 ); 251 valueText.getDocument().addDocumentListener(new DocumentListener () { 252 public void changedUpdate(DocumentEvent e) { valueTextChanged();} 253 public void insertUpdate(DocumentEvent e) {valueTextChanged();} 254 public void removeUpdate(DocumentEvent e) {valueTextChanged();} 255 } 256 ); 257 258 } 259 260 private void keyBundleTextChanged() { 261 if (internalTextChange==0) { 262 String key = ((javax.swing.JTextField )keyBundleCombo.getEditor().getEditorComponent()).getText(); 263 264 if (!key.equals(i18nString.getKey())) { 265 i18nString.setKey(key); 266 firePropertyChange(PROP_STRING,null,null); 267 } 268 } 269 } 270 271 private void valueTextChanged() { 272 i18nString.setValue(valueText.getText()); 273 firePropertyChange(PROP_STRING,null,null); 275 } 276 277 278 283 private void initComponents() { 285 286 commentLabel = new javax.swing.JLabel (); 287 commentScroll = new javax.swing.JScrollPane (); 288 commentText = new javax.swing.JTextArea (); 289 keyLabel = new javax.swing.JLabel (); 290 valueLabel = new javax.swing.JLabel (); 291 valueScroll = new javax.swing.JScrollPane (); 292 valueText = new javax.swing.JTextArea (); 293 keyBundleCombo = new javax.swing.JComboBox (); 294 replaceFormatTextField = new javax.swing.JTextField (); 295 replaceFormatLabel = new javax.swing.JLabel (); 296 replaceFormatButton = new javax.swing.JButton (); 297 jLabel1 = new javax.swing.JLabel (); 298 resourceText = new javax.swing.JTextField (); 299 argumentsButton = new javax.swing.JButton (); 300 browseButton = new javax.swing.JButton (); 301 302 commentLabel.setLabelFor(commentText); 303 org.openide.awt.Mnemonics.setLocalizedText(commentLabel, I18nUtil.getBundle().getString("LBL_Comment")); 305 commentText.setColumns(40); 306 commentText.setRows(2); 307 commentText.addFocusListener(new java.awt.event.FocusAdapter () { 308 public void focusLost(java.awt.event.FocusEvent evt) { 309 commentTextFocusLost(evt); 310 } 311 }); 312 commentScroll.setViewportView(commentText); 313 314 keyLabel.setLabelFor(keyBundleCombo); 315 org.openide.awt.Mnemonics.setLocalizedText(keyLabel, I18nUtil.getBundle().getString("LBL_Key")); 317 valueLabel.setLabelFor(valueText); 318 org.openide.awt.Mnemonics.setLocalizedText(valueLabel, I18nUtil.getBundle().getString("LBL_Value")); 320 valueText.setColumns(40); 321 valueText.setRows(2); 322 valueText.addFocusListener(new java.awt.event.FocusAdapter () { 323 public void focusLost(java.awt.event.FocusEvent evt) { 324 valueTextFocusLost(evt); 325 } 326 }); 327 valueScroll.setViewportView(valueText); 328 329 keyBundleCombo.setEditable(true); 330 keyBundleCombo.addActionListener(new java.awt.event.ActionListener () { 331 public void actionPerformed(java.awt.event.ActionEvent evt) { 332 keyBundleComboActionPerformed(evt); 333 } 334 }); 335 336 replaceFormatTextField.setColumns(40); 337 replaceFormatTextField.setEditable(false); 338 replaceFormatTextField.selectAll(); 339 replaceFormatTextField.addFocusListener(new java.awt.event.FocusAdapter () { 340 public void focusGained(java.awt.event.FocusEvent evt) { 341 replaceFormatTextFieldFocusGained(evt); 342 } 343 }); 344 345 replaceFormatLabel.setLabelFor(replaceFormatTextField); 346 org.openide.awt.Mnemonics.setLocalizedText(replaceFormatLabel, I18nUtil.getBundle().getString("LBL_ReplaceFormat")); 348 org.openide.awt.Mnemonics.setLocalizedText(replaceFormatButton, I18nUtil.getBundle().getString("CTL_Format")); replaceFormatButton.addActionListener(new java.awt.event.ActionListener () { 350 public void actionPerformed(java.awt.event.ActionEvent evt) { 351 replaceFormatButtonActionPerformed(evt); 352 } 353 }); 354 355 jLabel1.setLabelFor(resourceText); 356 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, I18nUtil.getBundle().getString("LBL_BundleName")); 358 org.openide.awt.Mnemonics.setLocalizedText(argumentsButton, I18nUtil.getBundle().getString("CTL_Arguments")); 360 org.openide.awt.Mnemonics.setLocalizedText(browseButton, I18nUtil.getBundle().getString("CTL_BrowseButton")); browseButton.addActionListener(new java.awt.event.ActionListener () { 362 public void actionPerformed(java.awt.event.ActionEvent evt) { 363 browseButtonActionPerformed(evt); 364 } 365 }); 366 367 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 368 this.setLayout(layout); 369 layout.setHorizontalGroup( 370 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 371 .add(layout.createSequentialGroup() 372 .addContainerGap() 373 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 374 .add(layout.createSequentialGroup() 375 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 376 .add(jLabel1) 377 .add(valueLabel) 378 .add(commentLabel) 379 .add(keyLabel) 380 .add(replaceFormatLabel)) 381 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 382 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 383 .add(layout.createSequentialGroup() 384 .add(resourceText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE) 385 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 386 .add(browseButton)) 387 .add(keyBundleCombo, 0, 414, Short.MAX_VALUE) 388 .add(valueScroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE) 389 .add(commentScroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE) 390 .add(replaceFormatTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))) 391 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 392 .add(argumentsButton) 393 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 394 .add(replaceFormatButton))) 395 .addContainerGap()) 396 ); 397 layout.setVerticalGroup( 398 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 399 .add(layout.createSequentialGroup() 400 .addContainerGap() 401 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 402 .add(jLabel1) 403 .add(resourceText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 404 .add(browseButton)) 405 .add(12, 12, 12) 406 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 407 .add(keyLabel) 408 .add(keyBundleCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 409 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 410 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 411 .add(valueLabel) 412 .add(valueScroll)) 413 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 414 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 415 .add(commentLabel) 416 .add(commentScroll)) 417 .add(12, 12, 12) 418 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 419 .add(replaceFormatTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 420 .add(replaceFormatLabel)) 421 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 422 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 423 .add(replaceFormatButton) 424 .add(argumentsButton)) 425 .addContainerGap()) 426 ); 427 } 429 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { ResourceHolder rh = i18nString.getSupport().getResourceHolder(); 431 DataObject template; 432 try { 433 template = rh.getTemplate(rh.getResourceClasses()[0]); 434 } 435 catch (IOException ex) { 436 ErrorManager.getDefault().notify(ex); 437 return; 438 } 439 DataObject resource = SelectorUtils.selectOrCreateBundle(file, template); 440 if (resource != null) { 442 changeResource(resource); 443 } 444 445 } 447 private void replaceFormatTextFieldFocusGained(java.awt.event.FocusEvent evt) { replaceFormatTextField.selectAll(); 450 } 452 private void replaceFormatButtonActionPerformed(java.awt.event.ActionEvent evt) { final Dialog [] dialogs = new Dialog [1]; 454 final HelpStringCustomEditor customPanel = new HelpStringCustomEditor( 455 i18nString.getReplaceFormat(), 456 I18nUtil.getReplaceFormatItems(), 457 I18nUtil.getReplaceHelpItems(), 458 I18nUtil.getBundle().getString("LBL_ReplaceCodeFormat"), 459 I18nUtil.PE_REPLACE_CODE_HELP_ID); 460 461 DialogDescriptor dd = new DialogDescriptor( 462 customPanel, 463 I18nUtil.getBundle().getString("LBL_ReplaceStringFormatEditor"), 464 true, 465 DialogDescriptor.OK_CANCEL_OPTION, 466 DialogDescriptor.OK_OPTION, 467 new ActionListener () { 468 public void actionPerformed(ActionEvent ev) { 469 if (ev.getSource() == DialogDescriptor.OK_OPTION) { 470 String newText = (String )customPanel.getPropertyValue(); 471 472 if(!newText.equals(replaceFormatTextField.getText())) { 473 i18nString.setReplaceFormat(newText); 474 updateReplaceText(); 475 firePropertyChange(PROP_STRING,null,null); 476 477 I18nUtil.getOptions().setReplaceJavaCode(newText); 479 } 480 481 dialogs[0].setVisible(false); 482 dialogs[0].dispose(); 483 } else if (ev.getSource() == DialogDescriptor.CANCEL_OPTION) { 484 dialogs[0].setVisible(false); 485 dialogs[0].dispose(); 486 } 487 } 488 }); 489 dialogs[0] = DialogDisplayer.getDefault().createDialog(dd); 490 dialogs[0].setVisible(true); 491 } 493 private void keyBundleComboActionPerformed(java.awt.event.ActionEvent evt) { if(DUMMY_ACTION.equals(evt.getActionCommand())) 495 return; 496 497 String key = (String )keyBundleCombo.getSelectedItem(); 498 i18nString.setKey(key); 499 updateKey(); 500 501 String value = i18nString.getSupport().getResourceHolder().getValueForKey(key); 502 if(value != null) { 503 i18nString.setValue(value); 504 updateValue(); 505 } 506 507 String comment = i18nString.getSupport().getResourceHolder().getCommentForKey(key); 508 if(comment != null) { 509 i18nString.setComment(comment); 510 updateComment(); 511 } 512 firePropertyChange(PROP_STRING,null,null); 513 } 515 private void commentTextFocusLost(java.awt.event.FocusEvent evt) { i18nString.setComment(commentText.getText()); 517 updateComment(); 518 firePropertyChange(PROP_STRING,null,null); 519 } 521 private void valueTextFocusLost(java.awt.event.FocusEvent evt) { valueTextChanged(); 523 } 525 protected javax.swing.JButton argumentsButton; 527 private javax.swing.JButton browseButton; 528 private javax.swing.JLabel commentLabel; 529 private javax.swing.JScrollPane commentScroll; 530 private javax.swing.JTextArea commentText; 531 private javax.swing.JLabel jLabel1; 532 private javax.swing.JComboBox keyBundleCombo; 533 private javax.swing.JLabel keyLabel; 534 private javax.swing.JButton replaceFormatButton; 535 private javax.swing.JLabel replaceFormatLabel; 536 private javax.swing.JTextField replaceFormatTextField; 537 private javax.swing.JTextField resourceText; 538 private javax.swing.JLabel valueLabel; 539 private javax.swing.JScrollPane valueScroll; 540 private javax.swing.JTextArea valueText; 541 543 } 544 | Popular Tags |