1 19 20 21 package org.netbeans.modules.i18n.wizard; 22 23 24 import java.awt.BorderLayout ; 25 import java.awt.Component ; 26 import java.awt.Container ; 27 import java.awt.Dialog ; 28 import java.awt.GridBagConstraints ; 29 import java.awt.GridBagLayout ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.Insets ; 34 import java.util.ArrayList ; 35 import java.util.EventObject ; 36 import java.util.HashSet ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import javax.swing.DefaultCellEditor ; 41 import javax.swing.DefaultComboBoxModel ; 42 import javax.swing.JButton ; 43 import javax.swing.JComponent ; 44 import javax.swing.JLabel ; 45 import javax.swing.JPanel ; 46 import javax.swing.JTable ; 47 import javax.swing.JTextField ; 48 import javax.swing.table.AbstractTableModel ; 49 import javax.swing.table.DefaultTableCellRenderer ; 50 import javax.swing.table.TableCellEditor ; 51 import javax.swing.AbstractCellEditor ; 52 import org.netbeans.api.java.classpath.ClassPath; 53 54 import org.netbeans.modules.i18n.HardCodedString; 55 import org.netbeans.modules.i18n.I18nString; 56 import org.netbeans.modules.i18n.I18nSupport; 57 import org.netbeans.modules.i18n.I18nUtil; 58 import org.netbeans.modules.i18n.PropertyPanel; 59 60 import org.openide.DialogDescriptor; 61 import org.openide.loaders.DataObject; 62 import org.openide.util.HelpCtx; 63 import org.openide.util.NbBundle; 64 import org.openide.WizardDescriptor; 65 import org.openide.DialogDisplayer; 66 67 68 75 final class TestStringWizardPanel extends JPanel { 76 77 78 private static final int COLUMN_INDEX_CHECK = 0; 79 80 private static final int COLUMN_INDEX_HARDSTRING = 1; 81 82 private static final int COLUMN_INDEX_KEY = 2; 83 84 private static final int COLUMN_INDEX_VALUE = 3; 85 86 87 private final Map sourceMap = Util.createWizardSourceMap(); 88 89 90 private final AbstractTableModel tableModel = new TestStringTableModel(); 91 92 93 private TestStringWizardPanel() { 94 95 initComponents(); 96 97 postInitComponents(); 98 99 initTable(); 100 101 setComboModel(sourceMap); 102 } 103 104 105 106 private void setComboModel(Map sourceMap) { 107 Object [] sources = sourceMap.keySet().toArray(); 108 109 ArrayList nonEmptySources = new ArrayList (); 110 111 for(int i = 0; i < sources.length; i++) { 112 if(!((SourceData)sourceMap.get(sources[i])).getStringMap().isEmpty()) 113 nonEmptySources.add(sources[i]); 114 } 115 116 sourceCombo.setModel(new DefaultComboBoxModel (nonEmptySources.toArray())); 117 } 118 119 120 private void postInitComponents() { 121 sourceLabel.setLabelFor(sourceCombo); 122 testStringLabel.setLabelFor(testStringTable); 123 } 124 125 126 public Map getSourceMap() { 127 return sourceMap; 128 } 129 130 131 public void setSourceMap(Map sourceMap) { 132 this.sourceMap.clear(); 133 this.sourceMap.putAll(sourceMap); 134 135 setComboModel(sourceMap); 136 } 137 138 139 private Map getStringMap() { 140 SourceData sourceData = (SourceData)sourceMap.get(sourceCombo.getSelectedItem()); 141 return sourceData == null ? null : sourceData.getStringMap(); 142 } 143 144 145 private Set getRemovedStrings() { 146 SourceData sourceData = (SourceData)sourceMap.get(sourceCombo.getSelectedItem()); 147 if(sourceData == null) 148 return null; 149 150 if(sourceData.getRemovedStrings() == null) 151 sourceData.setRemovedStrings(new HashSet ()); 152 153 return sourceData.getRemovedStrings(); 154 } 155 156 157 private void initTable() { 158 testStringTable.setDefaultRenderer(HardCodedString.class, new DefaultTableCellRenderer () { 159 public Component getTableCellRendererComponent(JTable table, Object value, 160 boolean isSelected, boolean hasFocus, int row, int column) { 161 162 JLabel label = (JLabel )super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 163 164 HardCodedString hcString = (HardCodedString)value; 165 166 if(hcString != null) 167 label.setText(hcString.getText()); 168 else 169 label.setText(""); 171 return label; 172 } 173 }); 174 175 testStringTable.setDefaultRenderer(I18nString.class, new DefaultTableCellRenderer () { 176 177 public Component getTableCellRendererComponent(JTable table, Object value, 178 boolean isSelected, boolean hasFocus, int row, int column) { 179 180 I18nString i18nString = (I18nString)value; 181 182 JLabel label = (JLabel )super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 183 184 int modelColumn = testStringTable.convertColumnIndexToModel(column); 185 186 if(i18nString != null) { 187 if(modelColumn == COLUMN_INDEX_KEY) { 188 label.setText(i18nString.getKey()); 189 } else { 190 label.setText(i18nString.getValue()); 191 } 192 193 } else 194 label.setText(""); 196 197 return label; 198 } 199 }); 200 201 testStringTable.setDefaultEditor(I18nString.class, new DefaultCellEditor (new JTextField ()) { 202 203 public Component getTableCellEditorComponent( 204 JTable table, Object value, 205 boolean isSelected, 206 int row, int column) { 207 208 I18nString i18nString = (I18nString)value; 209 210 int modelColumn = testStringTable.convertColumnIndexToModel(column); 211 212 if(modelColumn == COLUMN_INDEX_KEY) 213 value = i18nString == null ? "" : i18nString.getKey(); else if(modelColumn == COLUMN_INDEX_VALUE) 215 value = i18nString == null ? "" : i18nString.getValue(); else 217 value = ""; 219 return super.getTableCellEditorComponent(table, value, isSelected, row, column); 220 } 221 }); 222 223 testStringTable.getColumnModel().getColumn(COLUMN_INDEX_CHECK).setMaxWidth(30); 225 } 226 227 232 private void initComponents() { 234 java.awt.GridBagConstraints gridBagConstraints; 235 236 sourceLabel = new javax.swing.JLabel (); 237 sourceCombo = new javax.swing.JComboBox (); 238 testStringLabel = new javax.swing.JLabel (); 239 scrollPane = new javax.swing.JScrollPane (); 240 testStringTable = new javax.swing.JTable (); 241 242 setLayout(new java.awt.GridBagLayout ()); 243 244 org.openide.awt.Mnemonics.setLocalizedText(sourceLabel, NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Source")); gridBagConstraints = new java.awt.GridBagConstraints (); 246 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 247 add(sourceLabel, gridBagConstraints); 248 249 sourceCombo.setRenderer(new SourceWizardPanel.DataObjectListCellRenderer()); 250 sourceCombo.addActionListener(new java.awt.event.ActionListener () { 251 public void actionPerformed(java.awt.event.ActionEvent evt) { 252 sourceComboActionPerformed(evt); 253 } 254 }); 255 gridBagConstraints = new java.awt.GridBagConstraints (); 256 gridBagConstraints.gridx = 0; 257 gridBagConstraints.gridy = 1; 258 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 259 gridBagConstraints.weightx = 1.0; 260 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 261 add(sourceCombo, gridBagConstraints); 262 263 org.openide.awt.Mnemonics.setLocalizedText(testStringLabel, NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_missing_keys")); gridBagConstraints = new java.awt.GridBagConstraints (); 265 gridBagConstraints.gridx = 0; 266 gridBagConstraints.gridy = 2; 267 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 268 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 0); 269 add(testStringLabel, gridBagConstraints); 270 271 scrollPane.setPreferredSize(new java.awt.Dimension (100, 100)); 272 273 testStringTable.setModel(tableModel); 274 scrollPane.setViewportView(testStringTable); 275 276 gridBagConstraints = new java.awt.GridBagConstraints (); 277 gridBagConstraints.gridx = 0; 278 gridBagConstraints.gridy = 3; 279 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 280 gridBagConstraints.weightx = 1.0; 281 gridBagConstraints.weighty = 1.0; 282 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 283 add(scrollPane, gridBagConstraints); 284 } 286 private void sourceComboActionPerformed(java.awt.event.ActionEvent evt) { if(((SourceData)sourceMap.get(sourceCombo.getSelectedItem())).getStringMap().isEmpty()) { 288 JLabel label = new JLabel (NbBundle.getBundle(TestStringWizardPanel.class).getString("TXT_AllI18nStringsSource")); 290 label.setHorizontalAlignment(JLabel.CENTER); 291 scrollPane.setViewportView(label); 292 } else { 293 scrollPane.setViewportView(testStringTable); 294 tableModel.fireTableDataChanged(); 295 } 296 tableModel.fireTableDataChanged(); 297 } 299 private javax.swing.JScrollPane scrollPane; 301 private javax.swing.JComboBox sourceCombo; 302 private javax.swing.JLabel sourceLabel; 303 private javax.swing.JLabel testStringLabel; 304 private javax.swing.JTable testStringTable; 305 307 308 private class TestStringTableModel extends AbstractTableModel { 309 310 311 public TestStringTableModel() { 312 } 313 314 315 316 public int getColumnCount() { 317 return 4; 318 } 319 320 321 public int getRowCount() { 322 Map stringMap = getStringMap(); 323 return stringMap == null ? 0 : stringMap.size(); 324 } 325 326 327 public Object getValueAt(int rowIndex, int columnIndex) { 328 Map stringMap = getStringMap(); 329 330 if(stringMap == null) 331 return null; 332 333 if(columnIndex == COLUMN_INDEX_CHECK) { 334 return !getRemovedStrings().contains(stringMap.keySet().toArray()[rowIndex]) ? Boolean.TRUE : Boolean.FALSE; 335 } else if(columnIndex == COLUMN_INDEX_HARDSTRING) { 336 return stringMap.keySet().toArray()[rowIndex]; 337 } else { 338 return stringMap.values().toArray()[rowIndex]; 339 } 340 } 341 342 344 public boolean isCellEditable(int rowIndex, int columnIndex) { 345 if(columnIndex == COLUMN_INDEX_CHECK || columnIndex == COLUMN_INDEX_VALUE) 346 return true; 347 else 348 return false; 349 } 350 351 352 public void setValueAt(Object value, int rowIndex, int columnIndex) { 353 Map stringMap = getStringMap(); 354 355 if(stringMap == null) 356 return; 357 358 if(columnIndex == COLUMN_INDEX_CHECK && value instanceof Boolean ) { 359 Object hardString = stringMap.keySet().toArray()[rowIndex]; 360 361 Set removedStrings = getRemovedStrings(); 362 363 if(((Boolean )value).booleanValue()) 364 removedStrings.remove(hardString); 365 else 366 removedStrings.add(hardString); 367 } 368 369 if(columnIndex == COLUMN_INDEX_VALUE) { 370 I18nString i18nString = (I18nString)getStringMap().values().toArray()[rowIndex]; 371 372 i18nString.setValue(value.toString()); 373 } 374 } 375 376 378 public Class getColumnClass(int columnIndex) { 379 if(columnIndex == COLUMN_INDEX_CHECK) 380 return Boolean .class; 381 else if(columnIndex == COLUMN_INDEX_HARDSTRING) 382 return HardCodedString.class; 383 else 384 return I18nString.class; 385 } 386 387 388 public String getColumnName(int column) { 389 if(column == COLUMN_INDEX_HARDSTRING) 390 return NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_HardString"); 391 else if(column == COLUMN_INDEX_KEY) 392 return NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Key"); 393 else if(column == COLUMN_INDEX_VALUE) 394 return NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Value"); 395 else 396 return " "; } 398 } 400 401 406 public static class CustomizeCellEditor extends AbstractCellEditor 407 implements TableCellEditor , ActionListener { 408 409 410 private I18nString i18nString; 411 412 413 private JButton editorComponent; 414 415 416 417 public CustomizeCellEditor() { 418 editorComponent = new JButton ("..."); 420 editorComponent.addActionListener(new ActionListener () { 421 public void actionPerformed(ActionEvent evt) { 422 PropertyPanel panel = i18nString.getSupport().getPropertyPanel(); 423 panel.setI18nString(i18nString); 424 425 DialogDescriptor dd = new DialogDescriptor(panel,"Customize Property"); 426 dd.setModal(true); 427 dd.setOptionType(DialogDescriptor.DEFAULT_OPTION); 428 dd.setOptions(new Object [] {DialogDescriptor.OK_OPTION}); 429 dd.setAdditionalOptions(new Object [0]); 430 dd.setButtonListener(CustomizeCellEditor.this); 431 432 Dialog dialog = DialogDisplayer.getDefault().createDialog(dd); 433 dialog.setVisible(true); 434 } 435 }); 436 } 437 438 439 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 440 i18nString = (I18nString)value; 441 442 return editorComponent; 443 } 444 445 446 public Object getCellEditorValue() { 447 return i18nString; 448 } 449 450 451 public boolean isCellEditable(EventObject anEvent) { 452 if(anEvent instanceof MouseEvent ) { 453 return ((MouseEvent )anEvent).getClickCount() >= 1; 455 } 456 return true; 457 } 458 459 460 public boolean shouldSelectCell(EventObject anEvent) { 461 return true; 462 } 463 464 465 public boolean stopCellEditing() { 466 fireEditingStopped(); 467 return true; 468 } 469 470 471 public void cancelCellEditing() { 472 fireEditingCanceled(); 473 } 474 475 476 public void actionPerformed(ActionEvent evt) { 477 stopCellEditing(); 478 } 479 480 } 481 482 483 486 public static class Panel extends I18nWizardDescriptor.Panel 487 implements WizardDescriptor.FinishablePanel, I18nWizardDescriptor.ProgressMonitor { 488 489 490 private final JLabel emptyLabel; 491 492 493 private transient TestStringWizardPanel testStringPanel; 494 495 public Panel() { 496 emptyLabel = new JLabel (NbBundle.getBundle(TestStringWizardPanel.class).getString("TXT_AllI18nStrings")); 497 emptyLabel.setHorizontalAlignment(JLabel.CENTER); 498 emptyLabel.setVerticalAlignment(JLabel.CENTER); 499 } 500 501 502 503 505 protected Component createComponent() { 506 JPanel panel = new JPanel (); 507 panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TestStringWizardPanel.class).getString("ACS_TestStringWizardPanel")); 508 509 panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (2)); panel.setName(NbBundle.getBundle(TestStringWizardPanel.class).getString("TXT_FoundMissingResource")); 511 panel.setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION); 512 panel.setLayout(new GridBagLayout ()); 513 GridBagConstraints constraints = new GridBagConstraints (); 514 constraints.weightx = 1.0; 515 constraints.weighty = 1.0; 516 constraints.fill = GridBagConstraints.BOTH; 517 panel.add(getUI(), constraints); 518 return panel; 519 } 520 521 522 public boolean isValid() { 523 return true; 524 } 525 526 528 public boolean isFinishPanel() { 529 return true; 530 } 531 532 533 public void readSettings(Object settings) { 534 super.readSettings(settings); 535 getUI().setSourceMap(getMap()); 536 537 JPanel panel = (JPanel )getComponent(); 538 if(foundStrings(getMap())) { 539 if(panel.isAncestorOf(emptyLabel)) { 540 panel.remove(emptyLabel); 541 GridBagConstraints constraints = new GridBagConstraints (); 542 constraints.weightx = 1.0; 543 constraints.weighty = 1.0; 544 constraints.fill = GridBagConstraints.BOTH; 545 panel.add(getUI(), constraints); 546 } 547 } else { 548 if(panel.isAncestorOf(getUI())) { 549 panel.remove(getUI()); 550 GridBagConstraints constraints = new GridBagConstraints (); 551 constraints.weightx = 1.0; 552 constraints.weighty = 1.0; 553 constraints.fill = GridBagConstraints.BOTH; 554 panel.add(emptyLabel, constraints); 555 } 556 } 557 } 558 559 560 public void storeSettings(Object settings) { 561 super.storeSettings(settings); 562 (getMap()).clear(); 564 (getMap()).putAll(getUI().getSourceMap()); 565 } 566 567 569 public void doLongTimeChanges() { 570 if (foundStrings(getMap())) { 571 final ProgressWizardPanel progressPanel = new ProgressWizardPanel(true); 573 progressPanel.setMainText(NbBundle.getBundle(getClass()).getString("LBL_Internationalizing")); 574 progressPanel.setMainProgress(0); 575 576 ((Container )getComponent()).remove(getUI()); 577 GridBagConstraints constraints = new GridBagConstraints (); 578 constraints.weightx = 1.0; 579 constraints.weighty = 1.0; 580 constraints.fill = GridBagConstraints.BOTH; 581 ((Container )getComponent()).add(progressPanel, constraints); 582 ((JComponent )getComponent()).revalidate(); 583 getComponent().repaint(); 584 585 Map sourceMap = getUI().getSourceMap(); 587 588 Iterator sourceIterator = sourceMap.keySet().iterator(); 589 590 for(int i=0; sourceIterator.hasNext(); i++) { 592 Object source = sourceIterator.next(); 593 594 SourceData sourceData = (SourceData)sourceMap.get(source); 596 597 I18nSupport support = sourceData.getSupport(); 599 600 Map stringMap = sourceData.getStringMap(); 602 603 Set removed = sourceData.getRemovedStrings(); 605 606 Iterator it = stringMap.keySet().iterator(); 608 609 ClassPath cp = ClassPath.getClassPath( ((DataObject)source).getPrimaryFile(), ClassPath.SOURCE ); 610 progressPanel.setSubText(Util.getString("LBL_Source")+" "+cp.getResourceName( ((DataObject)source).getPrimaryFile(), '.', false)); 612 for(int j=0; it.hasNext(); j++) { 613 HardCodedString hcString = (HardCodedString)it.next(); 614 I18nString i18nString = (I18nString)stringMap.get(hcString); 615 616 if(removed != null && removed.contains(hcString)) 617 continue; 619 620 String comment = i18nString.getComment(); 622 if (source instanceof DataObject && (comment == null || "".equals(comment) ) ) { 623 DataObject dobj = (DataObject) source; 624 cp = ClassPath.getClassPath( dobj.getPrimaryFile(), ClassPath.SOURCE ); 625 comment = cp.getResourceName( dobj.getPrimaryFile(), '.', false ); 626 } 627 628 String key = i18nString.getKey(); 631 String prev = support.getResourceHolder().getCommentForKey(key); 632 comment += (prev == null ? "" : " " + prev); support.getResourceHolder().addProperty(i18nString.getKey(), i18nString.getValue(), comment, false); 634 635 progressPanel.setSubProgress((int)((j+1)/(float)stringMap.size() * 100)); 636 } 638 if(support.hasAdditionalCustomizer()) { 640 support.performAdditionalChanges(); 641 } 642 643 progressPanel.setMainProgress((int)((i+1)/(float)sourceMap.size() * 100)); 644 } } } 647 648 649 public void reset() { 650 } 651 652 654 private static boolean foundStrings(Map sourceMap) { 655 Iterator it = sourceMap.keySet().iterator(); 656 657 while(it.hasNext()) { 658 SourceData sourceData = (SourceData)sourceMap.get(it.next()); 659 if(!sourceData.getStringMap().isEmpty()) 660 return true; 661 } 662 663 return false; 664 } 665 666 667 public HelpCtx getHelp() { 668 return new HelpCtx(I18nUtil.HELP_ID_TESTING); 669 } 670 671 private synchronized TestStringWizardPanel getUI() { 672 if (testStringPanel == null) { 673 testStringPanel = new TestStringWizardPanel(); 674 } 675 return testStringPanel; 676 } 677 } } 679 | Popular Tags |