1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.gui.views.controls;
22 
23 import javax.swing.JTextPane;
24 import javax.swing.UIManager;
25 import javax.swing.text.MutableAttributeSet;
26 import javax.swing.text.SimpleAttributeSet;
27 import javax.swing.text.StyleConstants;
28 
29 import net.sourceforge.atunes.model.IControlsBuilder;
30 import net.sourceforge.atunes.model.ILookAndFeelChangeListener;
31 
32 /**
33  * A CustomTextPane is a JTextPane using the fonts and colors configured for all
34  * application
35  *
36  * @author fleax
37  *
38  */
39 public class CustomTextPane extends JTextPane implements
40 		ILookAndFeelChangeListener {
41 
42 	private static final long serialVersionUID = -3601855261867415475L;
43 
44 	private final Integer alignment;
45 
46 	private final IControlsBuilder controlsBuilder;
47 
48 	/**
49 	 * @param alignment
50 	 * @param controlsBuilder
51 	 */
CustomTextPane(final Integer alignment, final IControlsBuilder controlsBuilder)52 	CustomTextPane(final Integer alignment,
53 			final IControlsBuilder controlsBuilder) {
54 		super();
55 		this.alignment = alignment;
56 		this.controlsBuilder = controlsBuilder;
57 		updateStyle(false);
58 	}
59 
60 	@Override
lookAndFeelChanged()61 	public void lookAndFeelChanged() {
62 		updateStyle(true);
63 	}
64 
65 	/**
66 	 * Updates style of text pane
67 	 *
68 	 * @param forceUpdate
69 	 *            if <code>true</code> will update component to use new style
70 	 */
updateStyle(final boolean forceUpdate)71 	private void updateStyle(final boolean forceUpdate) {
72 		MutableAttributeSet mainStyle = new SimpleAttributeSet();
73 		StyleConstants.setAlignment(
74 				mainStyle,
75 				this.alignment != null ? this.alignment : this.controlsBuilder
76 						.getComponentOrientationAsTextStyleConstant());
77 		StyleConstants.setFontFamily(mainStyle, UIManager.getFont("Label.font")
78 				.getFamily());
79 		StyleConstants.setFontSize(mainStyle, UIManager.getFont("Label.font")
80 				.getSize());
81 		StyleConstants.setForeground(mainStyle,
82 				UIManager.getColor("Label.foreground"));
83 		getStyledDocument().setParagraphAttributes(0, 0, mainStyle, true);
84 		if (forceUpdate) {
85 			// Setting text again to use new style
86 			setText(getText());
87 		}
88 	}
89 }
90