1 | |
package us.daveread.basicquery.util; |
2 | |
|
3 | |
import java.awt.Component; |
4 | |
import java.text.SimpleDateFormat; |
5 | |
import java.text.DecimalFormat; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.HashMap; |
8 | |
import java.util.List; |
9 | |
import java.util.Map; |
10 | |
|
11 | |
import javax.swing.JTable; |
12 | |
import javax.swing.table.TableColumn; |
13 | |
import javax.swing.table.TableModel; |
14 | |
|
15 | |
import org.apache.log4j.Logger; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class Utility { |
43 | |
|
44 | |
|
45 | |
|
46 | 1 | private static final Logger LOGGER = Logger.getLogger(Utility.class); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private static Map<String, SimpleDateFormat> formats; |
52 | |
|
53 | |
|
54 | |
static { |
55 | 1 | formats = new HashMap<String, SimpleDateFormat>(); |
56 | 1 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | private Utility() { |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public static String replace(String aString, String aOldSubString, |
77 | |
String aNewSubString) { |
78 | 1 | return replace(aString, aOldSubString, aNewSubString, 0, true); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public static String replace(String saString, String saOldSubString, |
100 | |
String saNewSubString, int iaStartIndex, |
101 | |
boolean replaceAll) { |
102 | |
StringBuffer sblResult; |
103 | |
boolean blDone, blFirstReplacement; |
104 | |
int ilIndex; |
105 | 10 | if (saString == null || saOldSubString == null || saNewSubString == null) { |
106 | 3 | throw new IllegalArgumentException("arguments may not be null"); |
107 | 7 | } else if (saString.equals("") || saOldSubString.equals("")) { |
108 | 2 | return saString; |
109 | 5 | } else if (iaStartIndex < 0) { |
110 | 1 | throw new IllegalArgumentException( |
111 | |
"iaStartIndex must be greater than or equal to 0"); |
112 | 4 | } else if (iaStartIndex >= saString.length()) { |
113 | 1 | throw new IllegalArgumentException("iaStartIndex cannot equal or exceed" |
114 | |
+ "length of input String saString"); |
115 | |
} |
116 | |
|
117 | 3 | sblResult = new StringBuffer(); |
118 | 3 | blDone = false; |
119 | 3 | blFirstReplacement = true; |
120 | 3 | String workingVersion = saString; |
121 | 3 | int workingIndex = iaStartIndex; |
122 | 12 | while (!blDone) { |
123 | 9 | ilIndex = workingVersion.indexOf(saOldSubString, workingIndex); |
124 | 9 | if ((!blFirstReplacement && !replaceAll) || ilIndex == -1) { |
125 | 1 | sblResult.append(workingVersion); |
126 | 1 | blDone = true; |
127 | |
} else { |
128 | 8 | sblResult.append(workingVersion.substring(0, ilIndex)); |
129 | 8 | sblResult.append(saNewSubString); |
130 | 8 | if (workingVersion.length() > ilIndex + saOldSubString.length()) { |
131 | 6 | workingVersion = workingVersion.substring( |
132 | |
ilIndex + saOldSubString.length(), |
133 | |
workingVersion.length()); |
134 | |
} else { |
135 | 2 | blDone = true; |
136 | |
} |
137 | |
|
138 | 8 | blFirstReplacement = false; |
139 | |
|
140 | |
|
141 | 8 | workingIndex = 0; |
142 | |
} |
143 | |
} |
144 | 3 | return sblResult.toString(); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public static void initColumnSizes(JTable table, TableModel model) { |
158 | 1 | TableColumn column = null; |
159 | 1 | Component comp = null; |
160 | 1 | int headerWidth = 0; |
161 | 1 | int cellWidth = 0; |
162 | 1 | final Object[] longValues = getLongestValues(model); |
163 | |
|
164 | 4 | for (int i = 0; i < longValues.length; i++) { |
165 | 3 | column = table.getColumnModel().getColumn(i); |
166 | |
|
167 | |
try { |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | 3 | comp = table.getTableHeader().getDefaultRenderer(). |
173 | |
getTableCellRendererComponent( |
174 | |
null, column.getHeaderValue() + "W", |
175 | |
false, false, 0, 0); |
176 | 3 | headerWidth = comp.getPreferredSize().width; |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | 3 | if (headerWidth > 10000) { |
184 | 0 | LOGGER.debug("Header unusually wide (" + headerWidth |
185 | |
+ "): calc again"); |
186 | |
|
187 | 0 | headerWidth = comp.getPreferredSize().width; |
188 | |
|
189 | 0 | LOGGER.debug("Result of header recalc (" + headerWidth + ")"); |
190 | |
} |
191 | 0 | } catch (NullPointerException npe) { |
192 | 0 | LOGGER |
193 | |
.error( |
194 | |
"getHeaderRenderer returns null in 1.3. The replacement is getDefaultRenderer.", |
195 | |
npe); |
196 | 3 | } |
197 | |
|
198 | 3 | comp = table.getDefaultRenderer(model.getColumnClass(i)). |
199 | |
getTableCellRendererComponent( |
200 | |
table, longValues[i] + "W", |
201 | |
false, false, 0, i); |
202 | 3 | cellWidth = comp.getPreferredSize().width; |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | 3 | if (cellWidth > 10000) { |
210 | 0 | LOGGER.debug("Column unusually wide (" + cellWidth + "): calc again"); |
211 | |
|
212 | 0 | cellWidth = comp.getPreferredSize().width; |
213 | |
|
214 | 0 | LOGGER.debug("Result of recalc (" + cellWidth + ")"); |
215 | |
} |
216 | |
|
217 | 3 | LOGGER.debug("Initializing width of column " + i + ". " |
218 | |
+ "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth |
219 | |
+ "; longValue = [" + longValues[i] + "]"); |
220 | |
|
221 | |
|
222 | 3 | column.setPreferredWidth(Math.max(headerWidth, cellWidth)); |
223 | |
} |
224 | 1 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public static Object[] getLongestValues(TableModel model) { |
235 | |
Object[] objlLongest; |
236 | |
Object objlValue; |
237 | |
int[] ilLen; |
238 | |
int ilThisLen; |
239 | |
int ilNumRows; |
240 | |
int ilRow; |
241 | |
int ilNumCols; |
242 | |
int ilCol; |
243 | |
|
244 | 2 | ilNumCols = model.getColumnCount(); |
245 | 2 | ilNumRows = model.getRowCount(); |
246 | |
|
247 | 2 | objlLongest = new Object[ilNumCols]; |
248 | 2 | ilLen = new int[ilNumCols]; |
249 | |
|
250 | 8 | for (ilCol = 0; ilCol < ilNumCols; ++ilCol) { |
251 | 6 | objlLongest[ilCol] = ""; |
252 | 6 | ilLen[ilCol] = 0; |
253 | |
} |
254 | |
|
255 | 13 | for (ilRow = 0; ilRow < ilNumRows; ++ilRow) { |
256 | 44 | for (ilCol = 0; ilCol < ilNumCols; ++ilCol) { |
257 | 33 | objlValue = model.getValueAt(ilRow, ilCol); |
258 | 33 | if (objlValue != null) { |
259 | 27 | if ((ilThisLen = objlValue.toString().length()) > ilLen[ilCol]) { |
260 | 9 | objlLongest[ilCol] = objlValue; |
261 | 9 | ilLen[ilCol] = ilThisLen; |
262 | 9 | LOGGER.debug("Get longest value, Checking(" + ilRow + "," + ilCol |
263 | |
+ ")=" + ilThisLen); |
264 | |
} |
265 | |
} |
266 | |
} |
267 | |
} |
268 | |
|
269 | 2 | return objlLongest; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public static String formattedDate(java.util.Date date) { |
281 | 1 | return formattedDate(date, "MM/dd/yyyy HH:mm:ss.SSS"); |
282 | |
} |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
public static String formattedDate(java.util.Date date, String formatString) { |
295 | |
SimpleDateFormat format; |
296 | |
|
297 | 2 | format = (SimpleDateFormat) formats.get(formatString); |
298 | 2 | if (format == null) { |
299 | 2 | format = new SimpleDateFormat(formatString); |
300 | 2 | formats.put(formatString, format); |
301 | |
} |
302 | |
|
303 | 2 | return format.format(date); |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public static String formattedNumber(long number, String format) { |
317 | |
DecimalFormat fmt; |
318 | |
|
319 | 1 | fmt = new DecimalFormat(format); |
320 | |
|
321 | 1 | return fmt.format(number); |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public static final String characterInsert(String saData, String saInsert, |
353 | |
int iaMinSectionLength, |
354 | |
int iaMaxSectionLength, |
355 | |
String saBreakCharacters) { |
356 | |
String result; |
357 | |
StringBuffer sblResult; |
358 | |
char[] clData; |
359 | |
int ilLen, ilPosit, ilSectionLength; |
360 | |
|
361 | 2 | if (saData != null) { |
362 | 2 | ilLen = saData.length(); |
363 | 2 | clData = new char[ilLen]; |
364 | 2 | saData.getChars(0, ilLen, clData, 0); |
365 | 2 | ilPosit = 0; |
366 | 2 | ilSectionLength = 0; |
367 | 2 | sblResult = new StringBuffer(ilLen * 2); |
368 | |
|
369 | 24 | for (ilPosit = 0; ilPosit < ilLen; ++ilPosit) { |
370 | 22 | sblResult.append(clData[ilPosit]); |
371 | 22 | ++ilSectionLength; |
372 | 22 | if (saInsert.length() == 1 && clData[ilPosit] == saInsert.charAt(0)) { |
373 | 1 | ilSectionLength = 0; |
374 | 21 | } else if ((ilSectionLength >= iaMinSectionLength && saBreakCharacters |
375 | |
.indexOf(clData[ilPosit]) >= 0) |
376 | |
|| ilSectionLength >= iaMaxSectionLength) { |
377 | 6 | sblResult.append(saInsert); |
378 | 6 | ilSectionLength = 0; |
379 | |
} |
380 | |
} |
381 | 2 | result = sblResult.toString(); |
382 | |
} else { |
383 | 0 | result = saData; |
384 | |
} |
385 | |
|
386 | 2 | return result; |
387 | |
} |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
public static String[] splitWithQuotes(String data, String splitPattern) { |
418 | |
String[] maxParts; |
419 | |
List<String> tempParts; |
420 | |
String recombined; |
421 | |
int partIndex; |
422 | |
List<Character> stackOfQuotes; |
423 | |
char[] partData; |
424 | |
int charIndex; |
425 | |
boolean escapeNext; |
426 | |
|
427 | 3 | stackOfQuotes = new ArrayList<Character>(); |
428 | 3 | maxParts = data.split(splitPattern); |
429 | 3 | tempParts = new ArrayList<String>(); |
430 | 3 | recombined = ""; |
431 | 3 | escapeNext = false; |
432 | |
|
433 | 3 | LOGGER.debug("Split [" + data + "] at [" + splitPattern + "]"); |
434 | |
|
435 | 17 | for (partIndex = 0; partIndex < maxParts.length; ++partIndex) { |
436 | 14 | partData = maxParts[partIndex].toCharArray(); |
437 | 106 | for (charIndex = 0; charIndex < partData.length; ++charIndex) { |
438 | 92 | if (!escapeNext |
439 | |
&& (partData[charIndex] == '\'' || partData[charIndex] == '"')) { |
440 | 8 | if (stackOfQuotes.size() > 0) { |
441 | 5 | if (((Character) stackOfQuotes.get(0)).charValue() == partData[charIndex]) { |
442 | 4 | stackOfQuotes.remove(0); |
443 | |
} else { |
444 | 1 | stackOfQuotes.add(0, new Character(partData[charIndex])); |
445 | |
} |
446 | |
} else { |
447 | 3 | stackOfQuotes.add(0, new Character(partData[charIndex])); |
448 | |
} |
449 | 84 | } else if (!escapeNext && partData[charIndex] == '\\') { |
450 | 0 | escapeNext = true; |
451 | 84 | } else if (escapeNext) { |
452 | 0 | escapeNext = false; |
453 | |
} |
454 | |
|
455 | |
} |
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | 14 | if (recombined.length() > 0) { |
471 | 5 | recombined += splitPattern; |
472 | |
} |
473 | |
|
474 | 14 | recombined += maxParts[partIndex]; |
475 | |
|
476 | |
|
477 | 14 | if (stackOfQuotes.size() == 0) { |
478 | 9 | tempParts.add(recombined); |
479 | 9 | recombined = ""; |
480 | |
} |
481 | |
} |
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | 3 | if (recombined.length() > 0) { |
489 | 0 | tempParts.add(recombined); |
490 | |
} |
491 | |
|
492 | |
|
493 | 3 | return (String[]) tempParts.toArray(new String[tempParts.size()]); |
494 | |
} |
495 | |
} |