Update of /cvsroot/freenet/freenet/src/freenet/node/rt/testapplication
In directory sc8-pr-cvs1:/tmp/cvs-serv31576/src/freenet/node/rt/testapplication
Added Files:
MessageBox.java
Log Message:
Added an MessageBox class that is blatantly stolen from
http://www.javaworld.com/javaworld/javatips/jw-javatip48.html. Question about wheter
this is allowed has been forwarded to JW.
--- NEW FILE: MessageBox.java ---
package freenet.node.rt.testapplication;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
/**
* Provides a reusable window that presents a message and
* choice buttons to the user. A modal dialog is used.
* Since a thread is used to set the dialog to visible,
* when the client calls ask() it will not block.
*
* The client may implement ActionListener, which has:
* public void actionPerformed(ActionEvent evt)
* if user response notification is desired.
*
* @author Jack Harich
* @author Iakin
*/
public class MessageBox implements Runnable,
ActionListener, WindowListener, KeyListener {
//---------- Private Fields ------------------------------
private ActionListener listener;
private JDialog dialog;
private String closeWindowCommand = "CloseRequested";
private String title;
private JFrame frame;
private boolean frameNotProvided;
private JPanel buttonPanel = new JPanel();
private ImageIcon imageIcon;
private Object syncObj = new Object();
private String sModalResult;
//---------- Initialization ------------------------------
/**
* This convenience constructor is used to delare the
* listener that will be notified when a button is clicked.
* The listener must implement ActionListener.
*/
public MessageBox(ActionListener listener) {
this();
this.listener = listener;
}
/**
* This constructor is used for no listener, such as for
* a simple okay dialog.
*/
public MessageBox() {
}
// Unit test. Shows only simple features.
public static void main(String args[]) {
MessageBox box = new MessageBox();
box.setTitle("Test MessageBox");
box.useImageIcon("LightBulb.gif");
box.askYesNo("Tell me now.\nDo you like Java?");
}
//---------- Runnable Implementation ---------------------
/**
* This prevents the caller from blocking on ask(), which
* if this class is used on an awt event thread would
* cause a deadlock.
*/
public void run() {
dialog.setVisible(true);
}
//---------- ActionListener Implementation ---------------
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
dialog.setVisible(false);
dialog.dispose();
if (frameNotProvided) frame.dispose();
if (listener != null) {
listener.actionPerformed(evt);
}
}
//---------- WindowListener Implementatons ---------------
public void windowClosing(WindowEvent evt) {
// User clicked on X or chose Close selection
fireCloseRequested();
}
public void windowClosed(WindowEvent evt) { }
public void windowDeiconified(WindowEvent evt) { }
public void windowIconified(WindowEvent evt) { }
public void windowOpened(WindowEvent evt) { }
public void windowActivated(WindowEvent evt) { }
public void windowDeactivated(WindowEvent evt) { }
//---------- KeyListener Implementation ------------------
public void keyTyped(KeyEvent evt) { }
public void keyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
fireCloseRequested();
}
}
public void keyReleased(KeyEvent evt) { }
private void fireCloseRequested() {
ActionEvent event = new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, closeWindowCommand);
actionPerformed(event);
}
//---------- Public Methods ------------------------------
/**
* This set the listener to be notified of button clicks
* and WindowClosing events.
*/
public void setActionListener(ActionListener listener) {
this.listener = listener;
}
public void setTitle(String title) {
this.title = title;
}
/**
* If a Frame is provided then it is used to instantiate
* the modal Dialog. Otherwise a temporary Frame is used.
* Providing a Frame will have the effect of putting the
* focus back on that Frame when the MessageBox is closed
* or a button is clicked.
*/
public void setFrame(JFrame frame) { // Optional
this.frame = frame;
}
/**
* Sets the ActionCommand used in the ActionEvent when the
* user attempts to close the window. The window may be
* closed by clicking on "X", choosing Close from the
* window menu, or pressing the Escape key. The default
* command is "CloseRequested", which is just what a Close
* choice button would probably have as a command.
*/
public void setCloseWindowCommand(String command) {
closeWindowCommand = command;
}
/**
* This is handy for providing a small image that will be
* displayed to the left of the message.
*/
public void useImageIcon(ImageIcon imageIcon) {
this.imageIcon = imageIcon;
}
/**
* This loads the image from the specified @param fileName,
* which must be in the same directory as this class.
* For example @param fileName might be "LightBulb.gif".
*/
public void useImageIcon(String fileName) {
try {
URL u = MessageBox.class.getResource(fileName);
if(u != null)
useImageIcon(new ImageIcon(u));
} catch(Exception ex) {
print("MessageBox.helpfulHint() - Cannot load image " + fileName);
ex.printStackTrace();
}
}
/**
* The @param label will be used for the button and the
* @param command will be returned to the listener.
*/
public void addChoice(String label, String command) {
Button button = new Button(label);
button.setActionCommand(command);
button.addActionListener(this);
button.addKeyListener(this);
buttonPanel.add(button);
}
/**
* A convenience method that assumes the command is the
* same as the label.
*/
public void addChoice(String label) {
addChoice(label, label);
}
/**
* One of the "ask" methods must be the last call when
* using a MessageBox. This is the simplest "ask" method.
* It presents the provided @param message.
*/
public void ask(String message){
if (frame == null) {
frame = new JFrame();
frameNotProvided = true;
} else {
frameNotProvided = false;
}
dialog = new JDialog(frame, true); // Modal
dialog.addWindowListener(this);
dialog.addKeyListener(this);
dialog.setTitle(title);
dialog.getContentPane().setLayout(new BorderLayout(5, 5));
JPanel messagePanel = createMultiLinePanel(message);
if (imageIcon == null) {
dialog.getContentPane().add(messagePanel,BorderLayout.CENTER);
} else {
JPanel centerPanel = new JPanel();
centerPanel.add(new JLabel(imageIcon));
centerPanel.add(messagePanel);
dialog.getContentPane().add(centerPanel,BorderLayout.CENTER);
}
dialog.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
dialog.pack();
enforceMinimumSize(dialog, 200, 100);
centerWindow(dialog);
Toolkit.getDefaultToolkit().beep();
// Start a new thread to show the dialog
Thread thread = new Thread(this);
thread.start();
}
public String askModal(String message) throws InterruptedException{
this.setActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
sModalResult = e.getActionCommand();
synchronized(syncObj)
{
syncObj.notifyAll();
}
}
});
synchronized(syncObj)
{
ask(message);
syncObj.wait();
}
return sModalResult;
}
/**
* Same as ask(String message) except adds an "Okay" button.
*/
public void askOkay(String message){
addChoice("Okay");
ask(message);
}
public String askOkayModal(String message) throws InterruptedException{
addChoice("Okay");
return askModal(message);
}
/**
* Same as ask(String message) except adds "Yes" and "No"
* buttons.
*/
public void askYesNo(String message){
addChoice("Yes");
addChoice("No");
ask(message);
}
public String askYesNoModal(String message) throws InterruptedException{
addChoice("Yes");
addChoice("No");
return askModal(message);
}
//---------- Private Methods -----------------------------
private JPanel createMultiLinePanel(String message) {
JPanel mainPanel = new JPanel();
GridBagLayout gbLayout = new GridBagLayout();
mainPanel.setLayout(gbLayout);
addMultilineString(message, mainPanel);
return mainPanel;
}
// There are a variaty of ways to do this....
private void addMultilineString(String message,
Container container) {
GridBagConstraints constraints = getDefaultConstraints();
constraints.gridwidth = GridBagConstraints.REMAINDER;
// Insets() args are top, left, bottom, right
constraints.insets = new Insets(0,0,0,0);
GridBagLayout gbLayout = (GridBagLayout)container.getLayout();
while (message.length() > 0) {
int newLineIndex = message.indexOf('\n');
String line;
if (newLineIndex >= 0) {
line = message.substring(0, newLineIndex);
message = message.substring(newLineIndex + 1);
} else {
line = message;
message = "";
}
Label label = new Label(line);
gbLayout.setConstraints(label, constraints);
container.add(label);
}
}
private GridBagConstraints getDefaultConstraints() {
GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridheight = 1; // One row high
// Insets() args are top, left, bottom, right
constraints.insets = new Insets(4,4,4,4);
// fill of NONE means do not change size
constraints.fill = GridBagConstraints.NONE;
// WEST means align left
constraints.anchor = GridBagConstraints.WEST;
return constraints;
}
private void centerWindow(Window win) {
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
// If larger than screen, reduce window width or height
if (screenDim.width < win.getSize().width) {
win.setSize(screenDim.width, win.getSize().height);
}
if (screenDim.height < win.getSize().height) {
win.setSize(win.getSize().width, screenDim.height);
}
// Center Frame, Dialogue or Window on screen
int x = (screenDim.width - win.getSize().width) / 2;
int y = (screenDim.height - win.getSize().height) / 2;
win.setLocation(x, y);
}
private void enforceMinimumSize(Component comp,
int minWidth, int minHeight) {
if (comp.getSize().width < minWidth) {
comp.setSize(minWidth, comp.getSize().height);
}
if (comp.getSize().height < minHeight) {
comp.setSize(comp.getSize().width, minHeight);
}
}
//--- Std
private static void print(String text) {
System.out.println(text);
}
} // End class
_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs