KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > TabbedPaneExample


1 /*
2  * $Id: TabbedPaneExample.java,v 1.12 2005/05/27 15:03:47 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 wingset;
15
16
17 import org.wings.*;
18 import org.wings.style.CSSProperty;
19 import org.wings.style.CSSStyleSheet;
20
21 import javax.swing.event.ChangeEvent JavaDoc;
22 import javax.swing.event.ChangeListener JavaDoc;
23 import java.awt.*;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26
27 /**
28  * Example for STabbedPane.
29  *
30  * @author <a HREF="mailto:andre@lison.de">Andre Lison</a>
31  */

32 public class TabbedPaneExample extends WingSetPane {
33     private final static int INITIAL_TAB_COUNT = 12;
34     private final static SIcon JAVA_CUP_ICON = new SResourceIcon("org/wings/icons/JavaCup.gif");
35     private final static SIcon SMALL_COW_ICON = new SURLIcon("../icons/cowSmall.gif");
36     private TabbedPaneControls controls;
37     private STabbedPane tabbedPane;
38     private STextArea textArea;
39
40     protected SComponent createExample() {
41         controls = new TabbedPaneControls();
42
43         SForm c = new SForm(new SBorderLayout());
44         c.add(controls, SBorderLayout.NORTH);
45
46         tabbedPane = new STabbedPane();
47         tabbedPane.setAttribute(STabbedPane.SELECTOR_CONTENT,
48                 CSSProperty.BACKGROUND_COLOR, CSSStyleSheet
49                         .getAttribute(new java.awt.Color JavaDoc(200, 200, 255)));
50         tabbedPane.setShowAsFormComponent(false);
51         controls.addSizable(tabbedPane);
52
53         SPanel west = new SPanel(new SFlowDownLayout());
54         west.add(new SLabel("<html>Tab Placement:&nbsp;"));
55
56         PlacementActionListener placementActionListener = new PlacementActionListener(tabbedPane);
57
58         SRadioButton top = new SRadioButton("Top");
59         top.putClientProperty("placement", new Integer JavaDoc(SConstants.TOP));
60         top.addActionListener(placementActionListener);
61         top.setShowAsFormComponent(false);
62         west.add(top);
63
64         SRadioButton left = new SRadioButton("Left");
65         left.putClientProperty("placement", new Integer JavaDoc(SConstants.LEFT));
66         left.addActionListener(placementActionListener);
67         left.setShowAsFormComponent(false);
68         west.add(left);
69
70         SRadioButton right = new SRadioButton("Right");
71         right.putClientProperty("placement", new Integer JavaDoc(SConstants.RIGHT));
72         right.addActionListener(placementActionListener);
73         right.setShowAsFormComponent(false);
74         west.add(right);
75
76         SRadioButton bottom = new SRadioButton("Bottom");
77         bottom.putClientProperty("placement", new Integer JavaDoc(SConstants.BOTTOM));
78         bottom.addActionListener(placementActionListener);
79         bottom.setShowAsFormComponent(false);
80         west.add(bottom);
81
82         SButtonGroup group = new SButtonGroup();
83         group.add(top);
84         group.add(left);
85         group.add(right);
86         group.add(bottom);
87         group.setSelected(top, true);
88
89         west.add(new SLabel("<html>&nbsp;"));
90
91         group = new SButtonGroup();
92         west.add(new SLabel("<html>Color:&nbsp;"));
93
94         final Object JavaDoc[] clrs = {
95             "Yellow", Color.yellow,
96             "Green", Color.green,
97             "Lightblue", new Color(200, 200, 255),
98             "Lightgray", Color.lightGray,
99             "Orange", new Color(255, 153, 0)};
100
101         for (int i = 0; i < clrs.length; i += 2) {
102             SRadioButton btn = new SRadioButton(clrs[i].toString());
103             btn.putClientProperty("color", clrs[i + 1]);
104             btn.addActionListener(new ColorActionListener(tabbedPane));
105             btn.setShowAsFormComponent(false);
106             group.add(btn);
107             if (i == 0) {
108                 group.setSelected(btn, true);
109             }
110             west.add(btn);
111         }
112         west.setHorizontalAlignment(SConstants.CENTER);
113         c.add(west, "West");
114
115         textArea = new STextArea();
116         textArea.setPreferredSize(new SDimension("100%", "100%"));
117         for (int i = 0; i < INITIAL_TAB_COUNT; ++i) {
118             SPanel p = new SPanel(new SBorderLayout());
119             p.add(new SLabel("Tab # " + i), "North");
120             p.add(textArea);
121             tabbedPane.add("Tab " + i, p);
122         }
123         tabbedPane.setIconAt(3, JAVA_CUP_ICON);
124         tabbedPane.setIconAt(8, SMALL_COW_ICON);
125         tabbedPane.setEnabledAt(1, false);
126         tabbedPane.addChangeListener(new ChangeListener JavaDoc() {
127             public void stateChanged(ChangeEvent JavaDoc ce) {
128                 String JavaDoc t = textArea.getText();
129                 t += "\n";
130                 t += "wingS: Changed to tab " + tabbedPane.getSelectedIndex() + "\n";
131                 textArea.setText(t);
132             }
133         });
134         c.add(tabbedPane, "Center");
135
136         return c;
137     }
138
139     private static class PlacementActionListener implements ActionListener JavaDoc {
140         private final STabbedPane tpane;
141
142         public PlacementActionListener(STabbedPane tpane) {
143             this.tpane = tpane;
144         }
145
146         public void actionPerformed(ActionEvent JavaDoc ae) {
147             SComponent source = (SComponent) ae.getSource();
148             Integer JavaDoc placement = (Integer JavaDoc) source.getClientProperty("placement");
149             tpane.setTabPlacement(placement.intValue());
150         }
151     }
152
153     private static class ColorActionListener implements ActionListener JavaDoc {
154         private final STabbedPane tabs;
155
156         public ColorActionListener(STabbedPane tabs) {
157             this.tabs = tabs;
158         }
159
160         public void actionPerformed(ActionEvent JavaDoc ae) {
161             SComponent source = (SComponent) ae.getSource();
162             Color color = (Color) source.getClientProperty("color");
163             tabs.setAttribute(STabbedPane.SELECTOR_CONTENT,
164                     CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color));
165             tabs.setBackground(color);
166         }
167     }
168
169     class TabbedPaneControls extends ComponentControls {
170         private int tabCount = INITIAL_TAB_COUNT;
171         public TabbedPaneControls() {
172             final SCheckBox showAsFormComponent = new SCheckBox("Show as Form Component");
173             showAsFormComponent.addActionListener(new ActionListener JavaDoc() {
174                 public void actionPerformed(ActionEvent JavaDoc e) {
175                     tabbedPane.setShowAsFormComponent(showAsFormComponent.isSelected());
176                 }
177             });
178             add(showAsFormComponent);
179             final SButton addTab = new SButton("add a tab");
180             addTab.addActionListener(new ActionListener JavaDoc() {
181                 public void actionPerformed(ActionEvent JavaDoc e) {
182                     addTab();
183                 }
184             });
185             add(addTab);
186             final SCheckBox removeCurrent = new SCheckBox("remove current tab");
187             final SButton removeTab = new SButton("remove a tab");
188             removeTab.addActionListener(new ActionListener JavaDoc() {
189                 public void actionPerformed(ActionEvent JavaDoc e) {
190                     removeTab(removeCurrent.isSelected());
191                 }
192             });
193             add(removeTab);
194             add(removeCurrent);
195         }
196         protected void removeTab(boolean removeCurrent) {
197             if (removeCurrent) {
198                 int index = tabbedPane.getSelectedIndex();
199                 if (index != -1) {
200                     tabCount--;
201                     tabbedPane.removeTabAt(index);
202                 }
203             } else {
204                 if (tabCount > 0) {
205                     tabCount--;
206                     tabbedPane.removeTabAt(tabCount);
207                 }
208             }
209         }
210         protected void addTab() {
211             SPanel p = new SPanel(new SBorderLayout());
212             p.add(new SLabel("Tab # " + tabCount), "North");
213             p.add(textArea);
214             tabbedPane.add("Tab " + tabCount, p);
215             tabCount++;
216         }
217     }
218 }
219
Popular Tags