KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > MemberSpecificationDialog


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 import javax.swing.border.Border JavaDoc;
29
30 import proguard.*;
31 import proguard.classfile.*;
32 import proguard.classfile.util.*;
33 import proguard.util.*;
34
35 /**
36  * This <code>JDialog</code> allows the user to enter a String.
37  *
38  * @author Eric Lafortune
39  */

40 class MemberSpecificationDialog extends JDialog
41 {
42     /**
43      * Return value if the dialog is canceled (with the Cancel button or by
44      * closing the dialog window).
45      */

46     public static final int CANCEL_OPTION = 1;
47
48     /**
49      * Return value if the dialog is approved (with the Ok button).
50      */

51     public static final int APPROVE_OPTION = 0;
52
53
54     private boolean isField;
55
56     private JRadioButton[] publicRadioButtons;
57     private JRadioButton[] privateRadioButtons;
58     private JRadioButton[] protectedRadioButtons;
59     private JRadioButton[] staticRadioButtons;
60     private JRadioButton[] finalRadioButtons;
61
62     private JRadioButton[] volatileRadioButtons;
63     private JRadioButton[] transientRadioButtons;
64
65     private JRadioButton[] synchronizedRadioButtons;
66     private JRadioButton[] nativeRadioButtons;
67     private JRadioButton[] abstractRadioButtons;
68     private JRadioButton[] strictRadioButtons;
69
70     private JTextField annotationTypeTextField = new JTextField(20);
71     private JTextField nameTextField = new JTextField(20);
72     private JTextField typeTextField = new JTextField(20);
73     private JTextField argumentTypesTextField = new JTextField(20);
74     private int returnValue;
75
76
77     public MemberSpecificationDialog(JDialog owner, boolean isField)
78     {
79         super(owner, true);
80         setResizable(true);
81
82         // Create some constraints that can be reused.
83
GridBagConstraints constraints = new GridBagConstraints();
84         constraints.anchor = GridBagConstraints.WEST;
85         constraints.insets = new Insets(1, 2, 1, 2);
86
87         GridBagConstraints constraintsStretch = new GridBagConstraints();
88         constraintsStretch.fill = GridBagConstraints.HORIZONTAL;
89         constraintsStretch.weightx = 1.0;
90         constraintsStretch.anchor = GridBagConstraints.WEST;
91         constraintsStretch.insets = constraints.insets;
92
93         GridBagConstraints constraintsLast = new GridBagConstraints();
94         constraintsLast.gridwidth = GridBagConstraints.REMAINDER;
95         constraintsLast.anchor = GridBagConstraints.WEST;
96         constraintsLast.insets = constraints.insets;
97
98         GridBagConstraints constraintsLastStretch = new GridBagConstraints();
99         constraintsLastStretch.gridwidth = GridBagConstraints.REMAINDER;
100         constraintsLastStretch.fill = GridBagConstraints.HORIZONTAL;
101         constraintsLastStretch.weightx = 1.0;
102         constraintsLastStretch.anchor = GridBagConstraints.WEST;
103         constraintsLastStretch.insets = constraints.insets;
104
105         GridBagConstraints panelConstraints = new GridBagConstraints();
106         panelConstraints.gridwidth = GridBagConstraints.REMAINDER;
107         panelConstraints.fill = GridBagConstraints.HORIZONTAL;
108         panelConstraints.weightx = 1.0;
109         panelConstraints.weighty = 0.0;
110         panelConstraints.anchor = GridBagConstraints.NORTHWEST;
111         panelConstraints.insets = constraints.insets;
112
113         GridBagConstraints stretchPanelConstraints = new GridBagConstraints();
114         stretchPanelConstraints.gridwidth = GridBagConstraints.REMAINDER;
115         stretchPanelConstraints.fill = GridBagConstraints.BOTH;
116         stretchPanelConstraints.weightx = 1.0;
117         stretchPanelConstraints.weighty = 1.0;
118         stretchPanelConstraints.anchor = GridBagConstraints.NORTHWEST;
119         stretchPanelConstraints.insets = constraints.insets;
120
121         GridBagConstraints labelConstraints = new GridBagConstraints();
122         labelConstraints.anchor = GridBagConstraints.CENTER;
123         labelConstraints.insets = new Insets(2, 10, 2, 10);
124
125         GridBagConstraints lastLabelConstraints = new GridBagConstraints();
126         lastLabelConstraints.gridwidth = GridBagConstraints.REMAINDER;
127         lastLabelConstraints.anchor = GridBagConstraints.CENTER;
128         lastLabelConstraints.insets = labelConstraints.insets;
129
130         GridBagConstraints okButtonConstraints = new GridBagConstraints();
131         okButtonConstraints.weightx = 1.0;
132         okButtonConstraints.weighty = 1.0;
133         okButtonConstraints.anchor = GridBagConstraints.SOUTHEAST;
134         okButtonConstraints.insets = new Insets(4, 4, 8, 4);
135
136         GridBagConstraints cancelButtonConstraints = new GridBagConstraints();
137         cancelButtonConstraints.gridwidth = GridBagConstraints.REMAINDER;
138         cancelButtonConstraints.weighty = 1.0;
139         cancelButtonConstraints.anchor = GridBagConstraints.SOUTHEAST;
140         cancelButtonConstraints.insets = okButtonConstraints.insets;
141
142         GridBagLayout layout = new GridBagLayout();
143
144         Border JavaDoc etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
145
146         this.isField = isField;
147
148         // Create the access panel.
149
JPanel accessPanel = new JPanel(layout);
150         accessPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder,
151                                                                GUIResources.getMessage("access")));
152
153         accessPanel.add(Box.createGlue(), labelConstraints);
154         accessPanel.add(new JLabel(GUIResources.getMessage("required")), labelConstraints);
155         accessPanel.add(new JLabel(GUIResources.getMessage("not")), labelConstraints);
156         accessPanel.add(new JLabel(GUIResources.getMessage("dontCare")), labelConstraints);
157         accessPanel.add(Box.createGlue(), constraintsLastStretch);
158
159         publicRadioButtons = addRadioButtonTriplet("Public", accessPanel);
160         privateRadioButtons = addRadioButtonTriplet("Private", accessPanel);
161         protectedRadioButtons = addRadioButtonTriplet("Protected", accessPanel);
162         staticRadioButtons = addRadioButtonTriplet("Static", accessPanel);
163         finalRadioButtons = addRadioButtonTriplet("Final", accessPanel);
164
165         if (isField)
166         {
167             volatileRadioButtons = addRadioButtonTriplet("Volatile", accessPanel);
168             transientRadioButtons = addRadioButtonTriplet("Transient", accessPanel);
169         }
170         else
171         {
172             synchronizedRadioButtons = addRadioButtonTriplet("Synchronized", accessPanel);
173             nativeRadioButtons = addRadioButtonTriplet("Native", accessPanel);
174             abstractRadioButtons = addRadioButtonTriplet("Abstract", accessPanel);
175             strictRadioButtons = addRadioButtonTriplet("Strict", accessPanel);
176         }
177
178         // Create the type panel.
179
JPanel typePanel = new JPanel(layout);
180         typePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder,
181                                                              GUIResources.getMessage(isField ?
182                                                                                          "type" :
183                                                                                          "returnType")));
184
185         typePanel.add(typeTextField, constraintsLastStretch);
186
187         // Create the annotation type panel.
188
JPanel annotationTypePanel = new JPanel(layout);
189         annotationTypePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder,
190                                                              GUIResources.getMessage("annotation")));
191
192         annotationTypePanel.add(annotationTypeTextField, constraintsLastStretch);
193
194         // Create the name panel.
195
JPanel namePanel = new JPanel(layout);
196         namePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder,
197                                                              GUIResources.getMessage("name")));
198
199         namePanel.add(nameTextField, constraintsLastStretch);
200
201         // Create the arguments panel.
202
JPanel argumentsPanel = new JPanel(layout);
203         argumentsPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder,
204                                                                   GUIResources.getMessage("argumentTypes")));
205
206         argumentsPanel.add(argumentTypesTextField, constraintsLastStretch);
207
208         // Create the Ok button.
209
JButton okButton = new JButton(GUIResources.getMessage("ok"));
210         okButton.addActionListener(new ActionListener()
211         {
212             public void actionPerformed(ActionEvent e)
213             {
214                 returnValue = APPROVE_OPTION;
215                 hide();
216             }
217         });
218
219         // Create the Cancel button.
220
JButton cancelButton = new JButton(GUIResources.getMessage("cancel"));
221         cancelButton.addActionListener(new ActionListener()
222         {
223             public void actionPerformed(ActionEvent e)
224             {
225                 hide();
226             }
227         });
228
229         // Add all panels to the main panel.
230
JPanel mainPanel = new JPanel(layout);
231         mainPanel.add(accessPanel, panelConstraints);
232         mainPanel.add(annotationTypePanel, panelConstraints);
233         mainPanel.add(typePanel, panelConstraints);
234         mainPanel.add(namePanel, panelConstraints);
235
236         if (!isField)
237         {
238             mainPanel.add(argumentsPanel, panelConstraints);
239         }
240
241         mainPanel.add(okButton, okButtonConstraints);
242         mainPanel.add(cancelButton, cancelButtonConstraints);
243
244         getContentPane().add(mainPanel);
245     }
246
247
248     /**
249      * Adds a JLabel and three JRadioButton instances in a ButtonGroup to the
250      * given panel with a GridBagLayout, and returns the buttons in an array.
251      */

252     private JRadioButton[] addRadioButtonTriplet(String JavaDoc labelText,
253                                                  JPanel panel)
254     {
255         GridBagConstraints labelConstraints = new GridBagConstraints();
256         labelConstraints.anchor = GridBagConstraints.WEST;
257         labelConstraints.insets = new Insets(2, 10, 2, 10);
258
259         GridBagConstraints buttonConstraints = new GridBagConstraints();
260         buttonConstraints.insets = labelConstraints.insets;
261
262         GridBagConstraints lastGlueConstraints = new GridBagConstraints();
263         lastGlueConstraints.gridwidth = GridBagConstraints.REMAINDER;
264         lastGlueConstraints.weightx = 1.0;
265
266         // Create the radio buttons.
267
JRadioButton radioButton0 = new JRadioButton();
268         JRadioButton radioButton1 = new JRadioButton();
269         JRadioButton radioButton2 = new JRadioButton();
270
271         // Put them in a button group.
272
ButtonGroup buttonGroup = new ButtonGroup();
273         buttonGroup.add(radioButton0);
274         buttonGroup.add(radioButton1);
275         buttonGroup.add(radioButton2);
276
277         // Add the label and the buttons to the panel.
278
panel.add(new JLabel(labelText), labelConstraints);
279         panel.add(radioButton0, buttonConstraints);
280         panel.add(radioButton1, buttonConstraints);
281         panel.add(radioButton2, buttonConstraints);
282         panel.add(Box.createGlue(), lastGlueConstraints);
283
284         return new JRadioButton[]
285         {
286              radioButton0,
287              radioButton1,
288              radioButton2
289         };
290     }
291
292
293     /**
294      * Sets the MemberSpecification to be represented in this dialog.
295      */

296     public void setMemberSpecification(MemberSpecification memberSpecification)
297     {
298         String JavaDoc annotationType = memberSpecification.annotationType;
299         String JavaDoc name = memberSpecification.name;
300         String JavaDoc descriptor = memberSpecification.descriptor;
301
302         // Set the class name text fields.
303
annotationTypeTextField.setText(annotationType == null ? "" : ClassUtil.externalType(annotationType));
304
305         // Set the access radio buttons.
306
setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_PUBLIC, publicRadioButtons);
307         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_PRIVATE, privateRadioButtons);
308         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_PROTECTED, protectedRadioButtons);
309         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_STATIC, staticRadioButtons);
310         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_FINAL, finalRadioButtons);
311         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_VOLATILE, volatileRadioButtons);
312         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_TRANSIENT, transientRadioButtons);
313         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_SYNCHRONIZED, synchronizedRadioButtons);
314         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_NATIVE, nativeRadioButtons);
315         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_ABSTRACT, abstractRadioButtons);
316         setMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_STRICT, strictRadioButtons);
317
318         // Set the class name text fields.
319
nameTextField.setText(name == null ? "" : name);
320
321         if (isField)
322         {
323             typeTextField .setText(descriptor == null ? "" : ClassUtil.externalType(descriptor));
324         }
325         else
326         {
327             typeTextField .setText(descriptor == null ? "" : ClassUtil.externalMethodReturnType(descriptor));
328             argumentTypesTextField.setText(descriptor == null ? "" : ClassUtil.externalMethodArguments(descriptor));
329         }
330     }
331
332
333     /**
334      * Returns the MemberSpecification currently represented in this dialog.
335      */

336     public MemberSpecification getMemberSpecification()
337     {
338         String JavaDoc annotationType = annotationTypeTextField.getText();
339         String JavaDoc name = nameTextField.getText();
340         String JavaDoc type = typeTextField.getText();
341         String JavaDoc arguments = argumentTypesTextField.getText();
342
343         // Convert all class member specifications into the internal format.
344
annotationType = annotationType.equals("") ? null : ClassUtil.internalType(annotationType);
345
346         if (name.equals("") ||
347             name.equals("*"))
348         {
349             name = null;
350         }
351
352         if (type.equals("") ||
353             type.equals("*"))
354         {
355             type = null;
356         }
357
358         if (name != null ||
359             type != null)
360         {
361             if (isField)
362             {
363                 if (type == null)
364                 {
365                     type = ClassConstants.EXTERNAL_TYPE_INT;
366                 }
367
368                 type = ClassUtil.internalType(type);
369             }
370             else
371             {
372                 if (type == null)
373                 {
374                     type = ClassConstants.EXTERNAL_TYPE_VOID;
375                 }
376
377                 type = ClassUtil.internalMethodDescriptor(type, ListUtil.commaSeparatedList(arguments));
378             }
379         }
380
381         MemberSpecification memberSpecification =
382             new MemberSpecification(0, 0, annotationType, name, type);
383
384         // Also get the access radio button settings.
385
getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_PUBLIC, publicRadioButtons);
386         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_PRIVATE, privateRadioButtons);
387         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_PROTECTED, protectedRadioButtons);
388         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_STATIC, staticRadioButtons);
389         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_FINAL, finalRadioButtons);
390         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_VOLATILE, volatileRadioButtons);
391         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_TRANSIENT, transientRadioButtons);
392         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_SYNCHRONIZED, synchronizedRadioButtons);
393         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_NATIVE, nativeRadioButtons);
394         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_ABSTRACT, abstractRadioButtons);
395         getMemberSpecificationRadioButtons(memberSpecification, ClassConstants.INTERNAL_ACC_STRICT, strictRadioButtons);
396
397         return memberSpecification;
398     }
399
400
401     /**
402      * Shows this dialog. This method only returns when the dialog is closed.
403      *
404      * @return <code>CANCEL_OPTION</code> or <code>APPROVE_OPTION</code>,
405      * depending on the choice of the user.
406      */

407     public int showDialog()
408     {
409         returnValue = CANCEL_OPTION;
410
411         // Open the dialog in the right place, then wait for it to be closed,
412
// one way or another.
413
pack();
414         setLocationRelativeTo(getOwner());
415         show();
416
417         return returnValue;
418     }
419
420
421     /**
422      * Sets the appropriate radio button of a given triplet, based on the access
423      * flags of the given keep option.
424      */

425     private void setMemberSpecificationRadioButtons(MemberSpecification memberSpecification,
426                                                          int flag,
427                                                          JRadioButton[] radioButtons)
428     {
429         if (radioButtons != null)
430         {
431             int index = (memberSpecification.requiredSetAccessFlags & flag) != 0 ? 0 :
432                         (memberSpecification.requiredUnsetAccessFlags & flag) != 0 ? 1 :
433                                                                                        2;
434             radioButtons[index].setSelected(true);
435         }
436     }
437
438
439     /**
440      * Updates the access flag of the given keep option, based on the given radio
441      * button triplet.
442      */

443     private void getMemberSpecificationRadioButtons(MemberSpecification memberSpecification,
444                                                     int flag,
445                                                     JRadioButton[] radioButtons)
446     {
447         if (radioButtons != null)
448         {
449             if (radioButtons[0].isSelected())
450             {
451                 memberSpecification.requiredSetAccessFlags |= flag;
452             }
453             else if (radioButtons[1].isSelected())
454             {
455                 memberSpecification.requiredUnsetAccessFlags |= flag;
456             }
457         }
458     }
459 }
460
Popular Tags