CloseWorkspaceAction.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Resources;
  2. using SmartCoalApplication.Base;
  3. using SmartCoalApplication.Base.CommTool;
  4. using SmartCoalApplication.Core;
  5. using System;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace SmartCoalApplication.Actions
  9. {
  10. internal sealed class CloseWorkspaceAction : AppWorkspaceAction
  11. {
  12. private DocumentWorkspace closeMe;
  13. private bool cancelled;
  14. public bool Cancelled
  15. {
  16. get
  17. {
  18. return this.cancelled;
  19. }
  20. }
  21. public override void PerformAction(AppWorkspace appWorkspace)
  22. {
  23. if (appWorkspace == null)
  24. {
  25. throw new ArgumentNullException("appWorkspace");
  26. }
  27. DocumentWorkspace dw;
  28. if (this.closeMe == null)
  29. {
  30. dw = appWorkspace.ActiveDocumentWorkspace;
  31. }
  32. else
  33. {
  34. dw = this.closeMe;
  35. }
  36. if (dw != null)
  37. {
  38. if (dw.Document == null)
  39. {
  40. appWorkspace.RemoveDocumentWorkspace(dw);
  41. }
  42. else if (!dw.Document.Dirty)
  43. {
  44. appWorkspace.RemoveDocumentWorkspace(dw);
  45. }
  46. else
  47. {
  48. appWorkspace.ActiveDocumentWorkspace = dw;
  49. TaskButton saveTB = new TaskButton(
  50. PdnResources.GetImageResource("Icons.MenuFileSaveIcon.png").Reference,
  51. PdnResources.GetString("CloseWorkspaceAction.SaveButton.ActionText"),
  52. PdnResources.GetString("CloseWorkspaceAction.SaveButton.ExplanationText"));
  53. TaskButton dontSaveTB = new TaskButton(
  54. PdnResources.GetImageResource("Icons.MenuFileCloseIcon.png").Reference,
  55. PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ActionText"),
  56. PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ExplanationText"));
  57. TaskButton cancelTB = new TaskButton(
  58. PdnResources.GetImageResource("Icons.CancelIcon.png").Reference,
  59. PdnResources.GetString("CloseWorkspaceAction.CancelButton.ActionText"),
  60. PdnResources.GetString("CloseWorkspaceAction.CancelButton.ExplanationText"));
  61. string title = PdnResources.GetString("CloseWorkspaceAction.Title");
  62. string introTextFormat = PdnResources.GetString("CloseWorkspaceAction.IntroText.Format");
  63. string introText = string.Format(introTextFormat, dw.GetFriendlyName());
  64. Image taskImage = DrawRulerHelper.ResizeImage(appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap(), 115, 86, Base.Enum.ThumbnailMode.Cut);
  65. Form mainForm = appWorkspace.FindForm();
  66. if (mainForm != null)
  67. {
  68. PdnBaseForm asPDF = mainForm as PdnBaseForm;
  69. if (asPDF != null)
  70. {
  71. asPDF.RestoreWindow();
  72. }
  73. }
  74. Icon warningIcon;
  75. ImageResource warningIconImageRes = PdnResources.GetImageResource("Icons.WarningIcon.png");
  76. if (warningIconImageRes != null)
  77. {
  78. Image warningIconImage = warningIconImageRes.Reference;
  79. warningIcon = Utility.ImageToIcon(warningIconImage, false);
  80. }
  81. else
  82. {
  83. warningIcon = null;
  84. }
  85. TaskButton clickedTB = TaskDialog.Show(
  86. appWorkspace,
  87. warningIcon,
  88. title,
  89. taskImage,
  90. false,
  91. introText,
  92. new TaskButton[] { saveTB, dontSaveTB, cancelTB },
  93. saveTB,
  94. cancelTB,
  95. 340,
  96. false,
  97. 0,
  98. out bool unuse);
  99. if (clickedTB == saveTB)
  100. {
  101. if (dw.DoSaveNew())
  102. {
  103. this.cancelled = false;
  104. if (!dw.IsDisposed)
  105. appWorkspace.RemoveDocumentWorkspace(dw);
  106. }
  107. else
  108. {
  109. this.cancelled = true;
  110. }
  111. }
  112. else if (clickedTB == dontSaveTB)
  113. {
  114. this.cancelled = false;
  115. if (!dw.IsDisposed)
  116. appWorkspace.RemoveDocumentWorkspace(dw);
  117. }
  118. else
  119. {
  120. this.cancelled = true;
  121. }
  122. }
  123. }
  124. Utility.GCFullCollect();
  125. }
  126. public CloseWorkspaceAction() : this(null)
  127. {
  128. }
  129. public CloseWorkspaceAction(DocumentWorkspace closeMe)
  130. {
  131. this.closeMe = closeMe;
  132. this.cancelled = false;
  133. }
  134. }
  135. }