using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using WindowsApplication = System.Windows.Forms.Application;
using Core.Common.Base.Data;
using Core.Common.Base.Plugin;
using Core.Common.Base.Storage;
using Core.Common.Controls.Views;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.Forms.MainWindow;
using Core.Common.Gui.Forms.MessageWindow;
using Core.Common.Gui.Forms.PropertyGridView;
using Core.Common.Gui.Forms.SplashScreen;
using Core.Common.Gui.Forms.ViewManager;
using Core.Common.Gui.Properties;
using Core.Common.Utils;
using Core.Common.Utils.Events;
using Core.Common.Utils.Extensions;
using Core.Common.Utils.Reflection;
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using TreeNode = Core.Common.Controls.TreeView.TreeNode;
namespace Core.Common.Gui
{
///
/// Gui class provides graphical user functionality for a given IApplication.
///
public class RingtoetsGui : IGui, IContextMenuBuilderProvider
{
public event EventHandler SelectionChanged; // TODO: make it weak
public event EventHandler ActiveViewChanged;
public event Action ProjectOpened;
public event Action ProjectClosing;
private static readonly ILog log = LogManager.GetLogger(typeof(RingtoetsGui));
private static RingtoetsGui instance;
private static string instanceCreationStackTrace;
private readonly IList commands = new List();
private readonly ApplicationCore applicationCore;
private MainWindow mainWindow;
private object selection;
private ViewList documentViews;
private ViewList toolWindowViews;
private AvalonDockDockingManager toolWindowViewsDockingManager;
private SplashScreen splashScreen;
private bool settingSelection;
private bool runFinished;
private bool isExiting;
private Project project;
private bool userSettingsDirty;
private ApplicationSettingsBase userSettings;
private readonly ApplicationFeatureCommandHandler appFeatureApplicationCommands;
private StorageCommandHandler storageCommandHandler;
private readonly ViewCommandHandler viewCommandHandler;
private readonly ProjectCommandsHandler projectCommandsHandler;
private readonly ExportImportCommandHandler exportImportCommandHandler;
public RingtoetsGui(ApplicationCore applicationCore = null, GuiCoreSettings fixedSettings = null)
{
// error detection code, make sure we use only a single instance of RingtoetsGui at a time
if (instance != null)
{
instance = null; // reset to that the consequent creations won't fail.
throw new InvalidOperationException(Resources.RingtoetsGui_Only_a_single_instance_of_Ringtoets_is_allowed_at_the_same_time_per_process_Make_sure_that_the_previous_instance_was_disposed_correctly_stack_trace + instanceCreationStackTrace);
}
ApplicationCore = applicationCore ?? new ApplicationCore();
FixedSettings = fixedSettings ?? new GuiCoreSettings();
instance = this;
instanceCreationStackTrace = new StackTrace().ToString();
ViewPropertyEditor.Gui = this;
Plugins = new List();
UserSettings = Settings.Default;
appFeatureApplicationCommands = new ApplicationFeatureCommandHandler(this);
viewCommandHandler = new ViewCommandHandler(this);
storageCommandHandler = new StorageCommandHandler(viewCommandHandler, this);
exportImportCommandHandler = new ExportImportCommandHandler(this);
projectCommandsHandler = new ProjectCommandsHandler(this);
WindowsApplication.EnableVisualStyles();
ProjectOpened += ApplicationProjectOpened;
}
public string ProjectFilePath { get; set; }
public ApplicationCore ApplicationCore { get; private set; }
public Project Project
{
get
{
return project;
}
set
{
if (project != null)
{
if (ProjectClosing != null)
{
ProjectClosing(project);
}
}
project = value;
if (project != null)
{
if (ProjectOpened != null)
{
ProjectOpened(project);
}
}
}
}
public object Selection
{
get
{
return selection;
}
set
{
if (selection == value || settingSelection)
{
return;
}
if (null != DocumentViews)
{
DocumentViews.IgnoreActivation = true;
}
selection = value;
settingSelection = true;
try
{
if (SelectionChanged != null)
{
SelectionChanged(selection, new SelectedItemChangedEventArgs(value));
}
}
finally
{
settingSelection = false;
}
if (!isExiting && mainWindow != null && !mainWindow.IsWindowDisposed)
{
mainWindow.ValidateItems();
}
if (null != DocumentViews)
{
DocumentViews.IgnoreActivation = false;
}
}
}
public IList Commands
{
get
{
return commands;
}
}
public IApplicationFeatureCommands ApplicationCommands { get { return appFeatureApplicationCommands; } }
public IStorageCommands StorageCommands
{
get
{
return storageCommandHandler;
}
}
public IProjectCommands ProjectCommands
{
get
{
return projectCommandsHandler;
}
}
public IViewCommands ViewCommands
{
get
{
return viewCommandHandler;
}
}
public IViewList DocumentViews
{
get
{
return documentViews;
}
}
public IViewResolver DocumentViewsResolver { get; private set; }
public IPropertyResolver PropertyResolver { get; private set; }
public IViewList ToolWindowViews
{
get
{
return toolWindowViews;
}
}
public IMainWindow MainWindow
{
get
{
return mainWindow;
}
set
{
mainWindow = (MainWindow)value;
mainWindow.Gui = this;
}
}
public IList Plugins { get; private set; }
public GuiCoreSettings FixedSettings { get; private set; }
public ApplicationSettingsBase UserSettings
{
get
{
return userSettings;
}
private set
{
if (userSettings != null)
{
userSettings.PropertyChanged -= UserSettingsPropertyChanged;
}
userSettings = value;
if (userSettings != null)
{
userSettings.PropertyChanged += UserSettingsPropertyChanged;
}
userSettingsDirty = false;
}
}
public bool IsViewRemoveOnItemDeleteSuspended { get; set; }
public IContextMenuBuilderProvider ContextMenuProvider
{
get
{
return this;
}
}
public IView ActiveView
{
get
{
return DocumentViews.ActiveView;
}
}
public IContextMenuBuilder Get(TreeNode treeNode)
{
return new ContextMenuBuilder(appFeatureApplicationCommands, exportImportCommandHandler, ViewCommands, treeNode);
}
public IEnumerable GetAllDataWithViewDefinitionsRecursively(object rootDataObject)
{
var resultSet = new HashSet