1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 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.ClassSpecification; 24 import proguard.classfile.util.ClassUtil; 25 26 import javax.swing.*; 27 import java.awt.*; 28 import java.awt.event.*; 29 import java.util.*; 30 import java.util.List; 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 final ClassSpecificationDialog classSpecificationDialog; 42 43 ClassSpecificationsPanel(JFrame owner, boolean fullKeepOptions)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 addAddButton()62 protected void addAddButton() 63 { 64 JButton addButton = new JButton(msg("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(tip(addButton, "addTip")); 80 } 81 82 addEditButton()83 protected void addEditButton() 84 { 85 JButton editButton = new JButton(msg("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(tip(editButton, "editTip")); 105 } 106 107 createClassSpecification()108 protected ClassSpecification createClassSpecification() 109 { 110 return new ClassSpecification(); 111 } 112 113 setClassSpecification(ClassSpecification classSpecification)114 protected void setClassSpecification(ClassSpecification classSpecification) 115 { 116 classSpecificationDialog.setClassSpecification(classSpecification); 117 } 118 119 getClassSpecification()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 */ setClassSpecifications(List classSpecifications)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 */ getClassSpecifications()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 * Attaches the tool tip from the GUI resources that corresponds to the 170 * given key, to the given component. 171 */ tip(JComponent component, String messageKey)172 private static JComponent tip(JComponent component, String messageKey) 173 { 174 component.setToolTipText(msg(messageKey)); 175 176 return component; 177 } 178 179 180 /** 181 * Returns the message from the GUI resources that corresponds to the given 182 * key. 183 */ msg(String messageKey)184 private static String msg(String messageKey) 185 { 186 return GUIResources.getMessage(messageKey); 187 } 188 189 190 /** 191 * This ListCellRenderer renders ClassSpecification objects. 192 */ 193 private static class MyListCellRenderer implements ListCellRenderer 194 { 195 private final JLabel label = new JLabel(); 196 197 198 // Implementations for ListCellRenderer. 199 getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)200 public Component getListCellRendererComponent(JList list, 201 Object value, 202 int index, 203 boolean isSelected, 204 boolean cellHasFocus) 205 { 206 ClassSpecification classSpecification = (ClassSpecification)value; 207 208 String comments = classSpecification.comments; 209 210 label.setText(comments != null ? comments.trim() : 211 classSpecification.className != null ? (msg("class") + ' ' + ClassUtil.externalClassName(classSpecification.className)) : 212 classSpecification.extendsClassName != null ? (msg("extensionsOf") + ' ' + ClassUtil.externalClassName(classSpecification.extendsClassName)) : 213 (msg("specificationNumber") + index)); 214 215 if (isSelected) 216 { 217 label.setBackground(list.getSelectionBackground()); 218 label.setForeground(list.getSelectionForeground()); 219 } 220 else 221 { 222 label.setBackground(list.getBackground()); 223 label.setForeground(list.getForeground()); 224 } 225 226 label.setOpaque(true); 227 228 return label; 229 } 230 } 231 } 232