/*
 * Copyright 2007 Steve Chaloner
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package net.stevechaloner.intellijad.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import net.stevechaloner.intellijad.IntelliJadConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Closes all files marked as being decompiled by IntelliJad.
 */
public class CloseAllDecompiledFilesAction extends AnAction
{
    /** {@inheritDoc} */
    public void update(AnActionEvent e) {
        Project project = DataKeys.PROJECT.getData(e.getDataContext());
        List<VirtualFile> files = new ArrayList<VirtualFile>(Arrays.asList(FileEditorManager.getInstance(project).getOpenFiles()));
        boolean hasDecompiledFiles = false;

        for (int i = 0; !hasDecompiledFiles && i < files.size(); i++) {
            if (files.get(i).getUserData(IntelliJadConstants.DECOMPILED_BY_INTELLIJAD) != null)
            {
                hasDecompiledFiles = true;
            }
        }
        e.getPresentation().setEnabled(hasDecompiledFiles);
    }

    /** {@inheritDoc} */
    public void actionPerformed(AnActionEvent e)
    {
        Project project = DataKeys.PROJECT.getData(e.getDataContext());
        List<VirtualFile> files = new ArrayList<VirtualFile>(Arrays.asList(FileEditorManager.getInstance(project).getOpenFiles()));

        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        for (VirtualFile file : files)
        {
            if (file.getUserData(IntelliJadConstants.DECOMPILED_BY_INTELLIJAD) != null)
            {
                fileEditorManager.closeFile(file);
            }
        }
    }
}

@@ -1336,32 +1336,91 @@
 DataContext());%0A
+        boolean enabled = false;%0A%09if (project != null)%0A    
         List%3CVir
@@ -1539,53 +1539,13 @@
 ));%0A
+%0A
     
-    boolean hasDecompiledFiles = false;%0A%0A
     
@@ -1616,32 +1616,36 @@
 ) %7B%0A            
+   
+ 
 if (files.get(i)
@@ -1716,32 +1716,36 @@
 ll)%0A            
+    
 %7B%0A              
@@ -1750,34 +1750,45 @@
     
-hasDecompiledFiles = true;
+    enabled = true;%0A                %7D
 %0A   
@@ -1847,34 +1847,23 @@
 Enabled(
-hasDecompiledFiles
+enabled
 );%0A    %7D

check if project == null before using it