33import static processing .app .I18n ._ ;
44
55import java .awt .BorderLayout ;
6+ import java .awt .Container ;
67import java .awt .Dimension ;
78import java .awt .Font ;
89import java .awt .Rectangle ;
1516import javax .swing .AbstractAction ;
1617import javax .swing .Box ;
1718import javax .swing .BoxLayout ;
18- import javax .swing .JButton ;
19- import javax .swing .JCheckBox ;
20- import javax .swing .JComboBox ;
2119import javax .swing .JComponent ;
2220import javax .swing .JFrame ;
2321import javax .swing .JLabel ;
2422import javax .swing .JPanel ;
25- import javax .swing .JScrollPane ;
26- import javax .swing .JTextField ;
2723import javax .swing .KeyStroke ;
2824import javax .swing .SwingUtilities ;
2925import javax .swing .Timer ;
3733@ SuppressWarnings ("serial" )
3834public abstract class AbstractMonitor extends JFrame implements ActionListener {
3935
40- protected final JLabel noLineEndingAlert ;
41- protected TextAreaFIFO textArea ;
42- protected JScrollPane scrollPane ;
43- protected JTextField textField ;
44- protected JButton sendButton ;
45- protected JCheckBox autoscrollBox ;
46- protected JComboBox lineEndings ;
47- protected JComboBox serialRates ;
4836 private boolean monitorEnabled ;
4937 private boolean closed ;
5038
51- private Timer updateTimer ;
5239 private StringBuffer updateBuffer ;
40+ private Timer updateTimer ;
5341
5442 private BoardPort boardPort ;
5543
@@ -82,84 +70,10 @@ public void actionPerformed(ActionEvent event) {
8270 }
8371 }));
8472
85- getContentPane ().setLayout (new BorderLayout ());
86-
87- Font consoleFont = Theme .getFont ("console.font" );
88- Font editorFont = PreferencesData .getFont ("editor.font" );
89- Font font = new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ());
90-
91- textArea = new TextAreaFIFO (8000000 );
92- textArea .setRows (16 );
93- textArea .setColumns (40 );
94- textArea .setEditable (false );
95- textArea .setFont (font );
96-
97- // don't automatically update the caret. that way we can manually decide
98- // whether or not to do so based on the autoscroll checkbox.
99- ((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
100-
101- scrollPane = new JScrollPane (textArea );
102-
103- getContentPane ().add (scrollPane , BorderLayout .CENTER );
104-
105- JPanel upperPane = new JPanel ();
106- upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
107- upperPane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
108-
109- textField = new JTextField (40 );
110- sendButton = new JButton (_ ("Send" ));
111-
112- upperPane .add (textField );
113- upperPane .add (Box .createRigidArea (new Dimension (4 , 0 )));
114- upperPane .add (sendButton );
115-
116- getContentPane ().add (upperPane , BorderLayout .NORTH );
117-
118- final JPanel pane = new JPanel ();
119- pane .setLayout (new BoxLayout (pane , BoxLayout .X_AXIS ));
120- pane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
121-
122- autoscrollBox = new JCheckBox (_ ("Autoscroll" ), true );
123-
124- noLineEndingAlert = new JLabel (I18n .format (_ ("You've pressed {0} but nothing was sent. Should you select a line ending?" ), _ ("Send" )));
125- noLineEndingAlert .setToolTipText (noLineEndingAlert .getText ());
126- noLineEndingAlert .setForeground (pane .getBackground ());
127- Dimension minimumSize = new Dimension (noLineEndingAlert .getMinimumSize ());
128- minimumSize .setSize (minimumSize .getWidth () / 3 , minimumSize .getHeight ());
129- noLineEndingAlert .setMinimumSize (minimumSize );
130-
131- lineEndings = new JComboBox (new String []{_ ("No line ending" ), _ ("Newline" ), _ ("Carriage return" ), _ ("Both NL & CR" )});
132- lineEndings .addActionListener (new ActionListener () {
133- public void actionPerformed (ActionEvent event ) {
134- PreferencesData .setInteger ("serial.line_ending" , lineEndings .getSelectedIndex ());
135- noLineEndingAlert .setForeground (pane .getBackground ());
136- }
137- });
138- if (PreferencesData .get ("serial.line_ending" ) != null ) {
139- lineEndings .setSelectedIndex (PreferencesData .getInteger ("serial.line_ending" ));
140- }
141- lineEndings .setMaximumSize (lineEndings .getMinimumSize ());
142-
143- String [] serialRateStrings = {"300" , "1200" , "2400" , "4800" , "9600" , "19200" , "38400" , "57600" , "115200" , "230400" , "250000" };
144-
145- serialRates = new JComboBox ();
146- for (String rate : serialRateStrings ) {
147- serialRates .addItem (rate + " " + _ ("baud" ));
148- }
149-
150- serialRates .setMaximumSize (serialRates .getMinimumSize ());
151-
152- pane .add (autoscrollBox );
153- pane .add (Box .createHorizontalGlue ());
154- pane .add (noLineEndingAlert );
155- pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
156- pane .add (lineEndings );
157- pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
158- pane .add (serialRates );
15973
160- this . setMinimumSize ( new Dimension ( pane . getMinimumSize (). width , this . getPreferredSize (). height ));
74+ onCreateWindow ( getContentPane ( ));
16175
162- getContentPane ().add ( pane , BorderLayout . SOUTH );
76+ this . setMinimumSize ( new Dimension ( getContentPane ().getMinimumSize (). width , this . getPreferredSize (). height ) );
16377
16478 pack ();
16579
@@ -185,18 +99,15 @@ public void actionPerformed(ActionEvent event) {
18599 monitorEnabled = true ;
186100 closed = false ;
187101 }
102+
103+ protected abstract void onCreateWindow (Container mainPane );
188104
189105 public void enableWindow (boolean enable ) {
190- textArea .setEnabled (enable );
191- scrollPane .setEnabled (enable );
192- textField .setEnabled (enable );
193- sendButton .setEnabled (enable );
194- autoscrollBox .setEnabled (enable );
195- lineEndings .setEnabled (enable );
196- serialRates .setEnabled (enable );
197-
106+ onEnableWindow (enable );
198107 monitorEnabled = enable ;
199108 }
109+
110+ protected abstract void onEnableWindow (boolean enable );
200111
201112 // Puts the window in suspend state, closing the serial port
202113 // to allow other entity (the programmer) to use it
@@ -220,15 +131,6 @@ public void resume(BoardPort boardPort) throws Exception {
220131 open ();
221132 }
222133
223- public void onSerialRateChange (ActionListener listener ) {
224- serialRates .addActionListener (listener );
225- }
226-
227- public void onSendCommand (ActionListener listener ) {
228- textField .addActionListener (listener );
229- sendButton .addActionListener (listener );
230- }
231-
232134 protected void setPlacement (int [] location ) {
233135 setBounds (location [0 ], location [1 ], location [2 ], location [3 ]);
234136 }
@@ -246,16 +148,7 @@ protected int[] getPlacement() {
246148 return location ;
247149 }
248150
249- public void message (final String s ) {
250- SwingUtilities .invokeLater (new Runnable () {
251- public void run () {
252- textArea .append (s );
253- if (autoscrollBox .isSelected ()) {
254- textArea .setCaretPosition (textArea .getDocument ().getLength ());
255- }
256- }
257- });
258- }
151+ public abstract void message (final String s );
259152
260153 public boolean requiresAuthorization () {
261154 return false ;
@@ -295,21 +188,13 @@ private synchronized String consumeUpdateBuffer() {
295188 updateBuffer .setLength (0 );
296189 return s ;
297190 }
298-
191+
299192 public void actionPerformed (ActionEvent e ) {
300193 String s = consumeUpdateBuffer ();
301-
302194 if (s .isEmpty ()) {
303195 return ;
304- }
305-
306- //System.out.println("gui append " + s.length());
307- if (autoscrollBox .isSelected ()) {
308- textArea .appendTrim (s );
309- textArea .setCaretPosition (textArea .getDocument ().getLength ());
310196 } else {
311- textArea . appendNoTrim (s );
197+ message (s );
312198 }
313199 }
314-
315200}
0 commit comments