View Javadoc

1   package org.jcreme.swing.workspace;
2   
3   import java.util.HashSet;
4   import java.util.Hashtable;
5   import java.util.Iterator;
6   
7   /***
8    * This factory class enables to ensure that the identifiers chosen for
9    * workspaces are unique. It also provide a factory method to create workspaces
10   * and a method to retrieve previously built workspaces.
11   * 
12   * @author $Author: dbregeon $
13   * @version $Revision: 1.1 $
14   */
15  public abstract class WorkspaceFactory {
16      /***
17       * The workspaces created through this WorkspaceFactory.
18       */
19      private final Hashtable workspaces = new Hashtable();
20  
21      private final HashSet listeners = new HashSet();
22  
23      /***
24       * This method enables to build a Workspace with the given id.
25       * 
26       * @param id
27       *            the identifier for the new Workspace.
28       * @return the newly created Workspace.
29       * @throws IllegalArgumentException
30       *             if id is null or another Workspace already uses it.
31       */
32      public Workspace buildWorkspace(Object id) throws IllegalArgumentException {
33          if ((id != null) && (this.workspaces.get(id) != null)) {
34              throw new IllegalArgumentException("A workspace identified by "
35                      + id + " already exists.");
36          }
37          if (id == null) {
38              throw new IllegalArgumentException(
39                      "Null is no valid value Workspace identifier.");
40          }
41          Workspace result = build(id);
42          registerWorkspace(result);
43          return result;
44      }
45  
46      /***
47       * This is the factory method that will create the actual Workspace
48       * instances.
49       * 
50       * @param id
51       *            the identifier for the new Workspace.
52       * @return a newly created Workspace.
53       */
54      protected abstract Workspace build(Object id);
55  
56      /***
57       * This method gives access to existing Workspaces created by this factory.
58       * 
59       * @param id
60       *            the identifier of the Workspace to retrieve.
61       * @return the Workspace identified by id in this factory or null if no such
62       *         Workspace exists in this factory.
63       */
64      public Workspace getWorkspace(Object id) {
65          FrameWorkspace result = null;
66          if (id != null) {
67              synchronized (this.workspaces) {
68                  result = (FrameWorkspace) this.workspaces.get(id);
69              }
70          }
71          return result;
72      }
73  
74      /***
75       * This method provides a list of the Workspaces available in this factory.
76       * 
77       * @return an array that contains all the Workspace instances in this
78       *         factory.
79       */
80      public Workspace[] getWorkspaces() {
81          synchronized (this.workspaces) {
82              return (Workspace[]) this.workspaces.values().toArray(
83                      new Workspace[0]);
84          }
85      }
86  
87      /***
88       * This method provides a list of the identifiers used by Workspace in this
89       * factory.
90       * 
91       * @return an array that contains the identifiers of the Workspace instances
92       *         in this factory.
93       */
94      public Object[] getWorkspaceIdentifiers() {
95          synchronized (this.workspaces) {
96              return this.workspaces.keySet().toArray();
97          }
98      }
99  
100     /***
101      * This method enables to remove the Workspace identified by id from this
102      * factory.
103      * 
104      * @param id
105      *            the identifier of the Workspace to remove.
106      */
107     public void unregisterWorkspace(Object id) {
108         if (id != null) {
109             Object removed = null;
110             synchronized (this.workspaces) {
111                 removed = this.workspaces.remove(id);
112             }
113             if (removed != null) {
114                 fireWorkspaceRemovedEvent(id);
115             }
116         }
117     }
118 
119     /***
120      * This method enables to register a Workspace instance in this factory even
121      * if it was not created by it.
122      * 
123      * @param ws
124      *            the Workspace to register.
125      * @throws IllegalArgumentException
126      *             if ws is null, its id is null or a Workspace with the same id
127      *             already exists.
128      */
129     public void registerWorkspace(Workspace ws) throws IllegalArgumentException {
130         if (ws == null) {
131             throw new IllegalArgumentException("Null is not a valid Workspace.");
132         }
133         if (ws.getIdentifier() == null) {
134             throw new IllegalArgumentException(
135                     "Null is not a valid Workspace identifier.");
136         }
137         synchronized (this.workspaces) {
138             if (this.workspaces.get(ws.getIdentifier()) != null) {
139                 throw new IllegalArgumentException(
140                         "A Workspace is already registered with the same identifier, unregister it first.");
141             }
142             this.workspaces.put(ws.getIdentifier(), ws);
143         }
144         fireWorkspaceAddedEvent(ws.getIdentifier());
145     }
146 
147     /***
148      * 
149      * @param l
150      */
151     public void addWorkspaceFactoryListener(WorkspaceFactoryListener l) {
152         if (l != null) {
153             synchronized (this.listeners) {
154                 this.listeners.add(l);
155             }
156         }
157     }
158 
159     /***
160      * 
161      * @param l
162      */
163     public void removeWorkspaceFactoryListener(WorkspaceFactoryListener l) {
164         if (l != null) {
165             synchronized (this.listeners) {
166                 this.listeners.remove(l);
167             }
168         }
169     }
170 
171     protected void fireWorkspaceAddedEvent(Object id) {
172         WorkspaceFactoryEvent event = new WorkspaceFactoryEvent(this, id);
173         synchronized (this.listeners) {
174             Iterator iter = this.listeners.iterator();
175             while (iter.hasNext()) {
176                 ((WorkspaceFactoryListener) iter.next()).workspaceAdded(event);
177             }
178         }
179     }
180 
181     protected void fireWorkspaceRemovedEvent(Object id) {
182         WorkspaceFactoryEvent event = new WorkspaceFactoryEvent(this, id);
183         synchronized (this.listeners) {
184             Iterator iter = this.listeners.iterator();
185             while (iter.hasNext()) {
186                 ((WorkspaceFactoryListener) iter.next())
187                         .workspaceRemoved(event);
188             }
189         }
190     }
191 
192 }