KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
25 import java.awt.Container JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.beans.BeanInfo JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JTable JavaDoc;
40 import javax.swing.table.AbstractTableModel JavaDoc;
41 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
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; // PENDING
52

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 /**
61  * Second panel of I18N Wizard.
62  *
63  * @author Peter Zavadsky
64  * @author Marian Petras
65  * @see Panel
66  */

67 final class ResourceWizardPanel extends JPanel JavaDoc {
68
69     /** Local copy of i18n wizard data. */
70     private final Map JavaDoc sourceMap = Util.createWizardSourceMap();
71
72     /** Table model for resourcesTable. */
73     private final ResourceTableModel tableModel = new ResourceTableModel();
74
75     /** This component panel wizard descriptor.
76      * @see org.openide.WizardDescriptor.Panel
77      * @see Panel */

78     private final Panel JavaDoc descPanel;
79
80     private final boolean testMode;
81     
82     /** Creates new form SourceChooserPanel. */
83     private ResourceWizardPanel(Panel JavaDoc descPanel, boolean testMode) {
84         this.descPanel = descPanel;
85         this.testMode = testMode;
86         
87         initComponents();
88         
89         initTable();
90         
91         initAccesibility();
92     }
93
94     
95     /** Getter for <code>resources</code> property. */
96     public Map JavaDoc getSourceMap() {
97         return sourceMap;
98     }
99     
100     /** Setter for <code>resources</code> property. */
101     public void setSourceMap(Map JavaDoc sourceMap) {
102         this.sourceMap.clear();
103         this.sourceMap.putAll(sourceMap);
104         
105         tableModel.fireTableDataChanged();
106        
107         descPanel.fireStateChanged();
108     }
109     
110     private String JavaDoc 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     /** Inits table component. */
119     private void initTable() {
120         resourcesTable.setDefaultRenderer(DataObject.class, new DefaultTableCellRenderer JavaDoc() {
121             public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
122                 boolean isSelected, boolean hasFocus, int row, int column) {
123                     
124                 JLabel JavaDoc label = (JLabel JavaDoc)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
125                 DataObject dataObject = (DataObject)value;
126
127                 if(dataObject != null) {
128                     String JavaDoc name = "N/A";
129                     if (column == 0) {
130                         // name for the first column, from sources
131
ClassPath cp = ClassPath.getClassPath(dataObject.getPrimaryFile(), ClassPath.SOURCE );
132                         name = cp.getResourceName( dataObject.getPrimaryFile(), '.', false );
133                     } else {
134                         // name for resource bundle, from execution,
135
// but the reference file must be the
136
// corresponding source
137
DataObject dob = (DataObject)tableModel.getValueAt(row, 0);
138                         name = Util.getResourceName(dob.getPrimaryFile(), dataObject.getPrimaryFile(), '.', false);
139                     }
140
141                     label.setText(name); // NOI18N
142
label.setIcon(new ImageIcon JavaDoc(dataObject.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16)));
143                 } else {
144                     label.setText(""); // NOI18N
145
label.setIcon(null);
146                 }
147                 
148                 return label;
149             }
150         });
151
152         Component JavaDoc cellSample = new DefaultTableCellRenderer JavaDoc()
153                                .getTableCellRendererComponent(
154                                     resourcesTable, //table
155
"N/A", //value //NOI18N
156
false, //isSelected
157
false, //hasFocus
158
0, 0); //row, column
159
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 JavaDoc() {
164             public void valueChanged(ListSelectionEvent JavaDoc 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     /** This method is called from within the constructor to
181      * initialize the form.
182      * WARNING: Do NOT modify this code. The content of this method is
183      * always regenerated by the Form Editor.
184      */

185     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
186
private void initComponents() {
187         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
188
189         descTextArea = new javax.swing.JTextArea JavaDoc();
190         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
191         resourcesTable = new javax.swing.JTable JavaDoc();
192         addAllButton = new javax.swing.JButton JavaDoc();
193         addButton = new javax.swing.JButton JavaDoc();
194
195         setLayout(new java.awt.GridBagLayout JavaDoc());
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 JavaDoc().getForeground());
203         descTextArea.setEnabled(false);
204         descTextArea.setOpaque(false);
205         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
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 JavaDoc(5, 0, 0, 0);
221         add(jScrollPane1, gridBagConstraints);
222
223         org.openide.awt.Mnemonics.setLocalizedText(addAllButton, NbBundle.getBundle(ResourceWizardPanel.class).getString("CTL_SelectResourceAll")); // NOI18N
224
addAllButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
225             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
226                 addAllButtonActionPerformed(evt);
227             }
228         });
229         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(5, 11, 0, 0);
235         add(addAllButton, gridBagConstraints);
236
237         org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle.getBundle(ResourceWizardPanel.class).getString("CTL_SelectResource")); // NOI18N
238
addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
239             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
240                 addButtonActionPerformed(evt);
241             }
242         });
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(5, 11, 0, 0);
250         add(addButton, gridBagConstraints);
251     }// </editor-fold>//GEN-END:initComponents
252

253     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
254
DataObject resource = selectResource();
255         
256         if(resource == null)
257             return;
258
259         int[] selectedRows = resourcesTable.getSelectedRows();
260
261         // Feed data.
262
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     }//GEN-LAST:event_addButtonActionPerformed
272

273     private void addAllButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addAllButtonActionPerformed
274
DataObject resource = selectResource();
275         
276         if(resource == null)
277             return;
278
279         // Feed data.
280
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     }//GEN-LAST:event_addAllButtonActionPerformed
290

291     /** Helper method. Gets user selected resource. */
292     private DataObject selectResource() {
293         Project prj = null;
294         FileObject fo = null;
295         Iterator JavaDoc 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     // Variables declaration - do not modify//GEN-BEGIN:variables
307
private javax.swing.JButton JavaDoc addAllButton;
308     private javax.swing.JButton JavaDoc addButton;
309     private javax.swing.JTextArea JavaDoc descTextArea;
310     private javax.swing.JScrollPane JavaDoc jScrollPane1;
311     private javax.swing.JTable JavaDoc resourcesTable;
312     // End of variables declaration//GEN-END:variables
313

314     /** Table model for this class. */
315     private class ResourceTableModel extends AbstractTableModel JavaDoc {
316         
317         /** Constructor. */
318         public ResourceTableModel() {
319         }
320         
321         
322         /** Implements superclass abstract method. */
323         public int getColumnCount() {
324             return 2;
325         }
326         
327         /** Implemenst superclass abstract method. */
328         public int getRowCount() {
329             return sourceMap.size();
330         }
331         
332         /** Implements superclass abstract method. */
333         public Object JavaDoc 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         /** Overrides superclass method.
345          * @return DataObject.class */

346         public Class JavaDoc getColumnClass(int columnIndex) {
347             return DataObject.class;
348         }
349
350         /** Overrides superclass method. */
351         public String JavaDoc 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     } // End of ResourceTableModel inner class.
358

359     
360     /** <code>WizardDescriptor.Panel</code> used for <code>ResourceChooserPanel</code>.
361      * @see I18nWizardDescriptorPanel
362      * @see org.openide.WizardDescriptor.Panel */

363     public static class Panel extends I18nWizardDescriptor.Panel implements I18nWizardDescriptor.ProgressMonitor {
364
365         /** Cached component. */
366         private transient ResourceWizardPanel resourcePanel;
367         
368         /** Indicates whether this panel is used in i18n test wizard or not. */
369         private boolean testWizard;
370
371
372         /** Constructs Panel for i18n wizard. */
373         public Panel() {
374             this(false);
375         }
376
377         /** Constructs panel for i18n wizard or i18n test wizard. */
378         public Panel(boolean testWizard) {
379             this.testWizard = testWizard;
380         }
381         
382         
383         /** Gets component to display. Implements superclass abstract method.
384          * @return this instance */

385         protected Component JavaDoc createComponent() {
386             JPanel JavaDoc panel = new JPanel JavaDoc();
387
388             // Accessibility
389
panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACS_ResourceWizardPanel"));
390             
391             panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(1)); // NOI18N
392
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 JavaDoc());
400             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
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         /** Indicates if panel is valid. Overrides superclass method. */
410         public boolean isValid() {
411             return !getUI().getSourceMap().containsValue(null);
412         }
413         
414         /** Reads settings at the start when the panel comes to play. Overrides superclass method. */
415         public void readSettings(Object JavaDoc settings) {
416         super.readSettings(settings);
417             getUI().setSourceMap(getMap());
418         }
419
420         /** Stores settings at the end of panel show. Overrides superclass abstract method. */
421         public void storeSettings(Object JavaDoc settings) {
422         super.storeSettings(settings);
423             // Update sources.
424
getMap().clear();
425             getMap().putAll(getUI().getSourceMap());
426         }
427         
428         /** Searches hard coded strings in sources and puts found hard coded string - i18n string pairs
429          * into settings. Implements <code>ProgressMonitor</code> interface. */

430         public void doLongTimeChanges() {
431             // Replace panel.
432
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             // Do search.
440
Map JavaDoc sourceMap = getUI().getSourceMap();
441
442             Iterator JavaDoc sourceIterator = sourceMap.keySet().iterator();
443
444             // For each source perform the task.
445
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 )); // NOI18N
451

452
453                 // retrieve existing sourcedata -- will provide the resource for the new instance
454
SourceData sourceData = (SourceData)sourceMap.get(source);
455                 
456                 // prepare new sourcedata
457
// Get i18n support for this source.
458
I18nSupport support;
459                 try {
460                     support = FactoryRegistry.getFactory(source.getClass()).create(source);
461                 } catch(IOException JavaDoc ioe) {
462                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
463                     // Remove source from settings.
464
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 ) ); // NOI18N
473

474                 // Get string map.
475
Map JavaDoc stringMap = sourceData.getStringMap();
476
477                 HardCodedString[] foundStrings;
478                 
479                 if(testWizard) {
480                     // Find all i18n-zied hard coded strings in the source.
481
foundStrings = support.getFinder().findAllI18nStrings();
482                 } else {
483                     // Find all non-i18-ized hard coded strings in the source.
484
foundStrings = support.getFinder().findAllHardCodedStrings();
485                 }
486
487                 if(foundStrings == null) {
488                     // Set empty map.
489
sourceData.setStringMap(new HashMap JavaDoc(0));
490                     continue;
491                 }
492
493                 Map JavaDoc map = new HashMap JavaDoc(foundStrings.length);
494
495                 // Put hard coded string - i18n pairs into map.
496
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             } // End of outer for.
507
}
508         
509         /** Helper method. Places progress panel for monitoring search. */
510         private void showProgressPanel(ProgressWizardPanel progressPanel) {
511             ((Container JavaDoc)getComponent()).remove(getUI());
512             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
513             constraints.weightx = 1.0;
514             constraints.weighty = 1.0;
515             constraints.fill = GridBagConstraints.BOTH;
516             ((Container JavaDoc)getComponent()).add(progressPanel, constraints);
517             ((JComponent JavaDoc)getComponent()).revalidate();
518             getComponent().repaint();
519         }
520         
521         /** Resets panel back after monitoring search. Implements <code>ProgressMonitor</code> interface. */
522         public void reset() {
523             Container JavaDoc container = (Container JavaDoc)getComponent();
524             
525             if(!container.isAncestorOf(getUI())) {
526                 container.removeAll();
527                 GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
528                 constraints.weightx = 1.0;
529                 constraints.weighty = 1.0;
530                 constraints.fill = GridBagConstraints.BOTH;
531                 container.add(getUI(), constraints);
532             }
533         }
534         
535         /** Gets help. Implements superclass abstract method. */
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     } // End of nested Panel class.
551

552 }
553
Popular Tags