2
* ProGuard -- shrinking, optimization, obfuscation, and preverification
5
* Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
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)
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
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
26
* This class builds filters corresponding to the selections and names of a
27
* given list of check boxes.
29
public class FilterBuilder
31
private JCheckBox[] checkBoxes;
32
private char separator;
36
* Creates a new FilterBuilder.
37
* @param checkBoxes the check boxes with names and selections that should
38
* be reflected in the output filter.
39
* @param separator the separator for the names in the check boxes.
41
public FilterBuilder(JCheckBox[] checkBoxes, char separator)
43
this.checkBoxes = checkBoxes;
44
this.separator = separator;
49
* Builds a filter for the current names and selections of the check boxes.
51
public String buildFilter()
53
StringBuffer positive = new StringBuffer();
54
StringBuffer negative = new StringBuffer();
56
buildFilter("", positive, negative);
58
return positive.length() <= negative.length() ?
65
* Builds two versions of the filter for the given prefix.
66
* @param prefix the prefix.
67
* @param positive the filter to be extended, assuming the matching
68
* strings are accepted.
69
* @param negative the filter to be extended, assuming the matching
70
* strings are rejected.
72
private void buildFilter(String prefix,
73
StringBuffer positive,
74
StringBuffer negative)
76
int positiveCount = 0;
77
int negativeCount = 0;
79
// Count all selected and unselected check boxes with the prefix.
80
for (int index = 0; index < checkBoxes.length; index++)
82
JCheckBox checkBox = checkBoxes[index];
83
String name = checkBox.getText();
85
if (name.startsWith(prefix))
87
if (checkBox.isSelected())
98
// Are there only unselected check boxes?
99
if (positiveCount == 0)
101
// Extend the positive filter with exceptions and return.
102
if (positive.length() > 0)
104
positive.append(',');
106
positive.append('!').append(prefix);
107
if (prefix.length() == 0 ||
108
prefix.charAt(prefix.length()-1) == separator)
110
positive.append('*');
116
// Are there only selected check boxes?
117
if (negativeCount == 0)
119
// Extend the negative filter with exceptions and return.
120
if (negative.length() > 0)
122
negative.append(',');
124
negative.append(prefix);
125
if (prefix.length() == 0 ||
126
prefix.charAt(prefix.length()-1) == separator)
128
negative.append('*');
134
// Create new positive and negative filters for names starting with the
136
StringBuffer positiveFilter = new StringBuffer();
137
StringBuffer negativeFilter = new StringBuffer();
139
String newPrefix = null;
141
for (int index = 0; index < checkBoxes.length; index++)
143
String name = checkBoxes[index].getText();
145
if (name.startsWith(prefix))
147
if (newPrefix == null ||
148
!name.startsWith(newPrefix))
151
name.indexOf(separator, prefix.length()+1);
153
newPrefix = prefixIndex >= 0 ?
154
name.substring(0, prefixIndex+1) :
157
buildFilter(newPrefix,
164
// Extend the positive filter.
165
if (positiveFilter.length() <= negativeFilter.length() + prefix.length() + 3)
167
if (positive.length() > 0 &&
168
positiveFilter.length() > 0)
170
positive.append(',');
173
positive.append(positiveFilter);
177
if (positive.length() > 0 &&
178
negativeFilter.length() > 0)
180
positive.append(',');
183
positive.append(negativeFilter).append(",!").append(prefix).append('*');
186
// Extend the negative filter.
187
if (negativeFilter.length() <= positiveFilter.length() + prefix.length() + 4)
189
if (negative.length() > 0 &&
190
negativeFilter.length() > 0)
192
negative.append(',');
195
negative.append(negativeFilter);
199
if (negative.length() > 0 &&
200
positiveFilter.length() > 0)
202
negative.append(',');
205
negative.append(positiveFilter).append(',').append(prefix).append('*');