Back to index

wims  3.65+svn20090927
CloseFrame.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.awt.Dimension;
00004 import java.awt.Frame;
00005 import java.awt.Image;
00006 import java.awt.MediaTracker;
00007 import java.awt.Point;
00008 import java.awt.Toolkit;
00009 import java.awt.event.ActionEvent;
00010 import java.awt.event.ActionListener;
00011 import java.awt.event.FocusEvent;
00012 import java.awt.event.FocusListener;
00013 import java.awt.event.WindowEvent;
00014 import java.awt.event.WindowListener;
00015 import java.io.InputStream;
00016 import java.util.Hashtable;
00017 
00018 class ToFrontDelay extends Thread
00019 {      CloseFrame F;
00020        final int Delay=500;
00021        public ToFrontDelay (CloseFrame f)
00022        {      F=f;
00023               start();
00024        }
00025        public void run ()
00026        {      try
00027               {      sleep(Delay);
00028               }
00029               catch (Exception e) {}
00030               F.toFront(); F.requestFocus();
00031        }
00032 }
00033 
00050 public class CloseFrame extends Frame
00051     implements WindowListener, ActionListener, DoActionListener, FocusListener
00052 {      public CloseFrame (String s)
00053        {      super(s);
00054            addWindowListener(this);
00055            addFocusListener(this);
00056        }
00057        public CloseFrame ()
00058        {      addWindowListener(this);
00059            addFocusListener(this);
00060        }
00061        public void windowActivated (WindowEvent e) {}
00062        public void windowClosed (WindowEvent e) {}
00063        public void windowClosing (WindowEvent e)
00064        {   if (close()) doclose();
00065        }
00066        public void windowDeactivated (WindowEvent e) {}
00067        public void windowDeiconified (WindowEvent e) {}
00068        public void windowIconified (WindowEvent e) {}
00069        public void windowOpened (WindowEvent e) {}
00073        public boolean close ()
00074        {      return true;
00075        }
00076        public void actionPerformed (ActionEvent e)
00077        {   doAction(e.getActionCommand());
00078        }
00079        public void doAction (String o)
00080        {   if ("Close".equals(o) && close()) doclose();
00081        }
00082 
00087        public void doclose ()
00088        {      setMenuBar(null); // for Linux ?!
00089               setVisible(false); 
00090               // Because of a bug in Linux Java 1.4.2 etc.
00091               // dispose in a separate thread.
00092               Thread t=new Thread ()
00093               {      public void run ()
00094                      {      dispose();
00095                      }
00096               };
00097               t.start();
00098        }
00099        public void itemAction (String o, boolean flag) {}
00100        
00101        // the icon things
00102        static Hashtable Icons=new Hashtable();
00103        public void seticon (String file)
00104        {      try
00105               {      Object o=Icons.get(file);
00106                      if (o==null)
00107                      {      Image i;
00108                             InputStream in=getClass().getResourceAsStream("/"+file);
00109                             int pos=0;
00110                             int n=in.available();
00111                             byte b[]=new byte[20000];
00112                             while (n>0)
00113                             {      int k=in.read(b,pos,n);
00114                                    if (k<0) break;
00115                                    pos+=k;
00116                                    n=in.available();
00117                             }
00118                             i=Toolkit.getDefaultToolkit().createImage(b,0,pos);
00119                             MediaTracker T=new MediaTracker(this);
00120                             T.addImage(i,0);
00121                             T.waitForAll();
00122                             Icons.put(file,i);
00123                             setIconImage(i);
00124                      }
00125                      else
00126                      {      setIconImage((Image)o);
00127                      }
00128               } catch (Exception e) {}
00129        }
00133        public void focusGained (FocusEvent e) {}
00134        public void focusLost (FocusEvent e) {}
00135        
00139        public void notePosition (String name)
00140        {      Point l=getLocation();
00141               Dimension d=getSize();
00142               Global.setParameter(name+".x",l.x);
00143               Global.setParameter(name+".y",l.y);
00144               Global.setParameter(name+".w",d.width);
00145               if (d.height-Global.getParameter(name+".h",0)!=19)
00146                      // works around a bug in Windows
00147                      Global.setParameter(name+".h",d.height);
00148               boolean maximized=false;
00149               if ((getExtendedState()&Frame.MAXIMIZED_BOTH)!=0)
00150                      Global.setParameter(name+".maximized",true);
00151               else
00152                      Global.removeParameter(name+".maximized");
00153        }
00154 
00158        public void setPosition (String name)
00159        {      if (Global.getParameter(name+".maximized",false))
00160               {      setExtendedState(Frame.MAXIMIZED_BOTH);
00161                      return;
00162               }
00163               Point l=getLocation();
00164               Dimension d=getSize();
00165               Dimension dscreen=getToolkit().getScreenSize();
00166               int x=Global.getParameter(name+".x",l.x);
00167               int y=Global.getParameter(name+".y",l.y);
00168               int w=Global.getParameter(name+".w",d.width);
00169               int h=Global.getParameter(name+".h",d.height);
00170               if (w>dscreen.width) w=dscreen.width;
00171               if (h>dscreen.height) h=dscreen.height;
00172               if (x<0) x=0;
00173               if (x+w>dscreen.width) x=dscreen.width-w;
00174               if (y<0) y=0;
00175               if (y+h>dscreen.height) y=dscreen.height-h;
00176               setLocation(x,y);
00177               setSize(w,h);
00178        }
00179        
00180        public void front ()
00181        {      new ToFrontDelay(this);
00182        }
00183        
00184        public void center ()
00185        {      Dimension dscreen=getToolkit().getScreenSize();
00186               Dimension d=getSize();
00187               setLocation((dscreen.width-d.width)/2,(dscreen.height-d.height)/2);
00188        }
00189 
00190        public void centerOut (Frame f)
00191        {      Dimension si=f.getSize(),d=getSize(),
00192                      dscreen=getToolkit().getScreenSize();
00193               Point lo=f.getLocation();
00194               int x=lo.x+si.width-getSize().width+20;
00195               int y=lo.y+si.height/2+40;
00196               if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
00197               if (x<10) x=10;
00198               if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
00199               if (y<10) y=10;
00200               setLocation(x,y);
00201        }
00202 }