001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import java.util.Vector;
005: import java.util.List;
006: import net.suberic.util.VariableBundle;
007:
008: /**
009: * This is a Property Editor which displays a group of properties.
010: * These properties should all be definte by a single property.
011: *
012: * An example:
013: *
014: * Configuration=foo:bar
015: * Configuration.propertyType=Composite
016: * Configuration.scoped=false
017: * foo=zork
018: * bar=frobozz
019: *
020: * Options:
021: *
022: * Configuration.scoped - shows that the properties listed are subproperties
023: * of both the property and the template. So, in this example, if you
024: * had Configuration.scoped, the properties edited would be
025: * Configuration.foo and Configuration.bar
026: * Configuration.scopeRoot - if the setting is scoped, then this is the
027: * root for the template's scope. Useful when dealing with properties that
028: * can be reached from multiple points (i.e. if
029: * Configuration.one.two.three=foo:bar also, then you could set the
030: * scopeRoot to Configuration and use the already configured foo and bar.
031: * Configuration.subProperty.addSubProperty - shows whether or not you
032: * should add the given subproperty to the edited property for this editor.
033: * Useful if you have a CompositeEditorPane that contains other
034: * Composite or Tabbed EditorPanes. If Configuration.foo is another
035: * CompositeEditorPane which in turn edits .frotz and .ozmoo, and
036: * Configuration.foo.addSubProperty=true (the default), then
037: * Configuration.foo.frotz and Configuration.foo.ozmoo will be edited.
038: * If Configuration.foo.addSubProperty=false, then Configuration.frotz
039: * and Configuration.ozmoo will be edited, using Configuration.foo.frotz
040: * and Configuration.foo.ozmoo as templates. This is primarily useful
041: * when using MultiEditorPanes.
042: *
043: */
044: public class CompositeEditorPane extends CompositeSwingPropertyEditor {
045: boolean scoped;
046:
047: /**
048: * Creates a CompositeEditorPane.
049: */
050: public CompositeEditorPane() {
051:
052: }
053:
054: /**
055: * Creates a CompositeEditorPane editing the given list.
056: */
057: public CompositeEditorPane(List properties, List templates,
058: PropertyEditorManager mgr) {
059: configureEditor(properties, templates, mgr);
060: }
061:
062: /**
063: * This configures this editor with the following values.
064: *
065: * @param propertyName The property to be edited.
066: * @param template The property that will define the layout of the
067: * editor.
068: * @param manager The PropertyEditorManager that will manage the
069: * changes.
070: * @param isEnabled Whether or not this editor is enabled by default.
071: */
072: public void configureEditor(String propertyName, String template,
073: PropertyEditorManager newManager, boolean isEnabled) {
074: property = propertyName;
075: manager = newManager;
076: editorTemplate = template;
077: enabled = isEnabled;
078: originalValue = manager.getProperty(property, "");
079:
080: this .setBorder(BorderFactory.createEtchedBorder());
081:
082: debug = manager.getProperty("editors.debug", "false")
083: .equalsIgnoreCase("true");
084:
085: if (debug) {
086: System.out.println("creating CompositeEditorPane for "
087: + property + " with template " + editorTemplate);
088: }
089:
090: scoped = manager.getProperty(template + ".scoped", "false")
091: .equalsIgnoreCase("true");
092:
093: if (debug) {
094: System.out.println("manager.getProperty ("
095: + template
096: + ".scoped) = "
097: + manager
098: .getProperty(template + ".scoped", "false")
099: + " = " + scoped);
100: }
101:
102: List properties = new Vector();
103: List templates = new Vector();
104:
105: if (scoped) {
106: if (debug) {
107: System.out.println("testing for template " + template);
108: }
109: String scopeRoot = manager.getProperty(template
110: + ".scopeRoot", template);
111: if (debug) {
112: System.out.println("scopeRoot is " + scopeRoot);
113: }
114: List templateNames = manager
115: .getPropertyAsList(template, "");
116: if (debug) {
117: System.out.println("templateNames = getProp("
118: + template + ") = "
119: + manager.getProperty(template, ""));
120: }
121:
122: for (int i = 0; i < templateNames.size(); i++) {
123: String propToEdit = null;
124: String currentSubProperty = (String) templateNames
125: .get(i);
126: if (manager.getProperty(
127: scopeRoot + "." + currentSubProperty
128: + ".addSubProperty", "true")
129: .equalsIgnoreCase("false")) {
130: propToEdit = property;
131: } else {
132: propToEdit = property + "."
133: + (String) templateNames.get(i);
134: }
135: String templateToEdit = scopeRoot + "."
136: + (String) templateNames.get(i);
137: properties.add(propToEdit);
138: templates.add(templateToEdit);
139: if (debug) {
140: System.out.println("adding " + propToEdit
141: + ", template " + templateToEdit);
142: }
143: }
144: } else {
145: if (debug) {
146: System.out
147: .println("creating prop list for Composite EP using "
148: + property + ", " + template);
149: }
150: properties = manager.getPropertyAsList(property, "");
151: templates = manager.getPropertyAsList(template, "");
152: }
153:
154: addEditors(properties, templates);
155: }
156:
157: public void addEditors(List properties, List templates) {
158: SwingPropertyEditor currentEditor;
159:
160: editors = new Vector();
161:
162: java.awt.GridBagConstraints constraints = new java.awt.GridBagConstraints();
163: constraints.insets = new java.awt.Insets(1, 3, 0, 3);
164:
165: java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
166: JPanel contentPanel = new JPanel();
167: contentPanel.setLayout(layout);
168:
169: constraints.weightx = 1.0;
170: constraints.fill = java.awt.GridBagConstraints.BOTH;
171:
172: if (debug) {
173: System.out.println("creating editors for "
174: + properties.size() + " properties.");
175: }
176:
177: for (int i = 0; i < properties.size(); i++) {
178: currentEditor = (SwingPropertyEditor) manager.createEditor(
179: (String) properties.get(i), (String) templates
180: .get(i));
181: currentEditor.setEnabled(enabled);
182: editors.add(currentEditor);
183:
184: if (currentEditor.valueComponent != null) {
185: if (currentEditor.labelComponent != null) {
186: layout.setConstraints(currentEditor.labelComponent,
187: constraints);
188: contentPanel.add(currentEditor.labelComponent);
189: }
190: constraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
191: layout.setConstraints(currentEditor.valueComponent,
192: constraints);
193: contentPanel.add(currentEditor.valueComponent);
194: } else {
195: constraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
196: layout.setConstraints(currentEditor, constraints);
197: contentPanel.add(currentEditor);
198: }
199:
200: constraints.weightx = 0.0;
201: constraints.gridwidth = 1;
202:
203: }
204:
205: this .add(contentPanel);
206: alignEditorSizes();
207:
208: manager.registerPropertyEditor(property, this );
209: }
210:
211: /**
212: * This configures this editor with the following values.
213: *
214: * @param propertyName The property to be edited.
215: * @param template The property that will define the layout of the
216: * editor.
217: * @param manager The PropertyEditorManager that will manage the
218: * changes.
219: * @param isEnabled Whether or not this editor is enabled by default.
220: */
221: public void configureEditor(List properties, List templates,
222: PropertyEditorManager newManager) {
223: manager = newManager;
224: enabled = true;
225:
226: this .setBorder(BorderFactory.createEtchedBorder());
227:
228: debug = manager.getProperty("editors.debug", "false")
229: .equalsIgnoreCase("true");
230:
231: addEditors(properties, templates);
232: }
233:
234: /**
235: * This should even out the various editors on the panel.
236: */
237: public void alignEditorSizes() {
238: int labelWidth = 0;
239: int valueWidth = 0;
240: int totalWidth = 0;
241: for (int i = 0; i < editors.size(); i++) {
242: labelWidth = Math.max(labelWidth,
243: ((SwingPropertyEditor) editors.get(i))
244: .getMinimumLabelSize().width);
245: valueWidth = Math.max(valueWidth,
246: ((SwingPropertyEditor) editors.get(i))
247: .getMinimumValueSize().width);
248: totalWidth = Math.max(totalWidth,
249: ((SwingPropertyEditor) editors.get(i))
250: .getMinimumTotalSize().width);
251: }
252:
253: if (totalWidth > labelWidth + valueWidth) {
254: int difference = totalWidth - labelWidth - valueWidth;
255: labelWidth = labelWidth + (difference / 2);
256: valueWidth = totalWidth - labelWidth;
257: }
258:
259: for (int i = 0; i < editors.size(); i++) {
260: ((SwingPropertyEditor) editors.get(i)).setWidths(
261: labelWidth, valueWidth);
262: }
263:
264: }
265:
266: }
|