PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSVSPackageWPFToolWindow/MyToolWindow.cs

#
C# | 145 lines | 93 code | 19 blank | 33 comment | 6 complexity | eccbf34775e4c3fd706d6f74cc9a2c5b MD5 | raw file
  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Drawing;
  5using System.Data;
  6using System.Windows.Forms;
  7using System.Runtime.InteropServices;
  8using Microsoft.VisualStudio.Shell.Interop;
  9using Microsoft.VisualStudio.Shell;
 10using System.IO;
 11using System.Windows.Controls;
 12using Microsoft.VisualStudio;
 13using EnvDTE;
 14
 15namespace Company.VSPackageWPFToolWindow
 16{
 17    /// <summary>
 18    /// This class implements the tool window exposed by this package and hosts a user control.
 19    ///
 20    /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane, 
 21    /// usually implemented by the package implementer.
 22    ///
 23    /// This class derives from the ToolWindowPane class provided from the MPF in order to use its 
 24    /// implementation of the IVsWindowPane interface.
 25    /// </summary>
 26    [Guid("1c0ed058-d9bf-44f0-97fa-18ae474a30f5")]
 27    public class MyToolWindow : ToolWindowPane, Microsoft.VisualStudio.OLE.Interop.IServiceProvider
 28    {
 29        // This is the user control hosted by the tool window; it is exposed to the base class 
 30        // using the Window property. Note that, even if this class implements IDispose, we are
 31        // not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
 32        // the object returned by the Window property.
 33        private MyControl control;
 34        private WPFControl wpfControl;
 35
 36        /// <summary>
 37        /// Standard constructor for the tool window.
 38        /// </summary>
 39        public MyToolWindow() :
 40            base(null)
 41        {
 42            // Set the window title reading it from the resources.
 43            this.Caption = Resources.ToolWindowTitle;
 44            // Set the image that will appear on the tab of the window frame
 45            // when docked with an other window
 46            // The resource ID correspond to the one defined in the resx file
 47            // while the Index is the offset in the bitmap strip. Each image in
 48            // the strip being 16x16.
 49            this.BitmapResourceID = 301;
 50            this.BitmapIndex = 1;
 51
 52
 53            control = new MyControl();
 54            wpfControl = control.WPFControl;
 55        }
 56
 57        public override void OnToolWindowCreated()
 58        {
 59            base.OnToolWindowCreated();
 60            InitializeTreeViewContent();
 61
 62        }
 63
 64        private void InitializeTreeViewContent()
 65        {
 66            DirectoryInfo myDocInfo =
 67                new DirectoryInfo(
 68                    Environment.GetFolderPath(
 69                        Environment.SpecialFolder.MyDocuments
 70                    )
 71                );
 72            IntializeTreeViewContentRecursively(myDocInfo, wpfControl.WPFTreeView.Items);
 73        }
 74
 75        private void IntializeTreeViewContentRecursively(
 76            DirectoryInfo myDocInfo, ItemCollection itemCollection)
 77        {            
 78            if (myDocInfo == null)
 79                return;
 80
 81            try
 82            {
 83                TreeViewItem item = new TreeViewItem();
 84                item.Header = myDocInfo.Name;
 85                itemCollection.Add(item);
 86
 87                DirectoryInfo[] subDirs = myDocInfo.GetDirectories();
 88                if (subDirs != null)
 89                {
 90                    foreach (DirectoryInfo dir in subDirs)
 91                        IntializeTreeViewContentRecursively(dir, item.Items);
 92                }
 93
 94                FileInfo[] files = myDocInfo.GetFiles();
 95                if (files != null)
 96                {
 97                    foreach (FileInfo file in files)
 98                    {
 99                        TreeViewItem fileItem = new TreeViewItem();
100                        fileItem.Header = file.Name;
101                        fileItem.Tag = file.FullName;
102                        // fileItem.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(fileItem_MouseDoubleClick);
103                        item.Items.Add(fileItem);
104                    }
105                }
106            }
107            catch
108            {
109                return;
110            }
111        }
112
113        //void fileItem_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
114        //{
115        //    TreeViewItem item = sender as TreeViewItem;
116        //    DTE dte = GetService(typeof(DTE)) as DTE;
117
118        //    dte.ExecuteCommand("File.OpenFile", item.Tag as string);
119            
120        //}
121
122        /// <summary>
123        /// This property returns the handle to the user control that should
124        /// be hosted in the Tool Window.
125        /// </summary>
126        override public IWin32Window Window
127        {
128            get
129            {
130                return (IWin32Window)control;
131            }
132        }
133
134
135        #region IServiceProvider Members
136
137        public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
138        {
139            ppvObject = IntPtr.Zero;
140            return VSConstants.S_OK;
141        }
142
143        #endregion
144    }
145}