April 2007 Entries

Some mouseless computing tips/tools

 

I've been chatting with a friend a lot lately about how much more productive you can be by freeing yourself from the mouse.  So I figured I'd post just a few of the useful tips/tools that I use.

  • I saw JP mention this tip first, but I'll repeat it here...unplug your mouse!  There is no better way to really learn all those keyboard shortcuts that you've always known were there, but never used.  Just try it for a day or two (or longer if needed).  You'll be surprised at how much faster you'll start to become at those tasks you used to always do with the mouse.
  • Mouseless web browsing with the Hit-A-Hint Firefox extension.  You are using Firefox, right?!?  This lets you easily click links and set focus to text boxes by simply hitting the space bar and typing the number shown by the page element.  In rare cases, it won't be exactly obvious which numbers go to which element because of the page's html/css funkiness.  But fo the most part it works great!
  • Search with ease using the SearchKeys Firefox extension.  This takes the Hit-A-Hint technique to a whole new level for most search pages (i.e. Google, Yahoo, etc.).  It automatically puts numbers beside each search result, eliminating the need to hit the space bar first.  Just type the number shown and you're on your way.
  • For some reason, this one took a while for me to find.  A way to customize keyboard shortcuts for Firefox.  Well the answer I just found was the very useful KeyConfig Firefox extension.  I needed this simply because I wanted to map my "Close Current Tab" command to the same thing I map it to in Visual Studio, Ctrl-`.  Ctrl-F4 and Ctrl-W both just seem akward to me.
  • Ok, this is the last Firefox related tool I'll post for now (though I use a whole lot more).  If you use GMail, you simply have to install this GMail Macros Script giving you full power over GMail from the keyboard.  But first you'll need to download the Greasemonkey Firefox extension which is awesome, but that's for another post.
  • Of course apps like SlickRun & ReSharper are essential (IMHO), but those are covered numerous other places, so I won't bother going into detail on those in this post.
  • If you really wanna get crazy, check out Mouser!  This lets you move your mouse with your keyboard for those certain situations where keyboard shortcuts won't do what you need.

Once you start finding your own style with this stuff, you'll start to realize things like [WIN+Q g nhibernate] is just *supposed* to fire open a SlickRun box and search google for NHibernate.  And that [Alt-R-N-C] in Visual Studio (with R#) is how new classes *should* be created in a project.  And you just *know* that [Alt-F-S > Alt-Tab > Up > Enter] saves the current code file in Visual Studio, switches to your VS Command Prompt and runs "build test" to run your unit tests.  But anyways...  ;)

And if you're wondering about my current keyboard of choice, it's the Razer Tarantula.  I picked this up about 6 months ago and love it.  Like Dustin, I carry it everywhere with me.  As you can see I've haven't yet drank the ergonomic kool-aid.  :)

tarantula_LRG6

And of course I had to get the matching mouse, the Razer Copperhead (in Tempest Blue to match), for those times when I do use a mouse.  Like the rare opportunities I have for gaming, for which, this mouse rocks!  2000 DPI and 1000 HZ, oh yeah!

copperhead_LRG11

Happy Mouseless Computing!

ReSharper Template Library - Updated!

 

As you probably know, I'm a huge ReSharper fan.  I heavily use their templating features to greatly speed up my coding.  In a previous post, I offered up my R# template library for download.  Since then I've made a lot of changes and added quite a few new live templates and file templates, so I figured I'd offer my updated library for those who may find it useful.  Below you'll find a brief list of templates I've added and am actively using...

Since my last update, I've started using 3 new tools/frameworks for which I've created some live and file templates. 

  1. MbUnit - I've made the switch to MbUnit from NUnit because it just plain rocks.  There is no reason why you shouldn't use MbUnit over NUnit, IMHO.  It's literally a reference and namespace change to make the switch, since they both use the same attributes.  And you get all of the niceties of MbUnit, if for nothing else, [RowTest]!
  2. Rhino Mocks - I've also switched to Rhino Mocks exclusively for my mocking needs, now that Ayende has added full support for generic methods, finally!  Now I can ditch NMock2's string-based expectations in favor of strongly-typed expectations that are critical when refactoring.
  3. MonoRail - Not much more I need to say here, since that seems to be all I'm blogging about these days.  :P

So anyways, if you using one or all of these in your development, you may find the templates I've created for them useful. 

You can download the updated templates here:

Happy Coding!

Staying on top of updates in Castle Project's trunk

 

UPDATE: To reflect the fact that the validators don't really use ajax, but rather javascript generation.


If you're like me and have checked out the Castle Project's trunk source tree (for exploration and to help learn a lot about how it works), it's really cool to grab the latest updates once a week or so.  It really shows how hard these guys are working on adding and improving these frameworks. 

CastleSourceUpdateScreen

Of particular interest in the update I did tonight were a lot of validators added to the trunk.  Even though Hammett is 2nd guessing some of the Castle Validator implementation, I've found it to be a good fit for my current project.  I hand-rolled a basic attribute-based validation engine very similar to this about 6 months ago (not all that hard), but the nice thing about using Castle's is the nice integration points it has with MonoRail.  I haven't played much with turning on the Ajax integration  javascript generation for the validators, but I definitely plan to and it's nice to know it's there.

So what's cool, is that I've already noticed a couple validators that were added to the trunk that I've already custom built in my current project, so the more "out of the box" components like these that are added, means less I have to hand-roll.  And perhaps at some point, I may even submit a few of my own if folks would find them useful.

So if you're serious about learning the inner workings of the Castle Project or just to help learn about it in general, I'd encourage you to grab the latest source and give it a look.  They have a ton of great sample and test projects included in the source which have really helped me ramp up on everything.

If you're new to Subversion, you'll want to download TortoiseSVN to check out the source code from their Subversion repository.

Having fun creating reusable components in MonoRail

 

Scott Bellware recently blogged about wanting those of us in the "MonoRail Mafia" (sorry, couldn't resist) to show more examples and documentation of this thing called MonoRail.  So in an effort to try and inform folks about some of the cool things in MonoRail, here's another installment of creating and using view components in MonoRail. 

On my current ASP.NET project which I recently ported over to MonoRail, I'm really liking how easy it is to create reusable UI components using the MonoRail framework.  I've been going crazy porting over all of my previous custom "server controls" and "user controls" from the standard WebForms model to utilizing view components and shared sub views in MonoRail.  Here's a couple examples...

One of the standard controls I've been using is a styled LinkButton using a hyperlink instead of an input text tag, so I can better control the look and feel in CSS.  Creating this as a reusable MonoRail view component was very easy.

So I wanted to be able to do something like this in my views...

${Form.FormTag({ 'id':'someForm', 'action':'index' })}
    <?brail component LinkSubmitButton, { "linkText":"Submit", "formToSubmit":"someForm" } ?>
${Form.EndFormTag()}

 

Then the code for implementing this is pretty simple...

public class LinkSubmitButtonComponent : ViewComponent
{
    public const string FormSubmissionJavascriptFormat = "javascript: document.getElementById(\"{0}\").submit()"
    private string linkText;
    private string formToSubmit;

    public override void Initialize()
    {
        base.Initialize();

        linkText = ComponentParams["linkText"] as string ?? "Submit";
        formToSubmit = ComponentParams["formToSubmit"] as string ?? "form1";
    }

    public override void Render()
    {
        base.Render();

        string javaScriptToRender = string.Format(FormSubmissionJavascriptFormat, formToSubmit);
        string html = string.Format("<a href='{0}' class='linkButton'>{1}</a>", javaScriptToRender, linkText);

        RenderText(html);
    }
}

Pretty simple stuff.  It's amazing how less stressful it is to create this kinds of components when you don't have to worry about the noise of page lifecycles, control hierarchies and viewstate.

Taking it a step further, I noticed I have a lot of pages that need to use the same simple search form, which just so happened to use the link button component shown above.  So I could either create a shared sub view for it or create a view component.  Using a shared view is probably best in this situation, but I'm going to show an example of it as a view component because I want to show how you can create composite view components all in code.  (Something I haven't seen much about in the docs...)

Anywhere I needed a simple search form, I wanted to be able to just write something like this...

<?brail component SearchForm, { "actionToFire":"index.rails" } ?>

 

And this could be implemented as a view component like this...

public class SearchFormComponent : ViewComponent
{
    private const string searchFormName = "searchForm";
    private const string searchCriteriaKey = "searchCriteria";
    private string actionToFire;
    private string searchCriteria;

    public override void Initialize()
    {
        base.Initialize();

        actionToFire = ComponentParams["actionToFire"] as string ?? "index";
        searchCriteria = PropertyBag[searchCriteriaKey] as string ?? string.Empty;
    }

    public override void Render()
    {
        base.Render();

        ShowFormStart();
        ShowSearchCriteriaTextBox();
        ShowSearchButton();
        ShowFormEnd();
    }

    private void ShowFormStart()
    {
        RenderText(new HtmlHelper().Form(actionToFire, searchFormName, "post"));
    }

    private void ShowSearchCriteriaTextBox()
    {
        RenderText(
            string.Format("<input type='text' id='{0}' name='{0}' value='{1}' />", searchCriteriaKey, searchCriteria));
    }

    private void ShowSearchButton()
    {
        LinkSubmitButtonComponent linkButton = new LinkSubmitButtonComponent();
        linkButton.Init(RailsContext, Context);
        linkButton.Context.ComponentParameters.Add("linkText", "Search");
        linkButton.Context.ComponentParameters.Add("formToSubmit", searchFormName);
        linkButton.Initialize();
        linkButton.Render();

        string html = string.Empty;
        linkButton.Context.Writer.Write(html);
        RenderText(html);
    }

    private void ShowFormEnd()
    {
        RenderText(new HtmlHelper().EndForm());
    }
}

 

Notice I'm constructing the link button component in code, initializing it using the current IRailsEngineContext and IViewComponentContext, rendering it, grabbing the rendered html via its TextWriter and rendering it out to the view.  I'm sure there are better ways to achieve this that I haven't yet discovered, but this is one way you can do it.

You can access the source code here.  (Along with the rest of my emerging sample code library)

Creating a simple datepicker component for MonoRail

 

My current project, where I'm using a lot of MonoRail, I needed a simple datepicker control.  For my past webforms projects, I've always used the nice Calendar Popup control from eXcentrics world.  With the popularity of the prototype ajax library and scriptaculous, I wanted to try and leverage a datepicker control such as the one from Eulerian Technologies.  Turned out to be very easy to wrap this into a nice reusable view component for MonoRail (which conveniently already has prototype and scriptaculous support out of the box).

So first I set up a simple page for testing out the datepicker component... 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Layout</title>        
        <link href="${siteRoot}/content/css/datepicker.css" type="text/css" rel="stylesheet" />        
        
        ${Ajax.InstallScripts()}
        ${Scriptaculous.InstallScripts()}        
        <script type="text/javascript" src="${siteRoot}/Scripts/datepicker.js"></script>
    </head>    
    <body>
        <?brail component DatePickerComponent, {'textBoxId':'team.StartDate'} ?>    
    </body>
</html>

 

Notice, the only line you need in your view to show the datepicker is... 

<?brail component DatePickerComponent, {'textBoxId':'team.StartDate'} ?>     

 

Then I just created this simple view component for MonoRail to render the necessary input box and javascript...  

public class DatePickerComponent : ViewComponent
{
    private string textBoxId;
    private string inputBoxHtmlFormat = "<input type='text' id='{0}' name='{0}' />";

    private string datePickerJavascriptFormat =
        @"/*<[CDATA[*/
            var dpck    = new DatePicker({{
                relative    : '{0}',
                language    : 'en'
            }});
        /*]]>*/";

    public override void Initialize()
    {
        base.Initialize();
        textBoxId = ComponentParams["textBoxId"] as string ?? "dateBox";
    }

    public override void Render()
    {
        base.Render();
        RenderText(string.Format(inputBoxHtmlFormat, textBoxId));
        RenderText(AjaxHelper.ScriptBlock(string.Format(datePickerJavascriptFormat, textBoxId)));
    }
}

 

That's it.  I'm sure I'm going to be writing quite a few more of these nifty little components for MonoRail, especially considering how simple they are to create and how fast you can get them going in your MonoRail views.  Ayende has some great posts on MonoRail, including ones about creating custom components in MonoRail.

There might be better ways to do this, but for now, this is all I need.  And I was literally able to write it in a matter of minutes.  Now I need to see if I can find an ajaxy time picker control similar to eXcentrics world's Time Picker.  Anybody know of any?