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:
042: package org.netbeans.swing.tabcontrol.plaf;
043:
044: import java.awt.event.ActionEvent;
045: import javax.swing.BorderFactory;
046: import javax.swing.Icon;
047: import javax.swing.JButton;
048: import org.netbeans.swing.tabcontrol.TabDisplayer;
049: import org.netbeans.swing.tabcontrol.event.TabActionEvent;
050:
051: /**
052: * <p>A base class for control buttons placed within the tabs (view tabs) or
053: * next to the tab row (editor tabs). By default the button posts a TabActionEvent
054: * to the TabDisplayerUI when pressed.</p>
055: * <p>The button is painted using a set of icons only unless 'showBorder' is true.
056: * The icons should include 'fake' button border then.</p>
057: *
058: * @since 1.9
059: * @author S. Aubrecht
060: */
061: public abstract class TabControlButton extends JButton {
062:
063: public static final int ID_CLOSE_BUTTON = 1;
064: public static final int ID_PIN_BUTTON = 2;
065: public static final int ID_MAXIMIZE_BUTTON = 3;
066: public static final int ID_RESTORE_BUTTON = 4;
067: public static final int ID_SLIDE_LEFT_BUTTON = 5;
068: public static final int ID_SLIDE_RIGHT_BUTTON = 6;
069: public static final int ID_SLIDE_DOWN_BUTTON = 7;
070: public static final int ID_DROP_DOWN_BUTTON = 8;
071: public static final int ID_SCROLL_LEFT_BUTTON = 9;
072: public static final int ID_SCROLL_RIGHT_BUTTON = 10;
073:
074: public static final int STATE_DEFAULT = 0;
075: public static final int STATE_PRESSED = 1;
076: public static final int STATE_DISABLED = 2;
077: public static final int STATE_ROLLOVER = 3;
078:
079: private int buttonId;
080: private TabDisplayer displayer;
081: private boolean showBorder;
082: private boolean super ConstructorsCompleted = false;
083:
084: /**
085: * @param displayer Tab displayer where this button is displayed.
086: */
087: TabControlButton(TabDisplayer displayer) {
088: this (-1, displayer, false);
089: }
090:
091: /**
092: * @param buttonId Button type (close button, slide button etc)
093: * @param displayer Tab displayer where this button is displayed.
094: */
095: TabControlButton(int buttonId, TabDisplayer displayer) {
096: this (buttonId, displayer, false);
097: }
098:
099: /**
100: * @param buttonId Button type (close button, slide button etc)
101: * @param displayer Tab displayer where this button is displayed.
102: * @param showBorder if false then only icon will be make button overall look,
103: * true means regular button border
104: */
105: TabControlButton(int buttonId, TabDisplayer displayer,
106: boolean showBorder) {
107: super ();
108: this .super ConstructorsCompleted = true;
109: this .buttonId = buttonId;
110: this .displayer = displayer;
111: this .showBorder = showBorder;
112: configureButton();
113: }
114:
115: /**
116: * @param e
117: * @return Tab Action id that is posted to the TabDisplayerUI for processing
118: * when the button is pressed.
119: */
120: protected abstract String getTabActionCommand(ActionEvent e);
121:
122: /**
123: * @return Button type identification that is used by the TabDisplayerUI to select the correct
124: * icons for this button.
125: */
126: protected int getButtonId() {
127: return buttonId;
128: }
129:
130: public Icon getIcon() {
131: if (null != displayer)
132: return displayer.getUI().getButtonIcon(getButtonId(),
133: STATE_DEFAULT);
134: return null;
135: }
136:
137: public Icon getPressedIcon() {
138: if (null != displayer)
139: return displayer.getUI().getButtonIcon(getButtonId(),
140: STATE_PRESSED);
141: return null;
142: }
143:
144: public Icon getRolloverIcon() {
145: if (null != displayer)
146: return displayer.getUI().getButtonIcon(getButtonId(),
147: STATE_ROLLOVER);
148: return null;
149: }
150:
151: public Icon getRolloverSelectedIcon() {
152: return getRolloverIcon();
153: }
154:
155: public Icon getDisabledIcon() {
156: if (null != displayer)
157: return displayer.getUI().getButtonIcon(getButtonId(),
158: STATE_DISABLED);
159: return null;
160: }
161:
162: public Icon getDisabledSelectedIcon() {
163: return getDisabledIcon();
164: }
165:
166: public void updateUI() {
167: super .updateUI();
168: // don't call configureButton() from super constructor
169: if (super ConstructorsCompleted) {
170: configureButton();
171: }
172: }
173:
174: /**
175: * Make sure that only button icon gets painted (turn off borders etc)
176: */
177: protected void configureButton() {
178: setFocusable(false);
179: setRolloverEnabled(getRolloverIcon() != null);
180: if (showBorder) {
181: setContentAreaFilled(true);
182: setBorderPainted(true);
183: } else {
184: setContentAreaFilled(false);
185: setBorderPainted(false);
186: setBorder(BorderFactory.createEmptyBorder());
187: }
188: }
189:
190: protected void fireActionPerformed(ActionEvent event) {
191: super .fireActionPerformed(event);
192: performAction(event);
193: //clear the rollover flag because some operations (maximize/restore) do not send mouseExited event
194: getModel().setRollover(false);
195: }
196:
197: /**
198: * Post an event to the TabDisplayerUI that this button was pressed.
199: */
200: void performAction(ActionEvent e) {
201: displayer.getUI().postTabAction(createTabActionEvent(e));
202: }
203:
204: /**
205: * @return Tab action event that is posted to the TabDisplayerUI when this button is pressed.
206: */
207: protected TabActionEvent createTabActionEvent(ActionEvent e) {
208: return new TabActionEvent(this , getTabActionCommand(e),
209: displayer.getSelectionModel().getSelectedIndex());
210: }
211:
212: protected TabDisplayer getTabDisplayer() {
213: return displayer;
214: }
215: }
|