/
EventsOrderingFix.java
502 lines (437 loc) · 15.2 KB
/
EventsOrderingFix.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package fr.sazaju.mgqeditor.fix;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import org.apache.commons.io.FileUtils;
import fr.sazaju.mgqeditor.parser.regex.Events;
import fr.sazaju.mgqeditor.parser.regex.Events.Event;
import fr.sazaju.mgqeditor.util.LoggerUtil;
import fr.vergne.collection.util.NaturalComparator;
import fr.vergne.flatmap.FlatMapSupport;
import fr.vergne.parsing.layer.exception.ParsingException;
import fr.vergne.translation.util.Setting.SettingKey;
import fr.vergne.translation.util.Switcher;
import fr.vergne.translation.util.impl.PropertyFileSetting;
@SuppressWarnings("serial")
public class EventsOrderingFix extends JPanel {
private static final String CHARSET = "UTF8";
private static final Logger logger = Logger.getLogger(EventsOrderingFix.class.getName());
private final Map<File, Boolean> mapSelection = new LinkedHashMap<>();
private final Collection<JCheckBox> checkBoxes = new LinkedList<>();
private final Collection<JButton> featureButtons = new LinkedList<>();
private final JPanel mapListPanel;
public EventsOrderingFix(PropertyFileSetting settings, SettingKey<String> mapDirSetting) {
setLayout(new GridLayout(1, 1));
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setBorder(null);
add(splitPane);
mapListPanel = new JPanel();
splitPane.setLeftComponent(createFeatures(mapListPanel, settings, mapDirSetting));
splitPane.setRightComponent(createLogArea());
splitPane.setResizeWeight(0);
addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
splitPane.setDividerLocation(0.7);
removeComponentListener(this);
}
@Override
public void componentMoved(ComponentEvent e) {
// Nothing to do
}
@Override
public void componentShown(ComponentEvent e) {
// Nothing to do
}
@Override
public void componentHidden(ComponentEvent e) {
// Nothing to do
}
});
}
private JPanel createFeatures(JPanel mapListPanel, PropertyFileSetting settings, SettingKey<String> mapDirSetting) {
JPanel features = new JPanel();
features.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(5, 5, 5, 5);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = 2;
constraints.weightx = 1;
constraints.weighty = 1;
features.add(createProjectFolderInput(settings, mapDirSetting), constraints);
constraints.gridy++;
constraints.fill = GridBagConstraints.VERTICAL;
constraints.gridwidth = 1;
constraints.gridheight = 4;
constraints.weightx = 0;
constraints.weighty = 1;
JScrollPane scrollPane = new JScrollPane(mapListPanel) {
@Override
public Dimension getMinimumSize() {
Component component = viewport.getComponent(0);
int width = super.getMinimumSize().width + component.getMinimumSize().width;
return new Dimension(width, super.getMinimumSize().height);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, 300);
}
};
scrollPane.setBorder(new TitledBorder("Maps"));
features.add(scrollPane, constraints);
constraints.gridx++;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridheight = 1;
constraints.weightx = 1;
constraints.weighty = 1;
JButton selectAllButton = new JButton(new AbstractAction("Select all") {
@Override
public void actionPerformed(ActionEvent e) {
process(() -> {
logger.info("Select all maps.");
checkBoxes.forEach((checkBox) -> {
checkBox.setSelected(true);
});
mapSelection.forEach((file, isSelected) -> mapSelection.put(file, true));
logger.info("Maps selected.");
});
}
});
features.add(selectAllButton, constraints);
featureButtons.add(selectAllButton);
constraints.gridy++;
JButton deselectAllButton = new JButton(new AbstractAction("Deselect all") {
@Override
public void actionPerformed(ActionEvent e) {
process(() -> {
logger.info("Deselect all maps.");
checkBoxes.forEach((checkBox) -> {
checkBox.setSelected(false);
});
mapSelection.forEach((file, isSelected) -> mapSelection.put(file, false));
logger.info("Maps deselected.");
});
}
});
features.add(deselectAllButton, constraints);
featureButtons.add(deselectAllButton);
constraints.gridy++;
JButton sortingButon = createEventSortingButton();
features.add(sortingButon, constraints);
featureButtons.add(sortingButon);
constraints.gridy++;
JButton normalizeButton = createNormalizationButton();
features.add(normalizeButton, constraints);
featureButtons.add(normalizeButton);
return features;
}
private Component createProjectFolderInput(PropertyFileSetting settings, SettingKey<String> mapDirSetting) {
JTextField folderPathField = new JTextField();
folderPathField.setEditable(false);
String directory = settings.get(mapDirSetting);
if (directory == null) {
folderPathField.setText("Project folder...");
GridBagConstraints constraints = new GridBagConstraints();
mapListPanel.add(new JLabel("First select folder above."), constraints);
} else {
folderPathField.setText(directory);
updateMapList(new File(directory));
}
JButton browseButton = new JButton(new AbstractAction("Browse") {
@Override
public void actionPerformed(ActionEvent arg0) {
String path = folderPathField.getText();
JFileChooser fileChooser = new JFileChooser(new File(path.isEmpty() ? "." : path));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setFileHidingEnabled(true);
fileChooser.setMultiSelectionEnabled(false);
int answer = fileChooser.showDialog(EventsOrderingFix.this, "Browse");
if (answer == JFileChooser.APPROVE_OPTION) {
process(() -> {
File directory = fileChooser.getSelectedFile();
settings.set(mapDirSetting, directory.toString());
folderPathField.setText(directory.toString());
updateMapList(directory);
});
} else {
// do not consider it
}
}
});
browseButton.setToolTipText("Select the MGQ Paradox folder.");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
panel.add(folderPathField, constraints);
constraints.weightx = 0;
constraints.fill = GridBagConstraints.NONE;
constraints.gridx++;
panel.add(browseButton, constraints);
return panel;
}
protected void unlockFeatures() {
featureButtons.forEach((b) -> b.setEnabled(true));
}
protected void lockFeatures() {
featureButtons.forEach((b) -> b.setEnabled(false));
}
private JButton createEventSortingButton() {
JButton button = new JButton(new AbstractAction("Sort events") {
@Override
public void actionPerformed(ActionEvent e) {
process(() -> {
logger.info("Sort selected maps.");
mapSelection.forEach((file, isSelected) -> {
if (isSelected) {
reorderEvents(file);
}
});
logger.info("Sort done.");
});
}
});
button.setToolTipText("Sort the events by numbers.");
return button;
}
private JButton createNormalizationButton() {
JButton button = new JButton(new AbstractAction("Normalize sizes") {
@Override
public void actionPerformed(ActionEvent e) {
process(() -> {
logger.info("Normalize selected maps.");
mapSelection.forEach((file, isSelected) -> {
if (isSelected) {
normalizeSeparatingLines(file);
}
});
logger.info("Normalization done.");
});
}
});
button.setToolTipText("Force the events to be separated by exactly 1 empty line.");
return button;
}
private JScrollPane createLogArea() {
JEditorPane logArea = new JEditorPane();
logArea.setEditable(false);
logArea.setContentType("text/html");
logArea.setMinimumSize(new Dimension(0, 100));
HTMLEditorKit editor = (HTMLEditorKit) logArea.getEditorKit();
StyleSheet styleSheet = editor.getStyleSheet();
styleSheet.addRule("p {margin:0; padding:0;}");
styleSheet.addRule("p.error {color:red; font-weight:bold;}");
HTMLDocument document = (HTMLDocument) logArea.getDocument();
Element body = findBodyElement(document);
Handler handler = new Handler() {
@Override
public void publish(LogRecord record) {
try {
if (record.getLevel() == Level.SEVERE) {
document.insertBeforeEnd(body, "<p class='error'>" + record.getMessage() + "</p>");
document.insertBeforeEnd(body, "<p class='error'>" + "[" + record.getSourceClassName() + "."
+ record.getSourceMethodName() + "]" + "</p>");
} else {
document.insertBeforeEnd(body, "<p>" + record.getMessage() + "</p>");
}
Document doc = logArea.getDocument();
String text = doc.getText(0, doc.getLength());
logArea.setCaretPosition(text.lastIndexOf("\n") + 1);
} catch (Exception cause) {
throw new RuntimeException(cause);
}
}
@Override
public void flush() {
// Nothing to flush
}
@Override
public void close() throws SecurityException {
// Nothing to close
}
};
logger.addHandler(handler);
JScrollPane scrollPane = new JScrollPane(logArea);
scrollPane.setBorder(new TitledBorder("Logs"));
return scrollPane;
}
private Element findBodyElement(HTMLDocument document) {
Element[] roots = document.getRootElements();
Element body = null;
for (int i = 0; i < roots[0].getElementCount(); i++) {
Element element = roots[0].getElement(i);
if (element.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.BODY) {
body = element;
break;
}
}
return body;
}
private static Stream<Path> listPaths(Path p) {
try {
return Files.list(p);
} catch (IOException cause) {
throw new RuntimeException(cause);
}
}
private static boolean isHidden(Path p) {
try {
return Files.isHidden(p);
} catch (IOException cause) {
throw new RuntimeException(cause);
}
}
private void updateMapList(File mapDirectory) {
logger.info("Search for maps in " + mapDirectory);
List<File> maps = FlatMapSupport
.recursiveFlatMapFromRoot(mapDirectory.toPath(), (p) -> !isHidden(p) && Files.isDirectory(p),
(p) -> listPaths(p))
.filter((p) -> p.toFile().getName().matches("Map[0-9]+\\.txt")).map((p) -> p.toFile())
.sorted(new NaturalComparator<>()).collect(Collectors.toList());
logger.info("Maps found: " + maps.size());
logger.info("Updating graphical interface...");
mapListPanel.removeAll();
mapSelection.clear();
checkBoxes.clear();
mapListPanel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.FIRST_LINE_START;
constraints.gridx = 0;
constraints.gridy = 0;
for (File mapFile : maps) {
JCheckBox checkBox = new JCheckBox();
checkBox.setAction(new AbstractAction(mapFile.getName()) {
@Override
public void actionPerformed(ActionEvent e) {
mapSelection.put(mapFile, checkBox.isSelected());
}
});
checkBox.setSelected(false);
mapSelection.put(mapFile, false);
mapListPanel.add(checkBox, constraints);
checkBoxes.add(checkBox);
constraints.gridy++;
}
revalidate();
logger.info("Graphical interface updated");
}
public static void main(String[] args) throws IOException {
LoggerUtil.LoadLoggingProperties();
new Thread(new Runnable() {
public void run() {
try {
PropertyFileSetting settings = new PropertyFileSetting(new File("mgq-editor.ini"));
settings.setFutureSwitcher("mapDir", Switcher.identity());
SettingKey<String> mapDirSetting = settings.registerKey("mapDir", null);
JFrame frame = new JFrame("Event Ordering Fix");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridLayout(1, 1));
frame.getContentPane().add(new EventsOrderingFix(settings, mapDirSetting));
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
logger.log(Level.SEVERE, null, e);
}
}
}).start();
}
public static void reorderEvents(File file) {
try {
logger.info("Reorder events of " + file.getName());
Events events = new Events();
events.setContent(FileUtils.readFileToString(file, CHARSET));
events.sort((e1, e2) -> Integer.compare(e1.getEventID(), e2.getEventID()));
FileUtils.write(file, events.getContent(), CHARSET);
logger.info("Reordered events: " + events.size());
} catch (IOException cause) {
logger.log(Level.SEVERE, "Impossible to read file: " + file, cause);
} catch (ParsingException cause) {
logger.log(Level.SEVERE, "Impossible to parse file: " + file, cause);
}
}
public static void normalizeSeparatingLines(File file) {
try {
logger.info("Normalize separating lines of " + file.getName());
String content = FileUtils.readFileToString(file, CHARSET);
content = normalizeSeparatingLines(content);
FileUtils.write(file, content, CHARSET);
} catch (IOException cause) {
logger.log(Level.SEVERE, "Impossible to read file: " + file, cause);
} catch (ParsingException cause) {
logger.log(Level.SEVERE, "Impossible to parse file: " + file, cause);
}
}
public static String normalizeSeparatingLines(String content) {
Events events = new Events();
events.setContent(content);
if (events.size() >= 2) {
for (Event event : events.getEvents()) {
if (!event.getLine(event.size() - 1).getContent().trim().isEmpty()) {
event.addLine(event.size(), "");
} else {
while (event.getLine(event.size() - 2).getContent().trim().isEmpty()) {
event.removeLine(event.size() - 1);
}
}
}
logger.info("Events separated: " + events.size());
} else {
logger.info("No event to separate");
}
return events.getContent();
}
private void process(Runnable process) {
lockFeatures();
Thread thread = new Thread(() -> {
process.run();
unlockFeatures();
});
thread.setDaemon(true);
thread.start();
}
}