001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common.properties;
023:
024: // ToolBox imports
025: import org.enhydra.tool.common.DialogPanel;
026: import org.enhydra.tool.common.SwingUtil;
027:
028: // Standard imports
029: import java.awt.*;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.awt.event.ItemEvent;
033: import java.awt.event.ItemListener;
034: import java.beans.*;
035: import java.util.StringTokenizer;
036: import javax.swing.*;
037: import java.util.ResourceBundle;
038:
039: //
040: public class XMLCParameterEditor extends DialogPanel {
041:
042: // strings not to be resourced
043: static ResourceBundle res = ResourceBundle
044: .getBundle("org.enhydra.kelp.common.Res"); // nores
045: private final String CARD_COMBO = "combo"; // nores
046: private final String CARD_TEXT = "text"; // nores
047: private final String GENERATE_BOTH = "-generate both"; // nores
048:
049: //
050: private KnownParam[] knownParams = new KnownParam[0];
051: private LocalOpOneListener opOneListener = null;
052: private LocalParamListener comboListener = null;
053: private LocalRadioListener radioListener = null;
054: private LocalButtonListener buttonListener = null;
055: private GridBagLayout layoutMain = null;
056: private JRadioButton radioDirect = null;
057: private JRadioButton radioList = null;
058: private JPanel panelParam = null;
059: private JPanel panelOpOne = null;
060: private CardLayout paramDeck = null;
061: private CardLayout opOneDeck = null;
062: private JComboBox comboParam = null;
063: private JComboBox comboOpOne = null;
064: private JTextField textParam = null;
065: private JTextField textOpOne = null;
066: private JTextField textOpTwo = null;
067: private JPanel buttonControl = null;
068: private GridBagLayout layoutControl = null;
069: private JButton buttonCancel = null;
070: private JButton buttonOK = null;
071: private JPanel panelRadio = null;
072: private GridBagLayout layoutRadio = null;
073:
074: public static void main(String[] args) {
075: SwingUtil.setLookAndFeelToSystem();
076: XMLCParameterEditor editor = null;
077:
078: editor = new XMLCParameterEditor();
079: editor.showDialog();
080: System.exit(0);
081: }
082:
083: public XMLCParameterEditor() {
084: try {
085: jbInit();
086: pmInit();
087: } catch (Exception ex) {
088: ex.printStackTrace();
089: }
090: }
091:
092: public String getParameter() {
093: String param = new String();
094:
095: if (radioList.isSelected()) {
096: param = getListValue();
097: } else {
098: param = getDirectValue();
099: }
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: //
118: //
119: protected void clearDialog() {
120: super .clearDialog();
121: }
122:
123: //
124: //
125: 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: // default to -for-recomp if unknown
149: if (!isKnown(array)) {
150: array = paramToArray(param);
151: }
152:
153: // setup paramCombo
154: comboParam.setSelectedItem(array[0]);
155: paramDeck.first(panelParam);
156: index = comboParam.getSelectedIndex();
157:
158: // setup option one
159: if (array.length > 1) {
160: selections = knownParams[index].getSelections();
161: textOpOne.setText(array[1]);
162: if (selections.length >= 1) {
163: comboOpOne
164: .setModel(new DefaultComboBoxModel(selections));
165: comboOpOne.getModel().setSelectedItem(array[1]);
166: opOneDeck.first(panelOpOne);
167: } else {
168: textOpOne.setBackground(SystemColor.info);
169: textOpOne.setEnabled(true);
170: opOneDeck.last(panelOpOne);
171: }
172: } else {
173: textOpOne.setBackground(SystemColor.control);
174: textOpOne.setEnabled(false);
175: textOpOne.setText(new String());
176: opOneDeck.last(panelOpOne);
177: }
178:
179: // setup option two
180: if (array.length > 2) {
181: textOpTwo.setBackground(SystemColor.info);
182: textOpTwo.setEnabled(true);
183: textOpTwo.setText(array[2]);
184: } else {
185: textOpTwo.setBackground(SystemColor.control);
186: textOpTwo.setEnabled(false);
187: textOpTwo.setText(new String());
188: }
189: }
190:
191: private void showDirect(String param) {
192: textParam.setText(param);
193: paramDeck.last(panelParam);
194: textOpOne.setBackground(SystemColor.control);
195: textOpOne.setText(new String());
196: textOpOne.setEnabled(false);
197: opOneDeck.last(panelOpOne);
198: textOpTwo.setBackground(SystemColor.control);
199: textOpTwo.setText(new String());
200: textOpTwo.setEnabled(false);
201: }
202:
203: private String[] paramToArray(String param) {
204: StringTokenizer tokenizer = null;
205: String[] array = new String[0];
206: int count = -1;
207:
208: tokenizer = new StringTokenizer(param);
209: count = tokenizer.countTokens();
210: array = new String[count];
211: int i = 0;
212:
213: while (tokenizer.hasMoreTokens()) {
214: array[i] = tokenizer.nextToken();
215: i++;
216: }
217: return array;
218: }
219:
220: private boolean isKnown(String[] array) {
221: boolean known = false;
222:
223: for (int i = 0; i < knownParams.length; i++) {
224: if (knownParams[i].isValid(array)) {
225: known = true;
226: break;
227: }
228: }
229: return known;
230: }
231:
232: private void initOpts() {
233: String[] options = new String[0];
234: String[] selections = new String[0];
235: int index = 0;
236:
237: index = comboParam.getSelectedIndex();
238: if (index == -1) {
239: index = 0;
240: }
241: options = knownParams[index].getOptions();
242:
243: // setup option one
244: if (options.length >= 1) {
245: selections = knownParams[index].getSelections();
246: textOpOne.setText(options[0]);
247: if (selections.length >= 1) {
248: comboOpOne
249: .setModel(new DefaultComboBoxModel(selections));
250: comboOpOne.getModel().setSelectedItem(options[0]);
251: opOneDeck.first(panelOpOne);
252: } else {
253: textOpOne.setBackground(SystemColor.info);
254: textOpOne.setEnabled(true);
255: opOneDeck.last(panelOpOne);
256: }
257: } else {
258: textOpOne.setBackground(SystemColor.control);
259: textOpOne.setEnabled(false);
260: textOpOne.setText(new String());
261: opOneDeck.last(panelOpOne);
262: }
263:
264: // setup option two
265: if (options.length > 1) {
266: textOpTwo.setBackground(SystemColor.info);
267: textOpTwo.setText(options[1]);
268: textOpTwo.setEnabled(true);
269: } else {
270: textOpTwo.setBackground(SystemColor.control);
271: textOpTwo.setText(new String());
272: textOpTwo.setEnabled(false);
273: }
274: }
275:
276: private void toggleParam() {
277: if (radioList.isSelected()) {
278: showList(getDirectValue());
279: } else {
280: showDirect(getListValue());
281: }
282: updateUI();
283: }
284:
285: private void initKnown() {
286:
287: // strings not to be resourced
288: final String[] SELECTION_PARSERS = { "tidy", "swing", "xerces" }; // nores
289: final String[] SELECTION_VALIDATE = { "yes", "no" }; // nores
290: final String[] SELECTION_GENERATE = { "both", "interface",
291: "implementation", "class" }; // nores
292:
293: knownParams = new KnownParam[18];
294: knownParams[0] = new KnownParam("-delete-class"); // nores
295: knownParams[0].initOptions("classname"); // nores
296: knownParams[1] = new KnownParam("-domfactory"); // nores
297: knownParams[1].initOptions("classname"); // nores
298: knownParams[2] = new KnownParam("-extends"); // nores
299: knownParams[2].initOptions("classname"); // nores
300: knownParams[3] = new KnownParam("-for-recomp"); // nores
301: knownParams[4] = new KnownParam("-generate"); // nores
302: knownParams[4].setSelections(SELECTION_GENERATE);
303: knownParams[5] = new KnownParam("-html:encoding"); // nores
304: knownParams[5].initOptions("encoding"); // nores
305: knownParams[6] = new KnownParam("-html:addtagset"); // nores
306: knownParams[6].initOptions("cyberstudio"); // nores
307: knownParams[7] = new KnownParam("-html:addtag"); // nores
308: knownParams[7].initOptions("tagname"); // nores
309: knownParams[7].initOptions("flags"); // nores
310: knownParams[8] = new KnownParam("-html:addattr"); // nores
311: knownParams[8].initOptions("attrname"); // nores
312: knownParams[9] = new KnownParam("-html:old-class-constants"); // nores
313: knownParams[10] = new KnownParam("-html:old-name-constants"); // nores
314: knownParams[11] = new KnownParam("-parser"); // nores
315: knownParams[11].setSelections(SELECTION_PARSERS);
316: knownParams[12] = new KnownParam("-urlmapping"); // nores
317: knownParams[12].initOptions("orgURL", "newURL"); // nores
318: knownParams[13] = new KnownParam("-urlregexmapping"); // nores
319: knownParams[13].initOptions("regexp", "subst"); // nores
320: knownParams[14] = new KnownParam("-urlsetting"); // nores
321: knownParams[14].initOptions("id", "newURL"); // nores
322: knownParams[15] = new KnownParam("-ssi"); // nores
323: knownParams[16] = new KnownParam("-validate"); // nores
324: knownParams[16].setSelections(SELECTION_VALIDATE);
325: knownParams[17] = new KnownParam("-xcatalog"); // nores
326: knownParams[17].initOptions("catalog"); // nores
327: }
328:
329: private void pmInit() {
330: ButtonGroup radioGroup = null;
331: Dimension size = null;
332:
333: // BUTTONS
334: buttonListener = new LocalButtonListener();
335: buttonOK.addActionListener(buttonListener);
336: buttonCancel.addActionListener(buttonListener);
337:
338: // RADIO
339: radioListener = new LocalRadioListener();
340: radioGroup = new ButtonGroup();
341: radioGroup.add(radioList);
342: radioGroup.add(radioDirect);
343: radioList.addItemListener(radioListener);
344: radioDirect.addItemListener(radioListener);
345:
346: // WIDTH
347: this .remove(panelOpOne);
348: this .add(panelOpOne, new GridBagConstraints(0, 2, 2, 1, 0.0,
349: 0.0, GridBagConstraints.CENTER,
350: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
351: 500, 0));
352:
353: // TEXT
354: textOpOne.setText(new String());
355: textOpTwo.setText(new String());
356:
357: // COMBO
358: initKnown();
359: String[] knownStrings = new String[knownParams.length];
360:
361: for (int i = 0; i < knownStrings.length; i++) {
362: knownStrings[i] = knownParams[i].toString();
363: }
364: comboParam.setModel(new DefaultComboBoxModel(knownStrings));
365:
366: // defaults
367: String[] array = paramToArray(GENERATE_BOTH);
368:
369: comboParam.setSelectedItem(array[0]);
370: textParam.setText(GENERATE_BOTH);
371: setParameter(GENERATE_BOTH);
372:
373: // Start listening
374: comboListener = new LocalParamListener();
375: comboParam.addItemListener(comboListener);
376: opOneListener = new LocalOpOneListener();
377: comboOpOne.addItemListener(opOneListener);
378: setTitle(res.getString("XMLC_Parameter"));
379: }
380:
381: private void jbInit() throws Exception {
382: layoutRadio = (GridBagLayout) Beans.instantiate(getClass()
383: .getClassLoader(), GridBagLayout.class.getName());
384: layoutMain = (GridBagLayout) Beans.instantiate(getClass()
385: .getClassLoader(), GridBagLayout.class.getName());
386: radioList = (JRadioButton) Beans.instantiate(getClass()
387: .getClassLoader(), JRadioButton.class.getName());
388: radioDirect = (JRadioButton) Beans.instantiate(getClass()
389: .getClassLoader(), JRadioButton.class.getName());
390: panelOpOne = (JPanel) Beans.instantiate(getClass()
391: .getClassLoader(), JPanel.class.getName());
392: opOneDeck = (CardLayout) Beans.instantiate(getClass()
393: .getClassLoader(), CardLayout.class.getName());
394: panelParam = (JPanel) Beans.instantiate(getClass()
395: .getClassLoader(), JPanel.class.getName());
396: paramDeck = (CardLayout) Beans.instantiate(getClass()
397: .getClassLoader(), CardLayout.class.getName());
398: comboParam = (JComboBox) Beans.instantiate(getClass()
399: .getClassLoader(), JComboBox.class.getName());
400: textParam = (JTextField) Beans.instantiate(getClass()
401: .getClassLoader(), JTextField.class.getName());
402: textOpOne = (JTextField) Beans.instantiate(getClass()
403: .getClassLoader(), JTextField.class.getName());
404: comboOpOne = (JComboBox) Beans.instantiate(getClass()
405: .getClassLoader(), JComboBox.class.getName());
406: textOpTwo = (JTextField) Beans.instantiate(getClass()
407: .getClassLoader(), JTextField.class.getName());
408: buttonControl = (JPanel) Beans.instantiate(getClass()
409: .getClassLoader(), JPanel.class.getName());
410: layoutControl = (GridBagLayout) Beans.instantiate(getClass()
411: .getClassLoader(), GridBagLayout.class.getName());
412: buttonCancel = (JButton) Beans.instantiate(getClass()
413: .getClassLoader(), JButton.class.getName());
414: buttonOK = (JButton) Beans.instantiate(getClass()
415: .getClassLoader(), JButton.class.getName());
416: panelRadio = (JPanel) Beans.instantiate(getClass()
417: .getClassLoader(), JPanel.class.getName());
418:
419: //
420: radioDirect.setText(res.getString("radioDirect_Text"));
421: radioList.setSelected(true);
422: radioList.setText(res.getString("radioList_Text"));
423: buttonCancel.setPreferredSize(new Dimension(100, 27));
424: buttonCancel.setText(res.getString("Cancel"));
425: buttonOK.setPreferredSize(new Dimension(100, 27));
426: buttonOK.setText(res.getString("OK"));
427:
428: //
429: panelParam.setLayout(paramDeck);
430: comboOpOne.setLightWeightPopupEnabled(false);
431: panelParam.add(comboParam, CARD_COMBO);
432: panelParam.add(textParam, CARD_TEXT);
433:
434: //
435: panelOpOne.setLayout(opOneDeck);
436: panelOpOne.add(comboOpOne, CARD_COMBO);
437: panelOpOne.add(textOpOne, CARD_TEXT);
438:
439: //
440: panelRadio.setLayout(layoutRadio);
441: panelRadio.add(radioList, new GridBagConstraints(0, 0, 1, 1,
442: 0.0, 0.0, GridBagConstraints.CENTER,
443: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
444: panelRadio.add(radioDirect, new GridBagConstraints(1, 0, 1, 1,
445: 0.0, 0.0, GridBagConstraints.CENTER,
446: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
447:
448: //
449: buttonControl.setLayout(layoutControl);
450: buttonControl.add(buttonCancel, new GridBagConstraints(1, 0, 1,
451: 1, 0.0, 0.0, GridBagConstraints.CENTER,
452: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
453: buttonControl.add(buttonOK, new GridBagConstraints(0, 0, 1, 1,
454: 0.0, 0.0, GridBagConstraints.CENTER,
455: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
456: this .setLayout(layoutMain);
457: this .add(panelParam, new GridBagConstraints(0, 1, 1, 1, 0.1,
458: 0.0, GridBagConstraints.CENTER,
459: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
460: 0, 0));
461: this .add(panelOpOne, new GridBagConstraints(0, 2, 1, 1, 0.1,
462: 0.0, GridBagConstraints.CENTER,
463: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
464: 0, 0));
465: this .add(textOpTwo, new GridBagConstraints(0, 3, 1, 1, 0.1,
466: 0.0, GridBagConstraints.CENTER,
467: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
468: 0, 0));
469: this .add(buttonControl, new GridBagConstraints(0, 4, 1, 1, 0.1,
470: 0.0, GridBagConstraints.CENTER,
471: GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
472: this .add(panelRadio, new GridBagConstraints(0, 0, 1, 1, 0.1,
473: 0.0, GridBagConstraints.CENTER,
474: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
475: 0, 0));
476: }
477:
478: private class LocalRadioListener implements ItemListener {
479: public void itemStateChanged(ItemEvent e) {
480: if (e.getStateChange() == ItemEvent.SELECTED) {
481: toggleParam();
482: }
483: }
484:
485: }
486:
487: //
488: private class LocalParamListener implements ItemListener {
489: public void itemStateChanged(ItemEvent e) {
490: if (e.getStateChange() == ItemEvent.SELECTED) {
491: initOpts();
492: }
493: }
494:
495: }
496:
497: //
498: private class LocalOpOneListener implements ItemListener {
499: public void itemStateChanged(ItemEvent e) {
500: if (e.getStateChange() == ItemEvent.SELECTED) {
501: textOpOne.setText(comboOpOne.getModel()
502: .getSelectedItem().toString());
503: }
504: }
505:
506: }
507:
508: //
509: private class LocalButtonListener implements ActionListener {
510: public void actionPerformed(ActionEvent e) {
511: Object source = e.getSource();
512:
513: if (source == buttonOK) {
514: setOption(DialogPanel.OK_OPTION);
515: clearDialog();
516: } else if (source == buttonCancel) {
517: setOption(DialogPanel.CANCEL_OPTION);
518: clearDialog();
519: }
520: }
521:
522: }
523:
524: //
525: private class KnownParam {
526: private String param = new String();
527: private String[] selections = new String[0];
528: private String[] ops = new String[0];
529:
530: protected KnownParam(String p) {
531: param = p;
532: }
533:
534: public String toString() {
535: return param;
536: }
537:
538: protected void initOptions(String o) {
539: ops = new String[1];
540: ops[0] = o;
541: }
542:
543: protected void initOptions(String one, String two) {
544: ops = new String[2];
545: ops[0] = one;
546: ops[1] = two;
547: }
548:
549: protected String[] getOptions() {
550: return ops;
551: }
552:
553: protected void setSelections(String[] s) {
554: selections = s;
555: ops = new String[1];
556: ops[0] = selections[0];
557: }
558:
559: protected String[] getSelections() {
560: return selections;
561: }
562:
563: protected boolean isValid(String[] array) {
564: boolean valid = false;
565:
566: if (array.length == (ops.length + 1)) {
567: if (param.equals(array[0])) {
568: if (selections.length == 0) {
569: valid = true;
570: } else {
571: for (int i = 0; i < selections.length; i++) {
572: if (selections[i].equals(array[1])) {
573: valid = true;
574: break;
575: }
576: }
577: }
578: }
579: }
580: return valid;
581: }
582:
583: }
584: }
|