/*
 * Copyright (c) 2005 SchemeWay.com
 * 
 * This is free software. For terms and warranty disclaimer, see ./COPYING
 */
package org.schemeway.plugins.schemescript.action;

import gnu.mapping.*;
import kawa.standard.*;

import org.eclipse.core.runtime.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.ui.*;
import org.schemeway.plugins.schemescript.*;

/**
 * @author SchemeWay.com
 */
public final class SchemeProcedureAction extends Action implements IWorkbenchWindowActionDelegate, IExecutableExtension {

    private Procedure mProcedure = null;

    public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
            throws CoreException {
        
        if (data instanceof String) {
            String procedureName = (String)data;
            try {
                Object object = Scheme.getInstance().eval(procedureName);
                if (object instanceof Procedure) {
                    mProcedure = (Procedure) object;
                }
            }
            catch (Throwable e) {
                SchemeScriptPlugin.logException("Error while initializing Scheme action", e);
            }
        }
    }

    public void dispose() {
    }

    public void init(IWorkbenchWindow window) {
    }

    public void run(IAction action) {
        if (mProcedure != null) {
            try {
                mProcedure.apply0();
            }
            catch (Throwable e) {
                SchemeScriptPlugin.logException("Error while evaluating Scheme action", e);
            }
        }
    }

    public void selectionChanged(IAction action, ISelection selection) {
    }

}

@@ -570,16 +570,22 @@
 cedure m
+Cached
 Procedur
@@ -593,16 +593,54 @@
  = null;
+%0A    private String    mProcedureName;
 %0A%0A    pu
@@ -829,24 +829,18 @@
         
-String p
+mP
 rocedure
@@ -864,37 +864,80 @@
 ta;%0A            
-try %7B
+mCachedProcedure = getProcedure(mProcedureName);
 %0A               
@@ -933,59 +933,185 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-Object object = Scheme.getInstance().eval(p
+public void dispose() %7B%0A    %7D%0A%0A    public void init(IWorkbenchWindow window) %7B%0A    %7D%0A%0A    public void run(IAction action) %7B%0A        Procedure proc = getProcedure(mP
 roce
@@ -1121,32 +1121,33 @@
 eName);%0A        
+%0A
         if (obje
@@ -1146,26 +1146,39 @@
 if (
-object instanceof 
+proc != null && proc != mCached
 Proc
@@ -1183,22 +1183,17 @@
 ocedure)
- %7B%0A   
+%0A
         
@@ -1188,38 +1188,39 @@
 re)%0A            
-     m
+mCached
 Procedure = (Pro
@@ -1219,45 +1219,123 @@
 e = 
-(Procedure) object;%0A                %7D
+proc;%0A        %0A        if (mCachedProcedure != null) %7B%0A            try %7B%0A                mCachedProcedure.apply0();
 %0A   
@@ -1444,17 +1444,15 @@
 ile 
-initializ
+evaluat
 ing 
@@ -1514,32 +1514,77 @@
 public void 
-dispose(
+selectionChanged(IAction action, ISelection selection
 ) %7B%0A    %7D%0A%0A 
@@ -1585,110 +1585,151 @@
   %7D%0A
+    
 %0A    p
-ublic void init(IWorkbenchWindow window) %7B%0A    %7D%0A%0A    public void run(IAction action) %7B%0A
+rivate Procedure getProcedure(String name)%0A    %7B%0A        try %7B%0A            Object object = Scheme.getInstance().eval(name);%0A 
         
 if (
@@ -1728,33 +1728,86 @@
     
+   
 if (
-mProcedure != null) %7B
+object instanceof Procedure)%0A                return (Procedure) object;
 %0A   
@@ -1807,37 +1807,36 @@
 ct;%0A            
-try %7B
+else
 %0A               
@@ -1840,33 +1840,21 @@
     
-mProcedure.apply0();%0A    
+return null;%0A
     
@@ -1859,36 +1859,32 @@
       %7D%0A        
-    
 catch (Throwable
@@ -1881,36 +1881,32 @@
 (Throwable e) %7B%0A
-    
             Sche
@@ -1938,39 +1938,37 @@
 on(%22Error while 
-evaluat
+fetch
 ing Scheme actio
@@ -1991,99 +1991,30 @@
     
-%7D%0A        %7D%0A    %7D%0A%0A    public void selectionChanged(IAction action, ISelection selection) %7B
+return null;%0A        %7D
 %0A   

Make sure that if a procedure action is redefined, it will be properly invoked.