1 19 20 package org.netbeans.modules.java.platform.wizard; 21 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.event.ItemListener ; 26 import java.util.IdentityHashMap ; 27 import java.util.Iterator ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import javax.swing.ButtonGroup ; 32 import javax.swing.ButtonModel ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.JRadioButton ; 36 import javax.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 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 50 class SelectorPanel extends javax.swing.JPanel implements ItemListener { 51 52 private Map installersByButtonModels = new IdentityHashMap (); 53 private ButtonGroup group; 54 private SelectorPanel.Panel firer; 55 56 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 installers = regs.getAllInstallers(); 68 this.group = new ButtonGroup (); 69 JLabel label = new JLabel (NbBundle.getMessage(SelectorPanel.class,"TXT_SelectPlatform")); 70 label.setDisplayedMnemonic(NbBundle.getMessage(SelectorPanel.class,"AD_SelectPlatform").charAt(0)); 71 GridBagConstraints c = new GridBagConstraints (); 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 (12, 12, 6, 12); 79 ((GridBagLayout )this.getLayout()).setConstraints(label,c); 80 this.add (label); 81 Iterator it = installers.iterator(); 82 for (int i=0; it.hasNext(); i++) { 83 GeneralPlatformInstall pi = (GeneralPlatformInstall) it.next (); 84 JRadioButton button = new JRadioButton (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 (); 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 (6, 18, it.hasNext()? 0 : 12, 12); 99 ((GridBagLayout )this.getLayout()).setConstraints(button,c); 100 this.add (button); 101 } 102 JPanel pad = new JPanel (); 103 c = new GridBagConstraints (); 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 (12,0,0,12); 112 ((GridBagLayout )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 buttonEnum = this.group.getElements(); 119 assert buttonEnum.hasMoreElements(); 120 ((JRadioButton )buttonEnum.nextElement()).setSelected(true); 121 } 122 } 123 124 public void itemStateChanged(java.awt.event.ItemEvent e) { 125 this.firer.fireChange(); 126 } 127 128 129 133 boolean selectInstaller (GeneralPlatformInstall install) { 134 assert install != null; 135 for (Iterator it = this.installersByButtonModels.entrySet().iterator(); it.hasNext();) { 136 Map.Entry entry = (Map.Entry ) it.next(); 137 if (entry.getValue().equals(install)) { 138 ButtonModel model = (ButtonModel ) entry.getKey(); 139 model.setSelected(true); 140 return true; 141 } 142 } 143 return false; 144 } 145 146 151 private void initComponents() { 153 154 setLayout(new java.awt.GridBagLayout ()); 155 156 } 157 159 160 163 164 public static class Panel implements WizardDescriptor.Panel { 165 166 private List listeners; 167 private SelectorPanel component; 168 169 public synchronized void removeChangeListener(ChangeListener 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 l) { 178 assert l != null; 179 if (this.listeners == null) { 180 this.listeners = new ArrayList (); 181 } 182 this.listeners.add(l); 183 } 184 185 public void readSettings(Object settings) { 186 ((SelectorPanel)this.getComponent()).readSettings(); 187 } 188 189 public void storeSettings(Object 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 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 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 [] _listeners; 226 synchronized (this) { 227 if (this.listeners == null) { 228 return; 229 } 230 _listeners = (ChangeListener []) this.listeners.toArray(new ChangeListener [this.listeners.size()]); 231 } 232 ChangeEvent event = new ChangeEvent (this); 233 for (int i=0; i<_listeners.length; i++) { 234 _listeners[i].stateChanged(event); 235 } 236 } 237 } 238 } 239 | Popular Tags |