~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/looks-2.2.2/src/core/com/jgoodies/looks/plastic/PlasticXPBorders.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2001-2009 JGoodies Karsten Lentzsch. 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 JGoodies Karsten Lentzsch 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
 
 
31
package com.jgoodies.looks.plastic;
 
32
 
 
33
import java.awt.Component;
 
34
import java.awt.Graphics;
 
35
import java.awt.Insets;
 
36
 
 
37
import javax.swing.*;
 
38
import javax.swing.border.AbstractBorder;
 
39
import javax.swing.border.Border;
 
40
import javax.swing.border.CompoundBorder;
 
41
import javax.swing.plaf.BorderUIResource;
 
42
import javax.swing.plaf.UIResource;
 
43
import javax.swing.plaf.basic.BasicBorders;
 
44
import javax.swing.plaf.metal.MetalBorders;
 
45
import javax.swing.plaf.metal.MetalLookAndFeel;
 
46
import javax.swing.text.JTextComponent;
 
47
 
 
48
 
 
49
/**
 
50
 * This class consists of a set of <code>Border</code>s used
 
51
 * by the JGoodies Plastic XP Look and Feel UI delegates.
 
52
 *
 
53
 * @author Karsten Lentzsch
 
54
 * @author Andrej Golovnin
 
55
 * @version $Revision: 1.2 $
 
56
 */
 
57
 
 
58
final class PlasticXPBorders {
 
59
 
 
60
    private PlasticXPBorders() {
 
61
        // Overrides default constructor; prevents instantiation.
 
62
    }
 
63
 
 
64
 
 
65
    // Accessing and Creating Borders ***************************************
 
66
 
 
67
    private static Border comboBoxArrowButtonBorder;
 
68
    private static Border comboBoxEditorBorder;
 
69
    private static Border scrollPaneBorder;
 
70
    private static Border textFieldBorder;
 
71
    private static Border spinnerBorder;
 
72
    private static Border rolloverButtonBorder;
 
73
 
 
74
 
 
75
    /*
 
76
     * Returns a border instance for a <code>JButton</code>.
 
77
     */
 
78
    static Border getButtonBorder(Insets buttonMargin) {
 
79
        return new BorderUIResource.CompoundBorderUIResource(
 
80
                    new XPButtonBorder(buttonMargin),
 
81
                    new BasicBorders.MarginBorder());
 
82
    }
 
83
 
 
84
    /*
 
85
     * Returns a border instance for a <code>JComboBox</code>'s arrow button.
 
86
     */
 
87
    static Border getComboBoxArrowButtonBorder() {
 
88
        if (comboBoxArrowButtonBorder == null) {
 
89
            comboBoxArrowButtonBorder = new CompoundBorder(  // No UIResource
 
90
                                    new XPComboBoxArrowButtonBorder(),
 
91
                                    new BasicBorders.MarginBorder());
 
92
        }
 
93
        return comboBoxArrowButtonBorder;
 
94
    }
 
95
 
 
96
    /*
 
97
     * Returns a border instance for a <code>JComboBox</code>'s editor.
 
98
     */
 
99
    static Border getComboBoxEditorBorder() {
 
100
        if (comboBoxEditorBorder == null) {
 
101
            comboBoxEditorBorder = new CompoundBorder(  // No UIResource
 
102
                                    new XPComboBoxEditorBorder(),
 
103
                                    new BasicBorders.MarginBorder());
 
104
        }
 
105
        return comboBoxEditorBorder;
 
106
    }
 
107
 
 
108
    /*
 
109
     * Returns a border instance for a <code>JScrollPane</code>.
 
110
     */
 
111
    static Border getScrollPaneBorder() {
 
112
        if (scrollPaneBorder == null) {
 
113
            scrollPaneBorder = new XPScrollPaneBorder();
 
114
        }
 
115
        return scrollPaneBorder;
 
116
    }
 
117
 
 
118
    /*
 
119
     * Returns a border instance for a <code>JTextField</code>.
 
120
     */
 
121
    static Border getTextFieldBorder() {
 
122
        if (textFieldBorder == null) {
 
123
            textFieldBorder = new BorderUIResource.CompoundBorderUIResource(
 
124
                                    new XPTextFieldBorder(),
 
125
                                    new BasicBorders.MarginBorder());
 
126
        }
 
127
        return textFieldBorder;
 
128
    }
 
129
 
 
130
    /*
 
131
     * Returns a border instance for a <code>JToggleButton</code>.
 
132
     */
 
133
    static Border getToggleButtonBorder(Insets buttonMargin) {
 
134
         return new BorderUIResource.CompoundBorderUIResource(
 
135
                    new XPButtonBorder(buttonMargin),
 
136
                    new BasicBorders.MarginBorder());
 
137
    }
 
138
 
 
139
    /*
 
140
     * Returns a border instance for a <code>JSpinner</code>.
 
141
     */
 
142
    static Border getSpinnerBorder() {
 
143
        if (spinnerBorder == null) {
 
144
            spinnerBorder = new XPSpinnerBorder();
 
145
        }
 
146
        return spinnerBorder;
 
147
    }
 
148
 
 
149
 
 
150
    /**
 
151
     * Returns a rollover border for buttons in a <code>JToolBar</code>.
 
152
     *
 
153
     * @return the lazily created rollover button border
 
154
     */
 
155
    static Border getRolloverButtonBorder() {
 
156
        if (rolloverButtonBorder == null) {
 
157
            rolloverButtonBorder = new CompoundBorder(
 
158
                                        new RolloverButtonBorder(),
 
159
                                        new PlasticBorders.RolloverMarginBorder());
 
160
        }
 
161
        return rolloverButtonBorder;
 
162
    }
 
163
 
 
164
    /**
 
165
     * A border for XP style buttons.
 
166
     */
 
167
    private static class XPButtonBorder extends AbstractBorder implements UIResource {
 
168
 
 
169
        protected final Insets insets;
 
170
 
 
171
        protected XPButtonBorder(Insets insets) {
 
172
            this.insets = insets;
 
173
        }
 
174
 
 
175
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
176
            AbstractButton button = (AbstractButton) c;
 
177
            ButtonModel    model  = button.getModel();
 
178
 
 
179
            if (!model.isEnabled()) {
 
180
                PlasticXPUtils.drawDisabledButtonBorder(g, x, y, w, h);
 
181
                return;
 
182
            }
 
183
 
 
184
            boolean isPressed = model.isPressed() && model.isArmed();
 
185
            boolean isDefault = button instanceof JButton
 
186
                                     && ((JButton) button).isDefaultButton();
 
187
            boolean isFocused = button.isFocusPainted() && button.hasFocus();
 
188
 
 
189
            if (isPressed)
 
190
                PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h);
 
191
            else if (isFocused)
 
192
                PlasticXPUtils.drawFocusedButtonBorder(g, x, y, w, h);
 
193
            else if (isDefault)
 
194
                PlasticXPUtils.drawDefaultButtonBorder(g, x, y, w, h);
 
195
            else
 
196
                PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h);
 
197
        }
 
198
 
 
199
        public Insets getBorderInsets(Component c) { return insets; }
 
200
 
 
201
        public Insets getBorderInsets(Component c, Insets newInsets) {
 
202
            newInsets.top    = insets.top;
 
203
            newInsets.left   = insets.left;
 
204
            newInsets.bottom = insets.bottom;
 
205
            newInsets.right  = insets.right;
 
206
            return newInsets;
 
207
        }
 
208
    }
 
209
 
 
210
 
 
211
    /**
 
212
     * A border for combo box arrow buttons.
 
213
     */
 
214
    private static final class XPComboBoxArrowButtonBorder extends AbstractBorder implements UIResource {
 
215
 
 
216
        protected static final Insets INSETS = new Insets(1, 1, 1, 1);
 
217
 
 
218
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
219
            PlasticComboBoxButton button = (PlasticComboBoxButton) c;
 
220
            JComboBox comboBox = button.getComboBox();
 
221
            ButtonModel model = button.getModel();
 
222
 
 
223
            if (!model.isEnabled()) {
 
224
                PlasticXPUtils.drawDisabledButtonBorder(g, x, y, w, h);
 
225
            } else {
 
226
                boolean isPressed = model.isPressed() && model.isArmed();
 
227
                boolean isFocused = comboBox.hasFocus();
 
228
                if (isPressed)
 
229
                    PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h);
 
230
                else if (isFocused)
 
231
                    PlasticXPUtils.drawFocusedButtonBorder(g, x, y, w, h);
 
232
                else
 
233
                    PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h);
 
234
            }
 
235
            if (comboBox.isEditable()) {
 
236
                // Paint two pixel on the arrow button's left hand side.
 
237
                g.setColor(model.isEnabled()
 
238
                                ? PlasticLookAndFeel.getControlDarkShadow()
 
239
                                : MetalLookAndFeel.getControlShadow());
 
240
                g.fillRect(x, y,       1, 1);
 
241
                g.fillRect(x, y + h-1, 1, 1);
 
242
            }
 
243
        }
 
244
 
 
245
        public Insets getBorderInsets(Component c) { return INSETS; }
 
246
    }
 
247
 
 
248
 
 
249
    /**
 
250
     * A border for combo box editors.
 
251
     */
 
252
    private static final class XPComboBoxEditorBorder extends AbstractBorder {
 
253
 
 
254
        private static final Insets INSETS  = new Insets(1, 1, 1, 0);
 
255
 
 
256
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
257
            g.setColor(c.isEnabled()
 
258
                           ? PlasticLookAndFeel.getControlDarkShadow()
 
259
                           : MetalLookAndFeel.getControlShadow());
 
260
            PlasticXPUtils.drawRect(g, x, y, w+1, h-1);
 
261
        }
 
262
 
 
263
        public Insets getBorderInsets(Component c) { return INSETS; }
 
264
    }
 
265
 
 
266
 
 
267
    /**
 
268
     * A border for text fields.
 
269
     */
 
270
    private static final class XPTextFieldBorder extends AbstractBorder  {
 
271
 
 
272
        private static final Insets INSETS = new Insets(1, 1, 1, 1);
 
273
 
 
274
                public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
275
 
 
276
            boolean enabled = ((c instanceof JTextComponent)
 
277
                               && (c.isEnabled() && ((JTextComponent) c).isEditable()))
 
278
                               ||
 
279
                               c.isEnabled();
 
280
 
 
281
                g.setColor(enabled
 
282
                            ? PlasticLookAndFeel.getControlDarkShadow()
 
283
                            : MetalLookAndFeel.getControlShadow());
 
284
            PlasticXPUtils.drawRect(g, x, y, w-1, h-1);
 
285
        }
 
286
 
 
287
        public Insets getBorderInsets(Component c) { return INSETS; }
 
288
 
 
289
        public Insets getBorderInsets(Component c, Insets newInsets) {
 
290
            newInsets.top    = INSETS.top;
 
291
            newInsets.left   = INSETS.left;
 
292
            newInsets.bottom = INSETS.bottom;
 
293
            newInsets.right  = INSETS.right;
 
294
            return newInsets;
 
295
        }
 
296
        }
 
297
 
 
298
 
 
299
    /**
 
300
     * Unlike Metal we paint a simple rectangle.
 
301
     * Being a subclass of MetalBorders.ScrollPaneBorder ensures that
 
302
     * the ScrollPaneUI will update the ScrollbarsFreeStanding property.
 
303
     */
 
304
    private static final class XPScrollPaneBorder extends MetalBorders.ScrollPaneBorder  {
 
305
 
 
306
        private static final Insets INSETS = new Insets(1, 1, 1, 1);
 
307
 
 
308
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
309
            g.setColor(c.isEnabled()
 
310
                    ? PlasticLookAndFeel.getControlDarkShadow()
 
311
                    : MetalLookAndFeel.getControlShadow());
 
312
            PlasticXPUtils.drawRect(g, x, y, w-1, h-1);
 
313
        }
 
314
 
 
315
        public Insets getBorderInsets(Component c) { return INSETS; }
 
316
 
 
317
        public Insets getBorderInsets(Component c, Insets newInsets) {
 
318
            newInsets.top    = INSETS.top;
 
319
            newInsets.left   = INSETS.left;
 
320
            newInsets.bottom = INSETS.bottom;
 
321
            newInsets.right  = INSETS.right;
 
322
            return newInsets;
 
323
        }
 
324
    }
 
325
 
 
326
 
 
327
    /**
 
328
     * A border for <code>JSpinner</code> components.
 
329
     */
 
330
    private static final class XPSpinnerBorder extends MetalBorders.ScrollPaneBorder  {
 
331
 
 
332
        private static final Insets INSETS = new Insets(1, 1, 1, 1);
 
333
 
 
334
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
335
            g.setColor(c.isEnabled()
 
336
                    ? PlasticLookAndFeel.getControlDarkShadow()
 
337
                    : MetalLookAndFeel.getControlShadow());
 
338
            // If you change the value of arrowButtonWidth, don't forget
 
339
            // to change it in PlasticXPSpinnerUI#SpinnerXPArrowButton too.
 
340
            int arrowButtonWidth = UIManager.getInt("ScrollBar.width") - 1;
 
341
            w -= arrowButtonWidth;
 
342
            g.fillRect(x,   y,     w,   1);
 
343
            g.fillRect(x,   y+1,   1,   h-1);
 
344
            g.fillRect(x+1, y+h-1, w-1, 1);
 
345
        }
 
346
 
 
347
        public Insets getBorderInsets(Component c) { return INSETS; }
 
348
 
 
349
        public Insets getBorderInsets(Component c, Insets newInsets) {
 
350
            newInsets.top    = INSETS.top;
 
351
            newInsets.left   = INSETS.left;
 
352
            newInsets.bottom = INSETS.bottom;
 
353
            newInsets.right  = INSETS.right;
 
354
            return newInsets;
 
355
        }
 
356
    }
 
357
 
 
358
 
 
359
    /**
 
360
     * A rollover border for buttons in toolbars.
 
361
     */
 
362
    private static final class RolloverButtonBorder extends XPButtonBorder {
 
363
 
 
364
        private RolloverButtonBorder() {
 
365
            super(new Insets(3, 3, 3, 3));
 
366
        }
 
367
 
 
368
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
 
369
            AbstractButton b = (AbstractButton) c;
 
370
            ButtonModel model = b.getModel();
 
371
 
 
372
            if (!model.isEnabled())
 
373
                return;
 
374
 
 
375
            if (!(c instanceof JToggleButton)) {
 
376
                if (model.isRollover() && !(model.isPressed() && !model.isArmed())) {
 
377
                    super.paintBorder( c, g, x, y, w, h );
 
378
                }
 
379
                return;
 
380
            }
 
381
 
 
382
            if (model.isRollover()) {
 
383
                if (model.isPressed() && model.isArmed()) {
 
384
                    PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h);
 
385
                } else {
 
386
                    PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h);
 
387
                }
 
388
            } else if (model.isSelected()) {
 
389
                PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h);
 
390
            }
 
391
        }
 
392
 
 
393
    }
 
394
 
 
395
}