~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/utils/SubstanceStripingUtils.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.utils;
 
31
 
 
32
import java.awt.Color;
 
33
 
 
34
import javax.swing.JComponent;
 
35
 
 
36
import org.pushingpixels.substance.internal.ui.SubstanceTableUI;
 
37
 
 
38
/**
 
39
 * <p>
 
40
 * This class is used to speed up the striping of lists, tables, trees and
 
41
 * comboboxes that use Substance default renderers. This class if for internal
 
42
 * use only.
 
43
 * </p>
 
44
 * 
 
45
 * <p>
 
46
 * The usage is this:
 
47
 * </p>
 
48
 * <ul>
 
49
 * <li>Call {@link #setup(JComponent)} before starting painting the component
 
50
 * cells. An example -
 
51
 * {@link SubstanceTableUI#paint(java.awt.Graphics, JComponent)} that should
 
52
 * call this method prior to the call to its <code>paintCells</code>.</li>
 
53
 * <li>The specific renderer should call
 
54
 * {@link #applyStripedBackground(JComponent, int, JComponent)}.</li>
 
55
 * <li>After all cells have been renderered, call {@link #tearDown(JComponent)}.
 
56
 * </ul>
 
57
 * 
 
58
 * @author Kirill Grouchnikov
 
59
 */
 
60
public class SubstanceStripingUtils {
 
61
        /**
 
62
         * Name of the client property that stores the background fill color of odd
 
63
         * rows. The value should be an instance of {@link Color}.
 
64
         */
 
65
        private static final String ODD_COLOR = "substancelaf.internal.stripingColor.odd";
 
66
 
 
67
        /**
 
68
         * Name of the client property that stores the background fill color of even
 
69
         * rows. The value should be an instance of {@link Color}.
 
70
         */
 
71
        private static final String EVEN_COLOR = "substancelaf.internal.stripingColor.even";
 
72
 
 
73
        /**
 
74
         * Sets up the specified component for the UI delegate striping.
 
75
         * 
 
76
         * @param comp
 
77
         *            Component.
 
78
         */
 
79
        public static void setup(JComponent comp) {
 
80
                comp.putClientProperty(EVEN_COLOR, SubstanceColorUtilities
 
81
                                .getStripedBackground(comp, 0));
 
82
                comp.putClientProperty(ODD_COLOR, SubstanceColorUtilities
 
83
                                .getStripedBackground(comp, 1));
 
84
        }
 
85
 
 
86
        /**
 
87
         * Cleans the component after the UI delegate striping is over.
 
88
         * 
 
89
         * @param comp
 
90
         *            Component. Should be the same as passed to
 
91
         *            {@link #setup(JComponent)
 
92
         *              }.
 
93
         */
 
94
        public static void tearDown(JComponent comp) {
 
95
                comp.putClientProperty(EVEN_COLOR, null);
 
96
                comp.putClientProperty(ODD_COLOR, null);
 
97
        }
 
98
 
 
99
        /**
 
100
         * Applies the striped background to the specified renderer.
 
101
         * 
 
102
         * @param component
 
103
         *            Component (should be the same as passed to
 
104
         *            {@link #setup(JComponent)
 
105
         *              }).
 
106
         * @param rowIndex
 
107
         *            Row index.
 
108
         * @param renderer
 
109
         *            Renderer component.
 
110
         */
 
111
        public static void applyStripedBackground(JComponent component,
 
112
                        int rowIndex, JComponent renderer) {
 
113
                Color backgr = (Color) component
 
114
                                .getClientProperty((rowIndex % 2) == 0 ? EVEN_COLOR : ODD_COLOR);
 
115
                if (backgr == null)
 
116
                        return;
 
117
                renderer.setBackground(backgr);
 
118
        }
 
119
}