001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.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.BorderLayout;
022    import java.awt.Component;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Map;
026    import java.util.Vector;
027    import javax.swing.JScrollPane;
028    import javax.swing.JSplitPane;
029    import org.objectweb.jac.aspects.gui.*;
030    import org.objectweb.jac.util.ExtArrays;
031    
032    public class SwingPanelView extends AbstractCompositeView 
033        implements PanelView 
034    {
035       
036        int geometry;
037        int subPanesCount;
038        boolean[] scrollings;
039    
040        Vector subPanes = new Vector();
041        Vector splitters = new Vector();
042        Map splitterLocations;
043        Map paneContainers;
044    
045        public SwingPanelView(ViewFactory factory, 
046                              int subPanesCount, int geometry, 
047                              Map paneContainers,
048                              boolean[] scrollings, Map splitterLocations) {
049            this.factory = factory;
050            this.geometry = geometry;
051            this.subPanesCount = subPanesCount;
052            this.scrollings = scrollings;
053            this.paneContainers = paneContainers;
054            this.splitterLocations=splitterLocations;
055            setLayout(new BorderLayout());
056            construct();
057        }
058    
059        protected void construct() 
060        {
061            splitters.clear();
062            subPanes.clear();
063            removeAll();
064            Vector subScrollPanes = new Vector();
065            for(int i=0;i<subPanesCount;i++) {
066                CompositeView subPane;
067                if (paneContainers.containsKey(Integer.toString(i))) {
068                    subPane = factory.createCompositeView(
069                        "subpane["+i+"]",(String)paneContainers.get(Integer.toString(i)),
070                        new Object[] {},
071                        context);
072                } else {
073                    subPane = 
074                        factory.createCompositeView("subpane["+i+"]","SingleSlotContainer",
075                                                    ExtArrays.emptyObjectArray,
076                                                    context);
077                }
078                JScrollPane subScrollPane = null;
079                if (scrollings[i] == true) {
080                    subScrollPane = new JScrollPane((Component)subPane);
081                }
082                subPanes.add(subPane);
083                if (subScrollPane == null) {
084                    subScrollPanes.add(subPane);
085                } else {
086                    subScrollPanes.add(subScrollPane);
087                }
088            }
089    
090            if (subPanesCount == 1) {
091                add((Component)subScrollPanes.get(0));
092            } else if (subPanesCount == 2) {
093                // create a split panel
094                JSplitPane splitPane  = new JSplitPane(
095                    geometry==Constants.HORIZONTAL?
096                    JSplitPane.VERTICAL_SPLIT:JSplitPane.HORIZONTAL_SPLIT,
097                    (Component)subScrollPanes.get(0), (Component)subScrollPanes.get(1));
098                splitPane.setOneTouchExpandable(true);
099                splitters.add(splitPane);
100                add(splitPane);
101            } else if (subPanesCount == 4) {
102                // create two sub-split panels
103                JSplitPane subSplitPane1  = new JSplitPane(
104                    geometry==Constants.HORIZONTAL?
105                    JSplitPane.HORIZONTAL_SPLIT:JSplitPane.VERTICAL_SPLIT,
106                    (Component)subScrollPanes.get(0), (Component)subScrollPanes.get(1));
107                subSplitPane1.setOneTouchExpandable(true);
108                JSplitPane subSplitPane2  = new JSplitPane(
109                    geometry==Constants.HORIZONTAL?
110                    JSplitPane.HORIZONTAL_SPLIT:JSplitPane.VERTICAL_SPLIT,
111                    (Component)subScrollPanes.get(2), (Component)subScrollPanes.get(3));
112                subSplitPane2.setOneTouchExpandable(true);
113                // create a split panel
114                JSplitPane splitPane  = new JSplitPane(
115                    geometry==Constants.HORIZONTAL?
116                    JSplitPane.VERTICAL_SPLIT:JSplitPane.HORIZONTAL_SPLIT,
117                    subSplitPane1, subSplitPane2);
118                splitPane.setOneTouchExpandable(true);
119                splitters.add(splitPane);
120                splitters.add(subSplitPane1);
121                splitters.add(subSplitPane2);
122                add(splitPane);
123            } else if (subPanesCount == 3) {
124                // create one sub-split panels
125                JSplitPane subSplitPane  = new JSplitPane(
126                    geometry<Constants.VERTICAL?
127                    JSplitPane.HORIZONTAL_SPLIT:JSplitPane.VERTICAL_SPLIT,
128                    (Component)subScrollPanes.get(0), (Component)subScrollPanes.get(1));
129                subSplitPane.setOneTouchExpandable(true);
130                // create a split panel
131                int type;
132                int splitType;
133                if (geometry<Constants.VERTICAL) {
134                    type = geometry - Constants.HORIZONTAL;
135                    splitType = JSplitPane.VERTICAL_SPLIT;
136                } else {
137                    type = geometry - Constants.VERTICAL;
138                    splitType = JSplitPane.HORIZONTAL_SPLIT;
139                }
140                JSplitPane splitPane = new JSplitPane(
141                    splitType,
142                    type==1?subSplitPane:(Component)subScrollPanes.get(2),
143                    type==1?(Component)subScrollPanes.get(2):subSplitPane);
144                splitPane.setOneTouchExpandable(true);
145                splitters.add(splitPane);
146                splitters.add(subSplitPane);
147                add(splitPane);
148            }
149        }
150    
151        public void setSplitterLocation( int splitterId, float location ) {
152            JSplitPane splitter = (JSplitPane)splitters.get(splitterId);
153            splitter.setDividerLocation( (int)(getSize().getHeight()*location) );
154        }
155    
156        /**
157         * Adds a view.
158         *
159         * @param component the view to add
160         * @param extraInfo the panel ID
161         */ 
162    
163        public void addView(View component, Object extraInfo) {
164            int i;
165            try {
166                i = new Integer((String)extraInfo).intValue();
167            } catch (NumberFormatException e) {
168                switch (geometry) {
169                    case CustomizedGUI.HORIZONTAL:
170                        if (extraInfo.equals(PanelView.UPPER) || 
171                            extraInfo.equals(PanelView.UPPER_LEFT))
172                            i = 0;
173                        else if (extraInfo.equals(PanelView.LOWER) ||
174                                 extraInfo.equals(PanelView.LOWER_LEFT))
175                            i = 1;
176                        else if (extraInfo.equals(PanelView.UPPER_RIGHT))
177                            i = 2;
178                        else if (extraInfo.equals(PanelView.LOWER_RIGHT))
179                            i = 3;
180                        else
181                            throw new RuntimeException("Unknown position: "+extraInfo);
182                        break;
183                    case CustomizedGUI.VERTICAL:
184                        if (extraInfo.equals(PanelView.LEFT) || 
185                            extraInfo.equals(PanelView.UPPER_LEFT))
186                            i = 0;
187                        else if (extraInfo.equals(PanelView.RIGHT) || 
188                                 extraInfo.equals(PanelView.UPPER_RIGHT))
189                            i = 1;
190                        else if (extraInfo.equals(PanelView.LOWER_LEFT))
191                            i = 2;
192                        else if (extraInfo.equals(PanelView.LOWER_RIGHT))
193                            i = 3;
194                        else
195                            throw new RuntimeException("Unknown position: "+extraInfo);
196                        break;
197                    case CustomizedGUI.VERTICAL_RIGHT:
198                        if (extraInfo.equals(PanelView.LEFT))
199                            i = 2;
200                        else if (extraInfo.equals(PanelView.UPPER_RIGHT))
201                            i = 0;
202                        else if (extraInfo.equals(PanelView.LOWER_RIGHT))
203                            i = 1;
204                        else
205                            throw new RuntimeException("Unknown position: "+extraInfo);
206                        break;
207                    case CustomizedGUI.VERTICAL_LEFT:
208                        if (extraInfo.equals(PanelView.RIGHT))
209                            i = 2;
210                        else if (extraInfo.equals(PanelView.UPPER_LEFT))
211                            i = 0;
212                        else if (extraInfo.equals(PanelView.LOWER_LEFT))
213                            i = 1;
214                        else
215                            throw new RuntimeException("Unknown position: "+extraInfo);
216                        break;
217                    case CustomizedGUI.HORIZONTAL_DOWN:
218                        if (extraInfo.equals(PanelView.UPPER))
219                            i = 2;
220                        else if (extraInfo.equals(PanelView.LOWER_LEFT))
221                            i = 0;
222                        else if (extraInfo.equals(PanelView.LOWER_RIGHT))
223                            i = 1;
224                        else
225                            throw new RuntimeException("Unknown position: "+extraInfo);
226                        break;
227                    case CustomizedGUI.HORIZONTAL_UP:
228                        if (extraInfo.equals(PanelView.LOWER))
229                            i = 2;
230                        else if (extraInfo.equals(PanelView.UPPER_LEFT))
231                            i = 0;
232                        else if (extraInfo.equals(PanelView.UPPER_RIGHT))
233                            i = 1;
234                        else
235                            throw new RuntimeException("Unknown position: "+extraInfo);
236                        break;
237                    default:
238                        throw new RuntimeException("Invalid geometry: "+geometry);
239                }
240            }
241            CompositeView subPane=(CompositeView)subPanes.get(i);
242            subPane.addView(component,null);
243            component.setParentView(this);
244        }
245    
246        public Collection getViews() {
247            return subPanes;//Arrays.asList(getComponents());
248        }
249    
250        public View getView(Object id) {
251            return (View)subPanes.get(Integer.parseInt((String)id));
252        }
253    
254        public void close(boolean validate) {
255            Iterator it = subPanes.iterator();
256            while(it.hasNext()) {
257                View sp = (View)it.next();
258                sp.close(validate);
259            }
260        }
261    
262        public void removeAllViews() {
263            //
264        }
265    }