/**
 * Copyright 2014 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;

import java.util.Map;

import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UISelection;

/**
 * Helper class for {@link Project} objects
 * 
 * @author <a href="ggastald@redhat.com">George Gastaldi</a>
 */
public final class Projects
{
   /**
    * Stores the project inside the {@link UIContext}, to avoid unnecessary lookups
    */
   private static final String SCOPED_PROJECT_KEY = "org.jboss.forge.projects.CURRENT_PROJECT";

   /**
    * @return the project if {@link UIContext#getInitialSelection()} returns a path containing a project, null otherwise
    */
   public static Project getSelectedProject(ProjectFactory projectFactory, UIContext context)
   {
      Map<Object, Object> attributeMap = context.getAttributeMap();
      Project project = (Project) attributeMap.get(SCOPED_PROJECT_KEY);
      if (project == null)
      {
         UISelection<FileResource<?>> initialSelection = context.getInitialSelection();
         if (!initialSelection.isEmpty())
         {
            project = projectFactory.findProject(initialSelection.get());
         }
         if (project != null)
         {
            attributeMap.put(SCOPED_PROJECT_KEY, project);
         }
      }
      return project;
   }

   /**
    * @return true if {@link UIContext#getInitialSelection()} returns a path containing a project, false otherwise
    */
   public static boolean containsProject(ProjectFactory projectFactory, UIContext context)
   {
      Project project = (Project) context.getAttributeMap().get(SCOPED_PROJECT_KEY);
      if (project == null)
      {
         UISelection<FileResource<?>> initialSelection = context.getInitialSelection();
         if (!initialSelection.isEmpty())
         {
            return projectFactory.containsProject(initialSelection.get());
         }
         return false;
      }
      return true;
   }
}

@@ -221,31 +221,8 @@
 s;%0A%0A
-import java.util.Map;%0A%0A
 impo
@@ -525,203 +525,8 @@
 s%0A%7B%0A
-   /**%0A    * Stores the project inside the %7B@link UIContext%7D, to avoid unnecessary lookups%0A    */%0A   private static final String SCOPED_PROJECT_KEY = %22org.jboss.forge.projects.CURRENT_PROJECT%22;%0A%0A
    /
@@ -765,180 +765,32 @@
     
-Map%3CObject, Object%3E attributeMap = context.getAttributeMap();%0A      Project project = (Project) attributeMap.get(SCOPED_PROJECT_KEY);%0A      if (project == null)%0A      %7B%0A   
+Project project = null;%0A
     
@@ -868,35 +868,32 @@
 ection();%0A      
-   
 if (!initialSele
@@ -911,32 +911,26 @@
 ty())%0A      
- 
-  %7B%0A   
+%7B%0A
          pro
@@ -992,130 +992,8 @@
 ));%0A
-         %7D%0A         if (project != null)%0A         %7B%0A            attributeMap.put(SCOPED_PROJECT_KEY, project);%0A         %7D%0A
     
@@ -1253,131 +1253,8 @@
   %7B%0A
-      Project project = (Project) context.getAttributeMap().get(SCOPED_PROJECT_KEY);%0A      if (project == null)%0A      %7B%0A   
     
@@ -1340,19 +1340,16 @@
 ;%0A      
-   
 if (!ini
@@ -1379,24 +1379,18 @@
 )%0A      
-   %7B%0A   
+%7B%0A
         
@@ -1463,22 +1463,16 @@
     
-   %7D%0A  
+%7D%0A
       
- 
 retu
@@ -1485,34 +1485,8 @@
 se;%0A
-      %7D%0A      return true;
 %0A   

Revert "Minor performance optimization"