How-To : Creating your own Clock Type

So you like the built in clock types, but you wan't to create your own? No problem, from the very beginning I designed this portlet to be easily extended with new clock types.

Minimal

  1. Create a new class that implements IClock interface
  2. implement public Control GetViewControls(ClockPortlet portlet) method: this method is called when the clock is about to display, so you have to return a control that represents your clock

And that's all :) The portlet will automatically detect your class if you place your assembly in the bin folder

Custom Settings

probably you wish to add custom settings to your clock, to do so you have to:
  1. add [Serializable] attribute
  2. implement the ISerializable interface
  3. implement his methods, and add a constructor for deserialization : here you can see how must be this done
  4. implement Dictionary<string, Control> GetSettingsControls() this methos is called when the users click Settings button. You must return a Dictionary which values will be the displayed as settings Controls, and the keys will be displayed next to the control as text.
  5. implement SaveSettings() method. This function is called when the user clicks on the Save button on the Settings page. Save whatever you want :)

Thats all :)

Let's see an example!

We would like to create a clock which displays time hexadecimal.
  • Open Visual Studio and create a new project > ASP.NET Web Application, name it HexaClock
  • delete App_data, Default.aspx, Web.config files, we won't need them.
  • Add a new class, name it HexaClock
  • Add ClockPortlet, SenseNet.Portal, SenseNet.ContentRepository, SenseNet.Storage assembly to your refrences
  • inherit HexaClock class from IClock interface (using SenseNet.Portal.Portlets.ClockPortlet)
  • create a default, parameterless constructor
  • start to implement the IClock interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenseNet.Portal.Portlets.ClockPortlet;
using System.Web.UI;

namespace HexaClock
{
    public class HexaClock : IClock
    {
        public HexaClock() { }


        #region IClock Members

        public Control GetViewControls(ClockPortlet portlet)
        {
            //Your Code here
        }

        public Dictionary<string, Control> GetSettingsControls()
        {
            return new Dictionary<string, Control>();
        }

        public void SaveSettings()
        {
        }

        #endregion
    }
}
  • We don't have any settings, so we just return an empty dictionary.
  • Our clock will be displayed on a Label, so create one (need: using System.Web.UI.WebControls)
  • from portlet.CurrentLocalTime property we can get the local time, calculated from DateTime.UtcNow and from the user given Time Zone
Label lblClock = new Label();
DateTime currTime = portlet.CurrentLocalTime;
lblClock.Text = String.Format("{0}:{1}:{2}", currTime.Hour.ToString("X"), currTime.Minute.ToString("X"), currTime.Second.ToString("X"));
  • We are near to finish, we need to add the user given Css Class to our label, which is stored in portlet.CustomCssClass :
lblClock.CssClass = portlet.CustomCssClass;
  • We are done, compile it, and place the assemby file in the your sensenet bin folder.
  • Now if you add a new clock portlet to your site, you can select HexaClock in your Edit menü :) Great, isn't it? :)

The full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenseNet.Portal.Portlets.ClockPortlet;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HexaClock
{
    public class HexaClock : IClock

    {
        public HexaClock() { }


        #region IClock Members

        public Control GetViewControls(ClockPortlet portlet)
        {
            Label lblClock = new Label();
            DateTime currTime = portlet.CurrentLocalTime;
            lblClock.Text = String.Format("{0}:{1}:{2}", currTime.Hour.ToString("X"), currTime.Minute.ToString("X"), currTime.Second.ToString("X"));
            lblClock.CssClass = portlet.CustomCssClass;
            return lblClock;
        }

        public Dictionary<string, Control> GetSettingsControls()
        {
            return new Dictionary<string, Control>();
        }

        public void SaveSettings()
        {
        }

        #endregion
    }
}

Last edited Oct 31, 2009 at 8:37 PM by szelpe, version 11

Comments

No comments yet.