KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > TabbedPane


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.gui;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import javax.swing.*;
27
28
29 /**
30  * This <code>Jpanel</code> is similar to a <code>JTabbedPane</code>.
31  * It uses buttons on the left-hand side to switch between panels.
32  * An image can be added below these buttons.
33  * Some methods are provided to switch between tabs.
34  *
35  * @author Eric Lafortune
36  */

37 public class TabbedPane
38      extends JPanel
39 {
40     private CardLayout cardLayout = new CardLayout();
41     private JPanel cardPanel = new JPanel(cardLayout);
42     private ButtonGroup buttonGroup = new ButtonGroup();
43
44
45     /**
46      * Creates a new TabbedPane.
47      */

48     public TabbedPane()
49     {
50         GridBagLayout layout = new GridBagLayout();
51         setLayout(layout);
52
53         GridBagConstraints cardConstraints = new GridBagConstraints();
54         cardConstraints.gridx = 1;
55         cardConstraints.gridy = 0;
56         cardConstraints.gridheight = GridBagConstraints.REMAINDER;
57         cardConstraints.fill = GridBagConstraints.BOTH;
58         cardConstraints.weightx = 1.0;
59         cardConstraints.weighty = 1.0;
60         cardConstraints.anchor = GridBagConstraints.NORTHWEST;
61
62         add(cardPanel, cardConstraints);
63     }
64
65
66     /**
67      * Adds a component with a given title to the tabbed pane.
68      *
69      * @param title the title that will be used in the tab button.
70      * @param component the component that will be added as a tab.
71      */

72     public Component add(final String JavaDoc title, Component component)
73     {
74         GridBagConstraints buttonConstraints = new GridBagConstraints();
75         buttonConstraints.gridx = 0;
76         buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
77         buttonConstraints.anchor = GridBagConstraints.NORTHWEST;
78         buttonConstraints.ipadx = 10;
79         buttonConstraints.ipady = 4;
80
81         JToggleButton button = new JToggleButton(title);
82
83         // Let the button react on the mouse press, instead of waiting for the
84
// mouse release.
85
button.setModel(new JToggleButton.ToggleButtonModel()
86         {
87             public void setPressed(boolean b)
88             {
89                 if ((isPressed() == b) || !isEnabled())
90                 {
91                     return;
92                 }
93
94                 if (b == false && isArmed())
95                 {
96                     setSelected(!this.isSelected());
97                 }
98
99                 if (b)
100                 {
101                     stateMask |= PRESSED;
102                 }
103                 else
104                 {
105                     stateMask &= ~PRESSED;
106                 }
107
108                 fireStateChanged();
109
110                 if (isPressed())
111                 {
112                     fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand()));
113                 }
114             }
115
116         });
117
118         // Switch to the tab on a button press.
119
button.addActionListener(new ActionListener()
120         {
121             public void actionPerformed(ActionEvent e)
122             {
123                 cardLayout.show(cardPanel, title);
124             }
125         });
126
127         // Only one button can be selected at the same time.
128
buttonGroup.add(button);
129
130         // If this is the first tab, make sure its button is selected.
131
if (cardPanel.getComponentCount() == 0)
132         {
133             button.setSelected(true);
134         }
135
136         // Add the button and its panel.
137
add(button, buttonConstraints);
138         cardPanel.add(title, component);
139
140         return component;
141     }
142
143
144     /**
145      * Adds an image below the tab buttons, after all tabs have been added.
146      * The image will only be as visible as permitted by the available space.
147      *
148      * @param image the image.
149      * @return the component containing the image.
150      */

151     public Component addImage(final Image image)
152     {
153         GridBagConstraints imageConstraints = new GridBagConstraints();
154         imageConstraints.gridx = 0;
155         imageConstraints.weighty = 1.0;
156         imageConstraints.fill = GridBagConstraints.BOTH;
157         imageConstraints.anchor = GridBagConstraints.SOUTHWEST;
158
159         JPanel component = new JPanel()
160         {
161             public void paintComponent(Graphics graphics)
162             {
163                 graphics.drawImage(image, 0, getHeight() - image.getHeight(null), this);
164             }
165         };
166         component.setBorder(BorderFactory.createEtchedBorder());
167
168         add(component, imageConstraints);
169
170         return component;
171     }
172
173
174     /**
175      * Selects the first tab.
176      */

177     public void first()
178     {
179         cardLayout.first(cardPanel);
180         updateButtonSelection();
181     }
182
183
184     /**
185      * Selects the last tab.
186      */

187     public void last()
188     {
189         cardLayout.last(cardPanel);
190         updateButtonSelection();
191     }
192
193
194     /**
195      * Selects the previous tab.
196      */

197     public void previous()
198     {
199         cardLayout.previous(cardPanel);
200         updateButtonSelection();
201     }
202
203
204     /**
205      * Selects the next tab.
206      */

207     public void next()
208     {
209         cardLayout.next(cardPanel);
210         updateButtonSelection();
211     }
212
213
214     /**
215      * Lets the button selection reflect the currently visible panel.
216      */

217     private void updateButtonSelection()
218     {
219         int count = cardPanel.getComponentCount();
220         for (int index = 0 ; index < count ; index++) {
221             Component card = cardPanel.getComponent(index);
222             if (card.isShowing())
223             {
224                 JToggleButton button = (JToggleButton)getComponent(index+1);
225                 button.setSelected(true);
226             }
227         }
228     }
229 }
230
Popular Tags