Example #1
0
        /// <summary>
        /// Called when the tool is activated.
        /// </summary>
        protected override void OnActivateTool()
        {
            try {
                // Used to get a collection of entities from the
                // NetronClipboard.
                Type entitiesType = typeof(CollectionBase <IDiagramEntity>);

                // First calculate the insertion point based on the
                // current location of the cursor.
                InsertionPoint = Point.Round(
                    Controller.View.ViewToWorld(
                        Controller.View.DeviceToView(
                            Controller.ParentControl.PointToClient(Cursor.Position))));

                IDataObject data   = Clipboard.GetDataObject();
                string      format = typeof(CopyTool).FullName;
                if (data.GetDataPresent(format))
                {
                    MemoryStream stream = data.GetData(format) as MemoryStream;
                    CollectionBase <IDiagramEntity>    collection = null;
                    GenericFormatter <BinaryFormatter> f          =
                        new GenericFormatter <BinaryFormatter>();
                    //Anchors collection is a helper collection to re-connect connectors to their parent
                    Anchors.Clear();
                    //but is it actually a stream coming this application?
                    collection = f.Deserialize <CollectionBase <IDiagramEntity> >(stream);
                    UnwrapBundle(collection);
                }
                else if (NetronClipboard.ContainsData(entitiesType))
                {
                    CollectionBase <IDiagramEntity> collection =
                        (CollectionBase <IDiagramEntity>)NetronClipboard.Get(
                            entitiesType);

                    UnwrapBundle(collection.DeepCopy());
                }
                else if (data.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap bmp = data.GetData(DataFormats.Bitmap) as Bitmap;
                    if (bmp != null)
                    {
                        #region Unwrap into an image shape
                        //TODO: Insert the image shape here
                        ImageShape shape = new ImageShape();
                        shape.Image    = bmp;
                        shape.Location = InsertionPoint;
                        Controller.Model.AddShape(shape);
                        #endregion
                    }
                }
                else if (data.GetDataPresent(DataFormats.Text))
                {
                    string   text      = (string)data.GetData(typeof(string));
                    TextOnly textShape = new TextOnly(Controller.Model);
                    textShape.Text     = text;
                    textShape.Location = InsertionPoint;
                    Controller.Model.AddShape(textShape);
                }
            }
            catch (Exception exc) {
                throw new InconsistencyException("The Paste operation failed.", exc);
            }
            finally {
                DeactivateTool();
            }
        }