KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > TabControlButton


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.tabcontrol.plaf;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.BorderFactory JavaDoc;
25 import javax.swing.Icon JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import org.netbeans.swing.tabcontrol.TabDisplayer;
28 import org.netbeans.swing.tabcontrol.event.TabActionEvent;
29
30 /**
31  * <p>A base class for control buttons placed within the tabs (view tabs) or
32  * next to the tab row (editor tabs). By default the button posts a TabActionEvent
33  * to the TabDisplayerUI when pressed.</p>
34  * <p>The button is painted using a set of icons only. The icons should include
35  * 'fake' button border.</p>
36  *
37  * @author S. Aubrecht
38  */

39 abstract class TabControlButton extends JButton JavaDoc {
40     
41     public static final int ID_CLOSE_BUTTON = 1;
42     public static final int ID_PIN_BUTTON = 2;
43     public static final int ID_MAXIMIZE_BUTTON = 3;
44     public static final int ID_RESTORE_BUTTON = 4;
45     public static final int ID_SLIDE_LEFT_BUTTON = 5;
46     public static final int ID_SLIDE_RIGHT_BUTTON = 6;
47     public static final int ID_SLIDE_DOWN_BUTTON = 7;
48     public static final int ID_DROP_DOWN_BUTTON = 8;
49     public static final int ID_SCROLL_LEFT_BUTTON = 9;
50     public static final int ID_SCROLL_RIGHT_BUTTON = 10;
51     
52     public static final int STATE_DEFAULT = 0;
53     public static final int STATE_PRESSED = 1;
54     public static final int STATE_DISABLED = 2;
55     public static final int STATE_ROLLOVER = 3;
56     
57     private int buttonId;
58     private TabDisplayer displayer;
59     private String JavaDoc tabActionCommand;
60             
61     /**
62      * @param displayer Tab displayer where this button is displayed.
63      */

64     TabControlButton( TabDisplayer displayer ) {
65         this( -1, displayer );
66     }
67     
68     /**
69      * @param buttonId Button type (close button, slide button etc)
70      * @param displayer Tab displayer where this button is displayed.
71      */

72     TabControlButton( int buttonId, TabDisplayer displayer ) {
73         this.buttonId = buttonId;
74         this.displayer = displayer;
75         configureButton();
76     }
77     
78     /**
79      * @param e
80      * @return Tab Action id that is posted to the TabDisplayerUI for processing
81      * when the button is pressed.
82      */

83     protected abstract String JavaDoc getTabActionCommand( ActionEvent JavaDoc e );
84     
85     /**
86      * @return Button type identification that is used by the TabDisplayerUI to select the correct
87      * icons for this button.
88      */

89     protected int getButtonId() {
90         return buttonId;
91     }
92
93     public Icon JavaDoc getIcon() {
94         if( null != displayer )
95             return displayer.getUI().getButtonIcon( getButtonId(), STATE_DEFAULT );
96         return null;
97     }
98
99     public Icon JavaDoc getPressedIcon() {
100         if( null != displayer )
101             return displayer.getUI().getButtonIcon( getButtonId(), STATE_PRESSED );
102         return null;
103     }
104
105     public Icon JavaDoc getRolloverIcon() {
106         if( null != displayer )
107             return displayer.getUI().getButtonIcon( getButtonId(), STATE_ROLLOVER );
108         return null;
109     }
110
111     public Icon JavaDoc getRolloverSelectedIcon() {
112         return getRolloverIcon();
113     }
114
115     public Icon JavaDoc getDisabledIcon() {
116         if( null != displayer )
117             return displayer.getUI().getButtonIcon( getButtonId(), STATE_DISABLED );
118         return null;
119     }
120
121     public Icon JavaDoc getDisabledSelectedIcon() {
122         return getDisabledIcon();
123     }
124
125     public void updateUI() {
126         super.updateUI();
127         configureButton();
128     }
129     
130     /**
131      * Make sure that only button icon gets painted (turn off borders etc)
132      */

133     protected void configureButton() {
134         setFocusable( false );
135         setContentAreaFilled( false );
136         setBorderPainted( false );
137         setBorder( BorderFactory.createEmptyBorder() );
138         setRolloverEnabled( getRolloverIcon() != null );
139     }
140
141     protected void fireActionPerformed(ActionEvent JavaDoc event) {
142         super.fireActionPerformed(event);
143         performAction( event );
144         //clear the rollover flag because some operations (maximize/restore) do not send mouseExited event
145
getModel().setRollover( false );
146     }
147     
148     /**
149      * Post an event to the TabDisplayerUI that this button was pressed.
150      */

151     void performAction( ActionEvent JavaDoc e ) {
152         displayer.getUI().postTabAction( createTabActionEvent( e ) );
153     }
154     
155     /**
156      * @return Tab action event that is posted to the TabDisplayerUI when this button is pressed.
157      */

158     protected TabActionEvent createTabActionEvent( ActionEvent JavaDoc e ) {
159         return new TabActionEvent( this, getTabActionCommand( e ), displayer.getSelectionModel().getSelectedIndex() );
160     }
161     
162     protected TabDisplayer getTabDisplayer() {
163         return displayer;
164     }
165 }
166
Popular Tags