KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > wizard > SelectorPanel


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.platform.wizard;
21
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ItemListener JavaDoc;
26 import java.util.IdentityHashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.swing.ButtonGroup JavaDoc;
32 import javax.swing.ButtonModel JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JRadioButton JavaDoc;
36 import javax.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import org.netbeans.modules.java.platform.InstallerRegistry;
39 import org.netbeans.spi.java.platform.CustomPlatformInstall;
40 import org.netbeans.spi.java.platform.GeneralPlatformInstall;
41 import org.openide.WizardDescriptor;
42 import org.openide.loaders.TemplateWizard;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author Tomas Zezula
49  */

50 class SelectorPanel extends javax.swing.JPanel JavaDoc implements ItemListener JavaDoc {
51         
52     private Map JavaDoc installersByButtonModels = new IdentityHashMap JavaDoc ();
53     private ButtonGroup JavaDoc group;
54     private SelectorPanel.Panel firer;
55     
56     /** Creates new form SelectorPanel */
57     public SelectorPanel(SelectorPanel.Panel firer) {
58         this.firer = firer;
59         initComponents();
60         postInitComponents ();
61         this.setName (NbBundle.getMessage(SelectorPanel.class,"TXT_SelectPlatformTypeTitle"));
62         this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SelectorPanel.class,"AD_SelectPlatformType"));
63     }
64     
65     private void postInitComponents () {
66         InstallerRegistry regs = InstallerRegistry.getDefault();
67         List JavaDoc/*<GeneralPlatformInstall>*/ installers = regs.getAllInstallers();
68         this.group = new ButtonGroup JavaDoc ();
69         JLabel JavaDoc label = new JLabel JavaDoc (NbBundle.getMessage(SelectorPanel.class,"TXT_SelectPlatform"));
70         label.setDisplayedMnemonic(NbBundle.getMessage(SelectorPanel.class,"AD_SelectPlatform").charAt(0));
71         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc ();
72         c.gridx = c.gridy = GridBagConstraints.RELATIVE;
73         c.gridheight = 1;
74         c.gridwidth = GridBagConstraints.REMAINDER;
75         c.fill = GridBagConstraints.HORIZONTAL;
76         c.anchor = GridBagConstraints.NORTHWEST;
77         c.weightx = 1.0;
78         c.insets = new Insets JavaDoc (12, 12, 6, 12);
79         ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(label,c);
80         this.add (label);
81         Iterator JavaDoc/*<GeneralPlatformInstall>*/ it = installers.iterator();
82         for (int i=0; it.hasNext(); i++) {
83             GeneralPlatformInstall pi = (GeneralPlatformInstall) it.next ();
84             JRadioButton JavaDoc button = new JRadioButton JavaDoc (pi.getDisplayName());
85             if (i==0) {
86                 label.setLabelFor(button);
87             }
88             button.addItemListener(this);
89             this.installersByButtonModels.put (button.getModel(), pi);
90             this.group.add(button);
91             c = new GridBagConstraints JavaDoc ();
92             c.gridx = c.gridy = GridBagConstraints.RELATIVE;
93             c.gridheight = 1;
94             c.gridwidth = GridBagConstraints.REMAINDER;
95             c.fill = GridBagConstraints.HORIZONTAL;
96             c.anchor = GridBagConstraints.NORTHWEST;
97             c.weightx = 1.0;
98             c.insets = new Insets JavaDoc (6, 18, it.hasNext()? 0 : 12, 12);
99             ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(button,c);
100             this.add (button);
101         }
102         JPanel JavaDoc pad = new JPanel JavaDoc ();
103         c = new GridBagConstraints JavaDoc ();
104         c.gridx = c.gridy = GridBagConstraints.RELATIVE;
105         c.gridheight = 1;
106         c.gridwidth = GridBagConstraints.REMAINDER;
107         c.fill = GridBagConstraints.BOTH;
108         c.anchor = GridBagConstraints.NORTHWEST;
109         c.weightx = 1.0;
110         c.weighty = 1.0;
111         c.insets = new Insets JavaDoc (12,0,0,12);
112         ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(pad,c);
113         this.add (pad);
114     }
115     
116     private void readSettings () {
117         if (this.group.getSelection()==null) {
118             java.util.Enumeration JavaDoc buttonEnum = this.group.getElements();
119             assert buttonEnum.hasMoreElements();
120             ((JRadioButton JavaDoc)buttonEnum.nextElement()).setSelected(true);
121         }
122     }
123
124     public void itemStateChanged(java.awt.event.ItemEvent JavaDoc e) {
125         this.firer.fireChange();
126     }
127     
128     
129     /**
130      * Used by unit tests
131      * Select the GeneralPlatformInstall to allow step over this panel
132      */

133     boolean selectInstaller (GeneralPlatformInstall install) {
134         assert install != null;
135         for (Iterator JavaDoc/*<Map.Entry<ButtonModel, GeneralPlatformInstall>>*/ it = this.installersByButtonModels.entrySet().iterator(); it.hasNext();) {
136             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
137             if (entry.getValue().equals(install)) {
138                 ButtonModel JavaDoc model = (ButtonModel JavaDoc) entry.getKey();
139                 model.setSelected(true);
140                 return true;
141             }
142         }
143         return false;
144     }
145     
146     /** This method is called from within the constructor to
147      * initialize the form.
148      * WARNING: Do NOT modify this code. The content of this method is
149      * always regenerated by the Form Editor.
150      */

151     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
152
private void initComponents() {
153
154         setLayout(new java.awt.GridBagLayout JavaDoc());
155
156     }
157     // </editor-fold>//GEN-END:initComponents
158

159     
160     // Variables declaration - do not modify//GEN-BEGIN:variables
161
// End of variables declaration//GEN-END:variables
162

163     
164     public static class Panel implements WizardDescriptor.Panel {
165         
166         private List JavaDoc/*<ChangeListener>*/ listeners;
167         private SelectorPanel component;
168         
169         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
170             assert l != null;
171             if (this.listeners == null) {
172                 return;
173             }
174             this.listeners.remove(l);
175         }
176
177         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
178             assert l != null;
179             if (this.listeners == null) {
180                 this.listeners = new ArrayList JavaDoc ();
181             }
182             this.listeners.add(l);
183         }
184
185         public void readSettings(Object JavaDoc settings) {
186             ((SelectorPanel)this.getComponent()).readSettings();
187         }
188
189         public void storeSettings(Object JavaDoc settings) {
190         }
191
192         public HelpCtx getHelp() {
193             return new HelpCtx (SelectorPanel.class);
194         }
195
196         public boolean isValid() {
197             return this.component != null;
198         }
199
200         public java.awt.Component JavaDoc getComponent() {
201             if (this.component == null) {
202                 this.component = new SelectorPanel (this);
203             }
204             return this.component;
205         }
206         
207         public GeneralPlatformInstall getInstaller () {
208             SelectorPanel c = (SelectorPanel) getComponent ();
209             ButtonModel JavaDoc bm = c.group.getSelection();
210             if (bm != null) {
211                 return (GeneralPlatformInstall) c.installersByButtonModels.get (bm);
212             }
213             return null;
214         }
215         
216         public TemplateWizard.InstantiatingIterator getInstallerIterator () {
217             GeneralPlatformInstall platformInstall = getInstaller ();
218             if (platformInstall instanceof CustomPlatformInstall) {
219                 return ((CustomPlatformInstall)platformInstall).createIterator();
220             }
221             return null;
222         }
223         
224         void fireChange () {
225             ChangeListener JavaDoc[] _listeners;
226             synchronized (this) {
227                 if (this.listeners == null) {
228                     return;
229                 }
230                 _listeners = (ChangeListener JavaDoc[]) this.listeners.toArray(new ChangeListener JavaDoc[this.listeners.size()]);
231             }
232             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
233             for (int i=0; i<_listeners.length; i++) {
234                 _listeners[i].stateChanged(event);
235             }
236         }
237     }
238 }
239
Popular Tags