001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.gsfpath.platform.wizard;
043:
044: import java.awt.GridBagConstraints;
045: import java.awt.GridBagLayout;
046: import java.awt.Insets;
047: import java.awt.event.ItemListener;
048: import java.util.IdentityHashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import javax.swing.ButtonGroup;
053: import javax.swing.ButtonModel;
054: import javax.swing.JLabel;
055: import javax.swing.JPanel;
056: import javax.swing.JRadioButton;
057: import javax.swing.event.ChangeListener;
058: import org.netbeans.modules.gsfpath.platform.InstallerRegistry;
059: import org.netbeans.modules.gsfpath.spi.platform.CustomPlatformInstall;
060: import org.netbeans.modules.gsfpath.spi.platform.GeneralPlatformInstall;
061: import org.openide.WizardDescriptor;
062: import org.openide.loaders.TemplateWizard;
063: import org.openide.util.ChangeSupport;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.NbBundle;
066:
067: /**
068: *
069: * @author Tomas Zezula
070: */
071: class SelectorPanel extends javax.swing.JPanel implements ItemListener {
072:
073: private Map<ButtonModel, GeneralPlatformInstall> installersByButtonModels = new IdentityHashMap<ButtonModel, GeneralPlatformInstall>();
074: private ButtonGroup group;
075: private SelectorPanel.Panel firer;
076:
077: /** Creates new form SelectorPanel */
078: public SelectorPanel(SelectorPanel.Panel firer) {
079: this .firer = firer;
080: initComponents();
081: postInitComponents();
082: this .setName(NbBundle.getMessage(SelectorPanel.class,
083: "TXT_SelectPlatformTypeTitle"));
084: this .getAccessibleContext().setAccessibleDescription(
085: NbBundle.getMessage(SelectorPanel.class,
086: "AD_SelectPlatformType"));
087: }
088:
089: private void postInitComponents() {
090: InstallerRegistry regs = InstallerRegistry.getDefault();
091: List<GeneralPlatformInstall> installers = regs
092: .getAllInstallers();
093: this .group = new ButtonGroup();
094: JLabel label = new JLabel(NbBundle.getMessage(
095: SelectorPanel.class, "TXT_SelectPlatform"));
096: label.setDisplayedMnemonic(NbBundle.getMessage(
097: SelectorPanel.class, "AD_SelectPlatform").charAt(0));
098: GridBagConstraints c = new GridBagConstraints();
099: c.gridx = c.gridy = GridBagConstraints.RELATIVE;
100: c.gridheight = 1;
101: c.gridwidth = GridBagConstraints.REMAINDER;
102: c.fill = GridBagConstraints.HORIZONTAL;
103: c.anchor = GridBagConstraints.NORTHWEST;
104: c.weightx = 1.0;
105: c.insets = new Insets(12, 12, 6, 12);
106: ((GridBagLayout) this .getLayout()).setConstraints(label, c);
107: this .add(label);
108: Iterator<GeneralPlatformInstall> it = installers.iterator();
109: for (int i = 0; it.hasNext(); i++) {
110: GeneralPlatformInstall pi = it.next();
111: JRadioButton button = new JRadioButton(pi.getDisplayName());
112: if (i == 0) {
113: label.setLabelFor(button);
114: }
115: button.addItemListener(this );
116: this .installersByButtonModels.put(button.getModel(), pi);
117: this .group.add(button);
118: c = new GridBagConstraints();
119: c.gridx = c.gridy = GridBagConstraints.RELATIVE;
120: c.gridheight = 1;
121: c.gridwidth = GridBagConstraints.REMAINDER;
122: c.fill = GridBagConstraints.HORIZONTAL;
123: c.anchor = GridBagConstraints.NORTHWEST;
124: c.weightx = 1.0;
125: c.insets = new Insets(6, 18, it.hasNext() ? 0 : 12, 12);
126: ((GridBagLayout) this .getLayout())
127: .setConstraints(button, c);
128: this .add(button);
129: }
130: JPanel pad = new JPanel();
131: c = new GridBagConstraints();
132: c.gridx = c.gridy = GridBagConstraints.RELATIVE;
133: c.gridheight = 1;
134: c.gridwidth = GridBagConstraints.REMAINDER;
135: c.fill = GridBagConstraints.BOTH;
136: c.anchor = GridBagConstraints.NORTHWEST;
137: c.weightx = 1.0;
138: c.weighty = 1.0;
139: c.insets = new Insets(12, 0, 0, 12);
140: ((GridBagLayout) this .getLayout()).setConstraints(pad, c);
141: this .add(pad);
142: }
143:
144: private void readSettings() {
145: if (this .group.getSelection() == null) {
146: java.util.Enumeration buttonEnum = this .group.getElements();
147: assert buttonEnum.hasMoreElements();
148: ((JRadioButton) buttonEnum.nextElement()).setSelected(true);
149: }
150: }
151:
152: public void itemStateChanged(java.awt.event.ItemEvent e) {
153: this .firer.cs.fireChange();
154: }
155:
156: /**
157: * Used by unit tests
158: * Select the GeneralPlatformInstall to allow step over this panel
159: */
160: boolean selectInstaller(GeneralPlatformInstall install) {
161: assert install != null;
162: for (Map.Entry<ButtonModel, GeneralPlatformInstall> entry : installersByButtonModels
163: .entrySet()) {
164: if (entry.getValue().equals(install)) {
165: ButtonModel model = entry.getKey();
166: model.setSelected(true);
167: return true;
168: }
169: }
170: return false;
171: }
172:
173: /** This method is called from within the constructor to
174: * initialize the form.
175: * WARNING: Do NOT modify this code. The content of this method is
176: * always regenerated by the Form Editor.
177: */
178: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
179: private void initComponents() {
180:
181: setLayout(new java.awt.GridBagLayout());
182:
183: }
184:
185: // </editor-fold>//GEN-END:initComponents
186:
187: // Variables declaration - do not modify//GEN-BEGIN:variables
188: // End of variables declaration//GEN-END:variables
189:
190: public static class Panel implements
191: WizardDescriptor.Panel<WizardDescriptor> {
192:
193: private final ChangeSupport cs = new ChangeSupport(this );
194: private SelectorPanel component;
195:
196: public synchronized void removeChangeListener(ChangeListener l) {
197: cs.removeChangeListener(l);
198: }
199:
200: public synchronized void addChangeListener(ChangeListener l) {
201: cs.addChangeListener(l);
202: }
203:
204: public void readSettings(WizardDescriptor wiz) {
205: getComponent().readSettings();
206: }
207:
208: public void storeSettings(WizardDescriptor wiz) {
209: }
210:
211: public HelpCtx getHelp() {
212: return new HelpCtx(SelectorPanel.class);
213: }
214:
215: public boolean isValid() {
216: return this .component != null;
217: }
218:
219: public SelectorPanel getComponent() {
220: if (this .component == null) {
221: this .component = new SelectorPanel(this );
222: }
223: return this .component;
224: }
225:
226: public GeneralPlatformInstall getInstaller() {
227: SelectorPanel c = getComponent();
228: ButtonModel bm = c.group.getSelection();
229: if (bm != null) {
230: return c.installersByButtonModels.get(bm);
231: }
232: return null;
233: }
234:
235: public TemplateWizard.InstantiatingIterator getInstallerIterator() {
236: GeneralPlatformInstall platformInstall = getInstaller();
237: if (platformInstall instanceof CustomPlatformInstall) {
238: return ((CustomPlatformInstall) platformInstall)
239: .createIterator();
240: }
241: return null;
242: }
243:
244: }
245: }
|