KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > TabbedPaneCG


1 /*
2  * $Id: TabbedPaneCG.java,v 1.36 2005/06/07 12:55:59 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf.css;
15
16
17 import org.wings.SComponent;
18 import org.wings.SConstants;
19 import org.wings.SIcon;
20 import org.wings.STabbedPane;
21 import org.wings.session.Browser;
22 import org.wings.session.BrowserType;
23 import org.wings.io.Device;
24 import org.wings.style.CSSSelector;
25
26 import javax.swing.*;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 public class TabbedPaneCG extends AbstractComponentCG {
34     private static final Map JavaDoc placements = new HashMap JavaDoc();
35     
36     static {
37         placements.put(new Integer JavaDoc(SConstants.TOP), "top");
38         placements.put(new Integer JavaDoc(SConstants.BOTTOM), "bottom");
39         placements.put(new Integer JavaDoc(SConstants.LEFT), "left");
40         placements.put(new Integer JavaDoc(SConstants.RIGHT), "right");
41     }
42
43     public void installCG(SComponent component) {
44         super.installCG(component);
45
46         final STabbedPane tab = (STabbedPane) component;
47         InputMap inputMap = new InputMap();
48         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_DOWN_MASK, false), "previous");
49         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_DOWN_MASK, false), "next");
50         tab.setInputMap(inputMap);
51
52         Action action = new AbstractAction() {
53             public void actionPerformed(ActionEvent JavaDoc e) {
54                 if (tab.getSelectedIndex() > 0 && "previous".equals(e.getActionCommand()))
55                     tab.setSelectedIndex(tab.getSelectedIndex() - 1);
56                 else if (tab.getSelectedIndex() < tab.getTabCount() - 1 && "next".equals(e.getActionCommand()))
57                     tab.setSelectedIndex(tab.getSelectedIndex() + 1);
58                 tab.requestFocus();
59             }
60         };
61
62         ActionMap actionMap = new ActionMap();
63         actionMap.put("previous", action);
64         actionMap.put("next", action);
65         tab.setActionMap(actionMap);
66     }
67
68     public void writeContent(final Device device, final SComponent component)
69             throws java.io.IOException JavaDoc {
70         STabbedPane tabbedPane = (STabbedPane) component;
71         if (tabbedPane.getTabCount() > 0) {
72             String JavaDoc style = component.getStyle();
73             boolean childSelectorWorkaround = !component.getSession().getUserAgent().supportsCssChildSelector();
74             int placement = tabbedPane.getTabPlacement();
75     
76             device.print("<table class=\"SLayout\"");
77             if (childSelectorWorkaround)
78                 Utils.optAttribute(device, "class", style);
79     
80             Utils.printCSSInlineFullSize(device, component.getPreferredSize());
81     
82             Utils.writeEvents(device, component);
83             device.print(">");
84     
85             if (placement == SConstants.TOP)
86                 device.print("<tr><th placement=\"top\"");
87             else if (placement == SConstants.LEFT)
88                 device.print("<tr><th placement=\"left\"");
89             else if (placement == SConstants.RIGHT)
90                 device.print("<tr><td");
91             else if (placement == SConstants.BOTTOM)
92                 device.print("<tr><td");
93     
94             if (childSelectorWorkaround) {
95                 if (placement == SConstants.TOP)
96                     Utils.optAttribute(device, "class", "STabbedPane_top");
97                 else if (placement == SConstants.LEFT)
98                     Utils.optAttribute(device, "class", "STabbedPane_left");
99                 else
100                     Utils.optAttribute(device, "class", "STabbedPane_pane");
101             }
102             device.print(">");
103     
104             if (placement == SConstants.TOP || placement == SConstants.LEFT)
105                 writeTabs(device, tabbedPane);
106             else
107                 writeSelectedPaneContent(device, tabbedPane);
108     
109             if (placement == SConstants.TOP)
110                 device.print("</th></tr><tr><td");
111             else if (placement == SConstants.LEFT)
112                 device.print("</th><td");
113             else if (placement == SConstants.RIGHT)
114                 device.print("</td><th placement=\"right\"");
115             else if (placement == SConstants.BOTTOM)
116                 device.print("</td></tr><tr><th placement=\"bottom\"");
117     
118             if (childSelectorWorkaround) {
119                 if (placement == SConstants.RIGHT)
120                     Utils.optAttribute(device, "class", "STabbedPane_right");
121                 else if (placement == SConstants.BOTTOM)
122                     Utils.optAttribute(device, "class", "STabbedPane_bottom");
123                 else
124                     Utils.optAttribute(device, "class", "STabbedPane_pane");
125             }
126             device.print(">");
127     
128             if (placement == SConstants.TOP
129                     || placement == SConstants.LEFT) {
130                 writeSelectedPaneContent(device, tabbedPane);
131                 device.print("</td></tr></table>");
132             } else {
133                 writeTabs(device, tabbedPane);
134                 device.print("</th></tr></table>");
135             }
136         } else {
137             Utils.printDebug(device, "<!-- tabbedPane has no tabs -->");
138         }
139     }
140
141     /** Renders the currently selected pane of the tabbed Pane. */
142     private void writeSelectedPaneContent(Device device, STabbedPane tabbedPane) throws IOException JavaDoc {
143         SComponent selected = tabbedPane.getSelectedComponent();
144         if (selected != null) {
145             selected.write(device);
146         }
147     }
148
149     private void writeTabs(Device device, STabbedPane tabbedPane) throws IOException JavaDoc {
150         boolean browserSupportCssChildSelector = !tabbedPane.getSession().getUserAgent().supportsCssChildSelector();
151         boolean showAsFormComponent = tabbedPane.getShowAsFormComponent();
152         final Browser browser = tabbedPane.getSession().getUserAgent();
153         // substitute whitespaces for konqueror and ie5.0x
154
boolean nbspWorkaround = browser.getBrowserType().equals(
155                 BrowserType.KONQUEROR)
156                 || (browser.getBrowserType().equals(BrowserType.IE)
157                         && browser.getMajorVersion() == 5 && browser
158                         .getMinorVersion() <= .1);
159
160         for (int i = 0; i < tabbedPane.getTabCount(); i++) {
161             SIcon icon = tabbedPane.getIconAt(i);
162             String JavaDoc title = tabbedPane.getTitleAt(i);
163             String JavaDoc tooltip = tabbedPane.getToolTipText();
164             if (nbspWorkaround)
165                 title = Utils.nonBreakingSpaces(title);
166             
167             /*
168              * needed here so that the tabs can be wrapped. else they are in
169              * one long line. noticed in firefox and konqueror.
170              */

171             Utils.printNewline(device, tabbedPane);
172             
173             if (showAsFormComponent) {
174                 writeButtonStart(device, tabbedPane, String.valueOf(i));
175                 device.print(" type=\"submit\" name=\"")
176                         .print(Utils.event(tabbedPane))
177                         .print("\" value=\"")
178                         .print(i)
179                         .print("\"");
180             } else {
181                 device.print("<a HREF=\"")
182                         .print(tabbedPane.getRequestURL()
183                         .addParameter(Utils.event(tabbedPane) + "=" + i).toString())
184                         .print("\"");
185             }
186
187             if (tooltip != null) {
188                 device.print(" title=\"");
189                 device.print(tooltip);
190                 device.print("\"");
191             }
192
193             device.print(" selected=\"").print(Boolean.toString(i == tabbedPane.getSelectedIndex())).print("\"");
194             if (i == tabbedPane.getSelectedIndex() && tabbedPane.isFocusOwner())
195                 Utils.optAttribute(device, "focus", tabbedPane.getName());
196
197             if (!tabbedPane.isEnabledAt(i))
198                 device.print(" disabled=\"true\"");
199
200             if (browserSupportCssChildSelector) {
201                 StringBuffer JavaDoc cssClassName = new StringBuffer JavaDoc("STabbedPane_Tab_");
202                 if (tabbedPane.getShowAsFormComponent())
203                     cssClassName.append("button_");
204                 cssClassName.append(placements.get(new Integer JavaDoc(tabbedPane.getTabPlacement())));
205                 if (i == tabbedPane.getSelectedIndex()) {
206                     Utils.optAttribute(device, "class", cssClassName.append(" STabbedPane_Tab_selected").toString());
207                 } else if (!tabbedPane.isEnabledAt(i)) {
208                     Utils.optAttribute(device, "class", cssClassName.append(" STabbedPane_Tab_disabled").toString());
209                 } else {
210                     Utils.optAttribute(device, "class", cssClassName.append(" STabbedPane_Tab_unselected").toString());
211                 }
212             }
213             device.print(">");
214
215             if (icon != null && tabbedPane.getTabPlacement() != SConstants.RIGHT) {
216                 device.print("<img");
217                 Utils.optAttribute(device, "src", icon.getURL());
218                 Utils.optAttribute(device, "width", icon.getIconWidth());
219                 Utils.optAttribute(device, "height", icon.getIconHeight());
220                 device.print(" alt=\"");
221                 device.print(icon.getIconTitle());
222                 device.print("\" style=\"margin-left:0.2em;\"/>");
223             }
224             device.print("&nbsp;");
225
226             Utils.write(device, title);
227             device.print("&nbsp;");
228
229             if (icon != null && tabbedPane.getTabPlacement() == SConstants.RIGHT) {
230                 device.print("<img");
231                 Utils.optAttribute(device, "src", icon.getURL());
232                 Utils.optAttribute(device, "width", icon.getIconWidth());
233                 Utils.optAttribute(device, "height", icon.getIconHeight());
234                 device.print(" alt=\"");
235                 device.print(icon.getIconTitle());
236                 device.print("\" style=\"margin-right:0.2em;\"/>");
237             }
238
239             if (showAsFormComponent) {
240                 writeButtonEnd(device);
241             } else {
242                 device.print("</a>");
243             }
244         }
245     }
246
247     protected void writeButtonEnd(Device device) throws IOException JavaDoc {
248         device.print("</button>");
249     }
250
251     protected void writeButtonStart(Device device, STabbedPane tabbedPane, String JavaDoc value) throws IOException JavaDoc {
252         device.print("<button");
253     }
254
255     public CSSSelector mapSelector(SComponent addressedComponent, CSSSelector selector) {
256         CSSSelector mappedSelector = null;
257             String JavaDoc selectorSuffix = (String JavaDoc) geckoMappings.get(selector);
258             if (selectorSuffix != null)
259                 mappedSelector = new CSSSelector("#"+addressedComponent.getName()+selectorSuffix) ;
260         return mappedSelector != null ? mappedSelector : selector;
261     }
262
263     private static final Map JavaDoc geckoMappings = new HashMap JavaDoc();
264     static {
265         geckoMappings.put(STabbedPane.SELECTOR_SELECTED_TAB, " > table > tbody > tr > th > *[selected=\"true\"]");
266         geckoMappings.put(STabbedPane.SELECTOR_UNSELECTED_TAB, " > table > tbody > tr > th > *[selected=\"false\"]");
267         geckoMappings.put(STabbedPane.SELECTOR_CONTENT, " > table > tbody > tr > td");
268         geckoMappings.put(STabbedPane.SELECTOR_TAB_AREA, " > table > tbody > tr > th");
269     }
270 }
271
Popular Tags