[2768] | 1 | using System;
|
---|
[4068] | 2 | using System.Diagnostics;
|
---|
[2768] | 3 | using System.IO;
|
---|
| 4 | using System.Reflection;
|
---|
| 5 | using System.Windows.Forms;
|
---|
| 6 |
|
---|
[4068] | 7 | namespace 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 | // ------------------------------------------------------------------
|
---|
[2768] | 16 | /// <summary>
|
---|
[4068] | 17 | /// The collection of shapes for this library.
|
---|
[2768] | 18 | /// </summary>
|
---|
[4068] | 19 | // ------------------------------------------------------------------
|
---|
| 20 | CollectionBase<IShape> myShapes;
|
---|
[2768] | 21 |
|
---|
[4068] | 22 | // ------------------------------------------------------------------
|
---|
| 23 | /// <summary>
|
---|
| 24 | /// The name of this library.
|
---|
| 25 | /// </summary>
|
---|
| 26 | // ------------------------------------------------------------------
|
---|
| 27 | string myName = "Library";
|
---|
[2768] | 28 |
|
---|
[4068] | 29 | // ------------------------------------------------------------------
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// The complete path to the assembly to load.
|
---|
| 32 | /// </summary>
|
---|
| 33 | // ------------------------------------------------------------------
|
---|
| 34 | string myPath = String.Empty;
|
---|
[2768] | 35 |
|
---|
[4068] | 36 | // ------------------------------------------------------------------
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Our assembly.
|
---|
| 39 | /// </summary>
|
---|
| 40 | // ------------------------------------------------------------------
|
---|
| 41 | Assembly assembly;
|
---|
[2768] | 42 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 56 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 67 |
|
---|
[4068] | 68 | // ------------------------------------------------------------------
|
---|
| 69 | /// <summary>
|
---|
| 70 | /// Constructor.
|
---|
| 71 | /// </summary>
|
---|
| 72 | // ------------------------------------------------------------------
|
---|
| 73 | public Library() {
|
---|
| 74 | this.myShapes = new CollectionBase<IShape>();
|
---|
| 75 | }
|
---|
[2768] | 76 |
|
---|
[4068] | 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;
|
---|
[2768] | 88 | }
|
---|
[4068] | 89 | }
|
---|
| 90 | return false;
|
---|
| 91 | }
|
---|
[2768] | 92 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 106 |
|
---|
[4068] | 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);
|
---|
[2768] | 113 |
|
---|
[4068] | 114 | return newInstance;
|
---|
[2768] | 115 | }
|
---|
[4068] | 116 | }
|
---|
| 117 | return null;
|
---|
| 118 | }
|
---|
[2768] | 119 |
|
---|
[4068] | 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));
|
---|
[2768] | 133 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 140 |
|
---|
[4068] | 141 | // The assembly types.
|
---|
| 142 | Type[] types = assembly.GetTypes();
|
---|
[2768] | 143 |
|
---|
[4068] | 144 | if (types == null) {
|
---|
| 145 | return;
|
---|
| 146 | }
|
---|
[2768] | 147 |
|
---|
[4068] | 148 | IShape shapeInstance = null;
|
---|
| 149 | object[] objs;
|
---|
[2768] | 150 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 159 |
|
---|
[4068] | 160 | objs = types[k].GetCustomAttributes(
|
---|
| 161 | typeof(ShapeAttribute), false);
|
---|
[2768] | 162 |
|
---|
[4068] | 163 | if (objs.Length < 1) {
|
---|
| 164 | // This module isn't a shape so go to the next one.
|
---|
| 165 | continue;
|
---|
| 166 | }
|
---|
[2768] | 167 |
|
---|
[4068] | 168 | // Now, we are sure to have a shape object.
|
---|
[2768] | 169 |
|
---|
[4068] | 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);
|
---|
[2768] | 177 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 190 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 200 |
|
---|
[4068] | 201 | }
|
---|
| 202 | catch (Exception e) {
|
---|
| 203 | Trace.WriteLine(e.Message, "An error occurred while " +
|
---|
| 204 | "loading library from:\n" + path);
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
[2768] | 207 | }
|
---|
[4068] | 208 | }
|
---|
[2768] | 209 | }
|
---|