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: * MetalEditorTabDisplayerUI.java
043: *
044: * Created on December 2, 2003, 9:40 PM
045: */
046:
047: package org.netbeans.swing.tabcontrol.plaf;
048:
049: import java.awt.Component;
050: import java.awt.Container;
051: import java.awt.Dimension;
052: import java.awt.FontMetrics;
053: import java.awt.Graphics;
054: import java.awt.Insets;
055: import java.awt.Rectangle;
056: import java.util.HashMap;
057: import java.util.Map;
058: import javax.swing.Icon;
059: import javax.swing.JComponent;
060: import javax.swing.UIManager;
061: import org.netbeans.swing.tabcontrol.TabDisplayer;
062: import javax.swing.plaf.ComponentUI;
063:
064: /**
065: * Tab displayer UI for Metal look and feel
066: *
067: * @author Tim Boudreau
068: */
069: public final class MetalEditorTabDisplayerUI extends
070: BasicScrollingTabDisplayerUI {
071:
072: private Rectangle scratch = new Rectangle();
073: private static Map<Integer, String[]> buttonIconPaths;
074:
075: /**
076: * Creates a new instance of MetalEditorTabDisplayerUI
077: */
078: public MetalEditorTabDisplayerUI(TabDisplayer displayer) {
079: super (displayer);
080: }
081:
082: protected TabCellRenderer createDefaultRenderer() {
083: return new MetalEditorTabCellRenderer();
084: }
085:
086: public static ComponentUI createUI(JComponent c) {
087: return new MetalEditorTabDisplayerUI((TabDisplayer) c);
088: }
089:
090: public Dimension getMinimumSize(JComponent c) {
091: return new Dimension(80, 28);
092: }
093:
094: public Dimension getPreferredSize(JComponent c) {
095: int prefHeight = 28;
096: Graphics g = BasicScrollingTabDisplayerUI
097: .getOffscreenGraphics();
098: if (g != null) {
099: FontMetrics fm = g.getFontMetrics(displayer.getFont());
100: Insets ins = getTabAreaInsets();
101: prefHeight = fm.getHeight() + ins.top + ins.bottom + 9;
102: }
103: return new Dimension(displayer.getWidth(), prefHeight);
104: }
105:
106: protected int createRepaintPolicy() {
107: return TabState.REPAINT_ALL_TABS_ON_ACTIVATION_CHANGE
108: | TabState.REPAINT_ALL_TABS_ON_SELECTION_CHANGE
109: | TabState.REPAINT_ON_MOUSE_PRESSED
110: | TabState.REPAINT_ON_CLOSE_BUTTON_PRESSED
111: | TabState.REPAINT_ON_MOUSE_ENTER_CLOSE_BUTTON;
112: }
113:
114: public Insets getTabAreaInsets() {
115: Insets results = super .getTabAreaInsets();
116: results.bottom += 4;
117: results.right += 3;
118: return results;
119: }
120:
121: public void install() {
122: super .install();
123: displayer.setBackground(UIManager.getColor("control")); //NOI18N
124: }
125:
126: protected void paintAfterTabs(Graphics g) {
127: Rectangle r = new Rectangle();
128: getTabsVisibleArea(r);
129: r.width = displayer.getWidth();
130: g.setColor(displayer.isActive() ? defaultRenderer
131: .getSelectedActivatedBackground() : defaultRenderer
132: .getSelectedBackground());
133:
134: Insets ins = getTabAreaInsets();
135: g.fillRect(r.x, r.y + r.height, r.x + r.width, displayer
136: .getHeight()
137: - (r.y + r.height));
138:
139: g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
140:
141: int selEnd = 0;
142: int i = selectionModel.getSelectedIndex();
143: if (i != -1) {
144: getTabRect(i, scratch);
145: if (scratch.width != 0) {
146: if (r.x < scratch.x) {
147: g.drawLine(r.x, displayer.getHeight() - ins.bottom,
148: scratch.x - 1, displayer.getHeight()
149: - ins.bottom);
150: }
151: if (scratch.x + scratch.width < r.x + r.width) {
152: selEnd = scratch.x + scratch.width;
153: g.drawLine(selEnd, displayer.getHeight()
154: - ins.bottom, r.x + r.width, displayer
155: .getHeight()
156: - ins.bottom);
157: }
158: } else {
159: //The selected tab is scrolled offscreen
160: g.drawLine(0, displayer.getHeight() - ins.bottom,
161: displayer.getWidth(), displayer.getHeight()
162: - ins.bottom);
163: }
164: g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
165: g.drawLine(selEnd, displayer.getHeight() - 5, displayer
166: .getWidth(), displayer.getHeight() - 5);
167: return;
168: }
169:
170: g.drawLine(r.x, displayer.getHeight() - ins.bottom, r.x
171: + r.width, displayer.getHeight() - ins.bottom);
172:
173: g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
174: g.drawLine(0, displayer.getHeight() - 5, displayer.getWidth(),
175: displayer.getHeight() - 5);
176: }
177:
178: private static void initIcons() {
179: if (null == buttonIconPaths) {
180: buttonIconPaths = new HashMap<Integer, String[]>(7);
181:
182: //left button
183: String[] iconPaths = new String[4];
184: iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_scrollleft_enabled.png"; // NOI18N
185: iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/metal_scrollleft_disabled.png"; // NOI18N
186: iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_scrollleft_rollover.png"; // NOI18N
187: iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_scrollleft_pressed.png"; // NOI18N
188: buttonIconPaths.put(TabControlButton.ID_SCROLL_LEFT_BUTTON,
189: iconPaths);
190:
191: //right button
192: iconPaths = new String[4];
193: iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_scrollright_enabled.png"; // NOI18N
194: iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/metal_scrollright_disabled.png"; // NOI18N
195: iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_scrollright_rollover.png"; // NOI18N
196: iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_scrollright_pressed.png"; // NOI18N
197: buttonIconPaths.put(
198: TabControlButton.ID_SCROLL_RIGHT_BUTTON, iconPaths);
199:
200: //drop down button
201: iconPaths = new String[4];
202: iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_popup_enabled.png"; // NOI18N
203: iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/metal_popup_disabled.png"; // NOI18N
204: iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_popup_rollover.png"; // NOI18N
205: iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_popup_pressed.png"; // NOI18N
206: buttonIconPaths.put(TabControlButton.ID_DROP_DOWN_BUTTON,
207: iconPaths);
208:
209: //maximize/restore button
210: iconPaths = new String[4];
211: iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_maximize_enabled.png"; // NOI18N
212: iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/metal_maximize_disabled.png"; // NOI18N
213: iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_maximize_rollover.png"; // NOI18N
214: iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_maximize_pressed.png"; // NOI18N
215: buttonIconPaths.put(TabControlButton.ID_MAXIMIZE_BUTTON,
216: iconPaths);
217:
218: iconPaths = new String[4];
219: iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_restore_enabled.png"; // NOI18N
220: iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/metal_restore_disabled.png"; // NOI18N
221: iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_restore_rollover.png"; // NOI18N
222: iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_restore_pressed.png"; // NOI18N
223: buttonIconPaths.put(TabControlButton.ID_RESTORE_BUTTON,
224: iconPaths);
225: }
226: }
227:
228: public Icon getButtonIcon(int buttonId, int buttonState) {
229: Icon res = null;
230: initIcons();
231: String[] paths = buttonIconPaths.get(buttonId);
232: if (null != paths && buttonState >= 0
233: && buttonState < paths.length) {
234: res = TabControlButtonFactory.getIcon(paths[buttonState]);
235: }
236: return res;
237: }
238:
239: protected Rectangle getControlButtonsRectangle(Container parent) {
240: Component c = getControlButtons();
241: return new Rectangle(parent.getWidth() - c.getWidth() - 3, 3, c
242: .getWidth(), c.getHeight());
243: }
244: }
|