Source code: jac/aspects/gui/web/Tabs.java
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 jac.aspects.gui.web;
19
20 import jac.aspects.gui.*;
21 import jac.aspects.gui.web.html.Button;
22 import jac.util.Log;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.util.Iterator;
26 import java.util.Vector;
27 import org.mortbay.html.Element;
28 import org.mortbay.util.UrlEncoded;
29
30 /**
31 * A tabs component.
32 */
33 public class Tabs extends AbstractCompositeView
34 implements TabsListener, TabsView
35 {
36 /* name of the tabs */
37 Vector tabs = new Vector();
38
39 /* icons */
40 Vector icons = new Vector();
41
42 /* the selected tab */
43 View selected;
44
45 public Tabs() {
46 }
47
48 /*
49 * Add a tab
50 *
51 * @param extraInfos a String which is the title of the pane
52 */
53 public void addView(View view, Object extraInfos) {
54 Log.trace("gui.web","TabbedPane.addView("+view+","+extraInfos+")");
55 add(view);
56 tabs.add((String)extraInfos);
57 icons.add("");
58 if (selected==null) {
59 selected=view;
60 }
61 }
62
63 public void addTab(View component, String category, String icon) {
64 Log.trace("gui.web","TabbedPane.addView("+component+","+category+")");
65
66 add(component);
67 tabs.add((String) category);
68 icons.add(icon);
69 if (selected==null) {
70 selected=component;
71 }
72 }
73
74 public View getView(Object id) {
75 if (id instanceof String)
76 try {
77 return (View)components.get(Integer.parseInt((String)id));
78 } catch (NumberFormatException e) {
79 return getTab((String)id);
80 }
81 else if (id instanceof Integer)
82 return (View)components.get(((Integer)id).intValue());
83 else
84 throw new RuntimeException("getView(): bad id "+id);
85 }
86
87 public void select(String tab) {
88 selected = getTab(tab);
89 }
90
91 /**
92 * Returns the tab with a given name
93 * @param tab the name of the tab
94 */
95 public View getTab(String tab) {
96 return getView(new Integer(tabs.indexOf(tab)));
97 }
98
99 // HTMLViewer interface
100
101 public void genHTML(PrintWriter out) throws IOException {
102 Iterator i = tabs.iterator();
103 Iterator j = icons.iterator();
104 int index = 0;
105
106 if (tabs.size() != icons.size())
107 throw new RuntimeException("Number of tabs and number" +
108 " of icons are different");
109
110 out.println("<div class=\"tabs\">");
111 JacRequest request=((WebDisplay)context.getDisplay()).getRequest();
112 if (request.isIEUserAgent()) {
113 //out.println(" <div class=\"ieheader\">");
114 out.println(" <table class=\"ieheader\"><tr>");
115 } else {
116 out.println(" <div class=\"header\">");
117 }
118
119 while (i.hasNext()) {
120 String icon = (String) j.next();
121 String label = (String)i.next();
122 String str;
123 if (icon != null)
124 str = iconElement(icon, "") + label;
125 else
126 str = label;
127 Element element = (Element)eventURL(str, "onSelect",
128 "&index=" + index);
129 if (selected==components.get(index)) {
130 element.cssClass("selected");
131 }
132 try {
133 if (request.isIEUserAgent()) {
134 if (selected==components.get(index))
135 out.println("<td class=\"td-selected\">");
136 else
137 out.println("<td class=\"td\">");
138 }
139 element.write(out);
140 if (request.isIEUserAgent()) {
141 out.println("</td>");
142 if (i.hasNext()) {
143 out.println("<td> </td>");
144 }
145 }
146 } catch(Exception e) {
147 e.printStackTrace();
148 }
149 index++;
150 }
151 if (request.isIEUserAgent()) {
152 out.println(" </tr></table>");
153 } else {
154 out.println(" </div>");
155 }
156 out.println(" <div class=\"body\">");
157 if (selected!=null)
158 ((HTMLViewer)selected).genHTML(out);
159 out.println(" </div>");
160 out.println("</div>");
161 }
162
163 // TabsListener interface
164
165 public void onSelect(int index) {
166 try {
167 selected = (View)components.get(index);
168 } finally {
169 context.getDisplay().refresh();
170 }
171 }
172 }