KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > PropertyEditorPane


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import java.util.List JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import javax.help.CSH;
6 import javax.help.HelpBroker;
7 import javax.help.DefaultHelpBroker;
8 import java.awt.*;
9
10 /**
11  * This is a top-level editor for properties. It includes buttons for
12  * activating changes, accepting and closing, and cancelling the action.
13  */

14 public class PropertyEditorPane extends JPanel {
15   SwingPropertyEditor editor;
16   PropertyEditorManager manager;
17   Container container;
18   boolean doCommit;
19   JButton defaultButton = null;
20
21   /**
22    * Constructor for subclasses.
23    */

24   PropertyEditorPane() {
25
26   }
27
28   /**
29    * This contructor creates a PropertyEditor using the given
30    * SwingPropertyEditor.
31    */

32   public PropertyEditorPane(PropertyEditorManager newManager,
33                             SwingPropertyEditor newEditor,
34                             Container newContainer) {
35     this(newManager, newEditor, newContainer, true);
36   }
37
38   /**
39    * This contructor creates a PropertyEditor using the given
40    * SwingPropertyEditor.
41    */

42   public PropertyEditorPane(PropertyEditorManager newManager,
43                             SwingPropertyEditor newEditor,
44                             Container newContainer,
45                             boolean newCommit) {
46     manager = newManager;
47     container = newContainer;
48     editor = newEditor;
49     doCommit = newCommit;
50
51     Component editorComponent = editor;
52
53     if (editor instanceof LabelValuePropertyEditor) {
54       JPanel editorPanel = new JPanel();
55       SpringLayout editorPanelLayout = new SpringLayout();
56       editorPanel.setLayout(editorPanelLayout);
57
58       LabelValuePropertyEditor lvEditor = (LabelValuePropertyEditor) editor;
59       editorPanel.add(lvEditor.getLabelComponent());
60       editorPanel.add(lvEditor.getValueComponent());
61
62       editorPanelLayout.putConstraint(SpringLayout.WEST, lvEditor.getLabelComponent(), 5, SpringLayout.WEST, editorPanel);
63       editorPanelLayout.putConstraint(SpringLayout.NORTH, lvEditor.getLabelComponent(), 5, SpringLayout.NORTH, editorPanel);
64       editorPanelLayout.putConstraint(SpringLayout.SOUTH, editorPanel, 5 ,SpringLayout.SOUTH, lvEditor.getLabelComponent());
65       //editorPanelLayout.putConstraint(SpringLayout.SOUTH, lvEditor.getLabelComponent(), -5, SpringLayout.SOUTH, editorPanel);
66

67       editorPanelLayout.putConstraint(SpringLayout.WEST, lvEditor.getValueComponent(), 5 ,SpringLayout.EAST, lvEditor.getLabelComponent());
68
69       editorPanelLayout.putConstraint(SpringLayout.NORTH, lvEditor.getValueComponent(), 5 ,SpringLayout.NORTH, editorPanel);
70       //editorPanelLayout.putConstraint(SpringLayout.SOUTH, editorPanel, 5 ,SpringLayout.SOUTH, lvEditor.getValueComponent());
71
editorPanelLayout.putConstraint(SpringLayout.EAST, editorPanel, 5 ,SpringLayout.EAST, lvEditor.getValueComponent());
72
73       editorComponent = editorPanel;
74     }
75
76     JPanel buttonPanel = createButtonPanel();
77
78     pepLayout(editorComponent, buttonPanel);
79
80     editor.acceptDefaultFocus();
81
82   }
83
84   /**
85    * Does the layout for the PropertyEditorPane.
86    */

87   void pepLayout(Component editorPanel, Component buttonPanel) {
88     SpringLayout layout = new SpringLayout();
89     this.setLayout(layout);
90
91     this.add(editorPanel);
92
93     layout.putConstraint(SpringLayout.WEST, editorPanel, 5, SpringLayout.WEST, this);
94     layout.putConstraint(SpringLayout.NORTH, editorPanel, 5, SpringLayout.NORTH, this);
95     layout.putConstraint(SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH, editorPanel);
96     //layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST, editorPanel);
97

98     this.add(buttonPanel);
99     layout.putConstraint(SpringLayout.NORTH, buttonPanel, 5, SpringLayout.SOUTH, editorPanel);
100
101     layout.putConstraint(SpringLayout.WEST, buttonPanel, 5, SpringLayout.WEST, this);
102     layout.putConstraint(SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH, buttonPanel);
103
104     Spring widthSpring = Spring.constant(0);
105     widthSpring = Spring.max(widthSpring, layout.getConstraints(buttonPanel).getWidth());
106     widthSpring = Spring.max(widthSpring, layout.getConstraints(editorPanel).getWidth());
107
108     layout.putConstraint(SpringLayout.EAST, this, Spring.sum(widthSpring, Spring.constant(10)), SpringLayout.WEST, this);
109
110   }
111
112   /**
113    * Accepts the changes for the edited properties, and writes them to
114    * the PropertyEditorManager.
115    */

116   public void setValue() throws PropertyValueVetoException {
117     editor.setValue();
118   }
119
120   /**
121    * Gets the currently selected values for the edited properties.
122    */

123   public java.util.Properties JavaDoc getValue() {
124     return editor.getValue();
125   }
126
127   /**
128    * Resets the original values for the edited properties.
129    */

130   public void resetDefaultValue() throws PropertyValueVetoException {
131     editor.resetDefaultValue();
132   }
133
134   /**
135    * Creates the appropriate buttons (Ok, Accept, Cancel) to this component.
136    */

137   public JPanel createButtonPanel() {
138     JPanel buttonPanel = new JPanel();
139     SpringLayout buttonLayout = new SpringLayout();
140     buttonPanel.setLayout(buttonLayout);
141
142     JButton helpButton = createButton("PropertyEditor.button.help", new AbstractAction() {
143         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
144           HelpBroker broker = manager.getFactory().getHelpBroker();
145
146           if (container != null && container instanceof JDialog) {
147             ((DefaultHelpBroker)broker).setActivationWindow((JDialog) container);
148           }
149
150           manager.getFactory().getHelpBroker().setCurrentID(editor.getHelpID());
151           manager.getFactory().getHelpBroker().setDisplayed(true);
152         }
153       });
154
155     //CSH.setHelpIDString(helpButton, "UserProfile");
156
buttonPanel.add(helpButton);
157
158     JButton okButton = createButton("PropertyEditor.button.ok", new AbstractAction() {
159         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
160           try {
161             setValue();
162             if (doCommit) {
163               manager.commit();
164             }
165             if (container instanceof JInternalFrame) {
166               try {
167                 ((JInternalFrame)container).setClosed(true);
168               } catch (java.beans.PropertyVetoException JavaDoc pve) {
169               }
170             } else if (container instanceof JFrame) {
171               ((JFrame)container).dispose();
172             } else if (container instanceof JDialog) {
173               ((JDialog)container).dispose();
174             }
175             editor.remove();
176           } catch (PropertyValueVetoException pvve) {
177             manager.getFactory().showError(PropertyEditorPane.this, pvve.getMessage());
178           }
179         }
180       });
181
182     JButton applyButton = createButton("PropertyEditor.button.apply", new AbstractAction() {
183         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
184           try {
185             setValue();
186             if (doCommit) {
187               manager.commit();
188             }
189           } catch (PropertyValueVetoException pvve) {
190             //manager.getFactory().showError(PropertyEditorPane.this, "Error changing value " + pvve.getProperty() + " to " + pvve.getRejectedValue() + ": " + pvve.getReason());
191
manager.getFactory().showError(PropertyEditorPane.this, pvve.getMessage());
192           }
193         }
194       });
195
196     JButton cancelButton = createButton("PropertyEditor.button.cancel", new AbstractAction() {
197         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
198           if (container instanceof JInternalFrame) {
199             try {
200               ((JInternalFrame)container).setClosed(true);
201             } catch (java.beans.PropertyVetoException JavaDoc pve) {
202             }
203           } else if (container instanceof JFrame) {
204             ((JFrame)container).dispose();
205           } else if (container instanceof JDialog) {
206             ((JDialog)container).dispose();
207           }
208           editor.remove();
209         }
210       });
211
212     buttonPanel.add(helpButton);
213     buttonPanel.add(cancelButton);
214     buttonPanel.add(applyButton);
215     buttonPanel.add(okButton);
216
217     Spring buttonWidth = Spring.constant(0);
218     buttonWidth = Spring.max(buttonWidth, buttonLayout.getConstraints(helpButton).getWidth());
219     buttonWidth = Spring.max(buttonWidth, buttonLayout.getConstraints(cancelButton).getWidth());
220     buttonWidth = Spring.max(buttonWidth, buttonLayout.getConstraints(applyButton).getWidth());
221     buttonWidth = Spring.max(buttonWidth, buttonLayout.getConstraints(okButton).getWidth());
222
223     buttonLayout.getConstraints(helpButton).setWidth(buttonWidth);
224     buttonLayout.getConstraints(cancelButton).setWidth(buttonWidth);
225     buttonLayout.getConstraints(applyButton).setWidth(buttonWidth);
226     buttonLayout.getConstraints(okButton).setWidth(buttonWidth);
227
228     buttonLayout.putConstraint(SpringLayout.WEST, helpButton, 5, SpringLayout.WEST, buttonPanel);
229     buttonLayout.putConstraint(SpringLayout.NORTH, helpButton, 5, SpringLayout.NORTH, buttonPanel);
230     buttonLayout.putConstraint(SpringLayout.SOUTH, buttonPanel, 5, SpringLayout.SOUTH, helpButton);
231
232     buttonLayout.putConstraint(SpringLayout.WEST, cancelButton, Spring.constant(5, 5, 32000), SpringLayout.EAST, helpButton);
233     buttonLayout.putConstraint(SpringLayout.NORTH, cancelButton, 5, SpringLayout.NORTH, buttonPanel);
234
235     buttonLayout.putConstraint(SpringLayout.WEST, applyButton, 5, SpringLayout.EAST, cancelButton);
236     buttonLayout.putConstraint(SpringLayout.NORTH, applyButton, 5, SpringLayout.NORTH, buttonPanel);
237
238     buttonLayout.putConstraint(SpringLayout.WEST, okButton, 5, SpringLayout.EAST, applyButton);
239     buttonLayout.putConstraint(SpringLayout.NORTH, okButton, 5, SpringLayout.NORTH, buttonPanel);
240     buttonLayout.putConstraint(SpringLayout.EAST, buttonPanel, 5, SpringLayout.EAST, okButton);
241
242     return buttonPanel;
243   }
244
245   /**
246    * Creates the appropriate Button.
247    */

248   JButton createButton(String JavaDoc key, Action e) {
249     JButton thisButton;
250
251     thisButton = new JButton(manager.getProperty(key +".label", key));
252     String JavaDoc mnemonic = manager.getProperty(key + ".keyBinding", "");
253     if (mnemonic.length() > 0) {
254       thisButton.setMnemonic(mnemonic.charAt(0));
255     }
256
257     if (manager.getProperty(key + ".default", "false").equalsIgnoreCase("true")) {
258       thisButton.setSelected(true);
259       setDefaultButton(thisButton);
260     }
261
262     thisButton.addActionListener(e);
263
264     return thisButton;
265   }
266
267   /**
268    * Returns the Container for this PropertyEditorPane.
269    */

270   public Container getContainer() {
271     return container;
272   }
273
274   /**
275    * Gets the default Button for this PropertyEditorPane.
276    */

277   public JButton getDefaultButton() {
278     return defaultButton;
279   }
280
281   /**
282    * Sets the default Button for this PropertyEditorPane.
283    */

284   public void setDefaultButton(JButton pDefaultButton) {
285     defaultButton = pDefaultButton;
286   }
287 }
288
Popular Tags