7
// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
34
using MonoDevelop.Core;
35
using MonoDevelop.Core.Serialization;
37
namespace MonoDevelop.Projects
39
public class Workspace: WorkspaceItem, ICustomDataItem
41
WorkspaceItemCollection items;
43
public override void Dispose ()
46
foreach (WorkspaceItem it in Items)
50
public override ReadOnlyCollection<string> GetConfigurations ()
52
List<string> configs = new List<string> ();
53
foreach (WorkspaceItem it in Items) {
54
foreach (string conf in it.GetConfigurations ()) {
55
if (!configs.Contains (conf))
59
return configs.AsReadOnly ();
62
public WorkspaceItemCollection Items {
65
items = new WorkspaceItemCollection (this);
70
public override ReadOnlyCollection<T> GetAllItems<T> ()
72
List<T> list = new List<T> ();
73
GetAllItems<T> (list, this);
74
return list.AsReadOnly ();
77
void GetAllItems<T> (List<T> list, WorkspaceItem item) where T: WorkspaceItem
82
if (item is Workspace) {
83
foreach (WorkspaceItem citem in ((Workspace)item).Items)
84
GetAllItems<T> (list, citem);
88
public override SolutionEntityItem FindSolutionItem (string fileName)
90
foreach (WorkspaceItem it in Items) {
91
SolutionEntityItem si = it.FindSolutionItem (fileName);
99
public override Project GetProjectContainingFile (FilePath fileName)
101
foreach (WorkspaceItem it in Items) {
102
Project p = it.GetProjectContainingFile (fileName);
109
public override bool ContainsItem (IWorkspaceObject obj)
111
if (base.ContainsItem (obj))
114
foreach (WorkspaceItem it in Items) {
115
if (it.ContainsItem (obj))
122
public override ReadOnlyCollection<T> GetAllSolutionItems<T> ()
124
List<T> list = new List<T> ();
125
foreach (WorkspaceItem it in Items) {
126
list.AddRange (it.GetAllSolutionItems<T> ());
128
return list.AsReadOnly ();
131
public override void ConvertToFormat (FileFormat format, bool convertChildren)
133
base.ConvertToFormat (format, convertChildren);
134
if (convertChildren) {
135
foreach (WorkspaceItem it in Items)
136
it.ConvertToFormat (format, true);
141
internal protected override BuildResult OnRunTarget (IProgressMonitor monitor, string target, ConfigurationSelector configuration)
143
BuildResult result = null;
144
monitor.BeginTask (null, Items.Count);
146
foreach (WorkspaceItem it in Items) {
147
BuildResult res = it.RunTarget (monitor, target, configuration);
149
if (result == null) {
150
result = new BuildResult ();
151
result.BuildCount = 0;
163
protected internal override void OnExecute (IProgressMonitor monitor, ExecutionContext context, ConfigurationSelector configuration)
165
throw new NotImplementedException ();
168
protected internal override bool OnGetNeedsBuilding (ConfigurationSelector configuration)
170
foreach (WorkspaceItem it in Items) {
171
if (it.NeedsBuilding (configuration))
177
protected internal override void OnSetNeedsBuilding (bool val, ConfigurationSelector configuration)
179
foreach (WorkspaceItem it in Items)
180
it.SetNeedsBuilding (val, configuration);
183
public WorkspaceItem ReloadItem (IProgressMonitor monitor, WorkspaceItem item)
185
if (Items.IndexOf (item) == -1)
186
throw new InvalidOperationException ("Item '" + item.Name + "' does not belong to workspace '" + Name + "'");
190
WorkspaceItem newItem;
192
newItem = Services.ProjectService.ReadWorkspaceItem (monitor, item.FileName);
193
} catch (Exception ex) {
194
UnknownWorkspaceItem e = new UnknownWorkspaceItem ();
195
e.LoadError = ex.Message;
196
e.FileName = item.FileName;
200
// Replace in the file list
201
Items.Replace (item, newItem);
204
NotifyItemRemoved (new WorkspaceItemChangeEventArgs (item, true));
205
NotifyItemAdded (new WorkspaceItemChangeEventArgs (newItem, true));
211
public override List<FilePath> GetItemFiles (bool includeReferencedFiles)
213
List<FilePath> list = base.GetItemFiles (includeReferencedFiles);
214
if (includeReferencedFiles) {
215
foreach (WorkspaceItem it in Items)
216
list.AddRange (it.GetItemFiles (true));
222
internal void NotifyItemAdded (WorkspaceItemChangeEventArgs args)
225
OnConfigurationsChanged ();
228
internal void NotifyItemRemoved (WorkspaceItemChangeEventArgs args)
230
OnItemRemoved (args);
231
OnConfigurationsChanged ();
234
protected virtual void OnItemAdded (WorkspaceItemChangeEventArgs args)
236
if (ItemAdded != null)
237
ItemAdded (this, args);
238
OnDescendantItemAdded (args);
241
protected virtual void OnItemRemoved (WorkspaceItemChangeEventArgs args)
243
if (ItemRemoved != null)
244
ItemRemoved (this, args);
245
OnDescendantItemRemoved (args);
248
protected virtual void OnDescendantItemAdded (WorkspaceItemChangeEventArgs args)
250
if (DescendantItemAdded != null)
251
DescendantItemAdded (this, args);
252
if (ParentWorkspace != null)
253
ParentWorkspace.OnDescendantItemAdded (args);
256
protected virtual void OnDescendantItemRemoved (WorkspaceItemChangeEventArgs args)
258
if (DescendantItemRemoved != null)
259
DescendantItemRemoved (this, args);
260
if (ParentWorkspace != null)
261
ParentWorkspace.OnDescendantItemRemoved (args);
264
DataCollection ICustomDataItem.Serialize (ITypeSerializer handler)
266
DataCollection data = handler.Serialize (this);
267
DataItem items = new DataItem ();
268
items.Name = "Items";
269
items.UniqueNames = false;
270
string baseDir = Path.GetDirectoryName (handler.SerializationContext.BaseFile);
271
foreach (WorkspaceItem it in Items) {
272
DataValue item = new DataValue ("Item", FileService.AbsoluteToRelativePath (baseDir, it.FileName));
273
items.ItemData.Add (item);
279
void ICustomDataItem.Deserialize (ITypeSerializer handler, DataCollection data)
281
DataItem items = (DataItem) data.Extract ("Items");
282
handler.Deserialize (this, data);
283
IProgressMonitor monitor = handler.SerializationContext.ProgressMonitor;
285
monitor = new MonoDevelop.Core.ProgressMonitoring.NullProgressMonitor ();
287
string baseDir = Path.GetDirectoryName (handler.SerializationContext.BaseFile);
288
monitor.BeginTask (null, items.ItemData.Count);
290
foreach (DataValue item in items.ItemData) {
291
string file = Path.Combine (baseDir, item.Value);
292
WorkspaceItem it = Services.ProjectService.ReadWorkspaceItem (monitor, file);
303
public event EventHandler<WorkspaceItemChangeEventArgs> ItemAdded;
304
public event EventHandler<WorkspaceItemChangeEventArgs> ItemRemoved;
305
public event EventHandler<WorkspaceItemChangeEventArgs> DescendantItemAdded; // Fired if added in child workspaces
306
public event EventHandler<WorkspaceItemChangeEventArgs> DescendantItemRemoved; // Fired if added in child workspaces