KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > gui > UserStyleDialog


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.util.gui;
20
21 import java.awt.FlowLayout JavaDoc;
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32
33 import javax.swing.AbstractAction JavaDoc;
34 import javax.swing.Action JavaDoc;
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JCheckBox JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JFileChooser JavaDoc;
40 import javax.swing.JFrame JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JOptionPane JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JTextField JavaDoc;
45 import javax.swing.event.ChangeEvent JavaDoc;
46 import javax.swing.event.ChangeListener JavaDoc;
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 /**
54  * This class represents a dialog to select the user style sheet.
55  *
56  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
57  * @version $Id: UserStyleDialog.java,v 1.7 2004/10/30 18:38:06 deweese Exp $
58  */

59 public class UserStyleDialog extends JDialog JavaDoc implements ActionMap {
60
61     /**
62      * The return value if 'OK' is chosen.
63      */

64     public final static int OK_OPTION = 0;
65
66     /**
67      * The return value if 'Cancel' is chosen.
68      */

69     public final static int CANCEL_OPTION = 1;
70
71     /**
72      * The resource file name
73      */

74     protected final static String JavaDoc RESOURCES =
75         "org.apache.batik.util.gui.resources.UserStyleDialog";
76
77     /**
78      * The resource bundle
79      */

80     protected static ResourceBundle JavaDoc bundle;
81
82     /**
83      * The resource manager
84      */

85     protected static ResourceManager resources;
86
87     static {
88         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
89         resources = new ResourceManager(bundle);
90     }
91
92     /**
93      * The main panel.
94      */

95     protected Panel JavaDoc panel;
96
97     /**
98      * The chosen path.
99      */

100     protected String JavaDoc chosenPath;
101
102     /**
103      * The last return code.
104      */

105     protected int returnCode;
106
107     /**
108      * Creates a new user style dialog.
109      */

110     public UserStyleDialog(JFrame JavaDoc 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 JavaDoc());
119         getContentPane().add("South", createButtonsPanel());
120         pack();
121     }
122
123     /**
124      * Shows the dialog.
125      * @return OK_OPTION or CANCEL_OPTION.
126      */

127     public int showDialog() {
128         pack();
129         show();
130         return returnCode;
131     }
132
133     /**
134      * Returns the chosen path or null.
135      */

136     public String JavaDoc getPath() {
137         return chosenPath;
138     }
139
140     /**
141      * Sets the current dialog path.
142      */

143     public void setPath(String JavaDoc s) {
144         chosenPath = s;
145         panel.fileTextField.setText(s);
146         panel.fileCheckBox.setSelected(true);
147     }
148
149     /**
150      * Creates the OK/Cancel buttons panel
151      */

152     protected JPanel JavaDoc createButtonsPanel() {
153         JPanel JavaDoc p = new JPanel JavaDoc(new FlowLayout JavaDoc(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     /**
162      * The action associated with the 'OK' button
163      */

164     protected class OKButtonAction extends AbstractAction JavaDoc {
165         public void actionPerformed(ActionEvent JavaDoc e) {
166             if (panel.fileCheckBox.isSelected()) {
167                 String JavaDoc 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 JavaDoc f = new File JavaDoc(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     /**
195      * The action associated with the 'Cancel' button
196      */

197     protected class CancelButtonAction extends AbstractAction JavaDoc {
198         public void actionPerformed(ActionEvent JavaDoc e) {
199             returnCode = CANCEL_OPTION;
200             dispose();
201         }
202     }
203
204     /**
205      * The map that contains the listeners
206      */

207     protected Map JavaDoc listeners = new HashMap JavaDoc();
208
209     /**
210      * Returns the action associated with the given string
211      * or null on error
212      * @param key the key mapped with the action to get
213      * @throws MissingListenerException if the action is not found
214      */

215     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
216         return (Action JavaDoc)listeners.get(key);
217     }
218
219     /**
220      * This class represents the main panel of the dialog.
221      */

222     public static class Panel extends JPanel JavaDoc {
223         /**
224          * The file check box
225          */

226         protected JCheckBox JavaDoc fileCheckBox;
227
228         /**
229          * The file label
230          */

231         protected JLabel JavaDoc fileLabel;
232
233         /**
234          * The file text field
235          */

236         protected JTextField JavaDoc fileTextField;
237
238         /**
239          * The browse button
240          */

241         protected JButton JavaDoc browseButton;
242
243         /**
244          * Creates a new Panel object.
245          */

246         public Panel() {
247             super(new GridBagLayout JavaDoc());
248             setBorder(BorderFactory.createTitledBorder
249                       (BorderFactory.createEtchedBorder(),
250                        resources.getString("Panel.title")));
251
252             ExtendedGridBagConstraints constraints =
253                 new ExtendedGridBagConstraints();
254             constraints.insets = new Insets JavaDoc(5, 5, 5, 5);
255             
256             fileCheckBox =
257                 new JCheckBox JavaDoc(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 JavaDoc(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 JavaDoc(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         /**
295          * Returns the chosen path or null.
296          */

297         public String JavaDoc getPath() {
298             if(fileCheckBox.isSelected()){
299                 return fileTextField.getText();
300             }
301             else{
302                 return null;
303             }
304         }
305         
306         /**
307          * Sets the current dialog path.
308          */

309         public void setPath(String JavaDoc 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         /**
322          * To listen to the file checkbox
323          */

324         protected class FileCheckBoxChangeListener implements ChangeListener JavaDoc {
325             public void stateChanged(ChangeEvent JavaDoc e) {
326                 boolean selected = fileCheckBox.isSelected();
327                 fileLabel.setEnabled(selected);
328                 fileTextField.setEnabled(selected);
329                 browseButton.setEnabled(selected);
330             }
331         }
332
333         /**
334          * The action associated with the 'browse' button
335          */

336         protected class FileBrowseButtonAction extends AbstractAction JavaDoc {
337             public void actionPerformed(ActionEvent JavaDoc e) {
338                 JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc(new File JavaDoc("."));
339                 fileChooser.setFileHidingEnabled(false);
340
341                 int choice = fileChooser.showOpenDialog(Panel.this);
342                 if (choice == JFileChooser.APPROVE_OPTION) {
343                     File JavaDoc f = fileChooser.getSelectedFile();
344                     try {
345                         fileTextField.setText(f.getCanonicalPath());
346                     } catch (IOException JavaDoc ex) {
347                     }
348                 }
349             }
350         }
351     }
352 }
353
Popular Tags