using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Threading;
using System.Collections;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Web;
using System.Globalization;
using Microsoft.VisualStudio.Shell.Interop;
using System.Net;
namespace MS.SDKMSDNKeywordIndex
{
/// <summary>
/// Interaction logic for SDKMSDNHelpUtility.xaml
/// </summary>
public partial class SDKMSDNHelpUtility : UserControl
{
private Thread updater = null;
private delegate void bindListDelegate();
private HelpApiShim api = new HelpApiShim();
private string browser = "";
private bool embedTocParam = false;
public SDKMSDNHelpUtility()
{
InitializeComponent();
dataGrid1.Visibility = Visibility.Hidden;
}
private RootNodeViewModel rootNodes()
{
try
{
List<TocNode> nodes = api.GetRootTocNodes();
RootNodeViewModel viewmodel = new RootNodeViewModel(nodes);
return viewmodel;
}
catch (Exception)
{
api = new HelpApiShim();
List<TocNode> nodes = api.GetRootTocNodes();
RootNodeViewModel viewmodel = new RootNodeViewModel(nodes);
return viewmodel;
}
}
private void indexText_TextChanged(object sender, TextChangedEventArgs e)
{
if (updater != null)
{
updater.Abort();
}
string searchText = indexText.Text.Trim();
if (!string.IsNullOrEmpty(searchText))
{
updater = new Thread(UpdateThread);
updater.IsBackground = true;
updater.Start(searchText as object);
}
else
{
dataGrid1.ItemsSource = null;
dataGrid1.Visibility = Visibility.Hidden;
}
}
private void UpdateThread(object searchTextObject)
{
try
{
Thread.Sleep(10);
string searchText = searchTextObject as string;
List<kwdItem> nList = new List<kwdItem>();
nList = api.GetKeywordItems(searchText);
this.Dispatcher.Invoke((bindListDelegate)
delegate()
{
txtExceptionDetails.Text = "";
txtExceptionDetails.Visibility = System.Windows.Visibility.Hidden;
dataGrid1.Visibility = Visibility.Visible;
dataGrid1.ItemsSource = nList;
});
}
catch (WebException ex)
{
this.Dispatcher.Invoke((bindListDelegate)
delegate()
{
txtExceptionDetails.Text = ex.Message + ": Help Agent unavailable, attempting to restart";
txtExceptionDetails.Visibility = System.Windows.Visibility.Visible;
dataGrid1.ItemsSource = null;
api = new HelpApiShim();
});
}
}
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
disambiguator.IsOpen = false;
if (e.AddedItems.Count == 0)
{
return;
}
try
{
txtExceptionDetails.Text = "";
txtExceptionDetails.Visibility = System.Windows.Visibility.Hidden;
kwdItem kwdItem = e.AddedItems[0] as kwdItem;
XmlDocument topics = api.GetTopicXMLForKeyword(kwdItem);
if (topics == null)
return;
XmlNodeList topicNodes = topics.SelectNodes("/keywords/topic");
if (topicNodes.Count > 1)
{
// more than one, so we'll need to disambiguate
List<TopicItem> topicList = new List<TopicItem>();
foreach (XmlNode node in topicNodes)
{
// new up some topic objects
TopicItem topic = new TopicItem(node.Attributes["title"].Value.ToString(), node.Attributes["id"].Value.ToString(), node.Attributes["topicversion"].Value.ToString(), node.Attributes["topiclocale"].Value.ToString());
topicList.Add(topic);
}
//bind and throw the popup
dgTopics.ItemsSource = topicList;
disambiguator.IsOpen = true;
}
else
{
//pass along the topic XML to reduce another round trip
api.NavigateKeywordTopic(topics, browser, embedTocParam);
}
}
catch (WebException ex)
{
txtExceptionDetails.Text = ex.Message + ": Help Agent unavailable, attempting to restart";
txtExceptionDetails.Visibility = System.Windows.Visibility.Visible;
dataGrid1.ItemsSource = null;
api = new HelpApiShim();
}
}
private void dgTopics_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
try
{
TopicItem topic = e.AddedItems[0] as TopicItem;
disambiguator.IsOpen = false;
api.NavigateTopicItem(topic, browser, embedTocParam);
}
catch (WebException ex)
{
txtExceptionDetails.Text = ex.Message + ": Help Agent unavailable, attempting to restart";
txtExceptionDetails.Visibility = System.Windows.Visibility.Visible;
dataGrid1.ItemsSource = null;
api = new HelpApiShim();
}
}
}
private void tocTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
try
{
TocNodeViewModel foo = e.NewValue as TocNodeViewModel;
Dispatcher.BeginInvoke((bindListDelegate)
delegate()
{
api.NavigateTocNode(foo, browser, embedTocParam);
},
System.Windows.Threading.DispatcherPriority.Background
);
}
catch (WebException ex)
{
txtExceptionDetails.Text = ex.Message + ": Help Agent unavailable, attempting to restart";
txtExceptionDetails.Visibility = System.Windows.Visibility.Visible;
dataGrid1.ItemsSource = null;
api = new HelpApiShim();
}
}
private void rb1_Checked(object sender, RoutedEventArgs e)
{
browser = "default";
if (chkTOC != null)
{
chkTOC.IsEnabled = false;
}
}
private void rb2_Checked(object sender, RoutedEventArgs e)
{
browser = "IDE";
chkTOC.IsEnabled = true;
}
private void chkTOC_Checked(object sender, RoutedEventArgs e)
{
embedTocParam = false;
}
private void chkTOC_Unchecked(object sender, RoutedEventArgs e)
{
embedTocParam = true;
}
private void indexText_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
//if the user is hitting enter, grab the first keyword item in the list.
if (e.Key == System.Windows.Input.Key.Enter)
{
if (dataGrid1.Items.Count > 0)
{
dataGrid1.SelectedItem = dataGrid1.Items[0];
}
}
}
private void searchText_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
//if the user is hitting enter, search on the term
if (e.Key == System.Windows.Input.Key.Enter)
{
if (searchText.Text != "")
{
executeSearch(searchText.Text);
}
}
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
//execute search using the text.
if (searchText.Text != "")
{
executeSearch(searchText.Text);
}
}
private void executeSearch(string searchText)
{
try
{
api.NavigateSearchTerm(searchText, browser, embedTocParam);
}
catch (WebException ex)
{
txtExceptionDetails.Text = ex.Message + ": Help Agent unavailable, attempting to restart";
txtExceptionDetails.Visibility = System.Windows.Visibility.Visible;
api = new HelpApiShim();
}
}
}
}