예제 #1
0
        /// <summary>A node has been dragged over another node. Allow drop?</summary>
        /// <param name="sender">Sending node</param>
        /// <param name="e">Node arguments</param>
        public void OnAllowDrop(object sender, AllowDropArgs e)
        {
            e.Allow = false;

            Model destinationModel = Apsim.Get(this.ApsimXFile, e.NodePath) as Model;

            if (destinationModel != null)
            {
                DragObject           dragObject  = e.DragObject as DragObject;
                ValidParentAttribute validParent = ReflectionUtilities.GetAttribute(dragObject.ModelType, typeof(ValidParentAttribute), false) as ValidParentAttribute;
                if (validParent == null || validParent.ParentModels.Length == 0)
                {
                    e.Allow = true;
                }
                else
                {
                    foreach (Type allowedParentType in validParent.ParentModels)
                    {
                        if (allowedParentType == destinationModel.GetType())
                        {
                            e.Allow = true;
                        }
                    }
                }
            }
        }
예제 #2
0
파일: TreeView.cs 프로젝트: ilhuber/ApsimX
        /// <summary>Node has been dragged over another node. Allow a drop here?</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragOver(object sender, DragMotionArgs e)
        {
            try
            {
                // e.Effect = DragDropEffects.None;
                e.RetVal = false;
                Gdk.Drag.Status(e.Context, 0, e.Time); // Default to no drop

                Gdk.Atom target = Drag.DestFindTarget(treeview1, e.Context, null);
                // Get the drop location
                TreePath path;
                TreeIter dest;
                if (treeview1.GetPathAtPos(e.X, e.Y, out path) && treemodel.GetIter(out dest, path) &&
                    target != Gdk.Atom.Intern("GDK_NONE", false))
                {
                    AllowDropArgs args = new AllowDropArgs();
                    args.NodePath = GetFullPath(path);
                    Drag.GetData(treeview1, e.Context, target, e.Time);
                    if (dragDropData != null)
                    {
                        args.DragObject = dragDropData;
                        if (AllowDrop != null)
                        {
                            AllowDrop(this, args);
                            if (args.Allow)
                            {
                                e.RetVal = true;
                                string sourceParent = null;
                                if (sourcePathOfItemBeingDragged != null)
                                {
                                    sourceParent = StringUtilities.ParentName(sourcePathOfItemBeingDragged);
                                }

                                // Now determine the effect. If the drag originated from a different view
                                // (e.g. a toolbox or another file) then only copy is supported.
                                if (sourcePathOfItemBeingDragged == null)
                                {
                                    Gdk.Drag.Status(e.Context, Gdk.DragAction.Copy, e.Time);
                                }
                                else if (sourceParent == args.NodePath)
                                {
                                    Gdk.Drag.Status(e.Context, Gdk.DragAction.Copy, e.Time);
                                }
                                else
                                {
                                    // The "SuggestedAction" will normally be Copy, but will be Move
                                    // if shift is pressed, and Link if Ctrl-Shift is pressed
                                    Gdk.Drag.Status(e.Context, e.Context.SuggestedAction, e.Time);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ShowError(err);
            }
        }
예제 #3
0
        /// <summary>Node has been dragged over another node. Allow a drop here?</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragOver(object sender, DragMotionArgs e)
        {
            // e.Effect = DragDropEffects.None;
            e.RetVal = false;

            // Get the drop location
            TreePath path;
            TreeIter dest;

            if (treeview1.GetPathAtPos(e.X, e.Y, out path) && treemodel.GetIter(out dest, path))
            {
                AllowDropArgs Args = new AllowDropArgs();
                Args.NodePath = FullPath(path);
                Drag.GetData(treeview1, e.Context, e.Context.Targets[0], e.Time);
                if (DragDropData != null)
                {
                    Args.DragObject = DragDropData;
                    if (AllowDrop != null)
                    {
                        AllowDrop(this, Args);
                        if (Args.Allow)
                        {
                            e.RetVal = true;
                            string SourceParent = null;
                            if (sourcePathOfItemBeingDragged != null)
                            {
                                SourceParent = StringUtilities.ParentName(sourcePathOfItemBeingDragged);
                            }

                            // Now determine the effect. If the drag originated from a different view
                            // (e.g. a toolbox or another file) then only copy is supported.
                            if (sourcePathOfItemBeingDragged == null)
                            {
                                Gdk.Drag.Status(e.Context, Gdk.DragAction.Copy, e.Time);
                            }
                            else if (SourceParent == Args.NodePath)
                            {
                                Gdk.Drag.Status(e.Context, Gdk.DragAction.Copy, e.Time);
                            }
                            else
                            {
                                // The "SuggestedAction" will normally be Copy, but will be Move
                                // if shift is pressed, and Link if Ctrl-Shift is pressed
                                Gdk.Drag.Status(e.Context, e.Context.SuggestedAction, e.Time);
                            }
                        }
                        else
                        {
                            Gdk.Drag.Status(e.Context, 0, e.Time);
                        }
                    }
                }
            }
        }
예제 #4
0
        /// <summary>Node has been dragged over another node. Allow a drop here?</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;

            // Get the drop location
            TreeNode DestinationNode = TreeView.GetNodeAt(TreeView.PointToClient(new Point(e.X, e.Y)));

            if (DestinationNode != null)
            {
                AllowDropArgs Args = new AllowDropArgs();
                Args.NodePath = FullPath(DestinationNode);

                string[] Formats = e.Data.GetFormats();
                if (Formats.Length > 0)
                {
                    Args.DragObject = e.Data.GetData(Formats[0]) as ISerializable;
                    if (AllowDrop != null)
                    {
                        AllowDrop(this, Args);
                        if (Args.Allow)
                        {
                            string SourceParent = null;
                            if (sourcePathOfItemBeingDragged != null)
                            {
                                SourceParent = StringUtilities.ParentName(sourcePathOfItemBeingDragged);
                            }

                            // Now determine the effect. If the drag originated from a different view
                            // (e.g. a toolbox or another file) then only copy is supported.
                            if (sourcePathOfItemBeingDragged == null)
                            {
                                e.Effect = DragDropEffects.Copy;  // Dragging from a foreign view.
                            }
                            else if (SourceParent == Args.NodePath)
                            {
                                e.Effect = DragDropEffects.Copy;  // Dragged node's parent is the node we're currently over
                            }
                            else if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
                            {
                                e.Effect = DragDropEffects.Link;
                            }
                            else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                            {
                                e.Effect = DragDropEffects.Move;
                            }
                            else
                            {
                                e.Effect = DragDropEffects.Copy;
                            }
                        }
                    }
                }
            }
        }
예제 #5
0
        /// <summary>A node has been dragged over another node. Allow drop?</summary>
        /// <param name="sender">Sending node</param>
        /// <param name="e">Node arguments</param>
        public void OnAllowDrop(object sender, AllowDropArgs e)
        {
            e.Allow = false;

            Model parentModel = Apsim.Get(this.ApsimXFile, e.NodePath) as Model;

            if (parentModel != null)
            {
                DragObject dragObject = e.DragObject as DragObject;
                e.Allow = Apsim.IsChildAllowable(parentModel, dragObject.ModelType);
            }
        }
예제 #6
0
파일: TreeView.cs 프로젝트: lie112/ApsimX
        /// <summary>Node has been dropped. Send to presenter.</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragDrop(object sender, DragDropArgs e)
        {
            try
            {
                Gdk.Atom target = Drag.DestFindTarget(treeview1, e.Context, null);
                // Get the drop location
                TreePath path;
                TreeIter dest;
                bool     success = false;
                if (treeview1.GetPathAtPos(e.X, e.Y, out path) && treemodel.GetIter(out dest, path) &&
                    target != Gdk.Atom.Intern("GDK_NONE", false))
                {
                    AllowDropArgs args = new AllowDropArgs();
                    args.NodePath = GetFullPath(path);

                    Drag.GetData(treeview1, e.Context, target, e.Time);
                    if (dragDropData != null)
                    {
                        DropArgs dropArgs = new DropArgs();
                        dropArgs.NodePath = GetFullPath(path);

                        dropArgs.DragObject = dragDropData;
                        Gdk.DragAction action = e.Context.SelectedAction;
                        if ((action & Gdk.DragAction.Move) == Gdk.DragAction.Move)
                        {
                            dropArgs.Moved = true;
                        }
                        else if ((action & Gdk.DragAction.Copy) == Gdk.DragAction.Copy)
                        {
                            dropArgs.Copied = true;
                        }
                        else
                        {
                            dropArgs.Linked = true;
                        }
                        Droped(this, dropArgs);
                        success = true;
                    }
                }
                Gtk.Drag.Finish(e.Context, success, e.Context.SelectedAction == Gdk.DragAction.Move, e.Time);
                e.RetVal = success;
            }
            catch (Exception err)
            {
                ShowError(err);
            }
        }
예제 #7
0
        /// <summary>Node has been dropped. Send to presenter.</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragDrop(object sender, DragDropArgs e)
        {
            Gdk.Atom target = Drag.DestFindTarget(treeview1, e.Context, null);
            // Get the drop location
            TreePath path;
            TreeIter dest;
            bool     success = false;

            if (treeview1.GetPathAtPos(e.X, e.Y, out path) && treemodel.GetIter(out dest, path) &&
                target != Gdk.Atom.Intern("GDK_NONE", false))
            {
                AllowDropArgs Args = new AllowDropArgs();
                Args.NodePath = FullPath(path);

                Drag.GetData(treeview1, e.Context, target, e.Time);
                if (DragDropData != null)
                {
                    DropArgs args = new DropArgs();
                    args.NodePath = FullPath(path);

                    args.DragObject = DragDropData;
                    if (e.Context.Action == Gdk.DragAction.Copy)
                    {
                        args.Copied = true;
                    }
                    else if (e.Context.Action == Gdk.DragAction.Move)
                    {
                        args.Moved = true;
                    }
                    else
                    {
                        args.Linked = true;
                    }
                    Droped(this, args);
                    success = true;
                }
            }
            Gtk.Drag.Finish(e.Context, success, e.Context.Action == Gdk.DragAction.Move, e.Time);
            e.RetVal = success;
        }
예제 #8
0
        /// <summary>Node has been dragged over another node. Allow a drop here?</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragOver(object sender, DragMotionArgs e)
        {
            //e.Effect = DragDropEffects.None;
            e.RetVal = false;

            // Get the drop location
            TreePath path;
            TreeIter dest;
            if (treeview1.GetPathAtPos(e.X, e.Y, out path) && treemodel.GetIter(out dest, path))
            {
                AllowDropArgs Args = new AllowDropArgs();
                Args.NodePath = FullPath(path);
                Drag.GetData(treeview1, e.Context, e.Context.Targets[0], e.Time);
                if (DragDropData != null)
                {
                    Args.DragObject = DragDropData;
                    if (AllowDrop != null)
                    {
                        AllowDrop(this, Args);
                        if (Args.Allow)
                        {
                            e.RetVal = true;
                            string SourceParent = null;
                            if (sourcePathOfItemBeingDragged != null)
                                SourceParent = StringUtilities.ParentName(sourcePathOfItemBeingDragged);

                            // Now determine the effect. If the drag originated from a different view
                            // (e.g. a toolbox or another file) then only copy is supported.
                            if (sourcePathOfItemBeingDragged == null)
                                Gdk.Drag.Status(e.Context, Gdk.DragAction.Copy, e.Time);
                            else if (SourceParent == Args.NodePath)
                                Gdk.Drag.Status(e.Context, Gdk.DragAction.Copy, e.Time);
                            else
                            // The "SuggestedAction" will normally be Copy, but will be Move
                            // if shift is pressed, and Link if Ctrl-Shift is pressed
                                Gdk.Drag.Status(e.Context, e.Context.SuggestedAction, e.Time);

                        }
                        else
                            Gdk.Drag.Status(e.Context, 0, e.Time);
                    }
                }
            }
        }
예제 #9
0
        /// <summary>Node has been dropped. Send to presenter.</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragDrop(object sender, DragDropArgs e)
        {
            // Get the drop location
            TreePath path;
            TreeIter dest;
            bool success = false;
            if (treeview1.GetPathAtPos(e.X, e.Y, out path) && treemodel.GetIter(out dest, path))
            {
                AllowDropArgs Args = new AllowDropArgs();
                Args.NodePath = FullPath(path);

                Drag.GetData(treeview1, e.Context, e.Context.Targets[0], e.Time);
                if (DragDropData != null)
                {
                    DropArgs args = new DropArgs();
                    args.NodePath = FullPath(path);

                    args.DragObject = DragDropData;
                    if (e.Context.Action == Gdk.DragAction.Copy)
                        args.Copied = true;
                    else if (e.Context.Action == Gdk.DragAction.Move)
                        args.Moved = true;
                    else
                        args.Linked = true;
                    Droped(this, args);
                    success = true;
                }
            }
            Gtk.Drag.Finish(e.Context, success, e.Context.Action == Gdk.DragAction.Move, e.Time);
            e.RetVal = success;
        }
예제 #10
0
        /// <summary>
        /// Pastes the contents of the clipboard.
        /// </summary>
        /// <param name="xml">The XML document text</param>
        /// <param name="parentPath">Path to the parent</param>
        public void Add(string xml, string parentPath)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                try
                {
                    document.LoadXml(xml);
                }
                catch (XmlException)
                {
                    MainPresenter.ShowMessage("Invalid XML. Are you sure you're trying to paste an APSIM model?", Simulation.ErrorLevel.Error);
                }

                object newModel = XmlUtilities.Deserialise(document.DocumentElement, this.ApsimXFile.GetType().Assembly);

                // See if the presenter is happy with this model being added.
                Model         parentModel   = Apsim.Get(this.ApsimXFile, parentPath) as Model;
                AllowDropArgs allowDropArgs = new AllowDropArgs();
                allowDropArgs.NodePath   = parentPath;
                allowDropArgs.DragObject = new DragObject()
                {
                    NodePath  = null,
                    ModelType = newModel.GetType(),
                    Xml       = this.GetClipboardText()
                };

                this.OnAllowDrop(null, allowDropArgs);

                // If it is happy then issue an AddModelCommand.
                if (allowDropArgs.Allow)
                {
                    // If the model xml is a soil object then try and convert from old
                    // APSIM format to new.
                    if (document.DocumentElement.Name == "Soil" && XmlUtilities.Attribute(document.DocumentElement, "Name") != string.Empty)
                    {
                        XmlDocument newDoc = new XmlDocument();
                        newDoc.AppendChild(newDoc.CreateElement("D"));
                        APSIMImporter importer = new APSIMImporter();
                        importer.ImportSoil(document.DocumentElement, newDoc.DocumentElement, newDoc.DocumentElement);
                        XmlNode soilNode = XmlUtilities.FindByType(newDoc.DocumentElement, "Soil");
                        if (soilNode != null &&
                            XmlUtilities.FindByType(soilNode, "Sample") == null &&
                            XmlUtilities.FindByType(soilNode, "InitialWater") == null)
                        {
                            // Add in an initial water and initial conditions models.
                            XmlNode initialWater = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("InitialWater"));
                            XmlUtilities.SetValue(initialWater, "Name", "Initial water");
                            XmlUtilities.SetValue(initialWater, "PercentMethod", "FilledFromTop");
                            XmlUtilities.SetValue(initialWater, "FractionFull", "1");
                            XmlUtilities.SetValue(initialWater, "DepthWetSoil", "NaN");
                            XmlNode initialConditions = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("Sample"));
                            XmlUtilities.SetValue(initialConditions, "Name", "Initial conditions");
                            XmlUtilities.SetValue(initialConditions, "Thickness/double", "1800");
                            XmlUtilities.SetValue(initialConditions, "NO3/double", "10");
                            XmlUtilities.SetValue(initialConditions, "NH4/double", "1");
                            XmlUtilities.SetValue(initialConditions, "NO3Units", "kgha");
                            XmlUtilities.SetValue(initialConditions, "NH4Units", "kgha");
                            XmlUtilities.SetValue(initialConditions, "SWUnits", "Volumetric");
                        }

                        document.LoadXml(newDoc.DocumentElement.InnerXml);
                    }

                    IModel child = XmlUtilities.Deserialise(document.DocumentElement, this.ApsimXFile.GetType().Assembly) as IModel;

                    AddModelCommand command = new AddModelCommand(parentModel, document.DocumentElement, this.GetNodeDescription(child), this.view);
                    this.CommandHistory.Add(command, true);
                }
            }
            catch (Exception exception)
            {
                this.MainPresenter.ShowMessage(exception.Message, Simulation.ErrorLevel.Error);
            }
        }
예제 #11
0
        /// <summary>A node has been dragged over another node. Allow drop?</summary>
        /// <param name="sender">Sending node</param>
        /// <param name="e">Node arguments</param>
        public void OnAllowDrop(object sender, AllowDropArgs e)
        {
            e.Allow = false;

            Model parentModel = Apsim.Get(this.ApsimXFile, e.NodePath) as Model;
            if (parentModel != null)
            {
                DragObject dragObject = e.DragObject as DragObject;
                e.Allow = Apsim.IsChildAllowable(parentModel, dragObject.ModelType);
            }
        }
예제 #12
0
        /// <summary>Pastes the contents of the clipboard.</summary>
        public void Add(string xml, string parentPath)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                try
                {
                    document.LoadXml(xml);
                }
                catch(XmlException)
                {
                    MainPresenter.ShowMessage("Invalid XML. Are you sure you're trying to paste an APSIM model?", DataStore.ErrorLevel.Error);
                }
                object newModel = XmlUtilities.Deserialise(document.DocumentElement, ApsimXFile.GetType().Assembly);

                // See if the presenter is happy with this model being added.
                Model parentModel = Apsim.Get(this.ApsimXFile, parentPath) as Model;
                AllowDropArgs allowDropArgs = new AllowDropArgs();
                allowDropArgs.NodePath = parentPath;
                allowDropArgs.DragObject = new DragObject()
                {
                    NodePath = null,
                    ModelType = newModel.GetType(),
                    Xml = GetClipboardText()
                };
                this.OnAllowDrop(null, allowDropArgs);

                // If it is happy then issue an AddModelCommand.
                if (allowDropArgs.Allow)
                {
                    // If the model xml is a soil object then try and convert from old
                    // APSIM format to new.
                    if (document.DocumentElement.Name == "Soil" && XmlUtilities.Attribute(document.DocumentElement, "Name") != "")
                    {
                        XmlDocument newDoc = new XmlDocument();
                        newDoc.AppendChild(newDoc.CreateElement("D"));
                        APSIMImporter importer = new APSIMImporter();
                        importer.ImportSoil(document.DocumentElement, newDoc.DocumentElement, newDoc.DocumentElement);
                        XmlNode soilNode = XmlUtilities.FindByType(newDoc.DocumentElement, "Soil");
                        if (soilNode != null &&
                            XmlUtilities.FindByType(soilNode, "Sample") == null &&
                            XmlUtilities.FindByType(soilNode, "InitialWater") == null)
                        {
                            // Add in an initial water and initial conditions models.
                            XmlNode initialWater = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("InitialWater"));
                            XmlUtilities.SetValue(initialWater, "Name", "Initial water");
                            XmlUtilities.SetValue(initialWater, "PercentMethod", "FilledFromTop");
                            XmlUtilities.SetValue(initialWater, "FractionFull", "1");
                            XmlUtilities.SetValue(initialWater, "DepthWetSoil", "NaN");
                            XmlNode initialConditions = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("Sample"));
                            XmlUtilities.SetValue(initialConditions, "Name", "Initial conditions");
                            XmlUtilities.SetValue(initialConditions, "Thickness/double", "1800");
                            XmlUtilities.SetValue(initialConditions, "NO3/double", "10");
                            XmlUtilities.SetValue(initialConditions, "NH4/double", "1");
                            XmlUtilities.SetValue(initialConditions, "NO3Units", "kgha");
                            XmlUtilities.SetValue(initialConditions, "NH4Units", "kgha");
                            XmlUtilities.SetValue(initialConditions, "SWUnits", "Volumetric");
                        }
                        document.LoadXml(newDoc.DocumentElement.InnerXml);
                    }

                    IModel child = XmlUtilities.Deserialise(document.DocumentElement, ApsimXFile.GetType().Assembly) as IModel;

                    AddModelCommand command = new AddModelCommand(parentModel, document.DocumentElement,
                                                                  GetNodeDescription(child), view);
                    this.CommandHistory.Add(command, true);
                }
            }
            catch (Exception exception)
            {
                this.MainPresenter.ShowMessage(exception.Message, DataStore.ErrorLevel.Error);
            }
        }
예제 #13
0
        /// <summary>Node has been dragged over another node. Allow a drop here?</summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event data.</param>
        private void OnDragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;

            // Get the drop location
            TreeNode DestinationNode = TreeView.GetNodeAt(TreeView.PointToClient(new Point(e.X, e.Y)));
            if (DestinationNode != null)
            {
                AllowDropArgs Args = new AllowDropArgs();
                Args.NodePath = FullPath(DestinationNode);

                string[] Formats = e.Data.GetFormats();
                if (Formats.Length > 0)
                {
                    Args.DragObject = e.Data.GetData(Formats[0]) as ISerializable;
                    if (AllowDrop != null)
                    {
                        AllowDrop(this, Args);
                        if (Args.Allow)
                        {
                            string SourceParent = null;
                            if (sourcePathOfItemBeingDragged != null)
                                SourceParent = StringUtilities.ParentName(sourcePathOfItemBeingDragged);

                            // Now determine the effect. If the drag originated from a different view
                            // (e.g. a toolbox or another file) then only copy is supported.
                            if (sourcePathOfItemBeingDragged == null)
                                e.Effect = DragDropEffects.Copy;  // Dragging from a foreign view.
                            else if (SourceParent == Args.NodePath)
                                e.Effect = DragDropEffects.Copy;  // Dragged node's parent is the node we're currently over
                            else if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
                                e.Effect = DragDropEffects.Link;
                            else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                                e.Effect = DragDropEffects.Move;
                            else
                                e.Effect = DragDropEffects.Copy;
                        }
                    }
                }
            }
        }
예제 #14
0
        /// <summary>A node has been dragged over another node. Allow drop?</summary>
        /// <param name="sender">Sending node</param>
        /// <param name="e">Node arguments</param>
        public void OnAllowDrop(object sender, AllowDropArgs e)
        {
            e.Allow = false;

            Model destinationModel = Apsim.Get(this.ApsimXFile, e.NodePath) as Model;
            if (destinationModel != null)
            {
                if (destinationModel.GetType() == typeof(Folder))
                    e.Allow = true;

                DragObject dragObject = e.DragObject as DragObject;
                ValidParentAttribute validParent = ReflectionUtilities.GetAttribute(dragObject.ModelType, typeof(ValidParentAttribute), true) as ValidParentAttribute;
                if (validParent == null || validParent.ParentModels.Length == 0)
                {
                    e.Allow = true;
                }
                else
                {
                    foreach (Type allowedParentType in validParent.ParentModels)
                    {
                        if (allowedParentType == destinationModel.GetType())
                        {
                            e.Allow = true;
                        }
                    }
                }
            }
        }