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 */ 21package proguard.gui; 22 23import proguard.util.ListUtil; 24 25import javax.swing.*; 26import javax.swing.border.*; 27import java.awt.*; 28import java.awt.event.*; 29import java.util.List; 30 31/** 32 * This <code>JDialog</code> allows the user to enter a String. 33 * 34 * @author Eric Lafortune 35 */ 36public class FilterDialog extends JDialog 37{ 38 /** 39 * Return value if the dialog is canceled (with the Cancel button or by 40 * closing the dialog window). 41 */ 42 public static final int CANCEL_OPTION = 1; 43 44 /** 45 * Return value if the dialog is approved (with the Ok button). 46 */ 47 public static final int APPROVE_OPTION = 0; 48 49 private static final String DEFAULT_FILTER = "**"; 50 private static final String DEFAULT_JAR_FILTER = "**.jar"; 51 private static final String DEFAULT_WAR_FILTER = "**.war"; 52 private static final String DEFAULT_EAR_FILTER = "**.ear"; 53 private static final String DEFAULT_ZIP_FILTER = "**.zip"; 54 55 56 private final JTextField filterTextField = new JTextField(40); 57 private final JTextField jarFilterTextField = new JTextField(40); 58 private final JTextField warFilterTextField = new JTextField(40); 59 private final JTextField earFilterTextField = new JTextField(40); 60 private final JTextField zipFilterTextField = new JTextField(40); 61 private int returnValue; 62 63 64 public FilterDialog(JFrame owner, 65 String explanation) 66 { 67 super(owner, true); 68 setResizable(true); 69 70 // Create some constraints that can be reused. 71 GridBagConstraints textConstraints = new GridBagConstraints(); 72 textConstraints.gridwidth = GridBagConstraints.REMAINDER; 73 textConstraints.fill = GridBagConstraints.HORIZONTAL; 74 textConstraints.weightx = 1.0; 75 textConstraints.weighty = 1.0; 76 textConstraints.anchor = GridBagConstraints.NORTHWEST; 77 textConstraints.insets = new Insets(10, 10, 10, 10); 78 79 GridBagConstraints labelConstraints = new GridBagConstraints(); 80 labelConstraints.anchor = GridBagConstraints.WEST; 81 labelConstraints.insets = new Insets(1, 2, 1, 2); 82 83 GridBagConstraints textFieldConstraints = new GridBagConstraints(); 84 textFieldConstraints.gridwidth = GridBagConstraints.REMAINDER; 85 textFieldConstraints.fill = GridBagConstraints.HORIZONTAL; 86 textFieldConstraints.weightx = 1.0; 87 textFieldConstraints.anchor = GridBagConstraints.WEST; 88 textFieldConstraints.insets = labelConstraints.insets; 89 90 GridBagConstraints panelConstraints = new GridBagConstraints(); 91 panelConstraints.gridwidth = GridBagConstraints.REMAINDER; 92 panelConstraints.fill = GridBagConstraints.HORIZONTAL; 93 panelConstraints.weightx = 1.0; 94 panelConstraints.weighty = 0.0; 95 panelConstraints.anchor = GridBagConstraints.NORTHWEST; 96 panelConstraints.insets = labelConstraints.insets; 97 98 GridBagConstraints okButtonConstraints = new GridBagConstraints(); 99 okButtonConstraints.weightx = 1.0; 100 okButtonConstraints.weighty = 1.0; 101 okButtonConstraints.anchor = GridBagConstraints.SOUTHEAST; 102 okButtonConstraints.insets = new Insets(4, 4, 8, 4); 103 104 GridBagConstraints cancelButtonConstraints = new GridBagConstraints(); 105 cancelButtonConstraints.gridwidth = GridBagConstraints.REMAINDER; 106 cancelButtonConstraints.weighty = 1.0; 107 cancelButtonConstraints.anchor = GridBagConstraints.SOUTHEAST; 108 cancelButtonConstraints.insets = okButtonConstraints.insets; 109 110 GridBagLayout layout = new GridBagLayout(); 111 112 Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); 113 114 // Create the panel with the explanation. 115 JTextArea explanationTextArea = new JTextArea(explanation, 3, 0); 116 explanationTextArea.setOpaque(false); 117 explanationTextArea.setEditable(false); 118 explanationTextArea.setLineWrap(true); 119 explanationTextArea.setWrapStyleWord(true); 120 121 // Create the filter labels. 122 JLabel filterLabel = new JLabel(msg("nameFilter")); 123 JLabel jarFilterLabel = new JLabel(msg("jarNameFilter")); 124 JLabel warFilterLabel = new JLabel(msg("warNameFilter")); 125 JLabel earFilterLabel = new JLabel(msg("earNameFilter")); 126 JLabel zipFilterLabel = new JLabel(msg("zipNameFilter")); 127 128 // Create the filter panel. 129 JPanel filterPanel = new JPanel(layout); 130 filterPanel.setBorder(BorderFactory.createTitledBorder(etchedBorder, 131 msg("filters"))); 132 133 filterPanel.add(explanationTextArea, textConstraints); 134 135 filterPanel.add(tip(filterLabel, "nameFilterTip"), labelConstraints); 136 filterPanel.add(tip(filterTextField, "fileNameFilterTip"), textFieldConstraints); 137 138 filterPanel.add(tip(jarFilterLabel, "jarNameFilterTip"), labelConstraints); 139 filterPanel.add(tip(jarFilterTextField, "fileNameFilterTip"), textFieldConstraints); 140 141 filterPanel.add(tip(warFilterLabel, "warNameFilterTip"), labelConstraints); 142 filterPanel.add(tip(warFilterTextField, "fileNameFilterTip"), textFieldConstraints); 143 144 filterPanel.add(tip(earFilterLabel, "earNameFilterTip"), labelConstraints); 145 filterPanel.add(tip(earFilterTextField, "fileNameFilterTip"), textFieldConstraints); 146 147 filterPanel.add(tip(zipFilterLabel, "zipNameFilterTip"), labelConstraints); 148 filterPanel.add(tip(zipFilterTextField, "fileNameFilterTip"), textFieldConstraints); 149 150 151 JButton okButton = new JButton(msg("ok")); 152 okButton.addActionListener(new ActionListener() 153 { 154 public void actionPerformed(ActionEvent e) 155 { 156 returnValue = APPROVE_OPTION; 157 hide(); 158 } 159 }); 160 161 JButton cancelButton = new JButton(msg("cancel")); 162 cancelButton.addActionListener(new ActionListener() 163 { 164 public void actionPerformed(ActionEvent e) 165 { 166 hide(); 167 } 168 }); 169 170 // Add all panels to the main panel. 171 JPanel mainPanel = new JPanel(layout); 172 mainPanel.add(filterPanel, panelConstraints); 173 mainPanel.add(okButton, okButtonConstraints); 174 mainPanel.add(cancelButton, cancelButtonConstraints); 175 176 getContentPane().add(mainPanel); 177 } 178 179 180 /** 181 * Sets the filter to be represented in this dialog. 182 */ 183 public void setFilter(List filter) 184 { 185 filterTextField.setText(filter != null ? ListUtil.commaSeparatedString(filter) : DEFAULT_FILTER); 186 } 187 188 189 /** 190 * Returns the filter currently represented in this dialog. 191 */ 192 public List getFilter() 193 { 194 String filter = filterTextField.getText(); 195 196 return filter.equals(DEFAULT_FILTER) ? null : ListUtil.commaSeparatedList(filter); 197 } 198 199 200 /** 201 * Sets the jar filter to be represented in this dialog. 202 */ 203 public void setJarFilter(List filter) 204 { 205 jarFilterTextField.setText(filter != null ? ListUtil.commaSeparatedString(filter) : DEFAULT_JAR_FILTER); 206 } 207 208 209 /** 210 * Returns the jar filter currently represented in this dialog. 211 */ 212 public List getJarFilter() 213 { 214 String filter = jarFilterTextField.getText(); 215 216 return filter.equals(DEFAULT_JAR_FILTER) ? null : ListUtil.commaSeparatedList(filter); 217 } 218 219 220 /** 221 * Sets the war filter to be represented in this dialog. 222 */ 223 public void setWarFilter(List filter) 224 { 225 warFilterTextField.setText(filter != null ? ListUtil.commaSeparatedString(filter) : DEFAULT_WAR_FILTER); 226 } 227 228 229 /** 230 * Returns the war filter currently represented in this dialog. 231 */ 232 public List getWarFilter() 233 { 234 String filter = warFilterTextField.getText(); 235 236 return filter.equals(DEFAULT_WAR_FILTER) ? null : ListUtil.commaSeparatedList(filter); 237 } 238 239 240 /** 241 * Sets the ear filter to be represented in this dialog. 242 */ 243 public void setEarFilter(List filter) 244 { 245 earFilterTextField.setText(filter != null ? ListUtil.commaSeparatedString(filter) : DEFAULT_EAR_FILTER); 246 } 247 248 249 /** 250 * Returns the ear filter currently represented in this dialog. 251 */ 252 public List getEarFilter() 253 { 254 String filter = earFilterTextField.getText(); 255 256 return filter.equals(DEFAULT_EAR_FILTER) ? null : ListUtil.commaSeparatedList(filter); 257 } 258 259 260 /** 261 * Sets the zip filter to be represented in this dialog. 262 */ 263 public void setZipFilter(List filter) 264 { 265 zipFilterTextField.setText(filter != null ? ListUtil.commaSeparatedString(filter) : DEFAULT_ZIP_FILTER); 266 } 267 268 269 /** 270 * Returns the zip filter currently represented in this dialog. 271 */ 272 public List getZipFilter() 273 { 274 String filter = zipFilterTextField.getText(); 275 276 return filter.equals(DEFAULT_ZIP_FILTER) ? null : ListUtil.commaSeparatedList(filter); 277 } 278 279 280 /** 281 * Shows this dialog. This method only returns when the dialog is closed. 282 * 283 * @return <code>CANCEL_OPTION</code> or <code>APPROVE_OPTION</code>, 284 * depending on the choice of the user. 285 */ 286 public int showDialog() 287 { 288 returnValue = CANCEL_OPTION; 289 290 // Open the dialog in the right place, then wait for it to be closed, 291 // one way or another. 292 pack(); 293 setLocationRelativeTo(getOwner()); 294 show(); 295 296 return returnValue; 297 } 298 299 300 /** 301 * Attaches the tool tip from the GUI resources that corresponds to the 302 * given key, to the given component. 303 */ 304 private static JComponent tip(JComponent component, String messageKey) 305 { 306 component.setToolTipText(msg(messageKey)); 307 308 return component; 309 } 310 311 312 /** 313 * Returns the message from the GUI resources that corresponds to the given 314 * key. 315 */ 316 private static String msg(String messageKey) 317 { 318 return GUIResources.getMessage(messageKey); 319 } 320} 321