8
8
import java .awt .Font ;
9
9
import java .awt .event .ActionEvent ;
10
10
import java .awt .event .ActionListener ;
11
+ import java .awt .event .KeyEvent ;
12
+ import java .awt .event .KeyAdapter ;
13
+ import java .awt .event .MouseWheelEvent ;
11
14
import java .awt .event .WindowAdapter ;
12
15
import java .awt .event .WindowEvent ;
13
16
import java .text .SimpleDateFormat ;
32
35
@ SuppressWarnings ("serial" )
33
36
public abstract class AbstractTextMonitor extends AbstractMonitor {
34
37
38
+ private final Base base ;
39
+
35
40
protected JLabel noLineEndingAlert ;
36
41
protected TextAreaFIFO textArea ;
37
42
protected JScrollPane scrollPane ;
@@ -43,27 +48,58 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
43
48
protected JComboBox lineEndings ;
44
49
protected JComboBox serialRates ;
45
50
46
- public AbstractTextMonitor (BoardPort boardPort ) {
51
+ public AbstractTextMonitor (Base base , BoardPort boardPort ) {
47
52
super (boardPort );
53
+ this .base = base ;
48
54
}
49
55
50
56
protected void onCreateWindow (Container mainPane ) {
51
- Font consoleFont = Theme .getFont ("console.font" );
52
- Font editorFont = PreferencesData .getFont ("editor.font" );
53
- Font font = Theme .scale (new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ()));
54
57
55
58
mainPane .setLayout (new BorderLayout ());
56
59
57
60
textArea = new TextAreaFIFO (8_000_000 );
58
61
textArea .setRows (16 );
59
62
textArea .setColumns (40 );
60
63
textArea .setEditable (false );
61
- textArea .setFont (font );
62
64
63
65
// don't automatically update the caret. that way we can manually decide
64
66
// whether or not to do so based on the autoscroll checkbox.
65
67
((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
66
68
69
+ // Add "CTRL scroll" hotkey for font size adjustment.
70
+ textArea .addMouseWheelListener ((MouseWheelEvent e ) -> {
71
+ if (e .isControlDown ()) {
72
+ if (e .getWheelRotation () < 0 ) {
73
+ base .handleFontSizeChange (1 );
74
+ } else {
75
+ base .handleFontSizeChange (-1 );
76
+ }
77
+ } else {
78
+ e .getComponent ().getParent ().dispatchEvent (e );
79
+ }
80
+ });
81
+
82
+ // Add "CTRL (SHIFT) =/+" and "CTRL -" hotkeys for font size adjustment.
83
+ textArea .addKeyListener (new KeyAdapter () {
84
+ @ Override
85
+ public void keyPressed (KeyEvent e ) {
86
+ if (e .getModifiersEx () == KeyEvent .CTRL_DOWN_MASK
87
+ || e .getModifiersEx () == (KeyEvent .CTRL_DOWN_MASK | KeyEvent .SHIFT_DOWN_MASK )) {
88
+ switch (e .getKeyCode ()) {
89
+ case KeyEvent .VK_PLUS :
90
+ case KeyEvent .VK_EQUALS :
91
+ base .handleFontSizeChange (1 );
92
+ break ;
93
+ case KeyEvent .VK_MINUS :
94
+ if (!e .isShiftDown ()) {
95
+ base .handleFontSizeChange (-1 );
96
+ }
97
+ break ;
98
+ }
99
+ }
100
+ }
101
+ });
102
+
67
103
scrollPane = new JScrollPane (textArea );
68
104
69
105
mainPane .add (scrollPane , BorderLayout .CENTER );
@@ -110,12 +146,6 @@ public void actionPerformed(ActionEvent event) {
110
146
noLineEndingAlert .setForeground (pane .getBackground ());
111
147
}
112
148
});
113
- if (PreferencesData .get ("serial.line_ending" ) != null ) {
114
- lineEndings .setSelectedIndex (PreferencesData .getInteger ("serial.line_ending" ));
115
- }
116
- if (PreferencesData .get ("serial.show_timestamp" ) != null ) {
117
- addTimeStampBox .setSelected (PreferencesData .getBoolean ("serial.show_timestamp" ));
118
- }
119
149
addTimeStampBox .addActionListener (new ActionListener () {
120
150
public void actionPerformed (ActionEvent e ) {
121
151
PreferencesData .setBoolean ("serial.show_timestamp" , addTimeStampBox .isSelected ());
@@ -142,6 +172,8 @@ public void actionPerformed(ActionEvent e) {
142
172
pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
143
173
pane .add (clearButton );
144
174
175
+ applyPreferences ();
176
+
145
177
mainPane .add (pane , BorderLayout .SOUTH );
146
178
}
147
179
@@ -189,6 +221,26 @@ protected void updateTextArea(String msg) {
189
221
}
190
222
}
191
223
224
+ @ Override
225
+ public void applyPreferences () {
226
+
227
+ // Apply font.
228
+ Font consoleFont = Theme .getFont ("console.font" );
229
+ Font editorFont = PreferencesData .getFont ("editor.font" );
230
+ textArea .setFont (Theme .scale (new Font (
231
+ consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ())));
232
+
233
+ // Apply line endings.
234
+ if (PreferencesData .get ("serial.line_ending" ) != null ) {
235
+ lineEndings .setSelectedIndex (PreferencesData .getInteger ("serial.line_ending" ));
236
+ }
237
+
238
+ // Apply timestamp visibility.
239
+ if (PreferencesData .get ("serial.show_timestamp" ) != null ) {
240
+ addTimeStampBox .setSelected (PreferencesData .getBoolean ("serial.show_timestamp" ));
241
+ }
242
+ }
243
+
192
244
private String addTimestamps (String text ) {
193
245
String now = new SimpleDateFormat ("HH:mm:ss.SSS -> " ).format (new Date ());
194
246
final StringBuilder sb = new StringBuilder (text .length () + now .length ());
0 commit comments