001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import java.util.*;
005: import java.awt.event.ActionListener;
006: import java.awt.event.ActionEvent;
007: import java.awt.CardLayout;
008:
009: /**
010: * This will made a panel which can change depending on
011: * exact properties which are then edited will depend on the value of
012: * another propery.
013: */
014: public class VariableEditorPane extends CompositeSwingPropertyEditor {
015:
016: String keyProperty;
017: HashMap idToEditorMap = new HashMap();
018:
019: boolean scoped;
020:
021: /**
022: * This configures this editor with the following values.
023: *
024: * @param propertyName The property to be edited.
025: * @param template The property that will define the layout of the
026: * editor.
027: * @param manager The PropertyEditorManager that will manage the
028: * changes.
029: * @param isEnabled Whether or not this editor is enabled by default.
030: */
031: public void configureEditor(String propertyName, String template,
032: PropertyEditorManager newManager, boolean isEnabled) {
033: property = propertyName;
034: manager = newManager;
035: editorTemplate = template;
036:
037: debug = manager.getProperty("editors.debug", "false")
038: .equalsIgnoreCase("true");
039:
040: enabled = isEnabled;
041:
042: editors = new Vector();
043:
044: String remove = manager.getProperty(editorTemplate
045: + ".removeString", "");
046: if (!remove.equals(""))
047: property = property.substring(0, property
048: .lastIndexOf(remove));
049:
050: scoped = manager.getProperty(editorTemplate + ".scoped",
051: "false").equalsIgnoreCase("true");
052: if (scoped) {
053: keyProperty = property
054: + "."
055: + manager.getProperty(editorTemplate
056: + ".keyProperty", "");
057: } else {
058: keyProperty = manager.getProperty(editorTemplate
059: + ".keyProperty", "");
060: }
061:
062: if (debug) {
063: System.out.println("Variable: property = " + property
064: + "; keyProperty = " + keyProperty);
065: }
066:
067: manager.addPropertyEditorListener(keyProperty,
068: new PropertyEditorAdapter() {
069: public void propertyChanged(PropertyEditorUI ui,
070: String prop, String newValue) {
071: showPanel(newValue);
072: }
073: });
074:
075: valueComponent = new JPanel();
076: valueComponent.setLayout(new java.awt.CardLayout());
077:
078: String currentValue = manager.getProperty(keyProperty, "");
079: if (currentValue == "") {
080: // check the editor for this, if any.
081: PropertyEditorUI keyEditor = manager
082: .getPropertyEditor(keyProperty);
083: if (keyEditor != null) {
084: currentValue = keyEditor.getValue().getProperty(
085: keyProperty, "");
086: }
087: }
088:
089: showPanel(currentValue);
090:
091: manager.registerPropertyEditor(property, this );
092: }
093:
094: /**
095: * This shows the editor window for the configured value.
096: */
097: public void showPanel(String selectedId) {
098: boolean enableMe = true;
099: boolean resize = false;
100: if (selectedId == null || selectedId.equals("")) {
101: enableMe = false;
102: // check to see the default.
103: //String possibleDefault = manager.getProperty(editorTemplate, "");
104: //if (idToEditorMap.get(possibleDefault) != null) {
105: //selectedId = possibleDefault;
106: //}
107: }
108:
109: CardLayout layout = (CardLayout) valueComponent.getLayout();
110:
111: Object newSelected = idToEditorMap.get(selectedId);
112: if (newSelected == null) {
113: // we'll have to make a new window.
114: if (selectedId == null || selectedId.equals("")) {
115: JPanel jp = new JPanel();
116: valueComponent.add(selectedId, jp);
117: } else {
118:
119: SwingPropertyEditor spe = createEditorPane(selectedId);
120:
121: // save reference to new pane in hash table
122: idToEditorMap.put(selectedId, spe);
123: editors.add(spe);
124:
125: spe.setEnabled(enableMe && enabled);
126: valueComponent.add(selectedId, spe);
127: resize = true;
128: }
129: }
130: layout.show(valueComponent, selectedId);
131: if (resize) {
132: doResize();
133: }
134: }
135:
136: /**
137: * Creates a SwingPropertyEditor for the given subproperty.
138: */
139: public SwingPropertyEditor createEditorPane(String selectedId) {
140:
141: String editValue = selectedId;
142:
143: if (scoped) {
144: editValue = editorTemplate + "." + selectedId;
145: if (debug) {
146: System.out.println("scoped; editValue = " + editValue);
147: }
148: } else {
149: if (debug)
150: System.out.println("not scoped; editValue = "
151: + editValue);
152: }
153:
154: SwingPropertyEditor returnValue = (SwingPropertyEditor) manager
155: .createEditor(property, editValue);
156: return returnValue;
157: }
158: }
|