#region Disclaimer / License
// Copyright (C) 2009, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OSGeo.MapGuide.MaestroAPI;
using OSGeo.MapGuide.MaestroAPI.Resource;
using OSGeo.MapGuide.MaestroAPI.Exceptions;
namespace Maestro.Base
{
///
/// A dialog for displaying validation results
///
public partial class ValidationResultsDialog : Form
{
private List> m_issues = new List>();
private Action _openAction;
///
/// Creates a new instance
///
///
///
public ValidationResultsDialog(string resourceId, ValidationIssue[] issues)
: this(resourceId, issues, null)
{ }
///
/// Creates a new instance
///
///
///
///
public ValidationResultsDialog(string resourceId, ValidationIssue[] issues, Action openAction)
: this(
new List>(
new KeyValuePair[] {
new KeyValuePair(resourceId, issues)
}
),
openAction
)
{ }
///
/// Creates a new instance
///
///
public ValidationResultsDialog(List> issues)
: this(issues, null)
{
}
///
/// Creates a new instance
///
///
///
public ValidationResultsDialog(List> issues, Action openAction)
: this()
{
m_issues = issues;
PopulateIssues();
_openAction = openAction;
btnOpen.Visible = (_openAction != null);
btnOpen.Enabled = false;
}
private void PopulateIssues()
{
lstIssues.Items.Clear();
foreach (KeyValuePair e in m_issues)
{
IEnumerable items = null;
if (chkErrors.Checked)
{
if (items == null)
items = e.Value.Where(x => x.Status == ValidationStatus.Error);
else
items = items.Concat(e.Value.Where(x => x.Status == ValidationStatus.Error));
}
if (chkWarnings.Checked)
{
if (items == null)
items = e.Value.Where(x => x.Status == ValidationStatus.Warning);
else
items = items.Concat(e.Value.Where(x => x.Status == ValidationStatus.Warning));
}
if (chkNotices.Checked)
{
if (items == null)
items = e.Value.Where(x => x.Status == ValidationStatus.Information);
else
items = items.Concat(e.Value.Where(x => x.Status == ValidationStatus.Information));
}
if (items == null)
continue;
foreach (ValidationIssue issue in items)
{
if (issue == null || issue.Resource == null || string.IsNullOrEmpty(issue.Message) || string.IsNullOrEmpty(issue.Resource.ResourceID))
continue;
ListViewItem lvi = new ListViewItem(issue.Resource.ResourceID);
switch (issue.Status)
{
case ValidationStatus.Information:
lvi.ImageIndex = 0;
break;
case ValidationStatus.Warning:
lvi.ImageIndex = 1;
break;
case ValidationStatus.Error:
lvi.ImageIndex = 2;
break;
default:
lvi.ImageIndex = -1;
break;
}
lvi.Tag = issue;
lvi.SubItems.Add(issue.Message);
lvi.SubItems.Add(issue.StatusCode.ToString());
lstIssues.Items.Add(lvi);
}
}
}
private ValidationResultsDialog()
{
InitializeComponent();
}
private void CancelBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void SaveReportBtn_Click(object sender, EventArgs e)
{
try
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFileDialog.FileName, false))
{
foreach (KeyValuePair p in m_issues)
{
if (p.Value.Length > 0)
{
sw.WriteLine(new string('*', 80)); //NOXLATE
sw.WriteLine(string.Format(Strings.ValidationProgressMessage, p.Key));
foreach (ValidationIssue i in p.Value)
sw.WriteLine(string.Format(Strings.ValidationResultFormat, i.Status, i.StatusCode, i.Message));
sw.WriteLine();
}
}
}
}
}
catch (Exception ex)
{
string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
MessageBox.Show(this, string.Format(Strings.ValidationFailedError, msg), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnResultFilterCheckedChanged(object sender, EventArgs e)
{
PopulateIssues();
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (_openAction != null && lstIssues.SelectedItems.Count == 1)
{
_openAction(((ValidationIssue)lstIssues.SelectedItems[0].Tag).Resource);
}
}
private void lstIssues_SelectedIndexChanged(object sender, EventArgs e)
{
btnOpen.Enabled = (lstIssues.SelectedItems.Count == 1);
btnOpen.Visible = (_openAction != null);
}
}
}