~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/ui/SubstanceTextFieldUI.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 *  o Redistributions of source code must retain the above copyright notice,
 
8
 *    this list of conditions and the following disclaimer.
 
9
 *
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice,
 
11
 *    this list of conditions and the following disclaimer in the documentation
 
12
 *    and/or other materials provided with the distribution.
 
13
 *
 
14
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of
 
15
 *    its contributors may be used to endorse or promote products derived
 
16
 *    from this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
package org.pushingpixels.substance.internal.ui;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.event.MouseEvent;
 
34
import java.beans.PropertyChangeEvent;
 
35
import java.beans.PropertyChangeListener;
 
36
 
 
37
import javax.swing.*;
 
38
import javax.swing.border.Border;
 
39
import javax.swing.plaf.*;
 
40
import javax.swing.plaf.basic.BasicBorders;
 
41
import javax.swing.plaf.basic.BasicTextFieldUI;
 
42
 
 
43
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
 
44
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
45
import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
 
46
import org.pushingpixels.substance.internal.utils.*;
 
47
import org.pushingpixels.substance.internal.utils.border.SubstanceTextComponentBorder;
 
48
import org.pushingpixels.trident.swing.SwingRepaintCallback;
 
49
 
 
50
/**
 
51
 * UI for text fields in <b>Substance</b> look and feel.
 
52
 * 
 
53
 * @author Kirill Grouchnikov
 
54
 */
 
55
public class SubstanceTextFieldUI extends BasicTextFieldUI implements
 
56
                TransitionAwareUI {
 
57
        protected StateTransitionTracker stateTransitionTracker;
 
58
 
 
59
        /**
 
60
         * The associated text field.
 
61
         */
 
62
        protected JTextField textField;
 
63
 
 
64
        /**
 
65
         * Property change listener.
 
66
         */
 
67
        protected PropertyChangeListener substancePropertyChangeListener;
 
68
 
 
69
        /**
 
70
         * Listener for transition animations.
 
71
         */
 
72
        private RolloverTextControlListener substanceRolloverListener;
 
73
 
 
74
        /**
 
75
         * Surrogate button model for tracking the state transitions.
 
76
         */
 
77
        private ButtonModel transitionModel;
 
78
 
 
79
        /*
 
80
         * (non-Javadoc)
 
81
         * 
 
82
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
83
         */
 
84
        public static ComponentUI createUI(JComponent comp) {
 
85
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
86
                return new SubstanceTextFieldUI(comp);
 
87
        }
 
88
 
 
89
        /**
 
90
         * Simple constructor.
 
91
         * 
 
92
         * @param c
 
93
         *            Component (text field).
 
94
         */
 
95
        public SubstanceTextFieldUI(JComponent c) {
 
96
                super();
 
97
                this.textField = (JTextField) c;
 
98
 
 
99
                this.transitionModel = new DefaultButtonModel();
 
100
                this.transitionModel.setArmed(false);
 
101
                this.transitionModel.setSelected(false);
 
102
                this.transitionModel.setPressed(false);
 
103
                this.transitionModel.setRollover(false);
 
104
                this.transitionModel.setEnabled(this.textField.isEnabled());
 
105
 
 
106
                this.stateTransitionTracker = new StateTransitionTracker(
 
107
                                this.textField, this.transitionModel);
 
108
                this.stateTransitionTracker
 
109
                                .setRepaintCallback(new StateTransitionTracker.RepaintCallback() {
 
110
                                        @Override
 
111
                                        public SwingRepaintCallback getRepaintCallback() {
 
112
                                                return SubstanceCoreUtilities
 
113
                                                                .getTextComponentRepaintCallback(textField);
 
114
                                        }
 
115
                                });
 
116
        }
 
117
 
 
118
        /*
 
119
         * (non-Javadoc)
 
120
         * 
 
121
         * @see
 
122
         * javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
 
123
         */
 
124
        @Override
 
125
        protected void paintBackground(Graphics g) {
 
126
                SubstanceTextUtilities.paintTextCompBackground(g, this.textField);
 
127
        }
 
128
 
 
129
        /*
 
130
         * (non-Javadoc)
 
131
         * 
 
132
         * @see javax.swing.plaf.basic.BasicTextUI#installListeners()
 
133
         */
 
134
        @Override
 
135
        protected void installListeners() {
 
136
                super.installListeners();
 
137
 
 
138
                this.substanceRolloverListener = new RolloverTextControlListener(
 
139
                                this.textField, this, this.transitionModel);
 
140
                this.substanceRolloverListener.registerListeners();
 
141
 
 
142
                this.stateTransitionTracker.registerModelListeners();
 
143
                this.stateTransitionTracker.registerFocusListeners();
 
144
 
 
145
                this.substancePropertyChangeListener = new PropertyChangeListener() {
 
146
                        @Override
 
147
            public void propertyChange(PropertyChangeEvent evt) {
 
148
                                if ("font".equals(evt.getPropertyName())) {
 
149
                                        SwingUtilities.invokeLater(new Runnable() {
 
150
                                                @Override
 
151
                        public void run() {
 
152
                                                        // remember the caret location - issue 404
 
153
                                                        int caretPos = textField.getCaretPosition();
 
154
                                                        textField.updateUI();
 
155
                                                        textField.setCaretPosition(caretPos);
 
156
                                                        Container parent = textField.getParent();
 
157
                                                        if (parent != null) {
 
158
                                                                parent.invalidate();
 
159
                                                                parent.validate();
 
160
                                                        }
 
161
                                                }
 
162
                                        });
 
163
                                }
 
164
 
 
165
                                if ("enabled".equals(evt.getPropertyName())) {
 
166
                                        transitionModel.setEnabled(textField.isEnabled());
 
167
                                }
 
168
                        }
 
169
                };
 
170
                this.textField
 
171
                                .addPropertyChangeListener(this.substancePropertyChangeListener);
 
172
        }
 
173
 
 
174
        /*
 
175
         * (non-Javadoc)
 
176
         * 
 
177
         * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
 
178
         */
 
179
        @Override
 
180
        protected void uninstallListeners() {
 
181
                this.stateTransitionTracker.unregisterModelListeners();
 
182
                this.stateTransitionTracker.unregisterFocusListeners();
 
183
 
 
184
                this.textField
 
185
                                .removePropertyChangeListener(this.substancePropertyChangeListener);
 
186
                this.substancePropertyChangeListener = null;
 
187
 
 
188
                this.substanceRolloverListener.unregisterListeners();
 
189
                this.substanceRolloverListener = null;
 
190
 
 
191
                // this.textField.removeFocusListener(this.substanceFocusListener);
 
192
                // this.substanceFocusListener = null;
 
193
 
 
194
                super.uninstallListeners();
 
195
        }
 
196
 
 
197
        /*
 
198
         * (non-Javadoc)
 
199
         * 
 
200
         * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
 
201
         */
 
202
        @Override
 
203
        protected void installDefaults() {
 
204
                super.installDefaults();
 
205
                Border b = this.textField.getBorder();
 
206
                if (b == null || b instanceof UIResource) {
 
207
                        Border newB = new BorderUIResource.CompoundBorderUIResource(
 
208
                                        new SubstanceTextComponentBorder(SubstanceSizeUtils
 
209
                                                        .getTextBorderInsets(SubstanceSizeUtils
 
210
                                                                        .getComponentFontSize(this.textField))),
 
211
                                        new BasicBorders.MarginBorder());
 
212
                        this.textField.setBorder(newB);
 
213
                }
 
214
 
 
215
                // support for per-window skins
 
216
                SwingUtilities.invokeLater(new Runnable() {
 
217
                        @Override
 
218
                        public void run() {
 
219
                                if (textField == null)
 
220
                                        return;
 
221
                                Color foregr = textField.getForeground();
 
222
                                if ((foregr == null) || (foregr instanceof UIResource)) {
 
223
                                        textField
 
224
                                                        .setForeground(SubstanceColorUtilities
 
225
                                                                        .getForegroundColor(SubstanceLookAndFeel
 
226
                                                                                        .getCurrentSkin(textField)
 
227
                                                                                        .getEnabledColorScheme(
 
228
                                                                                                        SubstanceLookAndFeel
 
229
                                                                                                                        .getDecorationType(textField))));
 
230
                                }
 
231
                        }
 
232
                });
 
233
        }
 
234
 
 
235
        @Override
 
236
        public boolean isInside(MouseEvent me) {
 
237
                if (!SubstanceLookAndFeel.isCurrentLookAndFeel()) {
 
238
                        return false;
 
239
                }
 
240
                Shape contour = SubstanceOutlineUtilities.getBaseOutline(
 
241
                                this.textField, 2.0f * SubstanceSizeUtils
 
242
                                                .getClassicButtonCornerRadius(SubstanceSizeUtils
 
243
                                                                .getComponentFontSize(this.textField)), null);
 
244
                return contour.contains(me.getPoint());
 
245
        }
 
246
 
 
247
        @Override
 
248
        public StateTransitionTracker getTransitionTracker() {
 
249
                return this.stateTransitionTracker;
 
250
        }
 
251
}