001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.plastic;
032:
033: import java.awt.Color;
034: import java.awt.Font;
035: import java.awt.FontMetrics;
036: import java.awt.Graphics;
037: import java.awt.Rectangle;
038:
039: import javax.swing.Icon;
040: import javax.swing.JInternalFrame;
041: import javax.swing.SwingUtilities;
042: import javax.swing.plaf.metal.MetalInternalFrameTitlePane;
043:
044: /**
045: * Paints the internal frame title. Uses JGoodies Plastic colors.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.3 $
049: */
050: public final class PlasticInternalFrameTitlePane extends
051: MetalInternalFrameTitlePane {
052:
053: private PlasticBumps paletteBumps;
054:
055: private final PlasticBumps activeBumps = new PlasticBumps(0, 0,
056: PlasticLookAndFeel.getPrimaryControlHighlight(),
057: PlasticLookAndFeel.getPrimaryControlDarkShadow(),
058: PlasticLookAndFeel.getPrimaryControl());
059:
060: private final PlasticBumps inactiveBumps = new PlasticBumps(0, 0,
061: PlasticLookAndFeel.getControlHighlight(),
062: PlasticLookAndFeel.getControlDarkShadow(),
063: PlasticLookAndFeel.getControl());
064:
065: public PlasticInternalFrameTitlePane(JInternalFrame frame) {
066: super (frame);
067: }
068:
069: public void paintPalette(Graphics g) {
070: boolean leftToRight = PlasticUtils.isLeftToRight(frame);
071:
072: int width = getWidth();
073: int height = getHeight();
074:
075: if (paletteBumps == null) {
076: paletteBumps = new PlasticBumps(0, 0, PlasticLookAndFeel
077: .getPrimaryControlHighlight(), PlasticLookAndFeel
078: .getPrimaryControlInfo(), PlasticLookAndFeel
079: .getPrimaryControlShadow());
080: }
081:
082: Color background = PlasticLookAndFeel.getPrimaryControlShadow();
083: Color darkShadow = PlasticLookAndFeel.getControlDarkShadow();
084:
085: g.setColor(background);
086: g.fillRect(0, 0, width, height);
087:
088: g.setColor(darkShadow);
089: g.drawLine(0, height - 1, width, height - 1);
090:
091: int buttonsWidth = getButtonsWidth();
092: int xOffset = leftToRight ? 4 : buttonsWidth + 4;
093: int bumpLength = width - buttonsWidth - 2 * 4;
094: int bumpHeight = getHeight() - 4;
095: paletteBumps.setBumpArea(bumpLength, bumpHeight);
096: paletteBumps.paintIcon(this , g, xOffset, 2);
097: }
098:
099: public void paintComponent(Graphics g) {
100: if (isPalette) {
101: paintPalette(g);
102: return;
103: }
104:
105: boolean leftToRight = PlasticUtils.isLeftToRight(frame);
106: boolean isSelected = frame.isSelected();
107:
108: int width = getWidth();
109: int height = getHeight();
110:
111: Color background = null;
112: Color foreground = null;
113: Color shadow = null;
114:
115: PlasticBumps bumps;
116:
117: if (isSelected) {
118: background = PlasticLookAndFeel.getWindowTitleBackground();
119: foreground = PlasticLookAndFeel.getWindowTitleForeground();
120: bumps = activeBumps;
121: } else {
122: background = PlasticLookAndFeel
123: .getWindowTitleInactiveBackground();
124: foreground = PlasticLookAndFeel
125: .getWindowTitleInactiveForeground();
126: bumps = inactiveBumps;
127: }
128: // JGoodies: darkShadow is always controlDarkShadow
129: shadow = PlasticLookAndFeel.getControlDarkShadow();
130:
131: /*
132:
133: if (isSelected) {
134: if (selectedBackgroundKey != null) {
135: background = UIManager.getColor(selectedBackgroundKey);
136: }
137: if (background == null) {
138: background = PlasticLookAndFeel.getWindowTitleBackground();
139: }
140: if (selectedForegroundKey != null) {
141: foreground = UIManager.getColor(selectedForegroundKey);
142: }
143: if (selectedShadowKey != null) {
144: shadow = UIManager.getColor(selectedShadowKey);
145: }
146: if (shadow == null) {
147: shadow = PlasticLookAndFeel.getPrimaryControlDarkShadow();
148: }
149: if (foreground == null) {
150: foreground = PlasticLookAndFeel.getWindowTitleForeground();
151: }
152: activeBumps.setBumpColors(activeBumpsHighlight, activeBumpsShadow,
153: background);
154: bumps = activeBumps;
155: } else {
156: background = PlasticLookAndFeel.getWindowTitleInactiveBackground();
157: foreground = PlasticLookAndFeel.getWindowTitleInactiveForeground();
158: shadow = PlasticLookAndFeel.getControlDarkShadow();
159: bumps = inactiveBumps;
160: }
161: */
162: g.setColor(background);
163: g.fillRect(0, 0, width, height);
164:
165: g.setColor(shadow);
166: g.drawLine(0, height - 1, width, height - 1);
167: g.drawLine(0, 0, 0, 0);
168: g.drawLine(width - 1, 0, width - 1, 0);
169:
170: int titleLength = 0;
171: int xOffset = leftToRight ? 5 : width - 5;
172: String frameTitle = frame.getTitle();
173:
174: Icon icon = frame.getFrameIcon();
175: if (icon != null) {
176: if (!leftToRight)
177: xOffset -= icon.getIconWidth();
178: int iconY = ((height / 2) - (icon.getIconHeight() / 2));
179: icon.paintIcon(frame, g, xOffset, iconY);
180: xOffset += leftToRight ? icon.getIconWidth() + 5 : -5;
181: }
182:
183: if (frameTitle != null) {
184: Font f = getFont();
185: g.setFont(f);
186: FontMetrics fm = g.getFontMetrics();
187: //int fHeight = fm.getHeight();
188:
189: g.setColor(foreground);
190:
191: int yOffset = ((height - fm.getHeight()) / 2)
192: + fm.getAscent();
193:
194: Rectangle rect = new Rectangle(0, 0, 0, 0);
195: if (frame.isIconifiable()) {
196: rect = iconButton.getBounds();
197: } else if (frame.isMaximizable()) {
198: rect = maxButton.getBounds();
199: } else if (frame.isClosable()) {
200: rect = closeButton.getBounds();
201: }
202: int titleW;
203:
204: if (leftToRight) {
205: if (rect.x == 0) {
206: rect.x = frame.getWidth() - frame.getInsets().right
207: - 2;
208: }
209: titleW = rect.x - xOffset - 4;
210: frameTitle = getTitle(frameTitle, fm, titleW);
211: } else {
212: titleW = xOffset - rect.x - rect.width - 4;
213: frameTitle = getTitle(frameTitle, fm, titleW);
214: xOffset -= SwingUtilities.computeStringWidth(fm,
215: frameTitle);
216: }
217:
218: titleLength = SwingUtilities.computeStringWidth(fm,
219: frameTitle);
220: g.drawString(frameTitle, xOffset, yOffset);
221: xOffset += leftToRight ? titleLength + 5 : -5;
222: }
223:
224: int bumpXOffset;
225: int bumpLength;
226: int buttonsWidth = getButtonsWidth();
227: if (leftToRight) {
228: bumpLength = width - buttonsWidth - xOffset - 5;
229: bumpXOffset = xOffset;
230: } else {
231: bumpLength = xOffset - buttonsWidth - 5;
232: bumpXOffset = buttonsWidth + 5;
233: }
234: int bumpYOffset = 3;
235: int bumpHeight = getHeight() - (2 * bumpYOffset);
236: bumps.setBumpArea(bumpLength, bumpHeight);
237: bumps.paintIcon(this , g, bumpXOffset, bumpYOffset);
238: }
239:
240: protected String getTitle(String text, FontMetrics fm,
241: int availTextWidth) {
242: if ((text == null) || (text.equals("")))
243: return "";
244: int textWidth = SwingUtilities.computeStringWidth(fm, text);
245: String clipString = "...";
246: if (textWidth > availTextWidth) {
247: int totalWidth = SwingUtilities.computeStringWidth(fm,
248: clipString);
249: int nChars;
250: for (nChars = 0; nChars < text.length(); nChars++) {
251: totalWidth += fm.charWidth(text.charAt(nChars));
252: if (totalWidth > availTextWidth) {
253: break;
254: }
255: }
256: text = text.substring(0, nChars) + clipString;
257: }
258: return text;
259: }
260:
261: private int getButtonsWidth() {
262: boolean leftToRight = PlasticUtils.isLeftToRight(frame);
263:
264: int w = getWidth();
265: int x = leftToRight ? w : 0;
266: int spacing;
267:
268: // assumes all buttons have the same dimensions
269: // these dimensions include the borders
270: int buttonWidth = closeButton.getIcon().getIconWidth();
271:
272: if (frame.isClosable()) {
273: if (isPalette) {
274: spacing = 3;
275: x += leftToRight ? -spacing - (buttonWidth + 2)
276: : spacing;
277: if (!leftToRight)
278: x += (buttonWidth + 2);
279: } else {
280: spacing = 4;
281: x += leftToRight ? -spacing - buttonWidth : spacing;
282: if (!leftToRight)
283: x += buttonWidth;
284: }
285: }
286:
287: if (frame.isMaximizable() && !isPalette) {
288: spacing = frame.isClosable() ? 10 : 4;
289: x += leftToRight ? -spacing - buttonWidth : spacing;
290: if (!leftToRight)
291: x += buttonWidth;
292: }
293:
294: if (frame.isIconifiable() && !isPalette) {
295: spacing = frame.isMaximizable() ? 2
296: : (frame.isClosable() ? 10 : 4);
297: x += leftToRight ? -spacing - buttonWidth : spacing;
298: if (!leftToRight)
299: x += buttonWidth;
300: }
301:
302: return leftToRight ? w - x : x;
303: }
304:
305: }
|