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.*;
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.BasicFormattedTextFieldUI;
041: import javax.swing.text.*;
042:
043: import org.jvnet.lafwidget.animation.FadeStateListener;
044: import org.jvnet.substance.painter.text.SubstanceTextPainter;
045: import org.jvnet.substance.text.SubstanceFieldView;
046: import org.jvnet.substance.text.SubstanceHighlighter;
047: import org.jvnet.substance.utils.SubstanceCoreUtilities;
048: import org.jvnet.substance.utils.SubstanceSizeUtils;
049:
050: /**
051: * UI for formatted text fields in <b>Substance</b> look and feel.
052: *
053: * @author Kirill Grouchnikov
054: */
055: public class SubstanceFormattedTextFieldUI extends
056: BasicFormattedTextFieldUI {
057: /**
058: * Listener for fade animations.
059: */
060: protected FadeStateListener substanceFadeStateListener;
061:
062: /**
063: * The associated formatted text field.
064: */
065: protected JFormattedTextField textField;
066:
067: /**
068: * Property change listener.
069: */
070: protected PropertyChangeListener substancePropertyChangeListener;
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
076: */
077: public static ComponentUI createUI(JComponent c) {
078: return new SubstanceFormattedTextFieldUI(c);
079: }
080:
081: /**
082: * Simple constructor.
083: *
084: * @param c
085: * Component (formatted text field).
086: */
087: public SubstanceFormattedTextFieldUI(JComponent c) {
088: super ();
089: this .textField = (JFormattedTextField) c;
090: }
091:
092: @Override
093: protected void installListeners() {
094: super .installListeners();
095:
096: this .substanceFadeStateListener = new FadeStateListener(
097: this .textField, null, null);
098: this .substanceFadeStateListener.registerListeners(false);
099:
100: this .substancePropertyChangeListener = new PropertyChangeListener() {
101: public void propertyChange(PropertyChangeEvent evt) {
102: if ("font".equals(evt.getPropertyName())) {
103: SwingUtilities.invokeLater(new Runnable() {
104: public void run() {
105: textField.updateUI();
106: }
107: });
108: }
109: }
110: };
111: this .textField
112: .addPropertyChangeListener(this .substancePropertyChangeListener);
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
119: */
120: @Override
121: protected void uninstallListeners() {
122: this .substanceFadeStateListener.unregisterListeners();
123: this .substanceFadeStateListener = null;
124:
125: this .textField
126: .removePropertyChangeListener(this .substancePropertyChangeListener);
127: this .substancePropertyChangeListener = null;
128:
129: super .uninstallListeners();
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
136: */
137: @Override
138: protected void installDefaults() {
139: super .installDefaults();
140: Border b = this .textField.getBorder();
141: if (b == null || b instanceof UIResource) {
142: Border newB = new BorderUIResource.CompoundBorderUIResource(
143: new SubstanceBorder(
144: SubstanceSizeUtils
145: .getTextBorderInsets(SubstanceSizeUtils
146: .getComponentFontSize(this .textField))),
147: new BasicBorders.MarginBorder());
148: this .textField.setBorder(newB);
149: }
150: }
151:
152: // /*
153: // * (non-Javadoc)
154: // *
155: // * @see javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
156: // */
157: // @Override
158: // protected void paintBackground(Graphics g) {
159: // SubstanceCoreUtilities.paintCompBackground(g, this.textField);
160: // }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see javax.swing.plaf.basic.BasicTextFieldUI#create(javax.swing.text.Element)
166: */
167: @Override
168: public View create(Element elem) {
169: View super Result = super .create(elem);
170: if (super Result instanceof FieldView) {
171: return new SubstanceFieldView(elem);
172: }
173: return super Result;
174: }
175:
176: /*
177: * (non-Javadoc)
178: *
179: * @see javax.swing.plaf.basic.BasicTextUI#createHighlighter()
180: */
181: @Override
182: protected Highlighter createHighlighter() {
183: return new SubstanceHighlighter();
184: }
185:
186: /*
187: * (non-Javadoc)
188: *
189: * @see javax.swing.plaf.basic.BasicTextUI#paintSafely(java.awt.Graphics)
190: */
191: @Override
192: protected void paintSafely(Graphics _g) {
193: // // Have to call the super implementation since it sets a
194: // // private "painted" flag which affects many other places.
195: // // Without this there will be many visual artifacts with
196: // // painting the caret and highlights
197: // super.paintSafely(_g);
198: //
199: // SubstanceTextPainter textPainter = SubstanceLookAndFeel
200: // .getCurrentTextPainter();
201: //
202: // SubstanceTextPainter.BackgroundPaintingCallback callback = new SubstanceTextPainter.BackgroundPaintingCallback() {
203: // public void paintBackground(Graphics g) {
204: // Highlighter highlighter = getComponent().getHighlighter();
205: //
206: // // paint the background
207: // if (getComponent().isOpaque()) {
208: // SubstanceFormattedTextFieldUI.this.paintBackground(g);
209: // }
210: //
211: // // paint the highlights
212: // if (highlighter != null) {
213: // highlighter.paint(g);
214: // }
215: // }
216: // };
217: // textPainter.init(textField, null, true);
218: // if (textPainter.needsBackgroundImage()) {
219: // textPainter.setBackgroundFill(textField, textField.getBackground(),
220: // false, 0, 0);
221: // textPainter.attachCallback(callback);
222: // } else {
223: // callback.paintBackground(_g);
224: // }
225: //
226: // // paint the view hierarchy
227: // Rectangle alloc = getVisibleEditorRect();
228: // if (alloc != null) {
229: // getRootView(textField).paint(_g, alloc);
230: // }
231: // textPainter.renderSurface(_g);
232: //
233: // // paint the caret
234: // Caret caret = getComponent().getCaret();
235: // if (caret != null) {
236: // caret.paint(_g);
237: // }
238: // Have to call the super implementation since it sets a
239: // private "painted" flag which affects many other places.
240: // Without this there will be many visual artifacts with
241: // painting the caret and highlights
242: Graphics2D dummy = (Graphics2D) _g.create(0, 0, 0, 0);
243: super.paintSafely(dummy);
244: dummy.dispose();
245:
246: SubstanceCoreUtilities.paintTextComponent(_g, this.textField,
247: this.getRootView(this.textField), this
248: .getVisibleEditorRect());
249: }
250: }
|