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