📄 documentprotectorform.cs
字号:
private void ToggleProtection()
{
Debug.Assert(this.wordProtector != null);
if (this.wordProtector.IsProtected)
this.UnprotectDocument();
else
this.ProtectDocument();
}
/// <summary>
/// Unprotects the current document if it is already protected.
/// </summary>
private void UnprotectDocument()
{
Debug.Assert(this.wordProtector != null);
Debug.Assert(this.wordProtector.IsProtected);
try
{
// Attempt to unprotect the current document with
// the last password known for this document.
this.wordProtector.Unprotect(
DataProtection.UnprotectData(this.documentPassword));
}
catch (System.Runtime.InteropServices.COMException)
{
// Document has a different password than the last
// one known, so ask the user for the password.
UnprotectForm unprotectForm = new UnprotectForm();
if (unprotectForm.ShowDialog(this) == DialogResult.OK)
{
// If the user provided a password, use it to
// unprotect the document. If it works,
// store the pasword for future reference.
try
{
this.wordProtector.Unprotect(unprotectForm.Password);
this.documentPassword =
DataProtection.ProtectData(unprotectForm.Password, this.Text);
}
catch (System.Runtime.InteropServices.COMException)
{
// The user did not provide a valid password
// to unprotect the document.
MessageBox.Show(
Properties.DocumentProtector.incorrectPasswordText,
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
/// <summary>
/// Protects the current document if it is already unprotected.
/// </summary>
private void ProtectDocument()
{
Debug.Assert(this.wordProtector != null);
Debug.Assert(!this.wordProtector.IsProtected);
this.wordProtector.Protect(
DataProtection.UnprotectData(this.documentPassword));
}
/// <summary>
/// Allows the user to modify/enter a password
/// to use to protect and unprotect the current document.
/// </summary>
private void SetDocumentPassword()
{
// Create a new PasswordForm with the last known
// password for the current document.
PasswordForm passwordForm = new PasswordForm(
DataProtection.UnprotectData(this.documentPassword));
if (passwordForm.ShowDialog(this) == DialogResult.OK)
{
// If the user submited a password, store it for
// future reference.
this.documentPassword = DataProtection.ProtectData(
passwordForm.Password, this.Text);
}
}
#endregion
#endregion
#region Controls Enabling/Disabling Methods
/// <summary>
/// Sets the document specific controls to be enabled or not.
/// </summary>
/// <param name="enable">
/// True to enable the document specific
/// controls and false to disable them.
/// </param>
private void SetDocumentControls(bool enable)
{
Debug.Assert(this.saveMenuItem != null);
Debug.Assert(this.protectMenuItem != null);
this.saveMenuItem.Enabled = enable;
this.protectMenuItem.Enabled = enable;
this.SetProtectionControls(!((this.wordProtector == null) ||
(this.wordProtector.IsProtected)));
}
/// <summary>
/// Sets the document protection specific controls to be enabled or not.
/// </summary>
/// <param name="enable">
/// True to enable the document protection specific
/// controls and false to disable them.
/// </param>
private void SetProtectionControls(bool enable)
{
Debug.Assert(this.passwordMenuItem != null);
Debug.Assert(this.protectMenuItem != null);
Debug.Assert(this.resetButton != null);
Debug.Assert(this.protectorTooltip != null);
Debug.Assert(this.documentItemsGroupBox != null);
Debug.Assert(this.documentStatusPanel != null);
this.passwordMenuItem.Enabled = enable;
this.resetButton.Enabled = enable;
this.documentItemsGroupBox.Enabled = enable;
this.protectorTooltip.Active = enable;
if (this.wordProtector == null)
{
// Special case for when no document
// has been opened yet.
Debug.Assert(!enable);
this.protectMenuItem.Text =
Properties.DocumentProtector.protectMenuItem_Text;
this.documentStatusPanel.Text = String.Empty;
}
else if (enable)
{
this.protectMenuItem.Text =
Properties.DocumentProtector.protectMenuItem_Text;
this.documentStatusPanel.Text =
Properties.DocumentProtector.unprotectedDocumentText;
}
else
{
this.protectMenuItem.Text =
Properties.DocumentProtector.unprotectMenuItem_Text;
this.documentStatusPanel.Text =
Properties.DocumentProtector.protectedDocumentText;
}
}
#endregion
#region Tree Modifying Methods
/// <summary>
/// Creates a TreeNode representation of a given WordArea.
/// </summary>
/// <param name="area">The area for which to create a TreeNode.</param>
/// <returns>Returns a TreeNode that represents the given WordArea.</returns>
private static TreeNode CreateAreaTreeNode(WordProtection.WordArea area)
{
Debug.Assert(area != null);
TreeNode newNode = new TreeNode(area.Name);
newNode.Tag = area;
newNode.Checked = !area.IsProtected;
return newNode;
}
/// <summary>
/// Removes all nodes from the tree and
/// the first level child nodes.
/// </summary>
private void CleanTree()
{
Debug.Assert(this.documentItemsTree != null);
Debug.Assert(this.bookmarkRootNode != null);
Debug.Assert(this.formfieldRootNode != null);
Debug.Assert(this.xmlNodesRootNode != null);
// Remove the root node from the tree.
if (this.rootNode != null)
{
Debug.Assert(this.documentItemsTree.Nodes.Contains(this.rootNode));
this.documentItemsTree.Nodes.Remove(this.rootNode);
}
// Clear the first level child nodes.
this.bookmarkRootNode.Nodes.Clear();
this.formfieldRootNode.Nodes.Clear();
this.xmlNodesRootNode.Nodes.Clear();
// Clear the check marks for the nodes.
this.bookmarkRootNode.Checked = false;
this.formfieldRootNode.Checked = false;
this.xmlNodesRootNode.Checked = false;
}
/// <summary>
/// Fills the tree with the empty document tree.
/// </summary>
private void FillTree()
{
Debug.Assert(this.documentItemsTree != null);
Debug.Assert(this.wordProtector != null);
Debug.Assert(this.bookmarkRootNode != null);
Debug.Assert(this.formfieldRootNode != null);
Debug.Assert(this.xmlNodesRootNode != null);
// Create the empty document tree.
this.rootNode = new TreeNode(this.wordProtector.Name);
this.documentItemsTree.Nodes.Add(this.rootNode);
this.rootNode.Nodes.Add(this.bookmarkRootNode);
this.rootNode.Nodes.Add(this.formfieldRootNode);
this.rootNode.Nodes.Add(this.xmlNodesRootNode);
// Populate the tree with the document areas.
this.FillTree(this.wordProtector.ProtectableAreas);
}
/// <summary>
/// Populates the top level area nodes with
/// the corresponding areas from the Word document.
/// </summary>
/// <param name="areas"></param>
private void FillTree(WordProtection.ProtectableAreaCollection areas)
{
Debug.Assert(this.bookmarkRootNode != null);
Debug.Assert(this.formfieldRootNode != null);
Debug.Assert(this.xmlNodesRootNode != null);
Debug.Assert(this.documentItemsTree != null);
Debug.Assert(areas != null);
// Loop through the collection of areas and add
// them to the appropriate area parent node.
for (int i = 0; i < areas.Count; i++)
{
WordProtection.XmlNodeArea xmlArea;
WordProtection.WordArea area = areas[i];
TreeNode newNode = CreateAreaTreeNode(area);
if (area is WordProtection.BookmarkArea)
{
this.bookmarkRootNode.Nodes.Add(newNode);
}
else if (area is WordProtection.FormFieldArea)
{
this.formfieldRootNode.Nodes.Add(newNode);
}
else if ((xmlArea = area as WordProtection.XmlNodeArea) != null)
{
// Only add root XML nodes to the XML Nodes
// Root Node. Otherwise, skip it here.
if (xmlArea.Parent == null)
{
// Add it to the root XML Nodes node
// and then populate its children.
this.xmlNodesRootNode.Nodes.Add(newNode);
FillTree(newNode, xmlArea.ChildAreas);
}
}
}
}
/// <summary>
/// Populates the child XML nodes as TreeNodes of the given parent.
/// </summary>
/// <param name="parent">TreeNode to add child nodes to.</param>
/// <param name="childAreas">Collection of XMLNodeAreas to add to the parent.</param>
private void FillTree(TreeNode parent,
System.Collections.Generic.IList<WordProtection.XmlNodeArea> childAreas)
{
Debug.Assert(parent != null);
Debug.Assert(childAreas != null);
// Loop through all of the childAreas of the given parent.
for (int i = 0; i < childAreas.Count; i++)
{
// Create a new TreeNode representation of the area.
WordProtection.XmlNodeArea xmlNodeArea = childAreas[i];
TreeNode newNode = CreateAreaTreeNode(xmlNodeArea);
parent.Nodes.Add(newNode);
// Populate the newly created node with its children.
this.FillTree(newNode, xmlNodeArea.ChildAreas);
}
}
/// <summary>
/// Builds the tree representation of the current document.
/// </summary>
private void BuildTree()
{
Debug.Assert(this.documentItemsTree != null);
Debug.Assert(this.wordProtector != null);
// While building the tree, stop listening for
// items on the tree being checked.
this.documentItemsTree.AfterCheck -= new TreeViewEventHandler(documentItemsTree_AfterCheck);
try
{
this.CleanTree();
this.FillTree();
this.PropagateTreeChanges(this.documentItemsTree.TopNode);
this.documentItemsTree.ExpandAll();
this.rootNode.EnsureVisible();
}
finally
{
// Now that the tree is done being built, start
// listening for items on the tree being checked.
this.documentItemsTree.AfterCheck += new TreeViewEventHandler(documentItemsTree_AfterCheck);
}
}
/// <summary>
/// Makes sure that the tree has the correct checked status
/// for the given node and its child nodes.
/// </summary>
/// <param name="node">The node to propagate changes for.</param>
private void PropagateTreeChanges(TreeNode node)
{
Debug.Assert(node != null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -