Skip to content

Commit df5530b

Browse files
committed
Update UILoader.
1 parent d47c647 commit df5530b

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

sierra/src/main/java/org/httprpc/sierra/UILoader.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import org.httprpc.kilo.io.Encoder;
2020

2121
import javax.imageio.ImageIO;
22+
import javax.swing.AbstractAction;
2223
import javax.swing.AbstractButton;
24+
import javax.swing.Action;
2325
import javax.swing.ButtonGroup;
2426
import javax.swing.Icon;
2527
import javax.swing.JButton;
@@ -43,11 +45,13 @@
4345
import javax.swing.JTextField;
4446
import javax.swing.JToggleButton;
4547
import javax.swing.JTree;
48+
import javax.swing.KeyStroke;
4649
import javax.swing.SwingConstants;
4750
import javax.swing.UIManager;
4851
import javax.swing.border.CompoundBorder;
4952
import javax.swing.border.EmptyBorder;
5053
import javax.swing.border.LineBorder;
54+
import javax.swing.undo.UndoManager;
5155
import javax.xml.stream.Location;
5256
import javax.xml.stream.XMLInputFactory;
5357
import javax.xml.stream.XMLStreamConstants;
@@ -57,6 +61,9 @@
5761
import java.awt.Dimension;
5862
import java.awt.Font;
5963
import java.awt.Image;
64+
import java.awt.event.ActionEvent;
65+
import java.awt.event.InputEvent;
66+
import java.awt.event.KeyEvent;
6067
import java.io.File;
6168
import java.io.FileOutputStream;
6269
import java.io.IOException;
@@ -341,12 +348,39 @@ void declareAttributeList(String tag, Class<?> type, Writer writer) throws IOExc
341348
private static final String REVERT = "revert";
342349
private static final String PERSIST = "persist";
343350

344-
private static Map<String, Class<?>> types = new HashMap<>();
345-
private static Map<String, Supplier<? extends JComponent>> suppliers = new HashMap<>();
351+
private static final String UNDO_ACTION_KEY = "undo";
346352

347-
private static Map<String, Color> colors = new HashMap<>();
353+
private static final Map<String, Class<?>> types = new HashMap<>();
354+
private static final Map<String, Supplier<? extends JComponent>> suppliers = new HashMap<>();
348355

349-
private static Map<String, Font> fonts = new HashMap<>();
356+
private static final Map<String, Color> colors = new HashMap<>();
357+
358+
private static final Map<String, Font> fonts = new HashMap<>();
359+
360+
private static final UndoManager undoManager = new UndoManager();
361+
362+
private static final Action undoAction = new AbstractAction() {
363+
@Override
364+
public void actionPerformed(ActionEvent event) {
365+
if (undoManager.canUndo()) {
366+
undoManager.undo();
367+
}
368+
}
369+
};
370+
371+
private static final KeyStroke undoKeyStroke;
372+
static {
373+
var osName = System.getProperty("os.name").toLowerCase();
374+
375+
int modifier;
376+
if (osName.contains("mac")) {
377+
modifier = InputEvent.META_DOWN_MASK;
378+
} else {
379+
modifier = InputEvent.CTRL_DOWN_MASK;
380+
}
381+
382+
undoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, modifier);
383+
}
350384

351385
static {
352386
bind("label", JLabel.class, JLabel::new);
@@ -365,7 +399,16 @@ void declareAttributeList(String tag, Class<?> type, Writer writer) throws IOExc
365399
bind("separator", JSeparator.class, JSeparator::new);
366400
bind("scroll-pane", JScrollPane.class, JScrollPane::new);
367401
bind("list", JList.class, JList::new);
368-
bind("text-area", JTextArea.class, JTextArea::new);
402+
bind("text-area", JTextArea.class, () -> {
403+
var textArea = new JTextArea();
404+
405+
textArea.getDocument().addUndoableEditListener(undoManager);
406+
407+
textArea.getActionMap().put(UNDO_ACTION_KEY, undoAction);
408+
textArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(undoKeyStroke, UNDO_ACTION_KEY);
409+
410+
return textArea;
411+
});
369412
bind("table", JTable.class, JTable::new);
370413
bind("tree", JTree.class, JTree::new);
371414

0 commit comments

Comments
 (0)