001: package com.opensymphony.workflow.config;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.net.URL;
006:
007: import com.opensymphony.workflow.FactoryException;
008: import com.opensymphony.workflow.designer.event.WorkspaceListener;
009: import com.opensymphony.workflow.designer.event.WorkspaceEvent;
010: import com.opensymphony.workflow.loader.RemoteWorkspace;
011: import com.opensymphony.workflow.loader.WorkflowFactory;
012: import com.opensymphony.workflow.designer.DesignerService;
013:
014: /**
015: * @author Hani Suleiman (hani@formicary.net)
016: * Date: May 15, 2003
017: * Time: 7:57:28 PM
018: */
019: public class WorkspaceManager {
020: //private Workspace currentWorkspace = null;
021: private WorkflowFactory currentWorkspace = null;
022: private Collection listeners;
023:
024: public void loadWorkspace(URL url) throws FactoryException,
025: IOException {
026: fireWorkspaceClosed();
027: saveWorkspace();
028: DefaultConfiguration config = new DefaultConfiguration();
029: config.load(url);
030: currentWorkspace = config.getFactory();
031: fireWorkspaceOpened();
032: }
033:
034: public void loadServiceWorkspace(DesignerService service)
035: throws FactoryException, IOException {
036: fireWorkspaceClosed();
037: saveWorkspace();
038: currentWorkspace = new RemoteWorkspace(service);
039: currentWorkspace.initDone();
040:
041: fireWorkspaceOpened();
042: }
043:
044: public void addWorkspaceListener(WorkspaceListener listener) {
045: if (listeners == null) {
046: listeners = new HashSet();
047: }
048: listeners.add(listener);
049: }
050:
051: public void removeWorkspaceListener(WorkspaceListener listener) {
052: if (listeners == null)
053: return;
054: listeners.remove(listener);
055: }
056:
057: protected void fireWorkspaceOpened() {
058: if (listeners == null)
059: return;
060: WorkspaceEvent event = new WorkspaceEvent(this ,
061: currentWorkspace, WorkspaceEvent.WORKSPACE_OPENED);
062: notifyListeners(event);
063: }
064:
065: protected void fireWorkspaceClosed() {
066: if (listeners == null)
067: return;
068: WorkspaceEvent event = new WorkspaceEvent(this ,
069: currentWorkspace, WorkspaceEvent.WORKSPACE_CLOSED);
070: notifyListeners(event);
071: }
072:
073: private void notifyListeners(WorkspaceEvent event) {
074: Iterator iter = listeners.iterator();
075: while (iter.hasNext()) {
076: WorkspaceListener listener = (WorkspaceListener) iter
077: .next();
078: listener.workspaceChanged(event);
079: if (event.isConsumed())
080: return;
081: }
082: }
083:
084: public void setCurrentWorkspace(WorkflowFactory current) {
085: saveWorkspace();
086: if (this .currentWorkspace != null)
087: fireWorkspaceClosed();
088: currentWorkspace = current;
089: if (currentWorkspace != null)
090: fireWorkspaceOpened();
091: }
092:
093: public void saveWorkspace() {
094: if (currentWorkspace != null) {
095: currentWorkspace.save();
096: }
097: }
098:
099: public WorkflowFactory getCurrentWorkspace() {
100: return currentWorkspace;
101: }
102: }
|