001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import java.util.List;
005: import java.util.LinkedList;
006: import java.awt.*;
007:
008: /**
009: * This is a top-level editor for properties. It includes buttons for
010: * activating changes, accepting and closing, and cancelling the action.
011: */
012: public class PropertyEditorPane extends Box {
013: SwingPropertyEditor editor;
014: PropertyEditorManager manager;
015: Container container;
016: Box buttonBox;
017: Box editorBox;
018:
019: /**
020: * This contructor creates a PropertyEditor for the list of
021: * properties in the properties List.
022: */
023: public PropertyEditorPane(PropertyEditorManager newManager,
024: List properties, Container newContainer) {
025: this (newManager, properties, properties, newContainer);
026: }
027:
028: /**
029: * This contructor creates a PropertyEditor for the list of
030: * properties in the properties List, using the template
031: * types in the templates List. Note that there should be
032: * one entry in each of the properties List and the templates
033: * List for each property to be edited.
034: */
035: public PropertyEditorPane(PropertyEditorManager newManager,
036: List properties, List templates, Container newContainer) {
037: super (BoxLayout.Y_AXIS);
038:
039: manager = newManager;
040: container = newContainer;
041:
042: editor = (SwingPropertyEditor) manager.createEditor(properties,
043: templates);
044:
045: editorBox = new Box(BoxLayout.X_AXIS);
046:
047: if (editor.getValueComponent() != null) {
048: if (editor.getLabelComponent() != null)
049: editorBox.add(editor.getLabelComponent());
050: editorBox.add(editor.getValueComponent());
051: } else {
052: editorBox.add(editor);
053: }
054: this .add(editorBox);
055:
056: this .addButtons();
057: }
058:
059: /**
060: * This contructor creates a PropertyEditor using the given
061: * SwingPropertyEditor.
062: */
063: public PropertyEditorPane(PropertyEditorManager newManager,
064: SwingPropertyEditor newEditor, Container newContainer) {
065: super (BoxLayout.Y_AXIS);
066:
067: manager = newManager;
068: container = newContainer;
069:
070: editor = newEditor;
071:
072: this .add(newEditor);
073:
074: this .addButtons();
075: }
076:
077: /**
078: * Accepts the changes for the edited properties, and writes them to
079: * the PropertyEditorManager.
080: */
081: public void setValue() throws PropertyValueVetoException {
082: editor.setValue();
083: }
084:
085: /**
086: * Gets the currently selected values for the edited properties.
087: */
088: public java.util.Properties getValue() {
089: return editor.getValue();
090: }
091:
092: /**
093: * Resets the original values for the edited properties.
094: */
095: public void resetDefaultValue() throws PropertyValueVetoException {
096: editor.resetDefaultValue();
097: }
098:
099: /**
100: * Adds the appropriate buttons (Ok, Accept, Cancel) to this component.
101: */
102: public void addButtons() {
103: buttonBox = new Box(BoxLayout.X_AXIS);
104:
105: buttonBox.add(createButton("Ok", new AbstractAction() {
106: public void actionPerformed(java.awt.event.ActionEvent e) {
107: try {
108: setValue();
109: manager.commit();
110: if (container instanceof JInternalFrame) {
111: try {
112: ((JInternalFrame) container)
113: .setClosed(true);
114: } catch (java.beans.PropertyVetoException pve) {
115: }
116: } else if (container instanceof JFrame) {
117: ((JFrame) container).dispose();
118: }
119: } catch (PropertyValueVetoException pvve) {
120: manager.getFactory().showError(
121: PropertyEditorPane.this ,
122: "Error changing value "
123: + pvve.getProperty() + " to "
124: + pvve.getRejectedValue() + ": "
125: + pvve.getReason());
126: }
127: }
128: }, true));
129:
130: buttonBox.add(createButton("Apply", new AbstractAction() {
131: public void actionPerformed(java.awt.event.ActionEvent e) {
132: try {
133: setValue();
134: manager.commit();
135: } catch (PropertyValueVetoException pvve) {
136: manager.getFactory().showError(
137: PropertyEditorPane.this ,
138: "Error changing value "
139: + pvve.getProperty() + " to "
140: + pvve.getRejectedValue() + ": "
141: + pvve.getReason());
142: }
143: }
144: }, false));
145:
146: buttonBox.add(createButton("Cancel", new AbstractAction() {
147: public void actionPerformed(java.awt.event.ActionEvent e) {
148: if (container instanceof JInternalFrame) {
149: try {
150: ((JInternalFrame) container).setClosed(true);
151: } catch (java.beans.PropertyVetoException pve) {
152: }
153: } else if (container instanceof JFrame) {
154: ((JFrame) container).dispose();
155: }
156: }
157: }, false));
158:
159: this .add(buttonBox);
160: }
161:
162: /**
163: * Creates the appropriate Button.
164: */
165: private JButton createButton(String label, Action e,
166: boolean isDefault) {
167: JButton this Button;
168:
169: this Button = new JButton(manager.getProperty("label." + label,
170: label));
171: String mnemonic = manager.getProperty("label." + label
172: + ".mnemonic", "");
173: if (mnemonic.length() > 0) {
174: this Button.setMnemonic(mnemonic.charAt(0));
175: }
176:
177: this Button.setSelected(isDefault);
178:
179: this Button.addActionListener(e);
180:
181: return this Button;
182: }
183:
184: /**
185: * Resizes this component. Called when a subcomponent changes its size.
186: */
187: public void resizeEditor() {
188: container.setSize(container.getPreferredSize());
189: }
190:
191: }
|