KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticInternalFrameTitlePane


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Font JavaDoc;
35 import java.awt.FontMetrics JavaDoc;
36 import java.awt.Graphics JavaDoc;
37 import java.awt.Rectangle JavaDoc;
38
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.JInternalFrame JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import javax.swing.plaf.metal.MetalInternalFrameTitlePane JavaDoc;
43
44 /**
45  * Paints the internal frame title. Uses JGoodies Plastic colors.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.2 $
49  */

50 public final class PlasticInternalFrameTitlePane
51     extends MetalInternalFrameTitlePane JavaDoc {
52
53     private PlasticBumps paletteBumps;
54
55     private final PlasticBumps activeBumps =
56         new PlasticBumps(
57             0,
58             0,
59             PlasticLookAndFeel.getPrimaryControlHighlight(),
60             PlasticLookAndFeel.getPrimaryControlDarkShadow(),
61             PlasticLookAndFeel.getPrimaryControl());
62
63     private final PlasticBumps inactiveBumps =
64         new PlasticBumps(
65             0,
66             0,
67             PlasticLookAndFeel.getControlHighlight(),
68             PlasticLookAndFeel.getControlDarkShadow(),
69             PlasticLookAndFeel.getControl());
70
71     public PlasticInternalFrameTitlePane(JInternalFrame JavaDoc frame) {
72         super(frame);
73     }
74
75     public void paintPalette(Graphics JavaDoc g) {
76         boolean leftToRight = PlasticUtils.isLeftToRight(frame);
77
78         int width = getWidth();
79         int height = getHeight();
80
81         if (paletteBumps == null) {
82             paletteBumps =
83                 new PlasticBumps(
84                     0,
85                     0,
86                     PlasticLookAndFeel.getPrimaryControlHighlight(),
87                     PlasticLookAndFeel.getPrimaryControlInfo(),
88                     PlasticLookAndFeel.getPrimaryControlShadow());
89         }
90
91         Color JavaDoc background = PlasticLookAndFeel.getPrimaryControlShadow();
92         Color JavaDoc darkShadow = PlasticLookAndFeel.getControlDarkShadow();
93
94         g.setColor(background);
95         g.fillRect(0, 0, width, height);
96
97         g.setColor(darkShadow);
98         g.drawLine(0, height - 1, width, height - 1);
99
100         int buttonsWidth = getButtonsWidth();
101         int xOffset = leftToRight ? 4 : buttonsWidth + 4;
102         int bumpLength = width - buttonsWidth - 2 * 4;
103         int bumpHeight = getHeight() - 4;
104         paletteBumps.setBumpArea(bumpLength, bumpHeight);
105         paletteBumps.paintIcon(this, g, xOffset, 2);
106     }
107
108     public void paintComponent(Graphics JavaDoc g) {
109         if (isPalette) {
110             paintPalette(g);
111             return;
112         }
113
114         boolean leftToRight = PlasticUtils.isLeftToRight(frame);
115         boolean isSelected = frame.isSelected();
116
117         int width = getWidth();
118         int height = getHeight();
119
120         Color JavaDoc background = null;
121         Color JavaDoc foreground = null;
122         Color JavaDoc shadow = null;
123
124         PlasticBumps bumps;
125
126         if (isSelected) {
127             background = PlasticLookAndFeel.getWindowTitleBackground();
128             foreground = PlasticLookAndFeel.getWindowTitleForeground();
129             bumps = activeBumps;
130         } else {
131             background = PlasticLookAndFeel.getWindowTitleInactiveBackground();
132             foreground = PlasticLookAndFeel.getWindowTitleInactiveForeground();
133             bumps = inactiveBumps;
134         }
135         // JGoodies: darkShadow is always controlDarkShadow
136
shadow = PlasticLookAndFeel.getControlDarkShadow();
137
138         /*
139          
140                 if (isSelected) {
141                     if (selectedBackgroundKey != null) {
142                         background = UIManager.getColor(selectedBackgroundKey);
143                     }
144                     if (background == null) {
145                         background = PlasticLookAndFeel.getWindowTitleBackground();
146                     }
147                     if (selectedForegroundKey != null) {
148                         foreground = UIManager.getColor(selectedForegroundKey);
149                     }
150                     if (selectedShadowKey != null) {
151                         shadow = UIManager.getColor(selectedShadowKey);
152                     }
153                     if (shadow == null) {
154                         shadow = PlasticLookAndFeel.getPrimaryControlDarkShadow();
155                     }
156                     if (foreground == null) {
157                         foreground = PlasticLookAndFeel.getWindowTitleForeground();
158                     }
159                     activeBumps.setBumpColors(activeBumpsHighlight, activeBumpsShadow,
160                                               background);
161                     bumps = activeBumps;
162                 } else {
163                     background = PlasticLookAndFeel.getWindowTitleInactiveBackground();
164                     foreground = PlasticLookAndFeel.getWindowTitleInactiveForeground();
165                     shadow = PlasticLookAndFeel.getControlDarkShadow();
166                     bumps = inactiveBumps;
167                 }
168         */

169         g.setColor(background);
170         g.fillRect(0, 0, width, height);
171
172         g.setColor(shadow);
173         g.drawLine(0, height - 1, width, height - 1);
174         g.drawLine(0, 0, 0, 0);
175         g.drawLine(width - 1, 0, width - 1, 0);
176
177         int titleLength = 0;
178         int xOffset = leftToRight ? 5 : width - 5;
179         String JavaDoc frameTitle = frame.getTitle();
180
181         Icon JavaDoc icon = frame.getFrameIcon();
182         if (icon != null) {
183             if (!leftToRight)
184                 xOffset -= icon.getIconWidth();
185             int iconY = ((height / 2) - (icon.getIconHeight() / 2));
186             icon.paintIcon(frame, g, xOffset, iconY);
187             xOffset += leftToRight ? icon.getIconWidth() + 5 : -5;
188         }
189
190         if (frameTitle != null) {
191             Font JavaDoc f = getFont();
192             g.setFont(f);
193             FontMetrics JavaDoc fm = g.getFontMetrics();
194             //int fHeight = fm.getHeight();
195

196             g.setColor(foreground);
197
198             int yOffset = ((height - fm.getHeight()) / 2) + fm.getAscent();
199
200             Rectangle JavaDoc rect = new Rectangle JavaDoc(0, 0, 0, 0);
201             if (frame.isIconifiable()) {
202                 rect = iconButton.getBounds();
203             } else if (frame.isMaximizable()) {
204                 rect = maxButton.getBounds();
205             } else if (frame.isClosable()) {
206                 rect = closeButton.getBounds();
207             }
208             int titleW;
209
210             if (leftToRight) {
211                 if (rect.x == 0) {
212                     rect.x = frame.getWidth() - frame.getInsets().right - 2;
213                 }
214                 titleW = rect.x - xOffset - 4;
215                 frameTitle = getTitle(frameTitle, fm, titleW);
216             } else {
217                 titleW = xOffset - rect.x - rect.width - 4;
218                 frameTitle = getTitle(frameTitle, fm, titleW);
219                 xOffset -= SwingUtilities.computeStringWidth(fm, frameTitle);
220             }
221
222             titleLength = SwingUtilities.computeStringWidth(fm, frameTitle);
223             g.drawString(frameTitle, xOffset, yOffset);
224             xOffset += leftToRight ? titleLength + 5 : -5;
225         }
226
227         int bumpXOffset;
228         int bumpLength;
229         int buttonsWidth = getButtonsWidth();
230         if (leftToRight) {
231             bumpLength = width - buttonsWidth - xOffset - 5;
232             bumpXOffset = xOffset;
233         } else {
234             bumpLength = xOffset - buttonsWidth - 5;
235             bumpXOffset = buttonsWidth + 5;
236         }
237         int bumpYOffset = 3;
238         int bumpHeight = getHeight() - (2 * bumpYOffset);
239         bumps.setBumpArea(bumpLength, bumpHeight);
240         bumps.paintIcon(this, g, bumpXOffset, bumpYOffset);
241     }
242
243     protected String JavaDoc getTitle(
244         String JavaDoc text,
245         FontMetrics JavaDoc fm,
246         int availTextWidth) {
247         if ((text == null) || (text.equals("")))
248             return "";
249         int textWidth = SwingUtilities.computeStringWidth(fm, text);
250         String JavaDoc clipString = "...";
251         if (textWidth > availTextWidth) {
252             int totalWidth = SwingUtilities.computeStringWidth(fm, clipString);
253             int nChars;
254             for (nChars = 0; nChars < text.length(); nChars++) {
255                 totalWidth += fm.charWidth(text.charAt(nChars));
256                 if (totalWidth > availTextWidth) {
257                     break;
258                 }
259             }
260             text = text.substring(0, nChars) + clipString;
261         }
262         return text;
263     }
264
265     private int getButtonsWidth() {
266         boolean leftToRight = PlasticUtils.isLeftToRight(frame);
267
268         int w = getWidth();
269         int x = leftToRight ? w : 0;
270         int spacing;
271
272         // assumes all buttons have the same dimensions
273
// these dimensions include the borders
274
int buttonWidth = closeButton.getIcon().getIconWidth();
275
276         if (frame.isClosable()) {
277             if (isPalette) {
278                 spacing = 3;
279                 x += leftToRight ? -spacing - (buttonWidth + 2) : spacing;
280                 if (!leftToRight)
281                     x += (buttonWidth + 2);
282             } else {
283                 spacing = 4;
284                 x += leftToRight ? -spacing - buttonWidth : spacing;
285                 if (!leftToRight)
286                     x += buttonWidth;
287             }
288         }
289
290         if (frame.isMaximizable() && !isPalette) {
291             spacing = frame.isClosable() ? 10 : 4;
292             x += leftToRight ? -spacing - buttonWidth : spacing;
293             if (!leftToRight)
294                 x += buttonWidth;
295         }
296
297         if (frame.isIconifiable() && !isPalette) {
298             spacing =
299                 frame.isMaximizable() ? 2 : (frame.isClosable() ? 10 : 4);
300             x += leftToRight ? -spacing - buttonWidth : spacing;
301             if (!leftToRight)
302                 x += buttonWidth;
303         }
304
305         return leftToRight ? w - x : x;
306     }
307
308 }
309
Popular Tags