Skip to content

Commit

Permalink
Merge pull request #126 from jirkapok/Fixed_Remove_Last_Connection
Browse files Browse the repository at this point in the history
Fixed remove last connection
  • Loading branch information
tdanner committed Mar 13, 2018
2 parents c98155a + c983f6a commit e26d80c
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 117 deletions.
16 changes: 11 additions & 5 deletions Src/SwqlStudio/ActivityMonitorTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

namespace SwqlStudio
{
public partial class ActivityMonitorTab : UserControl, IConnectionTab
public partial class ActivityMonitorTab : TabTemplate, IConnectionTab
{
private string subscriptionId;

public ConnectionInfo ConnectionInfo { get; set; }
private bool conneciotnSet = false;

public SubscriptionManager SubscriptionManager { get; set; }

public bool AllowsChangeConnection
public override bool AllowsChangeConnection => !this.conneciotnSet;

public override ConnectionInfo ConnectionInfo
{
get { return false; }
get { return base.ConnectionInfo; }
set
{
base.ConnectionInfo = value;
this.conneciotnSet = true;
}
}

public ActivityMonitorTab()
Expand Down
5 changes: 5 additions & 0 deletions Src/SwqlStudio/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,10 @@ public override int GetHashCode()
return hashCode;
}
}

public override string ToString()
{
return this.Title;
}
}
}
14 changes: 14 additions & 0 deletions Src/SwqlStudio/ConnectionsEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace SwqlStudio
{
internal class ConnectionsEventArgs : EventArgs
{
internal ConnectionInfo Connection { get; private set; }

public ConnectionsEventArgs(ConnectionInfo connection)
{
this.Connection = connection;
}
}
}
15 changes: 3 additions & 12 deletions Src/SwqlStudio/ConnectionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ internal class ConnectionsManager
{
private readonly IApplicationService applicationService;
private readonly ServerList serverList;
private readonly QueriesDockPanel dockPanel;

public ConnectionsManager(IApplicationService applicationService, ServerList serverList, QueriesDockPanel dockPanel)
public ConnectionsManager(IApplicationService applicationService, ServerList serverList)
{
this.applicationService = applicationService;
this.serverList = serverList;
this.dockPanel = dockPanel;
}

public void CreateConnection()
Expand Down Expand Up @@ -48,15 +46,8 @@ private ConnectionInfo ResolveExistingConnection(ConnectionInfo info)
return found;

info.Connect();
var provider = serverList.Add(info);

info.ConnectionClosed += (sender, args) =>
{
this.dockPanel.CloseAllFixedConnectionTabs(info);
serverList.Remove(info);
};

this.dockPanel.AddServer(provider, info);
info.ConnectionClosed += (sender, args) => serverList.Remove(info);
serverList.Add(info);
return info;
}

Expand Down
10 changes: 4 additions & 6 deletions Src/SwqlStudio/CrudTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SwqlStudio
{
public partial class CrudTab : UserControl, IConnectionTab
public partial class CrudTab : TabTemplate, IConnectionTab
{
private const int ControlsMargin = 3;
private readonly CrudOperation _operation;
Expand All @@ -31,11 +31,6 @@ public Entity Entity
}
}

/// <inheritdoc />
public ConnectionInfo ConnectionInfo { get; set; }

public bool AllowsChangeConnection => true;

public event EventHandler CloseItself;

private void CreateSubComponents()
Expand Down Expand Up @@ -90,6 +85,9 @@ private void CreateSubComponents()

private void OnCommit(object sender, EventArgs e)
{
if (ConnectionInfo == null)
return;

var propertyBag = CreatePropertyBag();

try
Expand Down
7 changes: 4 additions & 3 deletions Src/SwqlStudio/InvokeVerbTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

namespace SwqlStudio
{
public partial class InvokeVerbTab : UserControl, IConnectionTab
public partial class InvokeVerbTab : TabTemplate, IConnectionTab
{
public InvokeVerbTab()
{
InitializeComponent();
}

public ConnectionInfo ConnectionInfo { get; set; }
public bool AllowsChangeConnection => true;
private int locationX = 0;
private int locationY = 0;

Expand Down Expand Up @@ -127,6 +125,9 @@ private void FillTextBox()

private void Invoke_Click(object sender, HtmlElementEventArgs e)
{
if (ConnectionInfo == null)
return;

bool argumentParsingFailed = false;

XmlElement[] parameters = verb.Arguments.Select(argument =>
Expand Down
68 changes: 40 additions & 28 deletions Src/SwqlStudio/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Windows.Forms;
using SolarWinds.InformationService.Contract2;
using SolarWinds.InformationService.InformationServiceClient;
using SwqlStudio.Metadata;
using SwqlStudio.Properties;
using SwqlStudio.Subscriptions;
using SwqlStudio.Utils;
using WeifenLuo.WinFormsUI.Docking;

namespace SwqlStudio
{
Expand All @@ -23,6 +20,7 @@ internal partial class MainForm : Form, IApplicationService
{
private static readonly SolarWinds.Logging.Log log = new SolarWinds.Logging.Log();
private ServerList serverList;
private readonly BindingList<ConnectionInfo> connectionsDataSource = new BindingList<ConnectionInfo>();
private ConnectionsManager connectionsManager;

public PropertyBag QueryParameters
Expand All @@ -36,6 +34,7 @@ public PropertyBag QueryParameters
public ConnectionInfo SelectedConnection
{
get { return this.connectionsCombobox.SelectedItem as ConnectionInfo; }
set { this.connectionsCombobox.SelectedItem = value; }
}

public MainForm()
Expand All @@ -48,29 +47,36 @@ public MainForm()
startTimer.Enabled = true;

SubscriptionManager = new SubscriptionManager();
AssignConnectionsDataSource();
}

private void InitializeDockPanel()
{
var connectionsDropDown = this.connectionsCombobox.ComboBox;
connectionsDropDown.DisplayMember = "Title";
this.filesDock.SetObjectExplorerImageList(this.ObjectExplorerImageList);
this.serverList = new ServerList();
this.serverList.ConnectionsChanged += ServerListOnConnectionsChanged;
this.connectionsManager = new ConnectionsManager(this, this.serverList, this.filesDock);
this.serverList.ConnectionAdded += ServerListOnConnectionAdded;
this.serverList.ConnectionRemoved += ServerListOnConnectionRemoved;
this.connectionsManager = new ConnectionsManager(this, this.serverList);
var tabsFactory = new TabsFactory(this.filesDock, this, this.serverList, this.connectionsManager);
this.filesDock.SetAplicationService(tabsFactory);
this.filesDock.ActiveContentChanged += FilesDock_ActiveContentChanged;
}

private void AssignConnectionsDataSource()
{
var connectionsDropDown = this.connectionsCombobox.ComboBox;
connectionsDropDown.DisplayMember = "Title";
connectionsDropDown.DataSource = this.connectionsDataSource;
}

private void FilesDock_ActiveContentChanged(object sender, EventArgs e)
{
this.RefreshSelectedConnections();

IConnectionTab activeConnectionTab = this.filesDock.ActiveConnectionTab;
if (activeConnectionTab != null)
{
this.connectionsCombobox.SelectedItem = activeConnectionTab.ConnectionInfo;
this.SelectedConnection = activeConnectionTab.ConnectionInfo;
}
}

Expand All @@ -80,17 +86,26 @@ public void RefreshSelectedConnections()
this.connectionsCombobox.Enabled = activeConnectionTab == null || activeConnectionTab.AllowsChangeConnection;
}

private void ServerListOnConnectionsChanged(object sender, EventArgs eventArgs)
private void ServerListOnConnectionAdded(object sender, ConnectionsEventArgs e)
{
var connectionsDropDown = this.connectionsCombobox.ComboBox;
var lastSelected = this.connectionsCombobox.SelectedItem;
List<ConnectionInfo> serverListConnections = this.serverList.Connections;
connectionsDropDown.DataSource = new BindingList<ConnectionInfo>(serverListConnections);

if(lastSelected == null && serverListConnections.Any())
lastSelected = serverListConnections.First();

this.connectionsCombobox.SelectedItem = lastSelected;
ConnectionInfo addedConnection = e.Connection;
this.connectionsDataSource.Add(addedConnection);
this.serverList.TryGetProvider(addedConnection, out IMetadataProvider provider);
this.filesDock.AddServer(provider, addedConnection);
this.SelectedConnection = addedConnection;

if (this.connectionsDataSource.Count == 1)
this.filesDock.ReplaceConnection(null, addedConnection);
}

private void ServerListOnConnectionRemoved(object sender, ConnectionsEventArgs e)
{
this.connectionsDataSource.Remove(e.Connection);

if(connectionsDataSource.Any())
this.SelectedConnection = connectionsDataSource.First();

this.filesDock.CloseServer(e.Connection, this.SelectedConnection);
}

private void startTimer_Tick(object sender, EventArgs e)
Expand Down Expand Up @@ -246,7 +261,7 @@ private void TextEditorDragDrop(object sender, DragEventArgs e)
private void TextEditor_FormClosing(object sender, FormClosingEventArgs e)
{
// Ask user to save changes
foreach (var editor in this.filesDock.AllEditors)
foreach (var editor in this.filesDock.QueryTabs)
{
if (editor.Modified && Settings.Default.PromptToSaveOnClose)
{
Expand Down Expand Up @@ -424,20 +439,14 @@ private void newConnectionButton_Click(object sender, EventArgs e)

private void disconnectToolButton_Click(object sender, EventArgs e)
{
var connection = this.connectionsCombobox.SelectedItem as ConnectionInfo;
if (connection != null)
{
this.filesDock.CloseServer(connection);
}
this.SelectedConnection?.Close();
}

private void refreshToolButton_Click(object sender, EventArgs e)
{
var connection = this.connectionsCombobox.SelectedItem as ConnectionInfo;
var connection = this.SelectedConnection;
if (connection != null)
{
this.filesDock.RefreshServer(connection);
}
}

private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
Expand All @@ -462,7 +471,10 @@ private void getSwisDataPowerShellToolStripMenuItem_Click(object sender, EventAr

private void CopyQueryAs(Func<string, ConnectionInfo, string> formatter)
{
var connection = filesDock.ActiveConnectionTab.ConnectionInfo;
var connection = filesDock.ActiveConnectionTab?.ConnectionInfo;
if (connection == null)
return;

var query = filesDock.ActiveQueryTab.QueryText;
string command = formatter(query, connection);
Clipboard.SetText(command);
Expand Down
45 changes: 24 additions & 21 deletions Src/SwqlStudio/ObjectExplorer/ObjectExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ public void AddServer(IMetadataProvider provider, ConnectionInfo connection)
serverContextMenu.Items.Add("Activity Monitor", null, (s, e) => OpenActivityMonitor(_contextMenuNode));

serverContextMenu.Items.Add("Generate C# Code...", null, (s, e) => GenerateCSharpCode(_contextMenuNode));
var closeMenuItem = new ToolStripMenuItem("Disconnect", Properties.Resources.Disconnect_16x, (s, e) => CloseServer(_contextMenuNode));
var closeMenuItem = new ToolStripMenuItem("Disconnect", Properties.Resources.Disconnect_16x,
(s, e) => CloseServer(_contextMenuNode));
serverContextMenu.Items.Add(closeMenuItem);

_serverContextMenuItems.Add(connection.Title, serverContextMenu);
Expand All @@ -574,27 +575,17 @@ public void AddServer(IMetadataProvider provider, ConnectionInfo connection)
_treeData.SelectedNode = existingNodes[0];
UpdateDrawnNodesSelection();
}

connection.ConnectionClosed += (o, e) =>
{
_tableContextMenuItems.Remove(connection.Title);
tableContextMenuWithoutCrud.Dispose();
tableContextMenuWithoutCrud = null;
_tableCrudContextMenuItems.Remove(connection.Title);
tableContextMenuWithCrud.Dispose();
tableContextMenuWithCrud = null;
_serverContextMenuItems.Remove(connection.Title);
serverContextMenu.Dispose();
serverContextMenu = null;
};
}

internal void CloseServer(ConnectionInfo connection)
{
TreeNodeWithConnectionInfo serverNode = FindServerNodeByConnection(connection);
CloseServer(serverNode);
if (serverNode != null)
{
RemoveFromMenus(serverNode.Connection);
_treeData.Nodes.Remove(_treeBindings.FindDataNode(serverNode));
_tree.Nodes.Remove(serverNode);
}
}

private TreeNodeWithConnectionInfo FindServerNodeByConnection(ConnectionInfo connection)
Expand All @@ -607,11 +598,23 @@ private void CloseServer(TreeNode contextMenuNode)
{
var node = contextMenuNode as TreeNodeWithConnectionInfo;
if (node != null)
{
node.Connection.Close();
_treeData.Nodes.Remove(_treeBindings.FindDataNode(node));
_tree.Nodes.Remove(contextMenuNode);
}
}

private void RemoveFromMenus(ConnectionInfo connection)
{
string title = connection.Title;
var tableContextMenuWithoutCrud = _tableContextMenuItems[title];
_tableContextMenuItems.Remove(title);
tableContextMenuWithoutCrud.Dispose();

var tableContextMenuWithCrud = _tableCrudContextMenuItems[title];
_tableCrudContextMenuItems.Remove(title);
tableContextMenuWithCrud.Dispose();

var serverContextMenu = _serverContextMenuItems[title];
_serverContextMenuItems.Remove(title);
serverContextMenu.Dispose();
}

private static TreeNode CreateDatabaseNode(IMetadataProvider provider, ConnectionInfo connection)
Expand Down
Loading

0 comments on commit e26d80c

Please sign in to comment.