1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Drawing.Imaging;
|
---|
4 | using System.IO;
|
---|
5 | using System.Text;
|
---|
6 | using Netron.GraphLib.UI;
|
---|
7 | using System.Reflection;
|
---|
8 | namespace Netron.GraphLib.IO.HTML
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Exports a diagram to HTML
|
---|
12 | /// includes the diagram as an image with an
|
---|
13 | /// imagemap if there are URL's included on shapes.
|
---|
14 | /// </summary>
|
---|
15 | public class HTMLExporter
|
---|
16 | {
|
---|
17 |
|
---|
18 | /// <summary>
|
---|
19 | /// the graph control
|
---|
20 | /// </summary>
|
---|
21 | private GraphControl mSite;
|
---|
22 |
|
---|
23 | /// <summary>
|
---|
24 | /// Default constructor
|
---|
25 | /// </summary>
|
---|
26 | /// <param name="site"></param>
|
---|
27 | public HTMLExporter(GraphControl site)
|
---|
28 | {
|
---|
29 | mSite = site;
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | /// <summary>
|
---|
34 | /// Creates the necessary files and directories for the export
|
---|
35 | /// </summary>
|
---|
36 | /// <param name="filePath"></param>
|
---|
37 | private void CreateDirAndFiles(string filePath)
|
---|
38 | {
|
---|
39 | if(!Directory.Exists(Path.GetDirectoryName(filePath)))
|
---|
40 | {
|
---|
41 | Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
---|
42 | }
|
---|
43 | if(!Directory.Exists(Path.GetDirectoryName(filePath) + "\\images"))
|
---|
44 | {
|
---|
45 | Directory.CreateDirectory(Path.GetDirectoryName(filePath) + "\\images");
|
---|
46 | }
|
---|
47 | Stream stream=Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.GraphLib.Resources.Stripes.gif"); |
---|
48 | Bitmap bmp= Bitmap.FromStream(stream) as Bitmap; |
---|
49 | bmp.Save(Path.GetDirectoryName(filePath) + "\\images\\Stripes.gif",ImageFormat.Gif); |
---|
50 | stream.Close(); |
---|
51 | stream=null; |
---|
52 | /* |
---|
53 | Stream stream=Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.GraphLib.Resources.GradLeft.jpg"); |
---|
54 | Bitmap bmp= Bitmap.FromStream(stream) as Bitmap; |
---|
55 | bmp.Save(Path.GetDirectoryName(filePath) + "\\images\\GradLeft.jpg",ImageFormat.Jpeg); |
---|
56 | stream.Close(); |
---|
57 | stream=null; |
---|
58 | |
---|
59 | stream=Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.GraphLib.Resources.GradTop.jpg"); |
---|
60 | bmp= Bitmap.FromStream(stream) as Bitmap; |
---|
61 | bmp.Save(Path.GetDirectoryName(filePath) + "\\images\\GradTop.jpg",ImageFormat.Jpeg); |
---|
62 | stream.Close(); |
---|
63 | stream=null; |
---|
64 | */
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// <summary>
|
---|
68 | /// Creates the image-map for the clickable areas related to the URL on the shapes
|
---|
69 | /// </summary>
|
---|
70 | /// <returns></returns>
|
---|
71 | private string GetURLMap()
|
---|
72 | {
|
---|
73 | StringBuilder sb = new StringBuilder();
|
---|
74 | string template = @"<area shape=""rect"" coords=""{0},{1},{2},{3}"" href=""{4}"" target=""_blank"" title=""{4}"" >";
|
---|
75 | foreach(Shape sh in mSite.Abstract.Shapes)
|
---|
76 | {
|
---|
77 | if(sh.URL!=string.Empty)
|
---|
78 | {
|
---|
79 | sb.AppendFormat(template,Convert.ToInt32(sh.Rectangle.Right-16),Convert.ToInt32(sh.Rectangle.Bottom-16),Convert.ToInt32(sh.Rectangle.Right), Convert.ToInt32(sh.Rectangle.Bottom),sh.URL);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return sb.ToString();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /// <summary>
|
---|
86 | /// Saves an HTML version of the diagram to the given file
|
---|
87 | /// </summary>
|
---|
88 | /// <param name="filePath">a path</param>
|
---|
89 | public void SaveAs(string filePath)
|
---|
90 | {
|
---|
91 | CreateDirAndFiles(filePath);
|
---|
92 |
|
---|
93 | StreamWriter sw = null;
|
---|
94 | try
|
---|
95 | {
|
---|
96 | GraphInformation info = mSite.Abstract.GraphInformation;
|
---|
97 | Stream stream=Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.GraphLib.Resources.DefaultHTML.htm");
|
---|
98 | StreamReader reader = new StreamReader(stream,System.Text.Encoding.ASCII);
|
---|
99 | string template = reader.ReadToEnd();
|
---|
100 |
|
---|
101 | stream.Close();
|
---|
102 |
|
---|
103 | stream=null;
|
---|
104 |
|
---|
105 | sw = new StreamWriter(filePath);
|
---|
106 |
|
---|
107 | if(info.Title==string.Empty)
|
---|
108 | template = template.Replace("$title$", "A Netron diagram");
|
---|
109 | else
|
---|
110 | template = template.Replace("$title$",info.Title);
|
---|
111 |
|
---|
112 | if(info.Description==string.Empty)
|
---|
113 | template = template.Replace("$description$","Not set");
|
---|
114 | else
|
---|
115 | template = template.Replace("$description$",info.Description);
|
---|
116 |
|
---|
117 | if(info.Author==string.Empty)
|
---|
118 | template = template.Replace("$author$", "Not set");
|
---|
119 | else
|
---|
120 | template = template.Replace("$author$",info.Author);
|
---|
121 |
|
---|
122 | if(info.Subject==string.Empty)
|
---|
123 | template = template.Replace("$subject$", "Not set");
|
---|
124 | else
|
---|
125 | template = template.Replace("$subject$",info.Subject);
|
---|
126 |
|
---|
127 | template = template.Replace("$creationdate$",info.CreationDate);
|
---|
128 |
|
---|
129 | if(mSite.FileName==string.Empty)
|
---|
130 | template = template.Replace("$filepath$", "Unsaved diagram");
|
---|
131 | else
|
---|
132 | template = template.Replace("$filepath$",mSite.FileName);
|
---|
133 |
|
---|
134 | string imagename = Path.ChangeExtension(Path.GetFileName(filePath),"jpg");
|
---|
135 |
|
---|
136 | mSite.SaveImage(Path.GetDirectoryName(filePath) + "\\images\\" + imagename, true);
|
---|
137 |
|
---|
138 |
|
---|
139 | template = template.Replace("$imagepath$","images\\" + imagename);
|
---|
140 |
|
---|
141 | template = template.Replace("$urlmap$",GetURLMap());
|
---|
142 |
|
---|
143 |
|
---|
144 | sw.Write(template);
|
---|
145 | sw.Close();
|
---|
146 | }
|
---|
147 | catch(Exception exc)
|
---|
148 | {
|
---|
149 | mSite.OutputInfo(exc.Message,OutputInfoLevels.Exception);
|
---|
150 | if(sw!=null) sw.Close();
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|