View Javadoc
1   /*
2    * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4    *
5    *
6    *
7    *
8    *
9    *
10   *
11   *
12   *
13   *
14   *
15   *
16   *
17   *
18   *
19   *
20   *
21   *
22   *
23   *
24   */
25  package fr.ifremer.dali.ui.swing.util.plaf;
26  
27  /*-
28   * #%L
29   * Dali :: UI
30   * %%
31   * Copyright (C) 2014 - 2017 Ifremer
32   * %%
33   * This program is free software: you can redistribute it and/or modify
34   * it under the terms of the GNU Affero General Public License as published by
35   * the Free Software Foundation, either version 3 of the License, or
36   * (at your option) any later version.
37   * 
38   * This program is distributed in the hope that it will be useful,
39   * but WITHOUT ANY WARRANTY; without even the implied warranty of
40   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41   * GNU General Public License for more details.
42   * 
43   * You should have received a copy of the GNU Affero General Public License
44   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
45   * #L%
46   */
47  
48  import javax.swing.JComponent;
49  import javax.swing.plaf.nimbus.AbstractRegionPainter;
50  import java.awt.Color;
51  import java.awt.Graphics2D;
52  import java.awt.Paint;
53  import java.awt.Shape;
54  import java.awt.geom.Rectangle2D;
55  
56  /**
57   * Copy of javax.swing.plaf.nimbus.ComboBoxTextFieldPainter with possibility of overriding background color
58   */
59  public class ComboBoxTextFieldPainter extends AbstractRegionPainter {
60      //package private integers representing the available states that
61      //this painter will paint. These are used when creating a new instance
62      //of ComboBoxTextFieldPainter to determine which region/state is being painted
63      //by that instance.
64      static final int BACKGROUND_DISABLED = 1;
65      static final int BACKGROUND_ENABLED = 2;
66      static final int BACKGROUND_SELECTED = 3;
67  
68      private int state; //refers to one of the static final ints above
69      private PaintContext ctx;
70  
71      private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
72  
73      //All Colors used for painting are stored here. Ideally, only those colors being used
74      //by a particular instance of ComboBoxTextFieldPainter would be created. For the moment at least,
75      //however, all are created for each instance.
76      private Color color1 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, -237);
77      private Color color2 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.07187897f, 0.06666666f, 0);
78      private Color color3 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07703349f, 0.0745098f, 0);
79      private Color color4 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07968931f, 0.14509803f, 0);
80      private Color color5 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07856284f, 0.11372548f, 0);
81      private Color color6 = decodeColor("nimbusBase", 0.040395975f, -0.60315615f, 0.29411763f, 0);
82      private Color color7 = decodeColor("nimbusBase", 0.016586483f, -0.6051466f, 0.3490196f, 0);
83      private Color color8;
84      private Color color9;
85      private Color color10;
86      private Color color11;
87      private Color color12;
88  
89      public ComboBoxTextFieldPainter(Object ctx, int state, Color background) {
90          super();
91          this.state = state;
92          this.ctx = (PaintContext) ctx;
93  
94          this.color8 = background;
95          this.color9 = background;
96          this.color10 = background;
97          this.color11 = background;
98          this.color12 = background;
99      }
100 
101     @Override
102     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
103         //generate this entire method. Each state/bg/fg/border combo that has
104         //been painted gets its own KEY and paint method.
105         switch (state) {
106             case BACKGROUND_DISABLED:
107                 paintBackgroundDisabled(g);
108                 break;
109             case BACKGROUND_ENABLED:
110                 paintBackgroundEnabled(g);
111                 break;
112             case BACKGROUND_SELECTED:
113                 paintBackgroundSelected(g);
114                 break;
115 
116         }
117     }
118 
119     @Override
120     protected final PaintContext getPaintContext() {
121         return ctx;
122     }
123 
124     private void paintBackgroundDisabled(Graphics2D g) {
125         rect = decodeRect1();
126         g.setPaint(color1);
127         g.fill(rect);
128         rect = decodeRect2();
129         g.setPaint(decodeGradient1(rect));
130         g.fill(rect);
131         rect = decodeRect3();
132         g.setPaint(decodeGradient2(rect));
133         g.fill(rect);
134         rect = decodeRect4();
135         g.setPaint(color6);
136         g.fill(rect);
137         rect = decodeRect5();
138         g.setPaint(color7);
139         g.fill(rect);
140 
141     }
142 
143     private void paintBackgroundEnabled(Graphics2D g) {
144         rect = decodeRect1();
145         g.setPaint(color1);
146         g.fill(rect);
147         rect = decodeRect2();
148         g.setPaint(decodeGradient3(rect));
149         g.fill(rect);
150         rect = decodeRect3();
151         g.setPaint(decodeGradient4(rect));
152         g.fill(rect);
153         rect = decodeRect4();
154         g.setPaint(color12);
155         g.fill(rect);
156         rect = decodeRect5();
157         g.setPaint(color11);
158         g.fill(rect);
159 
160     }
161 
162     private void paintBackgroundSelected(Graphics2D g) {
163         rect = decodeRect1();
164         g.setPaint(color1);
165         g.fill(rect);
166         rect = decodeRect2();
167         g.setPaint(decodeGradient3(rect));
168         g.fill(rect);
169         rect = decodeRect3();
170         g.setPaint(decodeGradient4(rect));
171         g.fill(rect);
172         rect = decodeRect4();
173         g.setPaint(color12);
174         g.fill(rect);
175         rect = decodeRect5();
176         g.setPaint(color11);
177         g.fill(rect);
178 
179     }
180 
181     private Rectangle2D decodeRect1() {
182         rect.setRect(decodeX(0.6666667f), //x
183                 decodeY(2.3333333f), //y
184                 decodeX(3.0f) - decodeX(0.6666667f), //width
185                 decodeY(2.6666667f) - decodeY(2.3333333f)); //height
186         return rect;
187     }
188 
189     private Rectangle2D decodeRect2() {
190         rect.setRect(decodeX(0.6666667f), //x
191                 decodeY(0.4f), //y
192                 decodeX(3.0f) - decodeX(0.6666667f), //width
193                 decodeY(1.0f) - decodeY(0.4f)); //height
194         return rect;
195     }
196 
197     private Rectangle2D decodeRect3() {
198         rect.setRect(decodeX(1.0f), //x
199                 decodeY(0.6f), //y
200                 decodeX(3.0f) - decodeX(1.0f), //width
201                 decodeY(1.0f) - decodeY(0.6f)); //height
202         return rect;
203     }
204 
205     private Rectangle2D decodeRect4() {
206         rect.setRect(decodeX(0.6666667f), //x
207                 decodeY(1.0f), //y
208                 decodeX(3.0f) - decodeX(0.6666667f), //width
209                 decodeY(2.3333333f) - decodeY(1.0f)); //height
210         return rect;
211     }
212 
213     private Rectangle2D decodeRect5() {
214         rect.setRect(decodeX(1.0f), //x
215                 decodeY(1.0f), //y
216                 decodeX(3.0f) - decodeX(1.0f), //width
217                 decodeY(2.0f) - decodeY(1.0f)); //height
218         return rect;
219     }
220 
221     private Paint decodeGradient1(Shape s) {
222         Rectangle2D bounds = s.getBounds2D();
223         float x = (float) bounds.getX();
224         float y = (float) bounds.getY();
225         float w = (float) bounds.getWidth();
226         float h = (float) bounds.getHeight();
227         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
228                 new float[]{0.0f, 0.5f, 1.0f},
229                 new Color[]{color2,
230                         decodeColor(color2, color3, 0.5f),
231                         color3});
232     }
233 
234     private Paint decodeGradient2(Shape s) {
235         Rectangle2D bounds = s.getBounds2D();
236         float x = (float) bounds.getX();
237         float y = (float) bounds.getY();
238         float w = (float) bounds.getWidth();
239         float h = (float) bounds.getHeight();
240         return decodeGradient((0.5f * w) + x, (1.0f * h) + y, (0.5f * w) + x, (0.0f * h) + y,
241                 new float[]{0.0f, 0.5f, 1.0f},
242                 new Color[]{color4,
243                         decodeColor(color4, color5, 0.5f),
244                         color5});
245     }
246 
247     private Paint decodeGradient3(Shape s) {
248         Rectangle2D bounds = s.getBounds2D();
249         float x = (float) bounds.getX();
250         float y = (float) bounds.getY();
251         float w = (float) bounds.getWidth();
252         float h = (float) bounds.getHeight();
253         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
254                 new float[]{0.0f, 0.49573863f, 0.99147725f},
255                 new Color[]{color8,
256                         decodeColor(color8, color9, 0.5f),
257                         color9});
258     }
259 
260     private Paint decodeGradient4(Shape s) {
261         Rectangle2D bounds = s.getBounds2D();
262         float x = (float) bounds.getX();
263         float y = (float) bounds.getY();
264         float w = (float) bounds.getWidth();
265         float h = (float) bounds.getHeight();
266         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
267                 new float[]{0.1f, 0.49999997f, 0.9f},
268                 new Color[]{color10,
269                         decodeColor(color10, color11, 0.5f),
270                         color11});
271     }
272 
273 }