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 proguard.*; 31 import proguard.classfile.*; 32 import proguard.classfile.util.*; 33 import proguard.util.*; 34 35 40 class MemberSpecificationDialog extends JDialog 41 { 42 46 public static final int CANCEL_OPTION = 1; 47 48 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 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 etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); 145 146 this.isField = isField; 147 148 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 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 JPanel annotationTypePanel = new JPanel(layout); 189 annotationTypePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 190 GUIResources.getMessage("annotation"))); 191 192 annotationTypePanel.add(annotationTypeTextField, constraintsLastStretch); 193 194 JPanel namePanel = new JPanel(layout); 196 namePanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 197 GUIResources.getMessage("name"))); 198 199 namePanel.add(nameTextField, constraintsLastStretch); 200 201 JPanel argumentsPanel = new JPanel(layout); 203 argumentsPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 204 GUIResources.getMessage("argumentTypes"))); 205 206 argumentsPanel.add(argumentTypesTextField, constraintsLastStretch); 207 208 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 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 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 252 private JRadioButton[] addRadioButtonTriplet(String 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 JRadioButton radioButton0 = new JRadioButton(); 268 JRadioButton radioButton1 = new JRadioButton(); 269 JRadioButton radioButton2 = new JRadioButton(); 270 271 ButtonGroup buttonGroup = new ButtonGroup(); 273 buttonGroup.add(radioButton0); 274 buttonGroup.add(radioButton1); 275 buttonGroup.add(radioButton2); 276 277 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 296 public void setMemberSpecification(MemberSpecification memberSpecification) 297 { 298 String annotationType = memberSpecification.annotationType; 299 String name = memberSpecification.name; 300 String descriptor = memberSpecification.descriptor; 301 302 annotationTypeTextField.setText(annotationType == null ? "" : ClassUtil.externalType(annotationType)); 304 305 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 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 336 public MemberSpecification getMemberSpecification() 337 { 338 String annotationType = annotationTypeTextField.getText(); 339 String name = nameTextField.getText(); 340 String type = typeTextField.getText(); 341 String arguments = argumentTypesTextField.getText(); 342 343 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 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 407 public int showDialog() 408 { 409 returnValue = CANCEL_OPTION; 410 411 pack(); 414 setLocationRelativeTo(getOwner()); 415 show(); 416 417 return returnValue; 418 } 419 420 421 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 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 |