001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance;
031:
032: import java.awt.Graphics;
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035:
036: import javax.swing.*;
037: import javax.swing.border.Border;
038: import javax.swing.plaf.*;
039: import javax.swing.plaf.basic.BasicBorders;
040: import javax.swing.plaf.basic.BasicTextPaneUI;
041: import javax.swing.text.Highlighter;
042:
043: import org.jvnet.lafwidget.animation.FadeStateListener;
044: import org.jvnet.substance.text.SubstanceHighlighter;
045: import org.jvnet.substance.utils.SubstanceCoreUtilities;
046: import org.jvnet.substance.utils.SubstanceSizeUtils;
047:
048: /**
049: * UI for text panes in <b>Substance</b> look and feel.
050: *
051: * @author Kirill Grouchnikov
052: */
053: public class SubstanceTextPaneUI extends BasicTextPaneUI {
054: /**
055: * Listener for fade animations.
056: */
057: protected FadeStateListener substanceFadeStateListener;
058:
059: /**
060: * The associated text pane.
061: */
062: protected JTextPane textPane;
063:
064: /**
065: * Property change listener.
066: */
067: protected PropertyChangeListener substancePropertyChangeListener;
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
073: */
074: public static ComponentUI createUI(JComponent c) {
075: return new SubstanceTextPaneUI(c);
076: }
077:
078: /**
079: * Simple constructor.
080: *
081: * @param c
082: * Component (text pane).
083: */
084: public SubstanceTextPaneUI(JComponent c) {
085: super ();
086: this .textPane = (JTextPane) c;
087: }
088:
089: @Override
090: protected void installListeners() {
091: super .installListeners();
092:
093: this .substanceFadeStateListener = new FadeStateListener(
094: this .textPane, null, null);
095: this .substanceFadeStateListener.registerListeners(false);
096:
097: this .substancePropertyChangeListener = new PropertyChangeListener() {
098: public void propertyChange(PropertyChangeEvent evt) {
099: if ("font".equals(evt.getPropertyName())) {
100: SwingUtilities.invokeLater(new Runnable() {
101: public void run() {
102: textPane.updateUI();
103: }
104: });
105: }
106: }
107: };
108: this .textPane
109: .addPropertyChangeListener(this .substancePropertyChangeListener);
110: }
111:
112: /*
113: * (non-Javadoc)
114: *
115: * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
116: */
117: @Override
118: protected void uninstallListeners() {
119: this .substanceFadeStateListener.unregisterListeners();
120: this .substanceFadeStateListener = null;
121:
122: this .textPane
123: .removePropertyChangeListener(this .substancePropertyChangeListener);
124: this .substancePropertyChangeListener = null;
125:
126: super .uninstallListeners();
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
133: */
134: @Override
135: protected void installDefaults() {
136: super .installDefaults();
137: Border b = this .textPane.getBorder();
138: if (b == null || b instanceof UIResource) {
139: Border newB = new BorderUIResource.CompoundBorderUIResource(
140: new SubstanceBorder(
141: SubstanceSizeUtils
142: .getTextBorderInsets(SubstanceSizeUtils
143: .getComponentFontSize(this .textPane))),
144: new BasicBorders.MarginBorder());
145: this .textPane.setBorder(newB);
146: }
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
153: */
154: @Override
155: protected void paintBackground(Graphics g) {
156: SubstanceCoreUtilities
157: .paintTextCompBackground(g, this .textPane);
158: }
159:
160: /*
161: * (non-Javadoc)
162: *
163: * @see javax.swing.plaf.basic.BasicTextUI#createHighlighter()
164: */
165: @Override
166: protected Highlighter createHighlighter() {
167: return new SubstanceHighlighter();
168: }
169: }
|