Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Core/Library.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 7.0 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.Reflection;
5using System.Windows.Forms;
6
7namespace Netron.Diagramming.Core {
8  // ----------------------------------------------------------------------
9  /// <summary>
10  /// Contains a collection of shapes and handles loading shapes from
11  /// assemblies.  This can also be serialized for later use.
12  /// </summary>
13  // ----------------------------------------------------------------------
14  public class Library {
15    // ------------------------------------------------------------------
16    /// <summary>
17    /// The collection of shapes for this library.
18    /// </summary>
19    // ------------------------------------------------------------------
20    CollectionBase<IShape> myShapes;
21
22    // ------------------------------------------------------------------
23    /// <summary>
24    /// The name of this library.
25    /// </summary>
26    // ------------------------------------------------------------------
27    string myName = "Library";
28
29    // ------------------------------------------------------------------
30    /// <summary>
31    /// The complete path to the assembly to load.
32    /// </summary>
33    // ------------------------------------------------------------------
34    string myPath = String.Empty;
35
36    // ------------------------------------------------------------------
37    /// <summary>
38    /// Our assembly.
39    /// </summary>
40    // ------------------------------------------------------------------
41    Assembly assembly;
42
43    // ------------------------------------------------------------------
44    /// <summary>
45    /// Gets or sets the name of this library.
46    /// </summary>
47    // ------------------------------------------------------------------
48    public string Name {
49      get {
50        return myName;
51      }
52      set {
53        myName = value;
54      }
55    }
56
57    // ------------------------------------------------------------------
58    /// <summary>
59    /// Gets the shapes in the library.
60    /// </summary>
61    // ------------------------------------------------------------------
62    public CollectionBase<IShape> Shapes {
63      get {
64        return myShapes;
65      }
66    }
67
68    // ------------------------------------------------------------------
69    /// <summary>
70    /// Constructor.
71    /// </summary>
72    // ------------------------------------------------------------------
73    public Library() {
74      this.myShapes = new CollectionBase<IShape>();
75    }
76
77    // ------------------------------------------------------------------
78    /// <summary>
79    /// Returns if this library contains a shape with the GUID specified.
80    /// </summary>
81    /// <param name="guid"></param>
82    /// <returns></returns>
83    // ------------------------------------------------------------------
84    public bool ContainsShape(string guid) {
85      foreach (IShape shape in myShapes) {
86        if (shape.Uid.ToString() == guid) {
87          return true;
88        }
89      }
90      return false;
91    }
92
93    // ------------------------------------------------------------------
94    /// <summary>
95    /// Creates a new instance of the shape with the GUID specified.
96    /// </summary>
97    /// <param name="guid">string</param>
98    /// <returns>IShape</returns>
99    // ------------------------------------------------------------------
100    public IShape CreateNewInstance(string guid) {
101      if ((assembly == null) ||
102          (guid == "") ||
103          (guid == String.Empty)) {
104        return null;
105      }
106
107      string typeName = "";
108      foreach (IShape shape in myShapes) {
109        if (shape.Uid.ToString() == guid) {
110          typeName = shape.GetType().FullName;
111          IShape newInstance =
112              (IShape)assembly.CreateInstance(typeName);
113
114          return newInstance;
115        }
116      }
117      return null;
118    }
119
120    // ------------------------------------------------------------------
121    /// <summary>
122    /// Imports all shapes that have the "ShapeAttribute" attribute
123    /// from the assembly (i.e. "dll") specified.
124    /// </summary>
125    /// <param name="path">string: The complete path to the assembly to
126    /// load.</param>
127    // ------------------------------------------------------------------
128    public void Load(string path) {
129      try {
130        myPath = path;
131        Directory.SetCurrentDirectory(
132            Path.GetDirectoryName(Application.ExecutablePath));
133
134        // Use the "LoadFile" option so the assembly is also loaded
135        // into the current domain.  This is important!!!
136        assembly = Assembly.LoadFile(path);
137        if (assembly == null) {
138          return;
139        }
140
141        // The assembly types.
142        Type[] types = assembly.GetTypes();
143
144        if (types == null) {
145          return;
146        }
147
148        IShape shapeInstance = null;
149        object[] objs;
150
151        // Loop over all modules in the assembly and get all shapes
152        // that are marked with the attribute that indicates they're
153        // to be loaded into the library.
154        for (int k = 0; k < types.Length; k++) {
155          // It has to be a class to be a shape!
156          if (!types[k].IsClass) {
157            continue;
158          }
159
160          objs = types[k].GetCustomAttributes(
161              typeof(ShapeAttribute), false);
162
163          if (objs.Length < 1) {
164            // This module isn't a shape so go to the next one.
165            continue;
166          }
167
168          // Now, we are sure to have a shape object.         
169
170          try {
171            // Normally you'd need the constructor passing the
172            // Model, but this instance will not actually live
173            // on the canvas and hence cause no problem.
174            // However, you do need a ctor with no parameters!
175            shapeInstance = (IShape)assembly.CreateInstance(
176                types[k].FullName);
177
178            ShapeAttribute shapeAtts = objs[0] as ShapeAttribute;
179            this.myShapes.Add(shapeInstance);
180            //summary = new ShapeSummary(path, shapeAtts.Key,shapeAtts.Name, shapeAtts.ShapeCategory, shapeAtts.ReflectionName, shapeAtts.Description);
181            //library.ShapeSummaries.Add(summary);
182          }
183          catch (Exception exc) {
184            Trace.WriteLine(exc.Message, "An error occurred " +
185                "while creating a shape from type " +
186                types[k].FullName);
187            continue;
188          }
189        }
190
191        // Now, set the name of this library to the assembly title.
192        foreach (Attribute attr in
193            Attribute.GetCustomAttributes(assembly)) {
194          // Check for the AssemblyTitle attribute.
195          if (attr.GetType() == typeof(AssemblyTitleAttribute)) {
196            this.myName = ((AssemblyTitleAttribute)attr).Title;
197            break;
198          }
199        }
200
201      }
202      catch (Exception e) {
203        Trace.WriteLine(e.Message, "An error occurred while " +
204            "loading library from:\n" + path);
205        return;
206      }
207    }
208  }
209}
Note: See TracBrowser for help on using the repository browser.