JAL-3032 adds Java 8 functionality (1/2)
[jalview.git] / src2 / net / miginfocom / layout / PlatformDefaults.java
1 package net.miginfocom.layout;
2
3 import java.util.HashMap;
4
5 /*
6  * License (BSD):
7  * ==============
8  *
9  * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,
13  * are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice, this list
15  * of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice, this
17  * list of conditions and the following disclaimer in the documentation and/or other
18  * materials provided with the distribution.
19  * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
20  * used to endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
29  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGE.
33  *
34  * @version 1.0
35  * @author Mikael Grev, MiG InfoCom AB
36  *         Date: 2006-sep-08
37  * @author Xxxx Xxxx, Xxxx  - Gnome support
38  *         Date: 2008-jan-16
39  */
40
41 /** Currently handles Windows, Mac OS X, and GNOME spacing.
42  */
43 public final class PlatformDefaults
44 {
45         /** Property to use in LAF settings and as JComponent client property
46          * to specify the visual padding.
47          * <p>
48          */
49         public static String VISUAL_PADDING_PROPERTY = "visualPadding";
50
51         private static int DEF_H_UNIT = UnitValue.LPX;
52         private static int DEF_V_UNIT = UnitValue.LPY;
53
54         private static InCellGapProvider GAP_PROVIDER = null;
55
56         private static volatile int MOD_COUNT = 0;
57
58 //      private static final UnitValue LPX1 = new UnitValue(1, UnitValue.LPX, null);
59 //      private static final UnitValue LPX4 = new UnitValue(4, UnitValue.LPX, null);
60         private static final UnitValue LPX6 = new UnitValue(6, UnitValue.LPX, null);
61         private static final UnitValue LPX7 = new UnitValue(7, UnitValue.LPX, null);
62 //      private static final UnitValue LPX8 = new UnitValue(8, UnitValue.LPX, null);
63 //      private static final UnitValue LPX9 = new UnitValue(9, UnitValue.LPX, null);
64 //      private static final UnitValue LPX10 = new UnitValue(10, UnitValue.LPX, null);
65         private static final UnitValue LPX11 = new UnitValue(11, UnitValue.LPX, null);
66         private static final UnitValue LPX12 = new UnitValue(12, UnitValue.LPX, null);
67 //      private static final UnitValue LPX14 = new UnitValue(14, UnitValue.LPX, null);
68         private static final UnitValue LPX16 = new UnitValue(16, UnitValue.LPX, null);
69         private static final UnitValue LPX18 = new UnitValue(18, UnitValue.LPX, null);
70         private static final UnitValue LPX20 = new UnitValue(20, UnitValue.LPX, null);
71
72 //      private static final UnitValue LPY1 = new UnitValue(1, UnitValue.LPY, null);
73 //      private static final UnitValue LPY4 = new UnitValue(4, UnitValue.LPY, null);
74         private static final UnitValue LPY6 = new UnitValue(6, UnitValue.LPY, null);
75         private static final UnitValue LPY7 = new UnitValue(7, UnitValue.LPY, null);
76 //      private static final UnitValue LPY8 = new UnitValue(8, UnitValue.LPY, null);
77 //      private static final UnitValue LPY9 = new UnitValue(9, UnitValue.LPY, null);
78 //      private static final UnitValue LPY10 = new UnitValue(10, UnitValue.LPY, null);
79         private static final UnitValue LPY11 = new UnitValue(11, UnitValue.LPY, null);
80         private static final UnitValue LPY12 = new UnitValue(12, UnitValue.LPY, null);
81 //      private static final UnitValue LPY14 = new UnitValue(14, UnitValue.LPY, null);
82         private static final UnitValue LPY16 = new UnitValue(16, UnitValue.LPY, null);
83         private static final UnitValue LPY18 = new UnitValue(18, UnitValue.LPY, null);
84         private static final UnitValue LPY20 = new UnitValue(20, UnitValue.LPY, null);
85
86         public static final int WINDOWS_XP = 0;
87         public static final int MAC_OSX = 1;
88         public static final int GNOME = 2;
89 //      private static final int KDE = 3;
90
91         private static int CUR_PLAF = WINDOWS_XP;
92
93         // Used for holding values.
94         private final static UnitValue[] PANEL_INS = new UnitValue[4];
95         private final static UnitValue[] DIALOG_INS = new UnitValue[4];
96
97         private static String BUTTON_FORMAT = null;
98
99         private static final HashMap<String, UnitValue> HOR_DEFS = new HashMap<String, UnitValue>(32);
100         private static final HashMap<String, UnitValue> VER_DEFS = new HashMap<String, UnitValue>(32);
101         private static BoundSize DEF_VGAP = null, DEF_HGAP = null;
102         static BoundSize RELATED_X = null, RELATED_Y = null, UNRELATED_X = null, UNRELATED_Y = null;
103         private static UnitValue BUTT_WIDTH = null;
104         private static UnitValue BUTT_PADDING = null;
105
106         private static Float horScale = null, verScale = null;
107
108         /** I value indicating that the size of the font for the container of the component
109          * will be used as a base for calculating the logical pixel size. This is much as how
110          * Windows calculated DLU (dialog units).
111          * @see net.miginfocom.layout.UnitValue#LPX
112          * @see net.miginfocom.layout.UnitValue#LPY
113          * @see #setLogicalPixelBase(int)
114          */
115         public static final int BASE_FONT_SIZE = 100;
116
117         /** I value indicating that the screen DPI will be used as a base for calculating the
118          * logical pixel size.
119          * <p>
120          * This is the default value.
121          * @see net.miginfocom.layout.UnitValue#LPX
122          * @see net.miginfocom.layout.UnitValue#LPY
123          * @see #setLogicalPixelBase(int)
124          * @see #setVerticalScaleFactor(Float)
125          * @see #setHorizontalScaleFactor(Float)
126          */
127         public static final int BASE_SCALE_FACTOR = 101;
128
129         /** I value indicating that the size of a logical pixel should always be a real pixel
130          * and thus no compensation will be made.
131          * @see net.miginfocom.layout.UnitValue#LPX
132          * @see net.miginfocom.layout.UnitValue#LPY
133          * @see #setLogicalPixelBase(int)
134          */
135         public static final int BASE_REAL_PIXEL = 102;
136
137         private static int LP_BASE = BASE_SCALE_FACTOR;
138
139         private static Integer BASE_DPI_FORCED = null;
140         private static int BASE_DPI = 96;
141
142         private static boolean dra = true;
143
144         private static final HashMap<String, int[]> VISUAL_BOUNDS = new HashMap<String, int[]>(64);
145
146         static {
147                 setPlatform(getCurrentPlatform());
148                 MOD_COUNT = 0;
149         }
150
151         /** Returns the platform that the JRE is running on currently.
152          * @return The platform that the JRE is running on currently. E.g. {@link #MAC_OSX}, {@link #WINDOWS_XP}, or {@link #GNOME}.
153          */
154         public static int getCurrentPlatform()
155         {
156                 final String os = System.getProperty("os.name");
157                 if (os.startsWith("Mac OS")) {
158                         return MAC_OSX;
159                 } else if (os.startsWith("Linux")) {
160                         return GNOME;
161                 } else {
162                         return WINDOWS_XP;
163                 }
164         }
165
166         private PlatformDefaults()
167         {
168         }
169
170         /** Set the defaults to the default for the platform
171          * @param plaf The platform. <code>PlatformDefaults.WINDOWS_XP</code>,
172          * <code>PlatformDefaults.MAC_OSX</code>, or
173          * <code>PlatformDefaults.GNOME</code>.
174          */
175         public static void setPlatform(int plaf)
176         {
177                 switch (plaf) {
178                         case WINDOWS_XP:
179                                 setDefaultVisualPadding("TabbedPane." + VISUAL_PADDING_PROPERTY, new int[]{1, 0, 1, 2});
180                                 setRelatedGap(LPX7, LPY7);
181                                 setUnrelatedGap(LPX11, LPY11);
182                                 setParagraphGap(LPX20, LPY20);
183                                 setIndentGap(LPX11, LPY11);
184                                 setGridCellGap(LPX7, LPY7);
185
186                                 setMinimumButtonWidth(new UnitValue(75, UnitValue.LPX, null));
187                                 setButtonOrder("L_E+U+YNBXOCAH_I_R");
188                                 setDialogInsets(LPY11, LPX11, LPY11, LPX11);
189                                 setPanelInsets(LPY7, LPX7, LPY7, LPX7);
190                                 break;
191
192                         case MAC_OSX:
193
194                                 setDefaultVisualPadding("Button." + VISUAL_PADDING_PROPERTY, new int[]{3, 6, 5, 6});
195                                 setDefaultVisualPadding("Button.icon." + VISUAL_PADDING_PROPERTY, new int[]{3, 2, 3, 2});
196                                 setDefaultVisualPadding("Button.square." + VISUAL_PADDING_PROPERTY, new int[]{4, 4, 4, 4});
197                                 setDefaultVisualPadding("Button.square.icon." + VISUAL_PADDING_PROPERTY, new int[]{4, 4, 4, 4});
198                                 setDefaultVisualPadding("Button.gradient." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
199                                 setDefaultVisualPadding("Button.gradient.icon." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
200                                 setDefaultVisualPadding("Button.bevel." + VISUAL_PADDING_PROPERTY, new int[]{2, 2, 3, 2});
201                                 setDefaultVisualPadding("Button.bevel.icon." + VISUAL_PADDING_PROPERTY, new int[]{2, 2, 3, 2});
202                                 setDefaultVisualPadding("Button.textured." + VISUAL_PADDING_PROPERTY, new int[]{3, 2, 3, 2});
203                                 setDefaultVisualPadding("Button.textured.icon." + VISUAL_PADDING_PROPERTY, new int[]{3, 2, 3, 2});
204                                 setDefaultVisualPadding("Button.roundRect." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
205                                 setDefaultVisualPadding("Button.roundRect.icon." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
206                                 setDefaultVisualPadding("Button.recessed." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
207                                 setDefaultVisualPadding("Button.recessed.icon." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
208                                 setDefaultVisualPadding("Button.help." + VISUAL_PADDING_PROPERTY, new int[]{4, 3, 3, 4});
209                                 setDefaultVisualPadding("Button.help.icon." + VISUAL_PADDING_PROPERTY, new int[]{4, 3, 3, 4});
210
211                                 setDefaultVisualPadding("ComboBox." + VISUAL_PADDING_PROPERTY, new int[]{2, 4, 4, 5});
212                                 setDefaultVisualPadding("ComboBox.isPopDown." + VISUAL_PADDING_PROPERTY, new int[]{2, 5, 4, 5});
213                                 setDefaultVisualPadding("ComboBox.isSquare." + VISUAL_PADDING_PROPERTY, new int[]{1, 6, 5, 7});
214
215                                 setDefaultVisualPadding("ComboBox.editable." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 2});
216                                 setDefaultVisualPadding("ComboBox.editable.isSquare." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 1});
217
218                                 setDefaultVisualPadding("TextField." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 3});
219                                 setDefaultVisualPadding("TabbedPane." + VISUAL_PADDING_PROPERTY, new int[]{4, 8, 11, 8});
220
221                                 setDefaultVisualPadding("Spinner." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 1});
222
223                                 setDefaultVisualPadding("RadioButton." + VISUAL_PADDING_PROPERTY, new int[]{4, 6, 3, 5});
224                                 setDefaultVisualPadding("RadioButton.small." + VISUAL_PADDING_PROPERTY, new int[]{4, 6, 3, 5});
225                                 setDefaultVisualPadding("RadioButton.mini." + VISUAL_PADDING_PROPERTY, new int[]{5, 7, 4, 5});
226                                 setDefaultVisualPadding("CheckBox." + VISUAL_PADDING_PROPERTY, new int[]{5, 7, 4, 5});
227                                 setDefaultVisualPadding("CheckBox.small." + VISUAL_PADDING_PROPERTY, new int[]{5, 7, 4, 5});
228                                 setDefaultVisualPadding("CheckBox.mini." + VISUAL_PADDING_PROPERTY, new int[]{6, 7, 3, 5});
229
230                                 setRelatedGap(LPX7, LPY7);
231                                 setUnrelatedGap(LPX11, LPY11);
232                                 setParagraphGap(LPX20, LPY20);
233                                 setIndentGap(LPX11, LPY11);
234                                 setGridCellGap(LPX7, LPY7);
235
236                                 setMinimumButtonWidth(new UnitValue(70, UnitValue.LPX, null));
237                                 setMinimumButtonPadding(new UnitValue(8, UnitValue.LPX, null));
238                                 setButtonOrder("L_HE+U+NYBXCOA_I_R");
239                                 setDialogInsets(LPY20, LPX20, LPY20, LPX20);
240                                 setPanelInsets(LPY16, LPX16, LPY16, LPX16);
241                                 break;
242
243                         case GNOME:
244                                 setRelatedGap(LPX6, LPY6);                    // GNOME HIG 8.2.3
245                                 setUnrelatedGap(LPX12, LPY12);                // GNOME HIG 8.2.3
246                                 setParagraphGap(LPX18, LPY18);                // GNOME HIG 8.2.3
247                                 setIndentGap(LPX12, LPY12);                   // GNOME HIG 8.2.3
248                                 setGridCellGap(LPX6, LPY6);                   // GNOME HIG 8.2.3
249
250                                 // GtkButtonBox, child-min-width property default value
251                                 setMinimumButtonWidth(new UnitValue(85, UnitValue.LPX, null));
252                                 setButtonOrder("L_HE+UNYACBXO_I_R");          // GNOME HIG 3.4.2, 3.7.1
253                                 setDialogInsets(LPY12, LPX12, LPY12, LPX12);  // GNOME HIG 3.4.3
254                                 setPanelInsets(LPY6, LPX6, LPY6, LPX6);       // ???
255                                 break;
256                         default:
257                                 throw new IllegalArgumentException("Unknown platform: " + plaf);
258                 }
259                 CUR_PLAF = plaf;
260                 BASE_DPI = BASE_DPI_FORCED != null ? BASE_DPI_FORCED : getPlatformDPI(plaf);
261         }
262
263         /** Sets the visual bounds for a component type.
264          * @param key The component type. E.g. "TabbedPane.visualPadding" or "ComboBox.editable.isSquare.visualPadding". See source code for list.
265          * @param insets Top, left, bottom, right. Always length 4 or null.
266          * @see net.miginfocom.layout.ComponentWrapper#getVisualPadding()
267          */
268         public static void setDefaultVisualPadding(String key, int[] insets)
269         {
270                 VISUAL_BOUNDS.put(key, insets);
271         }
272
273         /** Returns the visual bounds for a component type.
274          * @param key The component type. E.g. "TabbedPane.visualPadding" or "ComboBox.editable.isSquare.visualPadding". See source code for list.
275          * @return insets Top, left, bottom, right. Always length 4 or null. Live object, MUST NOT BE CHANGED!.
276          * @see net.miginfocom.layout.ComponentWrapper#getVisualPadding()
277          */
278         public static int[] getDefaultVisualPadding(String key)
279         {
280                 return VISUAL_BOUNDS.get(key);
281         }
282
283         public static int getPlatformDPI(int plaf)
284         {
285                 switch (plaf) {
286                         case WINDOWS_XP:
287                         case GNOME:
288                                 return 96;
289                         case MAC_OSX:
290                                 try {
291                                         return java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
292                                 } catch (Throwable t) {
293                                         return 72;
294                                 }
295                         default:
296                                 throw new IllegalArgumentException("Unknown platform: " + plaf);
297                 }
298         }
299
300         /** Returns the current platform
301          * @return <code>PlatformDefaults.WINDOWS</code> or <code>PlatformDefaults.MAC_OSX</code>
302          */
303         public static int getPlatform()
304         {
305                 return CUR_PLAF;
306         }
307
308         public static int getDefaultDPI()
309         {
310                 return BASE_DPI;
311         }
312
313         /** Sets the default platform DPI. Normally this is set in the {@link #setPlatform(int)} for the different platforms
314          * but it can be tweaked here. For instance SWT on Mac does this.
315          * <p>
316          * Note that this is not the actual current DPI, but the base DPI for the toolkit.
317          * @param dpi The base DPI. If null the default DPI is reset to the platform base DPI.
318          */
319         public static void setDefaultDPI(Integer dpi)
320         {
321                 BASE_DPI = dpi != null ? dpi : getPlatformDPI(CUR_PLAF);
322                 BASE_DPI_FORCED = dpi;
323         }
324
325         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
326          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
327          * (72 DPI for Mac and 92 DPI for Windows).
328          * @return The forced scale or <code>null</code> for default scaling.
329          * @see #getHorizontalScaleFactor()
330          * @see ComponentWrapper#getHorizontalScreenDPI()
331          */
332         public static Float getHorizontalScaleFactor()
333         {
334                 return horScale;
335         }
336
337         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
338          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
339          * (72 DPI for Mac and 92 DPI for Windows).
340          * @param f The forced scale or <code>null</code> for default scaling.
341          * @see #getHorizontalScaleFactor()
342          * @see ComponentWrapper#getHorizontalScreenDPI()
343          */
344         public static void setHorizontalScaleFactor(Float f)
345         {
346                 if (!LayoutUtil.equals(horScale, f)) {
347                         horScale = f;
348                         MOD_COUNT++;
349                 }
350         }
351
352         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
353          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
354          * (72 DPI for Mac and 92 DPI for Windows).
355          * @return The forced scale or <code>null</code> for default scaling.
356          * @see #getHorizontalScaleFactor()
357          * @see ComponentWrapper#getVerticalScreenDPI()
358          */
359         public static Float getVerticalScaleFactor()
360         {
361                 return verScale;
362         }
363
364         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
365          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
366          * (72 DPI for Mac and 92 DPI for Windows).
367          * @param f The forced scale or <code>null</code> for default scaling.
368          * @see #getHorizontalScaleFactor()
369          * @see ComponentWrapper#getVerticalScreenDPI()
370          */
371         public static void setVerticalScaleFactor(Float f)
372         {
373                 if (!LayoutUtil.equals(verScale, f)) {
374                         verScale = f;
375                         MOD_COUNT++;
376                 }
377         }
378
379         /** What base value should be used to calculate logical pixel sizes.
380          * @return The current base. Default is {@link #BASE_SCALE_FACTOR}
381          * @see #BASE_FONT_SIZE
382          * @see #BASE_SCALE_FACTOR
383          * @see #BASE_REAL_PIXEL
384 */
385         public static int getLogicalPixelBase()
386         {
387                 return LP_BASE;
388         }
389
390         /** What base value should be used to calculate logical pixel sizes.
391          * @param base The new base. Default is {@link #BASE_SCALE_FACTOR}
392          * @see #BASE_FONT_SIZE
393          * @see #BASE_SCALE_FACTOR
394          * @see #BASE_REAL_PIXEL
395          */
396         public static void setLogicalPixelBase(int base)
397         {
398                 if (LP_BASE != base) {
399                         if (base < BASE_FONT_SIZE || base > BASE_REAL_PIXEL)
400                                 throw new IllegalArgumentException("Unrecognized base: " + base);
401
402                         LP_BASE = base;
403                         MOD_COUNT++;
404                 }
405         }
406
407         /** Sets gap value for components that are "related".
408          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
409          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
410          */
411         public static void setRelatedGap(UnitValue x, UnitValue y)
412         {
413                 setUnitValue(new String[] {"r", "rel", "related"}, x, y);
414
415                 RELATED_X = new BoundSize(x, x, null, "rel:rel");
416                 RELATED_Y = new BoundSize(y, y, null, "rel:rel");
417         }
418
419         /** Sets gap value for components that are "unrelated".
420          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
421          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
422          */
423         public static void setUnrelatedGap(UnitValue x, UnitValue y)
424         {
425                 setUnitValue(new String[] {"u", "unrel", "unrelated"}, x, y);
426
427                 UNRELATED_X = new BoundSize(x, x, null, "unrel:unrel");
428                 UNRELATED_Y = new BoundSize(y, y, null, "unrel:unrel");
429         }
430
431         /** Sets paragraph gap value for components.
432          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
433          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
434          */
435         public static void setParagraphGap(UnitValue x, UnitValue y)
436         {
437                 setUnitValue(new String[] {"p", "para", "paragraph"}, x, y);
438         }
439
440         /** Sets gap value for components that are "intended".
441          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
442          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
443          */
444         public static void setIndentGap(UnitValue x, UnitValue y)
445         {
446                 setUnitValue(new String[] {"i", "ind", "indent"}, x, y);
447         }
448
449         /** Sets gap between two cells in the grid. Note that this is not a gap between component IN a cell, that has to be set
450          * on the component constraints. The value will be the min and preferred size of the gap.
451          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
452          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
453          */
454         public static void setGridCellGap(UnitValue x, UnitValue y)
455         {
456                 if (x != null)
457                         DEF_HGAP = new BoundSize(x, x, null, null);
458
459                 if (y != null)
460                         DEF_VGAP = new BoundSize(y, y, null, null);
461
462                 MOD_COUNT++;
463         }
464
465         /** Sets the recommended minimum button width.
466          * @param width The recommended minimum button width.
467          */
468         public static void setMinimumButtonWidth(UnitValue width)
469         {
470                 BUTT_WIDTH = width;
471                 MOD_COUNT++;
472         }
473
474         /** Returns the recommended minimum button width depending on the current set platform.
475          * @return The recommended minimum button width depending on the current set platform.
476          */
477         public static UnitValue getMinimumButtonWidth()
478         {
479                 return BUTT_WIDTH;
480         }
481
482         public static void setMinimumButtonPadding(UnitValue padding)
483         {
484                 BUTT_PADDING = padding;
485                 MOD_COUNT++;
486         }
487
488         public static UnitValue getMinimumButtonPadding()
489         {
490                 return BUTT_PADDING;
491         }
492
493         public static float getMinimumButtonWidthIncludingPadding(float refValue, ContainerWrapper parent, ComponentWrapper comp)
494         {
495                 final int buttonMinWidth = getMinimumButtonWidth().getPixels(refValue, parent, comp);
496                 if (comp != null && getMinimumButtonPadding() != null) {
497                         return Math.max(comp.getMinimumWidth(comp.getWidth()) + getMinimumButtonPadding().getPixels(refValue, parent, comp) * 2, buttonMinWidth);
498                 } else {
499                         return buttonMinWidth;
500                 }
501         }
502
503         /** Returns the unit value associated with the unit. (E.i. "related" or "indent"). Must be lower case.
504          * @param unit The unit string.
505          * @return The unit value associated with the unit. <code>null</code> for unrecognized units.
506          */
507         public static UnitValue getUnitValueX(String unit)
508         {
509                 return HOR_DEFS.get(unit);
510         }
511
512         /** Returns the unit value associated with the unit. (E.i. "related" or "indent"). Must be lower case.
513          * @param unit The unit string.
514          * @return The unit value associated with the unit. <code>null</code> for unrecognized units.
515          */
516         public static UnitValue getUnitValueY(String unit)
517         {
518                 return VER_DEFS.get(unit);
519         }
520
521         /** Sets the unit value associated with a unit string. This may be used to store values for new unit strings
522          * or modify old. Note that if a built in unit (such as "related") is modified all versions of it must be
523          * set (I.e. "r", "rel" and "related"). The build in values will be reset to the default ones if the platform
524          * is re-set.
525          * @param unitStrings The unit strings. E.g. "mu", "myunit". Will be converted to lower case and trimmed. Not <code>null</code>.
526          * @param x The value for the horizontal dimension. If <code>null</code> the value is not changed.
527          * @param y The value for the vertical dimension. Might be same object as for <code>x</code>. If <code>null</code> the value is not changed.
528          */
529         public static void setUnitValue(String[] unitStrings, UnitValue x, UnitValue y)
530         {
531                 for (String unitString : unitStrings) {
532                         String s = unitString.toLowerCase().trim();
533                         if (x != null)
534                                 HOR_DEFS.put(s, x);
535                         if (y != null)
536                                 VER_DEFS.put(s, y);
537                 }
538                 MOD_COUNT++;
539         }
540
541         /** Understands ("r", "rel", "related") OR ("u", "unrel", "unrelated") OR ("i", "ind", "indent") OR ("p", "para", "paragraph").
542          */
543         static int convertToPixels(float value, String unit, boolean isHor, float ref, ContainerWrapper parent, ComponentWrapper comp)
544         {
545                 UnitValue uv = (isHor ? HOR_DEFS : VER_DEFS).get(unit);
546                 return uv != null ? Math.round(value * uv.getPixels(ref, parent, comp)) : UnitConverter.UNABLE;
547         }
548
549         /** Returns the order for the typical buttons in a standard button bar. It is one letter per button type.
550          * @return The button order.
551          * @see #setButtonOrder(String)
552          */
553         public static String getButtonOrder()
554         {
555                 return BUTTON_FORMAT;
556         }
557
558         /** Sets the order for the typical buttons in a standard button bar. It is one letter per button type.
559          * <p>
560          * Letter in upper case will get the minimum button width that the {@link #getMinimumButtonWidth()} specifies
561          * and letters in lower case will get the width the current look&amp;feel specifies.
562          * <p>
563          * Gaps will never be added to before the first component or after the last component. However, '+' (push) will be
564          * applied before and after as well, but with a minimum size of 0 if first/last so there will not be a gap
565          * before or after.
566          * <p>
567          * If gaps are explicitly set on buttons they will never be reduced, but they may be increased.
568          * <p>
569          * These are the characters that can be used:
570          * <ul>
571          * <li><code>'L'</code> - Buttons with this style tag will statically end up on the left end of the bar.
572          * <li><code>'R'</code> - Buttons with this style tag will statically end up on the right end of the bar.
573          * <li><code>'H'</code> - A tag for the "help" button that normally is supposed to be on the right.
574          * <li><code>'E'</code> - A tag for the "help2" button that normally is supposed to be on the left.
575          * <li><code>'Y'</code> - A tag for the "yes" button.
576          * <li><code>'N'</code> - A tag for the "no" button.
577          * <li><code>'X'</code> - A tag for the "next &gt;" or "forward &gt;" button.
578          * <li><code>'B'</code> - A tag for the "&lt; back" or "&lt; previous" button.
579          * <li><code>'I'</code> - A tag for the "finish" button.
580          * <li><code>'A'</code> - A tag for the "apply" button.
581          * <li><code>'C'</code> - A tag for the "cancel" or "close" button.
582          * <li><code>'O'</code> - A tag for the "ok" or "done" button.
583          * <li><code>'U'</code> - All Uncategorized, Other, or "Unknown" buttons. Tag will be "other".
584          * <li><code>'+'</code> - A glue push gap that will take as much space as it can and at least an "unrelated" gap. (Platform dependent)
585          * <li><code>'_'</code> - (underscore) An "unrelated" gap. (Platform dependent)
586          * </ul>
587          * <p>
588          * Even though the style tags are normally applied to buttons this works with all components.
589          * <p>
590          * The normal style for MAC OS X is <code>"L_HE+U+NYBXCOA_I_R"</code>,
591          * for Windows is <code>"L_E+U+YNBXOCAH_I_R"</code>, and for GNOME is
592          * <code>"L_HE+UNYACBXO_I_R"</code>.
593          *
594          * @param order The new button order for the current platform.
595          */
596         public static void setButtonOrder(String order)
597         {
598                 BUTTON_FORMAT = order;
599                 MOD_COUNT++;
600         }
601
602         /** Returns the tag (used in the {@link CC}) for a char. The char is same as used in {@link #getButtonOrder()}.
603          * @param c The char. Must be lower case!
604          * @return The tag that corresponds to the char or <code>null</code> if the char is unrecognized.
605          */
606         static String getTagForChar(char c)
607         {
608                 switch (c) {
609                         case 'o':
610                                 return "ok";
611                         case 'c':
612                                 return "cancel";
613                         case 'h':
614                                 return "help";
615                         case 'e':
616                                 return "help2";
617                         case 'y':
618                                 return "yes";
619                         case 'n':
620                                 return "no";
621                         case 'a':
622                                 return "apply";
623                         case 'x':
624                                 return "next";  // a.k.a forward
625                         case 'b':
626                                 return "back";  // a.k.a. previous
627                         case 'i':
628                                 return "finish";
629                         case 'l':
630                                 return "left";
631                         case 'r':
632                                 return "right";
633                         case 'u':
634                                 return "other";
635                         default:
636                                 return null;
637                 }
638         }
639
640         /** Returns the platform recommended inter-cell gap in the horizontal (x) dimension..
641          * @return The platform recommended inter-cell gap in the horizontal (x) dimension..
642          */
643         public static BoundSize getGridGapX()
644         {
645                 return DEF_HGAP;
646         }
647
648         /** Returns the platform recommended inter-cell gap in the vertical (x) dimension..
649          * @return The platform recommended inter-cell gap in the vertical (x) dimension..
650          */
651         public static BoundSize getGridGapY()
652         {
653                 return DEF_VGAP;
654         }
655
656         /** Returns the default dialog insets depending of the current platform.
657          * @param side top == 0, left == 1, bottom = 2, right = 3.
658          * @return The insets. Never <code>null</code>.
659          */
660         public static UnitValue getDialogInsets(int side)
661         {
662                 return DIALOG_INS[side];
663         }
664
665         /** Sets the default insets for a dialog. Values that are null will not be changed.
666          * @param top The top inset. May be <code>null</code>.
667          * @param left The left inset. May be <code>null</code>.
668          * @param bottom The bottom inset. May be <code>null</code>.
669          * @param right The right inset. May be <code>null</code>.
670          */
671         public static void setDialogInsets(UnitValue top, UnitValue left, UnitValue bottom, UnitValue right)
672         {
673                 if (top != null)
674                         DIALOG_INS[0] = top;
675
676                 if (left != null)
677                         DIALOG_INS[1] = left;
678
679                 if (bottom != null)
680                         DIALOG_INS[2] = bottom;
681
682                 if (right != null)
683                         DIALOG_INS[3] = right;
684
685                 MOD_COUNT++;
686         }
687
688         /** Returns the default panel insets depending of the current platform.
689          * @param side top == 0, left == 1, bottom = 2, right = 3.
690          * @return The insets. Never <code>null</code>.
691          */
692         public static UnitValue getPanelInsets(int side)
693         {
694                 return PANEL_INS[side];
695         }
696
697         /** Sets the default insets for a dialog. Values that are null will not be changed.
698          * @param top The top inset. May be <code>null</code>.
699          * @param left The left inset. May be <code>null</code>.
700          * @param bottom The bottom inset. May be <code>null</code>.
701          * @param right The right inset. May be <code>null</code>.
702          */
703         public static void setPanelInsets(UnitValue top, UnitValue left, UnitValue bottom, UnitValue right)
704         {
705                 if (top != null)
706                         PANEL_INS[0] = top;
707
708                 if (left != null)
709                         PANEL_INS[1] = left;
710
711                 if (bottom != null)
712                         PANEL_INS[2] = bottom;
713
714                 if (right != null)
715                         PANEL_INS[3] = right;
716
717                 MOD_COUNT++;
718         }
719
720         /** Returns the percentage used for alignment for labels (0 is left, 50 is center and 100 is right).
721          * @return The percentage used for alignment for labels
722          */
723         public static float getLabelAlignPercentage()
724         {
725                 return CUR_PLAF == MAC_OSX ? 1f : 0f;
726         }
727
728         /** Returns the default gap between two components that <b>are in the same cell</b>.
729          * @param comp The component that the gap is for. Never <code>null</code>.
730          * @param adjacentComp The adjacent component if any. May be <code>null</code>.
731          * @param adjacentSide What side the <code>adjacentComp</code> is on. {@link javax.swing.SwingUtilities#TOP} (1) or
732          * {@link javax.swing.SwingUtilities#LEFT} (2) or {@link javax.swing.SwingUtilities#BOTTOM} (3) or {@link javax.swing.SwingUtilities#RIGHT} (4).
733          * @param tag The tag string that the component might be tagged with in the component constraints. May be <code>null</code>.
734          * @param isLTR If it is left-to-right.
735          * @return The default gap between two components or <code>null</code> if there should be no gap.
736          */
737         static BoundSize getDefaultComponentGap(ComponentWrapper comp, ComponentWrapper adjacentComp, int adjacentSide, String tag, boolean isLTR)
738         {
739                 if (GAP_PROVIDER != null)
740                         return GAP_PROVIDER.getDefaultGap(comp, adjacentComp, adjacentSide, tag, isLTR);
741
742                 if (adjacentComp == null)
743                         return null;
744
745 //              if (adjacentComp == null || adjacentSide == SwingConstants.LEFT || adjacentSide == SwingConstants.TOP)
746 //                      return null;
747
748 //              SwingConstants.RIGHT == 4, SwingConstants.LEFT == 2
749                 return (adjacentSide == 2 || adjacentSide == 4) ? RELATED_X : RELATED_Y;
750         }
751
752         /** Returns the current gap provider or <code>null</code> if none is set and "related" should always be used.
753          * @return The current gap provider or <code>null</code> if none is set and "related" should always be used.
754          */
755         public static InCellGapProvider getGapProvider()
756         {
757                 return GAP_PROVIDER;
758         }
759
760         /** Sets the current gap provider or <code>null</code> if none is set and "related" should always be used.
761          * @param provider The current gap provider or <code>null</code> if none is set and "related" should always be used.
762          */
763         public static void setGapProvider(InCellGapProvider provider)
764         {
765                 GAP_PROVIDER = provider;
766         }
767
768         /** Returns how many times the defaults has been changed. This can be used as a light weight check to
769          * see if layout caches needs to be refreshed.
770          * @return How many times the defaults has been changed.
771          */
772         public static int getModCount()
773         {
774                 return MOD_COUNT;
775         }
776
777         /** Tells all layout manager instances to revalidate and recalculated everything.
778          */
779         public void invalidate()
780         {
781                 MOD_COUNT++;
782         }
783
784         /** Returns the current default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
785          * @return The current default unit.
786          * @see UnitValue#PIXEL
787          * @see UnitValue#LPX
788          */
789         public static int getDefaultHorizontalUnit()
790         {
791                 return DEF_H_UNIT;
792         }
793
794         /** Sets the default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
795          * @param unit The new default unit.
796          * @see UnitValue#PIXEL
797          * @see UnitValue#LPX
798          */
799         public static void setDefaultHorizontalUnit(int unit)
800         {
801                 if (unit < UnitValue.PIXEL || unit > UnitValue.LABEL_ALIGN)
802                         throw new IllegalArgumentException("Illegal Unit: " + unit);
803
804                 if (DEF_H_UNIT != unit) {
805                         DEF_H_UNIT = unit;
806                         MOD_COUNT++;
807                 }
808         }
809
810         /** Returns the current default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
811          * @return The current default unit.
812          * @see UnitValue#PIXEL
813          * @see UnitValue#LPY
814          */
815         public static int getDefaultVerticalUnit()
816         {
817                 return DEF_V_UNIT;
818         }
819
820         /** Sets the default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
821          * @param unit The new default unit.
822          * @see UnitValue#PIXEL
823          * @see UnitValue#LPY
824          */
825         public static void setDefaultVerticalUnit(int unit)
826         {
827                 if (unit < UnitValue.PIXEL || unit > UnitValue.LABEL_ALIGN)
828                         throw new IllegalArgumentException("Illegal Unit: " + unit);
829
830                 if (DEF_V_UNIT != unit) {
831                         DEF_V_UNIT = unit;
832                         MOD_COUNT++;
833                 }
834         }
835
836         /** The default alignment for rows. Pre v3.5 this was <code>false</code> but now it is
837          * <code>true</code>.
838          * @return The current value. Default is <code>true</code>.
839          * @since 3.5
840          */
841         public static boolean getDefaultRowAlignmentBaseline()
842         {
843                 return dra;
844         }
845
846         /** The default alignment for rows. Pre v3.5 this was <code>false</code> but now it is
847          * <code>true</code>.
848          * @param b The new value. Default is <code>true</code> from v3.5.
849          * @since 3.5
850          */
851         public static void setDefaultRowAlignmentBaseline(boolean b)
852         {
853                 dra = b;
854         }
855 }