1 18 package org.apache.batik.util.gui; 19 20 import java.awt.FlowLayout ; 21 import java.awt.GridBagConstraints ; 22 import java.awt.GridBagLayout ; 23 import java.awt.Insets ; 24 import java.awt.event.ActionEvent ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.HashMap ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.ResourceBundle ; 31 32 import javax.swing.AbstractAction ; 33 import javax.swing.Action ; 34 import javax.swing.BorderFactory ; 35 import javax.swing.JButton ; 36 import javax.swing.JDialog ; 37 import javax.swing.JFileChooser ; 38 import javax.swing.JFrame ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JTextField ; 42 import javax.swing.event.DocumentEvent ; 43 import javax.swing.event.DocumentListener ; 44 import javax.swing.filechooser.FileFilter ; 45 46 import org.apache.batik.util.gui.resource.ActionMap; 47 import org.apache.batik.util.gui.resource.ButtonFactory; 48 import org.apache.batik.util.gui.resource.MissingListenerException; 49 import org.apache.batik.util.gui.resource.ResourceManager; 50 51 57 public class URIChooser extends JDialog implements ActionMap { 58 59 62 public final static int OK_OPTION = 0; 63 64 67 public final static int CANCEL_OPTION = 1; 68 69 72 protected final static String RESOURCES = 73 "org.apache.batik.util.gui.resources.URIChooserMessages"; 74 75 78 protected static ResourceBundle bundle; 79 80 83 protected static ResourceManager resources; 84 static { 85 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 86 resources = new ResourceManager(bundle); 87 } 88 89 92 protected ButtonFactory buttonFactory; 93 94 97 protected JTextField textField; 98 99 102 protected JButton okButton; 103 104 107 protected JButton clearButton; 108 109 112 protected String currentPath = "."; 113 114 117 protected FileFilter fileFilter; 118 119 122 protected int returnCode; 123 124 127 protected String chosenPath; 128 129 133 public URIChooser(JDialog d) { 134 super(d); 135 initialize(); 136 } 137 138 142 public URIChooser(JFrame f) { 143 super(f); 144 initialize(); 145 } 146 147 151 public int showDialog() { 152 pack(); 153 show(); 154 return returnCode; 155 } 156 157 160 public String getText() { 161 return chosenPath; 162 } 163 164 167 public void setFileFilter(FileFilter ff) { 168 fileFilter = ff; 169 } 170 171 174 protected void initialize() { 175 setModal(true); 176 177 listeners.put("BrowseButtonAction", new BrowseButtonAction()); 178 listeners.put("OKButtonAction", new OKButtonAction()); 179 listeners.put("CancelButtonAction", new CancelButtonAction()); 180 listeners.put("ClearButtonAction", new ClearButtonAction()); 181 182 setTitle(resources.getString("Dialog.title")); 183 buttonFactory = new ButtonFactory(bundle, this); 184 185 getContentPane().add("North", createURISelectionPanel()); 186 getContentPane().add("South", createButtonsPanel()); 187 } 188 189 192 protected JPanel createURISelectionPanel() { 193 JPanel p = new JPanel (new GridBagLayout ()); 194 p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 195 196 ExtendedGridBagConstraints constraints; 197 constraints = new ExtendedGridBagConstraints(); 198 199 constraints.insets = new Insets (5, 5, 5, 5); 200 constraints.weightx = 0; 201 constraints.weighty = 0; 202 constraints.fill = GridBagConstraints.HORIZONTAL; 203 constraints.setGridBounds(0, 0, 2, 1); 204 p.add(new JLabel (resources.getString("Dialog.label")), constraints); 205 206 textField = new JTextField (30); 207 textField.getDocument().addDocumentListener(new DocumentAdapter()); 208 constraints.weightx = 1.0; 209 constraints.weighty = 0; 210 constraints.fill = GridBagConstraints.HORIZONTAL; 211 constraints.setGridBounds(0, 1, 1, 1); 212 p.add(textField, constraints); 213 214 constraints.weightx = 0; 215 constraints.weighty = 0; 216 constraints.fill = GridBagConstraints.NONE; 217 constraints.setGridBounds(1, 1, 1, 1); 218 p.add(buttonFactory.createJButton("BrowseButton"), constraints); 219 220 return p; 221 } 222 223 226 protected JPanel createButtonsPanel() { 227 JPanel p = new JPanel (new FlowLayout ()); 228 229 p.add(okButton = buttonFactory.createJButton("OKButton")); 230 p.add(buttonFactory.createJButton("CancelButton")); 231 p.add(clearButton = buttonFactory.createJButton("ClearButton")); 232 233 okButton.setEnabled(false); 234 clearButton.setEnabled(false); 235 236 return p; 237 } 238 239 242 protected void updateOKButtonAction() { 243 okButton.setEnabled(!textField.getText().equals("")); 244 } 245 246 249 protected void updateClearButtonAction() { 250 clearButton.setEnabled(!textField.getText().equals("")); 251 } 252 253 256 protected class DocumentAdapter implements DocumentListener { 257 public void changedUpdate(DocumentEvent e) { 258 updateOKButtonAction(); 259 updateClearButtonAction(); 260 } 261 262 public void insertUpdate(DocumentEvent e) { 263 updateOKButtonAction(); 264 updateClearButtonAction(); 265 } 266 267 public void removeUpdate(DocumentEvent e) { 268 updateOKButtonAction(); 269 updateClearButtonAction(); 270 } 271 } 272 273 276 protected class BrowseButtonAction extends AbstractAction { 277 public void actionPerformed(ActionEvent e) { 278 JFileChooser fileChooser = new JFileChooser (currentPath); 279 fileChooser.setFileHidingEnabled(false); 280 fileChooser.setFileSelectionMode 281 (JFileChooser.FILES_AND_DIRECTORIES); 282 if (fileFilter != null) { 283 fileChooser.setFileFilter(fileFilter); 284 } 285 286 int choice = fileChooser.showOpenDialog(URIChooser.this); 287 if (choice == JFileChooser.APPROVE_OPTION) { 288 File f = fileChooser.getSelectedFile(); 289 try { 290 textField.setText(currentPath = f.getCanonicalPath()); 291 } catch (IOException ex) { 292 } 293 } 294 } 295 } 296 297 300 protected class OKButtonAction extends AbstractAction { 301 public void actionPerformed(ActionEvent e) { 302 returnCode = OK_OPTION; 303 chosenPath = textField.getText(); 304 dispose(); 305 } 306 } 307 308 311 protected class CancelButtonAction extends AbstractAction { 312 public void actionPerformed(ActionEvent e) { 313 returnCode = CANCEL_OPTION; 314 dispose(); 315 textField.setText(chosenPath); 316 } 317 } 318 319 322 protected class ClearButtonAction extends AbstractAction { 323 public void actionPerformed(ActionEvent e) { 324 textField.setText(""); 325 } 326 } 327 328 330 333 protected Map listeners = new HashMap (10); 334 335 341 public Action getAction(String key) throws MissingListenerException { 342 return (Action )listeners.get(key); 343 } 344 } 345 | Popular Tags |