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.BasicTextFieldUI;
041: import javax.swing.text.*;
042:
043: import org.jvnet.lafwidget.animation.FadeStateListener;
044: import org.jvnet.substance.text.SubstanceFieldView;
045: import org.jvnet.substance.text.SubstanceHighlighter;
046: import org.jvnet.substance.utils.*;
047:
048: /**
049: * UI for text fields in <b>Substance</b> look and feel.
050: *
051: * @author Kirill Grouchnikov
052: */
053: public class SubstanceTextFieldUI extends BasicTextFieldUI {
054: /**
055: * Listener for fade animations.
056: */
057: protected FadeStateListener substanceFadeStateListener;
058:
059: /**
060: * The associated text field.
061: */
062: protected JTextField textField;
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 SubstanceTextFieldUI(c);
076: }
077:
078: /**
079: * Simple constructor.
080: *
081: * @param c
082: * Component (text field).
083: */
084: public SubstanceTextFieldUI(JComponent c) {
085: super ();
086: this .textField = (JTextField) c;
087: }
088:
089: // /*
090: // * (non-Javadoc)
091: // *
092: // * @see javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
093: // */
094: // @Override
095: // protected void paintBackground(Graphics g) {
096: // SubstanceCoreUtilities.paintCompBackground(g, this.textField);
097: // }
098: //
099: @Override
100: protected void paintSafely(Graphics _g) {
101: // Have to call the super implementation since it sets a
102: // private "painted" flag which affects many other places.
103: // Without this there will be many visual artifacts with
104: // painting the caret and highlights
105: Graphics2D dummy = (Graphics2D) _g.create(0, 0, 0, 0);
106: super .paintSafely(dummy);
107: dummy.dispose();
108:
109: SubstanceCoreUtilities.paintTextComponent(_g, this .textField,
110: this .getRootView(this .textField), this
111: .getVisibleEditorRect());
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see javax.swing.plaf.basic.BasicTextUI#installListeners()
118: */
119: @Override
120: protected void installListeners() {
121: super .installListeners();
122:
123: this .substanceFadeStateListener = new FadeStateListener(
124: this .textField, null, null);
125: this .substanceFadeStateListener.registerListeners(false);
126:
127: this .substancePropertyChangeListener = new PropertyChangeListener() {
128: public void propertyChange(PropertyChangeEvent evt) {
129: if ("font".equals(evt.getPropertyName())) {
130: SwingUtilities.invokeLater(new Runnable() {
131: public void run() {
132: textField.updateUI();
133: }
134: });
135: }
136: }
137: };
138: this .textField
139: .addPropertyChangeListener(this .substancePropertyChangeListener);
140: }
141:
142: /*
143: * (non-Javadoc)
144: *
145: * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
146: */
147: @Override
148: protected void uninstallListeners() {
149: this .substanceFadeStateListener.unregisterListeners();
150: this .substanceFadeStateListener = null;
151:
152: this .textField
153: .removePropertyChangeListener(this .substancePropertyChangeListener);
154: this .substancePropertyChangeListener = null;
155:
156: super .uninstallListeners();
157: }
158:
159: /*
160: * (non-Javadoc)
161: *
162: * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
163: */
164: @Override
165: protected void installDefaults() {
166: super .installDefaults();
167: Border b = this .textField.getBorder();
168: if (b == null || b instanceof UIResource) {
169: Border newB = new BorderUIResource.CompoundBorderUIResource(
170: new SubstanceBorder(
171: SubstanceSizeUtils
172: .getTextBorderInsets(SubstanceSizeUtils
173: .getComponentFontSize(this .textField))),
174: new BasicBorders.MarginBorder());
175: this .textField.setBorder(newB);
176: }
177: }
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see javax.swing.plaf.basic.BasicTextFieldUI#create(javax.swing.text.Element)
183: */
184: @Override
185: public View create(Element elem) {
186: View super Result = super .create(elem);
187: if (super Result instanceof FieldView) {
188: return new SubstanceFieldView(elem);
189: }
190: return super Result;
191: }
192:
193: /*
194: * (non-Javadoc)
195: *
196: * @see javax.swing.plaf.basic.BasicTextUI#createHighlighter()
197: */
198: @Override
199: protected Highlighter createHighlighter() {
200: return new SubstanceHighlighter();
201: }
202: }
|