/OfflineClient-RSS/GuidanceExplorerNT/GuidanceExplorer/Controls/LibraryNode.cs
C# | 313 lines | 225 code | 35 blank | 53 comment | 39 complexity | 8a7f36574176e378699a073a663c49cd MD5 | raw file
- //===============================================================================
- // Microsoft patterns & practices
- // Guidance Explorer Offline Client
- //===============================================================================
- // Copyright Microsoft Corporation. All rights reserved.
- // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
- // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
- // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- // FITNESS FOR A PARTICULAR PURPOSE.
- //===============================================================================
-
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using GuidanceExplorer.Model;
-
- namespace GuidanceExplorer.Controls
- {
- /// <summary>
- /// The node for a single library.
- /// </summary>
- [MenuItemAttribute(10, "&File\\&New Item", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), typeof(NewItemMenuItemProvider), Keys.None)]
- [MenuItemAttribute(11, "&File\\New &Folder", GuidanceExplorer.Commands.NewFolder, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(12, "&File\\New &View", GuidanceExplorer.Commands.NewView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(13, "&File\\New Search Vie&w", GuidanceExplorer.Commands.NewSearchView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(20, "&File\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(30, "&File\\&Import Item...", GuidanceExplorer.Commands.ImportGuidanceItemToLibrary, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(40, "&File\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(50, "&File\\Save &Item as Xml...", "Save Items as Xml...", GuidanceExplorer.Commands.SaveItemAsXml, Keys.None)]
- [MenuItemAttribute(60, "&File\\-", null, Keys.None)]
- [MenuItemAttribute(70, "&File\\&Save Library as...", GuidanceExplorer.Commands.SaveLibraryAs, Keys.None)]
- [MenuItemAttribute(80, "&File\\Save Item &as...", "Save Items as...", GuidanceExplorer.Commands.SaveItemAs, Keys.None)]
- [MenuItemAttribute(90, "&File\\-", null, Keys.None)]
- [MenuItemAttribute(94, "&File\\Suscribe to &Library\\From &File...", GuidanceExplorer.Commands.SuscribeLibraryFromFile, Keys.None)]
- [MenuItemAttribute(95, "&File\\Suscribe to &Library\\From &Web...", GuidanceExplorer.Commands.SuscribeLibraryFromWeb, Keys.None)]
- [MenuItemAttribute(96, "&File\\Suscribe to &Library\\From &Rss...", GuidanceExplorer.Commands.SuscribeLibraryFromRss, Keys.None)]
- [MenuItemAttribute(97, "&File\\S&ynchronize Libraries", Commands.Synchronize, Keys.None)]
- [MenuItemAttribute(98, "&File\\-", null, Keys.None)]
- [MenuItemAttribute(100, "&File\\&Rename Library", GuidanceExplorer.Commands.RenameLibrary, Keys.None)]
- [MenuItemAttribute(110, "&File\\&Delete Library", GuidanceExplorer.Commands.DeleteLibrary, Keys.None)]
- [MenuItemAttribute(120, "&File\\-", null, Keys.None)]
- [MenuItemAttribute(128, "&File\\Add/Remove &Guidance Type...", GuidanceExplorer.Commands.AddRemoveGuidanceType, Keys.None)]
- [MenuItemAttribute(129, "&File\\-", null, Keys.None)]
- [MenuItemAttribute(130, "&File\\&Exit", GuidanceExplorer.Commands.FileExit, Keys.None)]
-
- [MenuItemAttribute(200, "&Edit\\&Undo drag/drop", GuidanceExplorer.Commands.UndoDragAndDrop, Keys.Control | Keys.Z)]
- [MenuItemAttribute(210, "&Edit\\-", null, Keys.None)]
- [MenuItemAttribute(230, "&Edit\\&Add To View...", GuidanceExplorer.Commands.MoveGuidanceItemToView, Keys.None)]
- [MenuItemAttribute(250, "&Edit\\Dele&te item", GuidanceExplorer.Commands.DeleteGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(260, "&Edit\\-", null, Keys.None)]
- [MenuItemAttribute(270, "&Edit\\&Select All", GuidanceExplorer.Commands.SelectAll, Keys.Control | Keys.A)]
-
- [MenuItemAttribute(300, "&View\\Reset &Sort Order", GuidanceExplorer.Commands.ResetSortOrder, Keys.None)]
- [MenuItemAttribute(310, "&View\\&Reset Columns", GuidanceExplorer.Commands.ResetColumns, Keys.None)]
- [MenuItemAttribute(320, "&View\\Reset &Filters", GuidanceExplorer.Commands.ResetFilters, Keys.None)]
- [MenuItemAttribute(330, "&View\\&Column Chooser", GuidanceExplorer.Commands.ColumnChooser, Keys.None)]
-
- [MenuItemAttribute(400, "&Help\\&About", GuidanceExplorer.Commands.HelpAbout, Keys.None)]
-
- [MenuItemAttribute(500, "context\\New Item", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), typeof(NewItemMenuItemProvider), Keys.None)]
- [MenuItemAttribute(501, "context\\New Folder", GuidanceExplorer.Commands.NewFolder, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(502, "context\\New View", GuidanceExplorer.Commands.NewView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(503, "context\\New Search View", GuidanceExplorer.Commands.NewSearchView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(505, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(510, "context\\Save Library As...", GuidanceExplorer.Commands.SaveLibraryAs, Keys.None)]
- [MenuItemAttribute(515, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(520, "context\\Import Item...", GuidanceExplorer.Commands.ImportGuidanceItemToLibrary, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(525, "context\\-", null, Keys.None)]
- [MenuItemAttribute(530, "context\\Export Library...", GuidanceExplorer.Commands.ExportLibrary, Keys.None)]
- [MenuItemAttribute(535, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
- [MenuItemAttribute(540, "context\\Rename Library", GuidanceExplorer.Commands.RenameLibrary, Keys.None)]
- [MenuItemAttribute(550, "context\\Delete Library", GuidanceExplorer.Commands.DeleteLibrary, Keys.None)]
- [MenuItemAttribute(560, "context\\-", null, typeof(WebSubscribedLibraryCondition), Keys.None)]
- [MenuItemAttribute(570, "context\\Synchronize Library", GuidanceExplorer.Commands.Synchronize, typeof(WebSubscribedLibraryCondition), Keys.None)]
- [MenuItemAttribute(580, "context\\Refresh Content", GuidanceExplorer.Commands.RefreshFileSuscribedLibrary, typeof(IsFileSuscribedLibraryCondition), Keys.None)]
- #if ONLINEENABLED
- [MenuItemAttribute(590, "context\\Publish (experimental)...", GuidanceExplorer.Commands.PublishLibrary, Keys.None)]
- #endif
- public class LibraryNode : ModelNode<Library>
- {
- private Dictionary<string, GuidanceTypeNode> _guidanceTypeNodes = new Dictionary<string, GuidanceTypeNode>();
-
- /// <summary>
- /// Factory.
- /// </summary>
- /// <returns></returns>
- public static LibraryNode New()
- {
- return new LibraryNode();
- }
-
- /// <summary>
- /// Default constructor.
- /// </summary>
- /// <param name="writeProtection">Defines the write protection of the library.</param>
- private LibraryNode()
- : base("<a library>", 2, 2)
- {
- }
-
- /// <summary>
- /// The Library instance.
- /// </summary>
- public override Library ModelInstance
- {
- set
- {
- base.ModelInstance = value;
-
- if (value.FileSuscribed || value.WebSuscribed)
- {
- if (value.ReadProtection == ReadProtection.ReadOnly)
- {
- this.SelectedImageIndex = this.ImageIndex = 7;
- }
- else
- {
- this.SelectedImageIndex = this.ImageIndex = 6;
- }
- }
- else
- {
- if (value.ReadProtection == ReadProtection.ReadOnly)
- {
- this.SelectedImageIndex = this.ImageIndex = 3;
- }
- else
- {
- this.SelectedImageIndex = this.ImageIndex = 2;
- }
- }
- }
- }
-
- /// <summary>
- /// Add a new GuidanceType node to the LibraryNode
- /// </summary>
- /// <param name="typeNode"></param>
- public void AddGuidanceTypeNode(GuidanceTypeNode typeNode)
- {
- string typeKey;
-
- if (string.IsNullOrEmpty(typeNode.ModelInstance.NodeDisplayName))
- {
- typeKey = typeNode.ModelInstance.Name;
- }
- else
- {
- typeKey = typeNode.ModelInstance.NodeDisplayName;
- typeNode.Text = typeKey;
- }
-
- _guidanceTypeNodes[typeKey] = typeNode;
-
- int i = 0;
- if (this.ModelInstance.ReadProtection == ReadProtection.ReadWrite)
- {
- for (i = 0; i < Nodes.Count; i++)
- {
- if (string.Compare(typeNode.Text, Nodes[i].Text) < 0 || !(Nodes[i] is GuidanceTypeNode))
- {
- break;
- }
- }
- }
- else
- {
- while (i < Nodes.Count && !(Nodes[i] is GuidanceTypeNode))
- {
- i++;
- }
-
- for (; i < Nodes.Count; i++)
- {
- if (string.Compare(typeNode.Text, Nodes[i].Text) < 0 || !(Nodes[i] is GuidanceTypeNode))
- {
- break;
- }
- }
- }
-
- Nodes.Insert(i, typeNode);
- }
-
- /// <summary>
- /// Removes the GuidanceType node for the specifies GuidanceType
- /// </summary>
- /// <param name="guidanceType"></param>
- public void RemoveGuidanceTypeNode(GuidanceType guidanceType)
- {
- string typeKey;
-
- if (string.IsNullOrEmpty(guidanceType.NodeDisplayName))
- {
- typeKey = guidanceType.Name;
- }
- else
- {
- typeKey = guidanceType.NodeDisplayName;
- }
-
- if (_guidanceTypeNodes.ContainsKey(typeKey))
- {
- Nodes.Remove(_guidanceTypeNodes[typeKey]);
- _guidanceTypeNodes[typeKey] = null;
- }
- }
-
- /// <summary>
- /// Return whether the LibraryNode contains a GuidanceType node for the specified GuidanceType
- /// </summary>
- /// <param name="guidanceType"></param>
- /// <returns></returns>
- public bool HasGuidanceTypeNode(GuidanceType guidanceType)
- {
- string typeKey;
-
- if (String.IsNullOrEmpty(guidanceType.NodeDisplayName))
- {
- typeKey = guidanceType.Name;
- }
- else
- {
- typeKey = guidanceType.NodeDisplayName;
- }
-
- return _guidanceTypeNodes.ContainsKey(typeKey) && _guidanceTypeNodes[typeKey] != null;
- }
-
- /// <summary>
- /// Whether the node is editable. ReadOnly libraries are not editable.
- /// </summary>
- public override bool IsEditable
- {
- get { return (ModelInstance.ReadProtection == ReadProtection.ReadWrite); }
- }
-
- public override bool CanDrop(IModelItem modelNode)
- {
- //If the library is read only, then all operations are cancelled.
- if (ModelInstance.ReadProtection == ReadProtection.ReadOnly)
- {
- return false;
- }
-
- ViewNode viewNode = modelNode as ViewNode;
- FolderNode folderNode = modelNode as FolderNode;
- SearchViewNode searchNode = modelNode as SearchViewNode;
- if (viewNode != null)
- {
- /*
- * Test for views:
- * Only views of the same library can be drop in the library.
- * The root views can't be droped in the library.
- */
- Library otherLibrary = viewNode.ModelInstance.GetContainerLibrary();
-
- //Root views cant be dragged
- if (viewNode.ModelInstance.RootView)
- {
- return false;
- }
-
- if (!Object.ReferenceEquals(otherLibrary, ModelInstance))
- {
- return false;
- }
-
- if (Object.ReferenceEquals(viewNode.ModelInstance.Container, ModelInstance))
- {
- return false;
- }
-
- return true;
- }
- else if (folderNode != null)
- {
- /*
- * Test for folders:
- * Only folders of the same library can be drop in the library.
- * The root folders can't be droped in the library.
- */
- Library otherLibrary = folderNode.ModelInstance.GetContainerLibrary();
-
- if (!Object.ReferenceEquals(otherLibrary, ModelInstance))
- {
- return false;
- }
-
- if (Object.ReferenceEquals(folderNode.ModelInstance.Container, ModelInstance))
- {
- return false;
- }
-
- return true;
- }
- else if (searchNode != null)
- {
- //Root views cant be dragged
- if (searchNode.ModelInstance.RootView)
- {
- return false;
- }
-
- return true;
- }
-
- return false;
- }
- }
- }
-