File: XPStyle.java

package info (click to toggle)
libjide-oss-java 3.7.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 13,448 kB
  • sloc: java: 91,177; xml: 661; makefile: 35
file content (691 lines) | stat: -rw-r--r-- 25,319 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/**
 * Copied from JDK source code and modified to provide additional integration between JIDE components and native windows L&F.
 */
package com.jidesoft.plaf.windows;

import com.jidesoft.plaf.windows.TMSchema.Part;
import com.jidesoft.plaf.windows.TMSchema.Prop;
import com.jidesoft.plaf.windows.TMSchema.State;
import com.jidesoft.plaf.windows.TMSchema.TypeEnum;
import com.jidesoft.utils.ReflectionUtils;
import sun.awt.image.SunWritableRaster;
import sun.security.action.GetPropertyAction;
import sun.swing.CachedPainter;

import javax.swing.*;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.InsetsUIResource;
import javax.swing.plaf.UIResource;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.security.AccessController;
import java.util.HashMap;


/**
 * Implements Windows XP Styles for the Windows Look and Feel.
 *
 * @author Leif Samuelsson
 */
public class XPStyle {
    // Singleton instance of this class
    private static XPStyle xp;

    // Singleton instance of SkinPainter
    private static SkinPainter skinPainter = new SkinPainter();

    private static Boolean themeActive = null;

    private HashMap<String, Border> borderMap;
    private HashMap<String, Color> colorMap;

    private boolean flatMenus;

    static {
        invalidateStyle();

        // make sure whenever L&F changes, we clear the xpstyle.
        UIManager.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("lookAndFeel".equals(evt.getPropertyName()))
                    invalidateStyle();
            }
        });
    }

    /**
     * Static method for clearing the hashmap and loading the current XP style and theme
     */
    public static synchronized void invalidateStyle() {
        xp = null;
        themeActive = null;
        skinPainter.flush();
    }

    /**
     * Get the singleton instance of this class
     *
     * @return the singleton instance of this class or null if XP styles are not active or if this is not Windows XP
     */
    public static synchronized XPStyle getXP() {
        if (themeActive == null) {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            themeActive =
                    (Boolean) toolkit.getDesktopProperty("win.xpstyle.themeActive");
            if (themeActive == null) {
                themeActive = Boolean.FALSE;
            }
            if (themeActive.booleanValue()) {
                GetPropertyAction propertyAction =
                        new GetPropertyAction("swing.noxp");
            }
        }
        return xp = null;
    }

    public static boolean isVista() {
        XPStyle xp = XPStyle.getXP();
        return (xp != null && xp.isSkinDefined(null, Part.CP_DROPDOWNBUTTONRIGHT));
    }

    /**
     * Get a named <code>String</code> value from the current style
     *
     * @param part  a <code>Part</code>
     * @param state a <code>String</code>
     * @param prop  a <code>String</code>
     * @return a <code>String</code> or null if key is not found in the current style
     * <p/>
     * This is currently only used by WindowsInternalFrameTitlePane for painting title foreground and can be removed
     * when no longer needed
     */
    public String getString(Component c, Part part, State state, Prop prop) {
        return getTypeEnumName(c, part, state, prop);
    }

    public TypeEnum getTypeEnum(Component c, Part part, State state, Prop prop) {
        int enumValue = 0;
        return TypeEnum.getTypeEnum(prop, enumValue);
    }

    private static String getTypeEnumName(Component c, Part part, State state, Prop prop) {
        int enumValue = 0;
        if (enumValue == -1) {
            return null;
        }
        return TypeEnum.getTypeEnum(prop, enumValue).getName();
    }


    /**
     * Get a named <code>int</code> value from the current style
     *
     * @param part a <code>Part</code>
     * @return an <code>int</code> or null if key is not found in the current style
     */
    public int getInt(Component c, Part part, State state, Prop prop, int fallback) {
        return 0;
    }

    /**
     * Get a named <code>Dimension</code> value from the current style
     *
     * @return a <code>Dimension</code> or null if key is not found in the current style
     * <p/>
     * This is currently only used by WindowsProgressBarUI and the value should probably be cached there instead of
     * here.
     */
    public Dimension getDimension(Component c, Part part, State state, Prop prop) {
        return null;
    }

    /**
     * Get a named <code>Point</code> (e.g. a location or an offset) value from the current style
     *
     * @return a <code>Point</code> or null if key is not found in the current style
     * <p/>
     * This is currently only used by WindowsInternalFrameTitlePane for painting title foregound and can be removed when
     * no longer needed
     */
    public Point getPoint(Component c, Part part, State state, Prop prop) {
        Dimension d = null;
        if (d != null) {
            return new Point(d.width, d.height);
        }
        else {
            return null;
        }
    }

    /**
     * Get a named <code>Insets</code> value from the current style
     *
     * @return an <code>Insets</code> object or null if key is not found in the current style
     * <p/>
     * This is currently only used to create borders and by WindowsInternalFrameTitlePane for painting title foregound.
     * The return value is already cached in those places.
     */
    public Insets getMargin(Component c, Part part, State state, Prop prop) {
        return null;
    }


    /**
     * Get a named <code>Color</code> value from the current style
     *
     * @return a <code>Color</code> or null if key is not found in the current style
     */
    public synchronized Color getColor(Skin skin, Prop prop, Color fallback) {
        String key = skin.toString() + "." + prop.name();
        Part part = skin.part;
        Color color = colorMap.get(key);
        if (color == null) {
            color = null;
            if (color != null) {
                color = new ColorUIResource(color);
                colorMap.put(key, color);
            }
        }
        return (color != null) ? color : fallback;
    }

    Color getColor(Component c, Part part, State state, Prop prop, Color fallback) {
        return getColor(new Skin(c, part, state), prop, fallback);
    }


    /**
     * Get a named <code>Border</code> value from the current style
     *
     * @param part a <code>Part</code>
     * @return a <code>Border</code> or null if key is not found in the current style or if the style for the particular
     * part is not defined as "borderfill".
     */
    public synchronized Border getBorder(Component c, Part part) {
        if (part == Part.MENU) {
            // Special case because XP has no skin for menus
            if (flatMenus) {
                // TODO: The classic border uses this color, but we should
                // create a new UI property called "PopupMenu.borderColor"
                // instead.
                return new XPFillBorder(UIManager.getColor("InternalFrame.borderShadow"),
                        1);
            }
            else {
                return null;    // Will cause L&F to use classic border
            }
        }
        Skin skin = new Skin(c, part, null);
        Border border = borderMap.get(skin.string);
        if (border == null) {
            String bgType = getTypeEnumName(c, part, null, Prop.BGTYPE);
            if ("borderfill".equalsIgnoreCase(bgType)) {
                int thickness = getInt(c, part, null, Prop.BORDERSIZE, 1);
                Color color = getColor(skin, Prop.BORDERCOLOR, Color.black);
                border = new XPFillBorder(color, thickness);
                if (part == Part.CP_COMBOBOX) {
                    border = new XPStatefulFillBorder(color, thickness, part, Prop.BORDERCOLOR);
                }
            }
            else if ("imagefile".equalsIgnoreCase(bgType)) {
                Insets m = getMargin(c, part, null, Prop.SIZINGMARGINS);
                if (m != null) {
                    if (getBoolean(c, part, null, Prop.BORDERONLY)) {
                        border = new XPImageBorder(c, part);
                    }
                    else if (part == Part.CP_COMBOBOX) {
                        border = new EmptyBorder(1, 1, 1, 1);
                    }
                    else {
                        if (part == Part.TP_BUTTON) {
                            border = new XPEmptyBorder(new Insets(3, 3, 3, 3));
                        }
                        else {
                            border = new XPEmptyBorder(m);
                        }
                    }
                }
            }
            if (border != null) {
                borderMap.put(skin.string, border);
            }
        }
        return border;
    }

    private class XPFillBorder extends LineBorder implements UIResource {
        XPFillBorder(Color color, int thickness) {
            super(color, thickness);
        }

        public Insets getBorderInsets(Component c, Insets insets) {
            Insets margin = null;
            //
            // Ideally we'd have an interface defined for classes which
            // support margins (to avoid this hackery), but we've
            // decided against it for simplicity
            //
            if (c instanceof AbstractButton) {
                margin = ((AbstractButton) c).getMargin();
            }
            else if (c instanceof JToolBar) {
                margin = ((JToolBar) c).getMargin();
            }
            else if (c instanceof JTextComponent) {
                margin = ((JTextComponent) c).getMargin();
            }
            insets.top = (margin != null ? margin.top : 0) + thickness;
            insets.left = (margin != null ? margin.left : 0) + thickness;
            insets.bottom = (margin != null ? margin.bottom : 0) + thickness;
            insets.right = (margin != null ? margin.right : 0) + thickness;

            return insets;
        }
    }

    private class XPStatefulFillBorder extends XPFillBorder {
        private final Part part;
        private final Prop prop;

        XPStatefulFillBorder(Color color, int thickness, Part part, Prop prop) {
            super(color, thickness);
            this.part = part;
            this.prop = prop;
        }

        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            State state = State.NORMAL;
            // special casing for comboboxes.
            // there may be more special cases in the future
            if (c instanceof JComboBox) {
                JComboBox cb = (JComboBox) c;
                // note. in the future this should be replaced with a call
                // to BasicLookAndFeel.getUIOfType()
//                    state = wcb.getXPComboBoxState(cb);
            }
            lineColor = getColor(c, part, state, prop, Color.black);
            super.paintBorder(c, g, x, y, width, height);
        }
    }

    private class XPImageBorder extends AbstractBorder implements UIResource {
        Skin skin;

        XPImageBorder(Component c, Part part) {
            this.skin = getSkin(c, part);
        }

        public void paintBorder(Component c, Graphics g,
                                int x, int y, int width, int height) {
            skin.paintSkin(g, x, y, width, height, null);
        }

//        public Insets getBorderInsets(Component c, Insets insets) {
//            Insets margin = null;
//            Insets borderInsets = skin.getContentMargin();
//            if (borderInsets == null) {
//                borderInsets = new Insets(0, 0, 0, 0);
//            }
//            //
//            // Ideally we'd have an interface defined for classes which
//            // support margins (to avoid this hackery), but we've
//            // decided against it for simplicity
//            //
//            if (c instanceof AbstractButton) {
//                margin = ((AbstractButton) c).getMargin();
//            }
//            else if (c instanceof JToolBar) {
//                margin = ((JToolBar) c).getMargin();
//            }
//            else if (c instanceof JTextComponent) {
//                margin = ((JTextComponent) c).getMargin();
//            }
//            insets.top = (margin != null ? margin.top : 0) + borderInsets.top;
//            insets.left = (margin != null ? margin.left : 0) + borderInsets.left;
//            insets.bottom = (margin != null ? margin.bottom : 0) + borderInsets.bottom;
//            insets.right = (margin != null ? margin.right : 0) + borderInsets.right;
//
//            return insets;
//        }
    }

    private class XPEmptyBorder extends EmptyBorder implements UIResource {
        XPEmptyBorder(Insets m) {
            super(m.top + 2, m.left + 2, m.bottom + 2, m.right + 2);
        }

        public Insets getBorderInsets(Component c, Insets insets) {
            insets = super.getBorderInsets(c, insets);

            Insets margin = null;
            if (c instanceof AbstractButton) {
                Insets m = ((AbstractButton) c).getMargin();
                // if this is a toolbar button then ignore getMargin()
                // and subtract the padding added by the constructor
                if (c.getParent() instanceof JToolBar
                        && !(c instanceof JRadioButton)
                        && !(c instanceof JCheckBox)
                        && m instanceof InsetsUIResource) {
                    insets.top -= 2;
                    insets.left -= 2;
                    insets.bottom -= 2;
                    insets.right -= 2;
                }
                else {
                    margin = m;
                }
            }
            else if (c instanceof JToolBar) {
                margin = ((JToolBar) c).getMargin();
            }
            else if (c instanceof JTextComponent) {
                margin = ((JTextComponent) c).getMargin();
            }
            if (margin != null) {
                insets.top = margin.top + 2;
                insets.left = margin.left + 2;
                insets.bottom = margin.bottom + 2;
                insets.right = margin.right + 2;
            }
            return insets;
        }
    }

    public boolean isSkinDefined(Component c, Part part) {
        return false;
    }


    /**
     * Get a <code>Skin</code> object from the current style for a named part (component type)
     *
     * @param part a <code>Part</code>
     * @return a <code>Skin</code> object
     */
    public synchronized Skin getSkin(Component c, Part part) {
        assert isSkinDefined(c, part) : "part " + part + " is not defined";
        return new Skin(c, part, null);
    }


    long getThemeTransitionDuration(Component c, Part part, State stateFrom,
                                    State stateTo, Prop prop) {
        return 0;
    }


    /**
     * A class which encapsulates attributes for a given part (component type) and which provides methods for painting
     * backgrounds and glyphs
     */
    public static class Skin {
        final Component component;
        final Part part;
        final State state;

        private final String string;
        private Dimension size = null;

        Skin(Component component, Part part) {
            this(component, part, null);
        }

        Skin(Part part, State state) {
            this(null, part, state);
        }

        Skin(Component component, Part part, State state) {
            this.component = component;
            this.part = part;
            this.state = state;

            String str = part.getControlName(component) + "." + part.name();
            if (state != null) {
                str += "(" + state.name() + ")";
            }
            string = str;
        }

//        Insets getContentMargin() {
//            /* idk: it seems margins are the same for all 'big enough'
//             * bounding rectangles.
//             */
//            int boundingWidth = 100;
//            int boundingHeight = 100;
//
//            return ThemeReader.getThemeBackgroundContentMargins(
//                    part.getControlName(null), part.getValue(),
//                    0, boundingWidth, boundingHeight);
//        }

        private int getWidth(State state) {
            if (size == null) {
                size = getPartSize(part, state);
            }
            return size.width;
        }

        int getWidth() {
            return getWidth((state != null) ? state : State.NORMAL);
        }

        private int getHeight(State state) {
            if (size == null) {
                size = getPartSize(part, state);
            }
            return size.height;
        }

        int getHeight() {
            return getHeight((state != null) ? state : State.NORMAL);
        }

        public String toString() {
            return string;
        }

        public boolean equals(Object obj) {
            return (obj instanceof Skin && ((Skin) obj).string.equals(string));
        }

        public int hashCode() {
            return string.hashCode();
        }

        /**
         * Paint a skin at x, y.
         *
         * @param g     the graphics context to use for painting
         * @param dx    the destination <i>x</i> coordinate
         * @param dy    the destination <i>y</i> coordinate
         * @param state which state to paint
         */
        public void paintSkin(Graphics g, int dx, int dy, State state) {
            if (state == null) {
                state = this.state;
            }
            paintSkin(g, dx, dy, getWidth(state), getHeight(state), state);
        }

        /**
         * Paint a skin in an area defined by a rectangle.
         *
         * @param g     the graphics context to use for painting
         * @param r     a <code>Rectangle</code> defining the area to fill, may cause the image to be stretched or
         *              tiled
         * @param state which state to paint
         */
        public void paintSkin(Graphics g, Rectangle r, State state) {
            paintSkin(g, r.x, r.y, r.width, r.height, state);
        }

        /**
         * Paint a skin at a defined position and size This method supports animation.
         *
         * @param g     the graphics context to use for painting
         * @param dx    the destination <i>x</i> coordinate
         * @param dy    the destination <i>y</i> coordinate
         * @param dw    the width of the area to fill, may cause the image to be stretched or tiled
         * @param dh    the height of the area to fill, may cause the image to be stretched or tiled
         * @param state which state to paint
         */
        public void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state) {
            paintSkinRaw(g, dx, dy, dw, dh, state);
        }

        /**
         * Paint a skin at a defined position and size. This method does not trigger animation. It is needed for the
         * animation support.
         *
         * @param g     the graphics context to use for painting
         * @param dx    the destination <i>x</i> coordinate.
         * @param dy    the destination <i>y</i> coordinate.
         * @param dw    the width of the area to fill, may cause the image to be stretched or tiled
         * @param dh    the height of the area to fill, may cause the image to be stretched or tiled
         * @param state which state to paint
         */
        public void paintSkinRaw(Graphics g, int dx, int dy, int dw, int dh, State state) {
            skinPainter.paint(null, g, dx, dy, dw, dh, this, state);
        }

        /**
         * Paint a skin at a defined position and size
         *
         * @param g          the graphics context to use for painting
         * @param dx         the destination <i>x</i> coordinate
         * @param dy         the destination <i>y</i> coordinate
         * @param dw         the width of the area to fill, may cause the image to be stretched or tiled
         * @param dh         the height of the area to fill, may cause the image to be stretched or tiled
         * @param state      which state to paint
         * @param borderFill should test if the component uses a border fill and skip painting if it is
         */
        public void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state,
                              boolean borderFill) {
            if (borderFill && "borderfill".equals(getTypeEnumName(component, part,
                    state, Prop.BGTYPE))) {
                return;
            }
            skinPainter.paint(null, g, dx, dy, dw, dh, this, state);
        }
    }

    private static class SkinPainter extends CachedPainter {
        SkinPainter() {
            super(30);
            flush();
        }

        public void flush() {
            super.flush();
        }

        protected void paintToImage(Component c, Image image, Graphics g,
                                    int w, int h, Object[] args) {
            // copied from JDK7 XPStyle. To make the code compilable under JDk6, we use RefectionUtils
            boolean accEnabled = false;
            Skin skin = (Skin) args[0];
            Part part = skin.part;
            State state = (State) args[1];
            if (state == null) {
                state = skin.state;
            }
            if (c == null) {
                c = skin.component;
            }
            BufferedImage bi = (BufferedImage) image;

            WritableRaster raster = bi.getRaster();
            DataBufferInt dbi = (DataBufferInt) raster.getDataBuffer();
            // Note that stealData() requires a markDirty() afterwards
            // since we modify the data in it.
        }

        protected Image createImage(Component c, int w, int h,
                                    GraphicsConfiguration config, Object[] args) {
            return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        }
    }

    static class GlyphButton extends JButton {
        private Skin skin;

        public GlyphButton(Component parent, Part part) {
            XPStyle xp = getXP();
            skin = xp.getSkin(parent, part);
            setBorder(null);
            setContentAreaFilled(false);
            setMinimumSize(new Dimension(5, 5));
            setPreferredSize(new Dimension(16, 16));
            setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
        }

        public boolean isFocusTraversable() {
            return false;
        }

        protected State getState() {
            State state = State.NORMAL;
            if (!isEnabled()) {
                state = State.DISABLED;
            }
            else if (getModel().isPressed()) {
                state = State.PRESSED;
            }
            else if (getModel().isRollover()) {
                state = State.HOT;
            }
            return state;
        }

        public void paintComponent(Graphics g) {
            Dimension d = getSize();
            skin.paintSkin(g, 0, 0, d.width, d.height, getState());
        }

        public void setPart(Component parent, Part part) {
            XPStyle xp = getXP();
            skin = xp.getSkin(parent, part);
            revalidate();
            repaint();
        }

        protected void paintBorder(Graphics g) {
        }


    }

    // Private constructor
    private XPStyle() {
        flatMenus = getSysBoolean(Prop.FLATMENUS);

        colorMap = new HashMap<String, Color>();
        borderMap = new HashMap<String, Border>();
        // Note: All further access to the maps must be synchronized
    }


    private boolean getBoolean(Component c, Part part, State state, Prop prop) {
        return false;
    }


    static Dimension getPartSize(Part part, State state) {
        return null;
    }

    private static boolean getSysBoolean(Prop prop) {
        // We can use any widget name here, I guess.
        return false;
    }
}