Ejemplo n.º 1
0
        /// <summary>
        /// Opens the binary-saved diagram
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="control">The control.</param>
        public static bool Open(string fileName, DiagramControlBase control)
        {
            FileStream fs = null;

            try {
                fs = File.OpenRead(fileName);
            }
            catch (System.IO.DirectoryNotFoundException exc) {
                System.Windows.Forms.MessageBox.Show(exc.Message);
            }
            catch (System.IO.FileLoadException exc) {
                System.Windows.Forms.MessageBox.Show(exc.Message);
            }
            catch (System.IO.FileNotFoundException exc) {
                System.Windows.Forms.MessageBox.Show(exc.Message);
            }
            catch (Exception exc) {
                throw new Exception("Non-CLS exception caught.", exc);
            }
            //donnot open anything if filestream is not there
            if (fs == null)
            {
                return(false);
            }

            try {
                GenericFormatter <BinaryFormatter> f = new GenericFormatter <BinaryFormatter>();
                //one-line deserialization, no bits 'nd bytes necessary....C# is amazing...
                Document document = f.Deserialize <Document>(fs);

                if (document == null)
                {
                    throw new InconsistencyException("The deserialization return 'null'.");
                }
                //call the standard method at the control level to attach a new document
                //In principle you could create a document programmatically and attach it this way as well.
                control.AttachToDocument(document);
                return(true);
            }
            catch (SerializationException exc) {
                MessageBox.Show(exc.Message);
            }
            catch (System.Reflection.TargetInvocationException exc) {
                MessageBox.Show(exc.Message, "BinarySerializer.Open");
            }
            catch (Exception exc) {
                MessageBox.Show(exc.Message, "BinarySerializer.Open");
            }
            finally {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// This method creates first a deep copy of the collection and puts the result in a <see cref="MemoryStream"/>.
 /// See the <see cref="CopyTool"/> for details.
 /// </summary>
 /// <returns></returns>
 public MemoryStream ToStream()
 {
     try
     {
         MemoryStream stream = new MemoryStream();
         GenericFormatter <BinaryFormatter> f = new GenericFormatter <BinaryFormatter>();
         f.Serialize(stream, this);
         return(stream);
     }
     catch (Exception exc)
     {
         throw new InconsistencyException("The ToStream() operation failed.", exc);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Called when the tool is activated.
        /// </summary>
        protected override void OnActivateTool()
        {
            try
            {
                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);



                    if (collection != null)
                    {
                        #region Unwrap the bundle
                        this.Controller.Model.Unwrap(collection);
                        Rectangle rec = Utils.BoundingRectangle(collection);
                        rec.Inflate(30, 30);
                        this.Controller.View.Invalidate(rec);
                        #endregion
                    }
                }
                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
                        #endregion
                    }
                }
            }
            catch (Exception exc)
            {
                throw new InconsistencyException("The Copy operation failed.", exc);
            }
            finally
            {
                DeactivateTool();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns a deep copy of this collection.
        /// <remarks>The returned collection is not attached to the <see cref="Model"/>
        /// and is as such on an in-memory collection of instances. You need to 'unwrap' the collection
        /// in the model and, to make it visible, deploy it in the paintables collection of the model.
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        public CollectionBase <T> DeepCopy()
        {
            /* This doesn't work seemingly....
             * if (!typeof(T).IsSerializable)
             *  throw new InconsistencyException("The generic type on which the collection is based is not serializable, the collection cannot generate a deep copy.");
             */

            try {
                CollectionBase <T> newobj            = null;
                MemoryStream       stream            = new MemoryStream();
                GenericFormatter <BinaryFormatter> f = new GenericFormatter <BinaryFormatter>();
                f.Serialize(stream, this);
                stream.Seek(0, SeekOrigin.Begin);
                newobj = f.Deserialize <CollectionBase <T> >(stream);
                stream.Close();
                return(newobj);
            }
            catch (Exception exc) {
                throw new InconsistencyException("The copy operation failed.", exc);
            }
        }
Ejemplo n.º 5
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();
            }
        }