KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > FilterDialog


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.gui;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import javax.swing.*;
27 import javax.swing.border.*;
28
29 /**
30  * This <code>JDialog</code> allows the user to enter a String.
31  *
32  * @author Eric Lafortune
33  */

34 public class FilterDialog extends JDialog
35 {
36     /**
37      * Return value if the dialog is canceled (with the Cancel button or by
38      * closing the dialog window).
39      */

40     public static final int CANCEL_OPTION = 1;
41
42     /**
43      * Return value if the dialog is approved (with the Ok button).
44      */

45     public static final int APPROVE_OPTION = 0;
46
47     private static final String JavaDoc DEFAULT_FILTER = "**";
48     private static final String JavaDoc DEFAULT_JAR_FILTER = "**.jar";
49     private static final String JavaDoc DEFAULT_WAR_FILTER = "**.war";
50     private static final String JavaDoc DEFAULT_EAR_FILTER = "**.ear";
51     private static final String JavaDoc DEFAULT_ZIP_FILTER = "**.zip";
52
53
54     private JTextField filterTextField = new JTextField(40);
55     private JTextField jarFilterTextField = new JTextField(40);
56     private JTextField warFilterTextField = new JTextField(40);
57     private JTextField earFilterTextField = new JTextField(40);
58     private JTextField zipFilterTextField = new JTextField(40);
59     private int returnValue;
60
61
62     public FilterDialog(JFrame owner,
63                         String JavaDoc explanation)
64     {
65         super(owner, true);
66         setResizable(true);
67
68         // Create some constraints that can be reused.
69
GridBagConstraints textConstraints = new GridBagConstraints();
70         textConstraints.gridwidth = GridBagConstraints.REMAINDER;
71         textConstraints.fill = GridBagConstraints.HORIZONTAL;
72         textConstraints.weightx = 1.0;
73         textConstraints.weighty = 1.0;
74         textConstraints.anchor = GridBagConstraints.NORTHWEST;
75         textConstraints.insets = new Insets(10, 10, 10, 10);
76
77         GridBagConstraints labelConstraints = new GridBagConstraints();
78         labelConstraints.anchor = GridBagConstraints.WEST;
79         labelConstraints.insets = new Insets(1, 2, 1, 2);
80
81         GridBagConstraints textFieldConstraints = new GridBagConstraints();
82         textFieldConstraints.gridwidth = GridBagConstraints.REMAINDER;
83         textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
84         textFieldConstraints.weightx = 1.0;
85         textFieldConstraints.anchor = GridBagConstraints.WEST;
86         textFieldConstraints.insets = labelConstraints.insets;
87
88         GridBagConstraints panelConstraints = new GridBagConstraints();
89         panelConstraints.gridwidth = GridBagConstraints.REMAINDER;
90         panelConstraints.fill = GridBagConstraints.HORIZONTAL;
91         panelConstraints.weightx = 1.0;
92         panelConstraints.weighty = 0.0;
93         panelConstraints.anchor = GridBagConstraints.NORTHWEST;
94         panelConstraints.insets = labelConstraints.insets;
95
96         GridBagConstraints okButtonConstraints = new GridBagConstraints();
97         okButtonConstraints.weightx = 1.0;
98         okButtonConstraints.weighty = 1.0;
99         okButtonConstraints.anchor = GridBagConstraints.SOUTHEAST;
100         okButtonConstraints.insets = new Insets(4, 4, 8, 4);
101
102         GridBagConstraints cancelButtonConstraints = new GridBagConstraints();
103         cancelButtonConstraints.gridwidth = GridBagConstraints.REMAINDER;
104         cancelButtonConstraints.weighty = 1.0;
105         cancelButtonConstraints.anchor = GridBagConstraints.SOUTHEAST;
106         cancelButtonConstraints.insets = okButtonConstraints.insets;
107
108         GridBagLayout layout = new GridBagLayout();
109
110         Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
111
112         // Create the panel with the explanation.
113
JTextArea explanationTextArea = new JTextArea(explanation, 3, 0);
114         explanationTextArea.setOpaque(false);
115         explanationTextArea.setEditable(false);
116         explanationTextArea.setLineWrap(true);
117         explanationTextArea.setWrapStyleWord(true);
118
119         // Create the filter labels.
120
JLabel filterLabel = new JLabel(GUIResources.getMessage("nameFilter"));
121         JLabel jarFilterLabel = new JLabel(GUIResources.getMessage("jarNameFilter"));
122         JLabel warFilterLabel = new JLabel(GUIResources.getMessage("warNameFilter"));
123         JLabel earFilterLabel = new JLabel(GUIResources.getMessage("earNameFilter"));
124         JLabel zipFilterLabel = new JLabel(GUIResources.getMessage("zipNameFilter"));
125
126         // Create the filter panel.
127
JPanel filterPanel = new JPanel(layout);
128         filterPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder,
129                                                                GUIResources.getMessage("filters")));
130
131         filterPanel.add(explanationTextArea, textConstraints);
132
133         filterPanel.add(filterLabel, labelConstraints);
134         filterPanel.add(filterTextField, textFieldConstraints);
135
136         filterPanel.add(jarFilterLabel, labelConstraints);
137         filterPanel.add(jarFilterTextField, textFieldConstraints);
138
139         filterPanel.add(warFilterLabel, labelConstraints);
140         filterPanel.add(warFilterTextField, textFieldConstraints);
141
142         filterPanel.add(earFilterLabel, labelConstraints);
143         filterPanel.add(earFilterTextField, textFieldConstraints);
144
145         filterPanel.add(zipFilterLabel, labelConstraints);
146         filterPanel.add(zipFilterTextField, textFieldConstraints);
147
148
149         JButton okButton = new JButton(GUIResources.getMessage("ok"));
150         okButton.addActionListener(new ActionListener()
151         {
152             public void actionPerformed(ActionEvent e)
153             {
154                 returnValue = APPROVE_OPTION;
155                 hide();
156             }
157         });
158
159         JButton cancelButton = new JButton(GUIResources.getMessage("cancel"));
160         cancelButton.addActionListener(new ActionListener()
161         {
162             public void actionPerformed(ActionEvent e)
163             {
164                 hide();
165             }
166         });
167
168         // Add all panels to the main panel.
169
JPanel mainPanel = new JPanel(layout);
170         mainPanel.add(filterPanel, panelConstraints);
171         mainPanel.add(okButton, okButtonConstraints);
172         mainPanel.add(cancelButton, cancelButtonConstraints);
173
174         getContentPane().add(mainPanel);
175     }
176
177
178     /**
179      * Sets the filter to be represented in this dialog.
180      */

181     public void setFilter(String JavaDoc filter)
182     {
183         filterTextField.setText(filter != null ? filter : DEFAULT_FILTER);
184     }
185
186
187     /**
188      * Returns the filter currently represented in this dialog.
189      */

190     public String JavaDoc getFilter()
191     {
192         String JavaDoc filter = filterTextField.getText();
193
194         return filter.equals(DEFAULT_FILTER) ? null : filter;
195     }
196
197
198     /**
199      * Sets the jar filter to be represented in this dialog.
200      */

201     public void setJarFilter(String JavaDoc filter)
202     {
203         jarFilterTextField.setText(filter != null ? filter : DEFAULT_JAR_FILTER);
204     }
205
206
207     /**
208      * Returns the jar filter currently represented in this dialog.
209      */

210     public String JavaDoc getJarFilter()
211     {
212         String JavaDoc filter = jarFilterTextField.getText();
213
214         return filter.equals(DEFAULT_JAR_FILTER) ? null : filter;
215     }
216
217
218     /**
219      * Sets the war filter to be represented in this dialog.
220      */

221     public void setWarFilter(String JavaDoc filter)
222     {
223         warFilterTextField.setText(filter != null ? filter : DEFAULT_WAR_FILTER);
224     }
225
226
227     /**
228      * Returns the war filter currently represented in this dialog.
229      */

230     public String JavaDoc getWarFilter()
231     {
232         String JavaDoc filter = warFilterTextField.getText();
233
234         return filter.equals(DEFAULT_WAR_FILTER) ? null : filter;
235     }
236
237
238     /**
239      * Sets the ear filter to be represented in this dialog.
240      */

241     public void setEarFilter(String JavaDoc filter)
242     {
243         earFilterTextField.setText(filter != null ? filter : DEFAULT_EAR_FILTER);
244     }
245
246
247     /**
248      * Returns the ear filter currently represented in this dialog.
249      */

250     public String JavaDoc getEarFilter()
251     {
252         String JavaDoc filter = earFilterTextField.getText();
253
254         return filter.equals(DEFAULT_EAR_FILTER) ? null : filter;
255     }
256
257
258     /**
259      * Sets the zip filter to be represented in this dialog.
260      */

261     public void setZipFilter(String JavaDoc filter)
262     {
263         zipFilterTextField.setText(filter != null ? filter : DEFAULT_ZIP_FILTER);
264     }
265
266
267     /**
268      * Returns the zip filter currently represented in this dialog.
269      */

270     public String JavaDoc getZipFilter()
271     {
272         String JavaDoc filter = zipFilterTextField.getText();
273
274         return filter.equals(DEFAULT_ZIP_FILTER) ? null : filter;
275     }
276
277
278     /**
279      * Shows this dialog. This method only returns when the dialog is closed.
280      *
281      * @return <code>CANCEL_OPTION</code> or <code>APPROVE_OPTION</code>,
282      * depending on the choice of the user.
283      */

284     public int showDialog()
285     {
286         returnValue = CANCEL_OPTION;
287
288         // Open the dialog in the right place, then wait for it to be closed,
289
// one way or another.
290
pack();
291         setLocationRelativeTo(getOwner());
292         show();
293
294         return returnValue;
295     }
296 }
297
Popular Tags