aboutsummaryrefslogtreecommitdiffstatsci
path: root/CrowIDE/src/Workspace.cs
blob: ddbd2d6e88640689ffc8f773afd99d2e032cc75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// Solution.cs
//
//code taken in project https://sourceforge.net/projects/syncproj/
// no licence info was included, I took the liberty to modify it.
// Author:
//		tarmopikaro
//      2018 Jean-Philippe Bruyère
//MIT-licenced

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Build.Construction;

namespace Crow.Coding
{
	public class StyleItemContainer {
		public object Value;
		public string Name;
		public StyleItemContainer(string name, object _value){
			Name = name;
			Value = _value;
		}
	}
	public class StyleContainer : IValueChange {
		#region IValueChange implementation
		public event EventHandler<ValueChangeEventArgs> ValueChanged;
		public virtual void NotifyValueChanged(string MemberName, object _value)
		{
			ValueChanged.Raise(this, new ValueChangeEventArgs(MemberName, _value));
		}
		#endregion

		Style style;
		bool isExpanded;

		public string Name;
		public List<StyleItemContainer> Items;
		public bool IsExpanded
		{
			get { return isExpanded; }
			set
			{
				if (value == isExpanded)
					return;
				isExpanded = value;
				NotifyValueChanged ("IsExpanded", isExpanded);
			}
		}
		public StyleContainer(string name, Style _style){
			Name = name;
			style = _style;

			Items = new List<StyleItemContainer> ();
			foreach (string k in style.Keys) {
				Items.Add(new StyleItemContainer(k, style[k]));
			}
		}
	}


	/// <summary>
/// .sln loaded into class.
/// </summary>
	public class Workspace: IValueChange
	{
		#region IValueChange implementation
		public event EventHandler<ValueChangeEventArgs> ValueChanged;
		public virtual void NotifyValueChanged(string MemberName, object _value)
		{
			ValueChanged.Raise(this, new ValueChangeEventArgs(MemberName, _value));
		}
		#endregion

		SolutionFile solutionFile;

		ProjectItem selectedItem = null;
		object selectedItemElement = null;
		ObservableList<ProjectItem> openedItems = new ObservableList<ProjectItem>();
		ObservableList<GraphicObjectDesignContainer> toolboxItems;

		public Dictionary<string, Style> Styling;
		public Dictionary<string, string> DefaultTemplates;

		public List<Style> Styles { get { return Styling.Values.ToList(); }}
		public List<StyleContainer> StylingContainers;
		//TODO: check project dependencies if no startup proj

		public void ReloadStyling () {
			Styling = new Dictionary<string, Style> ();
			if (StartupProject != null)
				StartupProject.GetStyling ();
			StylingContainers = new List<StyleContainer> ();
			foreach (string k in Styling.Keys) 
				StylingContainers.Add (new StyleContainer (k, Styling [k]));
			foreach (ImlProjectItem pf in openedItems.OfType<ImlProjectItem>()) {
				pf.SignalEditorOfType<ImlVisualEditor> ();
			}
		}
		public string[] AvailaibleStyles {
			get { return Styling == null ? new string[] {} : Styling.Keys.ToArray();}
		}
		public void ReloadDefaultTemplates () {
			DefaultTemplates = new Dictionary<string, string>();
			if (StartupProject != null)
				StartupProject.GetDefaultTemplates ();
		}
		public void updateToolboxItems () {
			Type[] crowItems = AppDomain.CurrentDomain.GetAssemblies ()
				.SelectMany (t => t.GetTypes ())
				.Where (t => t.IsClass && !t.IsAbstract && t.IsPublic &&					
					t.Namespace == "Crow" && t.IsSubclassOf(typeof(Widget)) &&
					t.GetCustomAttribute<DesignIgnore>(false) == null).ToArray ();
			ToolboxItems = new ObservableList<GraphicObjectDesignContainer> ();
			foreach (Type ci in crowItems) {
				toolboxItems.Add(new GraphicObjectDesignContainer(ci));
			}
		}
		public bool GetProjectFileFromPath (string path, out ProjectFile pi){
			pi = null;
			return false;/* StartupProject == null ? false :
				StartupProject.TryGetProjectFileFromPath (path, out pi);*/
		}

		public ObservableList<ProjectItem> OpenedItems {
			get { return openedItems; }
			set {
				if (openedItems == value)
					return;
				openedItems = value;
				NotifyValueChanged ("OpenedItems", openedItems);
			}
		}

		public ObservableList<GraphicObjectDesignContainer> ToolboxItems {
			get { return toolboxItems; }
			set {
				if (toolboxItems == value)
					return;
				toolboxItems = value;
				NotifyValueChanged ("ToolboxItems", toolboxItems);
			}			
		}
		public ProjectItem SelectedItem {
			get { return selectedItem; }
			set {
				if (selectedItem == value)
					return;
				if (SelectedItem != null)
					SelectedItem.IsSelected = false;
				selectedItem = value;
				if (SelectedItem != null) 
					SelectedItem.IsSelected = true;

				//UserConfig.Set ("SelectedProjItems", SelectedItem?.AbsolutePath);

				NotifyValueChanged ("SelectedItem", selectedItem);
			}
		}
		public object SelectedItemElement {
			get { return selectedItemElement; }
			set {
				if (selectedItemElement == value)
					return;
				selectedItemElement = value;
				NotifyValueChanged ("SelectedItemElement", selectedItemElement);
			}
		}
		public string DisplayName {
			get { return name; }
		}
		/// <summary>
		/// Gets solution path
		/// </summary>
		public String SolutionFolder
		{
			get { return Path.GetDirectoryName (path); }
		}

//		public System.CodeDom.Compiler.CompilerErrorCollection CompilationErrors {
//			get {
//				System.CodeDom.Compiler.CompilerErrorCollection tmp = Projects.SelectMany<Project>
//					(p => p.CompilationResults.Errors);
//				return tmp;
//			}
//		}
		public List<System.CodeDom.Compiler.CompilerError> CompilerErrors {
			get {
				int errCount = 0;
				for (int i = 0; i < Projects.Count; i++) {
					if (Projects [i].CompilationResults != null)
						errCount += Projects [i].CompilationResults.Errors.Count;
				}
				System.CodeDom.Compiler.CompilerError[] tmp = new System.CodeDom.Compiler.CompilerError[errCount];

				int ptr = 0;
				for (int i = 0; i < Projects.Count; i++) {
					if (Projects [i].CompilationResults == null)
						continue;
					Projects [i].CompilationResults.Errors.CopyTo (tmp,ptr);
					ptr += Projects [i].CompilationResults.Errors.Count;
				}
				return new List<System.CodeDom.Compiler.CompilerError>(tmp);
			}
		}

		public void UpdateErrorList () {
			NotifyValueChanged ("CompilerErrors", CompilerErrors);
		}

		void saveOpenedItemsInUserConfig (){
			if (openedItems.Count == 0)
				UserConfig.Set ("OpenedItems", "");
			else
				UserConfig.Set ("OpenedItems", openedItems.Select(o => o.AbsolutePath).Aggregate((a,b)=>a + ";" + b));
		}
		public void ReopenItemsSavedInUserConfig () {
			string tmp = UserConfig.Get<string> ("OpenedItems");
			string sel = UserConfig.Get<string> ("SelectedProjItems");
			ProjectFile selItem = null;
			if (string.IsNullOrEmpty (tmp))
				return;
			foreach (string f in tmp.Split(';')) {
				foreach (Project p in Projects) {
					ProjectFile pi;
					/*if (p.TryGetProjectFileFromAbsolutePath (f, out pi)) {
						pi.Open ();
						if (pi.AbsolutePath == sel)
							selItem = pi;
						break;
					}*/
				}
			}
			if (selItem == null)
				return;
			selItem.IsSelected = true;
		}

		/*void onSelectedItemChanged (object sender, SelectionChangeEventArgs e){							
			SelectedItem = e.NewValue as ProjectItem;
			UserConfig.Set ("SelectedProjItems", SelectedItem?.AbsolutePath);
		}*/

		public void OpenItem (ProjectItem pi) {
			if (!openedItems.Contains (pi)) {
				openedItems.Add (pi);
				saveOpenedItemsInUserConfig ();
			}
		}
		public void CloseItem (ProjectItem pi) {			
			openedItems.Remove (pi);
			saveOpenedItemsInUserConfig ();
		}

		public void CloseSolution () {
			while (openedItems.Count > 0) {
				openedItems.Remove (openedItems [0]);
			}
			while (toolboxItems?.Count > 0) {
				toolboxItems.Remove (toolboxItems [0]);
			}
			NotifyValueChanged ("Projects", null);
		}
	    /// <summary>
	    /// Solution name
	    /// </summary>
	    String name;
		/// <summary>
		/// File path from where solution was loaded.
		/// </summary>
		[XmlIgnore]
		String path;

	    /// <summary>
	    /// Solution name for debugger.
	    /// </summary>
	    [ExcludeFromCodeCoverage]
	    public override string ToString()
	    {
	        return "Solution: " + name;
	    }
			
		#region Solution properties
	    double slnVer;                                      // 11.00 - vs2010, 12.00 - vs2015

	    /// <summary>
	    /// Visual studio version information used for generation, for example 2010, 2012, 2015 and so on...
	    /// </summary>
	    public int fileFormatVersion;

	    /// <summary>
	    /// null for old visual studio's
	    /// </summary>
	    public String VisualStudioVersion;
	    
	    /// <summary>
	    /// null for old visual studio's
	    /// </summary>
	    public String MinimumVisualStudioVersion;

	    /// <summary>
	    /// List of project included into solution.
	    /// </summary>
	    public List<Project> Projects = new List<Project>();

	    /// <summary>
	    /// List of configuration list, in form "{Configuration}|{Platform}", for example "Release|Win32".
	    /// To extract individual platforms / configuration list, use following functions.
	    /// </summary>
	    public List<String> configurations = new List<string>();

	    /// <summary>
	    /// Extracts platfroms supported by solution
	    /// </summary>
	    public IEnumerable<String> getPlatforms()
	    {
	        return configurations.Select(x => x.Split('|')[1]).Distinct();
	    }

	    /// <summary>
	    /// Extracts configuration names supported by solution
	    /// </summary>
	    public IEnumerable<String> getConfigurations()
	    {
	        return configurations.Select(x => x.Split('|')[0]).Distinct();
	    }
		#endregion

		public Configuration UserConfig;

		public Project StartupProject {
			get { return null; }// Projects?.FirstOrDefault (p => p.ProjectGuid == UserConfig.Get<string> ("StartupProject")); }
			set {
				if (value == StartupProject)
					return;
				if (value == null)
					UserConfig.Set ("StartupProject", "");
				else {
					UserConfig.Set ("StartupProject", value.ProjectGuid);
					value.NotifyValueChanged("IsStartupProject", true);
				}
				NotifyValueChanged ("StartupProject", StartupProject);
				ReloadStyling ();
				ReloadDefaultTemplates ();
			}
		}

		#region CTOR
		/// <summary>
		/// Creates new solution.
		/// </summary>
		public Workspace (string path)
		{
			this.path = path;
			solutionFile = SolutionFile.Parse (path);
			UserConfig = new Configuration (path + ".user");

			foreach (ProjectInSolution pis in solutionFile.ProjectsInOrder) {
				switch (pis.ProjectType) {
				case SolutionProjectType.Unknown:
					break;
				case SolutionProjectType.KnownToBeMSBuildFormat:
					Projects.Add (new Project (this, pis));
					break;
				case SolutionProjectType.SolutionFolder:
					break;
				case SolutionProjectType.WebProject:
					break;
				case SolutionProjectType.WebDeploymentProject:
					break;
				case SolutionProjectType.EtpSubProject:
					break;
				case SolutionProjectType.SharedProject:
					break;
				}
			}
		}
		#endregion
	}
}