~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageManagementConsoleViewModel.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Collections.ObjectModel;
 
7
using System.Collections.Specialized;
 
8
using System.Linq;
 
9
using System.Windows.Input;
 
10
 
 
11
using ICSharpCode.AvalonEdit;
 
12
using ICSharpCode.Scripting;
 
13
using ICSharpCode.SharpDevelop.Project;
 
14
using NuGet;
 
15
 
 
16
namespace ICSharpCode.PackageManagement.Scripting
 
17
{
 
18
        public class PackageManagementConsoleViewModel : ViewModelBase<PackageManagementConsoleViewModel>
 
19
        {
 
20
                RegisteredPackageSources registeredPackageSources;
 
21
                IPackageManagementProjectService projectService;
 
22
                IPackageManagementConsoleHost consoleHost;
 
23
                
 
24
                DelegateCommand clearConsoleCommand;
 
25
                
 
26
                ObservableCollection<PackageSourceViewModel> packageSources = new ObservableCollection<PackageSourceViewModel>();
 
27
                PackageSourceViewModel activePackageSource;
 
28
                
 
29
                ObservableCollection<IProject> projects = new ObservableCollection<IProject>();
 
30
                
 
31
                PackageManagementConsole packageManagementConsole;
 
32
                
 
33
                public PackageManagementConsoleViewModel(
 
34
                        RegisteredPackageSources registeredPackageSources,
 
35
                        IPackageManagementProjectService projectService,
 
36
                        IPackageManagementConsoleHost consoleHost)
 
37
                {
 
38
                        this.registeredPackageSources = registeredPackageSources;
 
39
                        this.projectService = projectService;
 
40
                        this.consoleHost = consoleHost;
 
41
                        
 
42
                        Init();
 
43
                }
 
44
                
 
45
                void Init()
 
46
                {
 
47
                        CreateCommands();
 
48
                        UpdatePackageSourceViewModels();
 
49
                        ReceiveNotificationsWhenPackageSourcesUpdated();
 
50
                        AddProjects();
 
51
                        ReceiveNotificationsWhenSolutionIsUpdated();
 
52
                        InitConsoleHost();
 
53
                }
 
54
                
 
55
                void InitConsoleHost()
 
56
                {
 
57
                        packageManagementConsole = CreateConsole();
 
58
                        consoleHost.ScriptingConsole = packageManagementConsole;
 
59
                        consoleHost.Run();
 
60
                }
 
61
                
 
62
                protected virtual PackageManagementConsole CreateConsole()
 
63
                {
 
64
                        return new PackageManagementConsole();
 
65
                }
 
66
                
 
67
                void CreateCommands()
 
68
                {
 
69
                        clearConsoleCommand = new DelegateCommand(param => ClearConsole());
 
70
                }
 
71
                
 
72
                public ICommand ClearConsoleCommand {
 
73
                        get { return clearConsoleCommand; }
 
74
                }
 
75
                
 
76
                public void ClearConsole()
 
77
                {
 
78
                        consoleHost.Clear();
 
79
                        consoleHost.WritePrompt();
 
80
                }
 
81
                
 
82
                void UpdatePackageSourceViewModels()
 
83
                {
 
84
                        packageSources.Clear();
 
85
                        AddRegisteredPackageSourceViewModels();
 
86
                        AddAggregatePackageSourceViewModelIfMoreThanOnePackageSourceViewModelAdded();
 
87
                        SelectActivePackageSource();
 
88
                }
 
89
 
 
90
                void AddRegisteredPackageSourceViewModels()
 
91
                {
 
92
                        foreach (PackageSource packageSource in registeredPackageSources) {
 
93
                                AddPackageSourceViewModel(packageSource);
 
94
                        }
 
95
                }
 
96
                
 
97
                void AddPackageSourceViewModel(PackageSource packageSource)
 
98
                {
 
99
                        var viewModel = new PackageSourceViewModel(packageSource);
 
100
                        packageSources.Add(viewModel);
 
101
                }
 
102
                
 
103
                void AddAggregatePackageSourceViewModelIfMoreThanOnePackageSourceViewModelAdded()
 
104
                {
 
105
                        if (packageSources.Count > 1) {
 
106
                                AddPackageSourceViewModel(RegisteredPackageSourceSettings.AggregatePackageSource);
 
107
                        }
 
108
                }
 
109
                
 
110
                void SelectActivePackageSource()
 
111
                {
 
112
                        PackageSource activePackageSource = consoleHost.ActivePackageSource;
 
113
                        foreach (PackageSourceViewModel packageSourceViewModel in packageSources) {
 
114
                                if (packageSourceViewModel.GetPackageSource().Equals(activePackageSource)) {
 
115
                                        ActivePackageSource = packageSourceViewModel;
 
116
                                        return;
 
117
                                }
 
118
                        }
 
119
                        
 
120
                        SelectFirstActivePackageSource();
 
121
                }
 
122
                
 
123
                void SelectFirstActivePackageSource()
 
124
                {
 
125
                        if (packageSources.Count > 0) {
 
126
                                ActivePackageSource = packageSources[0];
 
127
                        }
 
128
                }
 
129
                
 
130
                void ReceiveNotificationsWhenPackageSourcesUpdated()
 
131
                {
 
132
                        registeredPackageSources.CollectionChanged += PackageSourcesChanged;
 
133
                }
 
134
 
 
135
                void PackageSourcesChanged(object sender, NotifyCollectionChangedEventArgs e)
 
136
                {
 
137
                        UpdatePackageSourceViewModels();
 
138
                }
 
139
                
 
140
                void AddProjects()
 
141
                {
 
142
                        Solution solution = projectService.OpenSolution;
 
143
                        if (solution != null) {
 
144
                                AddProjects(solution);
 
145
                        }
 
146
                        UpdateDefaultProject();
 
147
                }
 
148
 
 
149
                void UpdateDefaultProject()
 
150
                {
 
151
                        DefaultProject = projects.FirstOrDefault();
 
152
                }
 
153
                
 
154
                void AddProjects(Solution solution)
 
155
                {
 
156
                        foreach (IProject project in solution.Projects) {
 
157
                                projects.Add(project);
 
158
                        }
 
159
                }
 
160
                
 
161
                void ReceiveNotificationsWhenSolutionIsUpdated()
 
162
                {
 
163
                        projectService.ProjectAdded += ProjectAdded;
 
164
                        projectService.SolutionClosed += SolutionClosed;
 
165
                        projectService.SolutionLoaded += SolutionLoaded;
 
166
                        projectService.SolutionFolderRemoved += SolutionFolderRemoved;
 
167
                }
 
168
                
 
169
                void ProjectAdded(object sender, ProjectEventArgs e)
 
170
                {
 
171
                        projects.Add(e.Project);
 
172
                        UpdateDefaultProject();
 
173
                }
 
174
                
 
175
                void SolutionClosed(object sender, EventArgs e)
 
176
                {
 
177
                        projects.Clear();
 
178
                        DefaultProject = null;
 
179
                }
 
180
                
 
181
                void SolutionLoaded(object sender, SolutionEventArgs e)
 
182
                {
 
183
                        AddProjects(e.Solution);
 
184
                        UpdateDefaultProject();
 
185
                }
 
186
                
 
187
                void SolutionFolderRemoved(object sender, SolutionFolderEventArgs e)
 
188
                {
 
189
                        IProject project = e.SolutionFolder as IProject;
 
190
                        projects.Remove(project);
 
191
                        UpdateDefaultProject();
 
192
                }
 
193
                
 
194
                public ObservableCollection<PackageSourceViewModel> PackageSources {
 
195
                        get { return packageSources; }
 
196
                }
 
197
                
 
198
                public PackageSourceViewModel ActivePackageSource {
 
199
                        get { return activePackageSource; }
 
200
                        set {
 
201
                                activePackageSource = value;
 
202
                                consoleHost.ActivePackageSource = GetPackageSourceForViewModel(activePackageSource);
 
203
                                OnPropertyChanged(viewModel => viewModel.ActivePackageSource);
 
204
                        }
 
205
                }
 
206
                
 
207
                PackageSource GetPackageSourceForViewModel(PackageSourceViewModel activePackageSource)
 
208
                {
 
209
                        if (activePackageSource != null) {
 
210
                                return activePackageSource.GetPackageSource();
 
211
                        }
 
212
                        return null;
 
213
                }
 
214
                
 
215
                public ObservableCollection<IProject> Projects {
 
216
                        get { return projects; }
 
217
                }
 
218
                
 
219
                public IProject DefaultProject {
 
220
                        get { return consoleHost.DefaultProject; }
 
221
                        set {
 
222
                                consoleHost.DefaultProject = value;
 
223
                                OnPropertyChanged(viewModel => viewModel.DefaultProject);
 
224
                        }
 
225
                }
 
226
                
 
227
                public TextEditor TextEditor {
 
228
                        get { return packageManagementConsole.TextEditor; }
 
229
                }
 
230
                
 
231
                public bool ShutdownConsole()
 
232
                {
 
233
                        consoleHost.ShutdownConsole();
 
234
                        consoleHost.Dispose();
 
235
                        return !consoleHost.IsRunning;
 
236
                }
 
237
        }
 
238
}