View Javadoc

1   /***
2    * Workspace.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  
27  package org.jcreme.swing.workspace;
28  
29  import java.awt.Frame;
30  import java.util.HashSet;
31  import java.util.Hashtable;
32  import java.util.Iterator;
33  
34  import org.jcreme.state.MementoException;
35  import org.jcreme.swing.state.FrameMemento;
36  
37  /***
38   * This class enables to handle workspaces made of Frame objects. The
39   * composition of a workspace can be freely modified as Frames can be added and
40   * removed. Subclasses may provide a different FrameMemento factory method or
41   * they can restrict the modifications of the workspace by overloading the
42   * addFrame and removeFrame methods.
43   * 
44   * @author $Author: dbregeon $
45   * @version $Revision: 1.3 $
46   */
47  public class FrameWorkspace extends BaseWorkspace {
48  	/***
49  	 * Lists the Frames that are currently part of the workspace.
50  	 */
51  	private final HashSet framesList = new HashSet();
52  
53  	/***
54  	 * The FrameMementos associated to the workspace's Frames.
55  	 */
56  	private final Hashtable frameMementos = new Hashtable();
57  
58  	/***
59  	 * Creates new Workspace
60  	 * 
61  	 * @param o
62  	 *            the workspace identifier.
63  	 * @throws IllegalArgumentException
64  	 *             when o is null.
65  	 */
66  	protected FrameWorkspace(Object o) throws IllegalArgumentException {
67  		super(o);
68  	}
69  
70  	/***
71  	 * This method adds a Frame to be handled as part of the workspace.
72  	 * 
73  	 * @param view
74  	 *            the new FrameMemento that stores the Frame to add.
75  	 * @throws IllegalArgumentException
76  	 *             if this Workspace is not able to handle the view.
77  	 */
78  	public void addFrame(FrameMemento view) throws IllegalArgumentException {
79  		if (view != null) {
80  			synchronized (this.framesList) {
81  //				Frame frame = view.getFrame();
82  //				if (!this.framesList.contains(frame)) {
83  //					this.framesList.add(frame);
84  //					this.frameMementos.put(frame, view);
85  //				}
86  			}
87  		}
88  	}
89  
90  	/***
91  	 * This method enables to remove a Frame from the Workspace.
92  	 * 
93  	 * @param view
94  	 *            the FrameMemento that stores the Frame to be removed from the
95  	 *            Workspace.
96  	 * @throws IllegalArgumentException
97  	 *             if the Frame cannot be removed from the Workspace.
98  	 */
99  	public void removeFrame(FrameMemento view) throws IllegalArgumentException {
100 		if (view != null) {
101 			synchronized (this.framesList) {
102 //				Frame frame = view.getFrame();
103 //				this.frameMementos.remove(frame);
104 //				this.framesList.remove(frame);
105 			}
106 		}
107 	}
108 
109 	/***
110 	 * This method shows or hide a Frame. When hidden, its previous state is
111 	 * saved to a FrameMemento. When shown, its previous state is restored from
112 	 * a FrameMemento.
113 	 * 
114 	 * @param view
115 	 *            the Frame to show or hide.
116 	 * @param enable
117 	 *            true if the view is to be displayed, false otherwise.
118 	 */
119 	protected void showView(Frame view, boolean enable) {
120 		if (view != null) {
121 			synchronized (this.framesList) {
122 				if (this.framesList.contains(view)) {
123 					FrameMemento memento = (FrameMemento) this.frameMementos
124 							.get(view);
125 					if (enable) {
126 						if (memento != null) {
127 							try {
128 								memento.updateObject(view);
129 							} catch (MementoException e) {
130 								e.printStackTrace();
131 							}
132 						}
133 						view.show();
134 					} else {
135 						if (memento != null) {
136 							try {
137 								memento.updateMemento(view);
138 							} catch (MementoException e) {
139 								e.printStackTrace();
140 							}
141 						}
142 						view.hide();
143 					}
144 				}
145 			}
146 		}
147 	}
148 
149 	/***
150 	 * This method makes a Workspace to display itself.
151 	 */
152 	public void display() {
153 		synchronized (this.framesList) {
154 			Iterator iter = this.framesList.iterator();
155 			while (iter.hasNext()) {
156 				showView((Frame) iter.next(), true);
157 			}
158 		}
159 	}
160 
161 	/***
162 	 * This method makes a Workspace to hide itself.
163 	 */
164 	public void hide() {
165 		synchronized (this.framesList) {
166 			Iterator iter = this.framesList.iterator();
167 			while (iter.hasNext()) {
168 				showView((Frame) iter.next(), false);
169 			}
170 		}
171 	}
172 
173 	/***
174 	 * This method gives access to the FrameMementos that store the Frames in
175 	 * the Workspace.
176 	 * 
177 	 * @return the FrameMementos used in the Workspace.
178 	 */
179 	public FrameMemento[] getFrameMementos() {
180 		return (FrameMemento[]) this.frameMementos.values().toArray(
181 				new FrameMemento[0]);
182 	}
183 
184 	/***
185 	 * Provides access to the Memento of a specific Frame.
186 	 * 
187 	 * @param frame
188 	 *            the Frame for which we want the Memento.
189 	 * @return the Memento associated to the Frame or null if this Frame is not
190 	 *         in this Workspace.
191 	 */
192 	protected FrameMemento getFrameMemento(Frame frame) {
193 		return (FrameMemento) this.frameMementos.get(frame);
194 	}
195 }