//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace UserInterface.GUI
{
using System.Drawing;
using System.Windows.Forms;
using global::SharedObjects.GUI;
using global::SharedObjects.GUI.Popup;
using global::UserInterface.GUI.Objects;
///
/// Class for navigation tree in application.
///
public partial class ApplicationTree : UserControl, IApplicationTree
{
///
/// List of icons.
///
private static readonly string[] iconKeys =
{
"application_cascade.png",
"application_form_magnify.png",
"car.png", // Car
"car_yellow.png", // Car Yellow
"car_red.png", // Car Red
"chart_line.png",
"cog.png", // Cog
"cog_edit.png", // Cog Edit
"cog_green.png", // Cog Green
"connect.png", // Connect
"connect_green.png", // Connect Green
"disconnect.png", // Disconnect
"disconnect_yellow.png", // Disconnect Yellow
"eye.png",
"folder_wrench.png",
"table_multiple.png",
"text_columns.png",
"wrench.png", // Wrench
"wrench_orange.png", // Orange Wrench
"SubaruIcon.png",
};
///
/// List of images in tree.
///
private ImageList imageList = new ImageList();
///
/// Root node instance for interfaces.
///
private TreeNode interfacesRootNode = null;
///
/// Root node instance for preferences.
///
private TreeNode preferencesRootNode = null;
///
/// Current content panel instance.
///
private Panel contentPanel;
///
/// First page panel instance.
///
private FirstPagePanel firstPagePanel;
///
/// Activation callback for hints.
///
private IHintActivator hintActivator;
///
/// Initializes a new instance of the class.
///
public ApplicationTree()
{
this.InitializeComponent();
}
///
/// Gets or sets data file directory path.
///
private string dataFileDir { get; set; }
///
/// Initialize the instance.
///
/// Instance of panel where the tree is located.
/// Hint activator instance.
/// Data file directory path.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Reviewed, intentional.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1500:VariableNamesShouldNotMatchFieldNames", MessageId = "hintActivator", Justification = "Intentional")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1500:VariableNamesShouldNotMatchFieldNames", MessageId = "contentPanel", Justification = "Reviewed, intentional.")]
public void init(Panel contentPanel, IHintActivator hintActivator, string dataFileDir)
{
this.contentPanel = contentPanel;
this.hintActivator = hintActivator;
this.dataFileDir = dataFileDir;
this.addIcons();
this.treeView1.ShowNodeToolTips = true;
this.firstPagePanel = new FirstPagePanel(this, this.dataFileDir);
this.interfacesRootNode = new PanelTreeNode("Interfaces", "connect.png", "connect.png", this.firstPagePanel);
this.treeView1.Nodes.Add(this.interfacesRootNode);
this.interfacesRootNode.Expand();
}
///
/// Set the preferences node page.
///
/// Preferences panel instance.
public void setPreferencesNode(UserControl preferencesPanel)
{
this.preferencesRootNode = new PanelTreeNode("Preferences", "wrench.png", "wrench_orange.png", preferencesPanel);
this.preferencesRootNode.ToolTipText = "Configure your personal preferences.";
this.treeView1.Nodes.Add(this.preferencesRootNode);
}
///
/// Add interface node.
///
/// Node Name.
/// Control to display when node is selected.
public void addInterfaceNode(string name, IInterfacePanel interfacePanel)
{
if (interfacePanel != null)
{
TreeNode interfaceNode = new PanelTreeNode(name, "disconnect_yellow.png", "disconnect_yellow.png", (UserControl)interfacePanel);
this.interfacesRootNode.Nodes.Add(interfaceNode);
interfacePanel.setPanelTreeNode(interfaceNode);
this.firstPagePanel.registerNode(interfacePanel.iPassThru.code, interfaceNode);
}
}
///
/// Select the given node in the tree.
///
/// Node to select.
public void selectNode(TreeNode node)
{
this.treeView1.SelectedNode = node;
this.showNodeContent(node);
}
///
/// Add icons to the tree view.
///
private void addIcons()
{
string missingFiles = string.Empty;
for (int i = 0; i < iconKeys.Length; i++)
{
try
{
Image img = Image.FromFile(this.dataFileDir + "Icons\\" + iconKeys[i]);
this.imageList.Images.Add(iconKeys[i], img);
}
catch
{
missingFiles += this.dataFileDir + "Icons\\" + iconKeys[i] + "\r\n";
}
}
if (missingFiles.Length > 0)
{
MessageBox.Show(
"The following icons couldn't be loaded:\r\n\r\n"
+ missingFiles + "\r\n\r\nThe user experience will be impacted.",
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
this.treeView1.ImageList = this.imageList;
}
///
/// Action to perform after a selection of an item in the tree.
///
/// Sending object.
/// Event data.
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeView tv = (TreeView)sender;
if (tv.Focused)
{
TreeNode node = e.Node;
this.showNodeContent(node);
}
}
///
/// Show the selected node content.
///
/// The node carrying the content to be displayed.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Reviewed, intentional.")]
private void showNodeContent(TreeNode node)
{
if (node is PanelTreeNode)
{
PanelTreeNode panelTreeNode = (PanelTreeNode)node;
this.contentPanel.Controls.Clear();
UserControl userControl = panelTreeNode.userControl;
if ((userControl.Anchor & System.Windows.Forms.AnchorStyles.Left) == System.Windows.Forms.AnchorStyles.Left
&& (userControl.Anchor & System.Windows.Forms.AnchorStyles.Right) == System.Windows.Forms.AnchorStyles.Right)
{
userControl.Width = this.contentPanel.Width - 4;
}
if ((userControl.Anchor & System.Windows.Forms.AnchorStyles.Top) == System.Windows.Forms.AnchorStyles.Top
&& (userControl.Anchor & System.Windows.Forms.AnchorStyles.Bottom) == System.Windows.Forms.AnchorStyles.Bottom)
{
userControl.Height = this.contentPanel.Height - 4;
}
// userControl.Anchor
this.contentPanel.Controls.Add(userControl);
this.hintActivator.showHints(userControl);
if (!this.treeView1.Focused)
{
userControl.Focus();
}
}
}
}
}