Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/PasteTool.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/PasteTool.cs
r2768 r4068 1 1 using System; 2 using System.Diagnostics;3 2 using System.Collections; 4 3 using System.Drawing; 4 using System.IO; 5 using System.Runtime.Serialization.Formatters.Binary; 5 6 using System.Windows.Forms; 6 using System.IO; 7 using System.Runtime.Serialization; 8 using System.Runtime.Serialization.Formatters.Binary; 9 namespace Netron.Diagramming.Core 10 { 11 // ---------------------------------------------------------------------- 7 namespace 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 // ------------------------------------------------------------------ 12 15 /// <summary> 13 /// This tool pastes items from the clipboard to the canvas. 16 /// Specifies where the items from the clipboard will be 17 /// inserted at. 14 18 /// </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); 19 // ------------------------------------------------------------------ 20 public static Point InsertionPoint = new Point(50, 50); 25 21 26 27 22 #region Fields 23 #endregion 28 24 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 { 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); 37 89 } 38 #endregion 90 } 91 catch (Exception exc) { 92 throw new InconsistencyException("The Paste operation failed.", exc); 93 } 94 finally { 95 DeactivateTool(); 96 } 39 97 40 #region Methods98 } 41 99 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>); 100 #endregion 52 101 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)))); 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); 59 110 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); 111 Rectangle rec = Utils.BoundingRectangle(collection); 73 112 74 } 75 else if (NetronClipboard.ContainsData(entitiesType)) 76 { 77 CollectionBase<IDiagramEntity> collection = 78 (CollectionBase<IDiagramEntity>)NetronClipboard.Get( 79 entitiesType); 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); 80 120 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 121 foreach (IDiagramEntity entity in collection) { 122 entity.MoveBy(delta); 115 123 } 116 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); 117 130 #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 } 131 } 153 132 } 133 } 154 134 155 135 }
Note: See TracChangeset
for help on using the changeset viewer.