KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > ClassSpecificationsPanel


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 proguard.*;
24 import proguard.classfile.util.ClassUtil;
25
26 import java.awt.Component JavaDoc;
27 import java.awt.event.*;
28 import java.util.*;
29
30 import javax.swing.*;
31
32
33 /**
34  * This <code>ListPanel</code> allows the user to add, edit, move, and remove
35  * ClassSpecification entries in a list.
36  *
37  * @author Eric Lafortune
38  */

39 class ClassSpecificationsPanel extends ListPanel
40 {
41     protected ClassSpecificationDialog classSpecificationDialog;
42
43
44     public ClassSpecificationsPanel(JFrame owner, boolean fullKeepOptions)
45     {
46         super();
47
48         list.setCellRenderer(new MyListCellRenderer());
49
50         classSpecificationDialog = new ClassSpecificationDialog(owner, fullKeepOptions);
51
52         addAddButton();
53         addEditButton();
54         addRemoveButton();
55         addUpButton();
56         addDownButton();
57
58         enableSelectionButtons();
59     }
60
61
62     protected void addAddButton()
63     {
64         JButton addButton = new JButton(GUIResources.getMessage("add"));
65         addButton.addActionListener(new ActionListener()
66         {
67             public void actionPerformed(ActionEvent e)
68             {
69                 setClassSpecification(createClassSpecification());
70                 int returnValue = classSpecificationDialog.showDialog();
71                 if (returnValue == ClassSpecificationDialog.APPROVE_OPTION)
72                 {
73                     // Add the new element.
74
addElement(getClassSpecification());
75                 }
76             }
77         });
78
79         addButton(addButton);
80     }
81
82
83     protected void addEditButton()
84     {
85         JButton editButton = new JButton(GUIResources.getMessage("edit"));
86         editButton.addActionListener(new ActionListener()
87         {
88             public void actionPerformed(ActionEvent e)
89             {
90                 ClassSpecification selectedClassSpecification =
91                     (ClassSpecification)list.getSelectedValue();
92
93                 setClassSpecification(selectedClassSpecification);
94                 int returnValue = classSpecificationDialog.showDialog();
95                 if (returnValue == ClassSpecificationDialog.APPROVE_OPTION)
96                 {
97                     // Replace the old element.
98
setElementAt(getClassSpecification(),
99                                  list.getSelectedIndex());
100                 }
101             }
102         });
103
104         addButton(editButton);
105     }
106
107
108     protected ClassSpecification createClassSpecification()
109     {
110         return new ClassSpecification();
111     }
112
113
114     protected void setClassSpecification(ClassSpecification classSpecification)
115     {
116         classSpecificationDialog.setClassSpecification(classSpecification);
117     }
118
119
120     protected ClassSpecification getClassSpecification()
121     {
122         return classSpecificationDialog.getClassSpecification();
123     }
124
125
126     /**
127      * Sets the ClassSpecification objects to be represented in this panel.
128      */

129     public void setClassSpecifications(List classSpecifications)
130     {
131         listModel.clear();
132
133         if (classSpecifications != null)
134         {
135             for (int index = 0; index < classSpecifications.size(); index++)
136             {
137                 listModel.addElement(classSpecifications.get(index));
138             }
139         }
140
141         // Make sure the selection buttons are properly enabled,
142
// since the clear method doesn't seem to notify the listener.
143
enableSelectionButtons();
144     }
145
146
147     /**
148      * Returns the ClassSpecification objects currently represented in this panel.
149      */

150     public List getClassSpecifications()
151     {
152         int size = listModel.size();
153         if (size == 0)
154         {
155             return null;
156         }
157
158         List classSpecifications = new ArrayList(size);
159         for (int index = 0; index < size; index++)
160         {
161             classSpecifications.add(listModel.get(index));
162         }
163
164         return classSpecifications;
165     }
166
167
168     /**
169      * This ListCellRenderer renders ClassSpecification objects.
170      */

171     private static class MyListCellRenderer implements ListCellRenderer
172     {
173         JLabel label = new JLabel();
174
175
176         // Implementations for ListCellRenderer.
177

178         public Component JavaDoc getListCellRendererComponent(JList list,
179                                                       Object JavaDoc value,
180                                                       int index,
181                                                       boolean isSelected,
182                                                       boolean cellHasFocus)
183         {
184             ClassSpecification classSpecification = (ClassSpecification)value;
185
186             String JavaDoc comments = classSpecification.comments;
187
188             label.setText(comments != null ? comments.trim() :
189                           classSpecification.className != null ? (GUIResources.getMessage("class") + ' ' + ClassUtil.externalClassName(classSpecification.className)) :
190                           classSpecification.extendsClassName != null ? (GUIResources.getMessage("extensionsOf") + ' ' + ClassUtil.externalClassName(classSpecification.extendsClassName)) :
191                                                                         (GUIResources.getMessage("specificationNumber") + index));
192
193             if (isSelected)
194             {
195                 label.setBackground(list.getSelectionBackground());
196                 label.setForeground(list.getSelectionForeground());
197             }
198             else
199             {
200                 label.setBackground(list.getBackground());
201                 label.setForeground(list.getForeground());
202             }
203
204             label.setOpaque(true);
205
206             return label;
207         }
208     }
209 }
210
Popular Tags