Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/25/10 17:28:31 (14 years ago)
Author:
mkommend
Message:

finished mapping from OperatorGraph to GraphVisualizationInfo (ticket #867)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/UI/DiagramControlBase.cs

    r2768 r2868  
    88using System.Drawing.Printing;
    99using System.Collections;
    10 namespace Netron.Diagramming.Core
    11 {
    12     /// <summary>
    13     /// Abstract base class for the various diagram control representations
    14     /// (WebForm and WinForm).
    15     /// </summary>
    16     [ ToolboxItem(false)   ]
    17     public  class DiagramControlBase :
    18         ScrollableControl,
    19         ISupportInitialize,
    20         IDiagramControl
    21     {
    22         #region Constants
    23         protected const string constGeneral = "General";
    24         #endregion
    25 
    26         #region Events
    27         /// <summary>
    28         /// Occurs when the something got selected and the properties of it can/should be shown.
    29         /// </summary>
    30         [Category(constGeneral),
    31         Description("Occurs when the something got selected and the " +
    32             "properties of it can/should be shown."),
    33         Browsable(true)]
    34         public event EventHandler<SelectionEventArgs> OnShowSelectionProperties;
    35         /// <summary>
    36         /// Occurs when the Properties have changed
    37         /// </summary>
    38         [Category(constGeneral),
    39         Description("Occurs when the properties have changed."),
    40         Browsable(true)]
    41         public event EventHandler<PropertiesEventArgs> OnShowDocumentProperties;
    42         /// <summary>
    43         /// Occurs when the properties of the canvas are exposed.
    44         /// </summary>
    45         public event EventHandler<SelectionEventArgs> OnShowCanvasProperties;
    46         /// <summary>
    47         /// Occurs when an entity is added.
    48         /// <remarks>This event usually is bubbled from one of the layers</remarks>
    49         /// </summary>
    50         [Category(constGeneral), Description("Occurs when an entity is added."), Browsable(true)]
    51         public event EventHandler<EntityEventArgs> OnEntityAdded;
    52         /// <summary>
    53         /// Occurs when an entity is removed.
    54         /// <remarks>This event usually is bubbled from one of the layers</remarks>
    55         /// </summary>
    56         [Category(constGeneral), Description("Occurs when an entity is removed."), Browsable(true)]
    57         public event EventHandler<EntityEventArgs> OnEntityRemoved;
    58         /// <summary>
    59         /// Occurs on opening a file
    60         /// </summary>
    61         [Category(constGeneral), Description("Occurs when the control will open a file."), Browsable(true)]
    62         public event EventHandler<FileEventArgs> OnOpeningDiagram;
    63         /// <summary>
    64         /// Occurs when a file was opened
    65         /// </summary>
    66         [Category(constGeneral), Description(" Occurs when a file was opened."), Browsable(true)]
    67         public event EventHandler<FileEventArgs> OnDiagramOpened;       
    68         /// <summary>
    69         /// Occurs when the history has changed in the undo/redo mechanism
    70         /// </summary>
    71         public event EventHandler<HistoryChangeEventArgs> OnHistoryChange;
    72         /// <summary>
    73         /// Occurs before saving a diagram
    74         /// </summary>
    75         [Category(constGeneral), Description("Occurs before saving a diagram."), Browsable(true)]
    76         public event EventHandler<FileEventArgs> OnSavingDiagram;
    77         /// <summary>
    78         /// Occurs after saving a diagram
    79         /// </summary>
    80         [Category(constGeneral), Description("Occurs after saving a diagram."), Browsable(true)]
    81         public event EventHandler<FileEventArgs> OnDiagramSaved;
    82         /// <summary>
    83         /// Occurs when the control resets the document internally, usually through the Ctrl+N hotkey.
    84         /// </summary>
    85         [Category(constGeneral), Description("Occurs when the control resets the document internally, usually through the Ctrl+N hotkey."), Browsable(true)]
    86         public event EventHandler OnNewDiagram;
    87 
    88         #region Event raisers
    89 
    90         /// <summary>
    91         /// Raises the OnShowCanvasProperties event.
    92         /// </summary>
    93         protected void RaiseOnShowCanvasProperties(SelectionEventArgs e)
    94         {
    95             EventHandler<SelectionEventArgs> handler = OnShowCanvasProperties;
    96             if (handler != null)
    97             {
    98                 handler(this, e);
    99             }
     10namespace Netron.Diagramming.Core {
     11  /// <summary>
     12  /// Abstract base class for the various diagram control representations
     13  /// (WebForm and WinForm).
     14  /// </summary>
     15  [ToolboxItem(false)]
     16  public class DiagramControlBase :
     17      ScrollableControl,
     18      ISupportInitialize,
     19      IDiagramControl {
     20    #region Constants
     21    protected const string constGeneral = "General";
     22    #endregion
     23
     24    #region Events
     25    /// <summary>
     26    /// Occurs when the something got selected and the properties of it can/should be shown.
     27    /// </summary>
     28    [Category(constGeneral),
     29    Description("Occurs when the something got selected and the " +
     30        "properties of it can/should be shown."),
     31    Browsable(true)]
     32    public event EventHandler<SelectionEventArgs> OnShowSelectionProperties;
     33    /// <summary>
     34    /// Occurs when the Properties have changed
     35    /// </summary>
     36    [Category(constGeneral),
     37    Description("Occurs when the properties have changed."),
     38    Browsable(true)]
     39    public event EventHandler<PropertiesEventArgs> OnShowDocumentProperties;
     40    /// <summary>
     41    /// Occurs when the properties of the canvas are exposed.
     42    /// </summary>
     43    public event EventHandler<SelectionEventArgs> OnShowCanvasProperties;
     44    /// <summary>
     45    /// Occurs when an entity is added.
     46    /// <remarks>This event usually is bubbled from one of the layers</remarks>
     47    /// </summary>
     48    [Category(constGeneral), Description("Occurs when an entity is added."), Browsable(true)]
     49    public event EventHandler<EntityEventArgs> OnEntityAdded;
     50    /// <summary>
     51    /// Occurs when an entity is removed.
     52    /// <remarks>This event usually is bubbled from one of the layers</remarks>
     53    /// </summary>
     54    [Category(constGeneral), Description("Occurs when an entity is removed."), Browsable(true)]
     55    public event EventHandler<EntityEventArgs> OnEntityRemoved;
     56    /// <summary>
     57    /// Occurs on opening a file
     58    /// </summary>
     59    [Category(constGeneral), Description("Occurs when the control will open a file."), Browsable(true)]
     60    public event EventHandler<FileEventArgs> OnOpeningDiagram;
     61    /// <summary>
     62    /// Occurs when a file was opened
     63    /// </summary>
     64    [Category(constGeneral), Description(" Occurs when a file was opened."), Browsable(true)]
     65    public event EventHandler<FileEventArgs> OnDiagramOpened;
     66    /// <summary>
     67    /// Occurs when the history has changed in the undo/redo mechanism
     68    /// </summary>
     69    public event EventHandler<HistoryChangeEventArgs> OnHistoryChange;
     70    /// <summary>
     71    /// Occurs before saving a diagram
     72    /// </summary>
     73    [Category(constGeneral), Description("Occurs before saving a diagram."), Browsable(true)]
     74    public event EventHandler<FileEventArgs> OnSavingDiagram;
     75    /// <summary>
     76    /// Occurs after saving a diagram
     77    /// </summary>
     78    [Category(constGeneral), Description("Occurs after saving a diagram."), Browsable(true)]
     79    public event EventHandler<FileEventArgs> OnDiagramSaved;
     80    /// <summary>
     81    /// Occurs when the control resets the document internally, usually through the Ctrl+N hotkey.
     82    /// </summary>
     83    [Category(constGeneral), Description("Occurs when the control resets the document internally, usually through the Ctrl+N hotkey."), Browsable(true)]
     84    public event EventHandler OnNewDiagram;
     85
     86    #region Event raisers
     87
     88    /// <summary>
     89    /// Raises the OnShowCanvasProperties event.
     90    /// </summary>
     91    protected void RaiseOnShowCanvasProperties(SelectionEventArgs e) {
     92      EventHandler<SelectionEventArgs> handler = OnShowCanvasProperties;
     93      if (handler != null) {
     94        handler(this, e);
     95      }
     96    }
     97    /// <summary>
     98    /// Raises the OnNewDiagram event.
     99    /// </summary>
     100    protected void RaiseOnNewDiagram() {
     101      EventHandler handler = OnNewDiagram;
     102      if (handler != null) {
     103        handler(this, EventArgs.Empty);
     104      }
     105    }
     106    /// <summary>
     107    /// Raises the OnHistory change.
     108    /// </summary>
     109    protected void RaiseOnHistoryChange(HistoryChangeEventArgs e) {
     110      EventHandler<HistoryChangeEventArgs> handler = OnHistoryChange;
     111      if (handler != null) {
     112        handler(this, e);
     113      }
     114    }
     115    /// <summary>
     116    /// Raises the <see cref="OnShowDocumentProperties"/> event
     117    /// </summary>
     118    /// <param name="e">Properties event argument</param>
     119    protected virtual void RaiseOnShowDocumentProperties(PropertiesEventArgs e) {
     120      EventHandler<PropertiesEventArgs> handler = OnShowDocumentProperties;
     121      if (handler != null) {
     122        handler(this, e);
     123      }
     124    }
     125    /// <summary>
     126    /// Raises the OnSavingDiagram event
     127    /// </summary>
     128    /// <param name="filePath"></param>
     129    public void RaiseOnSavingDiagram(string filePath) {
     130      if (OnSavingDiagram != null)
     131        OnSavingDiagram(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
     132    }
     133    /// <summary>
     134    /// Raises the OnShowSelectionProperties event.
     135    /// </summary>
     136    /// <param name="e">The <see cref="T:Netron.Diagramming.Core.SelectionEventArgs"/> instance containing the event data.</param>
     137    protected virtual void RaiseOnShowSelectionProperties(SelectionEventArgs e) {
     138      EventHandler<SelectionEventArgs> handler = OnShowSelectionProperties;
     139      if (handler != null) {
     140        handler(this, e);
     141      }
     142    }
     143    /// <summary>
     144    /// Raises the <see cref="OnEntityAdded"/> event.
     145    /// </summary>
     146    /// <param name="e">The <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance containing the event data.</param>
     147    protected virtual void RaiseOnEntityAdded(EntityEventArgs e) {
     148      EventHandler<EntityEventArgs> handler = OnEntityAdded;
     149      if (handler != null) {
     150        handler(this, e);
     151      }
     152    }
     153    /// <summary>
     154    /// Raises the <see cref="OnEntityRemoved"/>.
     155    /// </summary>
     156    /// <param name="e">The <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance containing the event data.</param>
     157    protected virtual void RaiseOnEntityRemoved(EntityEventArgs e) {
     158      EventHandler<EntityEventArgs> handler = OnEntityRemoved;
     159      if (handler != null) {
     160        handler(this, e);
     161      }
     162    }
     163    /// <summary>
     164    /// Raises the OnDiagramSaved event
     165    /// </summary>
     166    /// <param name="filePath"></param>
     167    public void RaiseOnDiagramSaved(string filePath) {
     168      if (OnDiagramSaved != null)
     169        OnDiagramSaved(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
     170    }
     171    /// <summary>
     172    /// Raises the OnOpeningDiagram event
     173    /// </summary>
     174    public void RaiseOnOpeningDiagram(string filePath) {
     175      if (OnOpeningDiagram != null)
     176        OnOpeningDiagram(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
     177    }
     178    /// <summary>
     179    /// Raises the OnDiagramOpened event
     180    /// </summary>
     181    public void RaiseOnDiagramOpened(string filePath) {
     182      if (OnDiagramOpened != null)
     183        OnDiagramOpened(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
     184    }
     185    #endregion
     186
     187
     188    #endregion
     189
     190    #region Fields
     191
     192    // ------------------------------------------------------------------
     193    /// <summary>
     194    /// The page settings for printing the diagram.
     195    /// </summary>
     196    // ------------------------------------------------------------------
     197    protected PageSettings mPageSettings;
     198
     199    // ------------------------------------------------------------------
     200    /// <summary>
     201    /// The filename to use when saving the diagram.
     202    /// </summary>
     203    // ------------------------------------------------------------------
     204    protected string mFileName = "";
     205
     206    // ------------------------------------------------------------------
     207    /// <summary>
     208    /// Collection of files that were opened.  The collection can be
     209    /// saved to disk by calling 'SaveRecentFiles(string path)'.
     210    /// </summary>
     211    // ------------------------------------------------------------------
     212    protected ArrayList myRecentFiles = new ArrayList();
     213
     214    // ------------------------------------------------------------------
     215    /// <summary>
     216    /// the view
     217    /// </summary>
     218    // ------------------------------------------------------------------
     219    protected IView mView;
     220
     221    // ------------------------------------------------------------------
     222    /// <summary>
     223    /// the controller
     224    /// </summary>
     225    // ------------------------------------------------------------------
     226    protected IController mController;
     227
     228    // ------------------------------------------------------------------
     229    /// <summary>
     230    /// the Document field
     231    /// </summary>
     232    // ------------------------------------------------------------------
     233    protected Document mDocument;
     234    protected bool mEnableAddConnection = true;
     235
     236
     237    #endregion
     238
     239    #region Properties
     240
     241    // ------------------------------------------------------------------
     242    /// <summary>
     243    /// Gets or sets the page settings for the entire document.
     244    /// </summary>
     245    // ------------------------------------------------------------------
     246    public PageSettings PageSettings {
     247      get {
     248        return mPageSettings;
     249      }
     250      set {
     251        mPageSettings = value;
     252
     253        // PageSettings has the page size in hundreths of an inch and
     254        // the view requires mils (thousandths of an inch).
     255        mView.PageSize = new Size(
     256            mPageSettings.PaperSize.Width * 10,
     257            mPageSettings.PaperSize.Height * 10);
     258        mView.Landscape = mPageSettings.Landscape;
     259      }
     260    }
     261
     262    // ------------------------------------------------------------------
     263    /// <summary>
     264    /// Specifies if all shape's connectors are shown.
     265    /// </summary>
     266    // ------------------------------------------------------------------
     267    public bool ShowConnectors {
     268      get {
     269        return this.mView.ShowConnectors;
     270      }
     271      set {
     272        this.mView.ShowConnectors = value;
     273      }
     274    }
     275
     276    // ------------------------------------------------------------------
     277    /// <summary>
     278    /// Gets or sets the filename to save the diagram as.
     279    /// </summary>
     280    // ------------------------------------------------------------------
     281    public string FileName {
     282      get {
     283        return mFileName;
     284      }
     285      set {
     286        mFileName = value;
     287      }
     288    }
     289
     290    /// <summary>
     291    /// Pans the diagram.
     292    /// </summary>
     293    /// <value>The pan.</value>
     294    [Browsable(false)]
     295    public Point Origin {
     296      get { return this.Controller.View.Origin; }
     297      set { this.Controller.View.Origin = value; }
     298    }
     299    /// <summary>
     300    /// Gets or sets the magnification/zoom.
     301    /// </summary>
     302    /// <value>The magnification.</value>
     303    [Browsable(false)]
     304    public SizeF Magnification {
     305      get { return this.Controller.View.Magnification; }
     306      set { this.Controller.View.Magnification = value; }
     307    }
     308
     309    /// <summary>
     310    /// Gets the selected items.
     311    /// </summary>
     312    /// <value>The selected items.</value>
     313    [Browsable(false)]
     314    public CollectionBase<IDiagramEntity> SelectedItems {
     315      get { return this.Controller.Model.Selection.SelectedItems; }
     316    }
     317
     318    /// <summary>
     319    /// Gets or sets whether to show the rulers.
     320    /// </summary>
     321    /// <value><c>true</c> to show the rulers; otherwise, <c>false</c>.</value>
     322    [Browsable(true), Description("Gets or sets whether to show the rulers.")]
     323    public bool ShowRulers {
     324      get { return View.ShowRulers; }
     325      set { View.ShowRulers = value; }
     326    }
     327
     328    /// <summary>
     329    /// Gets or sets whether a connection can be added.
     330    /// </summary>
     331    /// <value><c>true</c> if [enable add connection]; otherwise, <c>false</c>.</value>
     332    [Browsable(true), Description("Gets or sets whether the user can add new connections to the diagram."), Category("Interactions")]
     333    public bool EnableAddConnection {
     334      get { return mEnableAddConnection; }
     335      set { mEnableAddConnection = value; }
     336    }
     337
     338    #endregion
     339
     340    #region Constructor
     341    /// <summary>
     342    /// Default constructor
     343    /// </summary>
     344    protected DiagramControlBase() {
     345      // Update the display setting.
     346      Graphics g = CreateGraphics();
     347      Display.DpiX = g.DpiX;
     348      Display.DpiY = g.DpiY;
     349
     350      //create the provider for all shapy diagram elements
     351      ShapeProvider shapeProvider = new ShapeProvider();
     352      TypeDescriptor.AddProvider(
     353          shapeProvider,
     354          typeof(SimpleShapeBase));
     355
     356      TypeDescriptor.AddProvider(
     357          shapeProvider,
     358          typeof(ComplexShapeBase));
     359
     360      //the provider for connections
     361      ConnectionProvider connectionProvider =
     362          new ConnectionProvider();
     363
     364      TypeDescriptor.AddProvider(
     365          connectionProvider,
     366          typeof(ConnectionBase));
     367
     368      //scrolling stuff
     369      this.AutoScroll = true;
     370      this.HScroll = true;
     371      this.VScroll = true;
     372
     373      mPageSettings = new PageSettings();
     374      mPageSettings.Landscape = true;
     375      mPageSettings.PaperSize = new PaperSize("Letter", 850, 1100);
     376    }
     377    #endregion
     378
     379    #region Properties
     380    /// <summary>
     381    /// Gets or sets the background image displayed in the control.
     382    /// </summary>
     383    /// <value></value>
     384    /// <returns>An <see cref="T:System.Drawing.Image"></see> that represents the image to display in the background of the control.</returns>
     385    /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
     386    public override Image BackgroundImage {
     387      get {
     388        return base.BackgroundImage;
     389      }
     390      set {
     391        base.BackgroundImage = value;
     392        //TODO: change the backgroundtype
     393      }
     394    }
     395    //protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbevent)
     396    //{
     397    //    base.OnGiveFeedback(gfbevent);
     398    //    gfbevent.UseDefaultCursors = false;
     399    //    Cursor.Current = CursorPallet.DropShape;
     400    //}
     401    /// <summary>
     402    ///
     403    /// </summary>
     404    [Browsable(true), Description("The background color of the canvas if the type is set to 'flat'"), Category("Appearance")]
     405    public new Color BackColor {
     406      get {
     407
     408        return base.BackColor;
     409      }
     410      set {
     411        //communicate the change down to the model
     412        //shouldn't this be done via the controller?
     413        mDocument.Model.CurrentPage.Ambience.BackgroundColor = value;
     414        ArtPalette.DefaultPageBackgroundColor = value;
     415        base.BackColor = value;
     416        this.Invalidate();
     417      }
     418    }
     419    /// <summary>
     420    /// Gets or sets the type of the background.
     421    /// </summary>
     422    /// <value>The type of the background.</value>
     423    [Browsable(true), Description("The background type"), Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
     424    public CanvasBackgroundTypes BackgroundType {
     425      get { return mDocument.Model.CurrentPage.Ambience.BackgroundType; }
     426      set { mDocument.Model.CurrentPage.Ambience.BackgroundType = value; }
     427    }
     428
     429    /// <summary>
     430    /// Gets or sets the view.
     431    /// </summary>
     432    /// <value>The view.</value>
     433    public IView View {
     434      get {
     435        return mView;
     436      }
     437      set {
     438        mView = value;
     439      }
     440    }
     441
     442    /// <summary>
     443    /// Gets or sets the controller.
     444    /// </summary>
     445    /// <value>The controller.</value>
     446    public IController Controller {
     447      get {
     448        return mController;
     449      }
     450      set { AttachToController(value); }
     451
     452    }
     453
     454    // ------------------------------------------------------------------
     455    /// <summary>
     456    /// Gets or sets the Document.  You must call
     457    /// 'AttachToDocument(Document)' to apply a new document.
     458    /// </summary>
     459    // ------------------------------------------------------------------
     460    public virtual Document Document {
     461      get { return mDocument; }
     462      set { mDocument = value; }
     463    }
     464
     465
     466    #endregion
     467
     468    #region Methods
     469
     470    // ------------------------------------------------------------------
     471    /// <summary>
     472    /// Saves all recently opened files to the path specified.  The files
     473    /// are saved to a simple text file, one filename per line.
     474    /// </summary>
     475    /// <param name="path"></param>
     476    /// <param name="maxFiles">int: The max number of filenams to
     477    /// save.</param>
     478    // ------------------------------------------------------------------
     479    public virtual void SaveRecentlyOpenedFiles(
     480        string path,
     481        int maxFiles) {
     482      if (myRecentFiles.Count < 1) {
     483        return;
     484      }
     485
     486      StreamWriter sw = File.CreateText(path);
     487
     488      // Keep track of all files saved so we don't save duplicates.
     489      ArrayList savedFiles = new ArrayList();
     490      string fileName;
     491
     492      // Start at the last (i.e. most recent file opened) and save
     493      // until either 0 or maxFiles is reached.
     494      for (int i = myRecentFiles.Count - 1; i >= 0; i--) {
     495        if (maxFiles == 0) {
     496          break;
    100497        }
    101         /// <summary>
    102         /// Raises the OnNewDiagram event.
    103         /// </summary>
    104         protected void RaiseOnNewDiagram()
    105         {
    106             EventHandler handler = OnNewDiagram;
    107             if (handler != null)
    108             {
    109                 handler(this, EventArgs.Empty);
    110             }
     498
     499        fileName = myRecentFiles[i].ToString();
     500        if (File.Exists(fileName)) {
     501          if (savedFiles.Contains(fileName) == false) {
     502            sw.WriteLine(fileName);
     503            savedFiles.Add(fileName);
     504            maxFiles--;
     505          }
    111506        }
    112         /// <summary>
    113         /// Raises the OnHistory change.
    114         /// </summary>
    115         protected void RaiseOnHistoryChange(HistoryChangeEventArgs e)
    116         {
    117             EventHandler<HistoryChangeEventArgs> handler = OnHistoryChange;
    118             if (handler != null)
    119             {
    120                 handler(this, e);
    121             }
     507      }
     508      sw.Flush();
     509      sw.Close();
     510    }
     511
     512    // ------------------------------------------------------------------
     513    /// <summary>
     514    /// Reads the list of recently opened filenames from the
     515    /// path specified.  If the path doesn't exist or if there aren't
     516    /// any files to read, then 'null' is returned.
     517    /// </summary>
     518    /// <param name="path">string[]</param>
     519    // ------------------------------------------------------------------
     520    public virtual string[] ReadRecentlyOpenedFiles(string path) {
     521      if (File.Exists(path) == false) {
     522        return null;
     523      }
     524
     525      StreamReader sr = new StreamReader(path);
     526
     527      string text = sr.ReadToEnd();
     528      string[] filenames = text.Split("\n".ToCharArray());
     529      this.myRecentFiles.Clear();
     530      string trimmedFileName;
     531      foreach (string filename in filenames) {
     532        trimmedFileName = filename.Trim("\r".ToCharArray());
     533        if (File.Exists(trimmedFileName)) {
     534          this.myRecentFiles.Add(trimmedFileName);
    122535        }
    123         /// <summary>
    124         /// Raises the <see cref="OnShowDocumentProperties"/> event
    125         /// </summary>
    126         /// <param name="e">Properties event argument</param>
    127         protected virtual void RaiseOnShowDocumentProperties(PropertiesEventArgs e)
    128         {
    129             EventHandler<PropertiesEventArgs> handler = OnShowDocumentProperties;
    130             if (handler != null)
    131             {
    132                 handler(this, e);
    133             }
     536      }
     537
     538      if (this.myRecentFiles.Count > 0) {
     539        return (string[])myRecentFiles.ToArray(typeof(string));
     540      } else {
     541        return null;
     542      }
     543    }
     544
     545    // ------------------------------------------------------------------
     546    /// <summary>
     547    /// Sets the layout root.
     548    /// </summary>
     549    /// <param name="shape">The shape.</param>
     550    // ------------------------------------------------------------------
     551    public virtual void SetLayoutRoot(IShape shape) {
     552      this.Controller.Model.LayoutRoot = shape;
     553    }
     554
     555    // ------------------------------------------------------------------
     556    /// <summary>
     557    /// Runs the activity.
     558    /// </summary>
     559    /// <param name="activityName">Name of the activity.</param>
     560    // ------------------------------------------------------------------
     561    public virtual void RunActivity(string activityName) {
     562      this.Controller.RunActivity(activityName);
     563    }
     564
     565    // ------------------------------------------------------------------
     566    /// <summary>
     567    /// Handles the OnEntityRemoved event of the Controller control.
     568    /// </summary>
     569    /// <param name="sender">The source of the event.</param>
     570    /// <param name="e">The
     571    /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
     572    /// containing the event data.</param>
     573    // ------------------------------------------------------------------
     574    protected virtual void HandleOnEntityRemoved(
     575        object sender,
     576        EntityEventArgs e) {
     577      RaiseOnEntityRemoved(e);
     578    }
     579
     580    // ------------------------------------------------------------------
     581    /// <summary>
     582    /// Handles the OnEntityAdded event of the Controller control.
     583    /// </summary>
     584    /// <param name="sender">The source of the event.</param>
     585    /// <param name="e">The
     586    /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
     587    /// containing the event data.</param>
     588    // ------------------------------------------------------------------
     589    protected virtual void HandleOnEntityAdded(
     590        object sender,
     591        EntityEventArgs e) {
     592      RaiseOnEntityAdded(e);
     593    }
     594
     595    // ------------------------------------------------------------------
     596    /// <summary>
     597    /// Handles the OnBackColorChange event of the View control.
     598    /// </summary>
     599    /// <param name="sender">The source of the event.</param>
     600    /// <param name="e">The
     601    /// <see cref="T:Netron.Diagramming.Core.ColorEventArgs"/> instance
     602    /// containing the event data.</param>
     603    // ------------------------------------------------------------------
     604    protected void View_OnBackColorChange(object sender, ColorEventArgs e) {
     605      base.BackColor = e.Color;
     606    }
     607
     608    // ------------------------------------------------------------------
     609    /// <summary>
     610    /// Bubbles the OnHistoryChange event from the Controller to the surface
     611    /// </summary>
     612    /// <param name="sender">The source of the event.</param>
     613    /// <param name="e">The
     614    /// <see cref="T:Netron.Diagramming.Core.HistoryChangeEventArgs"/>
     615    /// instance containing the event data.</param>
     616    // ------------------------------------------------------------------
     617    void mController_OnHistoryChange(
     618        object sender,
     619        HistoryChangeEventArgs e) {
     620      RaiseOnHistoryChange(e);
     621    }
     622
     623    void Controller_OnShowSelectionProperties(object sender, SelectionEventArgs e) {
     624      RaiseOnShowSelectionProperties(e);
     625    }
     626
     627    // ------------------------------------------------------------------
     628    /// <summary>
     629    /// Attaches to the controller specified and registers for all
     630    /// required events to update this control from the controller.
     631    /// </summary>
     632    /// <param name="controller">IController</param>
     633    // ------------------------------------------------------------------
     634    protected virtual void AttachToController(IController controller) {
     635
     636      if (controller == null)
     637        throw new ArgumentNullException();
     638      mController = controller;
     639      mController.OnHistoryChange +=
     640          new EventHandler<HistoryChangeEventArgs>(
     641          mController_OnHistoryChange);
     642
     643      mController.OnShowSelectionProperties +=
     644          new EventHandler<SelectionEventArgs>(
     645          Controller_OnShowSelectionProperties);
     646
     647      mController.OnEntityAdded +=
     648          new EventHandler<EntityEventArgs>(
     649          HandleOnEntityAdded);
     650
     651      mController.OnEntityRemoved +=
     652          new EventHandler<EntityEventArgs>(
     653          HandleOnEntityRemoved);
     654    }
     655
     656    // ------------------------------------------------------------------
     657    /// <summary>
     658    /// Resets the document and underlying model.
     659    /// </summary>
     660    // ------------------------------------------------------------------
     661    public virtual void NewDocument() {
     662      // Lets see, we really only want to ask the user if they want to
     663      // save their changes if they don't have changes to save.
     664      // We can check the un/redo mananger to see if there are changes.
     665      if ((this.mController.UndoManager.CanRedo()) ||
     666          (this.mController.UndoManager.CanUndo())) {
     667        DialogResult result = MessageBox.Show(
     668            "Save changes before clearing the current diagram?",
     669             "Confirm Clear",
     670             MessageBoxButtons.YesNoCancel,
     671             MessageBoxIcon.Question);
     672
     673        if (result == DialogResult.Yes) {
     674          this.Save();
     675        } else if (result == DialogResult.Cancel) {
     676          return;
    134677        }
    135         /// <summary>
    136         /// Raises the OnSavingDiagram event
    137         /// </summary>
    138         /// <param name="filePath"></param>
    139         public void RaiseOnSavingDiagram(string filePath)
    140         {
    141             if(OnSavingDiagram != null)
    142                 OnSavingDiagram(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
     678      }
     679      Document = new Document();
     680      this.mController.UndoManager.ClearUndoRedo();
     681      AttachToDocument(Document);
     682      this.mFileName = "";
     683      RaiseOnNewDiagram();
     684      //this.Controller.Model.Clear();           
     685    }
     686
     687    #region Printing
     688
     689    // ------------------------------------------------------------------
     690    /// <summary>
     691    /// Displays a PageSetupDialog so the user can specify how each
     692    /// page is printed.
     693    /// </summary>
     694    // ------------------------------------------------------------------
     695    public void PageSetup() {
     696      PageSetupDialog dialog = new PageSetupDialog();
     697      dialog.PageSettings = this.PageSettings;
     698      if (dialog.ShowDialog() == DialogResult.OK) {
     699        this.PageSettings = dialog.PageSettings;
     700      }
     701    }
     702
     703    // ------------------------------------------------------------------
     704    /// <summary>
     705    /// Prints all pages of the diagram.
     706    /// </summary>
     707    // ------------------------------------------------------------------
     708    public void Print() {
     709      DiagramPrinter myPrinter = new DiagramPrinter(
     710          this.PageSettings,
     711          this,
     712          false);
     713    }
     714
     715    // ------------------------------------------------------------------
     716    /// <summary>
     717    /// Print previews all pages of the diagram.
     718    /// </summary>
     719    // ------------------------------------------------------------------
     720    public void PrintPreview() {
     721      DiagramPrinter myPrinter = new DiagramPrinter(
     722          this.PageSettings,
     723          this,
     724          true);
     725    }
     726
     727    #endregion
     728
     729    #region IO
     730    // ------------------------------------------------------------------
     731    /// <summary>
     732    /// If the current filename (FileName property) is empty, then a
     733    /// SaveFileDialog is displayed for the user to specify what to save
     734    /// the diagram as.  Otherwise, the current filename is used to save
     735    /// the diagram.
     736    /// </summary>
     737    // ------------------------------------------------------------------
     738    public virtual void Save() {
     739      try {
     740        if (this.mFileName == "") {
     741          SaveFileDialog dialog = new SaveFileDialog();
     742          if (dialog.ShowDialog() == DialogResult.OK) {
     743            this.SaveAs(dialog.FileName);
     744          }
     745        } else {
     746          this.SaveAs(this.mFileName);
    143747        }
    144         /// <summary>
    145         /// Raises the OnShowSelectionProperties event.
    146         /// </summary>
    147         /// <param name="e">The <see cref="T:Netron.Diagramming.Core.SelectionEventArgs"/> instance containing the event data.</param>
    148         protected virtual void RaiseOnShowSelectionProperties(SelectionEventArgs e)
    149         {
    150             EventHandler<SelectionEventArgs> handler = OnShowSelectionProperties;
    151             if (handler != null)
    152             {
    153                 handler(this, e);
    154             }
    155         }
    156         /// <summary>
    157         /// Raises the <see cref="OnEntityAdded"/> event.
    158         /// </summary>
    159         /// <param name="e">The <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance containing the event data.</param>
    160         protected virtual void RaiseOnEntityAdded(EntityEventArgs e)
    161         {
    162             EventHandler<EntityEventArgs> handler = OnEntityAdded;
    163             if (handler != null)
    164             {
    165                 handler(this, e);
    166             }
    167         }
    168         /// <summary>
    169         /// Raises the <see cref="OnEntityRemoved"/>.
    170         /// </summary>
    171         /// <param name="e">The <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance containing the event data.</param>
    172         protected virtual void RaiseOnEntityRemoved(EntityEventArgs e)
    173         {
    174             EventHandler<EntityEventArgs> handler = OnEntityRemoved;
    175             if (handler != null)
    176             {
    177                 handler(this, e);
    178             }
    179         }
    180         /// <summary>
    181         /// Raises the OnDiagramSaved event
    182         /// </summary>
    183         /// <param name="filePath"></param>
    184         public void RaiseOnDiagramSaved(string filePath)
    185         {
    186             if(OnDiagramSaved != null)
    187                 OnDiagramSaved(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
    188         }
    189         /// <summary>
    190         /// Raises the OnOpeningDiagram event
    191         /// </summary>
    192         public void RaiseOnOpeningDiagram(string filePath)
    193         {
    194             if(OnOpeningDiagram != null)
    195                 OnOpeningDiagram(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
    196         }
    197         /// <summary>
    198         /// Raises the OnDiagramOpened event
    199         /// </summary>
    200         public void RaiseOnDiagramOpened(string filePath)
    201         {
    202             if(OnDiagramOpened != null)
    203                 OnDiagramOpened(this, new FileEventArgs(new System.IO.FileInfo(filePath)));
    204         }
    205         #endregion
    206 
    207        
    208         #endregion
    209 
    210         #region Fields
    211 
    212         // ------------------------------------------------------------------
    213         /// <summary>
    214         /// The page settings for printing the diagram.
    215         /// </summary>
    216         // ------------------------------------------------------------------
    217         protected PageSettings mPageSettings;
    218 
    219         // ------------------------------------------------------------------
    220         /// <summary>
    221         /// The filename to use when saving the diagram.
    222         /// </summary>
    223         // ------------------------------------------------------------------
    224         protected string mFileName = "";
    225 
    226         // ------------------------------------------------------------------
    227         /// <summary>
    228         /// Collection of files that were opened.  The collection can be
    229         /// saved to disk by calling 'SaveRecentFiles(string path)'.
    230         /// </summary>
    231         // ------------------------------------------------------------------
    232         protected ArrayList myRecentFiles = new ArrayList();
    233 
    234         // ------------------------------------------------------------------
    235         /// <summary>
    236         /// the view
    237         /// </summary>
    238         // ------------------------------------------------------------------
    239         protected IView mView;
    240 
    241         // ------------------------------------------------------------------
    242         /// <summary>
    243         /// the controller
    244         /// </summary>
    245         // ------------------------------------------------------------------
    246         protected IController mController;
    247 
    248         // ------------------------------------------------------------------
    249         /// <summary>
    250         /// the Document field
    251         /// </summary>
    252         // ------------------------------------------------------------------
    253         protected Document mDocument;
    254         protected bool mEnableAddConnection = true;
    255 
    256        
    257         #endregion
    258 
    259         #region Properties
    260 
    261         // ------------------------------------------------------------------
    262         /// <summary>
    263         /// Gets or sets the page settings for the entire document.
    264         /// </summary>
    265         // ------------------------------------------------------------------
    266         public PageSettings PageSettings
    267         {
    268             get
    269             {
    270                 return mPageSettings;
    271             }
    272             set
    273             {
    274                 mPageSettings = value;
    275 
    276                 // PageSettings has the page size in hundreths of an inch and
    277                 // the view requires mils (thousandths of an inch).
    278                 mView.PageSize = new Size(
    279                     mPageSettings.PaperSize.Width * 10,
    280                     mPageSettings.PaperSize.Height * 10);
    281                 mView.Landscape = mPageSettings.Landscape;
    282             }
    283         }
    284 
    285         // ------------------------------------------------------------------
    286         /// <summary>
    287         /// Specifies if all shape's connectors are shown.
    288         /// </summary>
    289         // ------------------------------------------------------------------
    290         public bool ShowConnectors
    291         {
    292             get
    293             {
    294                 return this.mView.ShowConnectors;
    295             }
    296             set
    297             {
    298                 this.mView.ShowConnectors = value;
    299             }
    300         }
    301 
    302         // ------------------------------------------------------------------
    303         /// <summary>
    304         /// Gets or sets the filename to save the diagram as.
    305         /// </summary>
    306         // ------------------------------------------------------------------
    307         public string FileName
    308         {
    309             get
    310             {
    311                 return mFileName;
    312             }
    313             set
    314             {
    315                 mFileName = value;
    316             }
    317         }
    318      
    319         /// <summary>
    320         /// Pans the diagram.
    321         /// </summary>
    322         /// <value>The pan.</value>
    323         [Browsable(false)]
    324         public Point Origin
    325         {
    326             get { return this.Controller.View.Origin; }
    327             set { this.Controller.View.Origin = value; }
    328         }
    329         /// <summary>
    330         /// Gets or sets the magnification/zoom.
    331         /// </summary>
    332         /// <value>The magnification.</value>
    333         [Browsable(false)]
    334         public SizeF Magnification
    335         {
    336             get { return this.Controller.View.Magnification; }
    337             set { this.Controller.View.Magnification = value; }
    338         }
    339 
    340         /// <summary>
    341         /// Gets the selected items.
    342         /// </summary>
    343         /// <value>The selected items.</value>
    344         [Browsable(false)]
    345         public CollectionBase<IDiagramEntity> SelectedItems
    346         {
    347             get { return Selection.SelectedItems; }
    348         }
    349 
    350         /// <summary>
    351         /// Gets or sets whether to show the rulers.
    352         /// </summary>
    353         /// <value><c>true</c> to show the rulers; otherwise, <c>false</c>.</value>
    354         [Browsable(true), Description("Gets or sets whether to show the rulers.")]
    355         public bool ShowRulers
    356         {
    357             get { return View.ShowRulers; }
    358             set { View.ShowRulers = value; }
    359         }
    360 
    361         /// <summary>
    362         /// Gets or sets whether a connection can be added.
    363         /// </summary>
    364         /// <value><c>true</c> if [enable add connection]; otherwise, <c>false</c>.</value>
    365         [Browsable(true), Description("Gets or sets whether the user can add new connections to the diagram."), Category("Interactions")]
    366         public bool EnableAddConnection
    367         {
    368             get { return mEnableAddConnection; }
    369             set { mEnableAddConnection = value; }
    370         }
    371        
    372         #endregion
    373 
    374         #region Constructor
    375         /// <summary>
    376         /// Default constructor
    377         /// </summary>
    378         protected DiagramControlBase()
    379         {
    380             // Update the display setting.
    381             Graphics g = CreateGraphics();
    382             Display.DpiX = g.DpiX;
    383             Display.DpiY = g.DpiY;
    384 
    385             //create the provider for all shapy diagram elements
    386             ShapeProvider shapeProvider = new ShapeProvider();
    387             TypeDescriptor.AddProvider(
    388                 shapeProvider,
    389                 typeof(SimpleShapeBase));
    390            
    391             TypeDescriptor.AddProvider(
    392                 shapeProvider,
    393                 typeof(ComplexShapeBase)); 
    394 
    395             //the provider for connections
    396             ConnectionProvider connectionProvider =
    397                 new ConnectionProvider();
    398 
    399             TypeDescriptor.AddProvider(
    400                 connectionProvider,
    401                 typeof(ConnectionBase));
    402 
    403             //scrolling stuff
    404             this.AutoScroll = true;
    405             this.HScroll = true;
    406             this.VScroll = true;
    407 
    408             mPageSettings = new PageSettings();
    409             mPageSettings.Landscape = true;
    410             mPageSettings.PaperSize = new PaperSize("Letter", 850, 1100);
    411         }
    412         #endregion
    413 
    414         #region Properties
    415         /// <summary>
    416         /// Gets or sets the background image displayed in the control.
    417         /// </summary>
    418         /// <value></value>
    419         /// <returns>An <see cref="T:System.Drawing.Image"></see> that represents the image to display in the background of the control.</returns>
    420         /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
    421         public override Image BackgroundImage
    422         {
    423             get
    424             {
    425                 return base.BackgroundImage;
    426             }
    427             set
    428             {
    429                 base.BackgroundImage = value;
    430                 //TODO: change the backgroundtype
    431             }
    432         }
    433         //protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbevent)
    434         //{
    435         //    base.OnGiveFeedback(gfbevent);
    436         //    gfbevent.UseDefaultCursors = false;
    437         //    Cursor.Current = CursorPallet.DropShape;
    438         //}
    439         /// <summary>
    440         ///
    441         /// </summary>
    442         [Browsable(true), Description("The background color of the canvas if the type is set to 'flat'"), Category("Appearance")]
    443         public new Color BackColor
    444         {
    445             get
    446             {               
    447                
    448                 return base.BackColor;
    449             }
    450             set
    451             {
    452                 //communicate the change down to the model
    453                 //shouldn't this be done via the controller?
    454                 mDocument.Model.CurrentPage.Ambience.BackgroundColor = value;
    455                 ArtPalette.DefaultPageBackgroundColor = value;
    456                 base.BackColor = value;
    457                 this.Invalidate();
    458             }
    459         }
    460         /// <summary>
    461         /// Gets or sets the type of the background.
    462         /// </summary>
    463         /// <value>The type of the background.</value>
    464         [Browsable(true), Description("The background type"), Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    465         public CanvasBackgroundTypes BackgroundType
    466         {
    467             get { return mDocument.Model.CurrentPage.Ambience.BackgroundType; }
    468             set { mDocument.Model.CurrentPage.Ambience.BackgroundType = value; }
    469         }
    470 
    471         /// <summary>
    472         /// Gets or sets the view.
    473         /// </summary>
    474         /// <value>The view.</value>
    475         public IView View
    476         {
    477             get
    478             {
    479                 return mView;
    480             }
    481             set
    482             {
    483                 mView = value;
    484             }
    485         }
    486 
    487         /// <summary>
    488         /// Gets or sets the controller.
    489         /// </summary>
    490         /// <value>The controller.</value>
    491         public  IController Controller
    492         {
    493             get
    494             {
    495                 return mController;
    496             }
    497             set { AttachToController(value); }
    498            
    499         }
    500 
    501         // ------------------------------------------------------------------
    502         /// <summary>
    503         /// Gets or sets the Document.  You must call
    504         /// 'AttachToDocument(Document)' to apply a new document.
    505         /// </summary>
    506         // ------------------------------------------------------------------
    507         public virtual Document Document
    508         {
    509             get { return mDocument; }
    510             set { mDocument = value; }
    511         }
    512 
    513 
    514         #endregion
    515 
    516         #region Methods
    517 
    518         // ------------------------------------------------------------------
    519         /// <summary>
    520         /// Saves all recently opened files to the path specified.  The files
    521         /// are saved to a simple text file, one filename per line.
    522         /// </summary>
    523         /// <param name="path"></param>
    524         /// <param name="maxFiles">int: The max number of filenams to
    525         /// save.</param>
    526         // ------------------------------------------------------------------
    527         public virtual void SaveRecentlyOpenedFiles(
    528             string path,
    529             int maxFiles)
    530         {
    531             if (myRecentFiles.Count < 1)
    532             {
    533                 return;
    534             }
    535 
    536             StreamWriter sw = File.CreateText(path);
    537 
    538             // Keep track of all files saved so we don't save duplicates.
    539             ArrayList savedFiles = new ArrayList();
    540             string fileName;
    541 
    542             // Start at the last (i.e. most recent file opened) and save
    543             // until either 0 or maxFiles is reached.
    544             for (int i = myRecentFiles.Count - 1; i >= 0; i--)
    545             {
    546                 if (maxFiles == 0)
    547                 {
    548                     break;
    549                 }
    550 
    551                 fileName = myRecentFiles[i].ToString();
    552                 if (File.Exists(fileName))
    553                 {
    554                     if (savedFiles.Contains(fileName) == false)
    555                     {
    556                         sw.WriteLine(fileName);
    557                         savedFiles.Add(fileName);
    558                         maxFiles--;
    559                     }
    560                 }
    561             }
    562             sw.Flush();
    563             sw.Close();
    564         }
    565 
    566         // ------------------------------------------------------------------
    567         /// <summary>
    568         /// Reads the list of recently opened filenames from the
    569         /// path specified.  If the path doesn't exist or if there aren't
    570         /// any files to read, then 'null' is returned.
    571         /// </summary>
    572         /// <param name="path">string[]</param>
    573         // ------------------------------------------------------------------
    574         public virtual string[] ReadRecentlyOpenedFiles(string path)
    575         {
    576             if (File.Exists(path) == false)
    577             {
    578                 return null;
    579             }
    580 
    581             StreamReader sr = new StreamReader(path);
    582            
    583             string text = sr.ReadToEnd();
    584             string[] filenames = text.Split("\n".ToCharArray());
    585             this.myRecentFiles.Clear();
    586             string trimmedFileName;
    587             foreach (string filename in filenames)
    588             {
    589                 trimmedFileName = filename.Trim("\r".ToCharArray());
    590                 if (File.Exists(trimmedFileName))
    591                 {
    592                     this.myRecentFiles.Add(trimmedFileName);
    593                 }
    594             }
    595 
    596             if (this.myRecentFiles.Count > 0)
    597             {
    598                 return (string[])myRecentFiles.ToArray(typeof(string));
    599             }
    600             else
    601             {
    602                 return null;
    603             }
    604         }
    605 
    606         // ------------------------------------------------------------------
    607         /// <summary>
    608         /// Sets the layout root.
    609         /// </summary>
    610         /// <param name="shape">The shape.</param>
    611         // ------------------------------------------------------------------
    612         public virtual void SetLayoutRoot(IShape shape)
    613         {           
    614             this.Controller.Model.LayoutRoot = shape;
    615         }
    616 
    617         // ------------------------------------------------------------------
    618         /// <summary>
    619         /// Runs the activity.
    620         /// </summary>
    621         /// <param name="activityName">Name of the activity.</param>
    622         // ------------------------------------------------------------------
    623         public virtual void RunActivity(string  activityName)
    624         {
    625             this.Controller.RunActivity(activityName);
    626         }
    627 
    628         // ------------------------------------------------------------------
    629         /// <summary>
    630         /// Handles the OnEntityRemoved event of the Controller control.
    631         /// </summary>
    632         /// <param name="sender">The source of the event.</param>
    633         /// <param name="e">The
    634         /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
    635         /// containing the event data.</param>
    636         // ------------------------------------------------------------------
    637         protected virtual void HandleOnEntityRemoved(
    638             object sender,
    639             EntityEventArgs e)
    640         {
    641             RaiseOnEntityRemoved(e);
    642         }
    643 
    644         // ------------------------------------------------------------------
    645         /// <summary>
    646         /// Handles the OnEntityAdded event of the Controller control.
    647         /// </summary>
    648         /// <param name="sender">The source of the event.</param>
    649         /// <param name="e">The
    650         /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
    651         /// containing the event data.</param>
    652         // ------------------------------------------------------------------
    653         protected virtual void HandleOnEntityAdded(
    654             object sender,
    655             EntityEventArgs e)
    656         {
    657             RaiseOnEntityAdded(e);
    658         }
    659 
    660         // ------------------------------------------------------------------
    661         /// <summary>
    662         /// Handles the OnBackColorChange event of the View control.
    663         /// </summary>
    664         /// <param name="sender">The source of the event.</param>
    665         /// <param name="e">The
    666         /// <see cref="T:Netron.Diagramming.Core.ColorEventArgs"/> instance
    667         /// containing the event data.</param>
    668         // ------------------------------------------------------------------
    669         protected void View_OnBackColorChange(object sender, ColorEventArgs e)
    670         {
    671             base.BackColor = e.Color;
    672         }
    673 
    674         // ------------------------------------------------------------------
    675         /// <summary>
    676         /// Bubbles the OnHistoryChange event from the Controller to the surface
    677         /// </summary>
    678         /// <param name="sender">The source of the event.</param>
    679         /// <param name="e">The
    680         /// <see cref="T:Netron.Diagramming.Core.HistoryChangeEventArgs"/>
    681         /// instance containing the event data.</param>
    682         // ------------------------------------------------------------------
    683         void mController_OnHistoryChange(
    684             object sender,
    685             HistoryChangeEventArgs e)
    686         {
    687             RaiseOnHistoryChange(e);
    688         }
    689 
    690         void Controller_OnShowSelectionProperties(object sender, SelectionEventArgs e)
    691         {
    692             RaiseOnShowSelectionProperties(e);
    693         }
    694 
    695         // ------------------------------------------------------------------
    696         /// <summary>
    697         /// Attaches to the controller specified and registers for all
    698         /// required events to update this control from the controller.
    699         /// </summary>
    700         /// <param name="controller">IController</param>
    701         // ------------------------------------------------------------------
    702         protected virtual void AttachToController(IController controller)
    703         {
    704            
    705             if (controller == null)
    706                 throw new ArgumentNullException();
    707             mController = controller;
    708             mController.OnHistoryChange +=
    709                 new EventHandler<HistoryChangeEventArgs>(
    710                 mController_OnHistoryChange);
    711 
    712             mController.OnShowSelectionProperties +=
    713                 new EventHandler<SelectionEventArgs>(
    714                 Controller_OnShowSelectionProperties);
    715 
    716             mController.OnEntityAdded +=
    717                 new EventHandler<EntityEventArgs>(
    718                 HandleOnEntityAdded);
    719 
    720             mController.OnEntityRemoved +=
    721                 new EventHandler<EntityEventArgs>(
    722                 HandleOnEntityRemoved);
    723         }
    724 
    725         // ------------------------------------------------------------------
    726         /// <summary>
    727         /// Resets the document and underlying model.
    728         /// </summary>
    729         // ------------------------------------------------------------------
    730         public virtual void NewDocument()
    731         {
    732             // Lets see, we really only want to ask the user if they want to
    733             // save their changes if they don't have changes to save.
    734             // We can check the un/redo mananger to see if there are changes.
    735             if ((this.mController.UndoManager.CanRedo()) ||
    736                 (this.mController.UndoManager.CanUndo()))
    737             {
    738                 DialogResult result = MessageBox.Show(
    739                     "Save changes before clearing the current diagram?",
    740                      "Confirm Clear",
    741                      MessageBoxButtons.YesNoCancel,
    742                      MessageBoxIcon.Question);
    743 
    744                 if (result == DialogResult.Yes)
    745                 {
    746                     this.Save();
    747                 }
    748                 else if (result == DialogResult.Cancel)
    749                 {
    750                     return;
    751                 }
    752             }
    753             Document = new Document();
    754             this.mController.UndoManager.ClearUndoRedo();
    755             AttachToDocument(Document);
    756             this.mFileName = "";
    757             RaiseOnNewDiagram();
    758             //this.Controller.Model.Clear();           
    759         }
    760 
    761         #region Printing
    762 
    763         // ------------------------------------------------------------------
    764         /// <summary>
    765         /// Displays a PageSetupDialog so the user can specify how each
    766         /// page is printed.
    767         /// </summary>
    768         // ------------------------------------------------------------------
    769         public void PageSetup()
    770         {
    771             PageSetupDialog dialog = new PageSetupDialog();
    772             dialog.PageSettings = this.PageSettings;
    773             if (dialog.ShowDialog() == DialogResult.OK)
    774             {
    775                 this.PageSettings = dialog.PageSettings;
    776             }
    777         }
    778 
    779         // ------------------------------------------------------------------
    780         /// <summary>
    781         /// Prints all pages of the diagram.
    782         /// </summary>
    783         // ------------------------------------------------------------------
    784         public void Print()
    785         {
    786             DiagramPrinter myPrinter = new DiagramPrinter(
    787                 this.PageSettings,
    788                 this,
    789                 false);
    790         }
    791 
    792         // ------------------------------------------------------------------
    793         /// <summary>
    794         /// Print previews all pages of the diagram.
    795         /// </summary>
    796         // ------------------------------------------------------------------
    797         public void PrintPreview()
    798         {
    799             DiagramPrinter myPrinter = new DiagramPrinter(
    800                 this.PageSettings,
    801                 this,
    802                 true);
    803         }
    804 
    805         #endregion
    806 
    807         #region IO
    808         // ------------------------------------------------------------------
    809         /// <summary>
    810         /// If the current filename (FileName property) is empty, then a
    811         /// SaveFileDialog is displayed for the user to specify what to save
    812         /// the diagram as.  Otherwise, the current filename is used to save
    813         /// the diagram.
    814         /// </summary>
    815         // ------------------------------------------------------------------
    816         public virtual void Save()
    817         {
    818             try
    819             {
    820                 if (this.mFileName == "")
    821                 {
    822                     SaveFileDialog dialog = new SaveFileDialog();
    823                     if (dialog.ShowDialog() == DialogResult.OK)
    824                     {
    825                         this.SaveAs(dialog.FileName);
    826                     }
    827                 }
    828                 else
    829                 {
    830                     this.SaveAs(this.mFileName);
    831                 }
    832             }
    833             catch (Exception ex)
    834             {
    835                 MessageBox.Show("An error occured while saving the " +
    836                     "diagram.  See below for the message about the " +
    837                     "error.\n\n" +
    838                     "Message: " + ex.Message,
    839                     "Save Diagram Error",
    840                     MessageBoxButtons.OK,
    841                     MessageBoxIcon.Error);
    842             }
    843         }
    844 
    845         // ------------------------------------------------------------------
    846         /// <summary>
    847         /// Displays a SaveFileDialog regardless if there's an existing
    848         /// filename so the user can save the diagram to a new location.
    849         /// </summary>
    850         // ------------------------------------------------------------------
    851         public virtual void SaveAs()
    852         {
    853             SaveFileDialog dialog = new SaveFileDialog();
    854 
    855             if (dialog.ShowDialog() == DialogResult.OK)
    856             {
    857                 SaveAs(dialog.FileName);
    858             }
    859         }
    860 
    861         // ------------------------------------------------------------------
    862         /// <summary>
    863         /// Saves the diagram to the given location.
    864         /// </summary>
    865         /// <param name="path">The path.</param>
    866         // ------------------------------------------------------------------
    867         public virtual void SaveAs(string path)
    868         {
    869             if (!Directory.Exists(Path.GetDirectoryName(path)))
    870             {
    871                 throw new DirectoryNotFoundException(
    872                     "Create the directory before saving the diagram to it.");
    873             }
    874             RaiseOnSavingDiagram(path);
    875             if (BinarySerializer.SaveAs(path, this) == true)
    876             {
    877                 // Store the filename if the save was successful.
    878                 this.mFileName = path;
    879 
    880                 // Clear the undo/redo history.
    881                 this.mController.UndoManager.ClearUndoRedo();
    882                 RaiseOnDiagramSaved(path);
    883             }
    884         }
    885 
    886         // ------------------------------------------------------------------
    887         /// <summary>
    888         /// Displays an OpenFileDialog for the user to specify the diagram
    889         /// to open.
    890         /// </summary>
    891         // ------------------------------------------------------------------
    892         public virtual void Open()
    893         {
    894             OpenFileDialog dialog = new OpenFileDialog();
    895             if (dialog.ShowDialog() == DialogResult.OK)
    896             {
    897                 this.Open(dialog.FileName);
    898             }
    899         }
    900 
    901         // ------------------------------------------------------------------
    902         /// <summary>
    903         /// Opens the diagram from the specified path.
    904         /// </summary>
    905         /// <param name="path">The path.</param>
    906         // ------------------------------------------------------------------
    907         public virtual void Open(string path)
    908         {
    909             if (!Directory.Exists(Path.GetDirectoryName(path)))
    910             {
    911                 throw new DirectoryNotFoundException(
    912                     "Create the directory before saving the diagram to it.");
    913             }
    914             this.NewDocument();//makes it possible for the host to ask for
    915             // saving first.
    916 
    917             RaiseOnOpeningDiagram(path);//do it before opening the new diagram.
    918             if (BinarySerializer.Open(path, this) == true)
    919             {
    920                 // Store the filename if successful.
    921                 this.mFileName = path;
    922 
    923                 // Add the filename to the list of previously opened files.
    924                 this.myRecentFiles.Add(path);
    925 
    926                 // Raise that a new diagram was opened.
    927                 RaiseOnDiagramOpened(path);
    928             }
    929             //this.mFileName = mFileName;
    930         }
    931         #endregion
    932 
    933         // ------------------------------------------------------------------
    934         /// <summary>
    935         /// Attachement to the document.
    936         /// </summary>
    937         /// <param name="document">The document.</param>
    938         // ------------------------------------------------------------------
    939         public virtual void AttachToDocument(Document document)
    940         {
    941 
    942             if(document == null)
    943                 throw new ArgumentNullException();
    944 
    945             this.mDocument = document;
    946 
    947             #region re-attach to the new model
    948             View.Model = Document.Model;
    949             Controller.Model = Document.Model;
    950             Selection.Controller = Controller;
    951             #endregion
    952             #region Update the ambience
    953             document.Model.SetCurrentPage(0);
    954             #endregion
    955            
    956             Selection.Clear();
    957             this.Invalidate();
    958         }
    959 
    960         // ------------------------------------------------------------------
    961         /// <summary>
    962         /// Activates a registered tool with the given name
    963         /// </summary>
    964         /// <param name="toolName">the name of a tool</param>
    965         // ------------------------------------------------------------------
    966         [System.Diagnostics.CodeAnalysis.SuppressMessage(
    967             "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
    968         public void ActivateTool(string toolName)
    969         {
    970             if (toolName == null || toolName.Trim().Length == 0)
    971             {
    972                 throw new ArgumentNullException("The tool name cannot " +
    973                     "be 'null' or empty.");
    974             }
    975 
    976             if (this.Controller == null)
    977             {
    978                 throw new InconsistencyException(
    979                     "The Controller of the surface is 'null', this is a " +
    980                     "strong inconsistency in the MVC model.");
    981             }
    982             if(toolName.Trim().Length>0)
    983                 this.Controller.ActivateTool(toolName);
    984         }
    985 
    986         // ------------------------------------------------------------------
    987         /// <summary>
    988         /// Starts the tool with the given name.
    989         /// </summary>
    990         /// <param name="toolName">the name of a registered tool</param>
    991         // ------------------------------------------------------------------
    992         public void LaunchTool(string toolName)
    993         {
    994             if (this.Controller == null) return;
    995             this.Controller.ActivateTool(toolName);
    996         }
    997 
    998         // ------------------------------------------------------------------
    999         /// <summary>
    1000         /// Overrides the base method to call the view which will paint the
    1001         /// diagram on the given graphics surface.
    1002         /// </summary>
    1003         /// <param name="e">PaintEventArgs</param>
    1004         // ------------------------------------------------------------------
    1005         protected override void OnPaint(PaintEventArgs e)
    1006         {
    1007             base.OnPaint(e);
    1008             mView.Paint(e.Graphics);
    1009         }
    1010 
    1011         // ------------------------------------------------------------------
    1012         /// <summary>
    1013         /// Paints the background of the control.
    1014         /// </summary>
    1015         /// <param name="pevent">A
    1016         /// <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that
    1017         /// contains information about the control to paint.</param>
    1018         // ------------------------------------------------------------------
    1019         protected override void OnPaintBackground(PaintEventArgs pevent)
    1020         {
    1021             base.OnPaintBackground(pevent);
    1022            
    1023             mView.PaintBackground(pevent.Graphics);
    1024         }
    1025 
    1026         // ------------------------------------------------------------------
    1027         /// <summary>
    1028         /// Undoes the last action that was recorded in the UndoManager.
    1029         /// </summary>
    1030         // ------------------------------------------------------------------
    1031         public void Undo()
    1032         {
    1033             this.Controller.Undo();
    1034         }
    1035 
    1036         // ------------------------------------------------------------------
    1037         /// <summary>
    1038         /// Redoes the last action that was recorded in the UndoManager.
    1039         /// </summary>
    1040         // ------------------------------------------------------------------
    1041         public void Redo()
    1042         {
    1043             this.Controller.Redo();
    1044         }
    1045         #endregion
    1046 
    1047         #region Explicit ISupportInitialize implementation
    1048         void ISupportInitialize.BeginInit()
    1049         {
    1050             //here you can check the conformity of properties
    1051             BeginInit();
    1052         }
    1053 
    1054         /// <summary>
    1055         /// Signals the object that initialization is complete.
    1056         /// </summary>
    1057         void ISupportInitialize.EndInit()
    1058         {
    1059             EndInit();
    1060         }
    1061 
    1062         /// <summary>
    1063         /// Signals the object that initialization is starting.
    1064         /// </summary>
    1065         protected virtual void BeginInit()
    1066         {
    1067 
    1068         }
    1069         /// <summary>
    1070         /// Signals the object that initialization is complete.
    1071         /// </summary>
    1072         protected virtual void EndInit()
    1073         {
    1074             //necessary if the background type is gradient since the brush requires the size of the client rectangle
    1075             mView.SetBackgroundType(BackgroundType);
    1076            
    1077             //the diagram control provider gives problems in design mode. If enable at design mode the
    1078             //diagramming control becomes a container component rather than a form control because the essential
    1079             //properties are disabled through the ControlProvider (the Location or ID for example).
    1080             //Also, do not try to put this in the constructor, you need the ISupportInitialize to get a meaningful DesignMode value.
    1081             if(!DesignMode)
    1082             {
    1083                 ControlProvider controlProvider = new ControlProvider();
    1084                 TypeDescriptor.AddProvider(controlProvider, typeof(DiagramControlBase));
    1085             }
    1086         }
    1087         #endregion       
    1088     }
     748      }
     749      catch (Exception ex) {
     750        MessageBox.Show("An error occured while saving the " +
     751            "diagram.  See below for the message about the " +
     752            "error.\n\n" +
     753            "Message: " + ex.Message,
     754            "Save Diagram Error",
     755            MessageBoxButtons.OK,
     756            MessageBoxIcon.Error);
     757      }
     758    }
     759
     760    // ------------------------------------------------------------------
     761    /// <summary>
     762    /// Displays a SaveFileDialog regardless if there's an existing
     763    /// filename so the user can save the diagram to a new location.
     764    /// </summary>
     765    // ------------------------------------------------------------------
     766    public virtual void SaveAs() {
     767      SaveFileDialog dialog = new SaveFileDialog();
     768
     769      if (dialog.ShowDialog() == DialogResult.OK) {
     770        SaveAs(dialog.FileName);
     771      }
     772    }
     773
     774    // ------------------------------------------------------------------
     775    /// <summary>
     776    /// Saves the diagram to the given location.
     777    /// </summary>
     778    /// <param name="path">The path.</param>
     779    // ------------------------------------------------------------------
     780    public virtual void SaveAs(string path) {
     781      if (!Directory.Exists(Path.GetDirectoryName(path))) {
     782        throw new DirectoryNotFoundException(
     783            "Create the directory before saving the diagram to it.");
     784      }
     785      RaiseOnSavingDiagram(path);
     786      if (BinarySerializer.SaveAs(path, this) == true) {
     787        // Store the filename if the save was successful.
     788        this.mFileName = path;
     789
     790        // Clear the undo/redo history.
     791        this.mController.UndoManager.ClearUndoRedo();
     792        RaiseOnDiagramSaved(path);
     793      }
     794    }
     795
     796    // ------------------------------------------------------------------
     797    /// <summary>
     798    /// Displays an OpenFileDialog for the user to specify the diagram
     799    /// to open.
     800    /// </summary>
     801    // ------------------------------------------------------------------
     802    public virtual void Open() {
     803      OpenFileDialog dialog = new OpenFileDialog();
     804      if (dialog.ShowDialog() == DialogResult.OK) {
     805        this.Open(dialog.FileName);
     806      }
     807    }
     808
     809    // ------------------------------------------------------------------
     810    /// <summary>
     811    /// Opens the diagram from the specified path.
     812    /// </summary>
     813    /// <param name="path">The path.</param>
     814    // ------------------------------------------------------------------
     815    public virtual void Open(string path) {
     816      if (!Directory.Exists(Path.GetDirectoryName(path))) {
     817        throw new DirectoryNotFoundException(
     818            "Create the directory before saving the diagram to it.");
     819      }
     820      this.NewDocument();//makes it possible for the host to ask for
     821      // saving first.
     822
     823      RaiseOnOpeningDiagram(path);//do it before opening the new diagram.
     824      if (BinarySerializer.Open(path, this) == true) {
     825        // Store the filename if successful.
     826        this.mFileName = path;
     827
     828        // Add the filename to the list of previously opened files.
     829        this.myRecentFiles.Add(path);
     830
     831        // Raise that a new diagram was opened.
     832        RaiseOnDiagramOpened(path);
     833      }
     834      //this.mFileName = mFileName;
     835    }
     836    #endregion
     837
     838    // ------------------------------------------------------------------
     839    /// <summary>
     840    /// Attachement to the document.
     841    /// </summary>
     842    /// <param name="document">The document.</param>
     843    // ------------------------------------------------------------------
     844    public virtual void AttachToDocument(Document document) {
     845
     846      if (document == null)
     847        throw new ArgumentNullException();
     848
     849      this.mDocument = document;
     850
     851      #region re-attach to the new model
     852      View.Model = Document.Model;
     853      Controller.Model = Document.Model;
     854      #endregion
     855      #region Update the ambience
     856      document.Model.SetCurrentPage(0);
     857      #endregion
     858
     859      this.Controller.Model.Selection.Clear();
     860      this.Invalidate();
     861    }
     862
     863    // ------------------------------------------------------------------
     864    /// <summary>
     865    /// Activates a registered tool with the given name
     866    /// </summary>
     867    /// <param name="toolName">the name of a tool</param>
     868    // ------------------------------------------------------------------
     869    [System.Diagnostics.CodeAnalysis.SuppressMessage(
     870        "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
     871    public void ActivateTool(string toolName) {
     872      if (toolName == null || toolName.Trim().Length == 0) {
     873        throw new ArgumentNullException("The tool name cannot " +
     874            "be 'null' or empty.");
     875      }
     876
     877      if (this.Controller == null) {
     878        throw new InconsistencyException(
     879            "The Controller of the surface is 'null', this is a " +
     880            "strong inconsistency in the MVC model.");
     881      }
     882      if (toolName.Trim().Length > 0)
     883        this.Controller.ActivateTool(toolName);
     884    }
     885
     886    // ------------------------------------------------------------------
     887    /// <summary>
     888    /// Starts the tool with the given name.
     889    /// </summary>
     890    /// <param name="toolName">the name of a registered tool</param>
     891    // ------------------------------------------------------------------
     892    public void LaunchTool(string toolName) {
     893      if (this.Controller == null) return;
     894      this.Controller.ActivateTool(toolName);
     895    }
     896
     897    // ------------------------------------------------------------------
     898    /// <summary>
     899    /// Overrides the base method to call the view which will paint the
     900    /// diagram on the given graphics surface.
     901    /// </summary>
     902    /// <param name="e">PaintEventArgs</param>
     903    // ------------------------------------------------------------------
     904    protected override void OnPaint(PaintEventArgs e) {
     905      base.OnPaint(e);
     906      mView.Paint(e.Graphics);
     907    }
     908
     909    // ------------------------------------------------------------------
     910    /// <summary>
     911    /// Paints the background of the control.
     912    /// </summary>
     913    /// <param name="pevent">A
     914    /// <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that
     915    /// contains information about the control to paint.</param>
     916    // ------------------------------------------------------------------
     917    protected override void OnPaintBackground(PaintEventArgs pevent) {
     918      base.OnPaintBackground(pevent);
     919
     920      mView.PaintBackground(pevent.Graphics);
     921    }
     922
     923    // ------------------------------------------------------------------
     924    /// <summary>
     925    /// Undoes the last action that was recorded in the UndoManager.
     926    /// </summary>
     927    // ------------------------------------------------------------------
     928    public void Undo() {
     929      this.Controller.Undo();
     930    }
     931
     932    // ------------------------------------------------------------------
     933    /// <summary>
     934    /// Redoes the last action that was recorded in the UndoManager.
     935    /// </summary>
     936    // ------------------------------------------------------------------
     937    public void Redo() {
     938      this.Controller.Redo();
     939    }
     940    #endregion
     941
     942    #region Explicit ISupportInitialize implementation
     943    void ISupportInitialize.BeginInit() {
     944      //here you can check the conformity of properties
     945      BeginInit();
     946    }
     947
     948    /// <summary>
     949    /// Signals the object that initialization is complete.
     950    /// </summary>
     951    void ISupportInitialize.EndInit() {
     952      EndInit();
     953    }
     954
     955    /// <summary>
     956    /// Signals the object that initialization is starting.
     957    /// </summary>
     958    protected virtual void BeginInit() {
     959
     960    }
     961    /// <summary>
     962    /// Signals the object that initialization is complete.
     963    /// </summary>
     964    protected virtual void EndInit() {
     965      //necessary if the background type is gradient since the brush requires the size of the client rectangle
     966      mView.SetBackgroundType(BackgroundType);
     967
     968      //the diagram control provider gives problems in design mode. If enable at design mode the
     969      //diagramming control becomes a container component rather than a form control because the essential
     970      //properties are disabled through the ControlProvider (the Location or ID for example).
     971      //Also, do not try to put this in the constructor, you need the ISupportInitialize to get a meaningful DesignMode value.
     972      if (!DesignMode) {
     973        ControlProvider controlProvider = new ControlProvider();
     974        TypeDescriptor.AddProvider(controlProvider, typeof(DiagramControlBase));
     975      }
     976    }
     977    #endregion
     978  }
    1089979}
Note: See TracChangeset for help on using the changeset viewer.