Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.SimulationCore.Dsl/DslPackage/GeneratedCode/DocData.cs @ 10525

Last change on this file since 10525 was 10525, checked in by abeham, 10 years ago

#1610: added visual model

File size: 21.1 KB
Line 
1//------------------------------------------------------------------------------
2// <auto-generated>
3//     This code was generated by a tool.
4//
5//     Changes to this file may cause incorrect behavior and will be lost if
6//     the code is regenerated.
7// </auto-generated>
8//------------------------------------------------------------------------------
9
10using DslShell = global::Microsoft.VisualStudio.Modeling.Shell;
11using DslModeling = global::Microsoft.VisualStudio.Modeling;
12using DslValidation = global::Microsoft.VisualStudio.Modeling.Validation;
13using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
14using VSShellInterop = global::Microsoft.VisualStudio.Shell.Interop;
15using global::System.Linq;
16
17namespace HeuristicLab.SimulationCore
18{
19  /// <summary>
20  /// Double-derived class to allow easier code customization.
21  /// </summary>
22  internal partial class HLSimDocData : HLSimDocDataBase
23  {
24    /// <summary>
25    /// Constructs a new HLSimDocData.
26    /// </summary>
27    public HLSimDocData(global::System.IServiceProvider serviceProvider, global::System.Guid editorFactoryId)
28      : base(serviceProvider, editorFactoryId)
29    {
30    }
31  }
32
33  /// <summary>
34  /// Class which represents a HLSim document in memory.
35  /// </summary>
36  internal abstract partial class HLSimDocDataBase : DslShell::ModelingDocData
37  {
38    /// <summary>
39    /// Document lock holder registered for the subordinate .diagram file.
40    /// </summary>
41    protected DslShell::SubordinateDocumentLockHolder diagramDocumentLockHolder;
42
43    /// <summary>
44    /// Constructs a new HLSimDocDataBase.
45    /// </summary>
46    protected HLSimDocDataBase(global::System.IServiceProvider serviceProvider, global::System.Guid editorFactoryId) : base(serviceProvider, editorFactoryId)
47    {
48    }
49
50    /// <summary>
51    /// Returns a list of file format specifiers for the Save dialog box.
52    /// </summary>
53    protected override string FormatList
54    {
55      get
56      {
57        return global::HeuristicLab.SimulationCore.HLSimDomainModel.SingletonResourceManager.GetString("FormatList");
58      }
59    }
60
61    #region Composition container
62
63    /// <summary>
64    /// Create and return a new store
65    /// </summary>
66    /// <remarks>
67    /// Override the default behaviour to put the doc data's composition container into the store's property bag.
68    /// By default, the runtime will add the CompositionService from the DslShell ModelingCompositionContainer
69    /// if the property has not already been set.
70    /// </remarks>
71    protected override DslModeling::Store CreateStore()
72    {
73      DslModeling::Store store = base.CreateStore();
74
75      global::System.ComponentModel.Composition.ICompositionService compositionService = this.CompositionService;
76      // Add the composition container to the property bag even if it is null. This will prevent the runtime from
77      // adding the shared ModelingCompositionContainer.CompositionService.
78      store.PropertyBag.Add(DslModeling::ExtensionEnablement.ExtensionEnablementConstants.ContainerId, compositionService);
79
80      return store;
81    }
82
83    /// <summary>
84    /// Initialize the doc data
85    /// </summary>
86    public override void Initialize(DslModeling::Store sharedStore)
87    {
88      this.InitializeComposition();
89      base.Initialize(sharedStore);
90    }
91       
92    /// <summary>
93    /// The MEF composition container used by the doc data.
94    /// </summary>
95    /// <remarks>Can be null. Returns the shared modeling composition service by default.</remarks>
96    protected virtual global::System.ComponentModel.Composition.ICompositionService CompositionService
97    {
98      get
99      {
100        return DslShell::ModelingCompositionContainer.CompositionService;
101      }
102    }
103
104    /// <summary>
105    /// The MEF Export Provider used by the doc data.
106    /// </summary>
107    /// <remarks>Can be null. Returns the shared modeling export provider by default</remarks>
108    protected virtual global::System.ComponentModel.Composition.Hosting.ExportProvider ExportProvider
109    {
110      get
111      {
112        return DslShell::ModelingCompositionContainer.ExportProvider;
113      }   
114    }
115
116    /// <summary>
117    /// Satisfy Imports in DocData object
118    /// </summary>   
119    protected virtual void InitializeComposition()
120    {
121      global::System.ComponentModel.Composition.ICompositionService compositionService = this.CompositionService;
122      if (compositionService != null)
123      {   
124        try
125        {
126          compositionService.SatisfyImportsOnce(global::System.ComponentModel.Composition.AttributedModelServices.CreatePart(this));
127        }
128        catch (global::System.Exception ex)
129        {
130          // Handle binding failures
131          if (!HandleBindingFailure(ex))
132          {
133            throw;
134          }
135        }
136      }
137    }
138
139    /// <summary>
140    /// Handles exceptions that occurred during binding.
141    /// </summary>
142    /// <param name="exception">The exception that occured</param>
143    /// <returns>A flag indicating whether the exception was handled or not.</returns>
144    /// <remarks>If the exception is not handled (i.e. the method returns false), it will be re-thrown
145    /// by the calling method.
146    /// This method will be called for all types of exception.</remarks>
147    protected virtual bool HandleBindingFailure(global::System.Exception exception)
148    {
149      // Log and suppress all binding failure exceptions.
150      string errorMessage = string.Format(global::System.Globalization.CultureInfo.CurrentCulture,
151        global::HeuristicLab.SimulationCore.HLSimDomainModel.SingletonResourceManager.GetString("BindingErrorOccurred"),
152        exception.ToString());
153     
154      this.AddErrorListItem(new DslShell::SimpleErrorListItem(errorMessage, this.FileName, global::Microsoft.VisualStudio.Shell.TaskPriority.Normal, global::Microsoft.VisualStudio.Shell.TaskErrorCategory.Warning));
155      return true;
156    }
157
158    #endregion // CompositionContainer
159
160    #region SerializerLocator
161
162    /// <summary>
163    /// Resolves the xml namespaces to domain model types
164    /// </summary>
165    private DslModeling::ISerializerLocator serializerLocator;
166
167    /// <summary>
168    /// The domain model resolver used during serialization.
169    /// </summary>
170    protected DslModeling::ISerializerLocator SerializerLocator
171    {
172      get
173      {
174        if (this.serializerLocator == null)
175        {
176          this.serializerLocator = CreateSerializerLocator();
177        }
178        return this.serializerLocator;
179      }
180    }
181
182    /// <summary>
183    /// Factory method to create a SerializerLocator.
184    /// </summary>
185    protected virtual DslModeling::ISerializerLocator CreateSerializerLocator()
186    {
187      // If we don't have a MEF ExportProvider, we won't be able to resolve
188      // any namespaces.
189      if (this.ExportProvider == null)
190      {
191        return null;
192      }
193
194      return new DslModeling::StandardSerializerLocator(this.ExportProvider);
195    }
196
197    #endregion // SerializerLocator
198   
199    #region ExtensionLocator
200
201    /// <summary>
202    /// Used to locate domain model extensions
203    /// </summary>
204    private DslModeling::IExtensionLocator extensionLocator;
205
206    /// <summary>
207    /// The locator used to find domain model extensions.
208    /// </summary>
209    protected DslModeling::IExtensionLocator ExtensionLocator
210    {
211      get
212      {
213        if (this.extensionLocator == null)
214        {
215          this.extensionLocator = CreateExtensionLocator();
216        }
217        return this.extensionLocator;
218      }
219    }
220
221    /// <summary>
222    /// Factory method to create an ExtensionLocator.
223    /// </summary>
224    protected virtual DslModeling::IExtensionLocator CreateExtensionLocator()
225    {
226      // If we don't have an MEF ExportProvider, we won't be able to locate
227      // any extensions
228      if (this.ExportProvider == null)
229      {
230        return null;
231      }
232
233      return new DslModeling::StandardExtensionLocator(this.ExportProvider);
234    }
235
236    #endregion // ExtensionLocator
237
238
239    /// <summary>
240    /// When the doc data is closed, make sure we reset the valiation messages
241    /// (if there's any) from the ErrorList window.
242    /// </summary>
243    /// <param name="disposing"></param>
244    protected override void Dispose(bool disposing)
245    {
246      try
247      {
248        if (this.diagramDocumentLockHolder != null)
249        {
250          this.diagramDocumentLockHolder.Dispose();
251          this.diagramDocumentLockHolder = null;
252        }
253        this.diagramPartitionId = global::System.Guid.Empty;
254      }
255      finally
256      {
257        base.Dispose(disposing);
258      }
259    }
260
261    /// <summary>
262    /// Returns a collection of domain models to load into the store.
263    /// </summary>
264    /// <remarks>The default implementation includes any extension domain models returned by the call to GetExtensionDomainModels().</remarks>
265    protected override global::System.Collections.Generic.IList<global::System.Type> GetDomainModels()
266    {
267      global::System.Collections.Generic.List<global::System.Type> allTypes = new System.Collections.Generic.List<System.Type>();
268
269      // In the type of our base domain model
270      allTypes.Add(typeof(global::HeuristicLab.SimulationCore.HLSimDomainModel));
271
272      // Add in any extension domain models
273      global::System.Collections.Generic.IEnumerable<global::System.Type> extensionTypes = this.GetExtensionDomainModels();
274      if (extensionTypes != null && extensionTypes.Count() > 0)
275      {
276        allTypes.AddRange(extensionTypes);
277      }
278
279      return allTypes;
280    }
281
282    /// <summary>
283    /// Returns the list of extension domain models to be loaded
284    /// </summary>
285    /// <returns>A list of domain model types. Can be empty.</returns>
286    /// <remarks>This method is called by "GetDomainModels", which aggregates the returned lists.</remarks>
287    protected virtual global::System.Collections.Generic.IEnumerable<global::System.Type> GetExtensionDomainModels()
288    {
289      if (this.ExtensionLocator == null)
290      {
291        return null;
292      }
293
294      global::System.Collections.Generic.IEnumerable<global::System.Type> extensionDomainModels = this.ExtensionLocator.GetExtendingDomainModels(typeof(global::HeuristicLab.SimulationCore.HLSimDomainModel));
295
296      return extensionDomainModels;
297    }
298
299
300
301
302    /// <summary>
303    /// Loads the given file.
304    /// </summary>
305    protected override void Load(string fileName, bool isReload)
306    {
307      DslModeling::SerializationResult serializationResult = new DslModeling::SerializationResult();
308      global::HeuristicLab.SimulationCore.SimulationModel modelRoot = null;
309      DslModeling::ISchemaResolver schemaResolver = new DslShell::ModelingSchemaResolver(this.ServiceProvider);
310      //clear the current root element
311      this.SetRootElement(null);
312      // Enable diagram fixup rules in our store, because we will load diagram data.
313      global::HeuristicLab.SimulationCore.HLSimDomainModel.EnableDiagramRules(this.Store);
314      string diagramFileName = fileName + this.DiagramExtension;
315     
316      modelRoot = global::HeuristicLab.SimulationCore.HLSimSerializationHelper.Instance.LoadModelAndDiagram(serializationResult, this.GetModelPartition(), fileName, this.GetDiagramPartition(), diagramFileName, schemaResolver, null /* no load-time validation */, this.SerializerLocator);
317
318      // Report serialization messages.
319      this.SuspendErrorListRefresh();
320      try
321      {
322        foreach (DslModeling::SerializationMessage serializationMessage in serializationResult)
323        {
324          this.AddErrorListItem(new DslShell::SerializationErrorListItem(this.ServiceProvider, serializationMessage));
325        }
326      }
327      finally
328      {
329        this.ResumeErrorListRefresh();
330      }
331
332      if (serializationResult.Failed)
333      {
334        // Load failed, can't open the file.
335        throw new global::System.InvalidOperationException(global::HeuristicLab.SimulationCore.HLSimDomainModel.SingletonResourceManager.GetString("CannotOpenDocument"));
336      }
337      else
338      {
339        this.SetRootElement(modelRoot);
340       
341        // Attempt to set the encoding
342        if (serializationResult.Encoding != null)
343        {
344          this.ModelingDocStore.SetEncoding(serializationResult.Encoding);
345          global::Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(this.SetDocDataDirty(0)); // Setting the encoding will mark the document as dirty, so clear the dirty flag.
346        }
347       
348        if (this.Hierarchy != null && global::System.IO.File.Exists(diagramFileName))
349        {
350          // Add a lock to the subordinate diagram file.
351          if (this.diagramDocumentLockHolder == null)
352          {
353            uint itemId = DslShell::SubordinateFileHelper.GetChildProjectItemId(this.Hierarchy, this.ItemId, this.DiagramExtension);
354            if (itemId != global::Microsoft.VisualStudio.VSConstants.VSITEMID_NIL)
355            {
356              this.diagramDocumentLockHolder = DslShell::SubordinateFileHelper.LockSubordinateDocument(this.ServiceProvider, this, diagramFileName, itemId);
357              if (this.diagramDocumentLockHolder == null)
358              {
359                throw new global::System.InvalidOperationException(string.Format(global::System.Globalization.CultureInfo.CurrentCulture,
360                          global::HeuristicLab.SimulationCore.HLSimDomainModel.SingletonResourceManager.GetString("CannotCloseExistingDiagramDocument"),
361                          diagramFileName));
362              }
363            }
364          }
365        }
366      }
367    }
368
369
370     
371    /// <summary>
372    /// Handle when document has been saved
373    /// </summary>
374    /// <param name="e"></param>
375    protected override void OnDocumentSaved(global::System.EventArgs e)
376    {
377      base.OnDocumentSaved(e);
378
379      // Notify the Running Document Table that the subordinate has been saved
380      // If this was a SaveAs, then let the subordinate document do this notification itself.
381      // Otherwise VS will never ask the subordinate to save itself.
382      DslShell::DocumentSavedEventArgs savedEventArgs = e as DslShell::DocumentSavedEventArgs;
383      if (savedEventArgs != null && this.ServiceProvider != null)
384      {
385        this.NotifySubordinateDocumentSaved(savedEventArgs.OldFileName, savedEventArgs.NewFileName);
386      }
387    }
388
389    /// <summary>
390    /// Notify the RDT that the sub-ordinate document has been saved, assuming saved to the same file as registered in the RDT
391    /// </summary>
392    protected virtual void NotifySubordinateDocumentSaved(string oldFileName, string newFileName)
393    {
394      if (this.ServiceProvider != null)
395      {
396        if (global::System.StringComparer.OrdinalIgnoreCase.Compare(oldFileName, newFileName) == 0)
397        {
398          VSShellInterop::IVsRunningDocumentTable rdt = (VSShellInterop.IVsRunningDocumentTable)this.ServiceProvider.GetService(typeof(VSShellInterop::SVsRunningDocumentTable));
399          if (rdt != null && this.diagramDocumentLockHolder != null && this.diagramDocumentLockHolder.SubordinateDocData != null)
400          {
401            global::Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(rdt.NotifyOnAfterSave(this.diagramDocumentLockHolder.SubordinateDocData.Cookie));
402          }
403        }
404      }
405    }
406
407    /// <summary>
408    /// Saves the given file.
409    /// </summary>
410    protected override void Save(string fileName)
411    {
412      DslModeling::SerializationResult serializationResult = new DslModeling::SerializationResult();
413      global::HeuristicLab.SimulationCore.SimulationModel modelRoot = (global::HeuristicLab.SimulationCore.SimulationModel)this.RootElement;
414
415     
416      // Only save the diagrams if
417      // a) There are any to save
418      // b) This is NOT a SaveAs operation.  SaveAs should allow the subordinate document to control the save of its data as it is writing a new file.
419      //    Except DO save the diagram on SaveAs if there isn't currently a diagram as there won't be a subordinate document yet to save it.
420
421      bool saveAs = global::System.StringComparer.OrdinalIgnoreCase.Compare(fileName, this.FileName) != 0;
422
423      global::System.Collections.Generic.IList<DslDiagrams::PresentationElement> diagrams = DslDiagrams::PresentationViewsSubject.GetPresentation(this.RootElement);
424      if (diagrams.Count > 0 && (!saveAs || this.diagramDocumentLockHolder == null))
425      {
426        global::HeuristicLab.SimulationCore.HLSimDiagram diagram = diagrams[0] as global::HeuristicLab.SimulationCore.HLSimDiagram;
427        if (diagram != null)
428        {
429          string diagramFileName = fileName + this.DiagramExtension;
430          try
431          {
432            this.SuspendFileChangeNotification(diagramFileName);
433           
434            global::HeuristicLab.SimulationCore.HLSimSerializationHelper.Instance.SaveModelAndDiagram(serializationResult, modelRoot, fileName, diagram, diagramFileName, this.Encoding, false);
435          }
436          finally
437          {
438            this.ResumeFileChangeNotification(diagramFileName);
439          }
440        }
441      }
442      else
443      {
444        global::HeuristicLab.SimulationCore.HLSimSerializationHelper.Instance.SaveModel(serializationResult, modelRoot, fileName, this.Encoding, false);
445      }
446      // Report serialization messages.
447      this.SuspendErrorListRefresh();
448      try
449      {
450        foreach (DslModeling::SerializationMessage serializationMessage in serializationResult)
451        {
452          this.AddErrorListItem(new DslShell::SerializationErrorListItem(this.ServiceProvider, serializationMessage));
453        }
454      }
455      finally
456      {
457        this.ResumeErrorListRefresh();
458      }
459
460      if (serializationResult.Failed)
461      { // Save failed.
462        throw new global::System.InvalidOperationException(global::HeuristicLab.SimulationCore.HLSimDomainModel.SingletonResourceManager.GetString("CannotSaveDocument"));
463      }
464    }
465    /// <summary>
466    /// Mark that the document has changed and thus a new backup should be created
467    /// </summary>
468    /// <remarks>
469    /// Call this method when you change the document's content
470    /// </remarks>
471    public override void MarkDocumentChangedForBackup()
472    {
473      base.MarkDocumentChangedForBackup();
474
475      // Also mark the subordinate document as changed
476      if (this.diagramDocumentLockHolder != null &&
477        this.diagramDocumentLockHolder.SubordinateDocData != null)
478      {
479        this.diagramDocumentLockHolder.SubordinateDocData.MarkDocumentChangedForBackup();
480      }
481    }
482   
483    #region Diagram file management
484   
485    /// <summary>
486    /// Save the given document that is subordinate to this document.
487    /// </summary>
488    /// <param name="subordinateDocument"></param>
489    /// <param name="fileName"></param>
490    protected override void SaveSubordinateFile(DslShell::DocData subordinateDocument, string fileName)
491    {
492      // In this case, the only subordinate is the diagram.
493      DslModeling::SerializationResult serializationResult = new DslModeling::SerializationResult();
494
495      global::System.Collections.Generic.IList<DslDiagrams::PresentationElement> diagrams = DslDiagrams::PresentationViewsSubject.GetPresentation(this.RootElement);
496      if (diagrams.Count > 0)
497      {
498        global::HeuristicLab.SimulationCore.HLSimDiagram diagram = diagrams[0] as global::HeuristicLab.SimulationCore.HLSimDiagram;
499        if (diagram != null)
500        {
501          try
502          {
503            this.SuspendFileChangeNotification(fileName);
504           
505            global::HeuristicLab.SimulationCore.HLSimSerializationHelper.Instance.SaveDiagram(serializationResult, diagram, fileName, this.Encoding, false);
506          }
507          finally
508          {
509            this.ResumeFileChangeNotification(fileName);
510          }
511        }
512      }
513      // Report serialization messages.
514      this.SuspendErrorListRefresh();
515      try
516      {
517        foreach (DslModeling::SerializationMessage serializationMessage in serializationResult)
518        {
519          this.AddErrorListItem(new DslShell::SerializationErrorListItem(this.ServiceProvider, serializationMessage));
520        }
521      }
522      finally
523      {
524        this.ResumeErrorListRefresh();
525      }
526     
527      if (!serializationResult.Failed)
528      {
529        this.NotifySubordinateDocumentSaved(subordinateDocument.FileName, fileName);
530      }
531      else
532      {
533        // Save failed.
534        throw new global::System.InvalidOperationException(global::HeuristicLab.SimulationCore.HLSimDomainModel.SingletonResourceManager.GetString("CannotSaveDocument"));
535      }           
536    }
537   
538    /// <summary>
539    /// Provide a suffix for the diagram file
540    /// </summary>
541    protected virtual string DiagramExtension
542    {
543      get
544      {
545        return Constants.DefaultDiagramExtension;
546      }
547    }
548    #endregion
549   
550    #region Base virtual overrides
551   
552    /// <summary>
553    /// Return the model in XML format
554    /// </summary>
555    protected override string SerializedModel
556    {
557      get
558      {
559        global::HeuristicLab.SimulationCore.SimulationModel modelRoot = this.RootElement as global::HeuristicLab.SimulationCore.SimulationModel;
560        string modelFile = string.Empty;
561        if (modelRoot != null)
562        {
563          modelFile = global::HeuristicLab.SimulationCore.HLSimSerializationHelper.Instance.GetSerializedModelString(modelRoot, this.Encoding);
564        }
565        return modelFile;
566      }
567    }
568    #endregion
569
570    #region Partition Support
571    /// <summary>
572    /// Get the Partition where model data will be loaded into.
573    /// Base implementation returns the default partition of the store.
574    /// </summary>
575    protected internal virtual DslModeling::Partition GetModelPartition()
576    {
577      if (this.Store != null)
578      {
579        return this.Store.DefaultPartition;
580      }
581     
582      return null;
583    }
584
585    /// <summary>
586    /// Id of the partition that contains diagram elements.
587    /// </summary>
588    protected global::System.Guid diagramPartitionId = global::System.Guid.Empty;
589   
590    /// <summary>
591    /// Get the Partition where diagram data will be loaded into.
592    /// Base implementation returns the default partition of the store.
593    /// </summary>
594    protected internal virtual DslModeling::Partition GetDiagramPartition()
595    {
596      DslModeling::Partition result = null;
597      if (this.Store != null)
598      {
599        if (this.diagramPartitionId == global::System.Guid.Empty || !this.Store.Partitions.TryGetValue(this.diagramPartitionId, out result))
600        {
601          result = new DslModeling::Partition(this.Store);
602          this.diagramPartitionId = result.Id;
603        }
604      }
605     
606      return result;
607    }
608    #endregion
609  }
610}
Note: See TracBrowser for help on using the repository browser.