Back to index

lightning-sunbird  0.9+nobinonly
BookmarksTreeView.cs
Go to the documentation of this file.
00001 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Manticore.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Silverstone Interactive.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Ben Goodger <ben@netscape.com> (Original Author)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039  
00040 namespace Silverstone.Manticore.Bookmarks
00041 {
00042   using System;
00043   using System.Drawing;
00044   using System.Windows.Forms;
00045   using System.Collections;
00046   
00047   using Silverstone.Manticore.Core;
00048   using Silverstone.Manticore.Toolkit;
00049   
00053   public class BookmarksTreeView : ManticoreTreeView
00054   {
00055     protected BaseTreeBuilder mBuilder;
00056     protected Queue mFilterAttributes;
00057 
00058     public BookmarksTreeView(String aRoot)
00059     {
00060       mBuilder = new BaseTreeBuilder(this, null);
00061       mBuilder.Root = aRoot;
00062       mBuilder.DataStore = DataStoreRegistry.GetDataStore("Bookmarks");
00063       mBuilder.DataStore.AddObserver(mBuilder);
00064 
00065       // Use bright green as transparent colour
00066       mIconTransparentColor = ColorTranslator.FromOle(0x00FF00);
00067 
00068       ImageList = new ImageList();
00069       // Built in Bookmark icons
00070       ImageList.Images.Add(Image.FromFile(@"resources\bookmark-folder-closed.png"), mIconTransparentColor);
00071       ImageList.Images.Add(Image.FromFile(@"resources\bookmark-folder-open.png"), mIconTransparentColor);
00072       ImageList.Images.Add(Image.FromFile(@"resources\bookmark.png"), mIconTransparentColor);
00073 
00074       // Don't draw lines to root
00075       ShowRootLines = false;
00076       ShowPlusMinus = true;
00077 
00078       AfterLabelEdit += new NodeLabelEditEventHandler(OnAfterLabelEdit);
00079       AfterExpand += new TreeViewEventHandler(OnAfterExpand);
00080       AfterCollapse += new TreeViewEventHandler(OnAfterCollapse);
00081     }
00082 
00083     public void Build()
00084     {
00085       mBuilder.Build();
00086     }
00087 
00088     public String Root 
00089     {
00090       get 
00091       {
00092         return mBuilder.Root;
00093       }
00094       set 
00095       {
00096         if (mBuilder.Root != value)
00097           mBuilder.Root = value;
00098       }
00099     }
00100 
00101     public void AddCriteria(String[] aAttrValuePair)
00102     {
00103       if (mFilterAttributes == null)
00104         mFilterAttributes = new Queue();
00105       mFilterAttributes.Enqueue(aAttrValuePair);
00106     }
00107 
00108     public override bool ShouldBuild(CommandTarget aTarget)
00109     {
00110       Bookmarks bmks = ServiceManager.Bookmarks;
00111 
00112       if (mFilterAttributes != null) 
00113       {
00114         IEnumerator criteria = mFilterAttributes.GetEnumerator();
00115         while (criteria.MoveNext()) 
00116         {
00117           String[] singleCriteria = criteria.Current as String[];
00118             if (bmks.GetBookmarkAttribute(aTarget.Data as String, singleCriteria[0]) != singleCriteria[1])
00119               return false;
00120         }
00121       }
00122       return true;
00123     }
00124 
00125     public override int GetIconIndex(CommandTarget aTarget)
00126     {
00127       int index = 2;
00128       if (aTarget.IsContainer) 
00129       {
00130         index = 0;
00131         if (aTarget.IsOpen) 
00132           index = 1;
00133       }
00134 
00135       int fileIndex = GetIconIndex(aTarget.IconURL);
00136       if (fileIndex != -1)
00137         index = fileIndex;
00138       return index;
00139     }
00140 
00145     protected ManticoreTreeNode GetRootItem()
00146     {
00147       if (Nodes.Count == 0) 
00148         return null;
00149       return Nodes[0] as ManticoreTreeNode;
00150     }
00151 
00152     public void NewFolder()
00153     {
00154       ManticoreTreeNode root = GetRootItem();
00155       if (root != null) 
00156       {
00157         ManticoreTreeNode temp = new ManticoreTreeNode("New Folder", null);
00158         root.Nodes.Add(temp);
00159         LabelEdit = true;
00160         temp.BeginEdit();
00161       }
00162     }
00163 
00164     protected void OnAfterLabelEdit(Object sender, NodeLabelEditEventArgs e)
00165     {
00166       ManticoreTreeNode root = GetRootItem();
00167       if (root != null && e.Label != "") 
00168       {
00169         ManticoreTreeNode temp = e.Node as ManticoreTreeNode;
00170         String parentID = root.Data as String;
00171         Bookmarks bmks = ServiceManager.Bookmarks;
00172         String bookmarkID = bmks.CreateBookmark(e.Label, parentID, -1);
00173         bmks.SetBookmarkAttribute(bookmarkID, "container", "true");
00174         bmks.SetBookmarkAttribute(bookmarkID, "open", "true");
00175         e.Node.Text = "FruitLoop";
00176         e.Node.EndEdit(false);
00177         root.Nodes.Remove(temp);
00178         LabelEdit = false;
00179       }
00180       else 
00181         e.Node.EndEdit(true);
00182     }
00183 
00184     protected void OnAfterExpand(Object sender, TreeViewEventArgs e)
00185     {
00186       ManticoreTreeNode node = e.Node as ManticoreTreeNode;
00187       String nodeID = node.Data as String;
00188       Bookmarks bmks = ServiceManager.Bookmarks;
00189       bmks.SetBookmarkAttribute(nodeID, "open", "true");
00190       node.ImageIndex = 1;
00191     }
00192 
00193     protected void OnAfterCollapse(Object sender, TreeViewEventArgs e)
00194     {
00195       ManticoreTreeNode node = e.Node as ManticoreTreeNode;
00196       String nodeID = node.Data as String;
00197       Bookmarks bmks = ServiceManager.Bookmarks;
00198       bmks.SetBookmarkAttribute(nodeID, "open", "false");
00199       node.ImageIndex = 0;
00200     }
00201        }
00202 }
00203