KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > wizard > HardStringWizardPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n.wizard;
22
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Container JavaDoc;
27 import java.awt.Dialog JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29 import java.awt.GridBagLayout JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.awt.event.MouseEvent JavaDoc;
33 import java.awt.Insets JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.EventObject JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.HashSet JavaDoc;
40 import javax.swing.DefaultCellEditor JavaDoc;
41 import javax.swing.DefaultComboBoxModel JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JComponent JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JTable JavaDoc;
47 import javax.swing.JTextField JavaDoc;
48 import javax.swing.event.TableModelEvent JavaDoc;
49 import javax.swing.event.TableModelListener JavaDoc;
50 import javax.swing.table.AbstractTableModel JavaDoc;
51 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
52 import javax.swing.table.TableCellEditor JavaDoc;
53 import javax.swing.AbstractCellEditor JavaDoc;
54 import org.netbeans.api.java.classpath.ClassPath;
55
56 import org.netbeans.modules.i18n.HardCodedString;
57 import org.netbeans.modules.i18n.I18nString;
58 import org.netbeans.modules.i18n.I18nSupport;
59 import org.netbeans.modules.i18n.I18nUtil;
60 import org.netbeans.modules.i18n.PropertyPanel;
61
62 import org.openide.DialogDescriptor;
63 import org.openide.NotifyDescriptor;
64 import org.openide.loaders.DataObject;
65 import org.openide.util.HelpCtx;
66 import org.openide.util.NbBundle;
67 import org.openide.WizardDescriptor;
68 import org.openide.DialogDisplayer;
69
70
71 /**
72  * <code>WizardDescriptor.Panel</code> used for to show found hard coded strings
73  * for sepcified sources. It offers default key-value pairs and allows modify them.
74  * These values will be used by actual i18n-zation of those sources.
75  * It is the fourth and last panel of I18N Wizard.
76  *
77  * @author Peter Zavadsky
78  * @author Marian Petras
79  * @see Panel
80  */

81 final class HardStringWizardPanel extends JPanel JavaDoc {
82
83     /** Column index of check box column. */
84     private static final int COLUMN_INDEX_CHECK = 0;
85     /** Column index of hard string column. */
86     private static final int COLUMN_INDEX_HARDSTRING = 1;
87     /** Column index of key column. */
88     private static final int COLUMN_INDEX_KEY = 2;
89     /** Column index of value column. */
90     private static final int COLUMN_INDEX_VALUE = 3;
91     /** Column index of custom column. */
92     private static final int COLUMN_INDEX_CUSTOM = 4;
93         
94     /** Local copy of i18n wizard data. */
95     private final Map JavaDoc sourceMap = Util.createWizardSourceMap();
96
97     /** Table model for <code>stringTable</code>. */
98     private final AbstractTableModel JavaDoc tableModel = new HardCodedStringTableModel();
99     
100     
101     /** Creates new form HardCodedStringsPanel */
102     private HardStringWizardPanel() {
103         initComponents();
104         
105         postInitComponents();
106         
107         initTable();
108
109         setComboModel(sourceMap);
110         
111         initAccessibility();
112     }
113
114     
115     /** Sets combo model only for source which were some found strings in. */
116     private void setComboModel(Map JavaDoc sourceMap) {
117         Object JavaDoc[] sources = sourceMap.keySet().toArray();
118         
119         ArrayList JavaDoc nonEmptySources = new ArrayList JavaDoc();
120         
121         for(int i = 0; i < sources.length; i++) {
122             if(!((SourceData)sourceMap.get(sources[i])).getStringMap().isEmpty())
123                 nonEmptySources.add(sources[i]);
124         }
125         
126         sourceCombo.setModel(new DefaultComboBoxModel JavaDoc(nonEmptySources.toArray()));
127     }
128     
129     /** Adds additional init of components. */
130     private void postInitComponents() {
131         sourceLabel.setLabelFor(sourceCombo);
132         hardStringLabel.setLabelFor(hardStringTable);
133     }
134
135     /** Getter for <code>resources</code> property. */
136     public Map JavaDoc getSourceMap() {
137         return sourceMap;
138     }
139     
140     /** Setter for <code>resources</code> property. */
141     public void setSourceMap(Map JavaDoc sourceMap) {
142         this.sourceMap.clear();
143         this.sourceMap.putAll(sourceMap);
144
145         setComboModel(sourceMap);
146     }
147     
148     
149     /** Gets string map for specified source data object. Utility method. */
150     private Map JavaDoc getStringMap() {
151         SourceData sourceData = (SourceData)sourceMap.get(sourceCombo.getSelectedItem());
152         return sourceData == null ? null : sourceData.getStringMap();
153     }
154     
155     /** Gets hard coded strings user wish to not proceed. */
156     private Set JavaDoc getRemovedStrings() {
157         SourceData sourceData = (SourceData)sourceMap.get(sourceCombo.getSelectedItem());
158         if(sourceData == null)
159             return null;
160         
161         if(sourceData.getRemovedStrings() == null) {
162             // init removed string for the first time
163
Set JavaDoc removed = new HashSet JavaDoc();
164             
165             // add all strings with empty keys
166
Map JavaDoc stringMap = sourceData.getStringMap(); // map<HardCodedString, I18nString>
167
Iterator JavaDoc hcsIt = stringMap.keySet().iterator(); // hard
168

169             while (hcsIt.hasNext()) {
170                 HardCodedString hcString = (HardCodedString)hcsIt.next();
171                 I18nString i18nString = (I18nString)stringMap.get(hcString);
172
173                 if (i18nString.getKey().equals(""))
174                     removed.add(hcString);
175             }
176             sourceData.setRemovedStrings(removed);
177         }
178         
179         return sourceData.getRemovedStrings();
180     }
181
182     /** Inits table component. */
183     private void initTable() {
184         hardStringTable.setDefaultRenderer(HardCodedString.class, new DefaultTableCellRenderer JavaDoc() {
185             public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
186                 boolean isSelected, boolean hasFocus, int row, int column) {
187                     
188                 JLabel JavaDoc label = (JLabel JavaDoc)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
189
190                 HardCodedString hcString = (HardCodedString)value;
191
192                 if(hcString != null)
193                     label.setText(hcString.getText());
194                 else
195                     label.setText(""); // NOI18N
196

197                 return label;
198             }
199         });
200         
201         hardStringTable.setDefaultRenderer(I18nString.class, new DefaultTableCellRenderer JavaDoc() {
202             private final JButton JavaDoc dotButton = new JButton JavaDoc("..."); // NOI18N
203

204             public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
205                 boolean isSelected, boolean hasFocus, int row, int column) {
206
207                 I18nString i18nString = (I18nString)value;
208                 
209                 int modelColumn = hardStringTable.convertColumnIndexToModel(column);
210
211                 if(modelColumn == COLUMN_INDEX_CUSTOM)
212                     return dotButton;
213                     
214                 JLabel JavaDoc label = (JLabel JavaDoc)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
215
216                 if(i18nString != null) {
217                     if(modelColumn == COLUMN_INDEX_KEY) {
218                         label.setText(i18nString.getKey());
219                     } else {
220                         label.setText(i18nString.getValue());
221                     }
222
223                 } else
224                     label.setText(""); // NOI18N
225

226                 
227                 return label;
228             }
229         });
230
231         hardStringTable.setDefaultEditor(I18nString.class, new DefaultCellEditor JavaDoc(new JTextField JavaDoc()) {
232             
233             public Component JavaDoc getTableCellEditorComponent(
234                 JTable JavaDoc table, Object JavaDoc value,
235                 boolean isSelected,
236                 int row, int column) {
237
238                 I18nString i18nString = (I18nString)value;
239                 
240                 int modelColumn = hardStringTable.convertColumnIndexToModel(column);
241                 
242                 if(modelColumn == COLUMN_INDEX_KEY)
243                     value = i18nString == null ? "" : i18nString.getKey(); // NOI18N
244
else if(modelColumn == COLUMN_INDEX_VALUE)
245                     value = i18nString == null ? "" : i18nString.getValue(); // NOI18N
246
else
247                     value = ""; // NOI18N
248

249                 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
250             }
251         });
252         
253         Component JavaDoc cellSample = new DefaultTableCellRenderer JavaDoc()
254                                .getTableCellRendererComponent(
255                                     hardStringTable, //table
256
"N/A", //value //NOI18N
257
false, //isSelected
258
false, //hasFocus
259
0, 0); //row, column
260
int cellHeight = cellSample.getPreferredSize().height;
261         int rowHeight = cellHeight + hardStringTable.getRowMargin();
262         hardStringTable.setRowHeight(Math.max(16, rowHeight));
263         
264         hardStringTable.getColumnModel().getColumn(COLUMN_INDEX_CUSTOM).setCellEditor(new CustomizeCellEditor());
265
266         // PENDING: Setting the size of columns with check box and customize button editor.
267
hardStringTable.getColumnModel().getColumn(COLUMN_INDEX_CHECK).setMaxWidth(30);
268         hardStringTable.getColumnModel().getColumn(COLUMN_INDEX_CUSTOM).setMaxWidth(30);
269     }
270     
271     private void initAccessibility() {
272         sourceCombo.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(HardStringWizardPanel.class).getString("ACSD_sourceCombo"));
273         hardStringTable.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(HardStringWizardPanel.class).getString("ACSD_hardStringTable"));
274     }
275     
276     /** This method is called from within the constructor to
277      * initialize the form.
278      * WARNING: Do NOT modify this code. The content of this method is
279      * always regenerated by the Form Editor.
280      */

281     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
282
private void initComponents() {
283         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
284
285         sourceLabel = new javax.swing.JLabel JavaDoc();
286         sourceCombo = new javax.swing.JComboBox JavaDoc();
287         hardStringLabel = new javax.swing.JLabel JavaDoc();
288         scrollPane = new javax.swing.JScrollPane JavaDoc();
289         hardStringTable = new javax.swing.JTable JavaDoc();
290
291         setLayout(new java.awt.GridBagLayout JavaDoc());
292
293         org.openide.awt.Mnemonics.setLocalizedText(sourceLabel, NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Source")); // NOI18N
294
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
295         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
296         add(sourceLabel, gridBagConstraints);
297
298         sourceCombo.setRenderer(new SourceWizardPanel.DataObjectListCellRenderer());
299         sourceCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
300             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
301                 sourceComboActionPerformed(evt);
302             }
303         });
304         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
305         gridBagConstraints.gridx = 0;
306         gridBagConstraints.gridy = 1;
307         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
308         gridBagConstraints.weightx = 1.0;
309         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
310         add(sourceCombo, gridBagConstraints);
311
312         org.openide.awt.Mnemonics.setLocalizedText(hardStringLabel, NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_FoundStrings")); // NOI18N
313
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
314         gridBagConstraints.gridx = 0;
315         gridBagConstraints.gridy = 2;
316         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
317         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 0, 0, 0);
318         add(hardStringLabel, gridBagConstraints);
319
320         scrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(100, 100));
321
322         hardStringTable.setModel(tableModel);
323         scrollPane.setViewportView(hardStringTable);
324
325         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
326         gridBagConstraints.gridx = 0;
327         gridBagConstraints.gridy = 3;
328         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
329         gridBagConstraints.weightx = 1.0;
330         gridBagConstraints.weighty = 1.0;
331         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
332         add(scrollPane, gridBagConstraints);
333     }// </editor-fold>//GEN-END:initComponents
334

335     private void sourceComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_sourceComboActionPerformed
336
if(((SourceData)sourceMap.get(sourceCombo.getSelectedItem())).getStringMap().isEmpty()) {
337             // There are no hardcoded strings found for this selected source.
338
JLabel JavaDoc label = new JLabel JavaDoc(NbBundle.getBundle(HardStringWizardPanel.class).getString("TXT_NoHardstringsSource"));
339             label.setHorizontalAlignment(JLabel.CENTER);
340             scrollPane.setViewportView(label);
341         } else {
342             scrollPane.setViewportView(hardStringTable);
343             tableModel.fireTableDataChanged();
344         }
345     }//GEN-LAST:event_sourceComboActionPerformed
346

347     // Variables declaration - do not modify//GEN-BEGIN:variables
348
private javax.swing.JLabel JavaDoc hardStringLabel;
349     private javax.swing.JTable JavaDoc hardStringTable;
350     private javax.swing.JScrollPane JavaDoc scrollPane;
351     private javax.swing.JComboBox JavaDoc sourceCombo;
352     private javax.swing.JLabel JavaDoc sourceLabel;
353     // End of variables declaration//GEN-END:variables
354

355     /** Table model for this class. */
356     private class HardCodedStringTableModel extends AbstractTableModel JavaDoc {
357         
358         /** Constructor. */
359         public HardCodedStringTableModel() {
360         }
361         
362         
363         
364         /** Implements superclass abstract method. */
365         public int getColumnCount() {
366             return 5;
367         }
368         
369         /** Implemenst superclass abstract method. */
370         public int getRowCount() {
371             Map JavaDoc stringMap = getStringMap();
372             return stringMap == null ? 0 : stringMap.size();
373         }
374         
375         /** Implements superclass abstract method. */
376         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
377             Map JavaDoc stringMap = getStringMap();
378             
379             if(stringMap == null)
380                 return null;
381             
382             if(columnIndex == COLUMN_INDEX_CHECK) {
383                 if (getRemovedStrings().contains(stringMap.keySet().toArray()[rowIndex]))
384                     return Boolean.FALSE ;
385                 else
386                     return Boolean.TRUE;
387                 
388             } else if(columnIndex == COLUMN_INDEX_HARDSTRING) {
389                 return stringMap.keySet().toArray()[rowIndex];
390             } else {
391                 return stringMap.values().toArray()[rowIndex];
392             }
393         }
394         
395         /** Overrides superclass method.
396          * @ return true for all columns but first */

397         public boolean isCellEditable(int rowIndex, int columnIndex) {
398             return (columnIndex != COLUMN_INDEX_HARDSTRING);
399         }
400         
401         /** Overrides superclass method. */
402         public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
403             Map JavaDoc stringMap = getStringMap();
404             if(stringMap == null) return;
405
406             switch (columnIndex) {
407                 case COLUMN_INDEX_HARDSTRING: return;
408                 case COLUMN_INDEX_CUSTOM:
409                     I18nString otherValue = (I18nString)getValueAt(rowIndex, COLUMN_INDEX_KEY);
410                     if (!((I18nString)value).getKey().equals(""))
411                         setValueAt(Boolean.TRUE, rowIndex, COLUMN_INDEX_CHECK);
412                     else
413                         setValueAt(Boolean.FALSE, rowIndex, COLUMN_INDEX_CHECK);
414                     break;
415                 case COLUMN_INDEX_CHECK :
416                     if(value instanceof Boolean JavaDoc) {
417
418                         // check that the key is not empty and thus it is allowed
419
// to change the value. Display a notification otherwise.
420
if ((((Boolean JavaDoc)value).booleanValue()==true) &&
421                             ((I18nString)getValueAt(rowIndex, COLUMN_INDEX_KEY)).getKey().equals(""))
422                         { // empty,not allowed
423
String JavaDoc message = NbBundle.getMessage(HardStringWizardPanel.class, "MSG_CANNOT_INSERT_EMPTY_KEYS");
424                             NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.Message.INFORMATION_MESSAGE);
425                             DialogDisplayer.getDefault().notify(nd);
426                         } else {
427                             Object JavaDoc hardString = stringMap.keySet().toArray()[rowIndex];
428
429                             Set JavaDoc removedStrings = getRemovedStrings();
430
431                             if(((Boolean JavaDoc)value).booleanValue())
432                                 removedStrings.remove(hardString);
433                             else
434                                 removedStrings.add(hardString);
435                         }
436                     }
437                     break;
438                 case COLUMN_INDEX_KEY : {
439                     I18nString i18nString = (I18nString)stringMap.values().toArray()[rowIndex];
440                     i18nString.setKey(value.toString());
441                     if (!value.toString().equals(""))
442                         setValueAt(Boolean.TRUE, rowIndex, COLUMN_INDEX_CHECK);
443                     else
444                         setValueAt(Boolean.FALSE, rowIndex, COLUMN_INDEX_CHECK);
445                     break;
446                 }
447
448                 case COLUMN_INDEX_VALUE: {
449                     I18nString i18nString = (I18nString)stringMap.values().toArray()[rowIndex];
450                     i18nString.setValue(value.toString());
451                     if (!i18nString.getKey().equals("")) setValueAt(Boolean.TRUE, rowIndex, COLUMN_INDEX_CHECK);
452                     break;
453                 }
454             } // switch (columnIndex)
455

456             fireTableRowsUpdated(rowIndex, rowIndex);
457         }
458         
459         /** Overrides superclass method.
460          * @return DataObject.class */

461         public Class JavaDoc getColumnClass(int columnIndex) {
462             if(columnIndex == COLUMN_INDEX_CHECK)
463                 return Boolean JavaDoc.class;
464             else if(columnIndex == COLUMN_INDEX_HARDSTRING)
465                 return HardCodedString.class;
466             else
467                 return I18nString.class;
468         }
469
470         /** Overrides superclass method. */
471         public String JavaDoc getColumnName(int column) {
472             if(column == COLUMN_INDEX_HARDSTRING)
473                 return NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_HardString");
474             else if(column == COLUMN_INDEX_KEY)
475                 return NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Key");
476             else if(column == COLUMN_INDEX_VALUE)
477                 return NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Value");
478             else
479                 return " "; // NOI18N
480
}
481     } // End of ResourceTableModel nested class.
482

483
484     /** Cell editor for the right most 'customize' column. It shows dialog
485      * constructed from <code>PropertyPanel</code> which provides actual custmization of the
486      * <code>I18nString</code> instance.
487      * @see org.netbeans.modules.i18n.PropertyPanel
488      */

489     public static class CustomizeCellEditor extends AbstractCellEditor JavaDoc
490     implements TableCellEditor JavaDoc, ActionListener JavaDoc {
491
492         /** <code>I18nString</code> instance to be edited by this editor. */
493         private I18nString i18nString;
494         
495         /** Editor component, in our case <code>JButton</code>. */
496         private JButton JavaDoc editorComponent;
497
498         
499         /** Constructor. */
500         public CustomizeCellEditor() {
501             editorComponent = new JButton JavaDoc("..."); // NOI18N
502

503             editorComponent.addActionListener(new ActionListener JavaDoc() {
504                 public void actionPerformed(ActionEvent JavaDoc evt) {
505                     PropertyPanel panel = i18nString.getSupport().getPropertyPanel();
506                     I18nString clone = (I18nString) i18nString.clone();
507                     panel.setI18nString(i18nString);
508
509                     String JavaDoc title = Util.getString("PROP_cust_dialog_name");
510                     DialogDescriptor dd = new DialogDescriptor(panel, title);
511                     dd.setModal(true);
512                     dd.setOptionType(DialogDescriptor.DEFAULT_OPTION);
513                     
514                     Object JavaDoc options[] = new Object JavaDoc[] {
515                         DialogDescriptor.OK_OPTION,
516                         DialogDescriptor.CANCEL_OPTION,
517                     };
518                     dd.setOptions(options);
519                     //dd.setAdditionalOptions(new Object[0]);
520
dd.setHelpCtx(new HelpCtx(I18nUtil.PE_I18N_STRING_HELP_ID));
521                     dd.setButtonListener(CustomizeCellEditor.this);
522
523                     Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dd);
524                     dialog.setVisible(true);
525                     if (dd.getValue() == DialogDescriptor.CANCEL_OPTION) {
526                         i18nString.become(clone);
527                     }
528                 }
529             });
530         }
531
532         /** Implements <code>TableCellEditor</code> interface. */
533         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column) {
534             i18nString = (I18nString)value;
535             
536             return editorComponent;
537         }
538         
539         /** Implements <code>TableCellEditor</code> interface. */
540         public Object JavaDoc getCellEditorValue() {
541             return i18nString;
542         }
543
544         /** Implements <code>TableCellEditor</code> interface. */
545         public boolean isCellEditable(EventObject JavaDoc anEvent) {
546             if(anEvent instanceof MouseEvent JavaDoc) {
547                 // Counts needed to start editing.
548
return ((MouseEvent JavaDoc)anEvent).getClickCount() >= 1;
549             }
550             return true;
551         }
552
553         /** Implements <code>TableCellEditor</code> interface. */
554         public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
555             return true;
556         }
557
558         /** Implements <code>TableCellEditor</code> interface. */
559         public boolean stopCellEditing() {
560             fireEditingStopped();
561             return true;
562         }
563
564         /** Implements <code>TableCellEditor</code> interface. */
565         public void cancelCellEditing() {
566            fireEditingCanceled();
567         }
568         
569         /** Implements <code>ActionListener</code> interface. */
570         public void actionPerformed(ActionEvent JavaDoc evt) {
571             stopCellEditing();
572         }
573
574     }
575     
576     
577     /** <code>WizardDescriptor.Panel</code> used for <code>HardCodedStringPanel</code>.
578      * @see I18nWizardDescriptorPanel
579      * @see org.openide.WizardDescriptor.Panel*/

580     public static class Panel extends I18nWizardDescriptor.Panel
581     implements WizardDescriptor.FinishablePanel, I18nWizardDescriptor.ProgressMonitor {
582
583         /** Empty label component. */
584         private final JLabel JavaDoc emptyLabel;
585         
586         /** HardString panel component cache. */
587         private transient HardStringWizardPanel hardStringPanel;
588                 
589         public Panel() {
590             emptyLabel = new JLabel JavaDoc(NbBundle.getBundle(HardStringWizardPanel.class).getString("TXT_NoHardstrings"));
591             emptyLabel.setHorizontalAlignment(JLabel.CENTER);
592             emptyLabel.setVerticalAlignment(JLabel.CENTER);
593         }
594
595
596         /** Gets component to display. Implements superclass abstract method.
597          * @return this instance */

598         protected Component JavaDoc createComponent() {
599             JPanel JavaDoc panel = new JPanel JavaDoc();
600             
601             panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(HardStringWizardPanel.class).getString("ACS_HardStringWizardPanel"));
602             
603             Integer JavaDoc index;
604             
605             if(I18nUtil.getOptions().isAdvancedWizard())
606                 index = new Integer JavaDoc(3);
607             else
608                 index = new Integer JavaDoc(2);
609             
610             panel.putClientProperty("WizardPanel_contentSelectedIndex", index); // NOI18N
611
panel.setName(NbBundle.getBundle(HardStringWizardPanel.class).getString("TXT_ModifyStrings"));
612             panel.setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION);
613             panel.setLayout(new GridBagLayout JavaDoc());
614             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
615             constraints.weightx = 1.0;
616             constraints.weighty = 1.0;
617             constraints.fill = GridBagConstraints.BOTH;
618             panel.add(getUI(), constraints);
619             
620             return panel;
621         }
622
623         /** Gets if panel is valid. Overrides superclass method. */
624         public boolean isValid() {
625             return true;
626         }
627         
628         /**
629          */

630         public boolean isFinishPanel() {
631             return true;
632         }
633         
634         /** Reads settings at the start when the panel comes to play. Overrides superclass method. */
635         public void readSettings(Object JavaDoc settings) {
636         super.readSettings(settings);
637             getUI().setSourceMap(getMap());
638
639             JPanel JavaDoc panel = (JPanel JavaDoc)getComponent();
640             if(foundStrings(getMap())) {
641                 if(panel.isAncestorOf(emptyLabel)) {
642                     panel.remove(emptyLabel);
643                     GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
644                     constraints.weightx = 1.0;
645                     constraints.weighty = 1.0;
646                     constraints.fill = GridBagConstraints.BOTH;
647                     panel.add(getUI(), constraints);
648                 }
649             } else {
650                 if(panel.isAncestorOf(getUI())) {
651                     panel.remove(getUI());
652                     GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
653                     constraints.weightx = 1.0;
654                     constraints.weighty = 1.0;
655                     constraints.fill = GridBagConstraints.BOTH;
656                     panel.add(emptyLabel, constraints);
657                 }
658             }
659         }
660
661         /** Stores settings at the end of panel show. Overrides superclass method. */
662         public void storeSettings(Object JavaDoc settings) {
663         super.storeSettings(settings);
664             // Update sources.
665
getMap().clear();
666             getMap().putAll(getUI().getSourceMap());
667         }
668         
669         /** Searches hard coded strings in sources and puts found hard coded string - i18n string pairs
670          * into settings. Implements <code>ProgressMonitor</code> interface method. */

671         public void doLongTimeChanges() {
672             // do this only if there's anything to do
673
if (foundStrings(getMap())) {
674                 // Replace panel.
675
final ProgressWizardPanel progressPanel = new ProgressWizardPanel(true);
676                 progressPanel.setMainText(NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Internationalizing"));
677                 progressPanel.setMainProgress(0);
678
679                 ((Container JavaDoc)getComponent()).remove(getUI());
680                 GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
681                 constraints.weightx = 1.0;
682                 constraints.weighty = 1.0;
683                 constraints.fill = GridBagConstraints.BOTH;
684                 ((Container JavaDoc)getComponent()).add(progressPanel, constraints);
685                 ((JComponent JavaDoc)getComponent()).revalidate();
686                 getComponent().repaint();
687
688                 // Do replacement job here.
689
Map JavaDoc sourceMap = getUI().getSourceMap();
690
691                 Iterator JavaDoc sourceIterator = sourceMap.keySet().iterator();
692
693                 // For each source perform the task.
694
for(int i=0; sourceIterator.hasNext(); i++) {
695                     Object JavaDoc source = sourceIterator.next();
696
697                     // Get source data.
698
SourceData sourceData = (SourceData)sourceMap.get(source);
699
700                     // Get i18n support for this source.
701
I18nSupport support = sourceData.getSupport();
702
703                     // Get string map.
704
Map JavaDoc stringMap = sourceData.getStringMap();
705                     Object JavaDoc[] stringEntries = stringMap.entrySet().toArray();
706
707                     // Get removed strings.
708
Set JavaDoc removed = sourceData.getRemovedStrings();
709
710                     // Do actual replacement.
711
ClassPath cp = ClassPath.getClassPath( ((DataObject)source).getPrimaryFile(), ClassPath.SOURCE );
712                     progressPanel.setSubText(NbBundle.getBundle(HardStringWizardPanel.class).getString("LBL_Source")+" "+cp.getResourceName( ((DataObject)source).getPrimaryFile(), '.', false ) );
713
714                     for(int j=0; j < stringEntries.length; j++) {
715                         Map.Entry JavaDoc e = (Map.Entry JavaDoc) stringEntries[j];
716                         HardCodedString hcString = (HardCodedString) e.getKey();
717                         I18nString i18nString = (I18nString) e.getValue();
718
719                         if(removed != null && removed.contains(hcString))
720                             // Don't proceed.
721
continue;
722
723                         // Put new property into bundle.
724
support.getResourceHolder().addProperty(i18nString.getKey(), i18nString.getValue(), i18nString.getComment());
725
726                         // Replace string in source.
727
support.getReplacer().replace(hcString, i18nString);
728
729                         progressPanel.setSubProgress((int)((j+1)/(float)stringMap.size() * 100));
730                     } // End of inner for.
731

732                     // Provide additional changes if there are some.
733
if(support.hasAdditionalCustomizer()) {
734                         support.performAdditionalChanges();
735                     }
736
737                     progressPanel.setMainProgress((int)((i+1)/(float)sourceMap.size() * 100));
738                 } // End of outer for.
739
} // if (foundStrings(getMap()))
740
}
741         
742         /** Implements <code>ProgressMonitor</code> interface method. Does nothing. */
743         public void reset() {}
744         
745         /** Indicates if there were found some hardcoded strings in any of selected sources.
746          * @return true if at least one hard coded string was found. */

747         private static boolean foundStrings(Map JavaDoc sourceMap) {
748             Iterator JavaDoc it = sourceMap.keySet().iterator();
749
750             while(it.hasNext()) {
751                 SourceData sourceData = (SourceData)sourceMap.get(it.next());
752                 if(!sourceData.getStringMap().isEmpty())
753                     return true;
754             }
755
756             return false;
757         }
758         
759         /** Gets help. Implements superclass abstract method. */
760         public HelpCtx getHelp() {
761             return new HelpCtx(I18nUtil.HELP_ID_WIZARD);
762         }
763
764         private synchronized HardStringWizardPanel getUI() {
765             if (hardStringPanel == null) {
766                 hardStringPanel = new HardStringWizardPanel();
767             }
768             return hardStringPanel;
769         }
770         
771     } // End of nested Panel class.
772
}
773
Popular Tags