Skip to content
This repository was archived by the owner on Jul 26, 2018. It is now read-only.

Commit e778e54

Browse files
author
peterst
committed
changed error handling, some usability features
1 parent c51c994 commit e778e54

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

bin/WebSocketClientTerminal-x.x.jar

1.13 KB
Binary file not shown.

lib/weberknecht-fork.jar

2.17 KB
Binary file not shown.

src/com/alphabot/websocket/client/terminal/WebSocketClientTerminalView.java

+23-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.alphabot.websocket.client.terminal;
1717

18-
import java.util.logging.Level;
19-
import java.util.logging.Logger;
2018
import org.jdesktop.application.Action;
2119
import org.jdesktop.application.ResourceMap;
2220
import org.jdesktop.application.SingleFrameApplication;
@@ -36,6 +34,8 @@
3634
import de.roderick.weberknecht.WebSocketMessage;
3735
import java.net.URI;
3836
import java.net.URISyntaxException;
37+
import java.util.LinkedHashMap;
38+
import javax.swing.JOptionPane;
3939

4040
/**
4141
* The application's main frame.
@@ -75,6 +75,8 @@ public void actionPerformed(ActionEvent e) {
7575

7676
messageField.setEnabled(false);
7777
messageButton.setEnabled(false);
78+
79+
this.getRootPane().setDefaultButton(messageButton);
7880

7981
// connecting action tasks to status bar via TaskMonitor
8082
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
@@ -321,11 +323,12 @@ public void connectClick() {
321323
if (protocolName.isEmpty()) {
322324
protocolName = null;
323325
}
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);
329332

330333

331334
// Register Event Handlers
@@ -336,7 +339,7 @@ public void onOpen() {
336339
}
337340

338341
public void onMessage(WebSocketMessage wsm) {
339-
writeLogEntry("[I] " + wsm.getText());
342+
writeLogEntry(wsm.getText());
340343
}
341344

342345
public void onClose() {
@@ -361,19 +364,15 @@ public void onPong() {
361364
changeUIState();
362365

363366
} catch (URISyntaxException use) {
364-
writeLogEntry("[exception] " + use.getMessage());
367+
displayErrorMessage(use.getMessage());
365368
use.printStackTrace();
366369
} catch (WebSocketException wse) {
367-
writeLogEntry("[exception] " + wse.getMessage());
370+
displayErrorMessage(wse.getMessage());
368371
wse.printStackTrace();
369372
}
370-
371-
372373
} else {
373374
disconnect();
374375
}
375-
376-
377376
}
378377

379378
private void disconnect() {
@@ -382,6 +381,7 @@ private void disconnect() {
382381
changeUIState();
383382
websocket.close();
384383
} catch (WebSocketException wse) {
384+
displayErrorMessage(wse.getMessage());
385385
wse.printStackTrace();
386386
}
387387
}
@@ -395,6 +395,7 @@ private void changeUIState() {
395395
originField.setEnabled(false);
396396
messageField.setEnabled(true);
397397
messageButton.setEnabled(true);
398+
messageField.requestFocus();
398399

399400
if (!busyIconTimer.isRunning()) {
400401
statusAnimationLabel.setIcon(busyIcons[0]);
@@ -422,23 +423,30 @@ private void changeUIState() {
422423
private void writeLogEntry(String logMessage) {
423424
logArea.append(logMessage + "\n");
424425
}
426+
427+
private void displayErrorMessage(String errorMessage) {
428+
JOptionPane.showMessageDialog(new JFrame(), errorMessage,"Error", JOptionPane.ERROR_MESSAGE);
429+
}
425430

426431
@Action
427432
public void sendClick() {
428433
try {
429434
websocket.send(messageField.getText());
430-
writeLogEntry("[O] " + messageField.getText());
435+
writeLogEntry(messageField.getText());
431436
} catch (WebSocketException wse) {
437+
displayErrorMessage(wse.getMessage());
432438
wse.printStackTrace();
433439
} finally {
434440
messageField.setText("");
441+
messageField.requestFocus();
435442
}
436443
}
437444

438445
@Action
439446
public void clearLog() {
440447
logArea.setText("");
441448
}
449+
442450
private WebSocket websocket;
443451
private boolean connected;
444452
// Variables declaration - do not modify//GEN-BEGIN:variables

0 commit comments

Comments
 (0)