diff --git a/src/studio/kdb/Config.java b/src/studio/kdb/Config.java index bb56ca68..713f9269 100755 --- a/src/studio/kdb/Config.java +++ b/src/studio/kdb/Config.java @@ -632,6 +632,31 @@ public void removeServer(Server server) { saveAllServers(); } + public void replaceServer(Server oldServer, Server newServer) { + ServerTreeNode oldFolder = serverTree.findPath(oldServer.getFolderPath(), false); + if (oldFolder == null) { + // ?? Is it possible + return; + } + ServerTreeNode newFolder = serverTree.findPath(newServer.getFolderPath(), true); + + String name = oldServer.getFullName(); + serverNames.remove(name); + servers.remove(name); + int index = oldFolder.remove(oldServer); + if (index == -1 || oldFolder!=newFolder) { + index = newFolder.getChildCount(); + } + + try { + addServerInternal(newServer); + newFolder.add(index, newServer); + } catch (IllegalArgumentException e) { + log.error("Failed to add server", e); + addServer(oldServer); + } + } + private void purgeAll() { servers.clear(); serverNames.clear(); diff --git a/src/studio/kdb/ServerTreeNode.java b/src/studio/kdb/ServerTreeNode.java index bfcec480..277148b9 100644 --- a/src/studio/kdb/ServerTreeNode.java +++ b/src/studio/kdb/ServerTreeNode.java @@ -79,7 +79,7 @@ public ServerTreeNode add(int index, String folder) { } // Remove direct child - public boolean remove(Server server) { + public int remove(Server server) { if (!isFolder()) { throw new IllegalArgumentException("This is not folder"); } @@ -89,10 +89,10 @@ public boolean remove(Server server) { if (treeNode.isFolder()) continue; if (treeNode.getServer().equals(server)) { remove(index); - return true; + return index; } } - return false; + return -1; } public ServerTreeNode getChild(String folder) { diff --git a/src/studio/ui/StudioWindow.java b/src/studio/ui/StudioWindow.java index 187d1c3e..728d8aca 100755 --- a/src/studio/ui/StudioWindow.java +++ b/src/studio/ui/StudioWindow.java @@ -556,10 +556,8 @@ private void initActions() { if (stopAction.isEnabled()) stopAction.actionPerformed(e); - CONFIG.removeServer(editor.getServer()); - Server newServer = f.getServer(); - CONFIG.addServer(newServer); + CONFIG.replaceServer(editor.getServer(), newServer); setServer(newServer); refreshAll(); }