Handle hostnames with upper-case letters
[webmin.git] / file / TabbedPanel.java
1 // TabbedPanel
2 // A panel capable of displaying one of many components at a time. The
3 // component to display is chosen by a row of tab buttons.
4 import java.awt.*;
5 import java.util.Vector;
6
7 public class TabbedPanel extends Panel
8 {
9         TabSelector tab;                // component for choosing panel
10         TabbedDisplayPanel disp;        // where other panels are displayed
11         CardLayout card;
12
13         TabbedPanel()
14         {
15         this(Util.body_hi, Util.dark_edge_hi, Util.body);
16         }
17
18         TabbedPanel(Color hi, Color lo, Color bk)
19         {
20         setLayout(new BorderLayout());
21         add("North",tab = new TabSelector(hi, lo, bk));
22         add("Center",disp = new TabbedDisplayPanel(hi, lo));
23         disp.setLayout(card = new CardLayout());
24         }
25
26         // addItem
27         // Add a component to be chosen by a tab with the given name
28         void addItem(String n, Component c)
29         {
30         tab.addItem(n);
31         disp.addItem(n, c);
32         }
33
34         // select
35         // Display a component in the panel
36         void select(String n)
37         {
38         tab.choose(n);
39         disp.choose(n);
40         }
41
42         // chose
43         // Called back by a TabSelector object when the user clicks on a tab
44         void chose(String n)
45         {
46         disp.choose(n);
47         }
48 }
49
50 class TabSelector extends Canvas
51 {
52         Color hi, lo, bk;
53         Vector name = new Vector();
54         int chosen = 0;
55         Font font = new Font("timesRoman", Font.PLAIN, 12),
56              chfont = new Font(font.getName(), Font.BOLD, 13);
57
58         TabSelector(Color h, Color l, Color b)
59         {
60         hi = h; lo = l; bk = b;
61         }
62
63         void addItem(String n)
64         {
65         name.addElement(n);
66         paint(getGraphics());
67         }
68
69         void choose(String n)
70         {
71         for(int i=0; i<name.size(); i++)
72                 if (((String)name.elementAt(i)).equals(n)) {
73                         chosen = i;
74                         paint(getGraphics());
75                         }
76         }
77
78         public void paint(Graphics g)
79         {
80         if (g == null || name.size() == 0)
81                 return;
82         g.setColor(bk);
83         g.fillRect(0, 0, size().width, size().height);
84         int tw = size().width / name.size(),
85             th = size().height;
86         for(int i=0; i<name.size(); i++) {
87                 int x = tw*i;
88                 if (i == chosen) {
89                         g.setColor(lo);
90                         g.drawLine(x+tw-3, 1, x+tw-3, th-1);
91                         g.drawLine(x+tw-4, 2, x+tw-4, th-1);
92                         g.setColor(hi);
93                         g.drawLine(x, 0, x, th-1);
94                         g.drawLine(x+1, 0, x+1, th-1);
95                         g.drawLine(x, 0, x+tw-4, 0);
96                         g.drawLine(x, 1, x+tw-5, 1);
97                         g.drawLine(x+tw-3, th-1, x+tw-1, th-1);
98                         g.drawLine(x+tw-3, th-2, x+tw-1, th-2);
99                         }
100                 else {
101                         g.setColor(lo);
102                         g.drawLine(x+tw-3, 6, x+tw-3, th-1);
103                         g.drawLine(x+tw-4, 7, x+tw-4, th-1);
104                         g.setColor(hi);
105                         g.drawLine(x, 5, x, th-1);
106                         g.drawLine(x+1, 5, x+1, th-1);
107                         g.drawLine(x, 5, x+tw-4, 5);
108                         g.drawLine(x, 6, x+tw-5, 6);
109                         g.drawLine(x, th-1, x+tw-1, th-1);
110                         g.drawLine(x, th-2, x+tw-1, th-2);
111                         }
112                 g.setColor(lo);
113                 if (i == chosen) g.setFont(chfont);
114                 else g.setFont(font);
115                 String str = (String)name.elementAt(i);
116                 int textw = g.getFontMetrics().stringWidth(str);
117                 int texth = g.getFontMetrics().getHeight();
118                 if (textw < tw-5)
119                         g.drawString(str, x+(tw-textw)/2, (th-texth)/2+texth);
120                 }
121         }
122
123         public boolean mouseDown(Event evt, int x, int y)
124         {
125         if (name.size() == 0) return false;
126         chosen = x / (size().width / name.size());
127         paint(getGraphics());
128         ((TabbedPanel)getParent()).chose((String)name.elementAt(chosen));
129         return true;
130         }
131
132         public Dimension minimumSize()
133         {
134         return new Dimension(50,25);
135         }
136
137         public Dimension preferredSize()
138         {
139         return minimumSize();
140         }
141 }
142
143 class TabbedDisplayPanel extends Panel
144 {
145         Color hi, lo;
146         CardLayout card;
147
148         TabbedDisplayPanel(Color h, Color l)
149         {
150         hi = h; lo = l;
151         setLayout(card = new CardLayout());
152         }
153
154         // addItem
155         // Add one component to the set of possibles to be shown
156         void addItem(String n, Component c)
157         {
158         add(n, c);
159         }
160
161         // choose
162         // Display the named panel
163         void choose(String n)
164         {
165         ((CardLayout)getLayout()).show(this, n);
166         }
167
168         public Insets insets()
169         {
170         return new Insets(5,5,5,5);
171         }
172
173         public void paint(Graphics g)
174         {
175         g.setColor(hi);
176         g.drawLine(0, 0, 0, size().height-1);
177         g.drawLine(1, 0, 1, size().height-1);
178         g.setColor(lo);
179         g.drawLine(0, size().height-1, size().width-1, size().height-1);
180         g.drawLine(0, size().height-2, size().width-1, size().height-2);
181         g.drawLine(size().width-1, size().height-1, size().width-1, 0);
182         g.drawLine(size().width-2, size().height-1, size().width-2, 0);
183         }
184 }
185