001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.plaf.metal;
021:
022: import java.awt.Color;
023: import java.awt.Graphics;
024: import java.awt.LayoutManager;
025: import java.awt.Rectangle;
026: import javax.swing.JComponent;
027: import javax.swing.JTabbedPane;
028: import javax.swing.UIManager;
029: import javax.swing.plaf.ComponentUI;
030: import javax.swing.plaf.basic.BasicTabbedPaneUI;
031:
032: import org.apache.harmony.x.swing.Utilities;
033:
034: public class MetalTabbedPaneUI extends BasicTabbedPaneUI {
035:
036: public class TabbedPaneLayout extends
037: BasicTabbedPaneUI.TabbedPaneLayout {
038: protected void normalizeTabRuns(final int tabPlacement,
039: final int tabCount, final int start, final int max) {
040: super .normalizeTabRuns(tabPlacement, tabCount, start, max);
041: }
042:
043: protected void padSelectedTab(final int tabPlacement,
044: final int selectedIndex) {
045: // overridden to do nothing
046: }
047:
048: protected void rotateTabRuns(final int tabPlacement,
049: final int selectedRun) {
050: // overridden to do nothing
051: }
052: }
053:
054: public static ComponentUI createUI(final JComponent c) {
055: return new MetalTabbedPaneUI();
056: }
057:
058: protected int minTabWidth = 40;
059: protected Color selectColor;
060: protected Color selectHighlight;
061: protected Color tabAreaBackground;
062:
063: protected int calculateMaxTabHeight(final int tabPlacement) {
064: return super .calculateMaxTabHeight(tabPlacement);
065: }
066:
067: protected LayoutManager createLayoutManager() {
068: if (tabPane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
069: return new TabbedPaneLayout();
070: } else {
071: return super .createLayoutManager();
072: }
073: }
074:
075: protected Color getColorForGap(final int currentRun, final int x,
076: final int y) {
077: int adjacentTabIndex = getAdjacentTab(x, y);
078:
079: if (adjacentTabIndex == -1) {
080: return tabPane.getBackground();
081: }
082:
083: return tabPane.getSelectedIndex() == adjacentTabIndex ? selectColor
084: : tabPane.getBackgroundAt(adjacentTabIndex);
085: }
086:
087: protected int getTabLabelShiftX(final int tabPlacement,
088: final int tabIndex, final boolean isSelected) {
089: return 0;
090: }
091:
092: protected int getTabLabelShiftY(final int tabPlacement,
093: final int tabIndex, final boolean isSelected) {
094: return 0;
095: }
096:
097: protected int getTabRunOverlay(final int tabPlacement) {
098: if (tabPlacement == TOP || tabPlacement == BOTTOM) {
099: return 2;
100: }
101:
102: return 6;
103: }
104:
105: protected void installDefaults() {
106: super .installDefaults();
107:
108: selectColor = UIManager.getColor("TabbedPane.selected");
109: selectHighlight = UIManager
110: .getColor("TabbedPane.selectHighlight");
111: tabAreaBackground = UIManager
112: .getColor("TabbedPane.tabAreaBackground");
113: }
114:
115: public void paint(final Graphics g, final JComponent c) {
116: super .paint(g, c);
117: }
118:
119: protected void paintContentBorderBottomEdge(final Graphics g,
120: final int tabPlacement, final int selectedIndex,
121: final int x, final int y, final int w, final int h) {
122: g.setColor(darkShadow);
123: g.drawRect(x, y + h - 1, w - 1, 0);
124: g.setColor(selectColor);
125: g.fillRect(x + 1, y + h - 3, w - 2, 2);
126: if (tabPlacement == BOTTOM
127: && isTabAdjacentToContentAreaBorder(selectedIndex)) {
128: getTabBounds(selectedIndex, calcRect);
129: g
130: .drawRect(calcRect.x + 1, y + h - 1,
131: calcRect.width - 2, 0);
132: }
133: }
134:
135: protected void paintContentBorderLeftEdge(final Graphics g,
136: final int tabPlacement, final int selectedIndex,
137: final int x, final int y, final int w, final int h) {
138: g.setColor(selectHighlight);
139: g.drawRect(x, y, 0, h - 1);
140: g.setColor(selectColor);
141: g.drawRect(x + 1, y + 1, 0, h - 2);
142: if (tabPlacement == LEFT
143: && isTabAdjacentToContentAreaBorder(selectedIndex)) {
144: getTabBounds(selectedIndex, calcRect);
145: g.drawRect(x, calcRect.y + 2, 0, calcRect.height - 3);
146: }
147: }
148:
149: protected void paintContentBorderRightEdge(final Graphics g,
150: final int tabPlacement, final int selectedIndex,
151: final int x, final int y, final int w, final int h) {
152: g.setColor(darkShadow);
153: g.drawRect(x + w - 1, y, 0, h - 1);
154: g.setColor(selectColor);
155: g.fillRect(x + w - 3, y + 1, 2, h - 2);
156: if (tabPlacement == RIGHT
157: && isTabAdjacentToContentAreaBorder(selectedIndex)) {
158: getTabBounds(selectedIndex, calcRect);
159: g.drawRect(x + w - 1, calcRect.y + 1, 0,
160: calcRect.height - 2);
161: }
162: }
163:
164: protected void paintContentBorderTopEdge(final Graphics g,
165: final int tabPlacement, final int selectedIndex,
166: final int x, final int y, final int w, final int h) {
167: g.setColor(selectHighlight);
168: g.drawRect(x, y, w - 1, 0);
169: g.setColor(selectColor);
170: g.drawRect(x + 1, y + 1, w - 2, 0);
171: if (tabPlacement == TOP
172: && isTabAdjacentToContentAreaBorder(selectedIndex)) {
173: getTabBounds(selectedIndex, calcRect);
174: g.drawRect(calcRect.x + 2, y, calcRect.width - 3, 0);
175: }
176: }
177:
178: protected void paintFocusIndicator(final Graphics g,
179: final int tabPlacement, final Rectangle[] rects,
180: final int tabIndex, final Rectangle iconRect,
181: final Rectangle textRect, final boolean isSelected) {
182: if (!isFocusPainted(tabIndex)) {
183: return;
184: }
185:
186: calcRect.setBounds(rects[tabIndex]);
187: calcRect.grow(-3, -3);
188:
189: g.setColor(focus);
190: g.drawRoundRect(calcRect.x, calcRect.y, calcRect.width,
191: calcRect.height, 5, 5);
192: }
193:
194: protected void paintHighlightBelowTab() {
195: // this method seems to be unused
196: }
197:
198: protected void paintBottomTabBorder(final int tabIndex,
199: final Graphics g, final int x, final int y, final int w,
200: final int h, final int btm, final int rght,
201: final boolean isSelected) {
202: int[] xs = { x, x, x + 1, x + 3, x + 6, rght - 5, rght - 2,
203: rght, rght + 1, rght + 1 };
204: int[] ys = { y - 2, btm - 5, btm - 2, btm, btm + 1, btm + 1,
205: btm, btm - 2, btm - 5, y - 3 };
206:
207: g.setColor(darkShadow);
208: g.drawPolyline(xs, ys, xs.length);
209:
210: int[] highlightXs = { x + 1, x + 1, x + 3 };
211: int[] highlightYs = { y - 2, btm - 4, btm - 1 };
212: g.setColor(isSelected ? selectHighlight : highlight);
213: g.drawPolyline(highlightXs, highlightYs, highlightXs.length);
214: }
215:
216: protected void paintLeftTabBorder(final int tabIndex,
217: final Graphics g, final int x, final int y, final int w,
218: final int h, final int btm, final int rght,
219: final boolean isSelected) {
220: int[] xs = { rght, x + 6, x + 3, x + 1, x, x, x + 1, x + 3,
221: x + 6, rght + 1 };
222: int[] ys = { btm + 1, btm + 1, btm, btm - 2, btm - 5, y + 6,
223: y + 3, y + 1, y, y };
224:
225: g.setColor(darkShadow);
226: g.drawPolyline(xs, ys, xs.length);
227:
228: int[] highlightXs = { x + 1, x + 1, x + 3, x + 6, rght + 1 };
229: int[] highlightYs = { btm - 4, y + 5, y + 2, y + 1, y + 1 };
230: g.setColor(isSelected ? selectHighlight : highlight);
231: g.drawPolyline(highlightXs, highlightYs, highlightXs.length);
232: }
233:
234: protected void paintRightTabBorder(final int tabIndex,
235: final Graphics g, final int x, final int y, final int w,
236: final int h, final int btm, final int rght,
237: final boolean isSelected) {
238: int[] xs = { x - 1, rght - 5, rght - 2, rght, rght + 1,
239: rght + 1, rght, rght - 2, rght - 5, x - 1 };
240: int[] ys = { btm + 1, btm + 1, btm, btm - 2, btm - 5, y + 6,
241: y + 3, y + 1, y, y };
242:
243: g.setColor(darkShadow);
244: g.drawPolyline(xs, ys, xs.length);
245:
246: int[] highlightXs = { x, rght - 4, rght - 1 };
247: int[] highlightYs = { y + 1, y + 1, y + 3 };
248: g.setColor(isSelected ? selectHighlight : highlight);
249: g.drawPolyline(highlightXs, highlightYs, highlightXs.length);
250: }
251:
252: protected void paintTopTabBorder(final int tabIndex,
253: final Graphics g, final int x, final int y, final int w,
254: final int h, final int btm, final int rght,
255: final boolean isSelected) {
256: int[] xs = { x, x, x + 1, x + 3, x + 6, rght - 5, rght - 2,
257: rght, rght + 1, rght + 1 };
258: int[] ys = { btm + 3, y + 6, y + 3, y + 1, y, y, y + 1, y + 3,
259: y + 6, btm + 4 };
260: g.setColor(darkShadow);
261: g.drawPolyline(xs, ys, xs.length);
262:
263: int[] highlightXs = { x + 1, x + 1, x + 3, x + 6, rght - 4,
264: rght - 1 };
265: int[] highlightYs = { btm + 3, y + 5, y + 2, y + 1, y + 1,
266: y + 3 };
267: g.setColor(isSelected ? selectHighlight : highlight);
268: g.drawPolyline(highlightXs, highlightYs, highlightXs.length);
269: }
270:
271: protected void paintTabBackground(final Graphics g,
272: final int tabPlacement, final int tabIndex, final int x,
273: final int y, final int w, final int h,
274: final boolean isSelected) {
275: Color background = isSelected ? selectColor : tabPane
276: .getBackgroundAt(tabIndex);
277: g.setColor(background);
278:
279: g.fillRoundRect(x, y, w + 1, h + 1, 12, 12);
280:
281: if (tabPlacement == TOP) {
282: g.fillRect(x + 1, y + h - 2, w - 1, 5);
283: } else if (tabPlacement == LEFT) {
284: g.fillRect(x + w - 2, y + 1, 4, h - 1);
285: } else if (tabPlacement == BOTTOM) {
286: g.fillRect(x + 1, y - 2, w - 1, 5);
287: } else if (tabPlacement == RIGHT) {
288: g.fillRect(x, y + 1, 4, h - 1);
289: }
290: }
291:
292: protected void paintTabBorder(final Graphics g,
293: final int tabPlacement, final int tabIndex, final int x,
294: final int y, final int w, final int h,
295: final boolean isSelected) {
296: int btm = y + h - 1;
297: int rght = x + w - 1;
298: if (tabPlacement == TOP) {
299: paintTopTabBorder(tabIndex, g, x, y, w, h, btm, rght,
300: isSelected);
301: } else if (tabPlacement == LEFT) {
302: paintLeftTabBorder(tabIndex, g, x, y, w, h, btm, rght,
303: isSelected);
304: } else if (tabPlacement == RIGHT) {
305: paintRightTabBorder(tabIndex, g, x, y, w, h, btm, rght,
306: isSelected);
307: } else if (tabPlacement == BOTTOM) {
308: paintBottomTabBorder(tabIndex, g, x, y, w, h, btm, rght,
309: isSelected);
310: }
311: }
312:
313: protected boolean shouldFillGap(final int currentRun,
314: final int tabIndex, final int x, final int y) {
315: return getAdjacentTab(x, y) != -1;
316: }
317:
318: protected boolean shouldPadTabRun(final int tabPlacement,
319: final int run) {
320: if (runCount <= 1) {
321: return false;
322: }
323:
324: return run != runCount - 1;
325: }
326:
327: protected boolean shouldRotateTabRuns(final int tabPlacement,
328: final int selectedRun) {
329: return false;
330: }
331:
332: public void update(final Graphics g, final JComponent c) {
333: super .update(g, c);
334: }
335:
336: private boolean isTabAdjacentToContentAreaBorder(
337: final int selectedIndex) {
338: return getRunForTab(tabPane.getTabCount(), selectedIndex) == 0;
339: }
340:
341: private int getAdjacentTab(final int x, final int y) {
342: int newX = x;
343: int newY = y;
344: int tabPlacement = tabPane.getTabPlacement();
345: switch (tabPlacement) {
346: case TOP:
347: case LEFT:
348: case RIGHT:
349: newY--;
350: break;
351: case BOTTOM:
352: newY++;
353: break;
354: }
355:
356: return tabForCoordinate(tabPane, newX, newY);
357: }
358:
359: private boolean isFocusPainted(final int tabIndex) {
360: if (tabIndex != getFocusIndex()) {
361: return false;
362: }
363:
364: return tabPane.getIconAt(tabIndex) != null
365: || !Utilities.isEmptyString(tabPane
366: .getTitleAt(tabIndex));
367: }
368: }
|