Eclipse Resource Filtering

Today i found a solution to the single most irritating thing with Eclipse. To not be able to filter away resources from the Open Resource dialog. This was of course possible but I've never even thought about it until a coworker passed me this link. I quote:

The trick is to open the "Navigator" view, right click on the folder to be ignored, and check the "Derived" property. This option informs Eclipse that this folder consists of generated resources that should not be directly edited. Once this is done, the "Open Resource..." view will only show matches that would be relevant to the developer.

Getting a FacesContext from a GWT service

I'm working on a project where we needed to integrate JSF and GWT. To be more specific, we needed to have access to JSF beans in the GWT services.

Because I missed the methods getThreadLocalRequest() and getThreadLocalResponse() in RemoteServiceServlet, I had to search quite a bit before i found an answer to this problem. So to save others some time i post the class i came up with.

Most of the code is taken from an article at ocpsoft.com which also points out how important it is to release the context when you're done with it. The big difference are the lines,
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "gwtViewID");
facesContext.setViewRoot(view);
, which sets the view root. If no view root is set you can't use, for instance MessageBundles.

The getBean methods are only convenience methods for retrieving beans by name.

/**
 * A substitute for RemoteServiceServlet that has access to the FacesContext.
  */
@SuppressWarnings("serial")
public class FacesContextAwareRemoteServiceServlet extends RemoteServiceServlet {
 protected  T getBean(final Class type) {
  String name = type.getSimpleName();
  name = name.substring(0, 1).toLowerCase().concat(name.substring(1));
  return getBean(name, type);
 }

 @SuppressWarnings("unchecked")
 protected  T getBean(final String name, final Class type) {
  ELContext el = getFacesContext().getELContext();
  return (T) el.getELResolver().getValue(el, null, name);
 }

 /**
  * Returns the current FacesContext.
  */
 protected FacesContext getFacesContext() {
  return getFacesContext(getThreadLocalRequest(), getThreadLocalResponse());
 }

 /**
  * Should be called when the FacesContext aren't needed any more.
  */
 protected void releaseFacesContext() {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  if (facesContext != null) {
   facesContext.release();
   InnerFacesContext.setFacesContextAsCurrentInstance(null);
  }
 }

 private FacesContext getFacesContext(ServletRequest request, ServletResponse response) {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  if (facesContext != null) {
   return facesContext;
  }

  FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
    .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
  LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
    .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
  Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

  ServletContext servletContext = ((HttpServletRequest) request).getSession().getServletContext();
  facesContext = contextFactory.getFacesContext(servletContext, request, response, lifecycle);

  InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
  
  UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "gwtViewID");
  facesContext.setViewRoot(view);

  return facesContext;
 }

 private abstract static class InnerFacesContext extends FacesContext {
  protected static void setFacesContextAsCurrentInstance(final FacesContext facesContext) {
   FacesContext.setCurrentInstance(facesContext);
  }
 }
}
Get from github

How to create a jQuery plugin

I tend to write everything i do in JavaScript as a jQuery plugin. Even if it's mostly useless, i think it's a convenient way to encapsulate the code. In this post I'll show how to make a simple plugin that replaces the background color of an element when the mouse hovers over it. In real life you would probably want to do this with CSS pseudo-classes.

Step 1 - Get ready

All plugins i write always starts with the same template:
(function($){
   $.fn.backgroundHover = function(options) {
      return this.each(function () {
         // Code goes here
      });
   };
})(jQuery);
Line 1 and 7 are only there to protect from $-function overloading by using variable scoping. On line 2 the plugin function is defined in the prototype of jQuery. fn is just an alias for prototype. Since a plugin can be applied to many DOM nodes in one call, we must loop through every element on line 3.

Step 2 - Get Set

Now we just need to write the actual code for the plugin. First we gonna add a couple of lines in the beginning of the plugin function to provide default values for the options.
$.fn.backgroundHover = function(options) {  
      options = $.extend({
         background: 'red',
      }, options); 
Then we add some code inside the each-function:
var self = $(this);
   self.mouseover(function() {
      self.oldBG = self.css('backgroundColor');
      self.css('backgroundColor',options.background);
   });

   self.mouseout(function() {
      self.css('backgroundColor',self.oldBG);
   });   
Line 1 is just a convenience variable that I usually define to get rid of unnecessary $-calls. Line 2-5 binds a function to the mouseover event. When the mouse is above the element the old background color is stored for later use and the new one is set. Line 7-9 restores the original background color of the element when a mouseout event is triggered.

Step 3 - Go!

That's it, the plugin is finished! And all that's left is to use it:
jQuery("#special").backgroundHover( { background: '#306090' } );
The complete source is as follows:
(function($){
   $.fn.backgroundHover = function(options) {
      options = $.extend({
         background: 'red',
      }, options); 

      return this.each(function () {
         var self = $(this);
         self.mouseover(function() {
            self.oldBG = self.css('backgroundColor');
            self.css('backgroundColor',options.background);
         });
   
         self.mouseout(function() {
            self.css('backgroundColor',self.oldBG);
         });   
      });
   };
})(jQuery);

Epilogue

If you found this post useful or have suggestions on how i can improve it, please leave a comment. Negative comments without any constructive criticism will be deleted without hesitation. My blog, my rules :)