Skip to content

Commit

Permalink
Add ProjectFinder class to the plugin code
Browse files Browse the repository at this point in the history
  • Loading branch information
mushketyk committed Mar 2, 2017
1 parent 438fcc6 commit 1e61d5d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
59 changes: 59 additions & 0 deletions VisualStudioPlugin/ProjectFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.Generic;

namespace QuantConnect.VisualStudioPlugin
{
/// <summary>
/// Stores associations between a list of files and a QuantConnect project
/// that is associated with them.
/// </summary>
class ProjectFinder
{
private IDictionary<HashSet<string>, string> _projectForFiles
= new Dictionary<HashSet<string>, string>(HashSet<string>.CreateSetComparer());

public ProjectFinder()
{

}

/// <summary>
/// Get a project name that is associated with the provided list of files
/// </summary>
/// <param name="files">List of files in a project</param>
/// <returns>A name of a project if a list of files is associated with a project, empty string otherwise</returns>
public string ProjectNameForFiles(List<string> files)
{
string projectName;
if (_projectForFiles.TryGetValue(new HashSet<string>(files), out projectName))
{
return projectName;
}
return "";
}

/// <summary>
/// Associate a project with a project name
/// </summary>
/// <param name="projectName">A project name to associate list of files with</param>
/// <param name="files">A list of files to associate with a project name</param>
public void AssociateProjectWith(string projectName, List<string> files)
{
_projectForFiles.Add(new HashSet<string>(files), projectName);
}
}
}
3 changes: 2 additions & 1 deletion VisualStudioPlugin/ProjectNameDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public partial class ProjectNameDialog : DialogWindow
private bool _projectNameProvided = false;
private string _selectedProjectName = null;

public ProjectNameDialog(List<string> projectNames)
public ProjectNameDialog(List<string> projectNames, string suggestedProjectName)
{
InitializeComponent();
projectNames.ForEach(p => projectNameBox.Items.Add(p));
projectNameBox.Text = suggestedProjectName;
}

private void SelectButton_Click(object sender, System.Windows.RoutedEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions VisualStudioPlugin/QuantConnect.VisualStudioPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="LogInCommand.cs" />
<Compile Include="LogInDialog.xaml.cs" />
<Compile Include="NotAuthenticatedException.cs" />
<Compile Include="ProjectFinder.cs" />
<Compile Include="ProjectNameDialog.xaml.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SolutionExplorerMenuCommand.cs" />
Expand Down
18 changes: 11 additions & 7 deletions VisualStudioPlugin/SolutionExplorerMenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal sealed class SolutionExplorerMenuCommand
private readonly Package package;
private DTE2 dte2;

private ProjectFinder _projectFinder = new ProjectFinder();

/// <summary>
/// Initializes a new instance of the <see cref="SolutionExplorerMenuCommand"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
Expand Down Expand Up @@ -115,9 +117,8 @@ public static void Initialize(Package package)

private void SendForBacktestingCallback(object sender, EventArgs e)
{
ExecuteOnProject((selectedProjectName) =>
ExecuteOnProject(sender, (selectedProjectName, files) =>
{
List<string> files = GetSelectedFiles(sender);
string message = string.Format(CultureInfo.CurrentCulture, "Send for backtesting to project {0}, files: {1}", selectedProjectName, string.Join(" ", files));
string title = "SendToBacktesting";

Expand All @@ -134,9 +135,8 @@ private void SendForBacktestingCallback(object sender, EventArgs e)

private void SaveToQuantConnectCallback(object sender, EventArgs e)
{
ExecuteOnProject((selectedProjectName) =>
ExecuteOnProject(sender, (selectedProjectName, files) =>
{
List<string> files = GetSelectedFiles(sender);
string message = string.Format(CultureInfo.CurrentCulture, "Save to project {0}, files {1}", selectedProjectName, string.Join(" ", files));
string title = "SaveToQuantConnect";

Expand All @@ -151,23 +151,27 @@ private void SaveToQuantConnectCallback(object sender, EventArgs e)
});
}

private void ExecuteOnProject(Action<string> onProject)
private void ExecuteOnProject(object sender, Action<string, List<string>> onProject)
{
if (LogInCommand.DoLogIn(this.ServiceProvider))
{
var api = AuthorizationManager.GetInstance().GetApi();
var projects = api.ListProjects().Projects;
var projectNames = projects.Select(p => p.Name).ToList();
var projectNameDialog = new ProjectNameDialog(projectNames);

List<string> files = GetSelectedFiles(sender);
var suggestedProjectName = _projectFinder.ProjectNameForFiles(files);
var projectNameDialog = new ProjectNameDialog(projectNames, suggestedProjectName);
projectNameDialog.HasMinimizeButton = false;
projectNameDialog.HasMaximizeButton = false;
projectNameDialog.ShowModal();

if (projectNameDialog.ProjectNameProvided())
{
var selectedProjectName = projectNameDialog.GetSelectedProjectName();
_projectFinder.AssociateProjectWith(selectedProjectName, files);

onProject.Invoke(selectedProjectName);
onProject.Invoke(selectedProjectName, files);
}
}
}
Expand Down

0 comments on commit 1e61d5d

Please sign in to comment.