15
15
*/
16
16
package com .alphabot .websocket .client .terminal ;
17
17
18
- import java .util .logging .Level ;
19
- import java .util .logging .Logger ;
20
18
import org .jdesktop .application .Action ;
21
19
import org .jdesktop .application .ResourceMap ;
22
20
import org .jdesktop .application .SingleFrameApplication ;
36
34
import de .roderick .weberknecht .WebSocketMessage ;
37
35
import java .net .URI ;
38
36
import java .net .URISyntaxException ;
37
+ import java .util .LinkedHashMap ;
38
+ import javax .swing .JOptionPane ;
39
39
40
40
/**
41
41
* The application's main frame.
@@ -75,6 +75,8 @@ public void actionPerformed(ActionEvent e) {
75
75
76
76
messageField .setEnabled (false );
77
77
messageButton .setEnabled (false );
78
+
79
+ this .getRootPane ().setDefaultButton (messageButton );
78
80
79
81
// connecting action tasks to status bar via TaskMonitor
80
82
TaskMonitor taskMonitor = new TaskMonitor (getApplication ().getContext ());
@@ -321,11 +323,12 @@ public void connectClick() {
321
323
if (protocolName .isEmpty ()) {
322
324
protocolName = null ;
323
325
}
324
- String origin = originField .getText ();
325
- if (origin .isEmpty ()) {
326
- origin = null ;
327
- }
328
- websocket = new WebSocket (url , protocolName , origin );
326
+ LinkedHashMap <String , String > extraHeaders = null ;
327
+ if (!originField .getText ().isEmpty ()) {
328
+ extraHeaders = new LinkedHashMap <String , String >();
329
+ extraHeaders .put ("Origin" , originField .getText ());
330
+ }
331
+ websocket = new WebSocket (url , protocolName , extraHeaders );
329
332
330
333
331
334
// Register Event Handlers
@@ -336,7 +339,7 @@ public void onOpen() {
336
339
}
337
340
338
341
public void onMessage (WebSocketMessage wsm ) {
339
- writeLogEntry ("[I] " + wsm .getText ());
342
+ writeLogEntry (wsm .getText ());
340
343
}
341
344
342
345
public void onClose () {
@@ -361,19 +364,15 @@ public void onPong() {
361
364
changeUIState ();
362
365
363
366
} catch (URISyntaxException use ) {
364
- writeLogEntry ( "[exception] " + use .getMessage ());
367
+ displayErrorMessage ( use .getMessage ());
365
368
use .printStackTrace ();
366
369
} catch (WebSocketException wse ) {
367
- writeLogEntry ( "[exception] " + wse .getMessage ());
370
+ displayErrorMessage ( wse .getMessage ());
368
371
wse .printStackTrace ();
369
372
}
370
-
371
-
372
373
} else {
373
374
disconnect ();
374
375
}
375
-
376
-
377
376
}
378
377
379
378
private void disconnect () {
@@ -382,6 +381,7 @@ private void disconnect() {
382
381
changeUIState ();
383
382
websocket .close ();
384
383
} catch (WebSocketException wse ) {
384
+ displayErrorMessage (wse .getMessage ());
385
385
wse .printStackTrace ();
386
386
}
387
387
}
@@ -395,6 +395,7 @@ private void changeUIState() {
395
395
originField .setEnabled (false );
396
396
messageField .setEnabled (true );
397
397
messageButton .setEnabled (true );
398
+ messageField .requestFocus ();
398
399
399
400
if (!busyIconTimer .isRunning ()) {
400
401
statusAnimationLabel .setIcon (busyIcons [0 ]);
@@ -422,23 +423,30 @@ private void changeUIState() {
422
423
private void writeLogEntry (String logMessage ) {
423
424
logArea .append (logMessage + "\n " );
424
425
}
426
+
427
+ private void displayErrorMessage (String errorMessage ) {
428
+ JOptionPane .showMessageDialog (new JFrame (), errorMessage ,"Error" , JOptionPane .ERROR_MESSAGE );
429
+ }
425
430
426
431
@ Action
427
432
public void sendClick () {
428
433
try {
429
434
websocket .send (messageField .getText ());
430
- writeLogEntry ("[O] " + messageField .getText ());
435
+ writeLogEntry (messageField .getText ());
431
436
} catch (WebSocketException wse ) {
437
+ displayErrorMessage (wse .getMessage ());
432
438
wse .printStackTrace ();
433
439
} finally {
434
440
messageField .setText ("" );
441
+ messageField .requestFocus ();
435
442
}
436
443
}
437
444
438
445
@ Action
439
446
public void clearLog () {
440
447
logArea .setText ("" );
441
448
}
449
+
442
450
private WebSocket websocket ;
443
451
private boolean connected ;
444
452
// Variables declaration - do not modify//GEN-BEGIN:variables
0 commit comments