/*
* PageFormatDialog.java
*
* Created on April 20, 2007, 1:27 PM
*/
package ika.gui;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
/**
* A dialog that lets the user enter a width and height value in millimeters.
* The values are stored in the GUI - not as it should properly be done!
* Usage: Dimension2D dim = PageFormatDialog.showDialog(null);
* @author Bernhard Jenny, Institute of Cartography, ETH Zurich.
*/
public class PageFormatDialog extends javax.swing.JDialog {
/**
* Each time a dialog is opened, we use this shared instance.
*/
private static PageFormatDialog dialog = null;
/**
* Remember whether the user canceled.
*/
private boolean abort = false;
/**
* Returns the last page format entered by the user.
*/
public static PageFormat getPageFormat() {
if (dialog == null || dialog.abort)
return null;
return dialog.pageFormatPanel.getPageFormat();
}
public static void setPageFormat(Frame ownerFrame, PageFormat pageFormat) {
if (dialog == null)
initDialog(ownerFrame);
dialog.pageFormatPanel.setPageFormat(pageFormat);
}
/**
* Shows a modal dialog and returns the dimension selected by the user
* or null if the user cancels.
*/
public static PageFormat showDialog(Frame ownerFrame) {
if (dialog == null)
initDialog(ownerFrame);
dialog.abort = false;
dialog.setModal(true);
dialog.setVisible(true);
return dialog.abort ? null : dialog.pageFormatPanel.getPageFormat();
}
/**
* Shows a modal dialog and returns the dimension selected by the user
* or null if the user cancels.
*/
public static PageFormat showDialog(Frame ownerFrame, boolean showScale) {
if (dialog == null)
initDialog(ownerFrame);
dialog.abort = false;
dialog.pageFormatPanel.setShowScale(showScale);
dialog.pack();
dialog.setModal(true);
dialog.setVisible(true);
return dialog.abort ? null : dialog.pageFormatPanel.getPageFormat();
}
private static void initDialog(Frame ownerFrame) {
if (dialog != null)
return ;
dialog = new PageFormatDialog(ownerFrame, true);
// location
java.awt.Dimension screenDim = dialog.getToolkit().getScreenSize();
java.awt.Dimension dialogDim = dialog.getSize();
dialog.setLocation((screenDim.width - dialogDim.width) / 2,
(screenDim.height - dialogDim.height) / 2 );
// ok button reacts on return key, cancel button on escape key.
dialog.getRootPane().setDefaultButton(dialog.okButton);
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.abort = true;
}
};
dialog.cancelButton.registerKeyboardAction(l, "EscapeKey",
KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE,
0 , true), JComponent.WHEN_IN_FOCUSED_WINDOW);
}
/** Creates new form PageFormatDialog */
public PageFormatDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
pageFormatPanel = new ika.gui.PageFormatPanel();
jPanel1 = new javax.swing.JPanel();
cancelButton = new javax.swing.JButton();
okButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Map Scale & Size");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
pageFormatPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(30, 30, 30, 30));
getContentPane().add(pageFormatPanel, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 30, 20));
cancelButton.setText("Cancel");
cancelButton.setDefaultCapable(false);
cancelButton.setPreferredSize(new java.awt.Dimension(90, 29));
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
jPanel1.add(cancelButton);
okButton.setText("OK");
okButton.setPreferredSize(new java.awt.Dimension(90, 29));
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
jPanel1.add(okButton);
getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
dialog.setVisible(false);
this.abort = true;
}//GEN-LAST:event_formWindowClosing
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
dialog.setVisible(false);
this.abort = true;
}//GEN-LAST:event_cancelButtonActionPerformed
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
dialog.setVisible(false);
this.abort = false;
}//GEN-LAST:event_okButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton okButton;
private ika.gui.PageFormatPanel pageFormatPanel;
// End of variables declaration//GEN-END:variables
}