~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/swing/tabcontrol/src/org/netbeans/swing/tabcontrol/SlidingButton.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.swing.tabcontrol;
 
42
 
 
43
import java.awt.Color;
 
44
import java.awt.Component;
 
45
import java.awt.Insets;
 
46
import java.awt.event.ActionEvent;
 
47
import java.awt.event.ActionListener;
 
48
import javax.swing.JComponent;
 
49
import javax.swing.Timer;
 
50
import javax.swing.JToggleButton;
 
51
import javax.swing.SwingConstants;
 
52
import javax.swing.UIManager;
 
53
import javax.swing.plaf.ButtonUI;
 
54
 
 
55
/**
 
56
 * JToggleButton subclass which maps to an index in the data model, and displays
 
57
 * whatever the content of the data model at that index is.  Buttons are added or removed
 
58
 * from the tab displayer as the model changes.  This class is public to allow
 
59
 * alternate UIs for the buttons to be provided via subclasses of <code>SlidingButtonUI</code>.
 
60
 *
 
61
 * @author Dafe Simonek, Tim Boudreau
 
62
 */
 
63
public final class SlidingButton extends JToggleButton {
 
64
    /** UI Class ID for IndexButtons, to be used by providers of UI delegates */
 
65
    public static final String UI_CLASS_ID = "SlidingButtonUI";
 
66
    
 
67
//    /**** XXX temporary - should go into LFDefaults table *********/
 
68
//    static {
 
69
//        UIManager.getDefaults().put(UI_CLASS_ID, "org.netbeans.core.windows.view.ui.slides.MetalSlidingButtonUI");
 
70
//    }
 
71
    
 
72
    /** orientation of this button */
 
73
    private int orientation;
 
74
    /** Ascoiated tab data */
 
75
    private TabData data;
 
76
        
 
77
     
 
78
    /** Create a new button representing TabData from the model.
 
79
     *
 
80
     * @param buttonData Tab data as text, icon, tooltip etc.
 
81
     * @param orientation horizontal/vertical orientation of the button
 
82
     */
 
83
    public SlidingButton(TabData buttonData, int orientation) {
 
84
        super(buttonData.getText(), buttonData.getIcon(), false);
 
85
 
 
86
        // grab the text from client property 
 
87
        // XXX please rewrite when API is invented - see issue #55955
 
88
        Component comp = buttonData.getComponent();
 
89
        if (comp instanceof JComponent) {
 
90
            Object slidingName = ((JComponent)comp).getClientProperty("SlidingName");
 
91
            if (slidingName instanceof String) {
 
92
                setText((String)slidingName);
 
93
            }
 
94
        }
 
95
        
 
96
        this.orientation = orientation;
 
97
        data = buttonData;
 
98
        // XXX
 
99
        //setFont (displayer.getFont());
 
100
        setFocusable(false);
 
101
//        setFocusPainted(false);
 
102
        setRolloverEnabled(true);
 
103
        setIconTextGap(3);
 
104
        setVerticalAlignment(SwingConstants.CENTER);
 
105
        setHorizontalAlignment(SwingConstants.CENTER);
 
106
//        setMargin(new Insets(1,1,1,1));
 
107
        setMargin(new Insets(0,3,0,3));
 
108
        setBorderPainted(false);
 
109
//        setHorizontalTextPosition(SwingConstants.);
 
110
//        setVerticalTextPosition(SwingConstants.CENTER);
 
111
        // note, updateUI() is called from superclass constructor
 
112
    }
 
113
 
 
114
    public void addNotify() {
 
115
        super.addNotify();
 
116
        //XXX register with tooltip manager
 
117
    }
 
118
 
 
119
    public void removeNotify() {
 
120
        super.removeNotify();
 
121
        setBlinking(false);
 
122
        //XXX register with tooltip manager
 
123
    }
 
124
    
 
125
    public String getToolTipText() {
 
126
        return data.getTooltip();
 
127
    }
 
128
    
 
129
    /************** Swing standard technique for attaching UI class *********/
 
130
    
 
131
    public void updateUI () {
 
132
        ButtonUI ui = (ButtonUI)UIManager.getUI(this);
 
133
        if (ui == null) {
 
134
            // create our default UI class if not found in UIManager
 
135
            ui = (ButtonUI)SlidingButtonUI.createUI(this);
 
136
        }
 
137
        setUI (ui);
 
138
    }
 
139
 
 
140
    public String getUIClassID() {
 
141
        return UI_CLASS_ID;
 
142
    }
 
143
    
 
144
    /************ Data accessing methods ***********/
 
145
 
 
146
    /** Returns orinetation of this button */
 
147
    public int getOrientation() {
 
148
        return orientation;
 
149
    }
 
150
    
 
151
    public boolean isBlinking() {
 
152
        return blinkState;
 
153
    }
 
154
    
 
155
    private Timer blinkTimer = null;
 
156
    private boolean blinkState = false;
 
157
    public void setBlinking (boolean val) {
 
158
        if (!val && blinkTimer != null) {
 
159
            blinkTimer.stop();
 
160
            blinkTimer = null;
 
161
            boolean wasBlinkState = blinkState;
 
162
            blinkState = false;
 
163
            if (wasBlinkState) {
 
164
                repaint();
 
165
            }
 
166
        } else if (val && blinkTimer == null) {
 
167
            blinkTimer = new Timer(700, new BlinkListener());
 
168
            blinkState = true;
 
169
            blinkTimer.start();
 
170
            repaint();
 
171
        }
 
172
    }
 
173
    
 
174
    private class BlinkListener implements ActionListener {
 
175
        public void actionPerformed (ActionEvent ae) {
 
176
            blinkState = !blinkState;
 
177
            repaint();
 
178
        }
 
179
    }
 
180
    
 
181
    /**
 
182
     * Used by the UI to determine whether to use the blink
 
183
     * color or the regular color
 
184
     */
 
185
    public final boolean isBlinkState() {
 
186
        return blinkState;
 
187
    }
 
188
    
 
189
    public final Color getBackground() {
 
190
        return isBlinkState() ? 
 
191
            new Color(252, 250, 244) : super.getBackground();
 
192
    }
 
193
    
 
194
    public TabData getData() {
 
195
        return data;
 
196
    }
 
197
    
 
198
}