KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.BeanInfo JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.swing.DefaultListCellRenderer JavaDoc;
29 import javax.swing.event.ListSelectionEvent JavaDoc;
30 import javax.swing.event.ListSelectionListener JavaDoc;
31 import javax.swing.ImageIcon JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JList JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import org.netbeans.api.java.classpath.ClassPath;
36 import org.netbeans.api.project.Project;
37
38 import org.netbeans.modules.i18n.FactoryRegistry;
39 import org.netbeans.modules.i18n.I18nUtil;
40
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43 import org.openide.nodes.Node;
44 import org.openide.nodes.NodeAcceptor;
45 import org.openide.nodes.NodeOperation;
46 import org.openide.util.HelpCtx;
47 import org.openide.util.NbBundle;
48 import org.openide.util.UserCancelException;
49 import org.netbeans.modules.i18n.SelectorUtils;
50
51 /**
52  * First panel used in I18N (test) Wizard.
53  *
54  * @author Peter Zavadsky
55  * @see Panel
56  */

57 final class SourceWizardPanel extends JPanel JavaDoc {
58
59     /** Sources selected by user. */
60     private final Map JavaDoc sourceMap = Util.createWizardSourceMap();
61     
62     /** This component panel wizard descriptor.
63      * @see org.openide.WizardDescriptor.Panel
64      * @see Panel */

65     private final Panel JavaDoc descPanel;
66
67     /**
68      * Panel role true (test wizard) false (i18n) wizard
69      */

70     private boolean testRole = false;
71     
72     /** Creates new form SourceChooserPanel.
73      * @param it's panel wizard descriptor */

74     private SourceWizardPanel(Panel JavaDoc descPanel, boolean testRole) {
75         this.descPanel = descPanel;
76         this.testRole = testRole;
77         
78         initComponents();
79
80         initAccessibility ();
81         
82         setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION);
83         
84         initList();
85         
86         putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(0)); // NOI18N
87

88         if (testRole) {
89             setName(Util.getString("TXT_SelecTestSources"));
90         } else {
91             setName(Util.getString("TXT_SelectSources"));
92         }
93     }
94     
95
96     /** Getter for <code>sources</code> property. */
97     public Map JavaDoc getSourceMap() {
98         return sourceMap;
99     }
100     
101     /** Setter for <code>sources</code> property. */
102     public void setSourceMap(Map JavaDoc sourceMap) {
103         this.sourceMap.clear();
104         this.sourceMap.putAll(sourceMap);
105         
106         sourcesList.setListData(sourceMap.keySet().toArray());
107         
108         descPanel.fireStateChanged();
109     }
110
111     /**
112      * Panel description depend of its container test or i18n role
113      */

114     private String JavaDoc getPanelDescription() {
115         if (testRole == false) {
116             return Util.getString("MSG_SourcesPanel_desc");
117         } else {
118             return Util.getString("MSG_SourcesPanel_test_desc");
119         }
120     }
121
122     /**
123      * Accessible panel description depends of its container test or i18n role
124      */

125     private String JavaDoc getAccessibleListDescription() {
126         if (testRole == false) {
127             return Util.getString("ACSD_sourcesList");
128         } else {
129             return Util.getString("ACSD_sourcesList_test");
130         }
131     }
132     
133     
134     /**
135      * List content drives remove button enableness.
136      */

137     private void initList() {
138         sourcesList.getSelectionModel().addListSelectionListener(
139             new ListSelectionListener JavaDoc() {
140                 public void valueChanged(ListSelectionEvent JavaDoc evt) {
141                     removeButton.setEnabled(!sourcesList.isSelectionEmpty());
142                 }
143             }
144         );
145         
146         removeButton.setEnabled(!sourcesList.isSelectionEmpty());
147     }
148     
149     private void initAccessibility() {
150         getAccessibleContext().setAccessibleDescription(getPanelDescription());
151         
152         addButton.setToolTipText(Util.getString("CTL_AddSource_desc"));
153         
154         removeButton.setToolTipText(Util.getString("CTL_RemoveSource_desc"));
155         
156         sourcesList.getAccessibleContext().setAccessibleName(Util.getString("ACSN_sourcesList"));
157         sourcesList.getAccessibleContext().setAccessibleDescription(getAccessibleListDescription());
158     }
159     
160     /** This method is called from within the constructor to
161      * initialize the form.
162      * WARNING: Do NOT modify this code. The content of this method is
163      * always regenerated by the Form Editor.
164      */

165     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
166
private void initComponents() {
167         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
168
169         descTextArea = new javax.swing.JTextArea JavaDoc();
170         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
171         sourcesList = new javax.swing.JList JavaDoc();
172         addButton = new javax.swing.JButton JavaDoc();
173         removeButton = new javax.swing.JButton JavaDoc();
174
175         setLayout(new java.awt.GridBagLayout JavaDoc());
176
177         descTextArea.setEditable(false);
178         descTextArea.setLineWrap(true);
179         descTextArea.setText(getPanelDescription());
180         descTextArea.setWrapStyleWord(true);
181         descTextArea.setDisabledTextColor(new JLabel JavaDoc().getForeground());
182         descTextArea.setEnabled(false);
183         descTextArea.setOpaque(false);
184         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
185         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
186         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
187         add(descTextArea, gridBagConstraints);
188
189         sourcesList.setCellRenderer(new DataObjectListCellRenderer());
190         jScrollPane1.setViewportView(sourcesList);
191
192         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193         gridBagConstraints.gridx = 0;
194         gridBagConstraints.gridy = 1;
195         gridBagConstraints.gridheight = 2;
196         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
197         gridBagConstraints.weightx = 1.0;
198         gridBagConstraints.weighty = 1.0;
199         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
200         add(jScrollPane1, gridBagConstraints);
201
202         org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle.getBundle(SourceWizardPanel.class).getString("CTL_AddSource")); // NOI18N
203
addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
204             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
205                 addButtonActionPerformed(evt);
206             }
207         });
208         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
209         gridBagConstraints.gridx = 1;
210         gridBagConstraints.gridy = 1;
211         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
212         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
213         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 11, 0, 0);
214         add(addButton, gridBagConstraints);
215
216         org.openide.awt.Mnemonics.setLocalizedText(removeButton, NbBundle.getBundle(SourceWizardPanel.class).getString("CTL_RemoveSource")); // NOI18N
217
removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
218             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
219                 removeButtonActionPerformed(evt);
220             }
221         });
222         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
223         gridBagConstraints.gridx = 1;
224         gridBagConstraints.gridy = 2;
225         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
226         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
227         gridBagConstraints.weighty = 1.0;
228         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 11, 0, 0);
229         add(removeButton, gridBagConstraints);
230     }// </editor-fold>//GEN-END:initComponents
231

232     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
233
Object JavaDoc[] selected = sourcesList.getSelectedValues();
234         
235         for(int i=0; i<selected.length; i++) {
236             sourceMap.remove(selected[i]);
237         }
238
239         sourcesList.setListData(sourceMap.keySet().toArray());
240         
241         descPanel.fireStateChanged();
242     }//GEN-LAST:event_removeButtonActionPerformed
243

244     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
245

246         // take actual project from first data object
247

248         Project prj = descPanel.getProject();
249   
250         // Selects source data objects which could be i18n-ized.
251
try {
252             Node[] selectedNodes= NodeOperation.getDefault().select(
253                 Util.getString("LBL_SelectSources"),
254                 Util.getString("LBL_Filesystems"),
255                 SelectorUtils.sourcesNode(prj, SelectorUtils.ALL_FILTER),
256                 new NodeAcceptor() {
257                     public boolean acceptNodes(Node[] nodes) {
258                         if(nodes == null || nodes.length == 0) {
259                             return false;
260                         }
261
262                         for(int i=0; i<nodes.length; i++) {
263                             // Has to be data object.
264
Object JavaDoc dataObject = nodes[i].getCookie(DataObject.class);
265                             if (dataObject == null) {
266                                 return false;
267                             }
268                             // if it is folder and constains some our data object.
269
if (dataObject instanceof DataFolder) {
270                                 if (I18nUtil.containsAcceptedDataObject((DataFolder) dataObject)) {
271                                     return true;
272                                 }
273                             } else if (FactoryRegistry.hasFactory(dataObject.getClass())) {
274                                 // Has to have registered i18n factory for that data object class name.
275
return true;
276                             }
277                         }
278                         
279                         return false;
280                     }
281                 }
282             );
283             
284             for(int i=0; i<selectedNodes.length; i++) {
285                 DataObject dataObject = (DataObject)selectedNodes[i].getCookie(DataObject.class);
286
287                 if (dataObject instanceof DataFolder) {
288                     // recursively add folder content
289
Iterator JavaDoc it = I18nUtil.getAcceptedDataObjects((DataFolder)dataObject).iterator();
290                     while (it.hasNext()) {
291                         Util.addSource(sourceMap, (DataObject)it.next());
292                     }
293                 } else {
294                     Util.addSource(sourceMap, dataObject);
295                 }
296             }
297             
298             sourcesList.setListData(sourceMap.keySet().toArray());
299            
300             descPanel.fireStateChanged();
301         } catch (UserCancelException uce) {
302            // ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, uce);
303
// nobody is interested in the message
304
}
305     }//GEN-LAST:event_addButtonActionPerformed
306

307     // Variables declaration - do not modify//GEN-BEGIN:variables
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.JButton JavaDoc removeButton;
312     private javax.swing.JList JavaDoc sourcesList;
313     // End of variables declaration//GEN-END:variables
314

315
316     /** List cell rendrerer which uses data object as values. */
317     public static class DataObjectListCellRenderer extends DefaultListCellRenderer JavaDoc {
318         public Component JavaDoc getListCellRendererComponent(
319         JList JavaDoc list,
320         Object JavaDoc value, // value to display
321
int index, // cell index
322
boolean isSelected, // is the cell selected
323
boolean cellHasFocus) // the list and the cell have the focus
324
{
325             JLabel JavaDoc label = (JLabel JavaDoc)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
326
327             DataObject dataObject = (DataObject)value;
328
329             if(dataObject != null) {
330                 ClassPath cp = ClassPath.getClassPath( dataObject.getPrimaryFile(), ClassPath.SOURCE );
331                                 
332                 label.setText(cp.getResourceName( dataObject.getPrimaryFile(), '.', false )); // NOI18N
333
label.setIcon(new ImageIcon JavaDoc(dataObject.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16)));
334             } else {
335                 label.setText(""); // NOI18N
336
label.setIcon(null);
337             }
338
339             return label;
340         }
341     }
342
343     
344     
345     /** <code>WizardDescriptor.Panel</code> used for <code>SourceChooserPanel</code>.
346      * @see I18nWizardDescriptorPanel
347      * @see org.openide.WizardDescriptor.Panel */

348     public static class Panel extends I18nWizardDescriptor.Panel {
349
350         /** Test wizard flag. */
351         private final boolean testWizard;
352         
353         
354         /** Constructor for i18n wizard. */
355         public Panel() {
356             this(false);
357         }
358         
359         /** Constructor for specified i18n wizard. */
360         public Panel(boolean testWizard) {
361             this.testWizard = testWizard;
362         }
363         
364         
365         /** Gets component to display. Implements superclass abstract method.
366          * @return this instance */

367         protected Component JavaDoc createComponent() {
368             Component JavaDoc component = new SourceWizardPanel(this, testWizard);
369             
370             return component;
371         }
372
373         /** Gets if panel is valid. Overrides superclass method. */
374         public boolean isValid() {
375             return !((SourceWizardPanel)getComponent()).getSourceMap().isEmpty();
376         }
377         
378         /** Reads settings at the start when the panel comes to play. Overrides superclass method. */
379         public void readSettings(Object JavaDoc settings) {
380       super.readSettings(settings);
381       ((SourceWizardPanel)getComponent()).setSourceMap(getMap());
382         }
383
384         /** Stores settings at the end of panel show. Overrides superclass method. */
385         public void storeSettings(Object JavaDoc settings) {
386       super.storeSettings(settings);
387         super.storeSettings(settings);
388             // Update sources.
389
getMap().clear();
390             getMap().putAll(((SourceWizardPanel)getComponent()).getSourceMap());
391         }
392         
393         /** Gets help. Implements superclass abstract method. */
394         public HelpCtx getHelp() {
395             if(testWizard)
396                 return new HelpCtx(I18nUtil.HELP_ID_TESTING);
397             else
398                 return new HelpCtx(I18nUtil.HELP_ID_WIZARD);
399         }
400
401     } // End of nested Panel class.
402

403 }
404
Popular Tags