1919import org .httprpc .kilo .io .Encoder ;
2020
2121import javax .imageio .ImageIO ;
22+ import javax .swing .AbstractAction ;
2223import javax .swing .AbstractButton ;
24+ import javax .swing .Action ;
2325import javax .swing .ButtonGroup ;
2426import javax .swing .Icon ;
2527import javax .swing .JButton ;
4345import javax .swing .JTextField ;
4446import javax .swing .JToggleButton ;
4547import javax .swing .JTree ;
48+ import javax .swing .KeyStroke ;
4649import javax .swing .SwingConstants ;
4750import javax .swing .UIManager ;
4851import javax .swing .border .CompoundBorder ;
4952import javax .swing .border .EmptyBorder ;
5053import javax .swing .border .LineBorder ;
54+ import javax .swing .undo .UndoManager ;
5155import javax .xml .stream .Location ;
5256import javax .xml .stream .XMLInputFactory ;
5357import javax .xml .stream .XMLStreamConstants ;
5761import java .awt .Dimension ;
5862import java .awt .Font ;
5963import java .awt .Image ;
64+ import java .awt .event .ActionEvent ;
65+ import java .awt .event .InputEvent ;
66+ import java .awt .event .KeyEvent ;
6067import java .io .File ;
6168import java .io .FileOutputStream ;
6269import 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