/// <summary>
        /// Copies the dragged Item to the dropped Item
        /// </summary>
        /// <param name="_sourceItem"></param>
        /// <param name="_targetItem"></param>
        private void CopyItem(ImagedConnectionTreeViewItem _sourceItem, ImagedConnectionTreeViewItem _targetItem)
        {
            try
            {
                //Only move, if the source is a Folder or Host and Target is only folder
                if (_sourceItem.Datatype == ImagedConnectionTreeViewDatatype.ConnectionProtocol ||
                    _sourceItem.Datatype == ImagedConnectionTreeViewDatatype.ConnectionProtocolOption ||
                    _targetItem.Datatype != ImagedConnectionTreeViewDatatype.Folder
                    )
                {
                    return;
                }


                if (DragDropMoved != null)
                {
                    DragDropEventArgs ddm = new DragDropEventArgs();
                    ddm.SelectedItem = _sourceItem;
                    ddm.Target       = _targetItem;

                    this.DragDropMoved(null, ddm);
                }
            }
            catch
            {
            }
        }
        private string saveStateHelper(string itemsSoFar, ImagedConnectionTreeViewItem currentItem)
        {
            if (currentItem.IsExpanded == true)
            {
                switch (currentItem.Datatype)
                {
                case ImagedConnectionTreeViewDatatype.Folder:
                    itemsSoFar += ",f";
                    break;

                case ImagedConnectionTreeViewDatatype.ConnectionHost:
                    itemsSoFar += ",h";
                    break;

                case ImagedConnectionTreeViewDatatype.ConnectionProtocol:
                    itemsSoFar += ",p";
                    break;
                }

                itemsSoFar += currentItem.ID.ToString();
            }

            if (currentItem.HasItems == true)
            {
                foreach (ImagedConnectionTreeViewItem ictvi in currentItem.Items)
                {
                    itemsSoFar = saveStateHelper(itemsSoFar, ictvi);
                }
            }
            return(itemsSoFar);
        }
        private void treeView_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                Point currentPosition = e.GetPosition(tvConnectionList);


                if ((Math.Abs(currentPosition.X - _LastMouseDown.X) > 10.0) ||
                    (Math.Abs(currentPosition.Y - _LastMouseDown.Y) > 10.0))
                {
                    // Verify that this is a valid drop and then store the drop target
                    ImagedConnectionTreeViewItem item = GetNearestContainer(e.OriginalSource as UIElement);
                    if (CheckDropTarget(_DraggedItem, item))
                    {
                        e.Effects = DragDropEffects.Move;
                    }
                    else
                    {
                        e.Effects = DragDropEffects.None;
                    }
                }
                e.Handled = true;
            }
            catch (Exception)
            {
            }
        }
        private bool CheckDropTarget(ImagedConnectionTreeViewItem _sourceItem, ImagedConnectionTreeViewItem _targetItem)
        {
            //Check whether the target item is meeting your condition
            bool _isEqual = false;

            if (!_sourceItem.Header.ToString().Equals(_targetItem.Header.ToString()))
            {
                _isEqual = true;
            }
            return(_isEqual);
        }
        private ImagedConnectionTreeViewItem GetNearestContainer(UIElement element)
        {
            // Walk up the element tree to the nearest tree view item.
            ImagedConnectionTreeViewItem container = element as ImagedConnectionTreeViewItem;

            while ((container == null) && (element != null))
            {
                element   = VisualTreeHelper.GetParent(element) as UIElement;
                container = element as ImagedConnectionTreeViewItem;
            }
            return(container);
        }
        /// <summary>
        /// Checks, if a value is valid and Sets the IsValidSelection-Value
        /// </summary>
        /// <param name="valiValue"></param>
        public void ValidateValue(ImagedConnectionTreeViewItem valiValue)
        {
            //Check, if the selected Value is an Allowed selection
            switch (valiValue.Datatype)
            {
            case ImagedConnectionTreeViewDatatype.Folder:
                if (_AllowFolderSelection == true)
                {
                    _HasValidSelection = true;
                }
                else
                {
                    _HasValidSelection = false;
                }
                break;

            case ImagedConnectionTreeViewDatatype.ConnectionHost:
                if (_AllowHostSelection == true)
                {
                    _HasValidSelection = true;
                }
                else
                {
                    _HasValidSelection = false;
                }
                break;

            case ImagedConnectionTreeViewDatatype.ConnectionProtocol:
                if (_AllowProtocolSelection == true)
                {
                    _HasValidSelection = true;
                }
                else
                {
                    _HasValidSelection = false;
                }
                break;
            }

            //If it is not allowed, Turn the Backcolor to alternative Color
            if (_HasValidSelection == false)
            {
                btnCmb.Background = _InvalidBackgroundColor;
            }
            else
            {
                if (btnCmdDefaultBackground != null) //Should never be null, but just to be sure
                {
                    btnCmb.Background = btnCmdDefaultBackground;
                }
            }
        }
        /// <summary>
        /// Get the content of a Folder
        /// </summary>
        /// <param name="folderId">id of the folder</param>
        /// <returns></returns>
        private ImagedConnectionTreeViewItem getTreeViewFolderItems(long folderId)
        {
            ImagedConnectionTreeViewItem ret = new ImagedConnectionTreeViewItem(ImagedConnectionTreeViewDatatype.Folder);

            ret.Datatype = ImagedConnectionTreeViewDatatype.Folder; //This is a Folder
            ret.ID       = folderId;                                //The folderID of this folder is the folderId-Parameter

            //Get this Folderproperties
            Folder thisFolder = StorageCore.Core.GetFolder(folderId);

            ret.Header = thisFolder.getName(); //This Displayname of this folder

            #region Determinate the Rights of this Folder
            if (thisFolder.getIsPublic() == true)                          //If the Folder is a Public folder
            {
                if (thisFolder.getOwner() == StorageCore.Core.GetUserId()) //Is the User also the owner?
                {
                    ret.IsPrivate = ImagedConnectionTreeViewRight.PublicAndOwner;
                }
                else //User is not the owner
                {
                    ret.IsPrivate = ImagedConnectionTreeViewRight.Public;
                }
            }
            else
            {
                ret.IsPrivate = ImagedConnectionTreeViewRight.Private;
            }
            #endregion

            //Get Subfolders and its childs and add them to the subitems
            List <Folder> subFolders = StorageCore.Core.GetSubfolders(folderId);
            foreach (Folder folder in subFolders)
            {
                ret.Items.Add(getTreeViewFolderItems(folder.getId()));
            }

            if (_ShowHosts == true)
            {
                //Get Connections and its childs and add them to the subitems
                List <ConnectionHost> subHosts = StorageCore.Core.GetConnectionsInFolder(folderId);
                foreach (ConnectionHost conn in subHosts)
                {
                    ret.Items.Add(getTreeViewConnectionItems(conn.ID));
                }
            }

            return(ret);
        }
        private void treeView_Drop(object sender, DragEventArgs e)
        {
            try
            {
                e.Effects = DragDropEffects.None;
                e.Handled = true;

                // Verify that this is a valid drop and then store the drop target
                ImagedConnectionTreeViewItem TargetItem = GetNearestContainer(e.OriginalSource as UIElement);
                if (TargetItem != null && _DraggedItem != null)
                {
                    _Target   = TargetItem;
                    e.Effects = DragDropEffects.Move;
                }
            }
            catch (Exception)
            {
            }
        }
 /// <summary>
 /// Helper for LoadState
 /// </summary>
 /// <param name="item">Current Item of the Nodetree</param>
 /// <param name="toExpand">ItemID and Datatype of Item to Expand</param>
 /// <returns>Found Item/Or Not</returns>
 private bool expandNode(ImagedConnectionTreeViewItem item, ImagedConnectionTreeViewItem toExpand)
 {
     //Check current Item
     if (item.Datatype == toExpand.Datatype && item.ID == toExpand.ID)
     {
         item.IsExpanded = true;
         return(true);
     }
     else
     {
         //Check other Subitems
         foreach (ImagedConnectionTreeViewItem itm in item.Items)
         {
             if (expandNode(itm, toExpand) == true)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        /// <summary>
        /// Set the Collapsed-State of the Nodes by run with a stateString, created by getState()
        /// </summary>
        /// <param name="stateString"></param>
        public void loadState(string stateString)
        {
            //must have at last 2 signs
            if (stateString.Length < 2)
            {
                return;
            }

            //stateString i.e. = "f1,h3,p2"
            string[] expanded = stateString.Split(',');

            //Expand each ID
            foreach (string aState in expanded)
            {
                ImagedConnectionTreeViewItem toExpand = new ImagedConnectionTreeViewItem();
                toExpand.ID = Convert.ToInt32(aState.Substring(1));

                //Set Datatype
                switch (aState.Substring(0, 1))
                {
                case "f":
                    toExpand.Datatype = ImagedConnectionTreeViewDatatype.Folder;
                    break;

                case "h":
                    toExpand.Datatype = ImagedConnectionTreeViewDatatype.ConnectionHost;
                    break;

                case "p":
                    toExpand.Datatype = ImagedConnectionTreeViewDatatype.ConnectionProtocol;
                    break;
                }

                //Expand Node
                foreach (ImagedConnectionTreeViewItem itm in this.Items)
                {
                    expandNode(itm, toExpand);
                }
            }
        }
        private bool selectValue(ImagedConnectionTreeViewDatatype datatype, long id, ImagedConnectionTreeViewItem parent)
        {
            if (parent.Datatype == datatype && parent.ID == id)
            {
                parent.IsSelected = true;
                ValidateValue(parent);
                return(true);
            }

            bool valueChanged = false;

            foreach (ImagedConnectionTreeViewItem anItem in parent.Items)
            {
                valueChanged = selectValue(datatype, id, anItem);

                if (valueChanged == true)
                {
                    return(true);
                }
            }

            return(valueChanged);
        }
        private void treeView_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point currentPosition = e.GetPosition(tvConnectionList);


                    if ((Math.Abs(currentPosition.X - _LastMouseDown.X) > 10.0) ||
                        (Math.Abs(currentPosition.Y - _LastMouseDown.Y) > 10.0))
                    {
                        _DraggedItem = (ImagedConnectionTreeViewItem)tvConnectionList.SelectedItem;
                        if (_DraggedItem != null)
                        {
                            DragDropEffects finalDropEffect = DragDrop.DoDragDrop(tvConnectionList, tvConnectionList.SelectedValue,
                                                                                  DragDropEffects.Move);
                            //Checking target is not null and item is dragging(moving)
                            if ((finalDropEffect == DragDropEffects.Move) && (_Target != null))
                            {
                                // A Move drop was accepted
                                if (!_DraggedItem.Header.ToString().Equals(_Target.Header.ToString()))
                                {
                                    CopyItem(_DraggedItem, _Target);
                                    _Target      = null;
                                    _DraggedItem = null;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// Get the Content of a Connection
        /// </summary>
        /// <param name="connectionId">ID of the connection</param>
        /// <returns></returns>
        private ImagedConnectionTreeViewItem getTreeViewConnectionItems(long connectionId)
        {
            ImagedConnectionTreeViewItem ret = new ImagedConnectionTreeViewItem(ImagedConnectionTreeViewDatatype.ConnectionHost);

            ret.Datatype = ImagedConnectionTreeViewDatatype.ConnectionHost; //This is a ConnectionHost
            ret.ID       = connectionId;                                    //The connectionId of this folder is the connectionId-Parameter

            //Get this Connection
            ConnectionHost thisConn = StorageCore.Core.GetConnection(connectionId);

            ret.Header = thisConn.Name; //This Displayname of this folder

            #region Determinate the Rights of this Connection
            if (thisConn.IsPublic == true && HidePublicFolder == false) //If the Connections is public and public connections are shown
            {
                if (thisConn.Owner == StorageCore.Core.GetUserId())     //Is the User also the owner?
                {
                    ret.IsPrivate = ImagedConnectionTreeViewRight.PublicAndOwner;
                }
                else //User is not the owner
                {
                    ret.IsPrivate = ImagedConnectionTreeViewRight.Public;
                }
            }
            else
            {
                ret.IsPrivate = ImagedConnectionTreeViewRight.Private;
            }
            #endregion

            #region Get containing Protocols
            List <ConnectionProtocol> protocols = StorageCore.Core.GetConnectionSettings(connectionId);
            if (_ShowProtocols == true)
            {
                foreach (ConnectionProtocol prot in protocols)
                {
                    if (Kernel.GetAvailableProtocols().ContainsKey(prot.Protocol))
                    {
                        ImagedConnectionTreeViewItem newProt = new ImagedConnectionTreeViewItem(ImagedConnectionTreeViewDatatype.ConnectionProtocol);
                        //Get the Icon From Protocol
                        newProt.Icon     = Kernel.GetAvailableProtocols()[prot.Protocol].GetProtocolIcon(Core.ProtocolSystem.ProtocolBase.Declaration.IconType.SMALL);
                        newProt.Datatype = ImagedConnectionTreeViewDatatype.ConnectionProtocol;
                        newProt.ID       = prot.Id;
                        //Get the Displayname from Protocol
                        newProt.Header = Kernel.GetAvailableProtocols()[prot.Protocol].GetProtocolName();

                        ret.Items.Add(newProt);
                    }
                    else
                    {
                    }
                }
            }
            #endregion

            #region Set Icon of the protocol used by this connection
            User userSettings         = StorageCore.Core.GetUserSettings();
            bool foundDefaultProtocol = false;

            //Determinate the default Connection that is used at a doubleclick and set the icon
            foreach (ConnectionProtocol prot in protocols)
            {
                if (prot.Protocol == userSettings.DefaultProtocol)
                {
                    ret.Icon             = Kernel.GetAvailableProtocols()[prot.Protocol].GetProtocolIcon(Core.ProtocolSystem.ProtocolBase.Declaration.IconType.SMALL);
                    foundDefaultProtocol = true;
                    break;
                }
            }

            if (foundDefaultProtocol == false)
            {
                if (protocols.Count > 0 && Kernel.GetAvailableProtocols().ContainsKey(protocols[0].Protocol))
                {
                    ret.Icon = Kernel.GetAvailableProtocols()[protocols[0].Protocol].GetProtocolIcon(Core.ProtocolSystem.ProtocolBase.Declaration.IconType.SMALL);
                }
                else
                {
                    ret.Icon = new BitmapImage(new Uri("pack://application:,,,/beRemote.GUI;component/Images/screen16.png"));
                }
            }

            #endregion

            return(ret);
        }