1 19 20 package org.netbeans.modules.j2ee.jboss4.customizer; 21 22 import java.awt.event.FocusAdapter ; 23 import java.awt.event.FocusEvent ; 24 import java.awt.event.FocusListener ; 25 import java.awt.event.ItemEvent ; 26 import java.awt.event.ItemListener ; 27 import java.util.Arrays ; 28 import javax.swing.ButtonGroup ; 29 import javax.swing.ButtonModel ; 30 import javax.swing.DefaultComboBoxModel ; 31 import javax.swing.JToggleButton ; 32 import javax.swing.SpinnerNumberModel ; 33 import javax.swing.event.ChangeListener ; 34 import javax.swing.event.DocumentEvent ; 35 import javax.swing.event.DocumentListener ; 36 import javax.swing.event.ListDataEvent ; 37 import javax.swing.event.ListDataListener ; 38 import javax.swing.text.BadLocationException ; 39 import javax.swing.text.Document ; 40 import javax.swing.text.PlainDocument ; 41 import org.netbeans.api.java.platform.JavaPlatform; 42 import org.netbeans.api.java.platform.JavaPlatformManager; 43 import org.netbeans.api.java.platform.Specification; 44 import org.netbeans.modules.j2ee.jboss4.util.JBProperties; 45 import org.openide.ErrorManager; 46 47 48 54 public class CustomizerDataSupport { 55 56 private DefaultComboBoxModel jvmModel; 58 private Document javaOptsModel; 59 private ButtonModel proxyModel; 60 private CustomizerSupport.PathModel sourceModel; 61 private CustomizerSupport.PathModel classModel; 62 private CustomizerSupport.PathModel javadocModel; 63 64 private boolean jvmModelFlag; 66 private boolean javaOptsModelFlag; 67 private boolean proxyModelFlag; 68 private boolean sourceModelFlag; 69 private boolean javadocModelFlag; 70 71 private JBProperties properties; 72 73 76 public CustomizerDataSupport(JBProperties properties) { 77 this.properties = properties; 78 init(); 79 } 80 81 82 private void init() { 83 84 jvmModel = new DefaultComboBoxModel (); 86 loadJvmModel(); 87 jvmModel.addListDataListener(new ListDataListener () { 88 public void contentsChanged(ListDataEvent e) { 89 jvmModelFlag = true; 90 store(); } 92 93 public void intervalAdded(ListDataEvent e) { 94 } 95 96 public void intervalRemoved(ListDataEvent e) { 97 } 98 }); 99 100 javaOptsModel = createDocument(properties.getJavaOpts()); 102 javaOptsModel.addDocumentListener(new ModelChangeAdapter() { 103 public void modelChanged() { 104 javaOptsModelFlag = true; 105 store(); } 107 }); 108 109 proxyModel = createToggleButtonModel(properties.getProxyEnabled()); 111 proxyModel.addItemListener(new ModelChangeAdapter() { 112 public void modelChanged() { 113 proxyModelFlag = true; 114 store(); } 116 }); 117 118 classModel = new CustomizerSupport.PathModel(properties.getClasses()); 120 121 sourceModel = new CustomizerSupport.PathModel(properties.getSources()); 123 sourceModel.addListDataListener(new ModelChangeAdapter() { 124 public void modelChanged() { 125 sourceModelFlag = true; 126 store(); } 128 }); 129 130 javadocModel = new CustomizerSupport.PathModel(properties.getJavadocs()); 132 javadocModel.addListDataListener(new ModelChangeAdapter() { 133 public void modelChanged() { 134 javadocModelFlag = true; 135 store(); } 137 }); 138 } 139 140 141 public void loadJvmModel() { 142 JavaPlatformManager jpm = JavaPlatformManager.getDefault(); 143 JavaPlatformAdapter curJvm = (JavaPlatformAdapter)jvmModel.getSelectedItem(); 144 String curPlatformName = null; 145 if (curJvm != null) { 146 curPlatformName = curJvm.getName(); 147 } else { 148 curPlatformName = (String )properties.getJavaPlatform().getProperties().get(JBProperties.PLAT_PROP_ANT_NAME); 149 } 150 151 jvmModel.removeAllElements(); 152 153 JavaPlatform[] j2sePlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); JavaPlatformAdapter[] platformAdapters = new JavaPlatformAdapter[j2sePlatforms.length]; 156 for (int i = 0; i < platformAdapters.length; i++) { 157 platformAdapters[i] = new JavaPlatformAdapter(j2sePlatforms[i]); 158 } 159 Arrays.sort(platformAdapters); 160 for (int i = 0; i < platformAdapters.length; i++) { 161 JavaPlatformAdapter platformAdapter = platformAdapters[i]; 162 jvmModel.addElement(platformAdapter); 163 if (curPlatformName != null) { 165 if (curPlatformName.equals(platformAdapter.getName())) { 166 jvmModel.setSelectedItem(platformAdapter); 167 } 168 } 169 } 170 } 171 172 174 public DefaultComboBoxModel getJvmModel() { 175 return jvmModel; 176 } 177 178 public Document getJavaOptsModel() { 179 return javaOptsModel; 180 } 181 182 public ButtonModel getProxyModel() { 183 return proxyModel; 184 } 185 186 public CustomizerSupport.PathModel getClassModel() { 187 return classModel; 188 } 189 190 public CustomizerSupport.PathModel getSourceModel() { 191 return sourceModel; 192 } 193 194 public CustomizerSupport.PathModel getJavadocsModel() { 195 return javadocModel; 196 } 197 198 200 201 private void store() { 202 203 if (jvmModelFlag) { 204 JavaPlatformAdapter platformAdapter = (JavaPlatformAdapter)jvmModel.getSelectedItem(); 205 properties.setJavaPlatform(platformAdapter.getJavaPlatform()); 206 jvmModelFlag = false; 207 } 208 209 if (javaOptsModelFlag) { 210 properties.setJavaOpts(getText(javaOptsModel)); 211 javaOptsModelFlag = false; 212 } 213 214 if (proxyModelFlag) { 215 properties.setProxyEnabled(proxyModel.isSelected()); 216 proxyModelFlag = false; 217 } 218 219 if (sourceModelFlag) { 220 properties.setSources(sourceModel.getData()); 221 sourceModelFlag = false; 222 } 223 224 if (javadocModelFlag) { 225 properties.setJavadocs(javadocModel.getData()); 226 javadocModelFlag = false; 227 } 228 } 229 230 231 private Document createDocument(String text) { 232 PlainDocument doc = new PlainDocument (); 233 if (text != null) { 234 try { 235 doc.insertString(0, text, null); 236 } catch(BadLocationException e) { 237 ErrorManager.getDefault().notify(e); 238 } 239 } 240 return doc; 241 } 242 243 244 private String getText(Document doc) { 245 try { 246 return doc.getText(0, doc.getLength()); 247 } catch(BadLocationException e) { 248 ErrorManager.getDefault().notify(e); 249 return null; 250 } 251 } 252 253 254 private JToggleButton.ToggleButtonModel createToggleButtonModel(boolean selected) { 255 JToggleButton.ToggleButtonModel model = new JToggleButton.ToggleButtonModel (); 256 model.setSelected(selected); 257 return model; 258 } 259 260 262 266 private abstract class ModelChangeAdapter implements ListDataListener , 267 DocumentListener , ItemListener , ChangeListener { 268 269 public abstract void modelChanged(); 270 271 public void contentsChanged(ListDataEvent e) { 272 modelChanged(); 273 } 274 275 public void intervalAdded(ListDataEvent e) { 276 modelChanged(); 277 } 278 279 public void intervalRemoved(ListDataEvent e) { 280 modelChanged(); 281 } 282 283 public void changedUpdate(DocumentEvent e) { 284 modelChanged(); 285 } 286 287 public void removeUpdate(DocumentEvent e) { 288 modelChanged(); 289 } 290 291 public void insertUpdate(DocumentEvent e) { 292 modelChanged(); 293 } 294 295 public void itemStateChanged(ItemEvent e) { 296 modelChanged(); 297 } 298 299 public void stateChanged(javax.swing.event.ChangeEvent e) { 300 modelChanged(); 301 } 302 } 303 304 305 private static class JavaPlatformAdapter implements Comparable { 306 private JavaPlatform platform; 307 308 public JavaPlatformAdapter(JavaPlatform platform) { 309 this.platform = platform; 310 } 311 312 public JavaPlatform getJavaPlatform() { 313 return platform; 314 } 315 316 public String getName() { 317 return (String )platform.getProperties().get(JBProperties.PLAT_PROP_ANT_NAME); 318 } 319 320 public String toString() { 321 return platform.getDisplayName(); 322 } 323 324 public int compareTo(Object o) { 325 return toString().compareTo(o.toString()); 326 } 327 } 328 } 329 | Popular Tags |