/**
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.addon.projects.ui;

import java.util.Set;

import org.jboss.forge.addon.facets.constraints.FacetConstraints;
import org.jboss.forge.addon.facets.constraints.FacetInspector;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFacet;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.Projects;
import org.jboss.forge.addon.ui.command.AbstractUICommand;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIContextProvider;
import org.jboss.forge.addon.ui.context.UISelection;

/**
 * Supports {@link Project} specific operations on {@link UICommand} sub-classes. Also enables the use of
 * {@link FacetConstraints} on implementations for which {@link AbstractProjectCommand#isProjectRequired()} returns
 * <code>true</code>.
 * 
 * @author <a href="ggastald@redhat.com">George Gastaldi</a>
 */
public abstract class AbstractProjectCommand extends AbstractUICommand
{
   @Override
   public boolean isEnabled(UIContext context)
   {
      return ((isProjectRequired() && containsProject(context) && constraintsSatisfied(context)) && super
               .isEnabled(context)) || (!isProjectRequired() && super.isEnabled(context));
   }

   private boolean constraintsSatisfied(UIContext context)
   {
      Set<Class<ProjectFacet>> facets = FacetInspector.getRequiredFacets(getMetadata(context).getType());
      return FacetInspector.isConstraintSatisfied(getSelectedProject(context), facets);
   }

   /**
    * Implementations should return <code>true</code> if a {@link Project} is required in order for this command to
    * execute. If <code>false</code>, this command will run without a {@link Project} selected in the current
    * {@link UIContext}. Returning <code>true</code> also enables the use of {@link FacetConstraints} on the
    * implementation {@link Class} declaration.
    */
   protected abstract boolean isProjectRequired();

   /**
    * Returns <code>true</code> if a {@link Project} exists in the current {@link UISelection}.
    */
   protected boolean containsProject(UIContext context)
   {
      return Projects.containsProject(getProjectFactory(), context);
   }

   /**
    * Returns the selected project. null if no project is found
    */
   protected Project getSelectedProject(UIContext context)
   {
      return Projects.getSelectedProject(getProjectFactory(), context);
   }

   protected Project getSelectedProject(UIContextProvider contextProvider)
   {
      return Projects.getSelectedProject(getProjectFactory(), contextProvider.getUIContext());
   }

   /**
    * Returns a reference to the {@link ProjectFactory}.
    */
   protected abstract ProjectFactory getProjectFactory();

}

@@ -2503,35 +2503,64 @@
 ted 
-p
+%7B@link P
 roject
+%7D
 . 
-null if no p
+%3Ccode%3Enull%3C/code%3E if no %7B@link P
 roject
+%7D
  is 
@@ -2564,16 +2564,17 @@
 is found
+.
 %0A    */%0A
@@ -2618,32 +2618,48 @@
 ct(UIContext
- context
+Provider contextProvider
 )%0A   %7B%0A     
@@ -2662,33 +2662,24 @@
      return 
-Projects.
 getSelectedP
@@ -2689,36 +2689,38 @@
 ect(
-getProjectFactory(), c
+contextProvider.getUIC
 ontext
+()
 );%0A 
@@ -2720,24 +2720,132 @@
 t());%0A   %7D%0A%0A
+   /**%0A    * Returns the selected %7B@link Project%7D. %3Ccode%3Enull%3C/code%3E if no %7B@link Project%7D is found.%0A    */%0A
    protected
@@ -2885,32 +2885,16 @@
 text
-Provider contextProvider
+ context
 )%0A  
@@ -2965,39 +2965,16 @@
  context
-Provider.getUIContext()
 );%0A   %7D%0A

Simplify methods