001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopys.com>, 
003                              Laurent Martelli <laurent@aopsys.com>
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful,
011      but WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013      GNU Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public License
016      along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
018    
019    package org.objectweb.jac.aspects.gui.swing;
020    
021    import java.awt.Component;
022    import java.awt.Dimension;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Vector;
026    import javax.swing.Box;
027    import org.objectweb.jac.aspects.gui.CommitException;
028    import org.objectweb.jac.aspects.gui.CompositeView;
029    import org.objectweb.jac.aspects.gui.DisplayContext;
030    import org.objectweb.jac.aspects.gui.View;
031    
032    public class AbstractCompositeView extends AbstractView 
033        implements CompositeView {
034       
035        public AbstractCompositeView() {
036        }
037    
038        public void setContext(DisplayContext context) {
039            super.setContext(context);
040            // recursively set the display of inner components
041            for (int i=0; i<getComponentCount(); i++) {
042                Component component = getComponent(i);
043                if (component instanceof View) {
044                    ((View)component).setContext(context);
045                }
046            }
047        }
048    
049        public void addView(View view, Object extraInfo) {
050            view.setContext(context);
051            add((Component)view);
052            view.setParentView(this);
053            validate();
054        }
055    
056        public void addView(View view) {
057            addView(view,null);
058        }
059    
060        public void addHorizontalStrut(int width) {
061            add(Box.createRigidArea(new Dimension(width,1)));
062        }
063    
064        public void addVerticalStrut(int height) {
065            add(Box.createRigidArea(new Dimension(1,height)));
066        }
067    
068        public Collection getViews() {
069            Object[] components = getComponents();
070            Vector views = new Vector();
071            // Filter out non View instances because some
072            // javax.swing.Box$Filler are sometimes added behind our back
073            for (int i=0; i<components.length; i++) {
074                if (components[i] instanceof View) {
075                    views.add(components[i]);
076                }
077            }
078            return views;
079        }
080    
081        public View getView(Object id) {
082            if (id instanceof String)
083                return (View)getComponent(Integer.parseInt((String)id));      
084            else if (id instanceof Integer)
085                return (View)getComponent(((Integer)id).intValue());
086            else
087                throw new RuntimeException("getView(): bad id "+id);
088        }
089    
090        public boolean containsView(String viewType, Object[] parameters) {
091            Iterator it = getViews().iterator();
092            while (it.hasNext()) {
093                View view = (View)it.next();
094                if (view.equalsView(viewType,parameters))
095                    return true;
096            }
097            return false;
098        }
099    
100        public void removeView(View component, boolean validate)
101        {
102            component.close(validate);
103            remove((Component)component);
104            validate();
105        }
106    
107        public void removeAllViews(boolean validate) {
108            close(validate);
109            removeAll();
110        }
111    
112        public void close(boolean validate) {
113            super.close(validate);
114            Iterator i = getViews().iterator();
115            while (i.hasNext()) {
116                Object view = i.next();
117                if (view instanceof View) {
118                    try {
119                        ((View)view).close(validate);
120                    } catch (CommitException e) {
121                        throw e;
122                    } catch (Exception e) {
123                        loggerClose.error("AbstractCompositeView.close: failed to close "+view,e);
124                    }
125                }
126            }
127        }
128    }
129