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 ;
3632@ SuppressWarnings ("serial" )
3733public abstract class AbstractMonitor extends JFrame implements ActionListener {
3834
39- protected final JLabel noLineEndingAlert ;
40- protected TextAreaFIFO textArea ;
41- protected JScrollPane scrollPane ;
42- protected JTextField textField ;
43- protected JButton sendButton ;
44- protected JCheckBox autoscrollBox ;
45- protected JComboBox lineEndings ;
46- protected JComboBox serialRates ;
4735 private boolean monitorEnabled ;
4836 private boolean closed ;
4937
50- private Timer updateTimer ;
5138 private StringBuffer updateBuffer ;
39+ private Timer updateTimer ;
5240
5341 public AbstractMonitor (String title ) {
5442 super (title );
@@ -78,87 +66,10 @@ public void actionPerformed(ActionEvent event) {
7866 }
7967 }));
8068
81- getContentPane ().setLayout (new BorderLayout ());
82-
83- Font consoleFont = Theme .getFont ("console.font" );
84- Font editorFont = PreferencesData .getFont ("editor.font" );
85- Font font = new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ());
86-
87- textArea = new TextAreaFIFO (8000000 );
88- textArea .setRows (16 );
89- textArea .setColumns (40 );
90- textArea .setEditable (false );
91- textArea .setFont (font );
92-
93- // don't automatically update the caret. that way we can manually decide
94- // whether or not to do so based on the autoscroll checkbox.
95- ((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
96-
97- scrollPane = new JScrollPane (textArea );
98-
99- getContentPane ().add (scrollPane , BorderLayout .CENTER );
100-
101- JPanel upperPane = new JPanel ();
102- upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
103- upperPane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
104-
105- textField = new JTextField (40 );
106- sendButton = new JButton (_ ("Send" ));
107-
108- upperPane .add (textField );
109- upperPane .add (Box .createRigidArea (new Dimension (4 , 0 )));
110- upperPane .add (sendButton );
111-
112- getContentPane ().add (upperPane , BorderLayout .NORTH );
113-
114- final JPanel pane = new JPanel ();
115- pane .setLayout (new BoxLayout (pane , BoxLayout .X_AXIS ));
116- pane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
117-
118- autoscrollBox = new JCheckBox (_ ("Autoscroll" ), true );
119-
120- noLineEndingAlert = new JLabel (I18n .format (_ ("You've pressed {0} but nothing was sent. Should you select a line ending?" ), _ ("Send" )));
121- noLineEndingAlert .setToolTipText (noLineEndingAlert .getText ());
122- noLineEndingAlert .setForeground (pane .getBackground ());
123- Dimension minimumSize = new Dimension (noLineEndingAlert .getMinimumSize ());
124- minimumSize .setSize (minimumSize .getWidth () / 3 , minimumSize .getHeight ());
125- noLineEndingAlert .setMinimumSize (minimumSize );
126-
127- lineEndings = new JComboBox (new String []{_ ("No line ending" ), _ ("Newline" ), _ ("Carriage return" ), _ ("Both NL & CR" )});
128- lineEndings .addActionListener (new ActionListener () {
129- public void actionPerformed (ActionEvent event ) {
130- PreferencesData .setInteger ("serial.line_ending" , lineEndings .getSelectedIndex ());
131- noLineEndingAlert .setForeground (pane .getBackground ());
132- }
133- });
134- if (PreferencesData .get ("serial.line_ending" ) != null ) {
135- lineEndings .setSelectedIndex (PreferencesData .getInteger ("serial.line_ending" ));
136- }
137- lineEndings .setMaximumSize (lineEndings .getMinimumSize ());
138-
139- String [] serialRateStrings = {
140- "300" , "1200" , "2400" , "4800" , "9600" ,
141- "19200" , "38400" , "57600" , "115200" , "230400" , "250000"
142- };
143-
144- serialRates = new JComboBox ();
145- for (String rate : serialRateStrings ) {
146- serialRates .addItem (rate + " " + _ ("baud" ));
147- }
148-
149- serialRates .setMaximumSize (serialRates .getMinimumSize ());
15069
151- pane .add (autoscrollBox );
152- pane .add (Box .createHorizontalGlue ());
153- pane .add (noLineEndingAlert );
154- pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
155- pane .add (lineEndings );
156- pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
157- pane .add (serialRates );
70+ onCreateWindow (getContentPane ());
15871
159- this .setMinimumSize (new Dimension (pane .getMinimumSize ().width , this .getPreferredSize ().height ));
160-
161- getContentPane ().add (pane , BorderLayout .SOUTH );
72+ this .setMinimumSize (new Dimension (getContentPane ().getMinimumSize ().width , this .getPreferredSize ().height ));
16273
16374 pack ();
16475
@@ -184,19 +95,16 @@ public void actionPerformed(ActionEvent event) {
18495 monitorEnabled = true ;
18596 closed = false ;
18697 }
98+
99+ protected abstract void onCreateWindow (Container mainPane );
187100
188101 public void enableWindow (boolean enable )
189102 {
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-
103+ onEnableWindow (enable );
198104 monitorEnabled = enable ;
199105 }
106+
107+ protected abstract void onEnableWindow (boolean enable );
200108
201109 // Puts the window in suspend state, closing the serial port
202110 // to allow other entity (the programmer) to use it
@@ -229,15 +137,6 @@ public void resume() throws SerialException
229137
230138 }
231139
232- public void onSerialRateChange (ActionListener listener ) {
233- serialRates .addActionListener (listener );
234- }
235-
236- public void onSendCommand (ActionListener listener ) {
237- textField .addActionListener (listener );
238- sendButton .addActionListener (listener );
239- }
240-
241140 protected void setPlacement (int [] location ) {
242141 setBounds (location [0 ], location [1 ], location [2 ], location [3 ]);
243142 }
@@ -255,16 +154,7 @@ protected int[] getPlacement() {
255154 return location ;
256155 }
257156
258- public void message (final String s ) {
259- SwingUtilities .invokeLater (new Runnable () {
260- public void run () {
261- textArea .append (s );
262- if (autoscrollBox .isSelected ()) {
263- textArea .setCaretPosition (textArea .getDocument ().getLength ());
264- }
265- }
266- });
267- }
157+ public abstract void message (final String s );
268158
269159 public boolean requiresAuthorization () {
270160 return false ;
@@ -291,18 +181,11 @@ private synchronized String consumeUpdateBuffer() {
291181 updateBuffer .setLength (0 );
292182 return s ;
293183 }
294-
184+
295185 public void actionPerformed (ActionEvent e ) {
296186 final String s = consumeUpdateBuffer ();
297187 if (s .length () > 0 ) {
298- //System.out.println("gui append " + s.length());
299- if (autoscrollBox .isSelected ()) {
300- textArea .appendTrim (s );
301- textArea .setCaretPosition (textArea .getDocument ().getLength ());
302- } else {
303- textArea .appendNoTrim (s );
304- }
188+ message (s );
305189 }
306190 }
307-
308191}
0 commit comments