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