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.SpinnerPanelSpinnerFormattedTextFieldPainter with possibility of overriding background color
58   */
59  public class SpinnerTextFieldPainter 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 SpinnerPanelSpinnerFormattedTextFieldPainter 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_FOCUSED = 3;
67      static final int BACKGROUND_SELECTED = 4;
68      static final int BACKGROUND_SELECTED_FOCUSED = 5;
69  
70      private int state; //refers to one of the static final ints above
71      private PaintContext ctx;
72  
73      //the following 4 variables are reused during the painting code of the layers
74      private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
75  
76      //All Colors used for painting are stored here. Ideally, only those colors being used
77      //by a particular instance of SpinnerPanelSpinnerFormattedTextFieldPainter would be created. For the moment at least,
78      //however, all are created for each instance.
79      private Color color1 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, -237);
80      private Color color2;
81      private Color color3;
82      private Color color4;
83      private Color color5;
84      private Color color6;
85      private Color color7;
86      private Color color8;
87      private Color color9;
88      private Color color10;
89      private Color color11;
90      private Color color12;
91      private Color color13;
92      private Color color14 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
93      private Color color15 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.1048766f, -0.05098039f, 0);
94  
95      public SpinnerTextFieldPainter(Object ctx, int state, Color background) {
96          super();
97          this.state = state;
98          this.ctx = (PaintContext) ctx;
99  
100         this.color2 = background;
101         this.color3 = background;
102         this.color4 = background;
103         this.color5 = background;
104         this.color6 = background;
105         this.color7 = background;
106         this.color8 = background;
107         this.color9 = background;
108         this.color10 = background;
109         this.color11 = background;
110         this.color12 = background;
111         this.color13 = background;
112 
113     }
114 
115     @Override
116     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
117         //generate this entire method. Each state/bg/fg/border combo that has
118         //been painted gets its own KEY and paint method.
119         switch (state) {
120             case BACKGROUND_DISABLED:
121                 paintBackgroundDisabled(g);
122                 break;
123             case BACKGROUND_ENABLED:
124                 paintBackgroundEnabled(g);
125                 break;
126             case BACKGROUND_FOCUSED:
127                 paintBackgroundFocused(g);
128                 break;
129             case BACKGROUND_SELECTED:
130                 paintBackgroundSelected(g);
131                 break;
132             case BACKGROUND_SELECTED_FOCUSED:
133                 paintBackgroundSelectedAndFocused(g);
134                 break;
135 
136         }
137     }
138 
139     @Override
140     protected final PaintContext getPaintContext() {
141         return ctx;
142     }
143 
144     private void paintBackgroundDisabled(Graphics2D g) {
145         rect = decodeRect1();
146         g.setPaint(color1);
147         g.fill(rect);
148         rect = decodeRect2();
149         g.setPaint(decodeGradient1(rect));
150         g.fill(rect);
151         rect = decodeRect3();
152         g.setPaint(decodeGradient2(rect));
153         g.fill(rect);
154         rect = decodeRect4();
155         g.setPaint(color6);
156         g.fill(rect);
157         rect = decodeRect5();
158         g.setPaint(color7);
159         g.fill(rect);
160 
161     }
162 
163     private void paintBackgroundEnabled(Graphics2D g) {
164         rect = decodeRect1();
165         g.setPaint(color1);
166         g.fill(rect);
167         rect = decodeRect2();
168         g.setPaint(decodeGradient3(rect));
169         g.fill(rect);
170         rect = decodeRect3();
171         g.setPaint(decodeGradient4(rect));
172         g.fill(rect);
173         rect = decodeRect4();
174         g.setPaint(color12);
175         g.fill(rect);
176         rect = decodeRect5();
177         g.setPaint(color13);
178         g.fill(rect);
179 
180     }
181 
182     private void paintBackgroundFocused(Graphics2D g) {
183         rect = decodeRect6();
184         g.setPaint(color14);
185         g.fill(rect);
186         rect = decodeRect2();
187         g.setPaint(decodeGradient5(rect));
188         g.fill(rect);
189         rect = decodeRect3();
190         g.setPaint(decodeGradient4(rect));
191         g.fill(rect);
192         rect = decodeRect4();
193         g.setPaint(color12);
194         g.fill(rect);
195         rect = decodeRect5();
196         g.setPaint(color13);
197         g.fill(rect);
198 
199     }
200 
201     private void paintBackgroundSelected(Graphics2D g) {
202         rect = decodeRect1();
203         g.setPaint(color1);
204         g.fill(rect);
205         rect = decodeRect2();
206         g.setPaint(decodeGradient3(rect));
207         g.fill(rect);
208         rect = decodeRect3();
209         g.setPaint(decodeGradient4(rect));
210         g.fill(rect);
211         rect = decodeRect4();
212         g.setPaint(color12);
213         g.fill(rect);
214         rect = decodeRect5();
215         g.setPaint(color13);
216         g.fill(rect);
217 
218     }
219 
220     private void paintBackgroundSelectedAndFocused(Graphics2D g) {
221         rect = decodeRect6();
222         g.setPaint(color14);
223         g.fill(rect);
224         rect = decodeRect2();
225         g.setPaint(decodeGradient5(rect));
226         g.fill(rect);
227         rect = decodeRect3();
228         g.setPaint(decodeGradient4(rect));
229         g.fill(rect);
230         rect = decodeRect4();
231         g.setPaint(color12);
232         g.fill(rect);
233         rect = decodeRect5();
234         g.setPaint(color13);
235         g.fill(rect);
236 
237     }
238 
239     private Rectangle2D decodeRect1() {
240         rect.setRect(decodeX(0.6666667f), //x
241                 decodeY(2.3333333f), //y
242                 decodeX(3.0f) - decodeX(0.6666667f), //width
243                 decodeY(2.6666667f) - decodeY(2.3333333f)); //height
244         return rect;
245     }
246 
247     private Rectangle2D decodeRect2() {
248         rect.setRect(decodeX(0.6666667f), //x
249                 decodeY(0.4f), //y
250                 decodeX(3.0f) - decodeX(0.6666667f), //width
251                 decodeY(1.0f) - decodeY(0.4f)); //height
252         return rect;
253     }
254 
255     private Rectangle2D decodeRect3() {
256         rect.setRect(decodeX(1.0f), //x
257                 decodeY(0.6f), //y
258                 decodeX(3.0f) - decodeX(1.0f), //width
259                 decodeY(1.0f) - decodeY(0.6f)); //height
260         return rect;
261     }
262 
263     private Rectangle2D decodeRect4() {
264         rect.setRect(decodeX(0.6666667f), //x
265                 decodeY(1.0f), //y
266                 decodeX(3.0f) - decodeX(0.6666667f), //width
267                 decodeY(2.3333333f) - decodeY(1.0f)); //height
268         return rect;
269     }
270 
271     private Rectangle2D decodeRect5() {
272         rect.setRect(decodeX(1.0f), //x
273                 decodeY(1.0f), //y
274                 decodeX(3.0f) - decodeX(1.0f), //width
275                 decodeY(2.0f) - decodeY(1.0f)); //height
276         return rect;
277     }
278 
279     private Rectangle2D decodeRect6() {
280         rect.setRect(decodeX(0.22222222f), //x
281                 decodeY(0.13333334f), //y
282                 decodeX(2.916668f) - decodeX(0.22222222f), //width
283                 decodeY(2.75f) - decodeY(0.13333334f)); //height
284         return rect;
285     }
286 
287     private Paint decodeGradient1(Shape s) {
288         Rectangle2D bounds = s.getBounds2D();
289         float x = (float) bounds.getX();
290         float y = (float) bounds.getY();
291         float w = (float) bounds.getWidth();
292         float h = (float) bounds.getHeight();
293         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
294                 new float[]{0.0f, 0.5f, 1.0f},
295                 new Color[]{color2,
296                         decodeColor(color2, color3, 0.5f),
297                         color3});
298     }
299 
300     private Paint decodeGradient2(Shape s) {
301         Rectangle2D bounds = s.getBounds2D();
302         float x = (float) bounds.getX();
303         float y = (float) bounds.getY();
304         float w = (float) bounds.getWidth();
305         float h = (float) bounds.getHeight();
306         return decodeGradient((0.5f * w) + x, (1.0f * h) + y, (0.5f * w) + x, (0.0f * h) + y,
307                 new float[]{0.0f, 0.5f, 1.0f},
308                 new Color[]{color4,
309                         decodeColor(color4, color5, 0.5f),
310                         color5});
311     }
312 
313     private Paint decodeGradient3(Shape s) {
314         Rectangle2D bounds = s.getBounds2D();
315         float x = (float) bounds.getX();
316         float y = (float) bounds.getY();
317         float w = (float) bounds.getWidth();
318         float h = (float) bounds.getHeight();
319         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
320                 new float[]{0.0f, 0.49573863f, 0.99147725f},
321                 new Color[]{color8,
322                         decodeColor(color8, color9, 0.5f),
323                         color9});
324     }
325 
326     private Paint decodeGradient4(Shape s) {
327         Rectangle2D bounds = s.getBounds2D();
328         float x = (float) bounds.getX();
329         float y = (float) bounds.getY();
330         float w = (float) bounds.getWidth();
331         float h = (float) bounds.getHeight();
332         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
333                 new float[]{0.0f, 0.1684492f, 1.0f},
334                 new Color[]{color10,
335                         decodeColor(color10, color11, 0.5f),
336                         color11});
337     }
338 
339     private Paint decodeGradient5(Shape s) {
340         Rectangle2D bounds = s.getBounds2D();
341         float x = (float) bounds.getX();
342         float y = (float) bounds.getY();
343         float w = (float) bounds.getWidth();
344         float h = (float) bounds.getHeight();
345         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
346                 new float[]{0.0f, 0.49573863f, 0.99147725f},
347                 new Color[]{color8,
348                         decodeColor(color8, color15, 0.5f),
349                         color15});
350     }
351 
352 }