88import java .awt .Font ;
99import java .awt .event .ActionEvent ;
1010import java .awt .event .ActionListener ;
11+ import java .awt .event .KeyEvent ;
12+ import java .awt .event .KeyAdapter ;
13+ import java .awt .event .MouseWheelEvent ;
1114import java .awt .event .WindowAdapter ;
1215import java .awt .event .WindowEvent ;
1316import java .text .SimpleDateFormat ;
3235@ SuppressWarnings ("serial" )
3336public abstract class AbstractTextMonitor extends AbstractMonitor {
3437
38+ private final Base base ;
39+
3540 protected JLabel noLineEndingAlert ;
3641 protected TextAreaFIFO textArea ;
3742 protected JScrollPane scrollPane ;
@@ -43,27 +48,58 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
4348 protected JComboBox lineEndings ;
4449 protected JComboBox serialRates ;
4550
46- public AbstractTextMonitor (BoardPort boardPort ) {
51+ public AbstractTextMonitor (Base base , BoardPort boardPort ) {
4752 super (boardPort );
53+ this .base = base ;
4854 }
4955
5056 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 ()));
5457
5558 mainPane .setLayout (new BorderLayout ());
5659
5760 textArea = new TextAreaFIFO (8_000_000 );
5861 textArea .setRows (16 );
5962 textArea .setColumns (40 );
6063 textArea .setEditable (false );
61- textArea .setFont (font );
6264
6365 // don't automatically update the caret. that way we can manually decide
6466 // whether or not to do so based on the autoscroll checkbox.
6567 ((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
6668
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+
67103 scrollPane = new JScrollPane (textArea );
68104
69105 mainPane .add (scrollPane , BorderLayout .CENTER );
@@ -110,12 +146,6 @@ public void actionPerformed(ActionEvent event) {
110146 noLineEndingAlert .setForeground (pane .getBackground ());
111147 }
112148 });
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- }
119149 addTimeStampBox .addActionListener (new ActionListener () {
120150 public void actionPerformed (ActionEvent e ) {
121151 PreferencesData .setBoolean ("serial.show_timestamp" , addTimeStampBox .isSelected ());
@@ -142,6 +172,8 @@ public void actionPerformed(ActionEvent e) {
142172 pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
143173 pane .add (clearButton );
144174
175+ applyPreferences ();
176+
145177 mainPane .add (pane , BorderLayout .SOUTH );
146178 }
147179
@@ -189,6 +221,26 @@ protected void updateTextArea(String msg) {
189221 }
190222 }
191223
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+
192244 private String addTimestamps (String text ) {
193245 String now = new SimpleDateFormat ("HH:mm:ss.SSS -> " ).format (new Date ());
194246 final StringBuilder sb = new StringBuilder (text .length () + now .length ());
0 commit comments