1 19 20 package org.netbeans.modules.tomcat5.customizer; 21 22 import java.awt.event.ItemEvent ; 23 import java.awt.event.ItemListener ; 24 import java.util.Arrays ; 25 import javax.swing.ButtonGroup ; 26 import javax.swing.ButtonModel ; 27 import javax.swing.DefaultComboBoxModel ; 28 import javax.swing.JToggleButton ; 29 import javax.swing.SpinnerNumberModel ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 33 import javax.swing.event.ListDataEvent ; 34 import javax.swing.event.ListDataListener ; 35 import javax.swing.text.BadLocationException ; 36 import javax.swing.text.Document ; 37 import javax.swing.text.PlainDocument ; 38 import org.netbeans.api.java.platform.JavaPlatform; 39 import org.netbeans.api.java.platform.JavaPlatformManager; 40 import org.netbeans.api.java.platform.Specification; 41 import org.netbeans.modules.tomcat5.TomcatManager; 42 import org.netbeans.modules.tomcat5.util.TomcatProperties; 43 import org.openide.ErrorManager; 44 45 46 52 public class CustomizerDataSupport { 53 54 private DefaultComboBoxModel jvmModel; 56 private Document javaOptsModel; 57 private ButtonModel secManagerModel; 58 private Document catalinaHomeModel; 59 private Document catalinaBaseModel; 60 private Document usernameModel; 61 private Document passwordModel; 62 private Document scriptPathModel; 63 private ButtonModel customScriptModel; 64 private ButtonModel forceStopModel; 65 private ButtonModel sharedMemModel; 66 private ButtonModel socketModel; 67 private ButtonModel monitorModel; 68 private ButtonModel proxyModel; 69 private Document sharedMemNameModel; 70 private CustomizerSupport.PathModel sourceModel; 71 private CustomizerSupport.PathModel classModel; 72 private CustomizerSupport.PathModel javadocModel; 73 private SpinnerNumberModel serverPortModel; 74 private SpinnerNumberModel shutdownPortModel; 75 private SpinnerNumberModel debugPortModel; 76 77 private boolean jvmModelFlag; 79 private boolean javaOptsModelFlag; 80 private boolean secManagerModelFlag; 81 private boolean usernameModelFlag; 82 private boolean passwordModelFlag; 83 private boolean scriptPathModelFlag; 84 private boolean customScriptModelFlag; 85 private boolean forceStopModelFlag; 86 private boolean sharedMemModelFlag; 87 private boolean socketModelFlag; 88 private boolean monitorModelFlag; 89 private boolean proxyModelFlag; 90 private boolean sharedMemNameModelFlag; 91 private boolean sourceModelFlag; 92 private boolean javadocModelFlag; 93 private boolean serverPortModelFlag; 94 private boolean shutdownPortModelFlag; 95 private boolean debugPortModelFlag; 96 97 private TomcatProperties tp; 98 private TomcatManager tm; 99 100 103 public CustomizerDataSupport(TomcatManager tm) { 104 this.tm = tm; 105 tp = tm.getTomcatProperties(); 106 init(); 107 } 108 109 110 private void init() { 111 112 jvmModel = new DefaultComboBoxModel (); 114 loadJvmModel(); 115 jvmModel.addListDataListener(new ListDataListener () { 116 public void contentsChanged(ListDataEvent e) { 117 jvmModelFlag = true; 118 store(); } 120 121 public void intervalAdded(ListDataEvent e) { 122 } 123 124 public void intervalRemoved(ListDataEvent e) { 125 } 126 }); 127 128 javaOptsModel = createDocument(tp.getJavaOpts()); 130 javaOptsModel.addDocumentListener(new ModelChangeAdapter() { 131 public void modelChanged() { 132 javaOptsModelFlag = true; 133 store(); } 135 }); 136 137 catalinaHomeModel = createDocument(tp.getCatalinaHome().toString()); 139 140 catalinaBaseModel = createDocument(tp.getCatalinaDir().toString()); 142 143 usernameModel = createDocument(tp.getUsername()); 145 usernameModel.addDocumentListener(new ModelChangeAdapter() { 146 public void modelChanged() { 147 usernameModelFlag = true; 148 store(); } 150 }); 151 152 passwordModel = createDocument(tp.getPassword()); 154 passwordModel.addDocumentListener(new ModelChangeAdapter() { 155 public void modelChanged() { 156 passwordModelFlag = true; 157 store(); } 159 }); 160 161 sharedMemNameModel = createDocument(tp.getSharedMem()); 163 sharedMemNameModel.addDocumentListener(new ModelChangeAdapter() { 164 public void modelChanged() { 165 sharedMemNameModelFlag = true; 166 store(); } 168 }); 169 170 scriptPathModel = createDocument(tp.getScriptPath()); 172 scriptPathModel.addDocumentListener(new ModelChangeAdapter() { 173 public void modelChanged() { 174 scriptPathModelFlag = true; 175 store(); } 177 }); 178 179 secManagerModel = createToggleButtonModel(tp.getSecManager()); 181 secManagerModel.addItemListener(new ModelChangeAdapter() { 182 public void modelChanged() { 183 secManagerModelFlag = true; 184 store(); } 186 }); 187 188 customScriptModel = createToggleButtonModel(tp.getCustomScript()); 190 customScriptModel.addItemListener(new ModelChangeAdapter() { 191 public void modelChanged() { 192 customScriptModelFlag = true; 193 store(); } 195 }); 196 197 forceStopModel = createToggleButtonModel(tp.getForceStop()); 199 forceStopModel.addItemListener(new ModelChangeAdapter() { 200 public void modelChanged() { 201 forceStopModelFlag = true; 202 store(); } 204 }); 205 206 monitorModel = createToggleButtonModel(tp.getMonitor()); 208 monitorModel.addItemListener(new ModelChangeAdapter() { 209 public void modelChanged() { 210 monitorModelFlag = true; 211 store(); } 213 }); 214 215 proxyModel = createToggleButtonModel(tp.getProxyEnabled()); 217 proxyModel.addItemListener(new ModelChangeAdapter() { 218 public void modelChanged() { 219 proxyModelFlag = true; 220 store(); } 222 }); 223 224 classModel = new CustomizerSupport.PathModel(tp.getClasses()); 226 227 sourceModel = new CustomizerSupport.PathModel(tp.getSources()); 229 sourceModel.addListDataListener(new ModelChangeAdapter() { 230 public void modelChanged() { 231 sourceModelFlag = true; 232 store(); } 234 }); 235 236 javadocModel = new CustomizerSupport.PathModel(tp.getJavadocs()); 238 javadocModel.addListDataListener(new ModelChangeAdapter() { 239 public void modelChanged() { 240 javadocModelFlag = true; 241 store(); } 243 }); 244 245 serverPortModel = new SpinnerNumberModel (tm.getServerPort(), 0, 65535, 1); 247 serverPortModel.addChangeListener(new ModelChangeAdapter() { 248 public void modelChanged() { 249 serverPortModelFlag = true; 250 store(); } 252 }); 253 254 shutdownPortModel = new SpinnerNumberModel (tm.getShutdownPort(), 0, 65535, 1); 256 shutdownPortModel.addChangeListener(new ModelChangeAdapter() { 257 public void modelChanged() { 258 shutdownPortModelFlag = true; 259 store(); } 261 }); 262 263 debugPortModel = new SpinnerNumberModel (tp.getDebugPort(), 0, 65535, 1); 265 debugPortModel.addChangeListener(new ModelChangeAdapter() { 266 public void modelChanged() { 267 debugPortModelFlag = true; 268 store(); } 270 }); 271 272 ButtonGroup debugButtonGroup = new ButtonGroup (); 273 274 socketModel = new JToggleButton.ToggleButtonModel (); 276 socketModel.setGroup(debugButtonGroup); 277 socketModel.addItemListener(new ModelChangeAdapter() { 278 public void modelChanged() { 279 socketModelFlag = true; 280 store(); } 282 }); 283 284 sharedMemModel = new JToggleButton.ToggleButtonModel (); 286 sharedMemModel.setGroup(debugButtonGroup); 287 sharedMemModel.addItemListener(new ModelChangeAdapter() { 288 public void modelChanged() { 289 sharedMemModelFlag = true; 290 store(); } 292 }); 293 294 boolean socketEnabled = TomcatProperties.DEBUG_TYPE_SOCKET.equalsIgnoreCase(tp.getDebugType()); 295 debugButtonGroup.setSelected(socketEnabled ? socketModel : sharedMemModel, true); 296 } 297 298 299 public void loadJvmModel() { 300 JavaPlatformManager jpm = JavaPlatformManager.getDefault(); 301 JavaPlatformAdapter curJvm = (JavaPlatformAdapter)jvmModel.getSelectedItem(); 302 String curPlatformName = null; 303 if (curJvm != null) { 304 curPlatformName = curJvm.getName(); 305 } else { 306 curPlatformName = (String )tp.getJavaPlatform().getProperties().get(TomcatProperties.PLAT_PROP_ANT_NAME); 307 } 308 309 jvmModel.removeAllElements(); 310 311 JavaPlatform[] j2sePlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); JavaPlatformAdapter[] platformAdapters = new JavaPlatformAdapter[j2sePlatforms.length]; 314 for (int i = 0; i < platformAdapters.length; i++) { 315 platformAdapters[i] = new JavaPlatformAdapter(j2sePlatforms[i]); 316 } 317 Arrays.sort(platformAdapters); 318 for (int i = 0; i < platformAdapters.length; i++) { 319 JavaPlatformAdapter platformAdapter = platformAdapters[i]; 320 jvmModel.addElement(platformAdapter); 321 if (curPlatformName != null) { 323 if (curPlatformName.equals(platformAdapter.getName())) { 324 jvmModel.setSelectedItem(platformAdapter); 325 } 326 } 327 } 328 } 329 330 332 public DefaultComboBoxModel getJvmModel() { 333 return jvmModel; 334 } 335 336 public Document getJavaOptsModel() { 337 return javaOptsModel; 338 } 339 340 public Document getCatalinaHomeModel() { 341 return catalinaHomeModel; 342 } 343 344 public Document getCatalinaBaseModel() { 345 return catalinaBaseModel; 346 } 347 348 public Document getUsernameModel() { 349 return usernameModel; 350 } 351 352 public Document getPasswordModel() { 353 return passwordModel; 354 } 355 356 public ButtonModel getCustomScriptModel() { 357 return customScriptModel; 358 } 359 360 public ButtonModel getForceStopModel() { 361 return forceStopModel; 362 } 363 364 public Document getScriptPathModel() { 365 return scriptPathModel; 366 } 367 368 public ButtonModel getSharedMemModel() { 369 return sharedMemModel; 370 } 371 372 public ButtonModel getSocketModel() { 373 return socketModel; 374 } 375 376 public ButtonModel getMonitorModel() { 377 return monitorModel; 378 } 379 380 public ButtonModel getProxyModel() { 381 return proxyModel; 382 } 383 384 public ButtonModel getSecManagerModel() { 385 return secManagerModel; 386 } 387 388 public Document getSharedMemNameModel() { 389 return sharedMemNameModel; 390 } 391 392 public CustomizerSupport.PathModel getClassModel() { 393 return classModel; 394 } 395 396 public CustomizerSupport.PathModel getSourceModel() { 397 return sourceModel; 398 } 399 400 public CustomizerSupport.PathModel getJavadocsModel() { 401 return javadocModel; 402 } 403 404 public SpinnerNumberModel getServerPortModel() { 405 return serverPortModel; 406 } 407 408 public SpinnerNumberModel getShutdownPortModel() { 409 return shutdownPortModel; 410 } 411 412 public SpinnerNumberModel getDebugPortModel() { 413 return debugPortModel; 414 } 415 416 418 419 private void store() { 420 421 if (jvmModelFlag) { 422 JavaPlatformAdapter platformAdapter = (JavaPlatformAdapter)jvmModel.getSelectedItem(); 423 tp.setJavaPlatform(platformAdapter.getJavaPlatform()); 424 jvmModelFlag = false; 425 } 426 427 if (javaOptsModelFlag) { 428 tp.setJavaOpts(getText(javaOptsModel)); 429 javaOptsModelFlag = false; 430 } 431 432 if (secManagerModelFlag) { 433 tp.setSecManager(secManagerModel.isSelected()); 434 secManagerModelFlag = false; 435 } 436 437 if (usernameModelFlag) { 438 tp.setUsername(getText(usernameModel)); 439 usernameModelFlag = false; 440 } 441 442 if (passwordModelFlag) { 443 tp.setPassword(getText(passwordModel)); 444 passwordModelFlag = false; 445 } 446 447 if (scriptPathModelFlag) { 448 tp.setScriptPath(getText(scriptPathModel)); 449 scriptPathModelFlag = false; 450 } 451 452 if (customScriptModelFlag) { 453 tp.setCustomScript(customScriptModel.isSelected()); 454 customScriptModelFlag = false; 455 } 456 457 if (forceStopModelFlag) { 458 tp.setForceStop(forceStopModel.isSelected()); 459 forceStopModelFlag = false; 460 } 461 462 if (sharedMemModelFlag || socketModelFlag) { 463 tp.setDebugType(sharedMemModel.isSelected() ? TomcatProperties.DEBUG_TYPE_SHARED 464 : TomcatProperties.DEBUG_TYPE_SOCKET); 465 sharedMemModelFlag = false; 466 socketModelFlag = false; 467 } 468 469 if (monitorModelFlag) { 470 tp.setMonitor(monitorModel.isSelected()); 471 monitorModelFlag = false; 472 } 473 474 if (proxyModelFlag) { 475 tp.setProxyEnabled(proxyModel.isSelected()); 476 proxyModelFlag = false; 477 } 478 479 if (sharedMemNameModelFlag) { 480 tp.setSharedMem(getText(sharedMemNameModel)); 481 sharedMemNameModelFlag = false; 482 } 483 484 if (sourceModelFlag) { 485 tp.setSources(sourceModel.getData()); 486 sourceModelFlag = false; 487 } 488 489 if (javadocModelFlag) { 490 tp.setJavadocs(javadocModel.getData()); 491 javadocModelFlag = false; 492 } 493 494 if (serverPortModelFlag) { 495 tm.setServerPort(((Integer )serverPortModel.getValue()).intValue()); 496 serverPortModelFlag = false; 497 } 498 499 if (shutdownPortModelFlag) { 500 tm.setShutdownPort(((Integer )shutdownPortModel.getValue()).intValue()); 501 shutdownPortModelFlag = false; 502 } 503 504 if (debugPortModelFlag) { 505 tp.setDebugPort(((Integer )debugPortModel.getValue()).intValue()); 506 debugPortModelFlag = false; 507 } 508 } 509 510 511 private Document createDocument(String text) { 512 PlainDocument doc = new PlainDocument (); 513 if (text != null) { 514 try { 515 doc.insertString(0, text, null); 516 } catch(BadLocationException e) { 517 ErrorManager.getDefault().notify(e); 518 } 519 } 520 return doc; 521 } 522 523 524 private JToggleButton.ToggleButtonModel createToggleButtonModel(boolean selected) { 525 JToggleButton.ToggleButtonModel model = new JToggleButton.ToggleButtonModel (); 526 model.setSelected(selected); 527 return model; 528 } 529 530 531 private String getText(Document doc) { 532 try { 533 return doc.getText(0, doc.getLength()); 534 } catch(BadLocationException e) { 535 ErrorManager.getDefault().notify(e); 536 return null; 537 } 538 } 539 540 542 546 private abstract class ModelChangeAdapter implements ListDataListener , 547 DocumentListener , ItemListener , ChangeListener { 548 549 public abstract void modelChanged(); 550 551 public void contentsChanged(ListDataEvent e) { 552 modelChanged(); 553 } 554 555 public void intervalAdded(ListDataEvent e) { 556 modelChanged(); 557 } 558 559 public void intervalRemoved(ListDataEvent e) { 560 modelChanged(); 561 } 562 563 public void changedUpdate(DocumentEvent e) { 564 modelChanged(); 565 } 566 567 public void removeUpdate(DocumentEvent e) { 568 modelChanged(); 569 } 570 571 public void insertUpdate(DocumentEvent e) { 572 modelChanged(); 573 } 574 575 public void itemStateChanged(ItemEvent e) { 576 modelChanged(); 577 } 578 579 public void stateChanged(javax.swing.event.ChangeEvent e) { 580 modelChanged(); 581 } 582 } 583 584 585 private static class JavaPlatformAdapter implements Comparable { 586 private JavaPlatform platform; 587 588 public JavaPlatformAdapter(JavaPlatform platform) { 589 this.platform = platform; 590 } 591 592 public JavaPlatform getJavaPlatform() { 593 return platform; 594 } 595 596 public String getName() { 597 return (String )platform.getProperties().get(TomcatProperties.PLAT_PROP_ANT_NAME); 598 } 599 600 public String toString() { 601 return platform.getDisplayName(); 602 } 603 604 public int compareTo(Object o) { 605 return toString().compareTo(o.toString()); 606 } 607 } 608 } 609 | Popular Tags |