1 19 20 21 package org.netbeans.modules.i18n.wizard; 22 23 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.beans.BeanInfo ; 29 import java.io.IOException ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import javax.swing.event.ListSelectionEvent ; 34 import javax.swing.event.ListSelectionListener ; 35 import javax.swing.ImageIcon ; 36 import javax.swing.JComponent ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 import javax.swing.JTable ; 40 import javax.swing.table.AbstractTableModel ; 41 import javax.swing.table.DefaultTableCellRenderer ; 42 import org.netbeans.api.java.classpath.ClassPath; 43 import org.netbeans.api.project.Project; 44 import org.netbeans.api.project.FileOwnerQuery; 45 46 import org.netbeans.modules.i18n.FactoryRegistry; 47 import org.netbeans.modules.i18n.HardCodedString; 48 import org.netbeans.modules.i18n.I18nSupport; 49 import org.netbeans.modules.i18n.I18nUtil; 50 import org.netbeans.modules.i18n.SelectorUtils; 51 import org.netbeans.modules.properties.UtilConvert; 53 import org.openide.loaders.DataObject; 54 import org.openide.util.HelpCtx; 55 import org.openide.util.NbBundle; 56 import org.openide.filesystems.FileObject; 57 import org.openide.ErrorManager; 58 59 60 67 final class ResourceWizardPanel extends JPanel { 68 69 70 private final Map sourceMap = Util.createWizardSourceMap(); 71 72 73 private final ResourceTableModel tableModel = new ResourceTableModel(); 74 75 78 private final Panel descPanel; 79 80 private final boolean testMode; 81 82 83 private ResourceWizardPanel(Panel descPanel, boolean testMode) { 84 this.descPanel = descPanel; 85 this.testMode = testMode; 86 87 initComponents(); 88 89 initTable(); 90 91 initAccesibility(); 92 } 93 94 95 96 public Map getSourceMap() { 97 return sourceMap; 98 } 99 100 101 public void setSourceMap(Map sourceMap) { 102 this.sourceMap.clear(); 103 this.sourceMap.putAll(sourceMap); 104 105 tableModel.fireTableDataChanged(); 106 107 descPanel.fireStateChanged(); 108 } 109 110 private String getPanelDescription() { 111 if (testMode == false) { 112 return Util.getString("MSG_ResourcePanel_desc"); 113 } else { 114 return Util.getString("MSG_ResourcePanel_test_desc"); 115 } 116 } 117 118 119 private void initTable() { 120 resourcesTable.setDefaultRenderer(DataObject.class, new DefaultTableCellRenderer () { 121 public Component getTableCellRendererComponent(JTable table, Object value, 122 boolean isSelected, boolean hasFocus, int row, int column) { 123 124 JLabel label = (JLabel )super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 125 DataObject dataObject = (DataObject)value; 126 127 if(dataObject != null) { 128 String name = "N/A"; 129 if (column == 0) { 130 ClassPath cp = ClassPath.getClassPath(dataObject.getPrimaryFile(), ClassPath.SOURCE ); 132 name = cp.getResourceName( dataObject.getPrimaryFile(), '.', false ); 133 } else { 134 DataObject dob = (DataObject)tableModel.getValueAt(row, 0); 138 name = Util.getResourceName(dob.getPrimaryFile(), dataObject.getPrimaryFile(), '.', false); 139 } 140 141 label.setText(name); label.setIcon(new ImageIcon (dataObject.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16))); 143 } else { 144 label.setText(""); label.setIcon(null); 146 } 147 148 return label; 149 } 150 }); 151 152 Component cellSample = new DefaultTableCellRenderer () 153 .getTableCellRendererComponent( 154 resourcesTable, "N/A", false, false, 0, 0); int cellHeight = cellSample.getPreferredSize().height; 160 int rowHeight = cellHeight + resourcesTable.getRowMargin(); 161 resourcesTable.setRowHeight(Math.max(16, rowHeight)); 162 163 resourcesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 164 public void valueChanged(ListSelectionEvent evt) { 165 addButton.setEnabled(!resourcesTable.getSelectionModel().isSelectionEmpty()); 166 } 167 }); 168 169 addButton.setEnabled(!resourcesTable.getSelectionModel().isSelectionEmpty()); 170 } 171 172 173 private void initAccesibility() { 174 addButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACS_CTL_SelectResource")); 175 addAllButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACS_CTL_SelectResourceAll")); 176 resourcesTable.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACSD_resourcesTable")); 177 resourcesTable.getAccessibleContext().setAccessibleName(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACSN_resourcesTable")); 178 } 179 180 185 private void initComponents() { 187 java.awt.GridBagConstraints gridBagConstraints; 188 189 descTextArea = new javax.swing.JTextArea (); 190 jScrollPane1 = new javax.swing.JScrollPane (); 191 resourcesTable = new javax.swing.JTable (); 192 addAllButton = new javax.swing.JButton (); 193 addButton = new javax.swing.JButton (); 194 195 setLayout(new java.awt.GridBagLayout ()); 196 197 descTextArea.setColumns(20); 198 descTextArea.setEditable(false); 199 descTextArea.setLineWrap(true); 200 descTextArea.setText(getPanelDescription()); 201 descTextArea.setWrapStyleWord(true); 202 descTextArea.setDisabledTextColor(new JLabel ().getForeground()); 203 descTextArea.setEnabled(false); 204 descTextArea.setOpaque(false); 205 gridBagConstraints = new java.awt.GridBagConstraints (); 206 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 207 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 208 add(descTextArea, gridBagConstraints); 209 210 resourcesTable.setModel(tableModel); 211 jScrollPane1.setViewportView(resourcesTable); 212 213 gridBagConstraints = new java.awt.GridBagConstraints (); 214 gridBagConstraints.gridx = 0; 215 gridBagConstraints.gridy = 1; 216 gridBagConstraints.gridheight = 2; 217 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 218 gridBagConstraints.weightx = 1.0; 219 gridBagConstraints.weighty = 1.0; 220 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 221 add(jScrollPane1, gridBagConstraints); 222 223 org.openide.awt.Mnemonics.setLocalizedText(addAllButton, NbBundle.getBundle(ResourceWizardPanel.class).getString("CTL_SelectResourceAll")); addAllButton.addActionListener(new java.awt.event.ActionListener () { 225 public void actionPerformed(java.awt.event.ActionEvent evt) { 226 addAllButtonActionPerformed(evt); 227 } 228 }); 229 gridBagConstraints = new java.awt.GridBagConstraints (); 230 gridBagConstraints.gridx = 1; 231 gridBagConstraints.gridy = 1; 232 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 233 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 234 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 0); 235 add(addAllButton, gridBagConstraints); 236 237 org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle.getBundle(ResourceWizardPanel.class).getString("CTL_SelectResource")); addButton.addActionListener(new java.awt.event.ActionListener () { 239 public void actionPerformed(java.awt.event.ActionEvent evt) { 240 addButtonActionPerformed(evt); 241 } 242 }); 243 gridBagConstraints = new java.awt.GridBagConstraints (); 244 gridBagConstraints.gridx = 1; 245 gridBagConstraints.gridy = 2; 246 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 247 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 248 gridBagConstraints.weighty = 1.0; 249 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 0); 250 add(addButton, gridBagConstraints); 251 } 253 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { DataObject resource = selectResource(); 255 256 if(resource == null) 257 return; 258 259 int[] selectedRows = resourcesTable.getSelectedRows(); 260 261 for(int i=0; i<selectedRows.length; i++) { 263 DataObject dataObject = (DataObject)resourcesTable.getValueAt(selectedRows[i], 0); 264 265 sourceMap.put(dataObject, new SourceData(resource)); 266 267 tableModel.fireTableCellUpdated(selectedRows[i], 1); 268 } 269 270 descPanel.fireStateChanged(); 271 } 273 private void addAllButtonActionPerformed(java.awt.event.ActionEvent evt) { DataObject resource = selectResource(); 275 276 if(resource == null) 277 return; 278 279 for(int i=0; i<resourcesTable.getRowCount(); i++) { 281 DataObject dataObject = (DataObject)resourcesTable.getValueAt(i, 0); 282 283 sourceMap.put(dataObject, new SourceData(resource)); 284 285 tableModel.fireTableCellUpdated(i, 1); 286 } 287 288 descPanel.fireStateChanged(); 289 } 291 292 private DataObject selectResource() { 293 Project prj = null; 294 FileObject fo = null; 295 Iterator it = sourceMap.keySet().iterator(); 296 if (it.hasNext()) { 297 DataObject dobj = (DataObject) it.next(); 298 fo = dobj.getPrimaryFile(); 299 prj = FileOwnerQuery.getOwner(fo); 300 } 301 302 return SelectorUtils.selectBundle(prj, fo); 303 } 304 305 306 private javax.swing.JButton addAllButton; 308 private javax.swing.JButton addButton; 309 private javax.swing.JTextArea descTextArea; 310 private javax.swing.JScrollPane jScrollPane1; 311 private javax.swing.JTable resourcesTable; 312 314 315 private class ResourceTableModel extends AbstractTableModel { 316 317 318 public ResourceTableModel() { 319 } 320 321 322 323 public int getColumnCount() { 324 return 2; 325 } 326 327 328 public int getRowCount() { 329 return sourceMap.size(); 330 } 331 332 333 public Object getValueAt(int rowIndex, int columnIndex) { 334 335 if(columnIndex == 0) { 336 return sourceMap.keySet().toArray()[rowIndex]; 337 } else { 338 SourceData value = (SourceData)sourceMap.values().toArray()[rowIndex]; 339 return value == null ? null : value.getResource(); 340 } 341 342 } 343 344 346 public Class getColumnClass(int columnIndex) { 347 return DataObject.class; 348 } 349 350 351 public String getColumnName(int column) { 352 if(column == 0) 353 return NbBundle.getBundle(ResourceWizardPanel.class).getString("CTL_Source"); 354 else 355 return NbBundle.getBundle(ResourceWizardPanel.class).getString("CTL_Resource"); 356 } 357 } 359 360 363 public static class Panel extends I18nWizardDescriptor.Panel implements I18nWizardDescriptor.ProgressMonitor { 364 365 366 private transient ResourceWizardPanel resourcePanel; 367 368 369 private boolean testWizard; 370 371 372 373 public Panel() { 374 this(false); 375 } 376 377 378 public Panel(boolean testWizard) { 379 this.testWizard = testWizard; 380 } 381 382 383 385 protected Component createComponent() { 386 JPanel panel = new JPanel (); 387 388 panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACS_ResourceWizardPanel")); 390 391 panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (1)); if(testWizard) 393 panel.setName(NbBundle.getBundle(ResourceWizardPanel.class).getString("TXT_SelectTestResource")); 394 else 395 panel.setName(NbBundle.getBundle(ResourceWizardPanel.class).getString("TXT_SelectResource")); 396 397 panel.setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION); 398 399 panel.setLayout(new GridBagLayout ()); 400 GridBagConstraints constraints = new GridBagConstraints (); 401 constraints.weightx = 1.0; 402 constraints.weighty = 1.0; 403 constraints.fill = GridBagConstraints.BOTH; 404 panel.add(getUI(), constraints); 405 406 return panel; 407 } 408 409 410 public boolean isValid() { 411 return !getUI().getSourceMap().containsValue(null); 412 } 413 414 415 public void readSettings(Object settings) { 416 super.readSettings(settings); 417 getUI().setSourceMap(getMap()); 418 } 419 420 421 public void storeSettings(Object settings) { 422 super.storeSettings(settings); 423 getMap().clear(); 425 getMap().putAll(getUI().getSourceMap()); 426 } 427 428 430 public void doLongTimeChanges() { 431 ProgressWizardPanel progressPanel = new ProgressWizardPanel(false); 433 434 showProgressPanel(progressPanel); 435 436 progressPanel.setMainText(NbBundle.getBundle(ResourceWizardPanel.class).getString("TXT_Loading")); 437 progressPanel.setMainProgress(0); 438 439 Map sourceMap = getUI().getSourceMap(); 441 442 Iterator sourceIterator = sourceMap.keySet().iterator(); 443 444 for(int i=0; sourceIterator.hasNext(); i++) { 446 DataObject source = (DataObject)sourceIterator.next(); 447 448 ClassPath cp = ClassPath.getClassPath( source.getPrimaryFile(), ClassPath.SOURCE ); 449 progressPanel.setMainText(NbBundle.getBundle(ResourceWizardPanel.class).getString("TXT_Loading") 450 + " " + cp.getResourceName( source.getPrimaryFile(), '.', false )); 452 453 SourceData sourceData = (SourceData)sourceMap.get(source); 455 456 I18nSupport support; 459 try { 460 support = FactoryRegistry.getFactory(source.getClass()).create(source); 461 } catch(IOException ioe) { 462 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 463 sourceMap.remove(source); 465 continue; 466 } 467 sourceData = new SourceData(sourceData.getResource(), support); 468 sourceMap.put(source, sourceData); 469 470 cp = ClassPath.getClassPath( source.getPrimaryFile(), ClassPath.SOURCE ); 471 progressPanel.setMainText(NbBundle.getBundle(ResourceWizardPanel.class).getString("TXT_SearchingIn") 472 + " " + cp.getResourceName( source.getPrimaryFile(), '.', false ) ); 474 Map stringMap = sourceData.getStringMap(); 476 477 HardCodedString[] foundStrings; 478 479 if(testWizard) { 480 foundStrings = support.getFinder().findAllI18nStrings(); 482 } else { 483 foundStrings = support.getFinder().findAllHardCodedStrings(); 485 } 486 487 if(foundStrings == null) { 488 sourceData.setStringMap(new HashMap (0)); 490 continue; 491 } 492 493 Map map = new HashMap (foundStrings.length); 494 495 for(int j=0; j<foundStrings.length; j++) { 497 if(testWizard && support.getResourceHolder().getValueForKey(UtilConvert.escapePropertiesSpecialChars(foundStrings[j].getText())) != null) 498 continue; 499 500 map.put(foundStrings[j], support.getDefaultI18nString(foundStrings[j])); 501 } 502 503 progressPanel.setMainProgress((int)((i+1)/(float)sourceMap.size() * 100)); 504 505 sourceData.setStringMap(map); 506 } } 508 509 510 private void showProgressPanel(ProgressWizardPanel progressPanel) { 511 ((Container )getComponent()).remove(getUI()); 512 GridBagConstraints constraints = new GridBagConstraints (); 513 constraints.weightx = 1.0; 514 constraints.weighty = 1.0; 515 constraints.fill = GridBagConstraints.BOTH; 516 ((Container )getComponent()).add(progressPanel, constraints); 517 ((JComponent )getComponent()).revalidate(); 518 getComponent().repaint(); 519 } 520 521 522 public void reset() { 523 Container container = (Container )getComponent(); 524 525 if(!container.isAncestorOf(getUI())) { 526 container.removeAll(); 527 GridBagConstraints constraints = new GridBagConstraints (); 528 constraints.weightx = 1.0; 529 constraints.weighty = 1.0; 530 constraints.fill = GridBagConstraints.BOTH; 531 container.add(getUI(), constraints); 532 } 533 } 534 535 536 public HelpCtx getHelp() { 537 if(testWizard) 538 return new HelpCtx(I18nUtil.HELP_ID_TESTING); 539 else 540 return new HelpCtx(I18nUtil.HELP_ID_WIZARD); 541 } 542 543 private synchronized ResourceWizardPanel getUI() { 544 if (resourcePanel == null) { 545 resourcePanel = new ResourceWizardPanel(this, testWizard); 546 } 547 return resourcePanel; 548 } 549 550 } 552 } 553 | Popular Tags |