001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.swing.tabcontrol;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Insets;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import javax.swing.JComponent;
049: import javax.swing.Timer;
050: import javax.swing.JToggleButton;
051: import javax.swing.SwingConstants;
052: import javax.swing.UIManager;
053: import javax.swing.plaf.ButtonUI;
054:
055: /**
056: * JToggleButton subclass which maps to an index in the data model, and displays
057: * whatever the content of the data model at that index is. Buttons are added or removed
058: * from the tab displayer as the model changes. This class is public to allow
059: * alternate UIs for the buttons to be provided via subclasses of <code>SlidingButtonUI</code>.
060: *
061: * @author Dafe Simonek, Tim Boudreau
062: */
063: public final class SlidingButton extends JToggleButton {
064: /** UI Class ID for IndexButtons, to be used by providers of UI delegates */
065: public static final String UI_CLASS_ID = "SlidingButtonUI";
066:
067: // /**** XXX temporary - should go into LFDefaults table *********/
068: // static {
069: // UIManager.getDefaults().put(UI_CLASS_ID, "org.netbeans.core.windows.view.ui.slides.MetalSlidingButtonUI");
070: // }
071:
072: /** orientation of this button */
073: private int orientation;
074: /** Ascoiated tab data */
075: private TabData data;
076:
077: /** Create a new button representing TabData from the model.
078: *
079: * @param buttonData Tab data as text, icon, tooltip etc.
080: * @param orientation horizontal/vertical orientation of the button
081: */
082: public SlidingButton(TabData buttonData, int orientation) {
083: super (buttonData.getText(), buttonData.getIcon(), false);
084:
085: // grab the text from client property
086: // XXX please rewrite when API is invented - see issue #55955
087: Component comp = buttonData.getComponent();
088: if (comp instanceof JComponent) {
089: Object slidingName = ((JComponent) comp)
090: .getClientProperty("SlidingName");
091: if (slidingName instanceof String) {
092: setText((String) slidingName);
093: }
094: }
095:
096: this .orientation = orientation;
097: data = buttonData;
098: // XXX
099: //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:
158: public void setBlinking(boolean val) {
159: if (!val && blinkTimer != null) {
160: blinkTimer.stop();
161: blinkTimer = null;
162: boolean wasBlinkState = blinkState;
163: blinkState = false;
164: if (wasBlinkState) {
165: repaint();
166: }
167: } else if (val && blinkTimer == null) {
168: blinkTimer = new Timer(700, new BlinkListener());
169: blinkState = true;
170: blinkTimer.start();
171: repaint();
172: }
173: }
174:
175: private class BlinkListener implements ActionListener {
176: public void actionPerformed(ActionEvent ae) {
177: blinkState = !blinkState;
178: repaint();
179: }
180: }
181:
182: /**
183: * Used by the UI to determine whether to use the blink
184: * color or the regular color
185: */
186: public final boolean isBlinkState() {
187: return blinkState;
188: }
189:
190: public final Color getBackground() {
191: return isBlinkState() ? new Color(252, 250, 244) : super
192: .getBackground();
193: }
194:
195: public TabData getData() {
196: return data;
197: }
198:
199: }
|