1 18 19 package org.apache.batik.util.gui; 20 21 import java.awt.FlowLayout ; 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.event.ActionEvent ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 import java.util.ResourceBundle ; 32 33 import javax.swing.AbstractAction ; 34 import javax.swing.Action ; 35 import javax.swing.BorderFactory ; 36 import javax.swing.JButton ; 37 import javax.swing.JCheckBox ; 38 import javax.swing.JDialog ; 39 import javax.swing.JFileChooser ; 40 import javax.swing.JFrame ; 41 import javax.swing.JLabel ; 42 import javax.swing.JOptionPane ; 43 import javax.swing.JPanel ; 44 import javax.swing.JTextField ; 45 import javax.swing.event.ChangeEvent ; 46 import javax.swing.event.ChangeListener ; 47 48 import org.apache.batik.util.gui.resource.ActionMap; 49 import org.apache.batik.util.gui.resource.ButtonFactory; 50 import org.apache.batik.util.gui.resource.MissingListenerException; 51 import org.apache.batik.util.gui.resource.ResourceManager; 52 53 59 public class UserStyleDialog extends JDialog implements ActionMap { 60 61 64 public final static int OK_OPTION = 0; 65 66 69 public final static int CANCEL_OPTION = 1; 70 71 74 protected final static String RESOURCES = 75 "org.apache.batik.util.gui.resources.UserStyleDialog"; 76 77 80 protected static ResourceBundle bundle; 81 82 85 protected static ResourceManager resources; 86 87 static { 88 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 89 resources = new ResourceManager(bundle); 90 } 91 92 95 protected Panel panel; 96 97 100 protected String chosenPath; 101 102 105 protected int returnCode; 106 107 110 public UserStyleDialog(JFrame f) { 111 super(f); 112 setModal(true); 113 setTitle(resources.getString("Dialog.title")); 114 115 listeners.put("OKButtonAction", new OKButtonAction()); 116 listeners.put("CancelButtonAction", new CancelButtonAction()); 117 118 getContentPane().add(panel = new Panel ()); 119 getContentPane().add("South", createButtonsPanel()); 120 pack(); 121 } 122 123 127 public int showDialog() { 128 pack(); 129 show(); 130 return returnCode; 131 } 132 133 136 public String getPath() { 137 return chosenPath; 138 } 139 140 143 public void setPath(String s) { 144 chosenPath = s; 145 panel.fileTextField.setText(s); 146 panel.fileCheckBox.setSelected(true); 147 } 148 149 152 protected JPanel createButtonsPanel() { 153 JPanel p = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 154 ButtonFactory bf = new ButtonFactory(bundle, this); 155 p.add(bf.createJButton("OKButton")); 156 p.add(bf.createJButton("CancelButton")); 157 158 return p; 159 } 160 161 164 protected class OKButtonAction extends AbstractAction { 165 public void actionPerformed(ActionEvent e) { 166 if (panel.fileCheckBox.isSelected()) { 167 String path = panel.fileTextField.getText(); 168 if (path.equals("")) { 169 JOptionPane.showMessageDialog 170 (UserStyleDialog.this, 171 resources.getString("StyleDialogError.text"), 172 resources.getString("StyleDialogError.title"), 173 JOptionPane.ERROR_MESSAGE); 174 return; 175 } else { 176 File f = new File (path); 177 if (f.exists()) { 178 if (f.isDirectory()) { 179 path = null; 180 } else { 181 path = "file:" + path; 182 } 183 } 184 chosenPath = path; 185 } 186 } else { 187 chosenPath = null; 188 } 189 returnCode = OK_OPTION; 190 dispose(); 191 } 192 } 193 194 197 protected class CancelButtonAction extends AbstractAction { 198 public void actionPerformed(ActionEvent e) { 199 returnCode = CANCEL_OPTION; 200 dispose(); 201 } 202 } 203 204 207 protected Map listeners = new HashMap (); 208 209 215 public Action getAction(String key) throws MissingListenerException { 216 return (Action )listeners.get(key); 217 } 218 219 222 public static class Panel extends JPanel { 223 226 protected JCheckBox fileCheckBox; 227 228 231 protected JLabel fileLabel; 232 233 236 protected JTextField fileTextField; 237 238 241 protected JButton browseButton; 242 243 246 public Panel() { 247 super(new GridBagLayout ()); 248 setBorder(BorderFactory.createTitledBorder 249 (BorderFactory.createEtchedBorder(), 250 resources.getString("Panel.title"))); 251 252 ExtendedGridBagConstraints constraints = 253 new ExtendedGridBagConstraints(); 254 constraints.insets = new Insets (5, 5, 5, 5); 255 256 fileCheckBox = 257 new JCheckBox (resources.getString("PanelFileCheckBox.text")); 258 fileCheckBox.addChangeListener(new FileCheckBoxChangeListener()); 259 constraints.weightx = 0; 260 constraints.weighty = 0; 261 constraints.fill = GridBagConstraints.HORIZONTAL; 262 constraints.setGridBounds(0, 2, 3, 1); 263 this.add(fileCheckBox, constraints); 264 265 fileLabel = new JLabel (resources.getString("PanelFileLabel.text")); 266 constraints.weightx = 0; 267 constraints.weighty = 0; 268 constraints.fill = GridBagConstraints.HORIZONTAL; 269 constraints.setGridBounds(0, 3, 3, 1); 270 this.add(fileLabel, constraints); 271 272 fileTextField = new JTextField (30); 273 constraints.weightx = 1.0; 274 constraints.weighty = 0; 275 constraints.fill = GridBagConstraints.HORIZONTAL; 276 constraints.setGridBounds(0, 4, 2, 1); 277 this.add(fileTextField, constraints); 278 279 ButtonFactory bf = new ButtonFactory(bundle, null); 280 constraints.weightx = 0; 281 constraints.weighty = 0; 282 constraints.fill = GridBagConstraints.NONE; 283 constraints.anchor = GridBagConstraints.EAST; 284 constraints.setGridBounds(2, 4, 1, 1); 285 browseButton = bf.createJButton("PanelFileBrowseButton"); 286 this.add(browseButton, constraints); 287 browseButton.addActionListener(new FileBrowseButtonAction()); 288 289 fileLabel.setEnabled(false); 290 fileTextField.setEnabled(false); 291 browseButton.setEnabled(false); 292 } 293 294 297 public String getPath() { 298 if(fileCheckBox.isSelected()){ 299 return fileTextField.getText(); 300 } 301 else{ 302 return null; 303 } 304 } 305 306 309 public void setPath(String s) { 310 if(s == null){ 311 fileTextField.setEnabled(false); 312 fileCheckBox.setSelected(false); 313 } 314 else{ 315 fileTextField.setEnabled(true); 316 fileTextField.setText(s); 317 fileCheckBox.setSelected(true); 318 } 319 } 320 321 324 protected class FileCheckBoxChangeListener implements ChangeListener { 325 public void stateChanged(ChangeEvent e) { 326 boolean selected = fileCheckBox.isSelected(); 327 fileLabel.setEnabled(selected); 328 fileTextField.setEnabled(selected); 329 browseButton.setEnabled(selected); 330 } 331 } 332 333 336 protected class FileBrowseButtonAction extends AbstractAction { 337 public void actionPerformed(ActionEvent e) { 338 JFileChooser fileChooser = new JFileChooser (new File (".")); 339 fileChooser.setFileHidingEnabled(false); 340 341 int choice = fileChooser.showOpenDialog(Panel.this); 342 if (choice == JFileChooser.APPROVE_OPTION) { 343 File f = fileChooser.getSelectedFile(); 344 try { 345 fileTextField.setText(f.getCanonicalPath()); 346 } catch (IOException ex) { 347 } 348 } 349 } 350 } 351 } 352 } 353 | Popular Tags |