KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > SlidingButton


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 package org.netbeans.swing.tabcontrol;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.Timer JavaDoc;
28 import javax.swing.JToggleButton JavaDoc;
29 import javax.swing.SwingConstants JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.plaf.ButtonUI JavaDoc;
32
33 /**
34  * JToggleButton subclass which maps to an index in the data model, and displays
35  * whatever the content of the data model at that index is. Buttons are added or removed
36  * from the tab displayer as the model changes. This class is public to allow
37  * alternate UIs for the buttons to be provided via subclasses of <code>SlidingButtonUI</code>.
38  *
39  * @author Dafe Simonek, Tim Boudreau
40  */

41 public final class SlidingButton extends JToggleButton JavaDoc {
42     /** UI Class ID for IndexButtons, to be used by providers of UI delegates */
43     public static final String JavaDoc UI_CLASS_ID = "SlidingButtonUI";
44     
45 // /**** XXX temporary - should go into LFDefaults table *********/
46
// static {
47
// UIManager.getDefaults().put(UI_CLASS_ID, "org.netbeans.core.windows.view.ui.slides.MetalSlidingButtonUI");
48
// }
49

50     /** orientation of this button */
51     private int orientation;
52     /** Ascoiated tab data */
53     private TabData data;
54         
55      
56     /** Create a new button representing TabData from the model.
57      *
58      * @param buttonData Tab data as text, icon, tooltip etc.
59      * @param orientation horizontal/vertical orientation of the button
60      */

61     public SlidingButton(TabData buttonData, int orientation) {
62         super(buttonData.getText(), buttonData.getIcon(), false);
63
64         // grab the text from client property
65
// XXX please rewrite when API is invented - see issue #55955
66
Component JavaDoc comp = buttonData.getComponent();
67         if (comp instanceof JComponent JavaDoc) {
68             Object JavaDoc slidingName = ((JComponent JavaDoc)comp).getClientProperty("SlidingName");
69             if (slidingName instanceof String JavaDoc) {
70                 setText((String JavaDoc)slidingName);
71             }
72         }
73         
74         this.orientation = orientation;
75         data = buttonData;
76         // XXX
77
//setFont (displayer.getFont());
78
setFocusable(false);
79 // setFocusPainted(false);
80
setRolloverEnabled(true);
81         setIconTextGap(3);
82         setVerticalAlignment(SwingConstants.CENTER);
83         setHorizontalAlignment(SwingConstants.CENTER);
84 // setMargin(new Insets(1,1,1,1));
85
setMargin(new Insets JavaDoc(0,3,0,3));
86         setBorderPainted(false);
87 // setHorizontalTextPosition(SwingConstants.);
88
// setVerticalTextPosition(SwingConstants.CENTER);
89
// note, updateUI() is called from superclass constructor
90
}
91
92     public void addNotify() {
93         super.addNotify();
94         //XXX register with tooltip manager
95
}
96
97     public void removeNotify() {
98         super.removeNotify();
99         setBlinking(false);
100         //XXX register with tooltip manager
101
}
102     
103     public String JavaDoc getToolTipText() {
104         return data.getTooltip();
105     }
106     
107     /************** Swing standard technique for attaching UI class *********/
108     
109     public void updateUI () {
110         ButtonUI JavaDoc ui = (ButtonUI JavaDoc)UIManager.getUI(this);
111         if (ui == null) {
112             // create our default UI class if not found in UIManager
113
ui = (ButtonUI JavaDoc)SlidingButtonUI.createUI(this);
114         }
115         setUI (ui);
116     }
117
118     public String JavaDoc getUIClassID() {
119         return UI_CLASS_ID;
120     }
121     
122     /************ Data accessing methods ***********/
123
124     /** Returns orinetation of this button */
125     public int getOrientation() {
126         return orientation;
127     }
128     
129     public boolean isBlinking() {
130         return blinkState;
131     }
132     
133     private Timer JavaDoc blinkTimer = null;
134     private boolean blinkState = false;
135     public void setBlinking (boolean val) {
136         if (!val && blinkTimer != null) {
137             blinkTimer.stop();
138             blinkTimer = null;
139             boolean wasBlinkState = blinkState;
140             blinkState = false;
141             if (wasBlinkState) {
142                 repaint();
143             }
144         } else if (val && blinkTimer == null) {
145             blinkTimer = new Timer JavaDoc(700, new BlinkListener());
146             blinkState = true;
147             blinkTimer.start();
148             repaint();
149         }
150     }
151     
152     private class BlinkListener implements ActionListener JavaDoc {
153         public void actionPerformed (ActionEvent JavaDoc ae) {
154             blinkState = !blinkState;
155             repaint();
156         }
157     }
158     
159     /**
160      * Used by the UI to determine whether to use the blink
161      * color or the regular color
162      */

163     public final boolean isBlinkState() {
164         return blinkState;
165     }
166     
167     public final Color JavaDoc getBackground() {
168         return isBlinkState() ?
169             new Color JavaDoc(252, 250, 244) : super.getBackground();
170     }
171     
172     public TabData getData() {
173         return data;
174     }
175     
176 }
177
Popular Tags