001    /*
002      Copyright (C) 2002-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,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.web;
019    
020    import java.io.IOException;
021    import java.io.PrintWriter;
022    import java.util.Arrays;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Vector;
026    import org.apache.log4j.Logger;
027    import org.objectweb.jac.aspects.gui.*;
028    import org.objectweb.jac.util.ExtArrays;
029    
030    /**
031     * Base class for composite views
032     */
033    public class AbstractCompositeView extends AbstractView 
034        implements CompositeView,HTMLViewer
035    {
036        static Logger logger = Logger.getLogger("gui.close");
037       
038        Vector components = new Vector();
039    
040        public AbstractCompositeView() {
041        }
042    
043        public void setContext(DisplayContext context) {
044            super.setContext(context);
045            // recursively set the display of inner components
046            Iterator i = getViews().iterator();
047            while (i.hasNext()) {
048                View component = (View)i.next();
049                component.setContext(context);
050            }
051        }
052    
053        public void addHorizontalStrut(int width) {}
054        public void addVerticalStrut(int height) {}
055    
056        public void addView(View view, Object extraInfo) {
057            view.setContext(context);
058            components.add(view);
059            view.setParentView(this);
060        }
061    
062        public void addView(View view) {
063            addView(view,null);
064        }
065    
066        public Collection getViews() {
067            return components;
068        }
069    
070        public View getView(Object id) {
071            if (id instanceof String)
072                return (View)components.get(Integer.parseInt((String)id));      
073            else if (id instanceof Integer)
074                return (View)components.get(((Integer)id).intValue());
075            else
076                throw new RuntimeException("getView(): bad id "+id);
077        }
078    
079        public boolean containsView(String viewType, Object[] parameters) {
080            Iterator it = components.iterator();
081            while (it.hasNext()) {
082                View view = (View)it.next();
083                if (view.equalsView(viewType,parameters))
084                    return true;
085            }
086            return false;
087        }
088    
089        public void removeView(View component, boolean validate) {
090            component.close(validate);
091            components.remove(component);
092        }
093    
094        public void removeAllViews(boolean validate) {
095            closeAllViews(validate);
096            components.clear();
097        }
098    
099        public void close(boolean validate) {
100            super.close(validate);
101            closeAllViews(validate);
102        }
103    
104        protected void closeAllViews(boolean validate) {
105            logger.debug("closing "+components.size()+" components of "+this+": "+components);
106            Iterator i = ((Vector)components.clone()).iterator();
107            while (i.hasNext()) {
108                ((View)i.next()).close(validate);
109            }
110        }
111    
112        protected void add(View component) {
113            component.setParentView(this);
114            components.add(component);
115        }
116    
117        public void genDescription(PrintWriter out) {
118            if (description!=null) {
119                if(!(this instanceof ObjectView && 
120                     (parentView!=null && parentView.getClass()==Dialog.class))) {
121                    out.println("<div class=\"description\">"+description+"</div>");
122                }
123            }
124        }
125    
126        public void genMessage(PrintWriter out) {
127            if(message!=null) {
128                String msg=(String)message.invoke(null,ExtArrays.emptyObjectArray);
129                out.println("<div class=\"message\">"+msg+"</div>");
130            }
131        }
132    
133        public void genHTML(PrintWriter out) throws IOException {
134            Iterator i = components.iterator();
135            while (i.hasNext()) {
136                HTMLViewer component = (HTMLViewer)i.next();
137                out.println("<div class=\""+type+
138                            "\" id=\""+((View)component).getLabel()+"\">");
139                component.genHTML(out);
140                out.println("</div>");
141            }
142        }
143    }
144