package com.opensymphony.workflow.designer.actions;

import java.awt.event.ActionEvent;
import java.awt.*;
import java.io.*;
import java.net.URL;
import javax.swing.*;

import com.opensymphony.workflow.designer.event.WorkspaceListener;
import com.opensymphony.workflow.designer.event.WorkspaceEvent;
import com.opensymphony.workflow.designer.WorkflowDesigner;
import com.opensymphony.workflow.designer.ResourceManager;
import com.opensymphony.workflow.designer.Utils;
import com.opensymphony.workflow.designer.swing.status.StatusDisplay;
import com.opensymphony.workflow.designer.dialogs.ImportWorkflowDialog;
import com.opensymphony.workflow.loader.Workspace;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Hani Suleiman (hani@formicary.net)
 * Date: May 21, 2003
 * Time: 12:55:51 AM
 */
public class ImportWorkflow extends AbstractAction implements WorkspaceListener
{
  private static final Log log = LogFactory.getLog(ImportWorkflow.class);

  private Workspace currentWorkspace;

  public ImportWorkflow()
  {
    setEnabled(false);
  }

  public void actionPerformed(ActionEvent e)
  {
    if(currentWorkspace.getLocation() == null)
    {
      JOptionPane.showMessageDialog((Component)e.getSource(), ResourceManager.getString("error.save.unsavedspace"), ResourceManager.getString("title.warning"), JOptionPane.WARNING_MESSAGE);
      return;
    }
    ImportWorkflowDialog dialog = new ImportWorkflowDialog(WorkflowDesigner.INSTANCE, (String)getValue(NAME), true);
    dialog.pack();
    Utils.centerComponent(WorkflowDesigner.INSTANCE, dialog);
    dialog.show();
    URL inputFile = dialog.getImportURL();
    if(inputFile!=null)
    {
      String f = inputFile.getFile();
      if(!"file".equals(inputFile.getProtocol()) && f.indexOf('?')>-1) f = f.substring(0, f.indexOf('?'));
      String fileName = f.substring(f.lastIndexOf('/')+1, f.length());
      try
      {
        File ouputFile = new File(currentWorkspace.getLocation().getParentFile(), fileName);
        //don't allow importing of files within the workspace!
        if(!"file".equals(inputFile.getProtocol()) ||
          new File(inputFile.getFile()).getCanonicalPath().indexOf(currentWorkspace.getLocation().getParentFile().getCanonicalPath())==-1)
        {
          StatusDisplay status = (StatusDisplay)WorkflowDesigner.INSTANCE.statusBar.getItemByName("Status");
          status.setIndeterminate(true);
          status.setProgressStatus("Loading...");
          FileOutputStream out = new FileOutputStream(ouputFile);
          InputStream in = inputFile.openStream();
          byte[] buff = new byte[4096];
          int nch;
          while((nch = in.read(buff, 0, buff.length)) != -1)
          {
            out.write(buff, 0, nch);
          }
          out.flush();
          out.close();
          status.setIndeterminate(false);
          currentWorkspace.importDescriptor(fileName.substring(0, fileName.lastIndexOf('.')), inputFile.openStream());
          WorkflowDesigner.INSTANCE.navigator().setWorkspace(currentWorkspace);
        }
      }
      catch(Exception t)
      {
        log.error("Error importing descriptor", t);
      }
    }
  }

  public void workspaceChanged(WorkspaceEvent event)
  {
    if(event.getId()==WorkspaceEvent.WORKSPACE_OPENED)
    {
      setEnabled(true);
      currentWorkspace = event.getWorkspace();
    }
    else
    {
      setEnabled(false);
      currentWorkspace = null;
    }
  }
}

@@ -738,16 +738,60 @@
 Factory;
+%0Aimport foxtrot.Worker;%0Aimport foxtrot.Task;
 %0A%0A/**%0A *
@@ -1672,21 +1672,27 @@
     
-URL inputFile
+final URL importURL
  = d
@@ -1721,24 +1721,24 @@
     if(i
-nputFile
+mportURL
 !=null)%0A
@@ -1761,24 +1761,24 @@
 ng f = i
-nputFile
+mportURL
 .getFile
@@ -1802,32 +1802,32 @@
 le%22.equals(i
-nputFile
+mportURL
 .getProtocol
@@ -1981,24 +1981,30 @@
   %7B%0A        
+final 
 File ouputFi
@@ -2166,24 +2166,24 @@
 equals(i
-nputFile
+mportURL
 .getProt
@@ -2213,24 +2213,24 @@
 w File(i
-nputFile
+mportURL
 .getFile
@@ -2475,79 +2475,247 @@
 .set
-Indeterminate(true);%0A          status.setProgressStatus(%22Loading...%22);%0A
+ProgressStatus(ResourceManager.getString(%22import.progress%22, new Object%5B%5D%7BfileName%7D));%0A          status.setIndeterminate(true);%0A          Worker.post(new Task()%0A          %7B%0A            public Object run() throws Exception%0A            %7B%0A    
     
@@ -2786,16 +2786,20 @@
         
+    
 InputStr
@@ -2808,24 +2808,24 @@
 m in = i
-nputFile
+mportURL
 .openStr
@@ -2827,24 +2827,28 @@
 enStream();%0A
+    
           by
@@ -2885,16 +2885,20 @@
         
+   
+ 
 int nch;
@@ -2898,16 +2898,20 @@
 nt nch;%0A
+    
         
@@ -2965,34 +2965,42 @@
 = -1)%0A          
-%7B%0A
+    %7B%0A    
             out.
@@ -3026,26 +3026,34 @@
 ;%0A          
-%7D%0A
+    %7D%0A    
           ou
@@ -3069,24 +3069,28 @@
 ;%0A          
+    
 out.close();
@@ -3086,24 +3086,79 @@
 ut.close();%0A
+              return null;%0A            %7D%0A          %7D);%0A
           st
@@ -3183,24 +3183,56 @@
 ate(false);%0A
+          status.setStatus(%22%22);%0A
           cu
@@ -3318,16 +3318,16 @@
 ), i
-nputFile
+mportURL
 .ope

Use foxtrot when importing remote descriptor