Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/PasteTool.cs @ 2768

Last change on this file since 2768 was 2768, checked in by mkommend, 14 years ago

added solution folders and sources for the netron library (ticket #867)

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