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