KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > actions > ImportWorkflow


1 package com.opensymphony.workflow.designer.actions;
2
3 import java.awt.event.ActionEvent JavaDoc;
4 import java.awt.*;
5 import java.io.*;
6 import java.net.URL JavaDoc;
7 import javax.swing.*;
8
9 import com.opensymphony.workflow.designer.event.WorkspaceListener;
10 import com.opensymphony.workflow.designer.event.WorkspaceEvent;
11 import com.opensymphony.workflow.designer.WorkflowDesigner;
12 import com.opensymphony.workflow.designer.ResourceManager;
13 import com.opensymphony.workflow.designer.Utils;
14 import com.opensymphony.workflow.designer.swing.status.StatusDisplay;
15 import com.opensymphony.workflow.designer.dialogs.ImportWorkflowDialog;
16 import com.opensymphony.workflow.loader.Workspace;
17 import com.opensymphony.workflow.FactoryException;
18 import foxtrot.Worker;
19 import foxtrot.Task;
20
21 /**
22  * @author Hani Suleiman (hani@formicary.net)
23  * Date: May 21, 2003
24  * Time: 12:55:51 AM
25  */

26 public class ImportWorkflow extends AbstractAction implements WorkspaceListener
27 {
28   private Workspace currentWorkspace;
29
30   public ImportWorkflow()
31   {
32     setEnabled(false);
33   }
34
35   public void actionPerformed(ActionEvent JavaDoc e)
36   {
37     if(currentWorkspace.getLocation() == null)
38     {
39       JOptionPane.showMessageDialog((Component)e.getSource(), ResourceManager.getString("error.save.unsavedspace"), ResourceManager.getString("title.warning"), JOptionPane.WARNING_MESSAGE);
40       return;
41     }
42     ImportWorkflowDialog dialog = new ImportWorkflowDialog(WorkflowDesigner.INSTANCE, (String JavaDoc)getValue(NAME), true);
43     dialog.pack();
44     dialog.getBanner().setTitle("");
45     dialog.getBanner().setSubtitle(ResourceManager.getString("import.long"));
46     Utils.centerComponent(WorkflowDesigner.INSTANCE, dialog);
47     if(!dialog.ask()) return;
48     importURL(dialog.getImportURL());
49   }
50
51   public void importURL(final URL JavaDoc url)
52   {
53     if(url==null) return;
54     StatusDisplay status = (StatusDisplay)WorkflowDesigner.INSTANCE.statusBar.getItemByName("Status");
55     String JavaDoc f = url.getFile();
56     if(!"file".equals(url.getProtocol()) && f.indexOf('?')>-1) f = f.substring(0, f.indexOf('?'));
57     String JavaDoc fileName = f.substring(f.lastIndexOf('/')+1, f.length());
58     try
59     {
60       final File outputFile = new File(currentWorkspace.getLocation().getParentFile(), fileName);
61       if(outputFile.isDirectory()) throw new Exception JavaDoc("Output file is a directory!");
62       //don't allow importing of files within the workspace!
63
if(!"file".equals(url.getProtocol()) ||
64         !new File(url.getFile()).getParentFile().getCanonicalPath().equals(currentWorkspace.getLocation().getParentFile().getCanonicalPath()))
65       {
66         status.setProgressStatus(ResourceManager.getString("import.progress", new Object JavaDoc[]{fileName}));
67         status.setIndeterminate(true);
68         Worker.post(new Task()
69         {
70           public Object JavaDoc run() throws Exception JavaDoc
71           {
72             FileOutputStream out = new FileOutputStream(outputFile);
73             InputStream in = url.openStream();
74             byte[] buff = new byte[4096];
75             int nch;
76             while((nch = in.read(buff, 0, buff.length)) != -1)
77             {
78               out.write(buff, 0, nch);
79             }
80             out.flush();
81             out.close();
82             return null;
83           }
84         });
85         String JavaDoc workflowName = fileName.substring(0, fileName.lastIndexOf('.'));
86         currentWorkspace.importDescriptor(workflowName, url.openStream());
87         //this ensures that the descriptor is loaded into the cache
88
try
89           {
90               currentWorkspace.getWorkflow(workflowName);
91               WorkflowDesigner.INSTANCE.navigator().setWorkspace(currentWorkspace);
92           }
93           catch(FactoryException e)
94           {
95               JOptionPane.showMessageDialog(WorkflowDesigner.INSTANCE, ResourceManager.getString("import.factory.error.long", new Object JavaDoc[]{fileName, e.getMessage()}),
96                                             ResourceManager.getString("import.factory.error"), JOptionPane.ERROR_MESSAGE);
97               currentWorkspace.deleteWorkflow(workflowName);
98           }
99       }
100     }
101     catch(FactoryException ex)
102     {
103     }
104     catch(Exception JavaDoc t)
105     {
106       t.printStackTrace();
107     }
108     finally
109     {
110       status.setIndeterminate(false);
111       status.setStatus("");
112     }
113   }
114
115   public void workspaceChanged(WorkspaceEvent event)
116   {
117     if(event.getId()==WorkspaceEvent.WORKSPACE_OPENED)
118     {
119       setEnabled(true);
120       currentWorkspace = event.getWorkspace();
121     }
122     else
123     {
124       setEnabled(false);
125       currentWorkspace = null;
126     }
127   }
128 }
129
Popular Tags