f3ee2f3b9b36b3809e7f393c6e0114f19cea08cb
[spider.git] / spider-web / spiderframe.java
1 import java.awt.*;
2 import java.applet.*;
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 public class spiderframe extends Frame {
8
9
10         public spiderframe(spiderclient parent) {
11                 
12                 super("Spider DX Cluster");
13                 this.parent = parent;
14                 this.setFont(OutFont);
15
16                 menubar = new MenuBar();
17                 file = new Menu("File");
18                 file.add(connect_menuitem);
19                 file.add(new MenuItem("About"));
20                 file.add(new MenuItem("Quit"));
21                 if (Call.equals("NOCALL")) connect_menuitem.disable();
22                 menubar.add(file);
23                 
24                 edit = new Menu("Edit");
25                 edit.add(copy_menuitem);
26                 edit.add(paste_menuitem);
27                 copy_menuitem.disable();
28                 paste_menuitem.disable();
29 menubar.add(edit);
30                 
31                 
32
33 //              settings = new Menu("Preferences");
34 //              settings.add(new MenuItem("Personal preferences ..."));
35 // menubar.add(settings);
36                 
37                 
38                 
39                 commands = new Menu("Commands");
40                 commands.add(new MenuItem("Help"));
41                 commands.add(new MenuItem("Bye"));
42 menubar.add(commands);
43                 
44                 show = new Menu("Show");
45                 show.add(new MenuItem("Show Last DX"));
46                 show.add(new MenuItem("Show Beam Direction"));
47                             show.add(new MenuItem("Show WWV"));
48                             show.add(new MenuItem("Search DX"));
49                 show.add(new MenuItem("Search Address"));
50                             show.add(new MenuItem("Search QSL Manager"));
51                 show.add(new MenuItem("Search QSL Info"));
52                 show.add(new MenuItem("Search DXCC"));
53                 show.add(new MenuItem("Status"));
54 menubar.add(show);
55
56
57                 set = new Menu("Settings");
58                 set.add(new MenuItem("Set Beep"));
59                 set.add(new MenuItem("Set No Beep"));
60                 set.add(new MenuItem("Set QTH / City"));
61                 set.add(new MenuItem("Set Name"));
62                 set.add(new MenuItem("Set Locator"));
63                 set.add(new MenuItem("Set HomeNode"));
64                 set.add(new MenuItem("Set DX-Grid"));
65                 set.add(new MenuItem("Set No DX-Grid"));
66                 set.add(new MenuItem("Show Personal Settings"));
67 menubar.add(set); 
68
69
70                 dxann = new Menu("DXannounce");
71                 dxann.add(new MenuItem("DXannounce"));
72 menubar.add(dxann);     
73
74                 mailbox = new Menu("Mailbox");
75                 mailbox.add(new MenuItem("Last 10 Msgs"));
76                 mailbox.add(new MenuItem("Last 50 Msgs"));
77                 mailbox.add(new MenuItem("New Messages"));
78                 mailbox.add(new MenuItem("Own Messages"));
79 menubar.add(mailbox); 
80
81
82
83                 this.setMenuBar(menubar);
84
85                 setLayout(new BorderLayout());
86                 
87                 Panel p1 = new Panel();
88                 p1.setLayout(new BorderLayout());
89                 
90                 output = new TextArea();
91                 output.setEditable(false);
92
93                 p1.add("Center", output);
94                 input = new TextArea(2,80);
95                 input.setEditable(false);
96                 p1.add("South", input);
97                 add("Center", p1);
98                 
99                 
100                 Panel p2 = new Panel();
101                 p2.setLayout(new FlowLayout());
102                 connectButton.enable();
103                 p2.add(connectButton);
104                                                 
105                 disconnectButton.disable();
106                 p2.add(disconnectButton);
107                 add("South", p2);
108                 
109
110                 Panel p3 = new Panel();
111                 GridBagLayout gbl = new GridBagLayout();
112                 p3.setLayout(gbl);
113                 
114                 GridBagConstraints gbc = new GridBagConstraints();
115                 gbc.weightx = 20;
116                 gbc.weighty = 100;
117                 gbc.fill = GridBagConstraints.HORIZONTAL;
118                 gbc.anchor = GridBagConstraints.CENTER;
119                 
120                 add(p3,DateLabel,gbl, gbc, 0, 0, 1, 1);
121                 add(p3,IdleLabel, gbl, gbc, 2, 0, 2, 1);
122                 add(p3,connectState,gbl, gbc, 4, 0, 2, 1);
123                 
124                 add("North",p3);
125                                 
126                 setColors();
127                 setFonts();
128                 
129                 setDate time = new setDate(this);
130                 idle = new idleTime(this);
131                 
132         }
133
134
135         private void add(Panel p,Component c, GridBagLayout gbl,
136                 GridBagConstraints gbc,
137                 int x, int y, int w, int h) {
138                 
139                 gbc.gridx = x;
140                 gbc.gridy = y;
141                 gbc.gridwidth = w;
142                 gbc.gridheight = h;
143                 gbl.setConstraints(c, gbc);
144                 p.add(c);
145                 }
146
147         public void setColors() {
148                 output.setBackground(OutBackgroundColor);
149                 output.setForeground(OutForegroundColor);
150                 input.setBackground(InBackgroundColor);
151                 input.setForeground(InForegroundColor);
152                 }
153         
154         public void setFonts() {
155                 output.setFont(OutFont);
156                 input.setFont(InFont);
157                 }
158         
159         public void initPrintStream(PrintStream out) {
160                 this.out = out;
161                 }
162         
163         public void setText(String s) {
164                 int i;
165                 
166                 for (i=0; i < s.length(); i++) {
167                         if (s.charAt(i) == '\007')
168                                 parent.beep();
169                 }
170                 output.appendText(s +'\n');
171                 idle.resetTimer();
172                 }
173         
174         public void setCall(String s) {
175                 Call = s;
176                 }
177
178         public void setPassword(String s) {
179                 Password = s ;
180                 }
181
182         public void setPrefix(String s) {
183                 Prefix = s;
184                 }
185                 
186         public void setCall2(String s) {
187                 Call2 = s;
188                 }
189         
190         public void setFreq(String s) {
191                 Freq = s;
192                 }
193         
194         public void setRemarks(String s) {
195                 Remarks = s;
196                 }
197         
198         public void setTime(String s) {
199                 DateLabel.setText(s);
200                 }
201
202         public void setIdle(String s) {
203                 IdleLabel.setText(s);
204                 }
205         
206         public String getCall() {
207                 return Call;
208                 }
209         
210         public String getPassword() {
211                 return Password;
212                 }
213     
214         public String setPrefix(){
215                 return Prefix;
216                 }
217         
218         public String setCall2(){
219                 return Call2;
220                 }
221         
222         public String setFreq(){
223                 return Freq;
224                 }
225         
226         public String setRemarks(){
227                 return Remarks;
228                 }
229         
230         public void setFullname(String s) {
231                 Fullname = s;
232                 if (Call.equals("NOCALL")) 
233                         connect_menuitem.disable();
234                 else
235                         connect_menuitem.enable();
236                 }
237         
238         public String getFullname() {
239                 return Fullname;
240                 }
241         
242         public void setHostname(String s) {
243                 Hostname = s;
244                 }
245                 
246         public String getHostname() {
247                 return Hostname;
248                 }
249         
250         public void setPort(String s) {
251                 Port = s;
252                 }
253         
254         public String getPort() {
255                 return Port;
256                 }
257
258         public void setChannel(String s) {
259                 Channel = s;
260                 }
261         
262         public String getChannel() {
263                 return Channel;
264                 }
265         
266 //      public void login() {
267 //              PersonalPreferences pp = new PersonalPreferences(this, Call, Fullname, OutFont);
268 //      }
269         
270         public void antrichtung () {
271                 beam pp = new beam (this, Prefix,OutFont);
272                 }
273         
274 //              public void dxannounce () {
275 //              dxannounce pp = new dxannounce (this, Call2, Freq, Remarks, OutFont);
276 //      }
277         
278
279         public boolean handleEvent(Event evt) {
280                 if (evt.id == Event.KEY_PRESS) {
281                         if (evt.key == '\n') {
282                                 
283                                 idle.resetTimer();
284                                 output.appendText(input.getText()+'\n');
285                                 out.println(input.getText());
286
287                                 if (MaxInputPos < 255) {
288                                         InputPos++;                     
289                                         
290                                         MaxInputPos++;
291                                 }
292                                 else {
293                                         for(int i=0; i < 254; i++) {
294                                                 InputBuffer[i] = new String(InputBuffer[i+1]);
295                                         }
296                                         
297                                                          InputPos = 255;
298                                 }
299                                 InputBuffer[InputPos-1] = new String(input.getText());
300                                 input.setText("");
301                                 return true;
302                         }
303                 } else if (evt.id == Event.KEY_ACTION) {
304                         if (evt.key == Event.UP) {
305                                 if (InputPos > 0) {
306                                  InputPos--;
307                                         input.setText(InputBuffer[InputPos]);
308                                 }
309                                 return true;
310                         }
311                         else if (evt.key == Event.DOWN) {
312                                 if (InputPos < MaxInputPos) {
313                                         InputPos++;
314                                         input.setText(InputBuffer[InputPos]);
315                                 }
316                                 else {
317                                         input.setText("");      
318                                 }
319                         } 
320                         return true;
321                 }
322                 return super.handleEvent(evt);
323         }
324
325         public synchronized void show() {
326                 move(50, 50);
327                 super.show();
328         }       
329         
330         public void setUserColor(Color c, String whichColor) {
331                 if (whichColor.equals("Output Background ...")) {
332                         OutBackgroundColor = c;
333                 }
334                 else if (whichColor.equals("Output Foreground ...")) {
335                         OutForegroundColor = c;
336                 } else  if (whichColor.equals("Input Background ...")) {
337                         InBackgroundColor = c;
338                 }
339                 else if (whichColor.equals("Input Foreground ...")) {
340                         InForegroundColor = c;
341                 } else if (whichColor.equals("Output own text ...")) {
342                         OutOwnColor = c;
343                 } 
344
345                 setColors();
346         }
347         
348         
349         public void connected() {
350                 connect_menuitem.setLabel("Disconnect");
351                 connectState.setText("Connected to "+Hostname+":"+Port);
352                 input.setEditable(true);
353                 copy_menuitem.enable();
354                 Connected = true;
355                 connectButton.disable();
356                 disconnectButton.enable();
357         }
358         
359         public void disconnected() {
360                 Connected = false;
361                 connect_menuitem.setLabel("Connect");
362                 connectState.setText("Disconnected from "+Hostname);
363                 input.setEditable(false);
364                 copy_menuitem.disable();
365                 paste_menuitem.disable();
366                 connectButton.enable();
367                 disconnectButton.disable();
368         }
369         
370         public void setUserFont(String name, int size, int style, 
371                                 String whichFont) {
372                 if (whichFont.equals("Area ...")) {
373                         OutFont = new Font(name, style, size);
374                 }
375                 else if (whichFont.equals("Input Line ...")) {
376                         InFont = new Font(name, style, size);
377                 }
378                 
379                 setFonts();
380         }
381         
382         
383         public void getSelectedText() {
384                 CopyPaste = new String(output.getSelectedText());
385                 paste_menuitem.enable();
386         }
387         
388         public boolean action(Event evt, Object arg) {
389                 Prefix =  "";
390                 Call2 = "";
391                 Freq = "";
392                 Remarks = "";
393                 if (evt.target instanceof MenuItem) {
394                         if (arg.equals("Quit")) {
395                                 this.hide();
396                 //      } else if (arg.equals("Personal preferences ...")) {
397                 //              PersonalPreferences pp = new PersonalPreferences(this,
398                 //                      Call, Fullname, OutFont);
399                         } else if (arg.equals("Connect")) {
400                                 parent.doconnect();
401                         } else if (arg.equals("Disconnect")) {
402                                 parent.dodisconnect();
403                         } else if (arg.equals("About")) {
404                                 InfoDialog id = new InfoDialog(this, "About", 
405                                 "JAVA Spider Webclient 0.6b\nPA4AB\n" +
406                                 "pa4ab@pa4ab.net \n" +
407                                 "April 2001\n" +
408                                 "Based on source of the CLX Client from dl6dbh" );
409                                 
410                          id.resize(500,300);
411                                 id.show();
412                         } else if (arg.equals("Copy")) {
413                                 getSelectedText();
414                         } else if (arg.equals("Paste")) {
415                                 input.insertText(CopyPaste,input.getSelectionStart());
416                         } else if (arg.equals("Bye")) {
417                                 if (Connected) out.println("bye");
418                         } else if (arg.equals("Help")) {
419                                 if (Connected) out.println("? all");
420                         } else if (arg.equals("Show Last DX")) {
421                                 if (Connected) out.println("sh/dx");
422                         } else if (arg.equals("Status")) {
423                                 if (Connected) out.println("sh/c");
424                         } else if (arg.equals("Show WWV")) {
425                                 if (Connected) out.println("sh/wwv");
426                         } else if (arg.equals("Show Beam Direction")) {
427                                 beam pp = new beam(this, Prefix, OutFont);
428                                 if ((Connected) && !(Prefix.equals(""))) out.println ("sh/heading " + Prefix );
429                         } else if (arg.equals("Search DX")) {
430                                 beam pp = new beam(this, Prefix, OutFont);
431                                 if ((Connected) && !(Prefix.equals(""))) out.println ("sh/dx " + Prefix );
432                         
433                         } else if (arg.equals("Search QSL Info")) {
434                                 beam pp = new beam(this, Prefix, OutFont);
435                                 if ((Connected) && !(Prefix.equals(""))) out.println ("sh/qsl " + Prefix );
436                          
437
438                         } else if (arg.equals("Search Address")) {
439                                 beam pp = new beam(this, Prefix, OutFont);
440                                 if ((Connected) && !(Prefix.equals(""))) out.println ("sh/qrz " + Prefix );
441                         
442
443                         } else if (arg.equals("Search QSL Manager")) {
444                                 beam pp = new beam(this, Prefix, OutFont);
445                                 if ((Connected) && !(Prefix.equals(""))) out.println ("sh/qsl " + Prefix );
446                         
447
448                         } else if (arg.equals("Search DXCC")) {
449                                 beam pp = new beam(this, Prefix, OutFont);
450                                 if ((Connected) && !(Prefix.equals(""))) out.println ("sh/pr " + Prefix );
451                         
452                         // button settings
453
454                         } else if (arg.equals("Set Beep")) {
455                                 if (Connected) out.println("set/beep");
456                         
457                         } else if (arg.equals("Set No Beep")) {
458                                 if (Connected) out.println("set/nobeep");
459                         
460                         } else if (arg.equals("Set QTH / City")) {
461                                 beam pp = new beam(this, Prefix, OutFont);
462                                 if ((Connected) && !(Prefix.equals(""))) out.println ("set/qth " + Prefix );
463                                 
464                         } else if (arg.equals("Set Name")) {
465                                 beam pp = new beam(this, Prefix, OutFont);
466                                 if ((Connected) && !(Prefix.equals(""))) out.println ("set/name " + Prefix );
467                         
468                         } else if (arg.equals("Set Locator")) {
469                                 beam pp = new beam(this, Prefix, OutFont);
470                                 if ((Connected) && !(Prefix.equals(""))) out.println ("set/qra " + Prefix );
471                         
472                         } else if (arg.equals("Set HomeNode")) {
473                                 beam pp = new beam(this, Prefix, OutFont);
474                                 if ((Connected) && !(Prefix.equals(""))) out.println ("set/homenode " + Prefix );
475
476                         } else if (arg.equals("Set DX-Grid")) {
477                                 if (Connected) out.println("set/dxgrid");
478                         
479                         } else if (arg.equals("Set No DX-Grid")) {
480                                 if (Connected) out.println("set/nodxgrid");
481                         
482                         } else if (arg.equals("Show Personal Settings")) {
483                                 if (Connected) out.println ("sh/sta " + Call );
484                         }
485
486                         // dx announce
487
488                         else if (arg.equals("DXannounce")) {
489                                 dxannounce pp = new dxannounce(this, Call2, Freq, Remarks, OutFont);
490                                 if ((Connected) && !(Call2.equals(""))) out.println ("dx " + Freq + " " + Call2 + " " + Remarks );
491                         }
492                         // mailbox
493                          else if (arg.equals("Last 10 Msgs")) {
494                                 if (Connected) out.println ("dir" );
495                         } else if (arg.equals("Last 50 Msgs")) {
496                                 if (Connected) out.println ("dir/50" );
497                         } else if (arg.equals("New Messages")) {
498                                 if (Connected) out.println ("dir/new" );
499                         } else if (arg.equals("Own Messages")) {
500                                 if (Connected) out.println ("dir/own" );
501                         }                       
502                         else return false;
503                 }
504                 else if (evt.target instanceof Button) {
505                         if (arg.equals("Connect")) {
506                                 if (!Connected) {
507                                         parent.doconnect();
508                                 } else return false;
509                         } else if (arg.equals("Disconnect")) {
510                                 if (Connected) {
511                                         parent.dodisconnect();
512                                 } else return false;
513                         }
514                         else return false;
515                 }
516                 return true;
517         }
518
519         private idleTime idle;
520
521         private TextArea input;
522         private TextArea output;
523         private MenuBar menubar;
524         private Menu file;
525         private Menu edit;
526         private Menu settings;
527         private Menu colors;
528         private Menu fonts;
529         private Menu commands;
530         private Menu show;
531         private Menu set;
532         private Menu dxann;
533         private Menu mailbox;
534
535
536         private MenuItem connect_menuitem = new MenuItem("Connect");
537         private MenuItem copy_menuitem = new MenuItem("Copy");
538         private MenuItem paste_menuitem = new MenuItem("Paste");
539
540         private Button connectButton = new java.awt.Button("Connect");
541         private Button disconnectButton = new java.awt.Button("Disconnect");
542
543         private Date today = new Date();
544         private Label DateLabel = new Label(today.toLocaleString());
545         private Label IdleLabel = new Label("00:00");
546         private Label connectState = new Label("not connected");
547         
548  
549         private Color OutBackgroundColor = Color.black;
550         private Color OutForegroundColor = Color.yellow;
551         private Color OutOwnColor = Color.red;
552         private Color InBackgroundColor = new Color(234,199,135);
553         private Color InForegroundColor = Color.red;
554         
555         private Font OutFont = new Font("Courier", Font.PLAIN, 13);
556         private Font InFont = new Font("Courier", Font.BOLD, 13);
557         
558         private String Call = new String("NOCALL");
559         private String Password = new String();
560         private String Fullname = new String("NOBODY");
561         private String Hostname = new String("localhost");
562         private String Port = new String("3600");
563         private String Channel = new String("0");
564
565
566         private String Prefix = new String ("");        
567         private String Call2 = new String ("");        
568         private String Freq = new String ("");        
569         private String Remarks = new String ("");        
570
571
572         private PrintStream out = null;
573         
574         private String InputBuffer[] = new String[256];
575         private int InputPos = 0;
576         private int MaxInputPos = 0;
577         
578         private String CopyPaste; 
579         
580         private boolean Connected;
581         
582         private spiderclient parent;
583 }
584
585 class setDate extends Thread {
586
587         spiderframe cf;
588         
589         public setDate(spiderframe cf) {
590                 this.cf = cf;
591                 this.start();
592         }
593
594         public void run() {
595                 for(;;) {
596                         try { sleep(1000); } catch (InterruptedException e) {}
597                         today = new Date();
598                         cf.setTime(today.toLocaleString());
599                 }
600         }
601         private Date today = new Date();
602 }
603
604 class idleTime extends Thread {
605
606         spiderframe cf;
607         int count;
608         
609         public idleTime(spiderframe cf) {
610                 this.cf = cf;
611                 this.start();
612                 count = 0;
613         }
614
615         public void resetTimer() {
616                 count=0;
617         }
618
619         public void run() {
620                 for(;;) {
621                         try { sleep(1000); } catch (InterruptedException e) {}
622                         count++;
623                         String sec = new Format("%02d").form(count % 60);
624                         String min = new Format("%02d").form(count / 60);
625                         cf.setIdle("Idle: "+min+":"+sec);
626                 }
627         }
628 }