Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import rene.dialogs.InfoDialog;
  6.  
  7. /**
  8. A dialog, which can be closed by clicking on the close window
  9. field (a cross on the top right corner in Windows 95), or by
  10. pressing the escape key.
  11. <p>
  12. Moreover, the dialog is a DoActionListener, which makes it possible
  13. to use the simplified TextFieldAction etc.
  14. */
  15.  
  16. public class CloseDialog extends Dialog
  17.     implements WindowListener, ActionListener, DoActionListener, KeyListener,
  18.     FocusListener
  19. {       boolean Dispose=true;
  20.         public boolean Aborted=false;
  21.         Frame F;       
  22.         public String Subject="";
  23.  
  24.         public CloseDialog (Frame f, String s, boolean modal)
  25.         {       super(f,s,modal);
  26.                 F=f;
  27.                 if (Global.ControlBackground!=null)
  28.                         setBackground(Global.ControlBackground);
  29.             addWindowListener(this);
  30.             addKeyListener(this);
  31.             addFocusListener(this);
  32.         }
  33.  
  34.         public void windowActivated (WindowEvent e) {}
  35.         public void windowClosed (WindowEvent e) {}
  36.         public void windowClosing (WindowEvent e)
  37.         {   if (close()) doclose();
  38.         }
  39.         public void windowDeactivated (WindowEvent e) {}
  40.         public void windowDeiconified (WindowEvent e) {}
  41.         public void windowIconified (WindowEvent e) {}
  42.         public void windowOpened (WindowEvent e) {}
  43.  
  44.         /**
  45.         @return true if the dialog is closed.
  46.         */
  47.         public boolean close ()
  48.         {       return true;
  49.         }
  50.  
  51.         /**
  52.         Calls close(), when the escape key is pressed.
  53.         @return true if the dialog may close.
  54.         */
  55.         public boolean escape ()
  56.         {       return close();
  57.         }
  58.        
  59.         public ActionEvent E;
  60.  
  61.         public void actionPerformed (ActionEvent e)
  62.         {   E=e;
  63.                 doAction(e.getActionCommand());
  64.         }
  65.         public void doAction (String o)
  66.         {   if ("Close".equals(o) && close())
  67.                 {       Aborted=true;
  68.                         doclose();
  69.                 }
  70.                 else if (o.equals("Help"))
  71.                 {       showHelp();
  72.                 }
  73.         }
  74.        
  75.         public void showHelp ()
  76.         {       InfoDialog.Subject=Subject;
  77.                 InfoDialog id=new InfoDialog(F);
  78.         }
  79.        
  80.         public void itemAction (String o, boolean flag) {}
  81.  
  82.         public void keyPressed (KeyEvent e)
  83.         {       if (e.getKeyCode()==KeyEvent.VK_ESCAPE && escape()) doclose();
  84.         }
  85.         public void keyReleased (KeyEvent e) {}
  86.         public void keyTyped (KeyEvent e) {}
  87.  
  88.         /**
  89.         Closes the dialog. This may be used in subclasses to
  90.         do some action. Then call super.doclose()
  91.         */
  92.         public void doclose ()
  93.         {       setVisible(false);
  94.                 // Because of a bug in Linux Java 1.4.2 etc.
  95.                 // dispose in a separate thread.
  96.                 Thread t=new Thread ()
  97.                 {       public void run ()
  98.                         {       if (Dispose) dispose();
  99.                         }
  100.                 };
  101.                 t.start();
  102.         }
  103.  
  104.         public void center (Frame f)
  105.         {       Dimension
  106.                         si=f.getSize(),
  107.                         d=getSize(),
  108.                         dscreen=getToolkit().getScreenSize();
  109.                 Point lo=f.getLocation();
  110.                 int x=lo.x+si.width/2-d.width/2;
  111.                 int y=lo.y+si.height/2-d.height/2;
  112.                 if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
  113.                 if (x<10) x=10;
  114.                 if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
  115.                 if (y<10) y=10;
  116.                 setLocation(x,y);
  117.         }
  118.         static public void center (Frame f, Dialog dialog)
  119.         {       Dimension
  120.                         si=f.getSize(),
  121.                         d=dialog.getSize(),
  122.                         dscreen=f.getToolkit().getScreenSize();
  123.                 Point lo=f.getLocation();
  124.                 int x=lo.x+si.width/2-d.width/2;
  125.                 int y=lo.y+si.height/2-d.height/2;
  126.                 if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
  127.                 if (x<10) x=10;
  128.                 if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
  129.                 if (y<10) y=10;
  130.                 dialog.setLocation(x,y);
  131.         }
  132.         public void centerOut (Frame f)
  133.         {       Dimension si=f.getSize(),d=getSize(),
  134.                         dscreen=getToolkit().getScreenSize();
  135.                 Point lo=f.getLocation();
  136.                 int x=lo.x+si.width-getSize().width+20;
  137.                 int y=lo.y+si.height/2+40;
  138.                 if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
  139.                 if (x<10) x=10;
  140.                 if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
  141.                 if (y<10) y=10;
  142.                 setLocation(x,y);
  143.         }
  144.         public void center ()
  145.         {       Dimension d=getSize(),dscreen=getToolkit().getScreenSize();
  146.                 setLocation((dscreen.width-d.width)/2,
  147.                         (dscreen.height-d.height)/2);
  148.         }
  149.        
  150.         /**
  151.         Note window position in Global.
  152.         */
  153.         public void notePosition (String name)
  154.         {       Point l=getLocation();
  155.                 Dimension d=getSize();
  156.                 Global.setParameter(name+".x",l.x);
  157.                 Global.setParameter(name+".y",l.y);
  158.                 Global.setParameter(name+".w",d.width);
  159.                 if (d.height-Global.getParameter(name+".h",0)!=19)
  160.                         // works around a bug in Windows
  161.                         Global.setParameter(name+".h",d.height);
  162.                 boolean maximized=false;
  163.         }
  164.  
  165.         /**
  166.         Set window position and size.
  167.         */
  168.         public void setPosition (String name)
  169.         {       Point l=getLocation();
  170.                 Dimension d=getSize();
  171.                 Dimension dscreen=getToolkit().getScreenSize();
  172.                 int x=Global.getParameter(name+".x",l.x);
  173.                 int y=Global.getParameter(name+".y",l.y);
  174.                 int w=Global.getParameter(name+".w",d.width);
  175.                 int h=Global.getParameter(name+".h",d.height);
  176.                 if (w>dscreen.width) w=dscreen.width;
  177.                 if (h>dscreen.height) h=dscreen.height;
  178.                 if (x<0) x=0;
  179.                 if (x+w>dscreen.width) x=dscreen.width-w;
  180.                 if (y<0) y=0;
  181.                 if (y+h>dscreen.height) y=dscreen.height-h;
  182.                 setLocation(x,y);
  183.                 setSize(w,h);
  184.         }
  185.        
  186.        
  187.         /**
  188.         Override to set the focus somewhere.
  189.         */
  190.         public void focusGained (FocusEvent e) {}
  191.         public void focusLost (FocusEvent e) {}
  192.  
  193.         /**
  194.         Note window size in Global.
  195.         */
  196.         public void noteSize (String name)
  197.         {       Dimension d=getSize();
  198.                 Global.setParameter(name+".w",d.width);
  199.                 Global.setParameter(name+".h",d.height);
  200.         }
  201.  
  202.         /**
  203.         Set window size.
  204.         */
  205.         public void setSize (String name)
  206.         {       if (!Global.haveParameter(name+".w")) pack();
  207.                 else
  208.                 {       Dimension d=getSize();
  209.                         int w=Global.getParameter(name+".w",d.width);
  210.                         int h=Global.getParameter(name+".h",d.height);
  211.                         setSize(w,h);
  212.                 }
  213.         }
  214.        
  215.         /**
  216.         This inihibits dispose(), when the dialog is closed.
  217.         */
  218.         public void setDispose (boolean flag)
  219.         {       Dispose=flag;
  220.         }
  221.        
  222.         public boolean isAborted ()
  223.         {       return Aborted;
  224.         }
  225.        
  226.         /**
  227.          * To add a help button to children.
  228.          * @param p
  229.          * @param subject
  230.          */
  231.         public void addHelp (Panel p, String subject)
  232.         {       p.add(new MyLabel(""));
  233.                 p.add(new ButtonAction(this,Global.name("help"),"Help"));
  234.                 Subject=subject;
  235.         }
  236.  
  237. }
  238.