//******************************************************************************************************
// MenuCommand.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 03/25/2011 - Mehulbhai P Thakkar
// Generated original version of source code.
// 05/13/2011 - Mehulbhai P Thakkar
// Modified Execute() method to set main window's title property.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Linq;
using System.Reflection;
using System.Windows.Input;
using GSF.IO;
using GSF.Security;
namespace GSF.TimeSeries.UI.Commands
{
///
/// Exposes object to be used to handle menu item click event.
///
public class MenuCommand : ICommand
{
#region [ Members ]
// Fields
private string m_roles;
private string m_userControlAssembly;
private string m_userControlPath;
private string m_description;
//Events
///
/// Raises when value of changes.
///
public event EventHandler CanExecuteChanged;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class.
///
public MenuCommand()
{
CommonFunctions.CurrentPrincipalRefreshed += (sender, args) => OnCanExecuteChanged();
}
#endregion
#region [ Properties ]
///
/// Gets or sets comma seperated values of roles for which menuitem is visible.
///
public string Roles
{
get
{
return m_roles;
}
set
{
m_roles = value;
}
}
///
/// Gets or sets name of the assembly where user control belongs to.
///
public string UserControlAssembly
{
get
{
return m_userControlAssembly;
}
set
{
m_userControlAssembly = value;
}
}
///
/// Gets or sets name of the user control to be loaded.
///
public string UserControlPath
{
get
{
return m_userControlPath;
}
set
{
m_userControlPath = value;
}
}
///
/// Gets or sets the description of associated .
///
public string Description
{
get
{
return m_description;
}
set
{
m_description = value;
}
}
#endregion
#region [ Methods ]
///
/// Evaluates if menu item should be visible to current user with access to .
///
///
/// Data used by the . If the does not require
/// data to be passed, this object can be set to null.
///
/// true if this can be executed; otherwise, false.
public bool CanExecute(object parameter)
{
SecurityPrincipal currentPrincipal = CommonFunctions.CurrentPrincipal;
return currentPrincipal.Identity.IsAuthenticated &&
(string.IsNullOrEmpty(Roles) || Roles == "*" || currentPrincipal.IsInRole(Roles));
}
///
/// Handles action.
/// Loads user control as defined in the property from assembly name set in the
/// property.
///
///
/// Data used by the . If the does not require
/// data to be passed, this object can be set to null.
///
public void Execute(object parameter)
{
try
{
Assembly assembly = Assembly.LoadFrom(FilePath.GetAbsolutePath(m_userControlAssembly));
CommonFunctions.LoadUserControl(m_description, assembly.GetType(m_userControlPath));
}
catch (Exception ex)
{
throw new InvalidOperationException(string.Format("Failed to create user control {0}: {1}", m_userControlPath, ex.Message), ex);
}
}
///
/// Raises the event.
///
protected virtual void OnCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
#endregion
}
}