From 1e190716af28b52bb9265ae6e4a370e6587fbfba Mon Sep 17 00:00:00 2001 From: dzmipt Date: Fri, 24 Nov 2023 21:05:52 +0100 Subject: [PATCH] Connection status added to status bar --- notes.md | 1 + src/kx/ConnectionStateListener.java | 6 ++++ src/kx/KConnection.java | 12 ++++++++ src/studio/kdb/Session.java | 11 ++++++- src/studio/ui/EditorPane.java | 4 +++ src/studio/ui/EditorTab.java | 5 ++++ src/studio/ui/statusbar/EditorStatusBar.java | 30 ++++++++++++++------ 7 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/kx/ConnectionStateListener.java diff --git a/notes.md b/notes.md index bf343edd..37017ccb 100644 --- a/notes.md +++ b/notes.md @@ -1,6 +1,7 @@ * Rework kdb connection logic * No IPC protocol downgrade in case of first attempt failure * Fix logic to detect server disconnect + * Connection status added to the status bar * File drag and drop (no support for a folder drag and drop) * Persist location of editor windows * Keep config properties sorted on disk diff --git a/src/kx/ConnectionStateListener.java b/src/kx/ConnectionStateListener.java new file mode 100644 index 00000000..1ab38406 --- /dev/null +++ b/src/kx/ConnectionStateListener.java @@ -0,0 +1,6 @@ +package kx; + +public interface ConnectionStateListener { + + void connectionStateChange(boolean connected); +} diff --git a/src/kx/KConnection.java b/src/kx/KConnection.java index 6dd3b376..ccbfbe17 100755 --- a/src/kx/KConnection.java +++ b/src/kx/KConnection.java @@ -21,6 +21,7 @@ public class KConnection { private Socket s; private SocketReader socketReader; + private ConnectionStateListener connectionStateListener = null; void io(Socket s) throws IOException { s.setTcpNoDelay(true); @@ -50,6 +51,9 @@ public void close() { s.close(); } catch (IOException e) {} } + if (connectionStateListener != null) { + connectionStateListener.connectionStateChange(false); + } } @@ -86,6 +90,10 @@ private void connect() throws IOException, K4AccessException { socketReader.setName("Reader " + host + ":" + port); socketReader.setDaemon(true); socketReader.start(); + + if (connectionStateListener != null) { + connectionStateListener.connectionStateChange(true); + } } public KConnection(String h, int p, String userPassword, boolean useTLS) { @@ -95,6 +103,10 @@ public KConnection(String h, int p, String userPassword, boolean useTLS) { this.useTLS = useTLS; } + public void setConnectionStateListener(ConnectionStateListener connectionStateListener) { + this.connectionStateListener = connectionStateListener; + } + private final static byte[] HEADER = new byte[] {0,1,0,0}; private void send(K.KBase query) throws IOException { diff --git a/src/studio/kdb/Session.java b/src/studio/kdb/Session.java index c93567de..04acc4d8 100644 --- a/src/studio/kdb/Session.java +++ b/src/studio/kdb/Session.java @@ -1,5 +1,6 @@ package studio.kdb; +import kx.ConnectionStateListener; import kx.K4Exception; import kx.KConnection; import kx.ProgressCallback; @@ -16,7 +17,7 @@ import java.util.List; import java.util.Map; -public class Session { +public class Session implements ConnectionStateListener { private KConnection kConn; private long created; private final Server server; @@ -55,6 +56,13 @@ public void removeTab(EditorTab editor) { } } + @Override + public void connectionStateChange(boolean connected) { + for(EditorTab editor: editors) { + editor.setSessonConnection(connected); + } + } + private Session(Server server) { this.server = server; init(); @@ -65,6 +73,7 @@ private void init() { kConn = createConnection(server); if (kConn == null) throw new RuntimeException("Failure in the authentication plugin"); created = System.currentTimeMillis(); + kConn.setConnectionStateListener(this); } static void mock(SessionCreator sessionCreator) { diff --git a/src/studio/ui/EditorPane.java b/src/studio/ui/EditorPane.java index 3c61f321..866462d9 100644 --- a/src/studio/ui/EditorPane.java +++ b/src/studio/ui/EditorPane.java @@ -67,6 +67,10 @@ public void stopClock() { editorStatusBar.stopClock(); } + public void setSessionConnected(boolean connected) { + editorStatusBar.setSessionConnected(connected); + } + @Override public void mouseWheelMoved(MouseWheelEvent e) { if ((e.getModifiers() & StudioWindow.menuShortcutKeyMask) == 0) return; diff --git a/src/studio/ui/EditorTab.java b/src/studio/ui/EditorTab.java index f99cae7d..84c40c55 100644 --- a/src/studio/ui/EditorTab.java +++ b/src/studio/ui/EditorTab.java @@ -155,6 +155,10 @@ public StudioWindow getStudioWindow() { return studioWindow; } + public void setSessonConnection(boolean connected) { + editorPane.setSessionConnected(connected); + } + public void setStudioWindow(StudioWindow studioWindow) { this.studioWindow = studioWindow; } @@ -251,6 +255,7 @@ public void setServer(Server server) { } this.server = server; session = Session.newSession(this); + setSessonConnection(!session.isClosed()); getTextArea().setBackground(server.getBackgroundColor()); diff --git a/src/studio/ui/statusbar/EditorStatusBar.java b/src/studio/ui/statusbar/EditorStatusBar.java index 1eb35d85..fc5d3e19 100644 --- a/src/studio/ui/statusbar/EditorStatusBar.java +++ b/src/studio/ui/statusbar/EditorStatusBar.java @@ -7,19 +7,23 @@ public class EditorStatusBar extends StatusBar { - private final MinSizeLabel lblClock; + private final MinSizeLabel lblConnection; private final Timer timer; private long clock; + private boolean sessionConnected = false; + + private final static String CONNECTED = "Connected"; + private final static String DISCONNECTED = "Disconnected"; public EditorStatusBar() { - lblClock = new MinSizeLabel(""); - lblClock.setHorizontalAlignment(JLabel.CENTER); - lblClock.setMinimumWidth("1:00:00"); - addComponent(lblClock); - lblClock.setVisible(false); + lblConnection = new MinSizeLabel(""); + lblConnection.setHorizontalAlignment(JLabel.CENTER); + lblConnection.setMinimumWidth("1:00:00", CONNECTED, DISCONNECTED); + addComponent(lblConnection); timer = new Timer(500, this::timerClockAction); + refreshConnectedLabel(); } public void startClock() { @@ -29,7 +33,16 @@ public void startClock() { public void stopClock() { timer.stop(); - lblClock.setVisible(false); + refreshConnectedLabel(); + } + + public void setSessionConnected(boolean connected) { + sessionConnected = connected; + refreshConnectedLabel(); + } + + private void refreshConnectedLabel() { + lblConnection.setText(sessionConnected ? CONNECTED : DISCONNECTED); } private void timerClockAction(ActionEvent event) { @@ -42,8 +55,7 @@ private void timerClockAction(ActionEvent event) { long min = time % 60; long hour = time / 60; - lblClock.setText(String.format("%d:%02d:%02d",hour, min, sec)); - lblClock.setVisible(true); + lblConnection.setText(String.format("%d:%02d:%02d",hour, min, sec)); } }