]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/demos/src/com/ibm/icu/dev/demo/impl/DemoUtility.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / demos / src / com / ibm / icu / dev / demo / impl / DemoUtility.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1997-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.demo.impl;\r
8 \r
9 import java.awt.Color;\r
10 import java.awt.Component;\r
11 import java.awt.Container;\r
12 import java.awt.Font;\r
13 import java.awt.GridBagConstraints;\r
14 import java.awt.GridBagLayout;\r
15 import java.awt.Insets;\r
16 import java.awt.Label;\r
17 import java.awt.Panel;\r
18 import java.awt.TextComponent;\r
19 import java.util.Locale;\r
20 \r
21 public class DemoUtility\r
22 {\r
23     public static final Font titleFont = new Font("TimesRoman",Font.BOLD,18);\r
24     public static final Font labelFont = new Font("TimesRoman",Font.BOLD,14);\r
25     public static final Font choiceFont = new Font("Helvetica",Font.BOLD,12);\r
26     public static final Font editFont = new Font("Helvetica",Font.PLAIN,14);\r
27     public static final Font creditFont = new Font("Helvetica",Font.PLAIN,10);\r
28     public static final Font numberFont = new Font("sansserif", Font.PLAIN, 14);\r
29 \r
30     public static final Color bgColor = Color.lightGray;\r
31     public static final Color choiceColor = Color.white;\r
32 \r
33     public static final String copyright1 =\r
34         "Copyright (C) IBM Corp and others. 1997 - 2002 All Rights Reserved";\r
35 \r
36     /**\r
37     Provides easy way to use basic functions of GridBagLayout, without\r
38     the complications. After building a panel, and inserting all the\r
39     * subcomponents, call this to lay it out in the desired number of columns.\r
40     */\r
41     public static void fixGrid(Container cont, int columns) {\r
42         GridBagLayout gridbag = new GridBagLayout();\r
43         cont.setLayout(gridbag);\r
44 \r
45         GridBagConstraints c = new GridBagConstraints();\r
46         c.fill = GridBagConstraints.VERTICAL;\r
47         c.weightx = 1.0;\r
48         c.insets = new Insets(2,2,2,2);\r
49 \r
50         Component[] components = cont.getComponents();\r
51         for (int i = 0; i < components.length; ++i) {\r
52             // not used int colNumber = i%columns;\r
53             c.gridwidth = 1;    // default\r
54             if ((i%columns) == columns - 1)\r
55                 c.gridwidth = GridBagConstraints.REMAINDER;    // last in grid\r
56             if (components[i] instanceof Label) {\r
57                 switch (((Label)components[i]).getAlignment()) {\r
58                 case Label.CENTER: c.anchor = GridBagConstraints.CENTER; break;\r
59                 case Label.LEFT: c.anchor = GridBagConstraints.WEST; break;\r
60                 case Label.RIGHT: c.anchor = GridBagConstraints.EAST; break;\r
61                 }\r
62             }\r
63             gridbag.setConstraints(components[i], c);\r
64         }\r
65 \r
66     }\r
67 \r
68     /**\r
69     Provides easy way to change the spacing around an object in a GridBagLayout.\r
70     Call AFTER fixGridBag, passing in the container, the component, and the\r
71     new insets.\r
72     */\r
73     public static void setInsets(Container cont, Component comp, Insets insets) {\r
74         GridBagLayout gbl = (GridBagLayout)cont.getLayout();\r
75         GridBagConstraints g = gbl.getConstraints(comp);\r
76         g.insets = insets;\r
77         gbl.setConstraints(comp,g);\r
78     }\r
79 \r
80     public static Panel createSpacer() {\r
81         Panel spacer = new Panel();\r
82         spacer.setLayout(null);\r
83         spacer.setSize(1000, 1);\r
84         return spacer;\r
85     }\r
86 \r
87     // to avoid goofy updates and misplaced cursors\r
88     public static void setText(TextComponent area, String newText) {\r
89         String foo = area.getText();\r
90         if (foo.equals(newText)) return;\r
91         area.setText(newText);\r
92     }\r
93     \r
94     /**\r
95      * Compares two locals. Return value is negative\r
96      * if they're different, and more positive the more\r
97      * fields that match.\r
98      */\r
99      \r
100     public static int compareLocales(Locale l1, Locale l2)\r
101     {\r
102         int result = -1;\r
103         \r
104         if (l1.getLanguage().equals(l2.getLanguage())) {\r
105             result += 1;\r
106             \r
107             if (l1.getCountry().equals(l2.getCountry())) {\r
108                 result += 1;\r
109                 \r
110                 if (l1.getVariant().equals(l2.getVariant())) {\r
111                     result += 1;\r
112                 }\r
113             }\r
114         }\r
115         \r
116         return result;\r
117     }\r
118     \r
119     /**\r
120      * Get the G7 locale list for demos.\r
121      */\r
122     public static Locale[] getG7Locales() {\r
123         return localeList;\r
124     }\r
125     private static Locale[] localeList = {\r
126         new Locale("DA", "DK", ""),\r
127         new Locale("EN", "US", ""),\r
128         new Locale("EN", "GB", ""),\r
129         new Locale("EN", "CA", ""),\r
130         new Locale("FR", "FR", ""),\r
131         new Locale("FR", "CA", ""),\r
132         new Locale("DE", "DE", ""),\r
133         new Locale("IT", "IT", ""),\r
134     //new Locale("JA", "JP", ""),\r
135     };\r
136 }\r