001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.components.favoritespanel;
015:
016: import com.jgoodies.forms.layout.CellConstraints;
017: import com.jgoodies.forms.layout.FormLayout;
018: import org.jetbrains.annotations.NonNls;
019: import org.jetbrains.annotations.NotNull;
020: import org.jetbrains.annotations.Nullable;
021: import org.pentaho.reportdesigner.lib.client.components.Category;
022:
023: import javax.swing.*;
024: import java.awt.*;
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.lang.reflect.InvocationTargetException;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: /**
032: * User: Martin
033: * Date: 11.03.2005
034: * Time: 16:31:21
035: */
036: public class DefaultHeaderComponent extends AbstractHeaderComponent
037: implements PropertyChangeListener {
038: @NotNull
039: @NonNls
040: private static final Logger LOG = Logger
041: .getLogger(DefaultHeaderComponent.class.getName());
042:
043: @NotNull
044: public static final Color DEFAULT_BACKGROUND_COLOR = Color.LIGHT_GRAY;
045: @NotNull
046: public static final Color DEFAULT_FOREGROUND_COLOR = Color.BLACK;
047:
048: private boolean focused;
049:
050: @NotNull
051: private JPanel containerPanel;
052:
053: @NotNull
054: private JLabel headerLabel;
055: @NotNull
056: private GradientPanel actionComponent;
057: @NotNull
058: private Category<?> category;
059:
060: @NotNull
061: private GradientPanel gradientPanel;
062:
063: @NotNull
064: private Color origForegroundColor;
065:
066: @Nullable
067: private AnimationThread animationThread;
068: private double darkeningFactor;
069:
070: public DefaultHeaderComponent() {
071: this (new Insets(1, 17, 1, 17), 0.7);
072: }
073:
074: public DefaultHeaderComponent(@NotNull
075: Insets insets, double darkeningFactor) {
076: this .darkeningFactor = darkeningFactor;
077: setLayout(new BorderLayout());
078:
079: containerPanel = new JPanel();
080:
081: focused = false;
082:
083: @NonNls
084: FormLayout formLayout = new FormLayout(
085: "0dlu, fill:10dlu:grow, 0dlu, pref, 0dlu",
086: "0dlu, pref, " + "0dlu");
087: @NonNls
088: CellConstraints cc = new CellConstraints();
089:
090: containerPanel.setLayout(formLayout);
091:
092: headerLabel = new JLabel(" ");
093:
094: headerLabel.setBorder(BorderFactory.createEmptyBorder(
095: insets.top, insets.left, insets.bottom, insets.right));
096:
097: actionComponent = new GradientPanel();
098: actionComponent
099: .setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 0));
100: actionComponent
101: .setDirection(GradientPanel.Direction.DIRECTION_LEFT);
102: Color acbg = actionComponent.getBackground();
103: if (acbg == null) {
104: acbg = DEFAULT_BACKGROUND_COLOR;
105: }
106: actionComponent.setGradientColors(new Color[] { acbg, acbg });
107:
108: gradientPanel = new GradientPanel();
109: gradientPanel.setGradientColors(new Color[] {
110: getDarkerColor(getBackground()), getBackground() });
111: gradientPanel
112: .setDirection(GradientPanel.Direction.DIRECTION_LEFT);
113:
114: gradientPanel.add(headerLabel, BorderLayout.CENTER);
115:
116: containerPanel.add(gradientPanel, cc.xy(2, 2, "fill, fill"));
117: containerPanel.add(actionComponent, cc.xy(4, 2));
118:
119: add(containerPanel, BorderLayout.CENTER);
120:
121: setOpaque(false);
122: Color background = containerPanel.getBackground();
123: if (background == null) {
124: background = DEFAULT_BACKGROUND_COLOR;
125: }
126: setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,
127: getDarkerColor(background)));
128:
129: origForegroundColor = headerLabel.getForeground();
130: }
131:
132: @NotNull
133: private Color getDarkerColor(@NotNull
134: Color color) {
135: return new Color(Math.max(
136: (int) (color.getRed() * darkeningFactor), 0), Math.max(
137: (int) (color.getGreen() * darkeningFactor), 0), Math
138: .max((int) (color.getBlue() * darkeningFactor), 0));
139: }
140:
141: public void setFocused(boolean focused) {
142: if (this .focused == focused) {
143: return;
144: }
145:
146: this .focused = focused;
147:
148: if (!focused) {
149: Color background = getBackground();
150: if (background == null) {
151: background = DEFAULT_BACKGROUND_COLOR;
152: }
153:
154: Color hihglight = UIManager
155: .getColor("List.selectionBackground");//NON-NLS
156: if (hihglight == null) {
157: hihglight = Color.BLUE;
158: }
159:
160: Color selectForegroundColor = UIManager
161: .getColor("List.selectionForeground");//NON-NLS
162: if (selectForegroundColor == null) {
163: selectForegroundColor = Color.BLACK;
164: }
165:
166: int startStep = 0;
167: AnimationThread at = animationThread;
168: if (at != null && !at.isInterrupted() && at.isAlive()) {
169: at.interrupt();
170: startStep = AnimationThread.STEPS - at.getCurrentStep();
171: }
172: at = new AnimationThread(startStep, this , hihglight,
173: getDarkerColor(background), selectForegroundColor,
174: origForegroundColor);
175: at.setDaemon(true);
176: at.start();
177: animationThread = at;
178: } else {
179: Color background = getBackground();
180: if (background == null) {
181: background = DEFAULT_BACKGROUND_COLOR;
182: }
183:
184: Color hihglight = UIManager
185: .getColor("List.selectionBackground");//NON-NLS
186:
187: if (hihglight == null) {
188: hihglight = Color.BLUE;
189: }
190:
191: Color selectForegroundColor = UIManager
192: .getColor("List.selectionForeground");//NON-NLS
193: if (selectForegroundColor == null) {
194: selectForegroundColor = Color.BLACK;
195: }
196:
197: int startStep = 0;
198: AnimationThread at = animationThread;
199: if (at != null && !at.isInterrupted() && at.isAlive()) {
200: at.interrupt();
201: startStep = AnimationThread.STEPS - at.getCurrentStep();
202: }
203: at = new AnimationThread(startStep, this ,
204: getDarkerColor(background), hihglight,
205: origForegroundColor, selectForegroundColor);
206: at.setDaemon(true);
207: at.start();
208: animationThread = at;
209: }
210: }
211:
212: @NotNull
213: public GradientPanel getGradientPanel() {
214: return gradientPanel;
215: }
216:
217: public void setCategory(@NotNull
218: Category<?> category) {
219: //noinspection ConstantConditions
220: if (this .category != null) {
221: this .category.removePropertyChangeListener(this );
222: }
223:
224: this .category = category;
225: update(category);
226: }
227:
228: private void update(@NotNull
229: Category<?> category) {
230: headerLabel.setIcon(category.getIconBig());
231: headerLabel.setText(category.getTitle());
232:
233: actionComponent.removeAll();
234: JToolBar toolBar = category.getToolBar();
235: if (toolBar != null) {
236: toolBar.setBackground(actionComponent.getBackground());
237: actionComponent.add(toolBar);
238: } else {
239: for (int i = 0; i < category.getActionComponents().length; i++) {
240: JComponent component = category.getActionComponents()[i];
241: actionComponent.add(component);
242: }
243: }
244:
245: revalidate();
246: repaint();
247: }
248:
249: public void propertyChange(@NotNull
250: PropertyChangeEvent evt) {
251: update(category);
252: }
253:
254: private void setCurrentColor(@NotNull
255: Color backgroundColor, @NotNull
256: Color textColor) {
257: Color background = getBackground();
258: if (background == null) {
259: background = DEFAULT_BACKGROUND_COLOR;
260: }
261:
262: Color[] gradientColors = new Color[] { backgroundColor,
263: background };
264: getGradientPanel().setGradientColors(gradientColors);
265: headerLabel.setForeground(textColor);
266:
267: revalidate();
268: repaint();
269: }
270:
271: private static class AnimationThread extends Thread {
272: public static final int STEPS = 5;
273:
274: @NotNull
275: private DefaultHeaderComponent defaultHeaderComponent;
276: @NotNull
277: private Color startColor;
278: @NotNull
279: private Color endColor;
280:
281: @NotNull
282: private Color startColor2;
283: @NotNull
284: private Color endColor2;
285:
286: private int step;
287:
288: private AnimationThread(int startStep, @NotNull
289: DefaultHeaderComponent defaultHeaderComponent, @NotNull
290: Color startColor, @NotNull
291: Color endColor, @NotNull
292: Color startColor2, @NotNull
293: Color endColor2) {
294: this .defaultHeaderComponent = defaultHeaderComponent;
295:
296: this .startColor = startColor;
297: this .endColor = endColor;
298:
299: this .startColor2 = startColor2;
300: this .endColor2 = endColor2;
301:
302: step = startStep;
303: }
304:
305: public void run() {
306: try {
307: Thread.sleep(50);
308: } catch (InterruptedException e) {
309: if (LOG.isLoggable(Level.FINE))
310: LOG
311: .log(
312: Level.FINE,
313: "DefaultHeaderComponent$AnimationThread.run ",
314: e);
315: return;
316: }
317:
318: double v = (double) STEPS;
319: for (; step < STEPS && !isInterrupted(); step++) {
320: try {
321: Thread.sleep(20);
322:
323: final Color currentColor = getIntermediateColor(v,
324: step, startColor, endColor);
325: final Color textColor = getIntermediateColor(v,
326: step, startColor2, endColor2);
327:
328: EventQueue.invokeAndWait(new Runnable() {
329: public void run() {
330: defaultHeaderComponent.setCurrentColor(
331: currentColor, textColor);
332: }
333: });
334: } catch (InterruptedException e) {
335: if (LOG.isLoggable(Level.FINE))
336: LOG
337: .log(
338: Level.FINE,
339: "DefaultHeaderComponent$AnimationThread.run ",
340: e);
341: return;
342: } catch (InvocationTargetException e) {
343: if (LOG.isLoggable(Level.FINE))
344: LOG
345: .log(
346: Level.FINE,
347: "DefaultHeaderComponent$AnimationThread.run ",
348: e);
349: }
350: }
351:
352: if (!isInterrupted()) {
353: try {
354: EventQueue.invokeAndWait(new Runnable() {
355: public void run() {
356: defaultHeaderComponent.setCurrentColor(
357: endColor, endColor2);
358: }
359: });
360: } catch (InterruptedException e) {
361: if (LOG.isLoggable(Level.FINE))
362: LOG
363: .log(
364: Level.FINE,
365: "DefaultHeaderComponent$AnimationThread.run ",
366: e);
367: } catch (InvocationTargetException e) {
368: if (LOG.isLoggable(Level.FINE))
369: LOG
370: .log(
371: Level.FINE,
372: "DefaultHeaderComponent$AnimationThread.run ",
373: e);
374: }
375: }
376: }
377:
378: public int getCurrentStep() {
379: return step;
380: }
381:
382: @NotNull
383: private Color getIntermediateColor(double v, int i, @NotNull
384: Color startColor, @NotNull
385: Color endColor) {
386: int rDiff = endColor.getRed() - startColor.getRed();
387: int rOff = (int) (rDiff / v * i);
388: int gDiff = endColor.getGreen() - startColor.getGreen();
389: int gOff = (int) (gDiff / v * i);
390: int bDiff = endColor.getBlue() - startColor.getBlue();
391: int bOff = (int) (bDiff / v * i);
392:
393: int red = startColor.getRed() + rOff;
394: int green = startColor.getGreen() + gOff;
395: int blue = startColor.getBlue() + bOff;
396:
397: return new Color(red, green, blue);
398: }
399: }
400: }
|