KickJava   Java API By Example, From Geeks To Geeks.

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


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.GridBagConstraints JavaDoc;
28 import java.awt.GridBagLayout JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.swing.DefaultComboBoxModel JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import org.netbeans.api.java.classpath.ClassPath;
40
41 import org.netbeans.modules.i18n.HardCodedString;
42 import org.netbeans.modules.i18n.I18nSupport;
43 import org.netbeans.modules.i18n.I18nUtil;
44
45 import org.openide.loaders.DataObject;
46 import org.openide.util.HelpCtx;
47 import org.openide.util.NbBundle;
48 import org.openide.WizardDescriptor;
49
50
51 /**
52  * <code>WizardDescriptor.Panel</code> used provide additional values modifying if avialble.
53  * E.g. typically offers creation resource field or method in source used by replacing code.
54  * It is the third panel of I18N Wizard.
55  *
56  * @author Peter Zavadsky
57  * @see Panel
58  */

59 final class AdditionalWizardPanel extends JPanel JavaDoc {
60
61     /** Local copy of i18n wizard data. */
62     private final Map JavaDoc sourceMap = Util.createWizardSourceMap();
63     
64     /** Sources on which additional modifications coudl happen in this panel. */
65     private final Set JavaDoc viewedSources = new HashSet JavaDoc(0);
66
67     /** Additional component. */
68     private JComponent JavaDoc additionalComponent = EMPTY_COMPONENT;
69     
70     /** Empty component to show when no additional values are supported. */
71     private static final JLabel JavaDoc EMPTY_COMPONENT =
72                          new JLabel JavaDoc(Util.getString("TXT_HasNoAdditonal"));
73     
74     
75     /** Creates new form HardCodedStringsPanel */
76     private AdditionalWizardPanel() {
77         initComponents();
78         initA11Y();
79         
80         // set customized model
81
setComboModel(sourceMap);
82     }
83
84     /** Does additional init of components. */
85     private void initA11Y() {
86         sourceLabel.setLabelFor(sourceCombo);
87         sourceCombo.getAccessibleContext().setAccessibleDescription(Util.getString("ACS_sourceCombo"));
88     }
89     
90     
91     /** Sets combo model only for source which support provides additional customizing. */
92     private void setComboModel(Map JavaDoc sourceMap) {
93         Object JavaDoc[] sources = sourceMap.keySet().toArray();
94         
95         ArrayList JavaDoc nonEmptySources = new ArrayList JavaDoc();
96
97         for (int i = 0; i < sources.length; i++) {
98             if(((SourceData)sourceMap.get(sources[i])).getSupport().hasAdditionalCustomizer())
99                 nonEmptySources.add(sources[i]);
100         }
101         
102         sourceCombo.setModel(new DefaultComboBoxModel JavaDoc(nonEmptySources.toArray()));
103         
104         // update view
105
Object JavaDoc selected = sourceCombo.getSelectedItem();
106         updateAdditionalComponent(selected);
107         
108     }
109
110     /** Getter for <code>viewedSources</code> property. */
111     Set JavaDoc getViewedSources() {
112         return viewedSources;
113     }
114
115     /** Getter for <code>sourceMap</code> property. */
116     Map JavaDoc getSourceMap() {
117         return sourceMap;
118     }
119     
120     /** Setter for <code>resources</code> property. */
121     void setSourceMap(Map JavaDoc sourceMap) {
122         this.sourceMap.clear();
123         this.sourceMap.putAll(sourceMap);
124         
125         setComboModel(sourceMap);
126     }
127         
128     
129     /** This method is called from within the constructor to
130      * initialize the form.
131      * WARNING: Do NOT modify this code. The content of this method is
132      * always regenerated by the Form Editor.
133      */

134     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
135
private void initComponents() {
136         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
137
138         sourceLabel = new javax.swing.JLabel JavaDoc();
139         sourceCombo = new javax.swing.JComboBox JavaDoc();
140
141         setLayout(new java.awt.GridBagLayout JavaDoc());
142
143         org.openide.awt.Mnemonics.setLocalizedText(sourceLabel, NbBundle.getBundle(AdditionalWizardPanel.class).getString("LBL_Source")); // NOI18N
144
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146         add(sourceLabel, gridBagConstraints);
147
148         sourceCombo.setRenderer(new SourceWizardPanel.DataObjectListCellRenderer());
149         sourceCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
150             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
151                 sourceComboActionPerformed(evt);
152             }
153         });
154         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
155         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
156         gridBagConstraints.weightx = 1.0;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 11, 0, 0);
158         add(sourceCombo, gridBagConstraints);
159     }// </editor-fold>//GEN-END:initComponents
160

161     private void sourceComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_sourceComboActionPerformed
162
Object JavaDoc selected = sourceCombo.getSelectedItem();
163         updateAdditionalComponent(selected);
164     }//GEN-LAST:event_sourceComboActionPerformed
165

166     private void updateAdditionalComponent(Object JavaDoc selected) {
167         
168         I18nSupport support = null;
169         
170         if (selected != null) {
171             support = ((SourceData)sourceMap.get(selected)).getSupport();
172         }
173
174         // remove last one
175
remove(additionalComponent);
176         
177         if(support != null && support.hasAdditionalCustomizer()) {
178             additionalComponent = support.getAdditionalCustomizer();
179             viewedSources.add(selected);
180         } else {
181             additionalComponent = EMPTY_COMPONENT;
182         }
183
184         // add it
185
GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
186         gridBagConstraints.gridx = 0;
187         gridBagConstraints.gridy = 1;
188         gridBagConstraints.gridwidth = 2;
189         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
190         gridBagConstraints.weightx = 1.0;
191         gridBagConstraints.weighty = 1.0;
192         add(additionalComponent, gridBagConstraints);
193         
194         revalidate();
195     }
196     
197     
198     // Variables declaration - do not modify//GEN-BEGIN:variables
199
private javax.swing.JComboBox JavaDoc sourceCombo;
200     private javax.swing.JLabel JavaDoc sourceLabel;
201     // End of variables declaration//GEN-END:variables
202

203     /** <code>WizardDescriptor.Panel</code> used for <code>AdditionalPanel</code>.
204      * @see org.openide.WizardDescriptor.Panel*/

205     public static class Panel extends I18nWizardDescriptor.Panel {
206
207         /** Empty label component. */
208         private final JLabel JavaDoc emptyLabel;
209         
210         /** Component. */
211         private transient AdditionalWizardPanel additionalPanel;
212
213         Panel () {
214             emptyLabel = new JLabel JavaDoc(Util.getString("TXT_HasNoAdditonal"));
215             emptyLabel.setHorizontalAlignment(JLabel.CENTER);
216             emptyLabel.setVerticalAlignment(JLabel.CENTER);
217         }
218         
219         
220         /** Gets component to display. Implements superclass abstract method.
221          * @return <code>AdditionalPanel</code> instance */

222         protected Component JavaDoc createComponent() {
223             JPanel JavaDoc panel = new JPanel JavaDoc();
224             
225             //Accessibility
226
panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(AdditionalWizardPanel.class).getString("ACS_AdditionalWizardPanel"));
227             
228             panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(2)); // NOI18N
229
panel.setName(NbBundle.getBundle(getClass()).getString("TXT_ModifyAdditional"));
230             panel.setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION);
231
232             panel.setLayout(new GridBagLayout JavaDoc());
233             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
234             constraints.weightx = 1.0;
235             constraints.weighty = 1.0;
236             constraints.fill = GridBagConstraints.BOTH;
237             panel.add(getUI(), constraints);
238             
239             return panel;
240         }
241
242         /** Reads settings at the start when the panel comes to play. Overrides superclass method. */
243         public void readSettings(Object JavaDoc settings) {
244         super.readSettings(settings);
245             getUI().setSourceMap(getMap());
246             
247             JPanel JavaDoc panel = (JPanel JavaDoc)getComponent();
248             if(hasAdditional(getMap())) {
249                 if(panel.isAncestorOf(emptyLabel)) {
250                     panel.remove(emptyLabel);
251                     GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
252                     constraints.weightx = 1.0;
253                     constraints.weighty = 1.0;
254                     constraints.fill = GridBagConstraints.BOTH;
255                     panel.add(getUI(), constraints);
256                 }
257             } else {
258                 if(panel.isAncestorOf(getUI())) {
259                     panel.remove(getUI());
260                     GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
261                     constraints.weightx = 1.0;
262                     constraints.weighty = 1.0;
263                     constraints.fill = GridBagConstraints.BOTH;
264                     panel.add(emptyLabel, constraints);
265                 }
266             }
267         }
268
269         /** Stores settings at the end of panel show. Overrides superclass abstract method. */
270         public void storeSettings(Object JavaDoc settings) {
271         super.storeSettings(settings);
272             // Alter i18n string values if changing additional values could affect them.
273
Map JavaDoc sourceMap = getUI().getSourceMap();
274             Iterator JavaDoc it = getUI().getViewedSources().iterator();
275             
276             while(it.hasNext()) {
277                 SourceData sourceData = (SourceData)sourceMap.get(it.next());
278                 
279                 Object JavaDoc[] hcStrings = sourceData.getStringMap().keySet().toArray();
280                 
281                 for(int i=0; i<hcStrings.length; i++) {
282                     // Actual replacing of default values.
283
sourceData.getStringMap().put(hcStrings[i], sourceData.getSupport().getDefaultI18nString((HardCodedString)hcStrings[i]));
284                 }
285             }
286             
287             // Update sources.
288
getMap().clear();
289             getMap().putAll(sourceMap);
290         }
291         
292         /** Searches hard coded strings in sources and puts found hard coded string - i18n string pairs
293          * into settings. Implements <code>ProgressMonitor</code> interface method. */

294         public void doLongTimeChanges() {
295             // Replace panel.
296
ProgressWizardPanel progressPanel = new ProgressWizardPanel(true);
297             
298             showProgressPanel(progressPanel);
299             
300             progressPanel.setMainText(NbBundle.getBundle(getClass()).getString("LBL_AdditionalIn"));
301             progressPanel.setMainProgress(0);
302             
303             // Alter i18n string values if changing additional values could affect them.
304
Map JavaDoc sourceMap = ((AdditionalWizardPanel)getComponent()).getSourceMap();
305             Iterator JavaDoc it = ((AdditionalWizardPanel)getComponent()).getViewedSources().iterator();
306             
307             for(int i=0; it.hasNext(); i++) {
308                 DataObject source = (DataObject)it.next();
309                 
310                 SourceData sourceData = (SourceData)sourceMap.get(source);
311                 
312                 ClassPath cp = ClassPath.getClassPath( source.getPrimaryFile(), ClassPath.SOURCE );
313
314                 progressPanel.setMainText(NbBundle.getBundle(getClass()).getString("LBL_AdditionalIn")+" "+cp.getResourceName( source.getPrimaryFile(), '.', false ) ); // NOI18N
315

316                 Object JavaDoc[] hcStrings = sourceData.getStringMap().keySet().toArray();
317                 
318                 for(int j=0; i<hcStrings.length; j++) {
319                     // Actual replacing of default values.
320
sourceData.getStringMap().put(hcStrings[j], sourceData.getSupport().getDefaultI18nString((HardCodedString)hcStrings[j]));
321                 } // End of inner for.
322

323                 progressPanel.setMainProgress((int)((i+1)/(float)sourceMap.size() * 100));
324             } // End of outer for.
325
}
326
327         /** Helper method. Places progress panel for monitoring search. */
328         private void showProgressPanel(ProgressWizardPanel progressPanel) {
329             ((Container JavaDoc)getComponent()).remove(getUI());
330             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
331             constraints.weightx = 1.0;
332             constraints.weighty = 1.0;
333             constraints.fill = GridBagConstraints.BOTH;
334             ((Container JavaDoc)getComponent()).add(progressPanel, constraints);
335             ((JComponent JavaDoc)getComponent()).revalidate();
336             getComponent().repaint();
337         }
338         
339         /** Resets panel back after monitoring search. Implements <code>ProgressMonitor</code> interface method. */
340         public void reset() {
341             Container JavaDoc container = (Container JavaDoc)getComponent();
342             
343             if(!container.isAncestorOf(getUI())) {
344                 container.removeAll();
345                 GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
346                 constraints.weightx = 1.0;
347                 constraints.weighty = 1.0;
348                 constraints.fill = GridBagConstraints.BOTH;
349                 container.add(getUI(), constraints);
350             }
351         }
352         
353         /** Gets help. Implements superclass abstract method. */
354         public HelpCtx getHelp() {
355             return new HelpCtx(I18nUtil.HELP_ID_WIZARD);
356         }
357         
358         /** Indicates if there are additional customizers in any of selected sources.
359          * @return true if at least one source suport has additional customizer. */

360         private static boolean hasAdditional(Map JavaDoc sourceMap) {
361             Iterator JavaDoc it = sourceMap.keySet().iterator();
362
363             while(it.hasNext()) {
364                 SourceData sourceData = (SourceData)sourceMap.get(it.next());
365                 if(sourceData.getSupport().hasAdditionalCustomizer())
366                     return true;
367             }
368
369             return false;
370         }
371         
372         private synchronized AdditionalWizardPanel getUI() {
373             if (additionalPanel == null) {
374                 additionalPanel = new AdditionalWizardPanel();
375             }
376             return additionalPanel;
377         }
378     } // End of nested Panel class.
379

380 }
381
Popular Tags