-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeautify_Table.java
137 lines (129 loc) · 4.06 KB
/
Beautify_Table.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
* Beautify Text Table 2
*
* @author Corin Langosch ([email protected])
* v1.0 (2011/04/05)
* @author Marcelo Gennari ([email protected])
* v2.1 (2022/03/28) +showOptionDialog +StringBuilder(faster) +metrics
*
* |Corin|Langosch|[email protected]
* |Marcelo|Gennari|[email protected]
*
* becomes
*
* | Corin | Langosch | [email protected]
* | Marcelo | Gennari | [email protected]
*
*******************************************************************************/
void showDialog() {
String[] options = new String[] {"Comma (,)", "Pipe (|)", "Semicolon (;)", "Tab"};
int optionValue = JOptionPane.showOptionDialog (
view // parent component
, "Choose delimiter" // message
, "Beautify Text Table" // title
, JOptionPane.DEFAULT_OPTION // optionType
, JOptionPane.PLAIN_MESSAGE // messageType
, null // icon
, options // options
, options[3] // initialValue
);
if (optionValue == 0) { beautifyTextTable(","); }
else if (optionValue == 1) { beautifyTextTable("|"); }
else if (optionValue == 2) { beautifyTextTable(";"); }
else if (optionValue == 3) { beautifyTextTable("\\t"); }
}
void beautifyTextTable(separator) {
long startTime = System.nanoTime();
int firstLineColumns; String msg;
String chrRegex = ("|".indexOf(separator) >= 0) ? "\\" + separator : separator;
String checkLastChar = ".*" + chrRegex + "$";
String text = null;
if (textArea.getSelectedText() != null) {
text = textArea.getSelectedText();
} else {
int userChoice = Macros.confirm(view, "Proceed to ALL text?", JOptionPane.YES_NO_OPTION);
if (userChoice == JOptionPane.YES_OPTION) {
text = textArea.getText();
}
}
if (text == null) { return; }
// Get all text into an array
String[] lines = text.split("\\r?\\n");
ArrayList rows = new ArrayList();
ArrayList column_widths = new ArrayList();
for (int i=0; i<lines.length; i++) {
String line = lines[i];
if (line.matches(checkLastChar)) { line += ' '; } // if lastchar is separator, add space to avoid line.split error
String[] cols = line.split(chrRegex);
if (i == 0) {
firstLineColumns = cols.length;
if (firstLineColumns <= 1) {
msg = "Just one column. Nothing to be done.";
Macros.message(view, msg); view.getStatus().setMessageAndClear(msg);
return;
}
} else {
if (cols.length != firstLineColumns) {
msg = "ERROR: Line " + ++i + " contains " + cols.length + " columns. Expected: " + firstLineColumns;
Macros.message(view, msg); view.getStatus().setMessageAndClear(msg);
return;
}
}
for (int j=0; j<cols.length; j++) {
String col = cols[j];
if (j > 0) {
col = col.trim();
cols[j] = col;
}
Integer width = col.length();
if (column_widths.size() <= j) {
column_widths.add(width);
} else {
if (column_widths.get(j) < width) {
column_widths.set(j, width);
}
}
}
rows.add(cols);
}
// Print array back into a formatted string
if (separator.equals("\\t")) { separator = "|"; }
String lineSeparator = "\n";
int lastColumn = column_widths.size() - 1;
int someReasonableIncrement = 10240;
StringBuilder sb = new StringBuilder(text.length() + someReasonableIncrement);
for (int i=0; i<rows.size(); i++) {
String[] cols = rows.get(i);
for (int j=0; j<column_widths.size(); j++) {
String col = cols[j];
Integer width = column_widths.get(j);
Integer padding = width - col.length();
if (j == 0) {
sb.append(col+' ');
} else {
if (j != lastColumn) {
sb.append(' '+col+' ');
} else {
sb.append(' '+col);
}
}
if (j != lastColumn) {
for (int k = 0; k < padding; k++) {
sb.append(" ");
}
sb.append(separator);
}
}
sb.append(lineSeparator);
}
if (textArea.getSelectedText() != null) {
textArea.setSelectedText(sb.toString());
} else {
textArea.setText(sb.toString());
}
long endTime = System.nanoTime();
long totalTime = (endTime - startTime) / 1000000;
msg = "Lines:" + rows.size() + " Columns:" + column_widths.size() + " Time:" + totalTime + " ms";
view.getStatus().setMessageAndClear(msg);
}
showDialog();