Handle hostnames with upper-case letters
[webmin.git] / file / Util.java
1 import java.awt.*;
2 import java.awt.image.*;
3
4 class Util
5 {
6         static Frame fr;
7         static Graphics g;
8         static Font f;
9         static FontMetrics fnm;
10         static Toolkit tk;
11
12         static Color light_edge = Color.white;
13         static Color dark_edge = Color.black;
14         static Color body = Color.lightGray;
15         static Color body_hi = new Color(210, 210, 210);
16         static Color light_edge_hi = Color.white;
17         static Color dark_edge_hi = Color.darkGray;
18         static Color dark_bg = new Color(150, 150, 150);
19         static Color text = Color.black;
20         static Color light_bg = Color.white;
21
22         static
23         {
24         fr = new Frame();
25         fr.addNotify();
26         g = fr.getGraphics();
27         setFont(new Font("TimesRoman", Font.PLAIN, 8));
28         tk = Toolkit.getDefaultToolkit();
29         }
30
31         static boolean waitForImage(Image i)
32         {
33         MediaTracker mt = new MediaTracker(fr);
34         mt.addImage(i, 0);
35         try { mt.waitForAll(); } catch(Exception e) { return false; }
36         return !mt.isErrorAny();
37         }
38
39         static boolean waitForImage(Image i, int w, int h)
40         {
41         MediaTracker mt = new MediaTracker(fr);
42         mt.addImage(i, w, h, 0);
43         try { mt.waitForAll(); } catch(Exception e) { return false; }
44         return !mt.isErrorAny();
45         }
46
47         static int getWidth(Image i)
48         {
49         waitForImage(i);
50         return i.getWidth(fr);
51         }
52
53         static int getHeight(Image i)
54         {
55         waitForImage(i);
56         return i.getHeight(fr);
57         }
58
59         static Image createImage(int w, int h)
60         {
61         return fr.createImage(w, h);
62         }
63
64         static Image createImage(ImageProducer p)
65         {
66         return fr.createImage(p);
67         }
68
69         static Object createObject(String name)
70         {
71         try {
72                 Class c = Class.forName(name);
73                 return c.newInstance();
74                 }
75         catch(Exception e) {
76                 System.err.println("Failed to create object "+name+" : "+
77                                    e.getClass().getName());
78                 System.exit(1);
79                 }
80         return null;
81         }
82
83         /**Create a new instance of some object
84          */
85         static Object createObject(Object o)
86         {
87         try { return o.getClass().newInstance(); }
88         catch(Exception e) {
89                 System.err.println("Failed to reproduce object "+o+" : "+
90                                  e.getClass().getName());
91                 System.exit(1);
92                 }
93         return null;
94         }
95
96
97         static void dottedRect(Graphics g, int x1, int y1,
98                                int x2, int y2, int s)
99         {
100         int i, s2 = s*2, t;
101         if (x2 < x1) { t = x1; x1 = x2; x2 = t; }
102         if (y2 < y1) { t = y1; y1 = y2; y2 = t; }
103         for(i=x1; i<=x2; i+=s2)
104                 g.drawLine(i, y1, i+s > x2 ? x2 : i+s, y1);
105         for(i=y1; i<=y2; i+=s2)
106                 g.drawLine(x2, i, x2, i+s > y2 ? y2 : i+s);
107         for(i=x2; i>=x1; i-=s2)
108                 g.drawLine(i, y2, i-s < x1 ? x1 : i-s, y2);
109         for(i=y2; i>=y1; i-=s2)
110                 g.drawLine(x1, i, x1, i-s < y1 ? y1 : i-s);
111         }
112
113         static void recursiveLayout(Container c)
114         {
115         c.layout();
116         for(int i=0; i<c.countComponents(); i++) {
117                 Component cc = c.getComponent(i);
118                 if (cc instanceof Container)
119                         recursiveLayout((Container)cc);
120                 }
121         }
122
123         static void recursiveBackground(Component c, Color b)
124         {
125         if (c instanceof TextField || c instanceof Choice ||
126             c instanceof TextArea)
127                 return;         // leave these alone
128         c.setBackground(b);
129         if (c instanceof Container) {
130                 Container cn = (Container)c;
131                 for(int i=0; i<cn.countComponents(); i++)
132                         recursiveBackground(cn.getComponent(i), b);
133                 }
134         }
135
136         static void recursiveBody(Component c)
137         {
138         recursiveBackground(c, Util.body);
139         }
140
141         static void setFont(Font nf)
142         {
143         f = nf;
144         g.setFont(f);
145         fnm = g.getFontMetrics();
146         }
147 }
148