explicitly sets MIG platform defaults to WINDOWS_XP
[jalview.git] / srcjar / 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                 if (/** @j2sNative true ||*/false)
157                         return WINDOWS_XP;
158                 final String os = System.getProperty("os.name");
159                 if (os.startsWith("Mac OS")) {
160                         return MAC_OSX;
161                 } else if (os.startsWith("Linux")) {
162                         return GNOME;
163                 } else {
164                         return WINDOWS_XP;
165                 }
166         }
167
168         private PlatformDefaults()
169         {
170         }
171
172         /** Set the defaults to the default for the platform
173          * @param plaf The platform. <code>PlatformDefaults.WINDOWS_XP</code>,
174          * <code>PlatformDefaults.MAC_OSX</code>, or
175          * <code>PlatformDefaults.GNOME</code>.
176          */
177         public static void setPlatform(int plaf)
178         {
179                 switch (plaf) {
180                         case WINDOWS_XP:
181                                 setDefaultVisualPadding("TabbedPane." + VISUAL_PADDING_PROPERTY, new int[]{1, 0, 1, 2});
182                                 setRelatedGap(LPX7, LPY7);
183                                 setUnrelatedGap(LPX11, LPY11);
184                                 setParagraphGap(LPX20, LPY20);
185                                 setIndentGap(LPX11, LPY11);
186                                 setGridCellGap(LPX7, LPY7);
187
188                                 setMinimumButtonWidth(new UnitValue(75, UnitValue.LPX, null));
189                                 setButtonOrder("L_E+U+YNBXOCAH_I_R");
190                                 setDialogInsets(LPY11, LPX11, LPY11, LPX11);
191                                 setPanelInsets(LPY7, LPX7, LPY7, LPX7);
192                                 break;
193
194                         case MAC_OSX:
195
196                                 setDefaultVisualPadding("Button." + VISUAL_PADDING_PROPERTY, new int[]{3, 6, 5, 6});
197                                 setDefaultVisualPadding("Button.icon." + VISUAL_PADDING_PROPERTY, new int[]{3, 2, 3, 2});
198                                 setDefaultVisualPadding("Button.square." + VISUAL_PADDING_PROPERTY, new int[]{4, 4, 4, 4});
199                                 setDefaultVisualPadding("Button.square.icon." + VISUAL_PADDING_PROPERTY, new int[]{4, 4, 4, 4});
200                                 setDefaultVisualPadding("Button.gradient." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
201                                 setDefaultVisualPadding("Button.gradient.icon." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
202                                 setDefaultVisualPadding("Button.bevel." + VISUAL_PADDING_PROPERTY, new int[]{2, 2, 3, 2});
203                                 setDefaultVisualPadding("Button.bevel.icon." + VISUAL_PADDING_PROPERTY, new int[]{2, 2, 3, 2});
204                                 setDefaultVisualPadding("Button.textured." + VISUAL_PADDING_PROPERTY, new int[]{3, 2, 3, 2});
205                                 setDefaultVisualPadding("Button.textured.icon." + VISUAL_PADDING_PROPERTY, new int[]{3, 2, 3, 2});
206                                 setDefaultVisualPadding("Button.roundRect." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
207                                 setDefaultVisualPadding("Button.roundRect.icon." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
208                                 setDefaultVisualPadding("Button.recessed." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
209                                 setDefaultVisualPadding("Button.recessed.icon." + VISUAL_PADDING_PROPERTY, new int[]{5, 4, 5, 4});
210                                 setDefaultVisualPadding("Button.help." + VISUAL_PADDING_PROPERTY, new int[]{4, 3, 3, 4});
211                                 setDefaultVisualPadding("Button.help.icon." + VISUAL_PADDING_PROPERTY, new int[]{4, 3, 3, 4});
212
213                                 setDefaultVisualPadding("ComboBox." + VISUAL_PADDING_PROPERTY, new int[]{2, 4, 4, 5});
214                                 setDefaultVisualPadding("ComboBox.isPopDown." + VISUAL_PADDING_PROPERTY, new int[]{2, 5, 4, 5});
215                                 setDefaultVisualPadding("ComboBox.isSquare." + VISUAL_PADDING_PROPERTY, new int[]{1, 6, 5, 7});
216
217                                 setDefaultVisualPadding("ComboBox.editable." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 2});
218                                 setDefaultVisualPadding("ComboBox.editable.isSquare." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 1});
219
220                                 setDefaultVisualPadding("TextField." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 3});
221                                 setDefaultVisualPadding("TabbedPane." + VISUAL_PADDING_PROPERTY, new int[]{4, 8, 11, 8});
222
223                                 setDefaultVisualPadding("Spinner." + VISUAL_PADDING_PROPERTY, new int[]{3, 3, 3, 1});
224
225                                 setDefaultVisualPadding("RadioButton." + VISUAL_PADDING_PROPERTY, new int[]{4, 6, 3, 5});
226                                 setDefaultVisualPadding("RadioButton.small." + VISUAL_PADDING_PROPERTY, new int[]{4, 6, 3, 5});
227                                 setDefaultVisualPadding("RadioButton.mini." + VISUAL_PADDING_PROPERTY, new int[]{5, 7, 4, 5});
228                                 setDefaultVisualPadding("CheckBox." + VISUAL_PADDING_PROPERTY, new int[]{5, 7, 4, 5});
229                                 setDefaultVisualPadding("CheckBox.small." + VISUAL_PADDING_PROPERTY, new int[]{5, 7, 4, 5});
230                                 setDefaultVisualPadding("CheckBox.mini." + VISUAL_PADDING_PROPERTY, new int[]{6, 7, 3, 5});
231
232                                 setRelatedGap(LPX7, LPY7);
233                                 setUnrelatedGap(LPX11, LPY11);
234                                 setParagraphGap(LPX20, LPY20);
235                                 setIndentGap(LPX11, LPY11);
236                                 setGridCellGap(LPX7, LPY7);
237
238                                 setMinimumButtonWidth(new UnitValue(70, UnitValue.LPX, null));
239                                 setMinimumButtonPadding(new UnitValue(8, UnitValue.LPX, null));
240                                 setButtonOrder("L_HE+U+NYBXCOA_I_R");
241                                 setDialogInsets(LPY20, LPX20, LPY20, LPX20);
242                                 setPanelInsets(LPY16, LPX16, LPY16, LPX16);
243                                 break;
244
245                         case GNOME:
246                                 setRelatedGap(LPX6, LPY6);                    // GNOME HIG 8.2.3
247                                 setUnrelatedGap(LPX12, LPY12);                // GNOME HIG 8.2.3
248                                 setParagraphGap(LPX18, LPY18);                // GNOME HIG 8.2.3
249                                 setIndentGap(LPX12, LPY12);                   // GNOME HIG 8.2.3
250                                 setGridCellGap(LPX6, LPY6);                   // GNOME HIG 8.2.3
251
252                                 // GtkButtonBox, child-min-width property default value
253                                 setMinimumButtonWidth(new UnitValue(85, UnitValue.LPX, null));
254                                 setButtonOrder("L_HE+UNYACBXO_I_R");          // GNOME HIG 3.4.2, 3.7.1
255                                 setDialogInsets(LPY12, LPX12, LPY12, LPX12);  // GNOME HIG 3.4.3
256                                 setPanelInsets(LPY6, LPX6, LPY6, LPX6);       // ???
257                                 break;
258                         default:
259                                 throw new IllegalArgumentException("Unknown platform: " + plaf);
260                 }
261                 CUR_PLAF = plaf;
262                 BASE_DPI = BASE_DPI_FORCED != null ? BASE_DPI_FORCED : getPlatformDPI(plaf);
263         }
264
265         /** Sets the visual bounds for a component type.
266          * @param key The component type. E.g. "TabbedPane.visualPadding" or "ComboBox.editable.isSquare.visualPadding". See source code for list.
267          * @param insets Top, left, bottom, right. Always length 4 or null.
268          * @see net.miginfocom.layout.ComponentWrapper#getVisualPadding()
269          */
270         public static void setDefaultVisualPadding(String key, int[] insets)
271         {
272                 VISUAL_BOUNDS.put(key, insets);
273         }
274
275         /** Returns the visual bounds for a component type.
276          * @param key The component type. E.g. "TabbedPane.visualPadding" or "ComboBox.editable.isSquare.visualPadding". See source code for list.
277          * @return insets Top, left, bottom, right. Always length 4 or null. Live object, MUST NOT BE CHANGED!.
278          * @see net.miginfocom.layout.ComponentWrapper#getVisualPadding()
279          */
280         public static int[] getDefaultVisualPadding(String key)
281         {
282                 return VISUAL_BOUNDS.get(key);
283         }
284
285         public static int getPlatformDPI(int plaf)
286         {
287                 switch (plaf) {
288                         case WINDOWS_XP:
289                         case GNOME:
290                                 return 96;
291                         case MAC_OSX:
292                                 try {
293                                         return java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
294                                 } catch (Throwable t) {
295                                         return 72;
296                                 }
297                         default:
298                                 throw new IllegalArgumentException("Unknown platform: " + plaf);
299                 }
300         }
301
302         /** Returns the current platform
303          * @return <code>PlatformDefaults.WINDOWS</code> or <code>PlatformDefaults.MAC_OSX</code>
304          */
305         public static int getPlatform()
306         {
307                 return CUR_PLAF;
308         }
309
310         public static int getDefaultDPI()
311         {
312                 return BASE_DPI;
313         }
314
315         /** Sets the default platform DPI. Normally this is set in the {@link #setPlatform(int)} for the different platforms
316          * but it can be tweaked here. For instance SWT on Mac does this.
317          * <p>
318          * Note that this is not the actual current DPI, but the base DPI for the toolkit.
319          * @param dpi The base DPI. If null the default DPI is reset to the platform base DPI.
320          */
321         public static void setDefaultDPI(Integer dpi)
322         {
323                 BASE_DPI = dpi != null ? dpi : getPlatformDPI(CUR_PLAF);
324                 BASE_DPI_FORCED = dpi;
325         }
326
327         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
328          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
329          * (72 DPI for Mac and 92 DPI for Windows).
330          * @return The forced scale or <code>null</code> for default scaling.
331          * @see #getHorizontalScaleFactor()
332          * @see ComponentWrapper#getHorizontalScreenDPI()
333          */
334         public static Float getHorizontalScaleFactor()
335         {
336                 return horScale;
337         }
338
339         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
340          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
341          * (72 DPI for Mac and 92 DPI for Windows).
342          * @param f The forced scale or <code>null</code> for default scaling.
343          * @see #getHorizontalScaleFactor()
344          * @see ComponentWrapper#getHorizontalScreenDPI()
345          */
346         public static void setHorizontalScaleFactor(Float f)
347         {
348                 if (!LayoutUtil.equals(horScale, f)) {
349                         horScale = f;
350                         MOD_COUNT++;
351                 }
352         }
353
354         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
355          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
356          * (72 DPI for Mac and 92 DPI for Windows).
357          * @return The forced scale or <code>null</code> for default scaling.
358          * @see #getHorizontalScaleFactor()
359          * @see ComponentWrapper#getVerticalScreenDPI()
360          */
361         public static Float getVerticalScaleFactor()
362         {
363                 return verScale;
364         }
365
366         /** The forced scale factor that all screen relative units (e.g. millimeters, inches and logical pixels) will be multiplied
367          * with. If <code>null</code> this will default to a scale that will scale the current screen to the default screen resolution
368          * (72 DPI for Mac and 92 DPI for Windows).
369          * @param f The forced scale or <code>null</code> for default scaling.
370          * @see #getHorizontalScaleFactor()
371          * @see ComponentWrapper#getVerticalScreenDPI()
372          */
373         public static void setVerticalScaleFactor(Float f)
374         {
375                 if (!LayoutUtil.equals(verScale, f)) {
376                         verScale = f;
377                         MOD_COUNT++;
378                 }
379         }
380
381         /** What base value should be used to calculate logical pixel sizes.
382          * @return The current base. Default is {@link #BASE_SCALE_FACTOR}
383          * @see #BASE_FONT_SIZE
384          * @see #BASE_SCALE_FACTOR
385          * @see #BASE_REAL_PIXEL
386 */
387         public static int getLogicalPixelBase()
388         {
389                 return LP_BASE;
390         }
391
392         /** What base value should be used to calculate logical pixel sizes.
393          * @param base The new base. Default is {@link #BASE_SCALE_FACTOR}
394          * @see #BASE_FONT_SIZE
395          * @see #BASE_SCALE_FACTOR
396          * @see #BASE_REAL_PIXEL
397          */
398         public static void setLogicalPixelBase(int base)
399         {
400                 if (LP_BASE != base) {
401                         if (base < BASE_FONT_SIZE || base > BASE_REAL_PIXEL)
402                                 throw new IllegalArgumentException("Unrecognized base: " + base);
403
404                         LP_BASE = base;
405                         MOD_COUNT++;
406                 }
407         }
408
409         /** Sets gap value for components that are "related".
410          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
411          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
412          */
413         public static void setRelatedGap(UnitValue x, UnitValue y)
414         {
415                 setUnitValue(new String[] {"r", "rel", "related"}, x, y);
416
417                 RELATED_X = new BoundSize(x, x, null, "rel:rel");
418                 RELATED_Y = new BoundSize(y, y, null, "rel:rel");
419         }
420
421         /** Sets gap value for components that are "unrelated".
422          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
423          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
424          */
425         public static void setUnrelatedGap(UnitValue x, UnitValue y)
426         {
427                 setUnitValue(new String[] {"u", "unrel", "unrelated"}, x, y);
428
429                 UNRELATED_X = new BoundSize(x, x, null, "unrel:unrel");
430                 UNRELATED_Y = new BoundSize(y, y, null, "unrel:unrel");
431         }
432
433         /** Sets paragraph gap value for components.
434          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
435          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
436          */
437         public static void setParagraphGap(UnitValue x, UnitValue y)
438         {
439                 setUnitValue(new String[] {"p", "para", "paragraph"}, x, y);
440         }
441
442         /** Sets gap value for components that are "intended".
443          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
444          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
445          */
446         public static void setIndentGap(UnitValue x, UnitValue y)
447         {
448                 setUnitValue(new String[] {"i", "ind", "indent"}, x, y);
449         }
450
451         /** 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
452          * on the component constraints. The value will be the min and preferred size of the gap.
453          * @param x The value that will be transformed to pixels. If <code>null</code> the current value will not change.
454          * @param y The value that will be transformed to pixels. If <code>null</code> the current value will not change.
455          */
456         public static void setGridCellGap(UnitValue x, UnitValue y)
457         {
458                 if (x != null)
459                         DEF_HGAP = new BoundSize(x, x, null, null);
460
461                 if (y != null)
462                         DEF_VGAP = new BoundSize(y, y, null, null);
463
464                 MOD_COUNT++;
465         }
466
467         /** Sets the recommended minimum button width.
468          * @param width The recommended minimum button width.
469          */
470         public static void setMinimumButtonWidth(UnitValue width)
471         {
472                 BUTT_WIDTH = width;
473                 MOD_COUNT++;
474         }
475
476         /** Returns the recommended minimum button width depending on the current set platform.
477          * @return The recommended minimum button width depending on the current set platform.
478          */
479         public static UnitValue getMinimumButtonWidth()
480         {
481                 return BUTT_WIDTH;
482         }
483
484         public static void setMinimumButtonPadding(UnitValue padding)
485         {
486                 BUTT_PADDING = padding;
487                 MOD_COUNT++;
488         }
489
490         public static UnitValue getMinimumButtonPadding()
491         {
492                 return BUTT_PADDING;
493         }
494
495         public static float getMinimumButtonWidthIncludingPadding(float refValue, ContainerWrapper parent, ComponentWrapper comp)
496         {
497                 final int buttonMinWidth = getMinimumButtonWidth().getPixels(refValue, parent, comp);
498                 if (comp != null && getMinimumButtonPadding() != null) {
499                         return Math.max(comp.getMinimumWidth(comp.getWidth()) + getMinimumButtonPadding().getPixels(refValue, parent, comp) * 2, buttonMinWidth);
500                 } else {
501                         return buttonMinWidth;
502                 }
503         }
504
505         /** Returns the unit value associated with the unit. (E.i. "related" or "indent"). Must be lower case.
506          * @param unit The unit string.
507          * @return The unit value associated with the unit. <code>null</code> for unrecognized units.
508          */
509         public static UnitValue getUnitValueX(String unit)
510         {
511                 return HOR_DEFS.get(unit);
512         }
513
514         /** Returns the unit value associated with the unit. (E.i. "related" or "indent"). Must be lower case.
515          * @param unit The unit string.
516          * @return The unit value associated with the unit. <code>null</code> for unrecognized units.
517          */
518         public static UnitValue getUnitValueY(String unit)
519         {
520                 return VER_DEFS.get(unit);
521         }
522
523         /** Sets the unit value associated with a unit string. This may be used to store values for new unit strings
524          * or modify old. Note that if a built in unit (such as "related") is modified all versions of it must be
525          * set (I.e. "r", "rel" and "related"). The build in values will be reset to the default ones if the platform
526          * is re-set.
527          * @param unitStrings The unit strings. E.g. "mu", "myunit". Will be converted to lower case and trimmed. Not <code>null</code>.
528          * @param x The value for the horizontal dimension. If <code>null</code> the value is not changed.
529          * @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.
530          */
531         public static void setUnitValue(String[] unitStrings, UnitValue x, UnitValue y)
532         {
533                 for (String unitString : unitStrings) {
534                         String s = unitString.toLowerCase().trim();
535                         if (x != null)
536                                 HOR_DEFS.put(s, x);
537                         if (y != null)
538                                 VER_DEFS.put(s, y);
539                 }
540                 MOD_COUNT++;
541         }
542
543         /** Understands ("r", "rel", "related") OR ("u", "unrel", "unrelated") OR ("i", "ind", "indent") OR ("p", "para", "paragraph").
544          */
545         static int convertToPixels(float value, String unit, boolean isHor, float ref, ContainerWrapper parent, ComponentWrapper comp)
546         {
547                 UnitValue uv = (isHor ? HOR_DEFS : VER_DEFS).get(unit);
548                 return uv != null ? Math.round(value * uv.getPixels(ref, parent, comp)) : UnitConverter.UNABLE;
549         }
550
551         /** Returns the order for the typical buttons in a standard button bar. It is one letter per button type.
552          * @return The button order.
553          * @see #setButtonOrder(String)
554          */
555         public static String getButtonOrder()
556         {
557                 return BUTTON_FORMAT;
558         }
559
560         /** Sets the order for the typical buttons in a standard button bar. It is one letter per button type.
561          * <p>
562          * Letter in upper case will get the minimum button width that the {@link #getMinimumButtonWidth()} specifies
563          * and letters in lower case will get the width the current look&amp;feel specifies.
564          * <p>
565          * Gaps will never be added to before the first component or after the last component. However, '+' (push) will be
566          * applied before and after as well, but with a minimum size of 0 if first/last so there will not be a gap
567          * before or after.
568          * <p>
569          * If gaps are explicitly set on buttons they will never be reduced, but they may be increased.
570          * <p>
571          * These are the characters that can be used:
572          * <ul>
573          * <li><code>'L'</code> - Buttons with this style tag will statically end up on the left end of the bar.
574          * <li><code>'R'</code> - Buttons with this style tag will statically end up on the right end of the bar.
575          * <li><code>'H'</code> - A tag for the "help" button that normally is supposed to be on the right.
576          * <li><code>'E'</code> - A tag for the "help2" button that normally is supposed to be on the left.
577          * <li><code>'Y'</code> - A tag for the "yes" button.
578          * <li><code>'N'</code> - A tag for the "no" button.
579          * <li><code>'X'</code> - A tag for the "next &gt;" or "forward &gt;" button.
580          * <li><code>'B'</code> - A tag for the "&lt; back" or "&lt; previous" button.
581          * <li><code>'I'</code> - A tag for the "finish" button.
582          * <li><code>'A'</code> - A tag for the "apply" button.
583          * <li><code>'C'</code> - A tag for the "cancel" or "close" button.
584          * <li><code>'O'</code> - A tag for the "ok" or "done" button.
585          * <li><code>'U'</code> - All Uncategorized, Other, or "Unknown" buttons. Tag will be "other".
586          * <li><code>'+'</code> - A glue push gap that will take as much space as it can and at least an "unrelated" gap. (Platform dependent)
587          * <li><code>'_'</code> - (underscore) An "unrelated" gap. (Platform dependent)
588          * </ul>
589          * <p>
590          * Even though the style tags are normally applied to buttons this works with all components.
591          * <p>
592          * The normal style for MAC OS X is <code>"L_HE+U+NYBXCOA_I_R"</code>,
593          * for Windows is <code>"L_E+U+YNBXOCAH_I_R"</code>, and for GNOME is
594          * <code>"L_HE+UNYACBXO_I_R"</code>.
595          *
596          * @param order The new button order for the current platform.
597          */
598         public static void setButtonOrder(String order)
599         {
600                 BUTTON_FORMAT = order;
601                 MOD_COUNT++;
602         }
603
604         /** Returns the tag (used in the {@link CC}) for a char. The char is same as used in {@link #getButtonOrder()}.
605          * @param c The char. Must be lower case!
606          * @return The tag that corresponds to the char or <code>null</code> if the char is unrecognized.
607          */
608         static String getTagForChar(char c)
609         {
610                 switch (c) {
611                         case 'o':
612                                 return "ok";
613                         case 'c':
614                                 return "cancel";
615                         case 'h':
616                                 return "help";
617                         case 'e':
618                                 return "help2";
619                         case 'y':
620                                 return "yes";
621                         case 'n':
622                                 return "no";
623                         case 'a':
624                                 return "apply";
625                         case 'x':
626                                 return "next";  // a.k.a forward
627                         case 'b':
628                                 return "back";  // a.k.a. previous
629                         case 'i':
630                                 return "finish";
631                         case 'l':
632                                 return "left";
633                         case 'r':
634                                 return "right";
635                         case 'u':
636                                 return "other";
637                         default:
638                                 return null;
639                 }
640         }
641
642         /** Returns the platform recommended inter-cell gap in the horizontal (x) dimension..
643          * @return The platform recommended inter-cell gap in the horizontal (x) dimension..
644          */
645         public static BoundSize getGridGapX()
646         {
647                 return DEF_HGAP;
648         }
649
650         /** Returns the platform recommended inter-cell gap in the vertical (x) dimension..
651          * @return The platform recommended inter-cell gap in the vertical (x) dimension..
652          */
653         public static BoundSize getGridGapY()
654         {
655                 return DEF_VGAP;
656         }
657
658         /** Returns the default dialog insets depending of the current platform.
659          * @param side top == 0, left == 1, bottom = 2, right = 3.
660          * @return The insets. Never <code>null</code>.
661          */
662         public static UnitValue getDialogInsets(int side)
663         {
664                 return DIALOG_INS[side];
665         }
666
667         /** Sets the default insets for a dialog. Values that are null will not be changed.
668          * @param top The top inset. May be <code>null</code>.
669          * @param left The left inset. May be <code>null</code>.
670          * @param bottom The bottom inset. May be <code>null</code>.
671          * @param right The right inset. May be <code>null</code>.
672          */
673         public static void setDialogInsets(UnitValue top, UnitValue left, UnitValue bottom, UnitValue right)
674         {
675                 if (top != null)
676                         DIALOG_INS[0] = top;
677
678                 if (left != null)
679                         DIALOG_INS[1] = left;
680
681                 if (bottom != null)
682                         DIALOG_INS[2] = bottom;
683
684                 if (right != null)
685                         DIALOG_INS[3] = right;
686
687                 MOD_COUNT++;
688         }
689
690         /** Returns the default panel insets depending of the current platform.
691          * @param side top == 0, left == 1, bottom = 2, right = 3.
692          * @return The insets. Never <code>null</code>.
693          */
694         public static UnitValue getPanelInsets(int side)
695         {
696                 return PANEL_INS[side];
697         }
698
699         /** Sets the default insets for a dialog. Values that are null will not be changed.
700          * @param top The top inset. May be <code>null</code>.
701          * @param left The left inset. May be <code>null</code>.
702          * @param bottom The bottom inset. May be <code>null</code>.
703          * @param right The right inset. May be <code>null</code>.
704          */
705         public static void setPanelInsets(UnitValue top, UnitValue left, UnitValue bottom, UnitValue right)
706         {
707                 if (top != null)
708                         PANEL_INS[0] = top;
709
710                 if (left != null)
711                         PANEL_INS[1] = left;
712
713                 if (bottom != null)
714                         PANEL_INS[2] = bottom;
715
716                 if (right != null)
717                         PANEL_INS[3] = right;
718
719                 MOD_COUNT++;
720         }
721
722         /** Returns the percentage used for alignment for labels (0 is left, 50 is center and 100 is right).
723          * @return The percentage used for alignment for labels
724          */
725         public static float getLabelAlignPercentage()
726         {
727                 return CUR_PLAF == MAC_OSX ? 1f : 0f;
728         }
729
730         /** Returns the default gap between two components that <b>are in the same cell</b>.
731          * @param comp The component that the gap is for. Never <code>null</code>.
732          * @param adjacentComp The adjacent component if any. May be <code>null</code>.
733          * @param adjacentSide What side the <code>adjacentComp</code> is on. {@link javax.swing.SwingUtilities#TOP} (1) or
734          * {@link javax.swing.SwingUtilities#LEFT} (2) or {@link javax.swing.SwingUtilities#BOTTOM} (3) or {@link javax.swing.SwingUtilities#RIGHT} (4).
735          * @param tag The tag string that the component might be tagged with in the component constraints. May be <code>null</code>.
736          * @param isLTR If it is left-to-right.
737          * @return The default gap between two components or <code>null</code> if there should be no gap.
738          */
739         static BoundSize getDefaultComponentGap(ComponentWrapper comp, ComponentWrapper adjacentComp, int adjacentSide, String tag, boolean isLTR)
740         {
741                 if (GAP_PROVIDER != null)
742                         return GAP_PROVIDER.getDefaultGap(comp, adjacentComp, adjacentSide, tag, isLTR);
743
744                 if (adjacentComp == null)
745                         return null;
746
747 //              if (adjacentComp == null || adjacentSide == SwingConstants.LEFT || adjacentSide == SwingConstants.TOP)
748 //                      return null;
749
750 //              SwingConstants.RIGHT == 4, SwingConstants.LEFT == 2
751                 return (adjacentSide == 2 || adjacentSide == 4) ? RELATED_X : RELATED_Y;
752         }
753
754         /** Returns the current gap provider or <code>null</code> if none is set and "related" should always be used.
755          * @return The current gap provider or <code>null</code> if none is set and "related" should always be used.
756          */
757         public static InCellGapProvider getGapProvider()
758         {
759                 return GAP_PROVIDER;
760         }
761
762         /** Sets the current gap provider or <code>null</code> if none is set and "related" should always be used.
763          * @param provider The current gap provider or <code>null</code> if none is set and "related" should always be used.
764          */
765         public static void setGapProvider(InCellGapProvider provider)
766         {
767                 GAP_PROVIDER = provider;
768         }
769
770         /** Returns how many times the defaults has been changed. This can be used as a light weight check to
771          * see if layout caches needs to be refreshed.
772          * @return How many times the defaults has been changed.
773          */
774         public static int getModCount()
775         {
776                 return MOD_COUNT;
777         }
778
779         /** Tells all layout manager instances to revalidate and recalculated everything.
780          */
781         public void invalidate()
782         {
783                 MOD_COUNT++;
784         }
785
786         /** Returns the current default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
787          * @return The current default unit.
788          * @see UnitValue#PIXEL
789          * @see UnitValue#LPX
790          */
791         public static int getDefaultHorizontalUnit()
792         {
793                 return DEF_H_UNIT;
794         }
795
796         /** Sets the default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
797          * @param unit The new default unit.
798          * @see UnitValue#PIXEL
799          * @see UnitValue#LPX
800          */
801         public static void setDefaultHorizontalUnit(int unit)
802         {
803                 if (unit < UnitValue.PIXEL || unit > UnitValue.LABEL_ALIGN)
804                         throw new IllegalArgumentException("Illegal Unit: " + unit);
805
806                 if (DEF_H_UNIT != unit) {
807                         DEF_H_UNIT = unit;
808                         MOD_COUNT++;
809                 }
810         }
811
812         /** Returns the current default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
813          * @return The current default unit.
814          * @see UnitValue#PIXEL
815          * @see UnitValue#LPY
816          */
817         public static int getDefaultVerticalUnit()
818         {
819                 return DEF_V_UNIT;
820         }
821
822         /** Sets the default unit. The default unit is the unit used if no unit is set. E.g. "width 10".
823          * @param unit The new default unit.
824          * @see UnitValue#PIXEL
825          * @see UnitValue#LPY
826          */
827         public static void setDefaultVerticalUnit(int unit)
828         {
829                 if (unit < UnitValue.PIXEL || unit > UnitValue.LABEL_ALIGN)
830                         throw new IllegalArgumentException("Illegal Unit: " + unit);
831
832                 if (DEF_V_UNIT != unit) {
833                         DEF_V_UNIT = unit;
834                         MOD_COUNT++;
835                 }
836         }
837
838         /** The default alignment for rows. Pre v3.5 this was <code>false</code> but now it is
839          * <code>true</code>.
840          * @return The current value. Default is <code>true</code>.
841          * @since 3.5
842          */
843         public static boolean getDefaultRowAlignmentBaseline()
844         {
845                 return dra;
846         }
847
848         /** The default alignment for rows. Pre v3.5 this was <code>false</code> but now it is
849          * <code>true</code>.
850          * @param b The new value. Default is <code>true</code> from v3.5.
851          * @since 3.5
852          */
853         public static void setDefaultRowAlignmentBaseline(boolean b)
854         {
855                 dra = b;
856         }
857 }