//-----------------------------------------------------------------------
//
// 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.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using global::UserInterface.GUI.Dialogs;
///
/// Empty control for where there is no need for user interaction.
///
public partial class FirstPagePanel : UserControl
{
///
/// Error text in RTF format, part 1.
///
private const string ERRTXT_PART1 = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1053{\\fonttbl{\\f0\\fnil\\fcharset0 Calibri;}}\r\n"
+ "{\\colortbl ;\\red255\\green0\\blue0;}\r\n"
+ "{\\*\\generator Msftedit 5.41.21.2510;}\\viewkind4\\uc1\\pard\\sa200\\sl276\\slmult1\\cf1\\lang29\\b\\f0\\fs22 File \\cf0\\b0 \"";
///
/// Error text in RTF format, part 2.
///
private const string ERRTXT_PART2 = "\"\\cf1\\b was not found!\\par\r\n"
+ "}\r\n";
///
/// File containing info text to display on first page.
///
private static string INFO_FILE = "Texts\\FirstPageInfo.rtf";
///
/// Application tree instance.
///
private ApplicationTree applicationTree;
///
/// Dictionary of interface nodes in the tree.
///
private Dictionary interfaceNodes = new Dictionary();
///
/// Information text form.
///
private InfoForm infoForm;
///
/// Data file directory path.
///
private string dataFileDir;
///
/// Initializes a new instance of the class.
///
/// Application tree instance.
/// Data file directory path.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "CanApp.CustomToolTip", Justification = "Reviewed, intentional.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Reviewed, intentional.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Reviewed.")]
public FirstPagePanel(ApplicationTree applicationTree, string dataFileDir)
{
this.applicationTree = applicationTree;
this.dataFileDir = dataFileDir;
this.InitializeComponent();
this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
foreach (Control control in this.flowLayoutPanel1.Controls)
{
if (control is Button)
{
Button button = (Button)control;
button.Click += new System.EventHandler(this.button_Click);
}
}
this.toolTip1.SetToolTip(this.helpButton, "Help");
}
///
/// Register a selectable node in the tree.
///
/// Key for node.
/// Actual node.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Reviewed.")]
public void registerNode(string key, TreeNode node)
{
this.interfaceNodes.Add(key, node);
foreach (Control control in this.flowLayoutPanel1.Controls)
{
if (control is Button)
{
Button button = (Button)control;
if (!string.IsNullOrEmpty(button.AccessibleName))
{
string[] sa = button.AccessibleName.Split(new char[] { ',' });
foreach (string buttonKey in sa)
{
if (buttonKey == key)
{
button.BackColor = Color.Green;
break;
}
}
}
}
}
}
///
/// Create an error message as a RTF text to show to the user when a file isn't found.
///
/// File name to refer to.
/// Composed RTF text.
private static string getErrorMessage(string fileName)
{
return ERRTXT_PART1 + fileName.Replace("\\", "\\\\") + ERRTXT_PART2;
}
///
/// Load text from file.
///
/// Throws an exception if file not found.
///
///
/// Name of file to load from.
/// Loaded text.
private static string loadTextFromFile(string fileName)
{
string buf = string.Empty;
StreamReader sr = new StreamReader(fileName);
try
{
string line;
while ((line = sr.ReadLine()) != null)
{
buf += line + "\r\n";
}
}
finally
{
sr.Close();
}
return buf;
}
///
/// Adds info text to the panel from an external RTF file.
///
private void addInfoText()
{
string rtf = string.Empty;
string fileName = this.dataFileDir + INFO_FILE;
try
{
string buf = loadTextFromFile(fileName);
rtf = buf;
}
catch
{
rtf = getErrorMessage(fileName);
}
this.infoForm.setText(rtf);
}
///
/// Button clicked event handler.
///
/// Sending object.
/// Event data.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Reviewed.")]
private void button_Click(object sender, System.EventArgs e)
{
if (sender is Button)
{
Button button = (Button)sender;
string txt;
string fileName = this.dataFileDir + "Texts\\" + button.Name + ".rtf";
try
{
txt = loadTextFromFile(fileName);
}
catch
{
txt = getErrorMessage(fileName);
}
MyInfoBox mib = new MyInfoBox();
try
{
bool enableOk = false;
string matchKey = null;
if (!string.IsNullOrEmpty(button.AccessibleName))
{
string[] sa = button.AccessibleName.Split(new char[] { ',' });
foreach (string buttonKey in sa)
{
if (this.interfaceNodes.ContainsKey(buttonKey))
{
enableOk = true;
matchKey = buttonKey;
break;
}
}
}
if (mib.Show("Information", "About " + button.Text, txt, enableOk) == DialogResult.OK)
{
TreeNode node = this.interfaceNodes[matchKey];
this.applicationTree.selectNode(node);
}
}
finally
{
mib.Dispose();
}
}
}
///
/// Handle click on Help button.
///
/// Sending object.
/// Event data.
private void helpButton_Click(object sender, System.EventArgs e)
{
if (this.infoForm == null || !this.infoForm.CanFocus)
{
this.infoForm = new InfoForm();
this.addInfoText();
}
this.infoForm.WindowState = FormWindowState.Normal;
this.infoForm.Show();
this.infoForm.Focus();
}
}
}