Lines 96-102
Link Here
|
96 |
* @param base |
96 |
* @param base |
97 |
* @param set |
97 |
* @param set |
98 |
*/ |
98 |
*/ |
99 |
private static void addNumberToBase(StringBuffer base, HashSet set) { |
99 |
private static void addNumberToBase(StringBuffer base, boolean bracketed, HashSet set) { |
100 |
if (set.size() > 0) { |
100 |
if (set.size() > 0) { |
101 |
// Limit on the number of auto-generated item numbers to check for |
101 |
// Limit on the number of auto-generated item numbers to check for |
102 |
int limit = 100; |
102 |
int limit = 100; |
Lines 109-117
Link Here
|
109 |
// Check if the number was already used to auto-generate an |
109 |
// Check if the number was already used to auto-generate an |
110 |
// existing item |
110 |
// existing item |
111 |
if (set.contains(new Integer(x)) == false) { |
111 |
if (set.contains(new Integer(x)) == false) { |
112 |
base.append(" ("); //$NON-NLS-1$ |
112 |
if (bracketed) |
|
|
113 |
base.append(" ("); //$NON-NLS-1$ |
113 |
base.append(x); |
114 |
base.append(x); |
114 |
base.append(")"); //$NON-NLS-1$ |
115 |
if (bracketed) |
|
|
116 |
base.append(")"); //$NON-NLS-1$ |
115 |
break; |
117 |
break; |
116 |
} |
118 |
} |
117 |
} |
119 |
} |
Lines 124-157
Link Here
|
124 |
* @param set |
126 |
* @param set |
125 |
* @param title |
127 |
* @param title |
126 |
*/ |
128 |
*/ |
127 |
private static void compareTitleWithBase(String base, HashSet set, String title) { |
129 |
private static void compareTitleWithBase(String base, boolean bracketed, HashSet set, String title) { |
128 |
// Check to see it the name starts with the base |
130 |
// Check to see it the name starts with the prefix |
129 |
if (title.startsWith(base)) { |
131 |
if (title.startsWith(base)) { |
130 |
// space, (, number, ) |
132 |
// with brackets add on is: space, (, #, ) |
131 |
int minSizeNumAddOn = 4; |
133 |
int minSizeNumAddOn = 4; |
|
|
134 |
if (!bracketed) |
135 |
// without brackets and space add on is just number |
136 |
minSizeNumAddOn = 1; |
132 |
// We found a possible auto-generated name |
137 |
// We found a possible auto-generated name |
133 |
// Determine number |
138 |
// Determine number |
134 |
if (title.length() >= (base.length() + minSizeNumAddOn)) { |
139 |
if (title.length() >= (base.length() + minSizeNumAddOn)) { |
135 |
// We skipped the space |
140 |
String numPart; |
136 |
String numPart = title.substring(base.length() + 1); |
141 |
if (bracketed && title.charAt(base.length()) == ' ') { |
|
|
142 |
// We skipped the space since we already checked |
143 |
numPart = title.substring(base.length() + 1); |
144 |
} else if (!bracketed) { |
145 |
// without brackets, the numPart is everything after the prefix |
146 |
numPart = title.substring(base.length()); |
147 |
} else { |
148 |
// We are using brackets and there was no space |
149 |
return; |
150 |
} |
151 |
if (bracketed) { |
152 |
if (numPart.charAt(0) == '(') { |
153 |
// We are using brackets and confirmed that the open bracket exists |
154 |
// move on to just the number part |
155 |
numPart = numPart.substring(1); |
156 |
} |
157 |
else { |
158 |
// We are using brackets and there is no opening bracket |
159 |
return; |
160 |
} |
161 |
} |
137 |
// We found an auto-generated name |
162 |
// We found an auto-generated name |
138 |
if (numPart.charAt(0) == '(') { |
163 |
StringBuffer buffer = new StringBuffer(); |
139 |
StringBuffer buffer = new StringBuffer(); |
164 |
// Parse the number between the brackets |
140 |
// Parse the number between the brackets |
165 |
for (int j = 0; j < numPart.length(); j++) { |
141 |
for (int j = 1; j < numPart.length(); j++) { |
166 |
char current = numPart.charAt(j); |
142 |
char current = numPart.charAt(j); |
167 |
// Make sure its a digit |
143 |
// Make sure its a digit |
168 |
if (Character.isDigit(current)) { |
144 |
if (Character.isDigit(current)) { |
169 |
buffer.append(current); |
145 |
buffer.append(current); |
170 |
} else { |
146 |
} else { |
171 |
if (!bracketed || numPart.charAt(j) != ')' || j != numPart.length() - 1) { |
147 |
// Break on non digits including ')' |
172 |
// without brackets, a non digits means this will not conflict |
148 |
break; |
173 |
// with brackets, anything other than a ')' means this will not conflict |
|
|
174 |
// with brackets, if this is not the last character it will not conflict |
175 |
return; |
149 |
} |
176 |
} |
|
|
177 |
// if all conditions passed, this is the last loop, no need to break |
150 |
} |
178 |
} |
151 |
// Convert the number we found into an actual number |
179 |
} |
152 |
if (buffer.length() > 0) { |
180 |
// Convert the number we found into an actual number |
153 |
set.add(new Integer(buffer.toString())); |
181 |
if (buffer.length() > 0) { |
154 |
} |
182 |
set.add(new Integer(buffer.toString())); |
155 |
} |
183 |
} |
156 |
|
184 |
|
157 |
} else { |
185 |
} else { |
Lines 166-171
Link Here
|
166 |
* @return |
194 |
* @return |
167 |
*/ |
195 |
*/ |
168 |
public static String generateName(String[] names, String base) { |
196 |
public static String generateName(String[] names, String base) { |
|
|
197 |
return generateName(names, base, true); |
198 |
} |
199 |
|
200 |
/** |
201 |
* <p>Generates a name that does not conflict with any of the given names with one of two forms: |
202 |
* <ol><li>"<base> (#)"</li><li>"<base>#"</li></ol> |
203 |
* The number will be omitted if the base name alone is available.</p> |
204 |
* |
205 |
* @param names |
206 |
* the existing names that should not be conflicted |
207 |
* @param base |
208 |
* the base name to add numbers to |
209 |
* @param bracketed |
210 |
* if true use the first form, otherwise use the second |
211 |
* @return |
212 |
* the non-conflicting name |
213 |
*/ |
214 |
public static String generateName(String[] names, String base, boolean bracketed){ |
169 |
StringBuffer result = new StringBuffer(base); |
215 |
StringBuffer result = new StringBuffer(base); |
170 |
// Used to track auto-generated numbers used |
216 |
// Used to track auto-generated numbers used |
171 |
HashSet set = new HashSet(); |
217 |
HashSet set = new HashSet(); |
Lines 174-184
Link Here
|
174 |
// Performance hit unnoticeable because number of items per cheatsheet |
220 |
// Performance hit unnoticeable because number of items per cheatsheet |
175 |
// should be minimal. |
221 |
// should be minimal. |
176 |
for (int i = 0; i < names.length; i++) { |
222 |
for (int i = 0; i < names.length; i++) { |
177 |
PDELabelUtility.compareTitleWithBase(base, set, names[i]); |
223 |
PDELabelUtility.compareTitleWithBase(base, bracketed, set, names[i]); |
178 |
} |
224 |
} |
179 |
// Add an auto-generated number |
225 |
// Add an auto-generated number |
180 |
PDELabelUtility.addNumberToBase(result, set); |
226 |
PDELabelUtility.addNumberToBase(result, bracketed, set); |
181 |
|
227 |
|
182 |
return result.toString(); |
228 |
return result.toString(); |
|
|
229 |
|
183 |
} |
230 |
} |
184 |
} |
231 |
} |