package rene.gui;
import java.awt.*;
import java.awt.event.*;
import rene.dialogs.InfoDialog;
/**
A dialog, which can be closed by clicking on the close window
field (a cross on the top right corner in Windows 95), or by
pressing the escape key.
<p>
Moreover, the dialog is a DoActionListener, which makes it possible
to use the simplified TextFieldAction etc.
*/
public class CloseDialog
extends Dialog
{ boolean Dispose=true;
public boolean Aborted=false;
{ super(f,s,modal);
F=f;
if (Global.ControlBackground!=null)
setBackground(Global.ControlBackground);
addWindowListener(this);
addKeyListener(this);
addFocusListener(this);
}
{ if (close()) doclose();
}
/**
@return true if the dialog is closed.
*/
public boolean close ()
{ return true;
}
/**
Calls close(), when the escape key is pressed.
@return true if the dialog may close.
*/
public boolean escape ()
{ return close();
}
{ E=e;
doAction(e.getActionCommand());
}
public void doAction
(String o
)
{ if ("Close".equals(o) && close())
{ Aborted=true;
doclose();
}
else if (o.equals("Help"))
{ showHelp();
}
}
public void showHelp ()
InfoDialog id=new InfoDialog(F);
}
public void itemAction
(String o,
boolean flag
) {}
{ if (e.
getKeyCode()==
KeyEvent.
VK_ESCAPE && escape
()) doclose
();
}
/**
Closes the dialog. This may be used in subclasses to
do some action. Then call super.doclose()
*/
public void doclose ()
{ setVisible(false);
// Because of a bug in Linux Java 1.4.2 etc.
// dispose in a separate thread.
{ public void run ()
{ if (Dispose) dispose();
}
};
t.start();
}
public void center
(Frame f
)
si=f.getSize(),
d=getSize(),
dscreen=getToolkit().getScreenSize();
Point lo=f.
getLocation();
int x=lo.x+si.width/2-d.width/2;
int y=lo.y+si.height/2-d.height/2;
if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
if (x<10) x=10;
if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
if (y<10) y=10;
setLocation(x,y);
}
si=f.getSize(),
d=dialog.getSize(),
dscreen=f.getToolkit().getScreenSize();
Point lo=f.
getLocation();
int x=lo.x+si.width/2-d.width/2;
int y=lo.y+si.height/2-d.height/2;
if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
if (x<10) x=10;
if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
if (y<10) y=10;
dialog.setLocation(x,y);
}
public void centerOut
(Frame f
)
dscreen=getToolkit().getScreenSize();
Point lo=f.
getLocation();
int x=lo.x+si.width-getSize().width+20;
int y=lo.y+si.height/2+40;
if (x+d.width>dscreen.width) x=dscreen.width-d.width-10;
if (x<10) x=10;
if (y+d.height>dscreen.height) y=dscreen.height-d.height-10;
if (y<10) y=10;
setLocation(x,y);
}
public void center ()
{ Dimension d=getSize
(),dscreen=getToolkit
().
getScreenSize();
setLocation((dscreen.width-d.width)/2,
(dscreen.height-d.height)/2);
}
/**
Note window position in Global.
*/
public void notePosition
(String name
)
Global.setParameter(name+".x",l.x);
Global.setParameter(name+".y",l.y);
Global.setParameter(name+".w",d.width);
if (d.height-Global.getParameter(name+".h",0)!=19)
// works around a bug in Windows
Global.setParameter(name+".h",d.height);
boolean maximized=false;
}
/**
Set window position and size.
*/
public void setPosition
(String name
)
Dimension dscreen=getToolkit
().
getScreenSize();
int x=Global.getParameter(name+".x",l.x);
int y=Global.getParameter(name+".y",l.y);
int w=Global.getParameter(name+".w",d.width);
int h=Global.getParameter(name+".h",d.height);
if (w>dscreen.width) w=dscreen.width;
if (h>dscreen.height) h=dscreen.height;
if (x<0) x=0;
if (x+w>dscreen.width) x=dscreen.width-w;
if (y<0) y=0;
if (y+h>dscreen.height) y=dscreen.height-h;
setLocation(x,y);
setSize(w,h);
}
/**
Override to set the focus somewhere.
*/
/**
Note window size in Global.
*/
public void noteSize
(String name
)
Global.setParameter(name+".w",d.width);
Global.setParameter(name+".h",d.height);
}
/**
Set window size.
*/
public void setSize
(String name
)
{ if (!Global.haveParameter(name+".w")) pack();
else
int w=Global.getParameter(name+".w",d.width);
int h=Global.getParameter(name+".h",d.height);
setSize(w,h);
}
}
/**
This inihibits dispose(), when the dialog is closed.
*/
public void setDispose (boolean flag)
{ Dispose=flag;
}
public boolean isAborted ()
{ return Aborted;
}
/**
* To add a help button to children.
* @param p
* @param subject
*/
{ p.add(new MyLabel(""));
p.add(new ButtonAction(this,Global.name("help"),"Help"));
}
}