Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Frame;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.awt.Point;
  8. import java.awt.Toolkit;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.FocusEvent;
  12. import java.awt.event.FocusListener;
  13. import java.awt.event.WindowEvent;
  14. import java.awt.event.WindowListener;
  15. import java.io.InputStream;
  16. import java.util.Hashtable;
  17.  
  18. class ToFrontDelay extends Thread
  19. {       CloseFrame F;
  20.         final int Delay=500;
  21.         public ToFrontDelay (CloseFrame f)
  22.         {       F=f;
  23.                 start();
  24.         }
  25.         public void run ()
  26.         {       try
  27.                 {       sleep(Delay);
  28.                 }
  29.                 catch (Exception e) {}
  30.                 F.toFront(); F.requestFocus();
  31.         }
  32. }
  33.  
  34. /**
  35. A Frame, which can be closed with the close button in the window
  36. frame.
  37. <p>
  38. This frame may set an icon. The icon file must be a GIF with 16x16
  39. dots in 256 colors. We use the simple method, which does not work
  40. in the Netscape browser.
  41. <p>
  42. This Frame is a DoActionListener. Thus it is possible to use TextFieldAction
  43. etc. in it. Override doAction(String) and itemAction(String,boolean) to
  44. react on events.
  45. <p>
  46. Sometimes the Frame wants to set the focus to a certain text field.
  47. To support this, override focusGained().
  48. */
  49.  
  50. public class CloseFrame extends Frame
  51.     implements WindowListener, ActionListener, DoActionListener, FocusListener
  52. {       public CloseFrame (String s)
  53.         {       super(s);
  54.             addWindowListener(this);
  55.             addFocusListener(this);
  56.         }
  57.         public CloseFrame ()
  58.         {       addWindowListener(this);
  59.             addFocusListener(this);
  60.         }
  61.         public void windowActivated (WindowEvent e) {}
  62.         public void windowClosed (WindowEvent e) {}
  63.         public void windowClosing (WindowEvent e)
  64.         {   if (close()) doclose();
  65.         }
  66.         public void windowDeactivated (WindowEvent e) {}
  67.         public void windowDeiconified (WindowEvent e) {}
  68.         public void windowIconified (WindowEvent e) {}
  69.         public void windowOpened (WindowEvent e) {}
  70.         /**
  71.         @return if the frame should close now.
  72.         */
  73.         public boolean close ()
  74.         {       return true;
  75.         }
  76.         public void actionPerformed (ActionEvent e)
  77.         {   doAction(e.getActionCommand());
  78.         }
  79.         public void doAction (String o)
  80.         {   if ("Close".equals(o) && close()) doclose();
  81.         }
  82.  
  83.         /**
  84.         Closes the frame. Override, if necessary, and call
  85.         super.doclose().
  86.         */
  87.         public void doclose ()
  88.         {       setMenuBar(null); // for Linux ?!
  89.                 setVisible(false);
  90.                 // Because of a bug in Linux Java 1.4.2 etc.
  91.                 // dispose in a separate thread.
  92.                 Thread t=new Thread ()
  93.                 {       public void run ()
  94.                         {       dispose();
  95.                         }
  96.                 };
  97.                 t.start();
  98.         }
  99.         public void itemAction (String o, boolean flag) {}
  100.        
  101.         // the icon things
  102.         static Hashtable Icons=new Hashtable();
  103.         public void seticon (String file)
  104.         {       try
  105.                 {       Object o=Icons.get(file);
  106.                         if (o==null)
  107.                         {       Image i;
  108.                                 InputStream in=getClass().getResourceAsStream("/"+file);
  109.                                 int pos=0;
  110.                                 int n=in.available();
  111.                                 byte b[]=new byte[20000];
  112.                                 while (n>0)
  113.                                 {       int k=in.read(b,pos,n);
  114.                                         if (k<0) break;
  115.                                         pos+=k;
  116.                                         n=in.available();
  117.                                 }
  118.                                 i=Toolkit.getDefaultToolkit().createImage(b,0,pos);
  119.                                 MediaTracker T=new MediaTracker(this);
  120.                                 T.addImage(i,0);
  121.                                 T.waitForAll();
  122.                                 Icons.put(file,i);
  123.                                 setIconImage(i);
  124.                         }
  125.                         else
  126.                         {       setIconImage((Image)o);
  127.                         }
  128.                 } catch (Exception e) {}
  129.         }
  130.         /**
  131.         Override to set the focus somewhere.
  132.         */
  133.         public void focusGained (FocusEvent e) {}
  134.         public void focusLost (FocusEvent e) {}
  135.        
  136.         /**
  137.         Note window position in Global.
  138.         */
  139.         public void notePosition (String name)
  140.         {       Point l=getLocation();
  141.                 Dimension d=getSize();
  142.                 Global.setParameter(name+".x",l.x);
  143.                 Global.setParameter(name+".y",l.y);
  144.                 Global.setParameter(name+".w",d.width);
  145.                 if (d.height-Global.getParameter(name+".h",0)!=19)
  146.                         // works around a bug in Windows
  147.                         Global.setParameter(name+".h",d.height);
  148.                 boolean maximized=false;
  149.                 if ((getExtendedState()&Frame.MAXIMIZED_BOTH)!=0)
  150.                         Global.setParameter(name+".maximized",true);
  151.                 else
  152.                         Global.removeParameter(name+".maximized");
  153.         }
  154.  
  155.         /**
  156.         Set window position and size.
  157.         */
  158.         public void setPosition (String name)
  159.         {       if (Global.getParameter(name+".maximized",false))
  160.                 {       setExtendedState(Frame.MAXIMIZED_BOTH);
  161.                         return;
  162.                 }
  163.                 Point l=getLocation();
  164.                 Dimension d=getSize();
  165.                 Dimension dscreen=getToolkit().getScreenSize();
  166.                 int x=Global.getParameter(name+".x",l.x);
  167.                 int y=Global.getParameter(name+".y",l.y);
  168.                 int w=Global.getParameter(name+".w",d.width);
  169.                 int h=Global.getParameter(name+".h",d.height);
  170.                 if (w>dscreen.width) w=dscreen.width;
  171.                 if (h>dscreen.height) h=dscreen.height;
  172.                 if (x<0) x=0;
  173.                 if (x+w>dscreen.width) x=dscreen.width-w;
  174.                 if (y<0) y=0;
  175.                 if (y+h>dscreen.height) y=dscreen.height-h;
  176.                 setLocation(x,y);
  177.                 setSize(w,h);
  178.         }
  179.        
  180.         public void front ()
  181.         {       new ToFrontDelay(this);
  182.         }
  183.        
  184.         public void center ()
  185.         {       Dimension dscreen=getToolkit().getScreenSize();
  186.                 Dimension d=getSize();
  187.                 setLocation((dscreen.width-d.width)/2,(dscreen.height-d.height)/2);
  188.         }
  189.  
  190.         public void centerOut (Frame f)
  191.         {       Dimension si=f.getSize(),d=getSize(),
  192.                         dscreen=getToolkit().getScreenSize();
  193.                 Point lo=f.getLocation();
  194.                 int x=lo.x+si.width-getSize().width+20;
  195.                 int y=lo.y+si.height/2+40;
  196.                 if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
  197.                 if (x<10) x=10;
  198.                 if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
  199.                 if (y<10) y=10;
  200.                 setLocation(x,y);
  201.         }
  202. }
  203.