Click here to Skip to main content
15,885,856 members
Articles
(untagged)

Named Bookmarks Add-In for VS.NET

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
25 Apr 20052 min read 41.1K   921   10  
Add-In for Visual Studio .NET that implement concept of Named Bookmarks
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Collections;

namespace SolBookmarks
{
	/// <summary>
	/// Summary description for BookmarkCollection.
	/// </summary>
	public class BookmarkCollection
	{
		public BookmarkCollection()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		[XmlIgnoreAttribute]
		public  TreeView	tree{ set{ _tree = value; } get { return _tree; } }
		private TreeView	_tree = null;
		
		private ArrayList	_bookmarkList = new ArrayList();

		public  void ReloadTree( TreeView tr )
		{
			tree = tr;
			tree.Nodes.Clear();
			foreach( BookmarkData data in _bookmarkList )
				set_bookmark( data );
		}
		public BookmarkData[] bookmarks 
		{
			set
			{
				_bookmarkList.Clear();
				foreach( BookmarkData data in value )
					_bookmarkList.Add( data );
			}
			get 
			{
				BookmarkData[] rt = new BookmarkData[0];
				if( tree == null )
					return rt;

				_bookmarkList.Clear();
				foreach( TreeNode nd in tree.Nodes )
				{
					if( nd.Nodes.Count > 0 )
						CopyNodes( nd, _bookmarkList );
					else if( nd.Tag.GetType() == typeof( BookmarkData ) ) 
					{
						((BookmarkData)nd.Tag).path = nd.FullPath;
						_bookmarkList.Add( nd.Tag );
					}
				}
				rt = new BookmarkData[_bookmarkList.Count];
				_bookmarkList.CopyTo(rt);
				return rt;
			}
		}
		private void set_bookmark( BookmarkData data )
		{
			if( data == null || data.path == null || data.path.Length <= 0 )
				return;
			string[] path = data.path.Split("\\".ToCharArray());
			TreeNodeCollection	nodes = tree.Nodes;
			TreeNode parent = null;
			int indx;
			for( indx =0; indx < path.Length - 1; indx++ ) 
			{
				parent = null;
				foreach( TreeNode node in nodes )
					if( node.Text == path[indx]  )
						parent = node;
				if( parent == null ) 
				{
					parent = new TreeNode( path[indx] );
					nodes.Add( parent );
				}
				nodes = parent.Nodes;
			}
			(parent = nodes.Add( path[path.Length - 1] )).Tag = data;
			if( data.label != null ) 
			{
				parent.ImageIndex			=	3;
				parent.SelectedImageIndex	=	3;
			} 
			else 
			{
				parent.ImageIndex			=	2;
				parent.SelectedImageIndex	=	2;
			}
		}
		private void CopyNodes( TreeNode node, ArrayList array )
		{
			foreach( TreeNode nd in node.Nodes )
			{
				if( nd.Nodes.Count > 0 )
					CopyNodes( nd, array );
				else if( nd.Tag.GetType() == typeof( BookmarkData ) ) 
				{
					((BookmarkData)nd.Tag).path = nd.FullPath;
					array.Add( nd.Tag );
				}
			}
		}
		public static XmlSerializer	serializer = new XmlSerializer( typeof(SolBookmarks.BookmarkCollection) );

		public static BookmarkCollection LoadBookmarks()
		{
			try 
			{
				string path = Globals.application.Solution.FullName;
				if( path.Length <= 0 )
					throw new ApplicationException();
				path = path.Substring( 0, path.LastIndexOf(".") + 1 ) + "bkm";
				return LoadBookmarks(path);
			} 
			catch
			{
				return null;
			}
		}
		public static BookmarkCollection LoadBookmarks( string path )
		{
			BookmarkCollection cn = null;
			XmlTextReader xtr = null;
			try
			{
				xtr = new XmlTextReader(path);
				cn = (BookmarkCollection)serializer.Deserialize(xtr);
			}
			catch( Exception e )
			{
				System.Diagnostics.Trace.WriteLine( e.ToString() );
			} 
			finally 
			{
				if( xtr != null )
					xtr.Close();
			}
			return cn;
		}
		public void SaveSolution( string solutionName )
		{
			if( solutionName == null || solutionName.Length <= 0 )
				return;
			solutionName = solutionName.Substring( 0, solutionName.LastIndexOf(".") + 1 ) + "bkm";
			Save(solutionName);
		}
		public void Save( )
		{
			if( Globals.application == null || Globals.application.Solution == null )
				return;
			string path = Globals.application.Solution.FullName;
			path = path.Substring( 0, path.LastIndexOf(".") + 1 ) + "bkm";
			Save(path);
		}
		public void Save( string path )
		{
			XmlTextWriter xtw = null;
			try 
			{
				xtw = new XmlTextWriter( path, System.Text.Encoding.Default);
				xtw.Formatting	= System.Xml.Formatting.Indented;
				serializer.Serialize(xtw,this);
			}
			catch( Exception e )
			{
				System.Diagnostics.Trace.WriteLine( e.ToString() );
			}
			finally
			{
				if( xtw != null )
					xtw.Close();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions