1 22 package org.enhydra.kelp.common.properties; 23 24 import org.enhydra.tool.common.DialogPanel; 26 import org.enhydra.tool.common.SwingUtil; 27 28 import java.awt.*; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.awt.event.ItemEvent ; 33 import java.awt.event.ItemListener ; 34 import java.beans.*; 35 import java.util.StringTokenizer ; 36 import javax.swing.*; 37 import java.util.ResourceBundle ; 38 39 public class XMLCParameterEditor extends DialogPanel { 41 42 static ResourceBundle res = 44 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private final String CARD_COMBO = "combo"; private final String CARD_TEXT = "text"; private final String GENERATE_BOTH = "-generate both"; 49 private KnownParam[] knownParams = new KnownParam[0]; 51 private LocalOpOneListener opOneListener = null; 52 private LocalParamListener comboListener = null; 53 private LocalRadioListener radioListener = null; 54 private LocalButtonListener buttonListener = null; 55 private GridBagLayout layoutMain = null; 56 private JRadioButton radioDirect = null; 57 private JRadioButton radioList = null; 58 private JPanel panelParam = null; 59 private JPanel panelOpOne = null; 60 private CardLayout paramDeck = null; 61 private CardLayout opOneDeck = null; 62 private JComboBox comboParam = null; 63 private JComboBox comboOpOne = null; 64 private JTextField textParam = null; 65 private JTextField textOpOne = null; 66 private JTextField textOpTwo = null; 67 private JPanel buttonControl = null; 68 private GridBagLayout layoutControl = null; 69 private JButton buttonCancel = null; 70 private JButton buttonOK = null; 71 private JPanel panelRadio = null; 72 private GridBagLayout layoutRadio = null; 73 74 public static void main(String [] args) { 75 SwingUtil.setLookAndFeelToSystem(); 76 XMLCParameterEditor editor = null; 77 78 editor = new XMLCParameterEditor(); 79 editor.showDialog(); 80 System.exit(0); 81 } 82 83 public XMLCParameterEditor() { 84 try { 85 jbInit(); 86 pmInit(); 87 } catch (Exception ex) { 88 ex.printStackTrace(); 89 } 90 } 91 92 public String getParameter() { 93 String param = new String (); 94 95 if (radioList.isSelected()) { 96 param = getListValue(); 97 } else { 98 param = getDirectValue(); 99 } 100 return param; 101 } 102 103 public void setParameter(String param) { 104 String [] array = new String [0]; 105 106 array = paramToArray(param); 107 if (isKnown(array)) { 108 radioList.setSelected(true); 109 showList(param); 110 } else { 111 radioDirect.setSelected(true); 112 showDirect(param); 113 } 114 updateUI(); 115 } 116 117 protected void clearDialog() { 120 super.clearDialog(); 121 } 122 123 private String getListValue() { 126 StringBuffer buf = new StringBuffer (); 127 int index = comboParam.getSelectedIndex(); 128 129 buf.append(comboParam.getSelectedItem().toString()); 130 buf.append(' '); 131 buf.append(textOpOne.getText()); 132 buf.append(' '); 133 buf.append(textOpTwo.getText()); 134 return buf.toString().trim(); 135 } 136 137 private String getDirectValue() { 138 return textParam.getText().trim(); 139 } 140 141 private void showList(String param) { 142 String [] array = new String [0]; 143 String [] selections = new String [0]; 144 int index = 0; 145 146 array = paramToArray(param); 147 148 if (!isKnown(array)) { 150 array = paramToArray(param); 151 } 152 153 comboParam.setSelectedItem(array[0]); 155 paramDeck.first(panelParam); 156 index = comboParam.getSelectedIndex(); 157 158 if (array.length > 1) { 160 selections = knownParams[index].getSelections(); 161 textOpOne.setText(array[1]); 162 if (selections.length >= 1) { 163 comboOpOne.setModel(new DefaultComboBoxModel(selections)); 164 comboOpOne.getModel().setSelectedItem(array[1]); 165 opOneDeck.first(panelOpOne); 166 } else { 167 textOpOne.setBackground(SystemColor.info); 168 textOpOne.setEnabled(true); 169 opOneDeck.last(panelOpOne); 170 } 171 } else { 172 textOpOne.setBackground(SystemColor.control); 173 textOpOne.setEnabled(false); 174 textOpOne.setText(new String ()); 175 opOneDeck.last(panelOpOne); 176 } 177 178 if (array.length > 2) { 180 textOpTwo.setBackground(SystemColor.info); 181 textOpTwo.setEnabled(true); 182 textOpTwo.setText(array[2]); 183 } else { 184 textOpTwo.setBackground(SystemColor.control); 185 textOpTwo.setEnabled(false); 186 textOpTwo.setText(new String ()); 187 } 188 } 189 190 private void showDirect(String param) { 191 textParam.setText(param); 192 paramDeck.last(panelParam); 193 textOpOne.setBackground(SystemColor.control); 194 textOpOne.setText(new String ()); 195 textOpOne.setEnabled(false); 196 opOneDeck.last(panelOpOne); 197 textOpTwo.setBackground(SystemColor.control); 198 textOpTwo.setText(new String ()); 199 textOpTwo.setEnabled(false); 200 } 201 202 private String [] paramToArray(String param) { 203 StringTokenizer tokenizer = null; 204 String [] array = new String [0]; 205 int count = -1; 206 207 tokenizer = new StringTokenizer (param); 208 count = tokenizer.countTokens(); 209 array = new String [count]; 210 int i = 0; 211 212 while (tokenizer.hasMoreTokens()) { 213 array[i] = tokenizer.nextToken(); 214 i++; 215 } 216 return array; 217 } 218 219 private boolean isKnown(String [] array) { 220 boolean known = false; 221 222 for (int i = 0; i < knownParams.length; i++) { 223 if (knownParams[i].isValid(array)) { 224 known = true; 225 break; 226 } 227 } 228 return known; 229 } 230 231 private void initOpts() { 232 String [] options = new String [0]; 233 String [] selections = new String [0]; 234 int index = 0; 235 236 index = comboParam.getSelectedIndex(); 237 if (index == -1) { 238 index = 0; 239 } 240 options = knownParams[index].getOptions(); 241 242 if (options.length >= 1) { 244 selections = knownParams[index].getSelections(); 245 textOpOne.setText(options[0]); 246 if (selections.length >= 1) { 247 comboOpOne.setModel(new DefaultComboBoxModel(selections)); 248 comboOpOne.getModel().setSelectedItem(options[0]); 249 opOneDeck.first(panelOpOne); 250 } else { 251 textOpOne.setBackground(SystemColor.info); 252 textOpOne.setEnabled(true); 253 opOneDeck.last(panelOpOne); 254 } 255 } else { 256 textOpOne.setBackground(SystemColor.control); 257 textOpOne.setEnabled(false); 258 textOpOne.setText(new String ()); 259 opOneDeck.last(panelOpOne); 260 } 261 262 if (options.length > 1) { 264 textOpTwo.setBackground(SystemColor.info); 265 textOpTwo.setText(options[1]); 266 textOpTwo.setEnabled(true); 267 } else { 268 textOpTwo.setBackground(SystemColor.control); 269 textOpTwo.setText(new String ()); 270 textOpTwo.setEnabled(false); 271 } 272 } 273 274 private void toggleParam() { 275 if (radioList.isSelected()) { 276 showList(getDirectValue()); 277 } else { 278 showDirect(getListValue()); 279 } 280 updateUI(); 281 } 282 283 private void initKnown() { 284 285 final String [] SELECTION_PARSERS = { 287 "tidy", "swing", "xerces" 288 }; final String [] SELECTION_VALIDATE = { 290 "yes", "no" 291 }; final String [] SELECTION_GENERATE = { 293 "both", "interface", "implementation", "class" 294 }; 296 knownParams = new KnownParam[18]; 297 knownParams[0] = new KnownParam("-delete-class"); knownParams[0].initOptions("classname"); knownParams[1] = new KnownParam("-domfactory"); knownParams[1].initOptions("classname"); knownParams[2] = new KnownParam("-extends"); knownParams[2].initOptions("classname"); knownParams[3] = new KnownParam("-for-recomp"); knownParams[4] = new KnownParam("-generate"); knownParams[4].setSelections(SELECTION_GENERATE); 306 knownParams[5] = new KnownParam("-html:encoding"); knownParams[5].initOptions("encoding"); knownParams[6] = new KnownParam("-html:addtagset"); knownParams[6].initOptions("cyberstudio"); knownParams[7] = new KnownParam("-html:addtag"); knownParams[7].initOptions("tagname"); knownParams[7].initOptions("flags"); knownParams[8] = new KnownParam("-html:addattr"); knownParams[8].initOptions("attrname"); knownParams[9] = new KnownParam("-html:old-class-constants"); knownParams[10] = new KnownParam("-html:old-name-constants"); knownParams[11] = new KnownParam("-parser"); knownParams[11].setSelections(SELECTION_PARSERS); 319 knownParams[12] = new KnownParam("-urlmapping"); knownParams[12].initOptions("orgURL", "newURL"); knownParams[13] = new KnownParam("-urlregexmapping"); knownParams[13].initOptions("regexp", "subst"); knownParams[14] = new KnownParam("-urlsetting"); knownParams[14].initOptions("id", "newURL"); knownParams[15] = new KnownParam("-ssi"); knownParams[16] = new KnownParam("-validate"); knownParams[16].setSelections(SELECTION_VALIDATE); 328 knownParams[17] = new KnownParam("-xcatalog"); knownParams[17].initOptions("catalog"); } 331 332 private void pmInit() { 333 ButtonGroup radioGroup = null; 334 Dimension size = null; 335 336 buttonListener = new LocalButtonListener(); 338 buttonOK.addActionListener(buttonListener); 339 buttonCancel.addActionListener(buttonListener); 340 341 radioListener = new LocalRadioListener(); 343 radioGroup = new ButtonGroup(); 344 radioGroup.add(radioList); 345 radioGroup.add(radioDirect); 346 radioList.addItemListener(radioListener); 347 radioDirect.addItemListener(radioListener); 348 349 this.remove(panelOpOne); 351 this.add(panelOpOne, 352 new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, 353 GridBagConstraints.CENTER, 354 GridBagConstraints.HORIZONTAL, 355 new Insets(5, 5, 5, 5), 500, 0)); 356 357 textOpOne.setText(new String ()); 359 textOpTwo.setText(new String ()); 360 361 initKnown(); 363 String [] knownStrings = new String [knownParams.length]; 364 365 for (int i = 0; i < knownStrings.length; i++) { 366 knownStrings[i] = knownParams[i].toString(); 367 } 368 comboParam.setModel(new DefaultComboBoxModel(knownStrings)); 369 370 String [] array = paramToArray(GENERATE_BOTH); 372 373 comboParam.setSelectedItem(array[0]); 374 textParam.setText(GENERATE_BOTH); 375 setParameter(GENERATE_BOTH); 376 377 comboListener = new LocalParamListener(); 379 comboParam.addItemListener(comboListener); 380 opOneListener = new LocalOpOneListener(); 381 comboOpOne.addItemListener(opOneListener); 382 setTitle(res.getString("XMLC_Parameter")); 383 } 384 385 private void jbInit() throws Exception { 386 layoutRadio = 387 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 388 GridBagLayout.class.getName()); 389 layoutMain = 390 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 391 GridBagLayout.class.getName()); 392 radioList = 393 (JRadioButton) Beans.instantiate(getClass().getClassLoader(), 394 JRadioButton.class.getName()); 395 radioDirect = 396 (JRadioButton) Beans.instantiate(getClass().getClassLoader(), 397 JRadioButton.class.getName()); 398 panelOpOne = (JPanel) Beans.instantiate(getClass().getClassLoader(), 399 JPanel.class.getName()); 400 opOneDeck = 401 (CardLayout) Beans.instantiate(getClass().getClassLoader(), 402 CardLayout.class.getName()); 403 panelParam = (JPanel) Beans.instantiate(getClass().getClassLoader(), 404 JPanel.class.getName()); 405 paramDeck = 406 (CardLayout) Beans.instantiate(getClass().getClassLoader(), 407 CardLayout.class.getName()); 408 comboParam = 409 (JComboBox) Beans.instantiate(getClass().getClassLoader(), 410 JComboBox.class.getName()); 411 textParam = 412 (JTextField) Beans.instantiate(getClass().getClassLoader(), 413 JTextField.class.getName()); 414 textOpOne = 415 (JTextField) Beans.instantiate(getClass().getClassLoader(), 416 JTextField.class.getName()); 417 comboOpOne = 418 (JComboBox) Beans.instantiate(getClass().getClassLoader(), 419 JComboBox.class.getName()); 420 textOpTwo = 421 (JTextField) Beans.instantiate(getClass().getClassLoader(), 422 JTextField.class.getName()); 423 buttonControl = 424 (JPanel) Beans.instantiate(getClass().getClassLoader(), 425 JPanel.class.getName()); 426 layoutControl = 427 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 428 GridBagLayout.class.getName()); 429 buttonCancel = 430 (JButton) Beans.instantiate(getClass().getClassLoader(), 431 JButton.class.getName()); 432 buttonOK = (JButton) Beans.instantiate(getClass().getClassLoader(), 433 JButton.class.getName()); 434 panelRadio = (JPanel) Beans.instantiate(getClass().getClassLoader(), 435 JPanel.class.getName()); 436 437 radioDirect.setText(res.getString("radioDirect_Text")); 439 radioList.setSelected(true); 440 radioList.setText(res.getString("radioList_Text")); 441 buttonCancel.setPreferredSize(new Dimension(100, 27)); 442 buttonCancel.setText(res.getString("Cancel")); 443 buttonOK.setPreferredSize(new Dimension(100, 27)); 444 buttonOK.setText(res.getString("OK")); 445 446 panelParam.setLayout(paramDeck); 448 comboOpOne.setLightWeightPopupEnabled(false); 449 panelParam.add(comboParam, CARD_COMBO); 450 panelParam.add(textParam, CARD_TEXT); 451 452 panelOpOne.setLayout(opOneDeck); 454 panelOpOne.add(comboOpOne, CARD_COMBO); 455 panelOpOne.add(textOpOne, CARD_TEXT); 456 457 panelRadio.setLayout(layoutRadio); 459 panelRadio.add(radioList, 460 new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 461 GridBagConstraints.CENTER, 462 GridBagConstraints.NONE, 463 new Insets(5, 5, 5, 5), 0, 0)); 464 panelRadio.add(radioDirect, 465 new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 466 GridBagConstraints.CENTER, 467 GridBagConstraints.NONE, 468 new Insets(5, 5, 5, 5), 0, 0)); 469 470 buttonControl.setLayout(layoutControl); 472 buttonControl.add(buttonCancel, 473 new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 474 GridBagConstraints.CENTER, 475 GridBagConstraints.NONE, 476 new Insets(5, 5, 5, 5), 0, 477 0)); 478 buttonControl.add(buttonOK, 479 new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 480 GridBagConstraints.CENTER, 481 GridBagConstraints.NONE, 482 new Insets(5, 5, 5, 5), 0, 483 0)); 484 this.setLayout(layoutMain); 485 this.add(panelParam, 486 new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0, 487 GridBagConstraints.CENTER, 488 GridBagConstraints.HORIZONTAL, 489 new Insets(5, 5, 5, 5), 0, 0)); 490 this.add(panelOpOne, 491 new GridBagConstraints(0, 2, 1, 1, 0.1, 0.0, 492 GridBagConstraints.CENTER, 493 GridBagConstraints.HORIZONTAL, 494 new Insets(5, 5, 5, 5), 0, 0)); 495 this.add(textOpTwo, 496 new GridBagConstraints(0, 3, 1, 1, 0.1, 0.0, 497 GridBagConstraints.CENTER, 498 GridBagConstraints.HORIZONTAL, 499 new Insets(5, 5, 5, 5), 0, 0)); 500 this.add(buttonControl, 501 new GridBagConstraints(0, 4, 1, 1, 0.1, 0.0, 502 GridBagConstraints.CENTER, 503 GridBagConstraints.BOTH, 504 new Insets(5, 5, 5, 5), 0, 0)); 505 this.add(panelRadio, 506 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, 507 GridBagConstraints.CENTER, 508 GridBagConstraints.HORIZONTAL, 509 new Insets(5, 5, 5, 5), 0, 0)); 510 } 511 512 private class LocalRadioListener implements ItemListener { 513 public void itemStateChanged(ItemEvent e) { 514 if (e.getStateChange() == ItemEvent.SELECTED) { 515 toggleParam(); 516 } 517 } 518 519 } 520 521 private class LocalParamListener implements ItemListener { 523 public void itemStateChanged(ItemEvent e) { 524 if (e.getStateChange() == ItemEvent.SELECTED) { 525 initOpts(); 526 } 527 } 528 529 } 530 531 private class LocalOpOneListener implements ItemListener { 533 public void itemStateChanged(ItemEvent e) { 534 if (e.getStateChange() == ItemEvent.SELECTED) { 535 textOpOne.setText(comboOpOne.getModel().getSelectedItem().toString()); 536 } 537 } 538 539 } 540 541 private class LocalButtonListener implements ActionListener { 543 public void actionPerformed(ActionEvent e) { 544 Object source = e.getSource(); 545 546 if (source == buttonOK) { 547 setOption(DialogPanel.OK_OPTION); 548 clearDialog(); 549 } else if (source == buttonCancel) { 550 setOption(DialogPanel.CANCEL_OPTION); 551 clearDialog(); 552 } 553 } 554 555 } 556 557 private class KnownParam { 559 private String param = new String (); 560 private String [] selections = new String [0]; 561 private String [] ops = new String [0]; 562 563 protected KnownParam(String p) { 564 param = p; 565 } 566 567 public String toString() { 568 return param; 569 } 570 571 protected void initOptions(String o) { 572 ops = new String [1]; 573 ops[0] = o; 574 } 575 576 protected void initOptions(String one, String two) { 577 ops = new String [2]; 578 ops[0] = one; 579 ops[1] = two; 580 } 581 582 protected String [] getOptions() { 583 return ops; 584 } 585 586 protected void setSelections(String [] s) { 587 selections = s; 588 ops = new String [1]; 589 ops[0] = selections[0]; 590 } 591 592 protected String [] getSelections() { 593 return selections; 594 } 595 596 protected boolean isValid(String [] array) { 597 boolean valid = false; 598 599 if (array.length == (ops.length + 1)) { 600 if (param.equals(array[0])) { 601 if (selections.length == 0) { 602 valid = true; 603 } else { 604 for (int i = 0; i < selections.length; i++) { 605 if (selections[i].equals(array[1])) { 606 valid = true; 607 break; 608 } 609 } 610 } 611 } 612 } 613 return valid; 614 } 615 616 } 617 } 618
| Popular Tags
|