001: /*
002: * @(#)BannerPanel.java
003: *
004: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
005: */
006: package com.jidesoft.dialog;
007:
008: import com.jidesoft.swing.JideSwingUtilities;
009: import com.jidesoft.swing.MultilineLabel;
010: import com.jidesoft.utils.SecurityUtils;
011:
012: import javax.swing.*;
013: import java.awt.*;
014: import java.beans.PropertyChangeEvent;
015: import java.beans.PropertyChangeListener;
016:
017: /**
018: * <code>BannerPanel</code> is a panel that can show title, subtitle and icon
019: * with title on top, subtitle on the bottom and icon on the left. You can use ImageIcon as the icon
020: * but you can also use your own component as the icon component by using {@link #setIconComponent(javax.swing.JComponent)}.
021: * <p/>
022: * <code>BannerPanel</code> can be placed on top of any dialog or any panel to show some help information
023: * or display a product logo.
024: */
025: public class BannerPanel extends JPanel {
026:
027: /**
028: * Title of the banner panel.
029: */
030: protected String _title;
031:
032: /**
033: * Subtitle or description of the banner panel.
034: */
035: protected String _subtitle;
036:
037: /**
038: * Icon for the banner panel.
039: */
040: protected ImageIcon _titleIcon;
041:
042: public static final String TITLE_PROPERTY = "title";
043: public static final String SUBTITLE_PROPERTY = "subTitle";
044: public static final String ICON_PROPERTY = "icon";
045: public static final String ICON_COMPONENT_PROPERTY = "iconComponent";
046: public static final String PROPERTY_TITLE_FONT = "titleFont";
047: public static final String PROPERTY_SUBTITLE_FONT = "subTitleFont";
048: public static final String PROPERTY_TITLE_ICON_LOCATION = "titleIconLocation";
049:
050: /**
051: * A component to be placed at position of icon.
052: */
053: private JComponent _iconComponent;
054:
055: protected int _subTitleIndent = 20;
056: protected Font _titleFont;
057: protected Color _titleColor;
058: protected Font _subTitleFont;
059: protected Color _subTitleColor;
060:
061: protected Paint _backgroundPaint;
062: protected PropertyChangeListener _propertyListener;
063:
064: private JLabel _titleLabel;
065: private MultilineLabel _subtitleLabel;
066:
067: protected Color _startColor;
068: protected Color _endColor;
069: protected boolean _isVertical;
070:
071: private int _titleIconLocation = SwingConstants.TRAILING;
072: public JPanel _textPanel;
073:
074: /**
075: * Creates an empty BannerPanel.
076: */
077: public BannerPanel() {
078: lazyInitialize();
079: }
080:
081: /**
082: * Creates a BannerPanel with title and subtitle.
083: *
084: * @param title the title.
085: */
086: public BannerPanel(String title) {
087: setTitle(title);
088: lazyInitialize();
089: }
090:
091: /**
092: * Creates a BannerPanel with title and subtitle.
093: *
094: * @param title the title.
095: * @param subtitle the sub title.
096: */
097: public BannerPanel(String title, String subtitle) {
098: setTitle(title);
099: setSubtitle(subtitle);
100: lazyInitialize();
101: }
102:
103: /**
104: * Creates a BannerPanel with title, subtitle and icon.
105: *
106: * @param title the title.
107: * @param subtitle the sub title.
108: * @param titleIcon the icon.
109: */
110: public BannerPanel(String title, String subtitle,
111: ImageIcon titleIcon) {
112: setTitle(title);
113: setSubtitle(subtitle);
114: setTitleIcon(titleIcon);
115: lazyInitialize();
116: }
117:
118: /**
119: * Creates a BannerPanel with title, subtitle and component.
120: *
121: * @param title the title.
122: * @param subtitle the sub title.
123: * @param iconComponent the icon component. It will appear where the icon is if using constructor {@link #BannerPanel(String,String,javax.swing.ImageIcon)}.
124: */
125: public BannerPanel(String title, String subtitle,
126: JComponent iconComponent) {
127: setTitle(title);
128: setSubtitle(subtitle);
129: _iconComponent = iconComponent;
130: lazyInitialize();
131: }
132:
133: public void lazyInitialize() {
134: removeAll();
135: _textPanel = new JPanel(new BorderLayout(5, 5));
136: _textPanel.setOpaque(false);
137: _textPanel.setBorder(BorderFactory.createEmptyBorder(3, 10, 2,
138: 10));
139:
140: if (getSubTitleFont() == null) {
141: setSubTitleFont(getFont());
142: }
143:
144: _subtitleLabel = new MultilineLabel(getSubtitle());
145: _subtitleLabel.setFont(getSubTitleFont());
146: if (getSubTitleColor() == null) {
147: setSubTitleColor(getForeground());
148: }
149: _subtitleLabel.setForeground(getSubTitleColor());
150: _subtitleLabel.setBorder(BorderFactory.createEmptyBorder(0,
151: getSubTitleIndent(), 0, 0));
152: _textPanel.add(_subtitleLabel, BorderLayout.CENTER);
153:
154: _titleLabel = new JLabel(getTitle());
155: if (getTitleFont() == null) {
156: setTitleFont(SecurityUtils.createFont(getFont()
157: .getFontName(), Font.BOLD, getFont().getSize() + 2));
158: }
159: _titleLabel.setFont(getTitleFont());
160: if (getTitleColor() == null) {
161: setTitleColor(getForeground());
162: }
163: _titleLabel.setForeground(getTitleColor());
164: if (getSubtitle() != null && getSubtitle().length() != 0) {
165: _textPanel.add(_titleLabel, BorderLayout.BEFORE_FIRST_LINE);
166: } else {
167: _textPanel.add(_titleLabel, BorderLayout.CENTER);
168: }
169:
170: if (getTitleIcon() == null && _iconComponent == null) {
171: _iconComponent = new JLabel("");
172: } else if (getTitleIcon() == null && _iconComponent != null) {
173: } else {
174: _iconComponent = new JLabel(getTitleIcon());
175: }
176: _iconComponent.setBorder(BorderFactory.createEmptyBorder(5, 5,
177: 5, 5));
178:
179: setLayout(new BorderLayout(5, 0));
180: add(_textPanel, BorderLayout.CENTER);
181: addIconComponent(_iconComponent);
182:
183: _propertyListener = new PropertyChangeListener() {
184: public void propertyChange(PropertyChangeEvent evt) {
185: if (_titleLabel != null
186: && TITLE_PROPERTY.equals(evt.getPropertyName())) {
187: _titleLabel.setText((String) evt.getNewValue());
188: } else if (_subtitleLabel != null
189: && SUBTITLE_PROPERTY.equals(evt
190: .getPropertyName())) {
191: String text = (String) evt.getNewValue();
192: _subtitleLabel.setText(text);
193: if (text != null && text.length() != 0) {
194: _textPanel.add(_titleLabel,
195: BorderLayout.BEFORE_FIRST_LINE);
196: _textPanel.add(_subtitleLabel,
197: BorderLayout.CENTER);
198: } else {
199: _textPanel
200: .add(_titleLabel, BorderLayout.CENTER);
201: }
202: } else if (ICON_PROPERTY.equals(evt.getPropertyName())) {
203: if (_iconComponent instanceof JLabel) {
204: ((JLabel) _iconComponent)
205: .setIcon(getTitleIcon());
206: }
207: } else if (ICON_COMPONENT_PROPERTY.equals(evt
208: .getPropertyName())) {
209: if (evt.getOldValue() instanceof JComponent) {
210: _textPanel.remove((JComponent) evt
211: .getOldValue());
212: }
213: if (evt.getNewValue() instanceof JComponent) {
214: addIconComponent((JComponent) evt.getNewValue());
215: }
216: } else if (PROPERTY_TITLE_FONT.equals(evt
217: .getPropertyName())) {
218: if (_titleLabel != null) {
219: _titleLabel.setFont((Font) evt.getNewValue());
220: }
221: } else if (PROPERTY_SUBTITLE_FONT.equals(evt
222: .getPropertyName())) {
223: if (_subtitleLabel != null) {
224: _subtitleLabel
225: .setFont((Font) evt.getNewValue());
226: }
227: } else if (PROPERTY_TITLE_ICON_LOCATION.equals(evt
228: .getPropertyName())) {
229: addIconComponent(_iconComponent);
230: }
231: }
232: };
233: addPropertyChangeListener(_propertyListener);
234: }
235:
236: private void addIconComponent(JComponent component) {
237: if (component != null) {
238: switch (getTitleIconLocation()) {
239: case SwingConstants.EAST:
240: add(component, BorderLayout.EAST);
241: break;
242: case SwingConstants.WEST:
243: add(component, BorderLayout.WEST);
244: break;
245: case SwingConstants.LEADING:
246: add(component, BorderLayout.BEFORE_LINE_BEGINS);
247: break;
248: case SwingConstants.TRAILING:
249: add(component, BorderLayout.AFTER_LINE_ENDS);
250: break;
251: }
252: }
253: }
254:
255: /**
256: * Prepares the title icon.
257: *
258: * @param icon the input icon fro setTitleIcon(icon).
259: * @return the image icon after processing. By default it will return the same image icon. Subclass
260: * can override it to scale the image or do other processing.
261: */
262: protected ImageIcon prepareTitleIcon(ImageIcon icon) {
263: return icon;
264: }
265:
266: /**
267: * Gets the Paint used to paint the background of the BannerPanel.
268: *
269: * @return the Paint used to paint the background.
270: */
271: public Paint getBackgroundPaint() {
272: return _backgroundPaint;
273: }
274:
275: /**
276: * Sets the Paint used to paint the background of the BannerPanel.
277: * User can set the paint to a gradient paint to make the BannerPanel looks attractive.
278: *
279: * @param backgroundPaint the background paint.
280: */
281: public void setBackgroundPaint(Paint backgroundPaint) {
282: _backgroundPaint = backgroundPaint;
283: }
284:
285: /**
286: * This method allows you to use gradient background without using {@link #setBackgroundPaint(java.awt.Paint)}
287: * method. You can use GradientPaint to do the same thing. However if you use this method,
288: * it will use fast gradient paint defined in JideSwingUtilities to do the painting.
289: *
290: * @param startColor start color of the gradient
291: * @param endColor end color of the gradient
292: * @param isVertical vertical or not
293: */
294: public void setGradientPaint(Color startColor, Color endColor,
295: boolean isVertical) {
296: setStartColor(startColor);
297: setEndColor(endColor);
298: setVertical(isVertical);
299: }
300:
301: /**
302: * Paints the background.
303: *
304: * @param g the Graphics
305: */
306: @Override
307: protected void paintComponent(Graphics g) {
308: super .paintComponent(g);
309: if (getStartColor() != null && getEndColor() != null) {
310: JideSwingUtilities.fillGradient((Graphics2D) g,
311: new Rectangle(0, 0, getWidth(), getHeight()),
312: getStartColor(), getEndColor(), isVertical());
313: } else if (getBackgroundPaint() != null) {
314: Graphics2D g2d = (Graphics2D) g;
315: g2d.setPaint(getBackgroundPaint());
316: g2d.fillRect(0, 0, getWidth(), getHeight());
317: }
318: }
319:
320: /**
321: * Gets the title.
322: *
323: * @return the title of the banner panel.
324: */
325: public String getTitle() {
326: return _title;
327: }
328:
329: /**
330: * Sets the title.
331: *
332: * @param title the new title.
333: */
334: public void setTitle(String title) {
335: String old = _title;
336: _title = title;
337: firePropertyChange(TITLE_PROPERTY, old, _title);
338: }
339:
340: /**
341: * Gets the subtitle.
342: *
343: * @return the subtitle
344: */
345: public String getSubtitle() {
346: return _subtitle;
347: }
348:
349: /**
350: * Sets the subtitle.
351: *
352: * @param subtitle the new subtitle.
353: */
354: public void setSubtitle(String subtitle) {
355: String old = _subtitle;
356: _subtitle = subtitle;
357: firePropertyChange(SUBTITLE_PROPERTY, old, _subtitle);
358: }
359:
360: /**
361: * Gets the title icon.
362: *
363: * @return the title icon
364: */
365: public ImageIcon getTitleIcon() {
366: return _titleIcon;
367: }
368:
369: /**
370: * Sets the title icon.
371: *
372: * @param titleIcon the new titleIcon.
373: */
374: public void setTitleIcon(ImageIcon titleIcon) {
375: ImageIcon old = _titleIcon;
376: _titleIcon = prepareTitleIcon(titleIcon);
377: firePropertyChange(ICON_PROPERTY, old, _titleIcon);
378: }
379:
380: /**
381: * Gets the icon component. If you use constructor {@link #BannerPanel(String,String,javax.swing.ImageIcon)},
382: * the icon component will be a JLabel with the icon in the 3rd parameter. If you use the constructor {@link #BannerPanel(String,String,javax.swing.JComponent)},
383: * it will return the component as in the 3rd parameter.
384: *
385: * @return the icon component
386: */
387: public JComponent getIconComponent() {
388: return _iconComponent;
389: }
390:
391: /**
392: * Sets the icon component.
393: *
394: * @param iconComponent the component that is used as the icon.
395: */
396: public void setIconComponent(JComponent iconComponent) {
397: JComponent old = _iconComponent;
398: _iconComponent = iconComponent;
399: firePropertyChange(ICON_COMPONENT_PROPERTY, old, _iconComponent);
400: }
401:
402: /**
403: * Gets the font of the subtitle.
404: *
405: * @return the font of the subtitle
406: */
407: public Font getSubTitleFont() {
408: return _subTitleFont;
409: }
410:
411: /**
412: * Sets the font for the subtitle.
413: *
414: * @param subTitleFont the new font for the subtitle.
415: */
416: public void setSubTitleFont(Font subTitleFont) {
417: Font old = _subTitleFont;
418: _subTitleFont = subTitleFont;
419: firePropertyChange(PROPERTY_SUBTITLE_FONT, old, _subTitleFont);
420: }
421:
422: /**
423: * Gets the font of the title.
424: *
425: * @return the font of the title
426: */
427: public Font getTitleFont() {
428: return _titleFont;
429: }
430:
431: /**
432: * Sets the font for the title.
433: *
434: * @param titleFont the new font for the title.
435: */
436: public void setTitleFont(Font titleFont) {
437: Font old = _titleFont;
438: _titleFont = titleFont;
439: firePropertyChange(PROPERTY_TITLE_FONT, old, _titleFont);
440: }
441:
442: /**
443: * Gets the subtitle indent.
444: *
445: * @return the subtitle indent.
446: */
447: public int getSubTitleIndent() {
448: return _subTitleIndent;
449: }
450:
451: /**
452: * Sets the subtitle indent. Subtitle is always behind the title.
453: * The indent will decide how behind. It's in pixels.
454: *
455: * @param subTitleIndent the new index.
456: */
457: public void setSubTitleIndent(int subTitleIndent) {
458: _subTitleIndent = subTitleIndent;
459: }
460:
461: /**
462: * Gets the title color.
463: *
464: * @return the color of title.
465: */
466: public Color getTitleColor() {
467: return _titleColor;
468: }
469:
470: /**
471: * Sets the title color.
472: *
473: * @param titleColor the text color for the title.
474: */
475: public void setTitleColor(Color titleColor) {
476: _titleColor = titleColor;
477: if (_titleLabel != null) {
478: _titleLabel.setForeground(titleColor);
479: }
480: }
481:
482: /**
483: * Gets the subtitle color.
484: *
485: * @return the color of subtitle.
486: */
487: public Color getSubTitleColor() {
488: return _subTitleColor;
489: }
490:
491: /**
492: * Sets the subtitle color.
493: *
494: * @param subTitleColor the text color for the subtitle.
495: */
496: public void setSubTitleColor(Color subTitleColor) {
497: _subTitleColor = subTitleColor;
498: if (_subtitleLabel != null) {
499: _subtitleLabel.setForeground(subTitleColor);
500: }
501: }
502:
503: @Override
504: public void setBackground(Color bg) {
505: super .setBackground(bg);
506: if (_titleLabel != null) {
507: _titleLabel.setBackground(bg);
508: }
509: if (_subtitleLabel != null) {
510: _subtitleLabel.setBackground(bg);
511: }
512: if (_iconComponent != null) {
513: _iconComponent.setBackground(bg);
514: }
515: }
516:
517: @Override
518: public void setForeground(Color fg) {
519: super .setForeground(fg);
520: setTitleColor(fg);
521: setSubTitleColor(fg);
522: if (_iconComponent != null) {
523: _iconComponent.setForeground(fg);
524: }
525: }
526:
527: public Color getStartColor() {
528: return _startColor;
529: }
530:
531: public void setStartColor(Color startColor) {
532: _startColor = startColor;
533: }
534:
535: public Color getEndColor() {
536: return _endColor;
537: }
538:
539: public void setEndColor(Color endColor) {
540: _endColor = endColor;
541: }
542:
543: public boolean isVertical() {
544: return _isVertical;
545: }
546:
547: public void setVertical(boolean vertical) {
548: _isVertical = vertical;
549: }
550:
551: /**
552: * Gets the title icon location. By default, it is SwingConstants.TRAILING.
553: *
554: * @return the title icon location.
555: */
556: public int getTitleIconLocation() {
557: return _titleIconLocation;
558: }
559:
560: /**
561: * Sets the title icon location. By default the title icon is added a border layout using BorderLayout.AFTER_LINE_ENDS.
562: * However you can use this method to decide where to add. Valid values are SwingContants.EAST and SwingContants.WEST
563: * as well as SwingContants.LEADING and SwingContants.TRAILING considering the case of both RTL and LTR.
564: *
565: * @param titleIconLocation the title icon location.
566: */
567: public void setTitleIconLocation(int titleIconLocation) {
568: int old = _titleIconLocation;
569: if (old != titleIconLocation) {
570: _titleIconLocation = titleIconLocation;
571: firePropertyChange(PROPERTY_TITLE_ICON_LOCATION, old,
572: _titleIconLocation);
573: }
574: }
575:
576: /**
577: * Gets the component for the title.
578: *
579: * @return a JLabel.
580: */
581: public JComponent getTitleLabel() {
582: return _titleLabel;
583: }
584:
585: /**
586: * Gets the component for the subtitle.
587: *
588: * @return a MultilineLabel
589: */
590: public JComponent getSubtitleLabel() {
591: return _subtitleLabel;
592: }
593:
594: /**
595: * Gets the component for the title icon.
596: *
597: * @return a JLabel or the third parameter you passed in using the constrcutor {@link #BannerPanel(String,String,javax.swing.JComponent)}.
598: * @deprecated use {@link #getIconComponent()} instead. We also add a setter for it which was missing before.
599: */
600: public JComponent getImageLabel() {
601: return _iconComponent;
602: }
603: }
|