KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > config > WorkspaceManager


1 package com.opensymphony.workflow.config;
2
3 import java.io.*;
4 import java.util.*;
5 import java.net.URL JavaDoc;
6
7 import com.opensymphony.workflow.FactoryException;
8 import com.opensymphony.workflow.designer.event.WorkspaceListener;
9 import com.opensymphony.workflow.designer.event.WorkspaceEvent;
10 import com.opensymphony.workflow.loader.Workspace;
11
12 /**
13  * @author Hani Suleiman (hani@formicary.net)
14  * Date: May 15, 2003
15  * Time: 7:57:28 PM
16  */

17 public class WorkspaceManager
18 {
19   private Workspace currentWorkspace = null;
20   private Collection listeners;
21
22   public void loadWorkspace(URL JavaDoc url) throws FactoryException, IOException
23   {
24     fireWorkspaceClosed();
25     saveWorkspace();
26     DefaultConfiguration config = new DefaultConfiguration();
27     config.load(url);
28     currentWorkspace = (Workspace)config.getFactory();
29     fireWorkspaceOpened();
30   }
31
32   public void addWorkspaceListener(WorkspaceListener listener)
33   {
34     if(listeners==null)
35     {
36       listeners = new HashSet();
37     }
38     listeners.add(listener);
39   }
40
41   public void removeWorkspaceListener(WorkspaceListener listener)
42   {
43     if(listeners==null) return;
44     listeners.remove(listener);
45   }
46
47   protected void fireWorkspaceOpened()
48   {
49     if(listeners==null) return;
50     WorkspaceEvent event = new WorkspaceEvent(this, currentWorkspace, WorkspaceEvent.WORKSPACE_OPENED);
51     notifyListeners(event);
52   }
53
54   protected void fireWorkspaceClosed()
55   {
56     if(listeners==null) return;
57     WorkspaceEvent event = new WorkspaceEvent(this, currentWorkspace, WorkspaceEvent.WORKSPACE_CLOSED);
58     notifyListeners(event);
59   }
60
61   private void notifyListeners(WorkspaceEvent event)
62   {
63     Iterator iter = listeners.iterator();
64     while(iter.hasNext())
65     {
66       WorkspaceListener listener = (WorkspaceListener)iter.next();
67       listener.workspaceChanged(event);
68       if(event.isConsumed()) return;
69     }
70   }
71
72   public void setCurrentWorkspace(Workspace current)
73   {
74     saveWorkspace();
75     if(this.currentWorkspace!=null)
76       fireWorkspaceClosed();
77     currentWorkspace = current;
78     if(currentWorkspace!=null)
79       fireWorkspaceOpened();
80   }
81
82   public void saveWorkspace()
83   {
84     if(currentWorkspace!=null)
85     {
86       currentWorkspace.save();
87     }
88   }
89
90   public Workspace getCurrentWorkspace()
91   {
92     return currentWorkspace;
93   }
94 }
95
Popular Tags