~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/SubstanceTextPaneUI.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.plaf.ComponentUI;
 
39
import javax.swing.plaf.UIResource;
 
40
import javax.swing.plaf.basic.BasicTextPaneUI;
 
41
 
 
42
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
 
43
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
44
import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
 
45
import org.pushingpixels.substance.internal.utils.*;
 
46
 
 
47
/**
 
48
 * UI for text panes in <b>Substance</b> look and feel.
 
49
 * 
 
50
 * @author Kirill Grouchnikov
 
51
 */
 
52
public class SubstanceTextPaneUI extends BasicTextPaneUI implements
 
53
                TransitionAwareUI {
 
54
        protected StateTransitionTracker stateTransitionTracker;
 
55
 
 
56
        /**
 
57
         * The associated text pane.
 
58
         */
 
59
        protected JTextPane textPane;
 
60
 
 
61
        /**
 
62
         * Property change listener.
 
63
         */
 
64
        protected PropertyChangeListener substancePropertyChangeListener;
 
65
 
 
66
        /**
 
67
         * Listener for transition animations.
 
68
         */
 
69
        private RolloverTextControlListener substanceRolloverListener;
 
70
 
 
71
        /**
 
72
         * Surrogate button model for tracking the state transitions.
 
73
         */
 
74
        private ButtonModel transitionModel;
 
75
 
 
76
        /*
 
77
         * (non-Javadoc)
 
78
         * 
 
79
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
80
         */
 
81
        public static ComponentUI createUI(JComponent comp) {
 
82
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
83
                return new SubstanceTextPaneUI(comp);
 
84
        }
 
85
 
 
86
        /**
 
87
         * Simple constructor.
 
88
         * 
 
89
         * @param c
 
90
         *            Component (text pane).
 
91
         */
 
92
        public SubstanceTextPaneUI(JComponent c) {
 
93
                super();
 
94
                this.textPane = (JTextPane) c;
 
95
 
 
96
                this.transitionModel = new DefaultButtonModel();
 
97
                this.transitionModel.setArmed(false);
 
98
                this.transitionModel.setSelected(false);
 
99
                this.transitionModel.setPressed(false);
 
100
                this.transitionModel.setRollover(false);
 
101
                this.transitionModel.setEnabled(this.textPane.isEnabled());
 
102
 
 
103
                this.stateTransitionTracker = new StateTransitionTracker(this.textPane,
 
104
                                this.transitionModel);
 
105
        }
 
106
 
 
107
        @Override
 
108
        protected void installListeners() {
 
109
                super.installListeners();
 
110
 
 
111
                this.substanceRolloverListener = new RolloverTextControlListener(
 
112
                                this.textPane, this, this.transitionModel);
 
113
                this.substanceRolloverListener.registerListeners();
 
114
 
 
115
                this.stateTransitionTracker.registerModelListeners();
 
116
                this.stateTransitionTracker.registerFocusListeners();
 
117
 
 
118
                this.substancePropertyChangeListener = new PropertyChangeListener() {
 
119
                        @Override
 
120
            public void propertyChange(PropertyChangeEvent evt) {
 
121
                                if ("font".equals(evt.getPropertyName())) {
 
122
                                        SwingUtilities.invokeLater(new Runnable() {
 
123
                                                @Override
 
124
                        public void run() {
 
125
                                                        // remember the caret location - issue 404
 
126
                                                        int caretPos = textPane.getCaretPosition();
 
127
                                                        textPane.updateUI();
 
128
                                                        textPane.setCaretPosition(caretPos);
 
129
                                                        Container parent = textPane.getParent();
 
130
                                                        if (parent != null) {
 
131
                                                                parent.invalidate();
 
132
                                                                parent.validate();
 
133
                                                        }
 
134
                                                }
 
135
                                        });
 
136
                                }
 
137
 
 
138
                                if ("enabled".equals(evt.getPropertyName())) {
 
139
                                        transitionModel.setEnabled(textPane.isEnabled());
 
140
                                }
 
141
                        }
 
142
                };
 
143
                this.textPane
 
144
                                .addPropertyChangeListener(this.substancePropertyChangeListener);
 
145
        }
 
146
 
 
147
        /*
 
148
         * (non-Javadoc)
 
149
         * 
 
150
         * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
 
151
         */
 
152
        @Override
 
153
        protected void uninstallListeners() {
 
154
                this.stateTransitionTracker.unregisterModelListeners();
 
155
                this.stateTransitionTracker.unregisterFocusListeners();
 
156
 
 
157
                this.textPane
 
158
                                .removePropertyChangeListener(this.substancePropertyChangeListener);
 
159
                this.substancePropertyChangeListener = null;
 
160
 
 
161
                super.uninstallListeners();
 
162
        }
 
163
 
 
164
        /*
 
165
         * (non-Javadoc)
 
166
         * 
 
167
         * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
 
168
         */
 
169
        @Override
 
170
        protected void installDefaults() {
 
171
                super.installDefaults();
 
172
 
 
173
                // support for per-window skins
 
174
                SwingUtilities.invokeLater(new Runnable() {
 
175
                        @Override
 
176
                        public void run() {
 
177
                                if (textPane == null)
 
178
                                        return;
 
179
                                Color foregr = textPane.getForeground();
 
180
                                if ((foregr == null) || (foregr instanceof UIResource)) {
 
181
                                        textPane
 
182
                                                        .setForeground(SubstanceColorUtilities
 
183
                                                                        .getForegroundColor(SubstanceLookAndFeel
 
184
                                                                                        .getCurrentSkin(textPane)
 
185
                                                                                        .getEnabledColorScheme(
 
186
                                                                                                        SubstanceLookAndFeel
 
187
                                                                                                                        .getDecorationType(textPane))));
 
188
                                }
 
189
                        }
 
190
                });
 
191
        }
 
192
 
 
193
        /*
 
194
         * (non-Javadoc)
 
195
         * 
 
196
         * @see
 
197
         * javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
 
198
         */
 
199
        @Override
 
200
        protected void paintBackground(Graphics g) {
 
201
                SubstanceTextUtilities.paintTextCompBackground(g, this.textPane);
 
202
        }
 
203
 
 
204
        @Override
 
205
        public boolean isInside(MouseEvent me) {
 
206
                return true;
 
207
        }
 
208
 
 
209
        @Override
 
210
        public StateTransitionTracker getTransitionTracker() {
 
211
                return this.stateTransitionTracker;
 
212
        }
 
213
}