~ubuntu-branches/ubuntu/trusty/drmips/trusty-backports

« back to all changes in this revision

Viewing changes to src/pc/DrMIPS/src/org/fife/ui/rtextarea/ChangeableHighlightPainter.java

  • Committer: Package Import Robot
  • Author(s): Bruno Nova
  • Date: 2014-09-27 12:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20140927122417-2gadkwt9k0u7j4zu
Tags: upstream-1.2.3
Import upstream version 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 11/10/2004
 
3
 *
 
4
 * ChangableHighlightPainter.java - A highlight painter whose color you can
 
5
 * change.
 
6
 * 
 
7
 * This library is distributed under a modified BSD license.  See the included
 
8
 * RSyntaxTextArea.License.txt file for details.
 
9
 */
 
10
package org.fife.ui.rtextarea;
 
11
 
 
12
import java.awt.AlphaComposite;
 
13
import java.awt.Color;
 
14
import java.awt.Composite;
 
15
import java.awt.Graphics;
 
16
import java.awt.Graphics2D;
 
17
import java.awt.Paint;
 
18
import java.awt.Rectangle;
 
19
import java.awt.Shape;
 
20
import java.awt.SystemColor;
 
21
import java.io.IOException;
 
22
import java.io.ObjectInputStream;
 
23
import java.io.ObjectOutputStream;
 
24
import java.io.Serializable;
 
25
import javax.swing.plaf.TextUI;
 
26
import javax.swing.text.BadLocationException;
 
27
import javax.swing.text.JTextComponent;
 
28
import javax.swing.text.LayeredHighlighter;
 
29
import javax.swing.text.Position;
 
30
import javax.swing.text.View;
 
31
 
 
32
 
 
33
/**
 
34
 * An extension of <code>LayerPainter</code> that allows the user to
 
35
 * change several of its properties:
 
36
 *
 
37
 * <ul>
 
38
 *   <li>Its color/fill style (can use a <code>GradientPaint</code>, for
 
39
 *       example).</li>
 
40
 *   <li>Whether the edges of a painted highlight are rounded.</li>
 
41
 *   <li>Whether painted highlights have translucency.</li>
 
42
 * </ul>
 
43
 *
 
44
 * @author Robert Futrell
 
45
 * @version 0.6
 
46
 */
 
47
public class ChangeableHighlightPainter
 
48
                extends LayeredHighlighter.LayerPainter implements Serializable {
 
49
 
 
50
 
 
51
        /**
 
52
         * The <code>Paint</code>/<code>Color</code> of this highlight.
 
53
         */
 
54
        private Paint paint;
 
55
 
 
56
        /**
 
57
         * Whether selections have rounded edges.
 
58
         */
 
59
        private boolean roundedEdges;
 
60
 
 
61
        /**
 
62
         * The alpha composite used to render with translucency.
 
63
         */
 
64
        private transient AlphaComposite alphaComposite;
 
65
 
 
66
        /**
 
67
         * The alpha value used in computing translucency.  This should stay in the
 
68
         * range <code>0.0f</code> (completely invisible) to <code>1.0f</code>
 
69
         * (completely opaque).
 
70
         */
 
71
        private float alpha;
 
72
 
 
73
 
 
74
        private static final int ARCWIDTH                               = 8;
 
75
        private static final int ARCHEIGHT                              = 8;
 
76
 
 
77
 
 
78
        /**
 
79
         * Creates a new <code>ChangableHighlightPainter</code> that paints
 
80
         * highlights with the text area's selection color (i.e., behaves exactly
 
81
         * like
 
82
         * <code>javax.swing.text.DefaultHighlighter.DefaultHighlightPainter
 
83
         * </code>).
 
84
         */
 
85
        public ChangeableHighlightPainter() {
 
86
                this(null);
 
87
        }
 
88
 
 
89
 
 
90
        /**
 
91
         * Creates a new highlight painter using the specified <code>Paint</code>
 
92
         * without rounded edges.
 
93
         *
 
94
         * @param paint The <code>Paint</code> (usually a
 
95
         *        <code>java.awt.Color</code>) with which to paint the
 
96
         *        highlights.
 
97
         */
 
98
        public ChangeableHighlightPainter(Paint paint) {
 
99
                this(paint, false);
 
100
        }
 
101
 
 
102
 
 
103
        /**
 
104
         * Creates a new highlight painter.
 
105
         *
 
106
         * @param paint The <code>Paint</code> (usually a
 
107
         *        <code>java.awt.Color</code>) with which to paint the
 
108
         *        highlights.
 
109
         * @param rounded Whether to use rounded edges on the highlights.
 
110
         */
 
111
        public ChangeableHighlightPainter(Paint paint, boolean rounded) {
 
112
                this(paint, rounded, 1.0f);
 
113
        }
 
114
 
 
115
 
 
116
        /**
 
117
         * Creates a new highlight painter.
 
118
         *
 
119
         * @param paint The <code>Paint</code> (usually a
 
120
         *        <code>java.awt.Color</code>) with which to paint the
 
121
         *        highlights.
 
122
         * @param rounded Whether to use rounded edges on the highlights.
 
123
         * @param alpha The alpha value to use when painting highlights.  This
 
124
         *        value should be in the range <code>0.0f</code> (completely
 
125
         *        transparent) through <code>1.0f</code> (opaque).
 
126
         */
 
127
        public ChangeableHighlightPainter(Paint paint, boolean rounded,
 
128
                                                                                        float alpha) {
 
129
                setPaint(paint);
 
130
                setRoundedEdges(rounded);
 
131
                setAlpha(alpha);
 
132
        }
 
133
 
 
134
 
 
135
        /**
 
136
         * Returns the alpha value used in computing the translucency of these
 
137
         * highlights.  A value of <code>1.0f</code> (the default) means that no
 
138
         * translucency is used; there is no performance hit for this value.  For
 
139
         * all other values (<code>[0.0f..1.0f)</code>), there will be a
 
140
         * performance hit.
 
141
         *
 
142
         * @return The alpha value.
 
143
         * @see #setAlpha
 
144
         */
 
145
        public float getAlpha() {
 
146
                return alpha;
 
147
        }
 
148
 
 
149
 
 
150
        /**
 
151
         * Returns the alpha composite to use when rendering highlights with this
 
152
         * painter.
 
153
         *
 
154
         * @return The alpha composite.
 
155
         */
 
156
        private AlphaComposite getAlphaComposite() {
 
157
                if (alphaComposite==null)
 
158
                        alphaComposite = AlphaComposite.getInstance(
 
159
                                                                        AlphaComposite.SRC_OVER, alpha);
 
160
                return alphaComposite;
 
161
        }
 
162
 
 
163
 
 
164
        /**
 
165
         * Returns the <code>Paint</code> (usually a <code>java.awt.Color</code>)
 
166
         * being used to paint highlights.
 
167
         *
 
168
         * @return The <code>Paint</code>.
 
169
         * @see #setPaint
 
170
         */
 
171
        public Paint getPaint() {
 
172
                return paint;
 
173
        }
 
174
 
 
175
 
 
176
        /**
 
177
         * Returns whether rounded edges are used when painting selections with
 
178
         * this highlight painter.
 
179
         *
 
180
         * @return Whether rounded edges are used.
 
181
         * @see #setRoundedEdges
 
182
         */
 
183
        public boolean getRoundedEdges() {
 
184
                return roundedEdges;
 
185
        }
 
186
 
 
187
 
 
188
        /**
 
189
         * Paints a highlight.
 
190
         *
 
191
         * @param g the graphics context
 
192
         * @param offs0 the starting model offset >= 0
 
193
         * @param offs1 the ending model offset >= offs1
 
194
         * @param bounds the bounding box for the highlight
 
195
         * @param c the editor
 
196
         */
 
197
        public void paint(Graphics g, int offs0, int offs1, Shape bounds,
 
198
                                        JTextComponent c) {
 
199
 
 
200
                Rectangle alloc = bounds.getBounds();
 
201
 
 
202
                // Set up translucency if necessary.
 
203
                Graphics2D g2d = (Graphics2D)g;
 
204
                Composite originalComposite = null;
 
205
                if (getAlpha()<1.0f) {
 
206
                        originalComposite = g2d.getComposite();
 
207
                        g2d.setComposite(getAlphaComposite());
 
208
                }
 
209
 
 
210
                try {
 
211
 
 
212
                        // Determine locations.
 
213
                        TextUI mapper = c.getUI();
 
214
                        Rectangle p0 = mapper.modelToView(c, offs0);
 
215
                        Rectangle p1 = mapper.modelToView(c, offs1);
 
216
                        Paint paint = getPaint();
 
217
                        if (paint==null)
 
218
                                g2d.setColor(c.getSelectionColor());
 
219
                        else
 
220
                                g2d.setPaint(paint);
 
221
 
 
222
                        // Entire highlight is on one line.
 
223
                        if (p0.y == p1.y) {
 
224
                                Rectangle r = p0.union(p1);
 
225
                                g2d.fillRect(r.x, r.y, r.width, r.height);
 
226
                        }
 
227
 
 
228
                        // Highlight spans lines.
 
229
                        else {
 
230
                                int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
 
231
                                g2d.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
 
232
                                if ((p0.y + p0.height) != p1.y) {
 
233
                                        g2d.fillRect(alloc.x, p0.y + p0.height, alloc.width, 
 
234
                                                                p1.y - (p0.y + p0.height));
 
235
                                }
 
236
                                g2d.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
 
237
                        }
 
238
 
 
239
                } catch (BadLocationException e) {
 
240
                        // Never happens.
 
241
                        e.printStackTrace();
 
242
                } finally {
 
243
                        // Restore state from before translucency if necessary.
 
244
                        if (getAlpha()<1.0f)
 
245
                                g2d.setComposite(originalComposite);
 
246
                }
 
247
 
 
248
        }
 
249
 
 
250
        
 
251
        /**
 
252
         * Paints a portion of a highlight.
 
253
         *
 
254
         * @param g the graphics context
 
255
         * @param offs0 the starting model offset >= 0
 
256
         * @param offs1 the ending model offset >= offs1
 
257
         * @param bounds the bounding box of the view, which is not
 
258
         *        necessarily the region to paint.
 
259
         * @param c the editor
 
260
         * @param view View painting for
 
261
         * @return region drawing occurred in
 
262
         */
 
263
        @Override
 
264
        public Shape paintLayer(Graphics g, int offs0, int offs1,
 
265
                                                Shape bounds, JTextComponent c, View view) {
 
266
 
 
267
 
 
268
                // Set up translucency if necessary.
 
269
                Graphics2D g2d = (Graphics2D)g;
 
270
                Composite originalComposite = null;
 
271
                if (getAlpha()<1.0f) {
 
272
                        originalComposite = g2d.getComposite();
 
273
                        g2d.setComposite(getAlphaComposite());
 
274
                }
 
275
 
 
276
                // Set the color (our own if defined, otherwise text area's).
 
277
                Paint paint = getPaint();
 
278
                if (paint==null)
 
279
                        g2d.setColor(c.getSelectionColor());
 
280
                else
 
281
                        g2d.setPaint(paint);
 
282
 
 
283
                // This special case isn't needed for most standard Swing Views (which
 
284
                // always return a width of 1 for modelToView() calls), but it is
 
285
                // needed for RSTA views, which actually return the width of chars for
 
286
                // modelToView calls.  But this should be faster anyway, as we
 
287
                // short-circuit and do only one modelToView() for one offset.
 
288
                if (offs0==offs1) {
 
289
                        try {
 
290
                                Shape s = view.modelToView(offs0, bounds,
 
291
                                                                                        Position.Bias.Forward);
 
292
                                Rectangle r = s.getBounds();
 
293
                                g.drawLine(r.x, r.y, r.x, r.y+r.height);
 
294
                                return r;
 
295
                        } catch (BadLocationException ble) {
 
296
                                ble.printStackTrace(); // Never happens
 
297
                                return null;
 
298
                        }
 
299
                }
 
300
 
 
301
                // Contained in view, can just use bounds.
 
302
                if (offs0==view.getStartOffset() && offs1==view.getEndOffset()) {
 
303
 
 
304
                        Rectangle alloc;
 
305
                        if (bounds instanceof Rectangle)
 
306
                                alloc = (Rectangle)bounds;
 
307
                        else
 
308
                                alloc = bounds.getBounds();
 
309
                        
 
310
                        g2d.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
 
311
 
 
312
                        // Restore state from before translucency if necessary.
 
313
                        if (getAlpha()<1.0f)
 
314
                                g2d.setComposite(originalComposite);
 
315
 
 
316
                        return alloc;
 
317
 
 
318
                }
 
319
 
 
320
                // Should only render part of View.
 
321
                else {
 
322
 
 
323
                        try {
 
324
 
 
325
                                Shape shape = view.modelToView(offs0, Position.Bias.Forward,
 
326
                                                                                offs1,Position.Bias.Backward,
 
327
                                                                                bounds);
 
328
                                Rectangle r = (shape instanceof Rectangle) ?
 
329
                                                                (Rectangle)shape : shape.getBounds();
 
330
                                if (roundedEdges) {
 
331
                                        g2d.fillRoundRect(r.x,r.y, r.width,r.height, ARCWIDTH,
 
332
                                                                                                        ARCHEIGHT);
 
333
                                }
 
334
                                else {
 
335
                                        g2d.fillRect(r.x, r.y, r.width, r.height);
 
336
                                }
 
337
 
 
338
                                // Restore state from before translucency if necessary.
 
339
                                if (getAlpha()<1.0f)
 
340
                                        g2d.setComposite(originalComposite);
 
341
 
 
342
                                return r;
 
343
 
 
344
                        } catch (BadLocationException ble) {
 
345
                                ble.printStackTrace();
 
346
                        } finally {
 
347
                                // Restore state from before translucency if necessary.
 
348
                                if (getAlpha()<1.0f)
 
349
                                        g2d.setComposite(originalComposite);
 
350
                        }
 
351
 
 
352
                }
 
353
 
 
354
                // Only if exception
 
355
                return null;
 
356
 
 
357
        }
 
358
 
 
359
 
 
360
        /**
 
361
         * Deserializes a painter.
 
362
         *
 
363
         * @param s The stream to read from.
 
364
         * @throws ClassNotFoundException
 
365
         * @throws IOException
 
366
         */
 
367
        private void readObject(ObjectInputStream s)
 
368
                                                throws ClassNotFoundException, IOException {
 
369
                s.defaultReadObject();
 
370
                // We cheat and always serialize the Paint as a Color.  "-1" means
 
371
                // no Paint (i.e. use system selection color when painting).
 
372
                int rgb = s.readInt();
 
373
                paint = rgb==-1 ? null : new Color(rgb);
 
374
                alphaComposite = null; // Keep FindBugs happy.  This will get set later
 
375
        }
 
376
 
 
377
 
 
378
        /**
 
379
         * Sets the alpha value used in rendering highlights.  If this value is
 
380
         * <code>1.0f</code> (the default), the highlights are rendered completely
 
381
         * opaque.  This behavior matches that of
 
382
         * <code>DefaultHighlightPainter</code> and imposes no performance hit.  If
 
383
         * this value is below <code>1.0f</code>, it represents how opaque the
 
384
         * highlight will be.  There will be a small performance hit for values
 
385
         * less than <code>1.0f</code>.
 
386
         *
 
387
         * @param alpha The new alpha value to use for transparency.
 
388
         * @see #getAlpha
 
389
         */
 
390
        public void setAlpha(float alpha) {
 
391
                this.alpha = alpha;
 
392
                this.alpha = Math.max(alpha, 0.0f);
 
393
                this.alpha = Math.min(1.0f, alpha);
 
394
                alphaComposite = null; // So it is recreated with new alpha.
 
395
        }
 
396
 
 
397
 
 
398
        /**
 
399
         * Sets the <code>Paint</code> (usually a <code>java.awt.Color</code>)
 
400
         * used to paint this highlight.
 
401
         *
 
402
         * @param paint The new <code>Paint</code>.
 
403
         * @see #getPaint
 
404
         */
 
405
        public void setPaint(Paint paint) {
 
406
                this.paint = paint;
 
407
        }
 
408
 
 
409
 
 
410
        /**
 
411
         * Sets whether rounded edges are used when painting this highlight.
 
412
         *
 
413
         * @param rounded Whether rounded edges should be used.
 
414
         * @see #getRoundedEdges
 
415
         */
 
416
        public void setRoundedEdges(boolean rounded) {
 
417
                roundedEdges = rounded;
 
418
        }
 
419
 
 
420
 
 
421
        /**
 
422
         * Serializes this painter.
 
423
         *
 
424
         * @param s The stream to write to.
 
425
         * @throws IOException If an IO error occurs.
 
426
         */
 
427
        private void writeObject(ObjectOutputStream s) throws IOException {
 
428
                s.defaultWriteObject();
 
429
                int rgb = -1; // No Paint -> Use JTextComponent's selection color
 
430
                if (paint!=null) {
 
431
                        // NOTE: We cheat and always serialize the Paint as a Color.
 
432
                        // This is (practically) always the case anyway.
 
433
                        Color c = (paint instanceof Color) ? ((Color)paint) :
 
434
                                                                                        SystemColor.textHighlight;
 
435
                        rgb = c.getRGB();
 
436
                }
 
437
                s.writeInt(rgb);
 
438
        }
 
439
 
 
440
 
 
441
}
 
 
b'\\ No newline at end of file'