001    /*
002      Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui;
020    
021    import java.lang.ref.WeakReference;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Vector;
025    import org.apache.log4j.Logger;
026    import org.objectweb.jac.util.Strings;
027    
028    /**
029     * This class implements a display context.
030     *
031     * <p>A display context contains a display (i.e. means to interact
032     * with the user and to create new view in customized vindows), and a
033     * customized view (i.e a root window of a GUI).</p>
034     *
035     * <p>A display context is passed in the interaction's flow so that
036     * each element of the GUI can construct the right GUI elements using
037     * the contextual factory. It is a defined as a collaboration
038     * attribute used by aspects needing to interact with the GUI
039     * (e.g. authentication, confirmation).
040     *
041     * @see org.objectweb.jac.core.Collaboration */
042    
043    public class DisplayContext implements EditorContainer {
044        static Logger logger = Logger.getLogger("gui.context");
045        static Logger loggerEdit = Logger.getLogger("gui.editor");
046    
047        CustomizedDisplay display;
048        CustomizedView customizedView;
049        // the current window should be garbaged when closed 
050        WeakReference window;
051        Vector editors = new Vector();
052        boolean showButtons = false;
053    
054        /**
055         * Construct a new display context from a display and a
056         * customized.
057         *
058         * @param display the display
059         * @param customizedView the customized */
060        public DisplayContext(CustomizedDisplay display, 
061                              CustomizedView customizedView) {
062            this.display = display;
063            this.customizedView = customizedView;
064        }
065    
066        /**
067         * Construct a new display context from a display and an existing
068         * window that can be of any type.
069         *
070         * @param display the display
071         * @param window a window */
072    
073        public DisplayContext(CustomizedDisplay display, 
074                              Object window) {
075            this.display = display;
076            this.window = new WeakReference(window);
077        }
078    
079        /**
080         * Returns the display for this context.
081         *
082         * <p>A display is an GUI entity that is used by the program to
083         * interact with the GUI users.
084         *
085         * @return the display */
086    
087        public CustomizedDisplay getDisplay() {
088            return display;
089        }
090    
091        /**
092         * Gets the current customized view.
093         * 
094         * <p>A customized is a root window for a GUI. A GUI may contain
095         * several customized.
096         *
097         * @return the current customized */
098    
099        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 window) {
118            this.window = new WeakReference(window);
119        }
120    
121        /**
122         * Gets the window for this display context.
123         *
124         * @return the window */
125    
126        public Object 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 editor) {
136            editors.add(editor);
137            logger.debug("addEditor "+editor+" -> size="+editors.size());
138        }
139        public void removeEditor(Object editor) {
140            editors.remove(editor);
141            logger.debug("removeEditor "+editor+" -> size="+editors.size());
142        }
143        public List getEditors() {
144            return (List)editors.clone();
145        }
146    
147        public boolean hasEnabledEditor() {
148            Iterator it = editors.iterator();
149            while (it.hasNext()) {
150                Object 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 toString() {
171            return "{display="+display+
172                ",customized="+customizedView+
173                ",window="+Strings.hex(window==null ? null : window.get())+"}";
174        }
175    }
176