Click here to Skip to main content
15,896,606 members
Articles / General Programming / Threads

Plugged.NET - An event based plug-in library for enterprise application integration and extensibility

Rate me:
Please Sign up or sign in to vote.
4.89/5 (19 votes)
25 Jul 2013CPOL6 min read 34.1K   1.4K   92  
An event based plug-in library for enterprise application integration, extensibility and cross business application management.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel.Composition;
using Common.Plugin.Platform.Common;
using Common.Plugin.Platform.Events;
using System.Timers;

namespace TimeEventPlugin
{
    [Export(typeof(IApplicationPlugin))]
    public class TwoPlugin : IApplicationPlugin
    {
        private TabText textControl;
        ExtensionEvent eventPump = new ExtensionEvent();

        Timer timer;

        public TwoPlugin()
        {
            textControl = new TabText(eventPump, Name);
            timer = new Timer(10000) { Enabled = true, AutoReset = true };
            timer.Elapsed += (o, e) => { eventPump.RaiseStartedEvent("TimeEventPlugin - time elapsed and reset at " + DateTime.Now.ToShortTimeString()); };
            timer.Start();
        }

        IExtensionEvents IApplicationPlugin.ExtensionEvents
        {
            get { return eventPump; }
        }

        public string Name
        {
            get { return "TimeEventPlugin"; }
        }

        public string DisplayName
        {
            get
            {
                return "TimeEvent";
            }
        }

        public string Version
        {
            get
            {
                return "0.0.0.1";
            }
        }

        public UserControl UserSettingsControl
        {
            get
            {
                // can return a databound settings for host to show all settings in the same place
                // plugin can also show its settings by itself
                return null;
            }
        }

        public void Initialize(UserContext userContext, string settings)
        {
            // manage initialization of the plugin with current user context; if required
        }

        public void RegisterEvents(IApplicationEvents eventPump)
        {
            eventPump.OnFileSystemEvent += eventPump_OnFileSystemEvent;
            eventPump.OnActivateEvent += eventPump_OnActivateEvent;
        }

        public void UnRegisterEvents(IApplicationEvents eventPump)
        {
            eventPump.OnFileSystemEvent -= eventPump_OnFileSystemEvent;
            eventPump.OnActivateEvent -= eventPump_OnActivateEvent;
        }

        public bool IsVisible
        {
            get { return textControl.IsVisible; }
        }

        private void eventPump_OnFileSystemEvent(object sender, EventArgs e)
        {
            var arg = e as FileSystemInfoEventArg;
            if (arg != null)
            {
                textControl.Dispatcher.Invoke(new Action(() => textControl.GetApplicationMessage(arg.ItemPath)));
            }
        }

        private void eventPump_OnActivateEvent(object sender, EventArgs e)
        {
            var arg = e as ActivateInfoEventArg;
            if (arg != null)
            {
                if (!textControl.IsLoaded && arg.VisibleChangedHandler != null)
                    textControl.IsVisibleChanged += arg.VisibleChangedHandler;

                textControl.Dispatcher.Invoke(new Action(() =>
                {
                    if (!double.IsNaN(arg.Left) && !double.IsNaN(arg.Top))
                    {
                        textControl.Left = arg.Left;
                        textControl.Top = arg.Top;
                    }

                    textControl.Topmost = arg.TopMost;
                    textControl.Show();
                    textControl.Activate();
                }));
            }
        }

        #region IStatusQuery Members

        public string StatusMessage
        {
            get { return "Idle"; }
        }

        private Status status;

        public Status Status
        {
            get
            {
                if (status == Status.NotResponding || status == Status.Suspended || status == Status.Waiting) return status;
                else return textControl.IsActive ? Status.Active : Status.Idle;
            }
            set
            {
                status = value;
            }
        }

        #endregion
    }

    public class ExtensionEvent : IExtensionEvents
    {
        public event EventHandler OnStarted;
        public event EventHandler OnCompleted;
        public event EventHandler OnError;

        public void RaiseCompletedEvent(string message, string openPath = null)
        {
            if (OnCompleted != null)
            {
                OnCompleted(this, new PluginNotifyIconMessageArgs() { Message = message, NotificationType = PluginNotifyMessageType.Message, InvokeOnClickCommand = openPath });
            }
        }

        public void RaiseStartedEvent(string message)
        {
            if (OnStarted != null)
            {
                OnStarted(this, new PluginNotifyIconMessageArgs() { Message = message, NotificationType = PluginNotifyMessageType.Message });
            }
        }

        public void RaiseErrorEvent(string message)
        {
            if (OnError != null)
            {
                OnError(this, new PluginNotifyIconMessageArgs() { Severity = PluginMessageSeverity.Error, Message = message, NotificationType = PluginNotifyMessageType.Message, ForceMessage = true });
            }
        }

        public AuthenticationDelegate Authenticate { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
is a poor software developer and thinker. Presently working on a theory of "complementary perception". It's a work in progress.

Comments and Discussions