KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.log4j.Logger;
27 import org.objectweb.jac.aspects.gui.*;
28 import org.objectweb.jac.util.ExtArrays;
29
30 /**
31  * Base class for composite views
32  */

33 public class AbstractCompositeView extends AbstractView
34     implements CompositeView,HTMLViewer
35 {
36     static Logger logger = Logger.getLogger("gui.close");
37    
38     Vector JavaDoc components = new Vector JavaDoc();
39
40     public AbstractCompositeView() {
41     }
42
43     public void setContext(DisplayContext context) {
44         super.setContext(context);
45         // recursively set the display of inner components
46
Iterator JavaDoc i = getViews().iterator();
47         while (i.hasNext()) {
48             View component = (View)i.next();
49             component.setContext(context);
50         }
51     }
52
53     public void addHorizontalStrut(int width) {}
54     public void addVerticalStrut(int height) {}
55
56     public void addView(View view, Object JavaDoc extraInfo) {
57         view.setContext(context);
58         components.add(view);
59         view.setParentView(this);
60     }
61
62     public void addView(View view) {
63         addView(view,null);
64     }
65
66     public Collection JavaDoc getViews() {
67         return components;
68     }
69
70     public View getView(Object JavaDoc id) {
71         if (id instanceof String JavaDoc)
72             return (View)components.get(Integer.parseInt((String JavaDoc)id));
73         else if (id instanceof Integer JavaDoc)
74             return (View)components.get(((Integer JavaDoc)id).intValue());
75         else
76             throw new RuntimeException JavaDoc("getView(): bad id "+id);
77     }
78
79     public boolean containsView(String JavaDoc viewType, Object JavaDoc[] parameters) {
80         Iterator JavaDoc it = components.iterator();
81         while (it.hasNext()) {
82             View view = (View)it.next();
83             if (view.equalsView(viewType,parameters))
84                 return true;
85         }
86         return false;
87     }
88
89     public void removeView(View component, boolean validate) {
90         component.close(validate);
91         components.remove(component);
92     }
93
94     public void removeAllViews(boolean validate) {
95         closeAllViews(validate);
96         components.clear();
97     }
98
99     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 JavaDoc i = ((Vector JavaDoc)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 JavaDoc 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 JavaDoc out) {
127         if(message!=null) {
128             String JavaDoc msg=(String JavaDoc)message.invoke(null,ExtArrays.emptyObjectArray);
129             out.println("<div class=\"message\">"+msg+"</div>");
130         }
131     }
132
133     public void genHTML(PrintWriter JavaDoc out) throws IOException JavaDoc {
134         Iterator JavaDoc 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
145
Popular Tags