001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ClassicTheme.java,v 1.14 2005/12/04 13:46:05 jesper Exp $
023: package net.infonode.tabbedpanel.theme;
024:
025: import net.infonode.gui.GraphicsUtil;
026: import net.infonode.gui.colorprovider.ColorProvider;
027: import net.infonode.gui.colorprovider.UIManagerColorProvider;
028: import net.infonode.tabbedpanel.TabbedPanel;
029: import net.infonode.tabbedpanel.TabbedPanelProperties;
030: import net.infonode.tabbedpanel.TabbedUIDefaults;
031: import net.infonode.tabbedpanel.TabbedUtils;
032: import net.infonode.tabbedpanel.border.OpenContentBorder;
033: import net.infonode.tabbedpanel.internal.TwoColoredLineBorder;
034: import net.infonode.tabbedpanel.titledtab.TitledTabBorderSizePolicy;
035: import net.infonode.tabbedpanel.titledtab.TitledTabProperties;
036: import net.infonode.util.Direction;
037:
038: import javax.swing.border.Border;
039: import javax.swing.border.CompoundBorder;
040: import java.awt.*;
041:
042: /**
043: * A theme with a "classic" look and with round edges for the titled tabs.
044: *
045: * @author $Author: jesper $
046: * @version $Revision: 1.14 $
047: * @since ITP 1.2.0
048: */
049: public class ClassicTheme extends TabbedPanelTitledTabTheme {
050: private TabbedPanelProperties tabbedPanelProperties = new TabbedPanelProperties();
051: private TitledTabProperties titledTabProperties = new TitledTabProperties();
052: private ColorProvider highlightColor = UIManagerColorProvider.TABBED_PANE_HIGHLIGHT;
053: private ColorProvider shadowColor = UIManagerColorProvider.TABBED_PANE_SHADOW;
054: private ColorProvider darkShadow = UIManagerColorProvider.TABBED_PANE_DARK_SHADOW;
055: private int raised;
056:
057: private Border shadowBorder;
058:
059: /**
060: * Constructs a default Classic Theme
061: */
062: public ClassicTheme() {
063: this (2);
064: }
065:
066: /**
067: * Constructs a Classic Theme
068: *
069: * @param raised number of pixels for the highlight raised effect
070: */
071: public ClassicTheme(final int raised) {
072: this .raised = raised;
073:
074: shadowBorder = new Border() {
075: public boolean isBorderOpaque() {
076: return false;
077: }
078:
079: public void paintBorder(Component c, Graphics g, int x,
080: int y, int width, int height) {
081: TabbedPanel tp = TabbedUtils.getParentTabbedPanel(c);
082: if (tp != null) {
083: Direction d = tp.getProperties()
084: .getTabAreaOrientation();
085: g.setColor(shadowColor.getColor());
086:
087: if (d == Direction.UP || d == Direction.DOWN)
088: GraphicsUtil.drawOptimizedLine(g,
089: x + width - 1, y, x + width - 1, y
090: + height - 1);
091: else
092: GraphicsUtil.drawOptimizedLine(g, x, y + height
093: - 1, x + width - 1, y + height - 1);
094: }
095: }
096:
097: public Insets getBorderInsets(Component c) {
098: TabbedPanel tp = TabbedUtils.getParentTabbedPanel(c);
099: if (tp != null) {
100: Direction d = tp.getProperties()
101: .getTabAreaOrientation();
102: return new Insets(
103: 0,
104: 0,
105: (d == Direction.LEFT || d == Direction.RIGHT) ? 1
106: : 0,
107: (d == Direction.UP || d == Direction.DOWN) ? 1
108: : 0);
109: }
110: return new Insets(0, 0, 0, 0);
111: }
112: };
113:
114: Border contentBorder = contentBorder = new CompoundBorder(
115: new OpenContentBorder(highlightColor, darkShadow, null,
116: 1), new OpenContentBorder(null, shadowColor,
117: null, 1));
118: tabbedPanelProperties.getContentPanelProperties()
119: .getComponentProperties().setBorder(contentBorder)
120: .setInsets(new Insets(1, 1, 1, 1));
121:
122: tabbedPanelProperties.setTabSpacing(-raised - 1)
123: .setShadowEnabled(false);
124:
125: tabbedPanelProperties.getTabAreaComponentsProperties()
126: .getComponentProperties().setBorder(
127: doCreateTabBorder(false, false, true, true));
128:
129: Border normalBorder = createInsetsTabBorder(true, true, false);
130: Border highlightBorder = doCreateTabBorder(true, true, true,
131: false);
132: Insets normalInsets = new Insets(0, 3, 0, 3);
133: Insets highlightInsets = new Insets(0,
134: (raised + 1) / 2 + (((raised + 1) & 1) == 1 ? 1 : 0)
135: + normalInsets.left, 1, (raised + 1) / 2
136: + normalInsets.right);
137:
138: titledTabProperties.setHighlightedRaised(raised)
139: .setBorderSizePolicy(
140: TitledTabBorderSizePolicy.INDIVIDUAL_SIZE);
141: titledTabProperties.getNormalProperties()
142: .getComponentProperties().setBorder(normalBorder)
143: .setInsets(normalInsets);
144: titledTabProperties.getHighlightedProperties()
145: .getComponentProperties().setBorder(highlightBorder)
146: .setInsets(highlightInsets);
147: }
148:
149: /**
150: * Gets the name for this theme
151: *
152: * @return the name
153: */
154: public String getName() {
155: return "Classic Theme";
156: }
157:
158: /**
159: * Gets the TabbedPanelProperties for this theme
160: *
161: * @return the TabbedPanelProperties
162: */
163: public TabbedPanelProperties getTabbedPanelProperties() {
164: return tabbedPanelProperties;
165: }
166:
167: /**
168: * Gets the TitledTabProperties for this theme
169: *
170: * @return the TitledTabProperties
171: */
172: public TitledTabProperties getTitledTabProperties() {
173: return titledTabProperties;
174: }
175:
176: /**
177: * Creates a tab border with extra insets border
178: *
179: * @param roundEdges true for round edges
180: * @param open true for open
181: * @param highlight true for highlight
182: * @return the created border
183: */
184: public Border createInsetsTabBorder(boolean roundEdges,
185: boolean open, boolean highlight) {
186: Border insetsBorder = new Border() {
187: public boolean isBorderOpaque() {
188: return false;
189: }
190:
191: public void paintBorder(Component c, Graphics g, int x,
192: int y, int width, int height) {
193: }
194:
195: public Insets getBorderInsets(Component c) {
196: TabbedPanel tp = TabbedUtils.getParentTabbedPanel(c);
197: if (tp != null) {
198: Direction d = tp.getProperties()
199: .getTabAreaOrientation();
200: return new Insets(
201: d == Direction.RIGHT || d == Direction.LEFT ? raised
202: : 0,
203: d == Direction.UP || d == Direction.DOWN ? raised
204: : 0,
205: d == Direction.RIGHT || d == Direction.LEFT ? 1
206: : 0,
207: d == Direction.UP || d == Direction.DOWN ? 1
208: : 0);
209: }
210: return new Insets(0, 0, 0, 0);
211: }
212: };
213: return new CompoundBorder(insetsBorder, doCreateTabBorder(
214: roundEdges, open, highlight, true));
215: }
216:
217: /**
218: * Creates a tab border without extra insets border
219: *
220: * @param roundEdges true for round edges
221: * @param open true for open
222: * @param highlight true for highlight
223: * @return the created border
224: */
225: public Border createTabBorder(boolean roundEdges, boolean open,
226: boolean highlight) {
227: return doCreateTabBorder(roundEdges, open, true, highlight);
228: }
229:
230: private ColorProvider createNormalHighlightColorProvider() {
231: return new ColorProvider() {
232: public Color getColor() {
233: Color highlightBackground = TabbedUIDefaults
234: .getHighlightedStateBackground();
235: highlightBackground = new Color(highlightBackground
236: .getRed() == 0 ? 1 : highlightBackground
237: .getRed(),
238: highlightBackground.getGreen() == 0 ? 1
239: : highlightBackground.getGreen(),
240: highlightBackground.getBlue() == 0 ? 1
241: : highlightBackground.getBlue());
242:
243: Color normalBackgroundColor = TabbedUIDefaults
244: .getNormalStateBackground();
245:
246: Color color = highlightColor.getColor();
247:
248: int r = (int) (normalBackgroundColor.getRed() * ((float) (color
249: .getRed()) / (float) (highlightBackground
250: .getRed())));
251: int g = (int) (normalBackgroundColor.getGreen() * ((float) (color
252: .getGreen()) / (float) (highlightBackground
253: .getGreen())));
254: int b = (int) (normalBackgroundColor.getBlue() * ((float) (color
255: .getBlue()) / (float) (highlightBackground
256: .getBlue())));
257:
258: r = (r + color.getRed()) / 2;
259: g = (g + color.getGreen()) / 2;
260: b = (b + color.getBlue()) / 2;
261:
262: return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g,
263: b > 255 ? 255 : b);
264: }
265:
266: public Color getColor(Component component) {
267: return getColor();
268: }
269: };
270: }
271:
272: private Border doCreateTabBorder(boolean roundEdges, boolean open,
273: boolean highlight, final boolean equalInset) {
274: return new CompoundBorder(new TwoColoredLineBorder(
275: highlight ? highlightColor
276: : createNormalHighlightColorProvider(),
277: darkShadow, roundEdges, open) {
278: protected Insets getShapedBorderInsets(Component c) {
279: return equalInset ? new Insets(1, 1, 1, 1) : super
280: .getShapedBorderInsets(c);
281: }
282: }, shadowBorder);
283: }
284: }
|