KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > SwingPanelView


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

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Vector JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import javax.swing.JSplitPane JavaDoc;
29 import org.objectweb.jac.aspects.gui.*;
30 import org.objectweb.jac.util.ExtArrays;
31
32 public class SwingPanelView extends AbstractCompositeView
33     implements PanelView
34 {
35    
36     int geometry;
37     int subPanesCount;
38     boolean[] scrollings;
39
40     Vector JavaDoc subPanes = new Vector JavaDoc();
41     Vector JavaDoc splitters = new Vector JavaDoc();
42     Map JavaDoc splitterLocations;
43     Map JavaDoc paneContainers;
44
45     public SwingPanelView(ViewFactory factory,
46                           int subPanesCount, int geometry,
47                           Map JavaDoc paneContainers,
48                           boolean[] scrollings, Map JavaDoc splitterLocations) {
49         this.factory = factory;
50         this.geometry = geometry;
51         this.subPanesCount = subPanesCount;
52         this.scrollings = scrollings;
53         this.paneContainers = paneContainers;
54         this.splitterLocations=splitterLocations;
55         setLayout(new BorderLayout JavaDoc());
56         construct();
57     }
58
59     protected void construct()
60     {
61         splitters.clear();
62         subPanes.clear();
63         removeAll();
64         Vector JavaDoc subScrollPanes = new Vector JavaDoc();
65         for(int i=0;i<subPanesCount;i++) {
66             CompositeView subPane;
67             if (paneContainers.containsKey(Integer.toString(i))) {
68                 subPane = factory.createCompositeView(
69                     "subpane["+i+"]",(String JavaDoc)paneContainers.get(Integer.toString(i)),
70                     new Object JavaDoc[] {},
71                     context);
72             } else {
73                 subPane =
74                     factory.createCompositeView("subpane["+i+"]","SingleSlotContainer",
75                                                 ExtArrays.emptyObjectArray,
76                                                 context);
77             }
78             JScrollPane JavaDoc subScrollPane = null;
79             if (scrollings[i] == true) {
80                 subScrollPane = new JScrollPane JavaDoc((Component JavaDoc)subPane);
81             }
82             subPanes.add(subPane);
83             if (subScrollPane == null) {
84                 subScrollPanes.add(subPane);
85             } else {
86                 subScrollPanes.add(subScrollPane);
87             }
88         }
89
90         if (subPanesCount == 1) {
91             add((Component JavaDoc)subScrollPanes.get(0));
92         } else if (subPanesCount == 2) {
93             // create a split panel
94
JSplitPane JavaDoc splitPane = new JSplitPane JavaDoc(
95                 geometry==Constants.HORIZONTAL?
96                 JSplitPane.VERTICAL_SPLIT:JSplitPane.HORIZONTAL_SPLIT,
97                 (Component JavaDoc)subScrollPanes.get(0), (Component JavaDoc)subScrollPanes.get(1));
98             splitPane.setOneTouchExpandable(true);
99             splitters.add(splitPane);
100             add(splitPane);
101         } else if (subPanesCount == 4) {
102             // create two sub-split panels
103
JSplitPane JavaDoc subSplitPane1 = new JSplitPane JavaDoc(
104                 geometry==Constants.HORIZONTAL?
105                 JSplitPane.HORIZONTAL_SPLIT:JSplitPane.VERTICAL_SPLIT,
106                 (Component JavaDoc)subScrollPanes.get(0), (Component JavaDoc)subScrollPanes.get(1));
107             subSplitPane1.setOneTouchExpandable(true);
108             JSplitPane JavaDoc subSplitPane2 = new JSplitPane JavaDoc(
109                 geometry==Constants.HORIZONTAL?
110                 JSplitPane.HORIZONTAL_SPLIT:JSplitPane.VERTICAL_SPLIT,
111                 (Component JavaDoc)subScrollPanes.get(2), (Component JavaDoc)subScrollPanes.get(3));
112             subSplitPane2.setOneTouchExpandable(true);
113             // create a split panel
114
JSplitPane JavaDoc splitPane = new JSplitPane JavaDoc(
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 JavaDoc subSplitPane = new JSplitPane JavaDoc(
126                 geometry<Constants.VERTICAL?
127                 JSplitPane.HORIZONTAL_SPLIT:JSplitPane.VERTICAL_SPLIT,
128                 (Component JavaDoc)subScrollPanes.get(0), (Component JavaDoc)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 JavaDoc splitPane = new JSplitPane JavaDoc(
141                 splitType,
142                 type==1 ? subSplitPane : (Component JavaDoc)subScrollPanes.get(2),
143                 type==1 ? (Component JavaDoc)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 JavaDoc splitter = (JSplitPane JavaDoc)splitters.get(splitterId);
153         splitter.setDividerLocation((double)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 JavaDoc extraInfo) {
164         int i;
165         try {
166             i = new Integer JavaDoc((String JavaDoc)extraInfo).intValue();
167         } catch (NumberFormatException JavaDoc 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 JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc("Unknown position: "+extraInfo);
236                     break;
237                 default:
238                     throw new RuntimeException JavaDoc("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 JavaDoc getViews() {
247         return subPanes;//Arrays.asList(getComponents());
248
}
249
250     public View getView(Object JavaDoc id) {
251         return (View)subPanes.get(Integer.parseInt((String JavaDoc)id));
252     }
253
254     public void close(boolean validate) {
255         Iterator JavaDoc 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 }
266
Popular Tags