Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/PasteTool.cs

Last change on this file was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.8 KB
Line 
1using System;
2using System.Collections;
3using System.Drawing;
4using System.IO;
5using System.Runtime.Serialization.Formatters.Binary;
6using System.Windows.Forms;
7namespace Netron.Diagramming.Core {
8  // ----------------------------------------------------------------------
9  /// <summary>
10  /// This tool pastes items from the clipboard to the canvas.
11  /// </summary>
12  // ----------------------------------------------------------------------
13  public class PasteTool : AbstractTool {
14    // ------------------------------------------------------------------
15    /// <summary>
16    /// Specifies where the items from the clipboard will be
17    /// inserted at.
18    /// </summary>
19    // ------------------------------------------------------------------
20    public static Point InsertionPoint = new Point(50, 50);
21
22    #region Fields
23    #endregion
24
25    #region Constructor
26    /// <summary>
27    /// Initializes a new instance of the <see cref="T:HoverTool"/> class.
28    /// </summary>
29    /// <param name="name">The name of the tool.</param>
30    public PasteTool(string name)
31      : base(name) {
32    }
33    #endregion
34
35    #region Methods
36
37    /// <summary>
38    /// Called when the tool is activated.
39    /// </summary>
40    protected override void OnActivateTool() {
41      try {
42        // Used to get a collection of entities from the
43        // NetronClipboard.
44        Type entitiesType = typeof(CollectionBase<IDiagramEntity>);
45
46        // First calculate the insertion point based on the
47        // current location of the cursor.
48        InsertionPoint = Point.Round(
49        Controller.View.ViewToWorld(
50        Controller.View.DeviceToView(
51        Controller.ParentControl.PointToClient(Cursor.Position))));
52
53        IDataObject data = Clipboard.GetDataObject();
54        string format = typeof(CopyTool).FullName;
55        if (data.GetDataPresent(format)) {
56          MemoryStream stream = data.GetData(format) as MemoryStream;
57          CollectionBase<IDiagramEntity> collection = null;
58          GenericFormatter<BinaryFormatter> f =
59              new GenericFormatter<BinaryFormatter>();
60          //Anchors collection is a helper collection to re-connect connectors to their parent
61          Anchors.Clear();
62          //but is it actually a stream coming this application?
63          collection = f.Deserialize<CollectionBase<IDiagramEntity>>(stream);
64          UnwrapBundle(collection);
65
66        } else if (NetronClipboard.ContainsData(entitiesType)) {
67          CollectionBase<IDiagramEntity> collection =
68              (CollectionBase<IDiagramEntity>)NetronClipboard.Get(
69              entitiesType);
70
71          UnwrapBundle(collection.DeepCopy());
72        } else if (data.GetDataPresent(DataFormats.Bitmap)) {
73          Bitmap bmp = data.GetData(DataFormats.Bitmap) as Bitmap;
74          if (bmp != null) {
75            #region Unwrap into an image shape
76            //TODO: Insert the image shape here
77            ImageShape shape = new ImageShape();
78            shape.Image = bmp;
79            shape.Location = InsertionPoint;
80            Controller.Model.AddShape(shape);
81            #endregion
82          }
83        } else if (data.GetDataPresent(DataFormats.Text)) {
84          string text = (string)data.GetData(typeof(string));
85          TextOnly textShape = new TextOnly(Controller.Model);
86          textShape.Text = text;
87          textShape.Location = InsertionPoint;
88          Controller.Model.AddShape(textShape);
89        }
90      }
91      catch (Exception exc) {
92        throw new InconsistencyException("The Paste operation failed.", exc);
93      }
94      finally {
95        DeactivateTool();
96      }
97
98    }
99
100    #endregion
101
102    /// <summary>
103    /// Unwraps the given bundle to the diagram.
104    /// </summary>
105    /// <param name="collection">CollectionBase<IDiagramEntity></param>
106    void UnwrapBundle(CollectionBase<IDiagramEntity> collection) {
107      if (collection != null) {
108        #region Unwrap the bundle
109        this.Controller.Model.Unwrap(collection);
110
111        Rectangle rec = Utils.BoundingRectangle(collection);
112
113        // The InsertionPoint specifies where the pasted
114        // entities should be.  So, shift each entity in the
115        // collection to the InsertionPoint.
116        Point delta = new Point(
117            InsertionPoint.X - rec.Location.X,
118            InsertionPoint.Y - rec.Location.Y);
119        //delta.Offset(rec.Location);
120
121        foreach (IDiagramEntity entity in collection) {
122          entity.MoveBy(delta);
123        }
124
125        // Recalculate the bounding rectangle and invalidate
126        // that area on the canvas.
127        rec = Utils.BoundingRectangle(collection);
128        rec.Inflate(30, 30);
129        this.Controller.View.Invalidate(rec);
130        #endregion
131      }
132    }
133  }
134
135}
Note: See TracBrowser for help on using the repository browser.