1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: SwingView.java,v 1.8 2002/09/13 17:11:28 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40
41 import java.awt.Rectangle;
42 import java.util.ArrayList;
43 import java.util.List;
44 import javax.swing.JMenuBar;
45 import javax.swing.JPanel;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import org.scopemvc.controller.basic.BasicController;
49 import org.scopemvc.controller.basic.ViewContext;
50 import org.scopemvc.controller.swing.SwingContext;
51 import org.scopemvc.core.Control;
52 import org.scopemvc.core.Controller;
53 import org.scopemvc.core.View;
54
55 /***
56 * <P>
57 *
58 * An abstract base JPanel implementation of View for use in Swing-based user
59 * interfaces. {@link org.scopemvc.controller.swing.SwingContext} assumes the
60 * use of SwingView subclasses, for which the following methods should be
61 * overridden:
62 * <UL>
63 * <LI> {@link #getTitle}</LI>
64 * <LI> {@link #getDisplayMode}</LI>
65 * <LI> {@link #getCloseControl}</LI>
66 * <LI> {@link #isResizable}</LI>
67 * </UL>
68 * </P> <P>
69 *
70 * Binding to the model object should be implemented in subclasses appropriately
71 * -- see {@link SPanel} for an example. This split in functionality is to
72 * support splitting Scope into a Controller/View subsystem and an independent
73 * model subsystem. </P> <P>
74 *
75 * SwingView subclasses must issue a CHANGE_MODEL_CONTROL_ID Control when the
76 * shown model object changes. This supports BasicController which needs to know
77 * when its current model changes. </P>
78 *
79 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
80 * @created 18 June 2002
81 * @version $Revision: 1.8 $ $Date: 2002/09/13 17:11:28 $
82 */
83 public abstract class SwingView extends JPanel implements View {
84
85 /***
86 * Description of the Field
87 */
88 public static final int PRIMARY_WINDOW = 0;
89 /***
90 * Description of the Field
91 */
92 public static final int MODAL_DIALOG = 1;
93 /***
94 * Description of the Field
95 */
96 public static final int MODELESS_DIALOG = 2;
97
98 // ------------------- Support SwingContext: don't usually override these -------------------
99
100 /***
101 * Description of the Field
102 */
103 public static final Rectangle CENTRED = new Rectangle();
104
105 private static final Log LOG = LogFactory.getLog(SwingView.class);
106
107 private List subViews = null;
108
109 /***
110 * The bounds that the view will be shown at. If null use default position.
111 * The bounds are saved on hideView so the view come back at the same
112 * position when shown again.
113 */
114 private Rectangle viewBounds;
115
116 private int displayMode = PRIMARY_WINDOW;
117
118 private String title = "Untitled";
119
120 private JMenuBar menuBar;
121
122 private Control closeControl = new Control(BasicController.HIDE_VIEW_CONTROL_ID, this);
123
124 // -------------- implement View ------------------
125
126 private Controller controller;
127
128 /***
129 * Automatically installs a SwingContext if SwingView is created anywhere
130 * and no ViewContext has been set previously.
131 */
132 public SwingView() {
133 if (ViewContext.getViewContext() == null) {
134 ViewContext.setGlobalContext(new SwingContext());
135 }
136 }
137
138
139 /***
140 * Get bounds to show this view at. If null default positioning, if
141 * CENTRED_BOUNDS then centre the view on the screen.
142 *
143 * @return The viewBounds value
144 */
145 public final Rectangle getViewBounds() {
146 return viewBounds;
147 }
148
149
150 /***
151 * @return The lastShownBounds value
152 * @deprecated see {@link #getViewBounds}
153 */
154 public final Rectangle getLastShownBounds() {
155 return getViewBounds();
156 }
157
158
159 // ------------------- Support SwingContext: override these -------------------
160
161 /***
162 * Provides the title for windows that show this view.
163 *
164 * @return The title value
165 * @see org.scopemvc.controller.swing.SwingContext
166 */
167 public String getTitle() {
168 return title;
169 }
170
171
172 /***
173 * Determines the type of window used to show this view.
174 *
175 * @return The displayMode value
176 * @see #PRIMARY_WINDOW
177 * @see #MODAL_DIALOG
178 * @see #MODELESS_DIALOG
179 * @see org.scopemvc.controller.swing.SwingContext
180 */
181 public int getDisplayMode() {
182 return displayMode;
183 }
184
185
186 /***
187 * The Control issued when the window containing this View is closed.
188 *
189 * @return The closeControl value
190 */
191 public Control getCloseControl() {
192 return closeControl;
193 }
194
195
196 /***
197 * Should a window showing this view be resizable?
198 *
199 * @return The resizable value
200 */
201 public boolean isResizable() {
202 return true;
203 }
204
205
206 /***
207 * The menubar to attach to a window showing this view, or null if none.
208 *
209 * @return The menuBar value
210 */
211 public JMenuBar getMenuBar() {
212 return menuBar;
213 }
214
215
216 /***
217 * Gets the controller
218 *
219 * @return The controller value
220 */
221 public Controller getController() {
222 return controller;
223 }
224
225
226 /***
227 * Issue a Control to the parent Controller, or if no direct parent, search
228 * upwards in the View hierarchy for the next up.
229 *
230 * @param inControl the Control to broadcast.
231 */
232 public void issueControl(Control inControl) {
233 SwingUtil.issueControl(this, inControl);
234 }
235
236 /***
237 * Gets the sub view count
238 *
239 * @return The subViewCount value
240 */
241 public int getSubViewCount() {
242 if (subViews != null) {
243 return subViews.size();
244 } else {
245 return 0;
246 }
247 }
248
249 /***
250 * Gets the sub view
251 *
252 * @param i Description of the Parameter
253 * @return The subView value
254 */
255 public SwingSubView getSubView(int i) {
256 if (subViews != null && i < subViews.size()) {
257 return (SwingSubView) subViews.get(i);
258 }
259 return null;
260 }
261
262 /***
263 * The parent Controller must register itself with the SwingView via this
264 * method in order to receive Control from it. Only one Controller can
265 * parent a SwingView, which should be fine if a Chain of Command is used
266 * between Controllers.
267 *
268 * @param inController the parent Controller.
269 */
270 public final void setController(Controller inController) {
271 if (LOG.isDebugEnabled()) {
272 LOG.debug("setController: " + inController);
273 }
274
275 controller = inController;
276 }
277
278
279 /***
280 * The Control issued when the window containing this View is closed.
281 *
282 * @param inCloseControl The closeControl value
283 */
284 public void setCloseControl(Control inCloseControl) {
285 closeControl = inCloseControl;
286 }
287
288 /***
289 * The Control issued when the window containing this View is closed.
290 *
291 * @param inCloseControlID The control ID to issue - usually one of the
292 * following values defined in {@link
293 * org.scopemvc.controller.basic.BasicController BasicController}
294 * @see org.scopemvc.controller.basic.BasicController#HIDE_VIEW_CONTROL_ID
295 * @see org.scopemvc.controller.basic.BasicController#EXIT_CONTROL_ID
296 */
297 public void setCloseControl(String inCloseControlID) {
298 closeControl = new Control(inCloseControlID, this);
299 }
300
301 /***
302 * Defines the type of window used to show this view.
303 *
304 * @param inDisplayMode The new displayMode value
305 * @see #PRIMARY_WINDOW
306 * @see #MODAL_DIALOG
307 * @see #MODELESS_DIALOG
308 * @see org.scopemvc.controller.swing.SwingContext
309 */
310 public void setDisplayMode(int inDisplayMode) {
311 displayMode = inDisplayMode;
312 }
313
314
315 /***
316 * Provides the title for windows that show this view.
317 *
318 * @param inTitle The new title value
319 * @see org.scopemvc.controller.swing.SwingContext
320 */
321 public void setTitle(String inTitle) {
322 title = inTitle;
323 }
324
325
326 /***
327 * Called when hiding a view to store last bounds of this view when visible.
328 * Override to return null if the view should always be packed and placed in
329 * default location. Call this during View ctor to set up initial bounds.
330 *
331 * @param inViewBounds The new viewBounds value
332 */
333 public void setViewBounds(Rectangle inViewBounds) {
334 viewBounds = inViewBounds;
335 }
336
337
338 /***
339 * @param inLastShownBounds The new lastShownBounds value
340 * @deprecated see {@link #setViewBounds}
341 */
342 public void setLastShownBounds(Rectangle inLastShownBounds) {
343 setViewBounds(inLastShownBounds);
344 }
345
346 /***
347 * The menubar to attach to a window showing this view, or null if none.
348 *
349 * @param inMenuBar The new menuBar value
350 */
351 public void setMenuBar(JMenuBar inMenuBar) {
352 menuBar = inMenuBar;
353 }
354
355 /***
356 * Description of the Method
357 *
358 * @return Description of the Return Value
359 */
360 public String toString() {
361 return getClass().toString() + "(" + getTitle() + ")";
362 }
363
364 /***
365 * <P>
366 *
367 * Adds a sub view to this view. <br>
368 * To use only for dependant views that are not Swing components, such as
369 * Actions and MenuItem. </P> <P>
370 *
371 * Standard component views are automatically registered in the SwingView
372 * when they are added to this container or one of its child container with
373 * the add() method. </P>
374 *
375 * @param inSubView The sub view to be added
376 * @see java.awt.Container#add
377 */
378 public void addSubView(SwingSubView inSubView) {
379 if (LOG.isDebugEnabled()) {
380 LOG.debug("addSubView: subView: " + inSubView);
381 }
382 if (subViews == null) {
383 subViews = new ArrayList();
384 }
385 if (!subViews.contains(inSubView)) {
386 subViews.add(inSubView);
387 inSubView.setOwner(this);
388 }
389 }
390
391 /***
392 * Remove a sub view from this view
393 *
394 * @param inSubView The sub view to remove
395 */
396 public void removeSubView(SwingSubView inSubView) {
397 if (subViews == null || inSubView.getOwner() != this) {
398 return;
399 }
400 subViews.remove(inSubView);
401 inSubView.unsetOwner(this);
402 }
403 }
404
This page was automatically generated by Maven