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.BasicEditorPaneUI;
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 editor panes in <b>Substance</b> look and feel.
050: *
051: * @author Kirill Grouchnikov
052: */
053: public class SubstanceEditorPaneUI extends BasicEditorPaneUI {
054: /**
055: * Listener for fade animations.
056: */
057: protected FadeStateListener substanceFadeStateListener;
058:
059: /**
060: * The associated editor pane.
061: */
062: protected JEditorPane editorPane;
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 SubstanceEditorPaneUI(c);
076: }
077:
078: /**
079: * Simple constructor.
080: *
081: * @param c
082: * Component (editor pane).
083: */
084: public SubstanceEditorPaneUI(JComponent c) {
085: super ();
086: this .editorPane = (JEditorPane) c;
087: }
088:
089: /*
090: * (non-Javadoc)
091: *
092: * @see javax.swing.plaf.basic.BasicTextUI#installListeners()
093: */
094: @Override
095: protected void installListeners() {
096: super .installListeners();
097: super .installListeners();
098:
099: this .substanceFadeStateListener = new FadeStateListener(
100: this .editorPane, null, null);
101: this .substanceFadeStateListener.registerListeners(false);
102:
103: this .substancePropertyChangeListener = new PropertyChangeListener() {
104: public void propertyChange(PropertyChangeEvent evt) {
105: if ("font".equals(evt.getPropertyName())) {
106: SwingUtilities.invokeLater(new Runnable() {
107: public void run() {
108: editorPane.updateUI();
109: }
110: });
111: }
112: }
113: };
114: this .editorPane
115: .addPropertyChangeListener(this .substancePropertyChangeListener);
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
122: */
123: @Override
124: protected void uninstallListeners() {
125: this .substanceFadeStateListener.unregisterListeners();
126: this .substanceFadeStateListener = null;
127:
128: this .editorPane
129: .removePropertyChangeListener(this .substancePropertyChangeListener);
130: this .substancePropertyChangeListener = null;
131:
132: super .uninstallListeners();
133: }
134:
135: /*
136: * (non-Javadoc)
137: *
138: * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
139: */
140: @Override
141: protected void installDefaults() {
142: super .installDefaults();
143: Border b = this .editorPane.getBorder();
144: if (b == null || b instanceof UIResource) {
145: Border newB = new BorderUIResource.CompoundBorderUIResource(
146: new SubstanceBorder(
147: SubstanceSizeUtils
148: .getTextBorderInsets(SubstanceSizeUtils
149: .getComponentFontSize(this .editorPane))),
150: new BasicBorders.MarginBorder());
151: this .editorPane.setBorder(newB);
152: }
153: }
154:
155: /*
156: * (non-Javadoc)
157: *
158: * @see javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
159: */
160: @Override
161: protected void paintBackground(Graphics g) {
162: SubstanceCoreUtilities.paintTextCompBackground(g,
163: this .editorPane);
164: }
165:
166: /*
167: * (non-Javadoc)
168: *
169: * @see javax.swing.plaf.basic.BasicTextUI#createHighlighter()
170: */
171: @Override
172: protected Highlighter createHighlighter() {
173: return new SubstanceHighlighter();
174: }
175: }
|