KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-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.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.aspects.gui.*;
26 import org.objectweb.jac.aspects.gui.web.html.*;
27
28 /**
29  * A tabs component.
30  */

31 public class Tabs extends AbstractCompositeView
32     implements TabsListener, TabsView
33 {
34     static Logger logger = Logger.getLogger("gui.web");
35     static Logger loggerEditor = Logger.getLogger("gui.editor");
36
37     /* name of the tabs */
38     Vector JavaDoc tabs = new Vector JavaDoc();
39
40     /* icons */
41     Vector JavaDoc icons = new Vector JavaDoc();
42
43     /* the selected tab */
44     View selected;
45
46     public Tabs() {
47     }
48
49     /**
50      * Add a tab
51      *
52      * @param extraInfos a String which is the title of the pane
53      */

54     public void addView(View view, Object JavaDoc extraInfos) {
55         logger.debug("TabbedPane.addView("+view+","+extraInfos+")");
56         add(view);
57         tabs.add((String JavaDoc)extraInfos);
58         icons.add("");
59         if (selected==null) {
60             setSelected(view);
61         }
62     }
63
64     public void addTab(View component, String JavaDoc category, String JavaDoc icon) {
65         logger.debug("TabbedPane.addView("+component+","+category+")");
66       
67         add(component);
68         tabs.add((String JavaDoc) category);
69         icons.add(icon);
70         if (selected==null) {
71             setSelected(component);
72         }
73     }
74
75     public View getView(Object JavaDoc id) {
76         if (id instanceof String JavaDoc)
77             try {
78                 return (View)components.get(Integer.parseInt((String JavaDoc)id));
79             } catch (NumberFormatException JavaDoc e) {
80                 return getTab((String JavaDoc)id);
81             }
82         else if (id instanceof Integer JavaDoc)
83             return (View)components.get(((Integer JavaDoc)id).intValue());
84         else
85             throw new RuntimeException JavaDoc("getView(): bad id "+id);
86     }
87
88     public void select(String JavaDoc tab) {
89         setSelected(getTab(tab));
90     }
91
92     /**
93      * Disable editors which are not an the selected tab
94      */

95     protected void setSelected(View selected) {
96         this.selected = selected;
97         loggerEditor.debug("setSelected "+selected);
98         Iterator JavaDoc it = context.getEditors().iterator();
99         while (it.hasNext()) {
100             Object JavaDoc view = it.next();
101             if (view instanceof FieldEditor) {
102                 FieldEditor editor = (FieldEditor)view;
103                 if (((View)editor).isDescendantOf(selected))
104                     editor.setEnabled(true);
105                 else
106                     editor.setEnabled(false);
107             }
108         }
109     }
110
111     /**
112      * Returns the tab with a given name
113      * @param tab the name of the tab
114      */

115     public View getTab(String JavaDoc tab) {
116         return getView(new Integer JavaDoc(tabs.indexOf(tab)));
117     }
118
119     // HTMLViewer interface
120

121     public void genHTML(PrintWriter JavaDoc out) throws IOException JavaDoc {
122         Iterator JavaDoc i = tabs.iterator();
123         Iterator JavaDoc j = icons.iterator();
124         int index = 0;
125       
126         if (tabs.size() != icons.size())
127             throw new RuntimeException JavaDoc("Number of tabs and number" +
128                                        " of icons are different");
129
130         out.println("<div class=\""+type+"\">");
131         JacRequest request=WebDisplay.getRequest();
132         if (request.isIEUserAgent()) {
133             //out.println(" <div class=\"ieheader\">");
134
out.println(" <table class=\"ieheader\"><tr>");
135         } else {
136             out.println(" <div class=\"header\">");
137         }
138
139         while (i.hasNext()) {
140             String JavaDoc icon = (String JavaDoc) j.next();
141             String JavaDoc label = (String JavaDoc)i.next();
142             String JavaDoc str;
143             if (icon != null)
144                 str = iconElement(icon, "") + label;
145             else
146                 str = label;
147             Element element = (Element)eventURL(str, "onSelect",
148                                                 "&amp;index=" + index);
149             if (selected==components.get(index)) {
150                 element.cssClass("selected");
151             }
152             try {
153                 if (request.isIEUserAgent()) {
154                     if (selected==components.get(index))
155                         out.println("<td class=\"td-selected\">");
156                     else
157                         out.println("<td class=\"td\">");
158                 }
159                 element.write(out);
160                 if (request.isIEUserAgent()) {
161                     out.println("</td>");
162                     if (i.hasNext()) {
163                         out.println("<td>&nbsp;</td>");
164                     }
165                 }
166             } catch(Exception JavaDoc e) {
167                 e.printStackTrace();
168             }
169             index++;
170         }
171         if (request.isIEUserAgent()) {
172             out.println(" </tr></table>");
173         } else {
174             out.println(" </div>");
175         }
176         out.println(" <div class=\"body\">");
177         if (selected!=null)
178             ((HTMLViewer)selected).genHTML(out);
179         out.println(" </div>");
180         out.println("</div>");
181     }
182
183     // TabsListener interface
184

185     public void onSelect(int index) {
186         try {
187             setSelected((View)components.get(index));
188         } finally {
189             context.getDisplay().refresh();
190         }
191     }
192 }
193
Popular Tags