Handle hostnames with upper-case letters
[webmin.git] / file / CbImageChooser.java
1 import java.awt.*;
2 import java.net.*;
3
4 class CbImageChooser extends Panel implements CbButtonCallback
5 {
6         Image img;
7         String imgsrc;
8         int imgw, imgh;
9         CbButton but;
10         CbImageFileWindow filewin;
11         //CbImageChooserCallback callback;
12
13         CbImageChooser(Image i)
14         {
15         this(i, null);
16         }
17
18         CbImageChooser(Image i, String s)
19         {
20         setLayout(new BorderLayout());
21         add("Center", but = new CbButton("Choose..", this));
22         setImage(i, s==null ? "" : s);
23         }
24
25         void setImage(Image i, String s)
26         {
27         img = i;
28         imgsrc = s;
29         if (img != null) but.setImage(img);
30         else but.setText("Choose..");
31         }
32
33         public void click(CbButton b)
34         {
35         if (b == but && filewin == null)
36                 new CbImageFileWindow(this);
37         }
38 }
39
40
41 class CbImageFileWindow extends FixedFrame implements CbButtonCallback
42 {
43         CbImageChooser parent;
44         ScrollImage imgp;
45         TextField url;
46         CbButton browse, ok, cancel;
47         FileDialog filedlog;
48         String lastfile = "";
49
50         CbImageFileWindow(CbImageChooser p)
51         {
52         parent = p;
53         parent.filewin = this;
54         setLayout(new BorderLayout());
55         add("Center", imgp = new ScrollImage(parent.img, 200, 200));
56         Panel bot = new GrayPanel();
57         bot.setLayout(new FlowLayout(FlowLayout.LEFT));
58         bot.add(new Label("URL:"));
59         bot.add(url = new TextField(parent.imgsrc, 20));
60         bot.add(browse = new CbButton("Browse..", this));
61         bot.add(new Label("  "));
62         bot.add(ok = new CbButton("Ok", this));
63         bot.add(cancel = new CbButton("Cancel", this));
64         add("South", bot);
65
66         pack();
67         show();
68         setTitle("Choose Image..");
69         Util.recursiveBackground(this, Util.body);
70         }
71
72         public void click(CbButton b)
73         {
74         if (b == ok)
75                 parent.setImage(imgp.img, lastfile);
76         if (b == ok || b == cancel)
77                 dispose();
78         else if (b == browse) {
79                 // Open file chooser here!
80                 FileDialog filedlog =
81                   new FileDialog(this, "Choose Image",FileDialog.LOAD);
82                 filedlog.show();
83                 if (filedlog.getFile() != null) {
84                         // file chosen.. load it in
85                         String fn = filedlog.getDirectory()+filedlog.getFile();
86                         url.setText(fn);
87                         loadFile(fn);
88                         }
89                 }
90         }
91
92         public void dispose()
93         {
94         super.dispose();
95         parent.filewin = null;
96         }
97
98         public boolean action(Event evt, Object obj)
99         {
100         if (evt.target == url) {
101                 String ut = url.getText();
102                 if (ut.startsWith("http:") || ut.startsWith("ftp:"))
103                         loadURL(ut);
104                 else
105                         loadFile(ut);
106                 return true;
107                 }
108         return false;
109         }
110
111         private void loadFile(String f)
112         {
113         Image i = Util.tk.getImage(f);
114         if (i == null || !Util.waitForImage(i))
115                 new ErrorWindow("Failed to load image "+f);
116         else {
117                 imgp.setImage(i);
118                 lastfile = f;
119                 }
120         }
121
122         private void loadURL(String u)
123         {
124         try {
125                 Image i = Util.tk.getImage(new URL(u));
126                 if (i == null || !Util.waitForImage(i))
127                         new ErrorWindow("Failed to load image from "+u);
128                 else {
129                         imgp.setImage(i);
130                         lastfile = u;
131                         }
132                 }
133         catch(MalformedURLException e) {
134                 new ErrorWindow(u+" is not a valid URL");
135                 }
136         }
137 }
138
139
140 class ScrollImage extends Panel implements CbScrollbarCallback
141 {
142         Image img;
143         int imgw, imgh;
144         int pw, ph;
145         CbScrollbar vsc, hsc;
146         boolean compute_scrollbars = true;
147
148         ScrollImage(Image i)
149         {
150         this(i, Util.getWidth(i), Util.getHeight(i));
151         }
152
153         ScrollImage(Image i, int w, int h)
154         {
155         pw = w; ph = h;
156         setLayout(new BorderLayout());
157         add("East", vsc = new CbScrollbar(CbScrollbar.VERTICAL, this));
158         add("South", hsc = new CbScrollbar(CbScrollbar.HORIZONTAL, this));
159         setImage(i);
160         }
161
162         void setImage(Image i)
163         {
164         img = i;
165         if (img != null) {
166                 imgw = Util.getWidth(img);
167                 imgh = Util.getHeight(img);
168                 }
169         compute_scrollbars = true;
170         repaint();
171         }
172
173         public void paint(Graphics g)
174         {
175         int w = size().width-vsc.size().width,
176             h = size().height-hsc.size().height;
177         if (compute_scrollbars) {
178                 if (img == null) {
179                         hsc.setValues(0, 1, 1);
180                         vsc.setValues(0, 1, 1);
181                         }
182                 else {
183                         if (imgw < w) hsc.setValues(0, 1, 1);
184                         else hsc.setValues(0, w, imgw);
185                         if (imgh < h) vsc.setValues(0, 1, 1);
186                         else vsc.setValues(0, h, imgh);
187                         }
188                 compute_scrollbars = false;
189                 }
190
191         g.setColor(Util.body);
192         g.fillRect(0, 0, w, h);
193         if (img != null) {
194                 if (imgw < w && imgh < h)
195                         g.drawImage(img, (w-imgw)/2, (h-imgh)/2, this);
196                 else
197                         g.drawImage(img, -hsc.getValue(), -vsc.getValue(),this);
198                 }
199         else {
200                 g.setFont(Util.f);
201                 g.setColor(Util.text);
202                 String s = "<None>";
203                 g.drawString(s, (w-Util.fnm.stringWidth(s))/2,
204                                 (h-Util.fnm.getHeight())/2);
205                 }
206         }
207
208         public void update(Graphics g) { paint(g); }
209
210         public void reshape(int nx, int ny, int nw, int nh)
211         {
212         super.reshape(nx, ny, nw, nh);
213         compute_scrollbars = true;
214         repaint();
215         }
216
217         public void moved(CbScrollbar s, int p)
218         {
219         repaint();
220         }
221
222         public void moving(CbScrollbar s, int p) { }
223
224         public Dimension minimumSize()
225         {
226         return new Dimension(pw, ph);
227         }
228
229         public Dimension preferredSize()
230         {
231         return minimumSize();
232         }
233 }