1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import java.util.List ; 4 import java.util.LinkedList ; 5 import javax.help.CSH; 6 import javax.help.HelpBroker; 7 import javax.help.DefaultHelpBroker; 8 import java.awt.*; 9 10 14 public class PropertyEditorPane extends JPanel { 15 SwingPropertyEditor editor; 16 PropertyEditorManager manager; 17 Container container; 18 boolean doCommit; 19 JButton defaultButton = null; 20 21 24 PropertyEditorPane() { 25 26 } 27 28 32 public PropertyEditorPane(PropertyEditorManager newManager, 33 SwingPropertyEditor newEditor, 34 Container newContainer) { 35 this(newManager, newEditor, newContainer, true); 36 } 37 38 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 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.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 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 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 116 public void setValue() throws PropertyValueVetoException { 117 editor.setValue(); 118 } 119 120 123 public java.util.Properties getValue() { 124 return editor.getValue(); 125 } 126 127 130 public void resetDefaultValue() throws PropertyValueVetoException { 131 editor.resetDefaultValue(); 132 } 133 134 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 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 buttonPanel.add(helpButton); 157 158 JButton okButton = createButton("PropertyEditor.button.ok", new AbstractAction() { 159 public void actionPerformed(java.awt.event.ActionEvent 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 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 e) { 184 try { 185 setValue(); 186 if (doCommit) { 187 manager.commit(); 188 } 189 } catch (PropertyValueVetoException pvve) { 190 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 e) { 198 if (container instanceof JInternalFrame) { 199 try { 200 ((JInternalFrame)container).setClosed(true); 201 } catch (java.beans.PropertyVetoException 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 248 JButton createButton(String key, Action e) { 249 JButton thisButton; 250 251 thisButton = new JButton(manager.getProperty(key +".label", key)); 252 String 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 270 public Container getContainer() { 271 return container; 272 } 273 274 277 public JButton getDefaultButton() { 278 return defaultButton; 279 } 280 281 284 public void setDefaultButton(JButton pDefaultButton) { 285 defaultButton = pDefaultButton; 286 } 287 } 288 | Popular Tags |