KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopys.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.Component JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Vector JavaDoc;
26 import javax.swing.Box JavaDoc;
27 import org.objectweb.jac.aspects.gui.CommitException;
28 import org.objectweb.jac.aspects.gui.CompositeView;
29 import org.objectweb.jac.aspects.gui.DisplayContext;
30 import org.objectweb.jac.aspects.gui.View;
31
32 public class AbstractCompositeView extends AbstractView
33     implements CompositeView {
34    
35     public AbstractCompositeView() {
36     }
37
38     public void setContext(DisplayContext context) {
39         super.setContext(context);
40         // recursively set the display of inner components
41
for (int i=0; i<getComponentCount(); i++) {
42             Component JavaDoc component = getComponent(i);
43             if (component instanceof View) {
44                 ((View)component).setContext(context);
45             }
46         }
47     }
48
49     public void addView(View view, Object JavaDoc extraInfo) {
50         view.setContext(context);
51         add((Component JavaDoc)view);
52         view.setParentView(this);
53         validate();
54     }
55
56     public void addView(View view) {
57         addView(view,null);
58     }
59
60     public void addHorizontalStrut(int width) {
61         add(Box.createRigidArea(new Dimension JavaDoc(width,1)));
62     }
63
64     public void addVerticalStrut(int height) {
65         add(Box.createRigidArea(new Dimension JavaDoc(1,height)));
66     }
67
68     public Collection JavaDoc getViews() {
69         Object JavaDoc[] components = getComponents();
70         Vector JavaDoc views = new Vector JavaDoc();
71         // Filter out non View instances because some
72
// javax.swing.Box$Filler are sometimes added behind our back
73
for (int i=0; i<components.length; i++) {
74             if (components[i] instanceof View) {
75                 views.add(components[i]);
76             }
77         }
78         return views;
79     }
80
81     public View getView(Object JavaDoc id) {
82         if (id instanceof String JavaDoc)
83             return (View)getComponent(Integer.parseInt((String JavaDoc)id));
84         else if (id instanceof Integer JavaDoc)
85             return (View)getComponent(((Integer JavaDoc)id).intValue());
86         else
87             throw new RuntimeException JavaDoc("getView(): bad id "+id);
88     }
89
90     public boolean containsView(String JavaDoc viewType, Object JavaDoc[] parameters) {
91         Iterator JavaDoc it = getViews().iterator();
92         while (it.hasNext()) {
93             View view = (View)it.next();
94             if (view.equalsView(viewType,parameters))
95                 return true;
96         }
97         return false;
98     }
99
100     public void removeView(View component, boolean validate)
101     {
102         component.close(validate);
103         remove((Component JavaDoc)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 JavaDoc i = getViews().iterator();
115         while (i.hasNext()) {
116             Object JavaDoc 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 JavaDoc e) {
123                     loggerClose.error("AbstractCompositeView.close: failed to close "+view,e);
124                 }
125             }
126         }
127     }
128 }
129
130
Popular Tags