/Editor/Editor/Src/Editor/PetriNetGraphical.cs

http://3pv.googlecode.com/ · C# · 446 lines · 380 code · 66 blank · 0 comment · 37 complexity · 8611f261a0753caec98bc192b2363ab1 MD5 · raw file

  1. namespace Pppv.Editor
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.IO;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15. using Pppv.ApplicationFramework;
  16. using Pppv.ApplicationFramework.Utils;
  17. using Pppv.Editor.Shapes;
  18. using Pppv.Editor.Tools;
  19. using Pppv.Net;
  20. public class PetriNetGraphical : PetriNet, IXmlSerializable
  21. {
  22. private SelectedNetElements selectedObjects;
  23. private int width, height;
  24. private string fileOfNetPath;
  25. private bool netSaved;
  26. private PetriNet baseNet;
  27. private Editor.NetCanvas canvas;
  28. private ShapeCollection shapes;
  29. public PetriNetGraphical(PetriNet baseNet)
  30. {
  31. this.baseNet = baseNet;
  32. this.NetSaved = true;
  33. this.FileOfNetPath = String.Empty;
  34. this.Change += this.ChangeController;
  35. this.shapes = new ShapeCollection(this);
  36. this.selectedObjects = new SelectedNetElements();
  37. this.Shapes.Change += this.ShapesChangeHandler;
  38. this.CreateShapesForBaseNet();
  39. }
  40. public PetriNetGraphical() : this(new PetriNet())
  41. {
  42. }
  43. public event EventHandler<SaveNetEventArgs> Save;
  44. public event PaintEventHandler Paint;
  45. public event EventHandler CanvasChange;
  46. public event EventHandler Change;
  47. public new string Id
  48. {
  49. get { return this.baseNet.Id; }
  50. protected set { this.baseNet.Id = value; }
  51. }
  52. public int Width
  53. {
  54. get { return this.width; }
  55. set { this.width = value; }
  56. }
  57. public int Height
  58. {
  59. get { return this.height; }
  60. set { this.height = value; }
  61. }
  62. public new string NetType
  63. {
  64. get { return this.baseNet.NetType; }
  65. }
  66. public Editor.NetCanvas Canvas
  67. {
  68. get
  69. {
  70. return this.canvas;
  71. }
  72. set
  73. {
  74. if (this.canvas != null)
  75. {
  76. this.canvas.Paint -= this.CanvasPaintRetranslator;
  77. }
  78. this.canvas = value;
  79. this.OnCanvasChange(new EventArgs());
  80. if (this.canvas != null)
  81. {
  82. this.canvas.Paint += this.CanvasPaintRetranslator;
  83. }
  84. }
  85. }
  86. public new ArrayList Places
  87. {
  88. get { return this.baseNet.Places; }
  89. }
  90. public new ArrayList Transitions
  91. {
  92. get { return this.baseNet.Transitions; }
  93. }
  94. public new ArrayList Arcs
  95. {
  96. get { return this.baseNet.Arcs; }
  97. }
  98. public ShapeCollection Shapes
  99. {
  100. get { return this.shapes; }
  101. }
  102. public SelectedNetElements SelectedObjects
  103. {
  104. get { return this.selectedObjects; }
  105. }
  106. public bool NetSaved
  107. {
  108. get { return this.netSaved; }
  109. private set { this.netSaved = value; }
  110. }
  111. public string FileOfNetPath
  112. {
  113. get { return this.fileOfNetPath; }
  114. set { this.fileOfNetPath = value; }
  115. }
  116. public new string AdditionalCode
  117. {
  118. get
  119. {
  120. return this.baseNet.AdditionalCode;
  121. }
  122. set
  123. {
  124. this.baseNet.AdditionalCode = value;
  125. this.OnChange(new EventArgs());
  126. }
  127. }
  128. public PetriNet BaseNet
  129. {
  130. get { return this.baseNet; }
  131. }
  132. public string FileName
  133. {
  134. get { return this.FileOfNetPath.Substring(this.FileOfNetPath.LastIndexOf("\\", StringComparison.Ordinal) + 1); }
  135. }
  136. public void AddElement(IShape shape)
  137. {
  138. this.baseNet.AddElement(shape.BaseElement);
  139. this.Shapes.Add(shape);
  140. this.OnChange(new EventArgs());
  141. }
  142. public new void AddElement(INetElement element)
  143. {
  144. this.baseNet.AddElement(element);
  145. this.Shapes.Add(this.CreateShapeForNetElement(element));
  146. this.OnChange(new EventArgs());
  147. }
  148. public void DeleteElement(IShape element)
  149. {
  150. IShape element2 = element as IShape;
  151. this.baseNet.DeleteElement(element2.BaseElement);
  152. this.Shapes.Remove(element2);
  153. this.OnChange(new EventArgs());
  154. }
  155. public IShape GetTopLevelShapeUnder(Point testPoint)
  156. {
  157. for (int index = this.Shapes.Count - 1; index >= 0; --index)
  158. {
  159. if (this.Shapes[index].Intersect(testPoint))
  160. {
  161. return this.Shapes[index];
  162. }
  163. }
  164. return null;
  165. }
  166. public IShape GetShapeUnder(Point testPoint)
  167. {
  168. IShape shape = this.GetTopLevelShapeUnder(testPoint);
  169. if (shape == null)
  170. {
  171. return null;
  172. }
  173. bool haveDeeperLevelForTest = true;
  174. while (haveDeeperLevelForTest)
  175. {
  176. haveDeeperLevelForTest = false;
  177. for (int index = shape.DependentShapes.Count - 1; index >= 0; --index)
  178. {
  179. if (shape.DependentShapes[index].Intersect(testPoint))
  180. {
  181. shape = shape.DependentShapes[index];
  182. haveDeeperLevelForTest = true;
  183. break;
  184. }
  185. }
  186. }
  187. return shape;
  188. }
  189. public Collection<IShape> GetShapeUnder(Rectangle selectedRectangle)
  190. {
  191. Collection<IShape> selectedObjects = new Collection<IShape>();
  192. int i = 0;
  193. for (i = 0; i < this.Shapes.Count; ++i)
  194. {
  195. if (this.Shapes[i].Intersect(selectedRectangle))
  196. {
  197. selectedObjects.Add(this.Shapes[i]);
  198. }
  199. }
  200. return selectedObjects;
  201. }
  202. public new NetElement GetElementById(string searchingId)
  203. {
  204. return this.baseNet.GetElementById(searchingId);
  205. }
  206. public bool SaveNet()
  207. {
  208. bool result = false;
  209. StreamWriter stream;
  210. if (!String.IsNullOrEmpty(this.FileOfNetPath))
  211. {
  212. if (File.Exists(this.FileOfNetPath))
  213. {
  214. File.Delete(this.FileOfNetPath);
  215. }
  216. stream = new StreamWriter(this.FileOfNetPath, false, Encoding.GetEncoding(1251));
  217. if (stream != null)
  218. {
  219. XmlSerializer serealizer = new XmlSerializer(this.baseNet.GetType());
  220. serealizer.Serialize(stream, this.baseNet);
  221. stream.Close();
  222. this.OnSave(new SaveNetEventArgs(this));
  223. result = true;
  224. }
  225. }
  226. else
  227. {
  228. result = this.SaveNetAs();
  229. }
  230. return result;
  231. }
  232. public bool SaveNetAs()
  233. {
  234. bool result = false;
  235. StreamWriter stream;
  236. string fileName = String.Empty;
  237. SaveFileDialog saveFileDialog = new SaveFileDialog();
  238. saveFileDialog.Filter = "pnml files (*.pnml)|*.pnml|All files (*.*)|*.*";
  239. saveFileDialog.FilterIndex = 1;
  240. saveFileDialog.RestoreDirectory = true;
  241. saveFileDialog.InitialDirectory = Environment.CurrentDirectory;
  242. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  243. {
  244. stream = new StreamWriter(saveFileDialog.FileName, false, Encoding.GetEncoding(1251));
  245. if (stream != null)
  246. {
  247. this.FileOfNetPath = fileName = saveFileDialog.FileName;
  248. if (String.IsNullOrEmpty(this.Id))
  249. {
  250. this.Id = fileName.Substring(fileName.LastIndexOf("\\", StringComparison.Ordinal) + 1);
  251. }
  252. XmlSerializer serializer = new XmlSerializer(this.baseNet.GetType());
  253. serializer.Serialize(stream, this.baseNet);
  254. stream.Close();
  255. this.OnSave(new SaveNetEventArgs(this));
  256. result = true;
  257. }
  258. }
  259. return result;
  260. }
  261. public IShape FindShapeForElement(INetElement element)
  262. {
  263. foreach (IShape shape in this.Shapes)
  264. {
  265. if (shape.BaseElement == element)
  266. {
  267. return shape;
  268. }
  269. }
  270. return null;
  271. }
  272. public IShape CreateShapeForNetElement(INetElement baseElement)
  273. {
  274. if (baseElement is IArc)
  275. {
  276. return this.CreateShapeForArc(baseElement);
  277. }
  278. if (baseElement is ITransition)
  279. {
  280. return this.CreateShapeForTransition(baseElement);
  281. }
  282. if (baseElement is IPlace)
  283. {
  284. return this.CreateShapeForPlace(baseElement);
  285. }
  286. return null;
  287. }
  288. public IShape CreateShapeForArc(INetElement baseElement)
  289. {
  290. return new ArcShape((IArc)baseElement, this);
  291. }
  292. public IShape CreateShapeForPlace(INetElement baseElement)
  293. {
  294. return new PlaceShape((IPlace)baseElement, this);
  295. }
  296. public IShape CreateShapeForTransition(INetElement baseElement)
  297. {
  298. return new TransitionShape((ITransition)baseElement, this);
  299. }
  300. private void CanvasPaintRetranslator(object sender, PaintEventArgs args)
  301. {
  302. this.OnPaint(args);
  303. }
  304. private void OnCanvasChange(EventArgs args)
  305. {
  306. if (this.CanvasChange != null)
  307. {
  308. this.CanvasChange(this, args);
  309. }
  310. }
  311. private void OnPaint(PaintEventArgs e)
  312. {
  313. if (this.Paint != null)
  314. {
  315. using (PreciseTimer pr = new PreciseTimer("PetriNet.Draw"))
  316. {
  317. this.Paint(this, e);
  318. }
  319. }
  320. }
  321. private void CalculateSize()
  322. {
  323. this.Width = this.Height = 0;
  324. int testX = 0,
  325. testY = 0;
  326. foreach (IShape shape in this.Shapes)
  327. {
  328. testX = shape.X + shape.Size.Width;
  329. testY = shape.Y + shape.Size.Height;
  330. if (testX > this.Width)
  331. {
  332. this.Width = testX;
  333. }
  334. if (testY > this.Height)
  335. {
  336. this.Height = testY;
  337. }
  338. }
  339. }
  340. private void ChangeController(object sender, System.EventArgs args)
  341. {
  342. this.NetSaved = false;
  343. this.CalculateSize();
  344. }
  345. private void OnSave(SaveNetEventArgs args)
  346. {
  347. this.NetSaved = true;
  348. if (this.Save != null)
  349. {
  350. this.Save(this, args);
  351. }
  352. }
  353. private void ShapesChangeHandler(object sender, System.EventArgs args)
  354. {
  355. this.OnChange(new EventArgs());
  356. }
  357. private void OnChange(EventArgs args)
  358. {
  359. if (this.Change != null)
  360. {
  361. this.Change(this, args);
  362. }
  363. }
  364. private void CreateShapesForBaseNet()
  365. {
  366. foreach (Place place in this.baseNet.Places)
  367. {
  368. this.Shapes.Add(this.CreateShapeForNetElement(place));
  369. }
  370. foreach (Transition transition in this.baseNet.Transitions)
  371. {
  372. this.Shapes.Add(this.CreateShapeForNetElement(transition));
  373. }
  374. foreach (Arc arc in this.baseNet.Arcs)
  375. {
  376. this.Shapes.Add(this.CreateShapeForNetElement(arc));
  377. }
  378. }
  379. }
  380. }