1 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 ; 29 30 import java.util.List ; 31 32 import proguard.*; 33 import proguard.classfile.ClassConstants; 34 import proguard.classfile.util.ClassUtil; 35 36 41 class ClassSpecificationDialog extends JDialog 42 { 43 47 public static final int CANCEL_OPTION = 1; 48 49 52 public static final int APPROVE_OPTION = 0; 53 54 55 private JTextArea commentsTextArea = new JTextArea(4, 20); 56 57 private JRadioButton keepClassesAndMembersRadioButton = new JRadioButton(GUIResources.getMessage("keep")); 58 private JRadioButton keepClassMembersRadioButton = new JRadioButton(GUIResources.getMessage("keepClassMembers")); 59 private JRadioButton keepClassesWithMembersRadioButton = new JRadioButton(GUIResources.getMessage("keepClassesWithMembers")); 60 61 private JRadioButton allowShrinkingRadioButton = new JRadioButton(GUIResources.getMessage("allowShrinking")); 62 private JRadioButton allowOptimizationRadioButton = new JRadioButton(GUIResources.getMessage("allowOptimization")); 63 private JRadioButton allowObfuscationRadioButton = new JRadioButton(GUIResources.getMessage("allowObfuscation")); 64 65 66 private JRadioButton[] publicRadioButtons; 67 private JRadioButton[] finalRadioButtons; 68 private JRadioButton[] interfaceRadioButtons; 69 private JRadioButton[] abstractRadioButtons; 70 71 private JTextField annotationTypeTextField = new JTextField(20); 72 private JTextField classNameTextField = new JTextField(20); 73 private JTextField extendsAnnotationTypeTextField = new JTextField(20); 74 private JTextField extendsClassNameTextField = new JTextField(20); 75 76 private MemberSpecificationsPanel memberSpecificationsPanel; 77 78 private int returnValue; 79 80 81 public ClassSpecificationDialog(JFrame owner, boolean fullKeepOptions) 82 { 83 super(owner, true); 84 setResizable(true); 85 86 GridBagConstraints constraints = new GridBagConstraints(); 88 constraints.anchor = GridBagConstraints.WEST; 89 constraints.insets = new Insets(1, 2, 1, 2); 90 91 GridBagConstraints constraintsStretch = new GridBagConstraints(); 92 constraintsStretch.fill = GridBagConstraints.HORIZONTAL; 93 constraintsStretch.weightx = 1.0; 94 constraintsStretch.anchor = GridBagConstraints.WEST; 95 constraintsStretch.insets = constraints.insets; 96 97 GridBagConstraints constraintsLast = new GridBagConstraints(); 98 constraintsLast.gridwidth = GridBagConstraints.REMAINDER; 99 constraintsLast.anchor = GridBagConstraints.WEST; 100 constraintsLast.insets = constraints.insets; 101 102 GridBagConstraints constraintsLastStretch = new GridBagConstraints(); 103 constraintsLastStretch.gridwidth = GridBagConstraints.REMAINDER; 104 constraintsLastStretch.fill = GridBagConstraints.HORIZONTAL; 105 constraintsLastStretch.weightx = 1.0; 106 constraintsLastStretch.anchor = GridBagConstraints.WEST; 107 constraintsLastStretch.insets = constraints.insets; 108 109 GridBagConstraints panelConstraints = new GridBagConstraints(); 110 panelConstraints.gridwidth = GridBagConstraints.REMAINDER; 111 panelConstraints.fill = GridBagConstraints.HORIZONTAL; 112 panelConstraints.weightx = 1.0; 113 panelConstraints.weighty = 0.0; 114 panelConstraints.anchor = GridBagConstraints.NORTHWEST; 115 panelConstraints.insets = constraints.insets; 116 117 GridBagConstraints stretchPanelConstraints = new GridBagConstraints(); 118 stretchPanelConstraints.gridwidth = GridBagConstraints.REMAINDER; 119 stretchPanelConstraints.fill = GridBagConstraints.BOTH; 120 stretchPanelConstraints.weightx = 1.0; 121 stretchPanelConstraints.weighty = 1.0; 122 stretchPanelConstraints.anchor = GridBagConstraints.NORTHWEST; 123 stretchPanelConstraints.insets = constraints.insets; 124 125 GridBagConstraints labelConstraints = new GridBagConstraints(); 126 labelConstraints.anchor = GridBagConstraints.CENTER; 127 labelConstraints.insets = new Insets(2, 10, 2, 10); 128 129 GridBagConstraints lastLabelConstraints = new GridBagConstraints(); 130 lastLabelConstraints.gridwidth = GridBagConstraints.REMAINDER; 131 lastLabelConstraints.anchor = GridBagConstraints.CENTER; 132 lastLabelConstraints.insets = labelConstraints.insets; 133 134 GridBagConstraints okButtonConstraints = new GridBagConstraints(); 135 okButtonConstraints.weightx = 1.0; 136 okButtonConstraints.weighty = 1.0; 137 okButtonConstraints.anchor = GridBagConstraints.SOUTHEAST; 138 okButtonConstraints.insets = new Insets(4, 4, 8, 4); 139 140 GridBagConstraints cancelButtonConstraints = new GridBagConstraints(); 141 cancelButtonConstraints.gridwidth = GridBagConstraints.REMAINDER; 142 cancelButtonConstraints.weighty = 1.0; 143 cancelButtonConstraints.anchor = GridBagConstraints.SOUTHEAST; 144 cancelButtonConstraints.insets = okButtonConstraints.insets; 145 146 GridBagLayout layout = new GridBagLayout(); 147 148 Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); 149 150 JPanel commentsPanel = new JPanel(layout); 152 commentsPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 153 GUIResources.getMessage("comments"))); 154 155 JScrollPane commentsScrollPane = new JScrollPane(commentsTextArea); 156 commentsScrollPane.setBorder(classNameTextField.getBorder()); 157 158 commentsPanel.add(commentsScrollPane, constraintsLastStretch); 159 160 ButtonGroup keepButtonGroup = new ButtonGroup(); 162 keepButtonGroup.add(keepClassesAndMembersRadioButton); 163 keepButtonGroup.add(keepClassMembersRadioButton); 164 keepButtonGroup.add(keepClassesWithMembersRadioButton); 165 166 JPanel keepOptionPanel = new JPanel(layout); 167 keepOptionPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 168 GUIResources.getMessage("keepTitle"))); 169 170 keepOptionPanel.add(keepClassesAndMembersRadioButton, constraintsLastStretch); 171 keepOptionPanel.add(keepClassMembersRadioButton, constraintsLastStretch); 172 keepOptionPanel.add(keepClassesWithMembersRadioButton, constraintsLastStretch); 173 174 JPanel allowOptionPanel = new JPanel(layout); 176 allowOptionPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 177 GUIResources.getMessage("allowTitle"))); 178 179 allowOptionPanel.add(allowShrinkingRadioButton, constraintsLastStretch); 180 allowOptionPanel.add(allowOptimizationRadioButton, constraintsLastStretch); 181 allowOptionPanel.add(allowObfuscationRadioButton, constraintsLastStretch); 182 183 JPanel accessPanel = new JPanel(layout); 185 accessPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 186 GUIResources.getMessage("access"))); 187 188 accessPanel.add(Box.createGlue(), labelConstraints); 189 accessPanel.add(new JLabel(GUIResources.getMessage("required")), labelConstraints); 190 accessPanel.add(new JLabel(GUIResources.getMessage("not")), labelConstraints); 191 accessPanel.add(new JLabel(GUIResources.getMessage("dontCare")), labelConstraints); 192 accessPanel.add(Box.createGlue(), constraintsLastStretch); 193 194 publicRadioButtons = addRadioButtonTriplet("Public", accessPanel); 195 finalRadioButtons = addRadioButtonTriplet("Final", accessPanel); 196 interfaceRadioButtons = addRadioButtonTriplet("Interface", accessPanel); 197 abstractRadioButtons = addRadioButtonTriplet("Abstract", accessPanel); 198 199 JPanel annotationTypePanel = new JPanel(layout); 201 annotationTypePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 202 GUIResources.getMessage("annotation"))); 203 204 annotationTypePanel.add(annotationTypeTextField, constraintsLastStretch); 205 206 JPanel classNamePanel = new JPanel(layout); 208 classNamePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 209 GUIResources.getMessage("class"))); 210 211 classNamePanel.add(classNameTextField, constraintsLastStretch); 212 213 JPanel extendsAnnotationTypePanel = new JPanel(layout); 215 extendsAnnotationTypePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 216 GUIResources.getMessage("extendsImplementsAnnotation"))); 217 218 extendsAnnotationTypePanel.add(extendsAnnotationTypeTextField, constraintsLastStretch); 219 220 JPanel extendsClassNamePanel = new JPanel(layout); 222 extendsClassNamePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 223 GUIResources.getMessage("extendsImplementsClass"))); 224 225 extendsClassNamePanel.add(extendsClassNameTextField, constraintsLastStretch); 226 227 228 memberSpecificationsPanel = new MemberSpecificationsPanel(this, fullKeepOptions); 230 memberSpecificationsPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 231 GUIResources.getMessage("classMembers"))); 232 233 JButton okButton = new JButton(GUIResources.getMessage("ok")); 235 okButton.addActionListener(new ActionListener() 236 { 237 public void actionPerformed(ActionEvent e) 238 { 239 returnValue = APPROVE_OPTION; 240 hide(); 241 } 242 }); 243 244 JButton cancelButton = new JButton(GUIResources.getMessage("cancel")); 246 cancelButton.addActionListener(new ActionListener() 247 { 248 public void actionPerformed(ActionEvent e) 249 { 250 hide(); 251 } 252 }); 253 254 JPanel mainPanel = new JPanel(layout); 256 mainPanel.add(commentsPanel, panelConstraints); 257 if (fullKeepOptions) 258 { 259 mainPanel.add(keepOptionPanel, panelConstraints); 260 mainPanel.add(allowOptionPanel, panelConstraints); 261 } 262 mainPanel.add(accessPanel, panelConstraints); 263 mainPanel.add(annotationTypePanel, panelConstraints); 264 mainPanel.add(classNamePanel, panelConstraints); 265 mainPanel.add(extendsAnnotationTypePanel, panelConstraints); 266 mainPanel.add(extendsClassNamePanel, panelConstraints); 267 mainPanel.add(memberSpecificationsPanel, stretchPanelConstraints); 268 269 mainPanel.add(okButton, okButtonConstraints); 270 mainPanel.add(cancelButton, cancelButtonConstraints); 271 272 getContentPane().add(mainPanel); 273 } 274 275 276 280 private JRadioButton[] addRadioButtonTriplet(String labelText, 281 JPanel panel) 282 { 283 GridBagConstraints labelConstraints = new GridBagConstraints(); 284 labelConstraints.anchor = GridBagConstraints.WEST; 285 labelConstraints.insets = new Insets(2, 10, 2, 10); 286 287 GridBagConstraints buttonConstraints = new GridBagConstraints(); 288 buttonConstraints.insets = labelConstraints.insets; 289 290 GridBagConstraints lastGlueConstraints = new GridBagConstraints(); 291 lastGlueConstraints.gridwidth = GridBagConstraints.REMAINDER; 292 lastGlueConstraints.weightx = 1.0; 293 294 JRadioButton radioButton0 = new JRadioButton(); 296 JRadioButton radioButton1 = new JRadioButton(); 297 JRadioButton radioButton2 = new JRadioButton(); 298 299 ButtonGroup buttonGroup = new ButtonGroup(); 301 buttonGroup.add(radioButton0); 302 buttonGroup.add(radioButton1); 303 buttonGroup.add(radioButton2); 304 305 panel.add(new JLabel(labelText), labelConstraints); 307 panel.add(radioButton0, buttonConstraints); 308 panel.add(radioButton1, buttonConstraints); 309 panel.add(radioButton2, buttonConstraints); 310 panel.add(Box.createGlue(), lastGlueConstraints); 311 312 return new JRadioButton[] 313 { 314 radioButton0, 315 radioButton1, 316 radioButton2 317 }; 318 } 319 320 321 324 public void setKeepSpecification(KeepSpecification keepSpecification) 325 { 326 boolean markClasses = keepSpecification.markClasses; 327 boolean markConditionally = keepSpecification.markConditionally; 328 boolean allowShrinking = keepSpecification.allowShrinking; 329 boolean allowOptimization = keepSpecification.allowOptimization; 330 boolean allowObfuscation = keepSpecification.allowObfuscation; 331 332 JRadioButton keepOptionRadioButton = 334 markConditionally ? keepClassesWithMembersRadioButton : 335 markClasses ? keepClassesAndMembersRadioButton : 336 keepClassMembersRadioButton; 337 338 keepOptionRadioButton.setSelected(true); 339 340 allowShrinkingRadioButton .setSelected(allowShrinking); 342 allowOptimizationRadioButton.setSelected(allowOptimization); 343 allowObfuscationRadioButton .setSelected(allowObfuscation); 344 345 setClassSpecification(keepSpecification); 346 } 347 348 349 352 public void setClassSpecification(ClassSpecification classSpecification) 353 { 354 String comments = classSpecification.comments; 355 String annotationType = classSpecification.annotationType; 356 String className = classSpecification.className; 357 String extendsAnnotationType = classSpecification.extendsAnnotationType; 358 String extendsClassName = classSpecification.extendsClassName; 359 List keepFieldOptions = classSpecification.fieldSpecifications; 360 List keepMethodOptions = classSpecification.methodSpecifications; 361 362 commentsTextArea.setText(comments == null ? "" : comments); 364 365 setClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_PUBLIC, publicRadioButtons); 367 setClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_FINAL, finalRadioButtons); 368 setClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_INTERFACE, interfaceRadioButtons); 369 setClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_ABSTRACT, abstractRadioButtons); 370 371 annotationTypeTextField .setText(annotationType == null ? "" : ClassUtil.externalType(annotationType)); 373 classNameTextField .setText(className == null ? "*" : ClassUtil.externalClassName(className)); 374 extendsAnnotationTypeTextField.setText(extendsAnnotationType == null ? "" : ClassUtil.externalType(extendsAnnotationType)); 375 extendsClassNameTextField .setText(extendsClassName == null ? "" : ClassUtil.externalClassName(extendsClassName)); 376 377 memberSpecificationsPanel.setMemberSpecifications(keepFieldOptions, keepMethodOptions); 379 } 380 381 382 385 public KeepSpecification getKeepSpecification() 386 { 387 boolean markClasses = !keepClassMembersRadioButton .isSelected(); 388 boolean markConditionally = keepClassesWithMembersRadioButton.isSelected(); 389 boolean allowShrinking = allowShrinkingRadioButton .isSelected(); 390 boolean allowOptimization = allowOptimizationRadioButton .isSelected(); 391 boolean allowObfuscation = allowObfuscationRadioButton .isSelected(); 392 393 return new KeepSpecification(markClasses, 394 markConditionally, 395 allowShrinking, 396 allowOptimization, 397 allowObfuscation, 398 getClassSpecification()); 399 } 400 401 402 405 public ClassSpecification getClassSpecification() 406 { 407 String comments = commentsTextArea.getText(); 408 String annotationType = annotationTypeTextField.getText(); 409 String className = classNameTextField.getText(); 410 String extendsAnnotationType = extendsAnnotationTypeTextField.getText(); 411 String extendsClassName = extendsClassNameTextField.getText(); 412 413 ClassSpecification classSpecification = 414 new ClassSpecification(comments.equals("") ? null : comments, 415 0, 416 0, 417 annotationType.equals("") ? null : ClassUtil.internalType(annotationType), 418 className.equals("") || 419 className.equals("*") ? null : ClassUtil.internalClassName(className), 420 extendsAnnotationType.equals("") ? null : ClassUtil.internalType(extendsAnnotationType), 421 extendsClassName.equals("") ? null : ClassUtil.internalClassName(extendsClassName)); 422 423 getClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_PUBLIC, publicRadioButtons); 425 getClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_FINAL, finalRadioButtons); 426 getClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_INTERFACE, interfaceRadioButtons); 427 getClassSpecificationRadioButtons(classSpecification, ClassConstants.INTERNAL_ACC_ABSTRACT, abstractRadioButtons); 428 429 classSpecification.fieldSpecifications = memberSpecificationsPanel.getMemberSpecifications(true); 431 classSpecification.methodSpecifications = memberSpecificationsPanel.getMemberSpecifications(false); 432 433 return classSpecification; 434 } 435 436 437 443 public int showDialog() 444 { 445 returnValue = CANCEL_OPTION; 446 447 pack(); 450 setLocationRelativeTo(getOwner()); 451 show(); 452 453 return returnValue; 454 } 455 456 457 461 private void setClassSpecificationRadioButtons(ClassSpecification classSpecification, 462 int flag, 463 JRadioButton[] radioButtons) 464 { 465 int index = (classSpecification.requiredSetAccessFlags & flag) != 0 ? 0 : 466 (classSpecification.requiredUnsetAccessFlags & flag) != 0 ? 1 : 467 2; 468 radioButtons[index].setSelected(true); 469 } 470 471 472 476 private void getClassSpecificationRadioButtons(ClassSpecification classSpecification, 477 int flag, 478 JRadioButton[] radioButtons) 479 { 480 if (radioButtons[0].isSelected()) 481 { 482 classSpecification.requiredSetAccessFlags |= flag; 483 } 484 else if (radioButtons[1].isSelected()) 485 { 486 classSpecification.requiredUnsetAccessFlags |= flag; 487 } 488 } 489 } 490 | Popular Tags |