~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/SubstanceEditorPaneUI.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.BasicEditorPaneUI;
 
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 editor panes in <b>Substance</b> look and feel.
 
49
 * 
 
50
 * @author Kirill Grouchnikov
 
51
 */
 
52
public class SubstanceEditorPaneUI extends BasicEditorPaneUI implements
 
53
                TransitionAwareUI {
 
54
        protected StateTransitionTracker stateTransitionTracker;
 
55
 
 
56
        /**
 
57
         * The associated editor pane.
 
58
         */
 
59
        protected JEditorPane editorPane;
 
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 SubstanceEditorPaneUI(comp);
 
84
        }
 
85
 
 
86
        /**
 
87
         * Simple constructor.
 
88
         * 
 
89
         * @param c
 
90
         *            Component (editor pane).
 
91
         */
 
92
        public SubstanceEditorPaneUI(JComponent c) {
 
93
                super();
 
94
                this.editorPane = (JEditorPane) 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.editorPane.isEnabled());
 
102
 
 
103
                this.stateTransitionTracker = new StateTransitionTracker(
 
104
                                this.editorPane, this.transitionModel);
 
105
        }
 
106
 
 
107
        /*
 
108
         * (non-Javadoc)
 
109
         * 
 
110
         * @see javax.swing.plaf.basic.BasicTextUI#installListeners()
 
111
         */
 
112
        @Override
 
113
        protected void installListeners() {
 
114
                super.installListeners();
 
115
                super.installListeners();
 
116
 
 
117
                this.substanceRolloverListener = new RolloverTextControlListener(
 
118
                                this.editorPane, this, this.transitionModel);
 
119
                this.substanceRolloverListener.registerListeners();
 
120
 
 
121
                this.stateTransitionTracker.registerModelListeners();
 
122
                this.stateTransitionTracker.registerFocusListeners();
 
123
 
 
124
                this.substancePropertyChangeListener = new PropertyChangeListener() {
 
125
                        @Override
 
126
            public void propertyChange(PropertyChangeEvent evt) {
 
127
                                if ("font".equals(evt.getPropertyName())) {
 
128
                                        SwingUtilities.invokeLater(new Runnable() {
 
129
                                                @Override
 
130
                        public void run() {
 
131
                                                        // remember the caret location - issue 404
 
132
                                                        int caretPos = editorPane.getCaretPosition();
 
133
                                                        editorPane.updateUI();
 
134
                                                        editorPane.setCaretPosition(caretPos);
 
135
                                                        Container parent = editorPane.getParent();
 
136
                                                        if (parent != null) {
 
137
                                                                parent.invalidate();
 
138
                                                                parent.validate();
 
139
                                                        }
 
140
                                                }
 
141
                                        });
 
142
                                }
 
143
 
 
144
                                if ("enabled".equals(evt.getPropertyName())) {
 
145
                                        transitionModel.setEnabled(editorPane.isEnabled());
 
146
                                }
 
147
                        }
 
148
                };
 
149
                this.editorPane
 
150
                                .addPropertyChangeListener(this.substancePropertyChangeListener);
 
151
        }
 
152
 
 
153
        /*
 
154
         * (non-Javadoc)
 
155
         * 
 
156
         * @see javax.swing.plaf.basic.BasicTextUI#uninstallListeners()
 
157
         */
 
158
        @Override
 
159
        protected void uninstallListeners() {
 
160
                this.stateTransitionTracker.unregisterModelListeners();
 
161
                this.stateTransitionTracker.unregisterFocusListeners();
 
162
 
 
163
                this.editorPane
 
164
                                .removePropertyChangeListener(this.substancePropertyChangeListener);
 
165
                this.substancePropertyChangeListener = null;
 
166
 
 
167
                super.uninstallListeners();
 
168
        }
 
169
 
 
170
        /*
 
171
         * (non-Javadoc)
 
172
         * 
 
173
         * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
 
174
         */
 
175
        @Override
 
176
        protected void installDefaults() {
 
177
                super.installDefaults();
 
178
 
 
179
                // support for per-window skins
 
180
                SwingUtilities.invokeLater(new Runnable() {
 
181
                        @Override
 
182
                        public void run() {
 
183
                                if (editorPane == null)
 
184
                                        return;
 
185
                                Color foregr = editorPane.getForeground();
 
186
                                if ((foregr == null) || (foregr instanceof UIResource)) {
 
187
                                        editorPane
 
188
                                                        .setForeground(SubstanceColorUtilities
 
189
                                                                        .getForegroundColor(SubstanceLookAndFeel
 
190
                                                                                        .getCurrentSkin(editorPane)
 
191
                                                                                        .getEnabledColorScheme(
 
192
                                                                                                        SubstanceLookAndFeel
 
193
                                                                                                                        .getDecorationType(editorPane))));
 
194
                                }
 
195
                        }
 
196
                });
 
197
        }
 
198
 
 
199
        /*
 
200
         * (non-Javadoc)
 
201
         * 
 
202
         * @see
 
203
         * javax.swing.plaf.basic.BasicTextUI#paintBackground(java.awt.Graphics)
 
204
         */
 
205
        @Override
 
206
        protected void paintBackground(Graphics g) {
 
207
                SubstanceTextUtilities.paintTextCompBackground(g, this.editorPane);
 
208
        }
 
209
 
 
210
        @Override
 
211
        public boolean isInside(MouseEvent me) {
 
212
                return true;
 
213
        }
 
214
 
 
215
        @Override
 
216
        public StateTransitionTracker getTransitionTracker() {
 
217
                return this.stateTransitionTracker;
 
218
        }
 
219
}