KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > DisplayContext


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui;
20
21 import java.lang.ref.WeakReference JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25 import org.apache.log4j.Logger;
26 import org.objectweb.jac.util.Strings;
27
28 /**
29  * This class implements a display context.
30  *
31  * <p>A display context contains a display (i.e. means to interact
32  * with the user and to create new view in customized vindows), and a
33  * customized view (i.e a root window of a GUI).</p>
34  *
35  * <p>A display context is passed in the interaction's flow so that
36  * each element of the GUI can construct the right GUI elements using
37  * the contextual factory. It is a defined as a collaboration
38  * attribute used by aspects needing to interact with the GUI
39  * (e.g. authentication, confirmation).
40  *
41  * @see org.objectweb.jac.core.Collaboration */

42
43 public class DisplayContext implements EditorContainer {
44     static Logger logger = Logger.getLogger("gui.context");
45     static Logger loggerEdit = Logger.getLogger("gui.editor");
46
47     CustomizedDisplay display;
48     CustomizedView customizedView;
49     // the current window should be garbaged when closed
50
WeakReference JavaDoc window;
51     Vector JavaDoc editors = new Vector JavaDoc();
52     boolean showButtons = false;
53
54     /**
55      * Construct a new display context from a display and a
56      * customized.
57      *
58      * @param display the display
59      * @param customizedView the customized */

60     public DisplayContext(CustomizedDisplay display,
61                           CustomizedView customizedView) {
62         this.display = display;
63         this.customizedView = customizedView;
64     }
65
66     /**
67      * Construct a new display context from a display and an existing
68      * window that can be of any type.
69      *
70      * @param display the display
71      * @param window a window */

72
73     public DisplayContext(CustomizedDisplay display,
74                           Object JavaDoc window) {
75         this.display = display;
76         this.window = new WeakReference JavaDoc(window);
77     }
78
79     /**
80      * Returns the display for this context.
81      *
82      * <p>A display is an GUI entity that is used by the program to
83      * interact with the GUI users.
84      *
85      * @return the display */

86
87     public CustomizedDisplay getDisplay() {
88         return display;
89     }
90
91     /**
92      * Gets the current customized view.
93      *
94      * <p>A customized is a root window for a GUI. A GUI may contain
95      * several customized.
96      *
97      * @return the current customized */

98
99     public CustomizedView getCustomizedView() {
100         return customizedView;
101     }
102    
103     /**
104      * Sets the customized of this display context.
105      *
106      * @param customizedView the new customized */

107    
108     public void setCustomizedView(CustomizedView customizedView) {
109         this.customizedView = customizedView;
110     }
111
112     /**
113      * Sets the window for this display context.
114      *
115      * @param window the window */

116
117     public void setWindow(Object JavaDoc window) {
118         this.window = new WeakReference JavaDoc(window);
119     }
120
121     /**
122      * Gets the window for this display context.
123      *
124      * @return the window */

125
126     public Object JavaDoc getWindow() {
127         if (customizedView!=null)
128             return customizedView;
129         else
130             return window==null ? null : window.get();
131     }
132
133     // EditorContainer interface
134

135     public void addEditor(Object JavaDoc editor) {
136         editors.add(editor);
137         logger.debug("addEditor "+editor+" -> size="+editors.size());
138     }
139     public void removeEditor(Object JavaDoc editor) {
140         editors.remove(editor);
141         logger.debug("removeEditor "+editor+" -> size="+editors.size());
142     }
143     public List JavaDoc getEditors() {
144         return (List JavaDoc)editors.clone();
145     }
146
147     public boolean hasEnabledEditor() {
148         Iterator JavaDoc it = editors.iterator();
149         while (it.hasNext()) {
150             Object JavaDoc view = it.next();
151             if (view instanceof FieldEditor &&
152                 ((FieldEditor)view).isEnabled()) {
153                 loggerEdit.debug("Found enabled editor "+view+
154                                  "("+((FieldEditor)view).getField()+")");
155                 return true;
156             }
157         }
158         return false;
159     }
160
161     public void setShowButtons(boolean value) {
162         this.showButtons = value;
163     }
164     public boolean showButtons() {
165         return showButtons;
166     }
167
168     /**
169      * A default string representation of the display context. */

170     public String JavaDoc toString() {
171         return "{display="+display+
172             ",customized="+customizedView+
173             ",window="+Strings.hex(window==null ? null : window.get())+"}";
174     }
175 }
176
177
Popular Tags