/*========================================================================= Program: FusionViewer Module: $RCSfile: TickmarkSpacingDialog.java,v $ Language: Java Date: $Date: 2007/02/02 19:27:57 $ Version: $Revision: 1.2 $ Copyright (c) Insightful Corporation. All rights reserved. See Copyright.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ package org.fusionviewer.ui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.KeyStroke; /**\class TickmarkSpacingDialog *\brief Dialog box for entering split window display mode tickmark spacing. */ public class TickmarkSpacingDialog extends JDialog { private boolean m_okPressed = false; private JTextField m_spacingField; private float m_spacing; private Frame m_frame; /** * Create a dialog box with the indicated owner * * @param owner parent frame of this dialog box * @param spacing initial spacing */ public TickmarkSpacingDialog(Frame owner, float spacing) { super(owner, true); m_frame = owner; m_spacing = spacing; final int BORDER = 10; Box panel = Box.createVerticalBox(); panel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER)); panel.add(createFields(spacing)); panel.add(Box.createVerticalStrut(10)); panel.add(createButtons()); getContentPane().add(panel); getRootPane().registerKeyboardAction(new DialogCloseButtonListener(false), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); setTitle("Tickmark Spacing"); pack(); PlatformUtility.centerWindow(this); } /** * Shows the dialog and blocks until the user dismisses it. * * @return true if the user clicked OK. */ public boolean run() { setVisible(true); return m_okPressed; } /* * Create the dialog box fields. */ private Component createFields(float initialSpacing) { JPanel panel = new JPanel(); final int BORDER = 15; panel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER)); panel.setLayout(new BorderLayout(5, 5)); final int NUM_COLUMNS = 10; m_spacingField = new JTextField(FloatFormatter.formatFloat(initialSpacing), NUM_COLUMNS); panel.add(new JLabel("Tickmark Spacing (mm)"), BorderLayout.WEST); panel.add(m_spacingField); return panel; } /* * Creates the OK and Cancel buttons. */ private Component createButtons() { JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton okBtn = new JButton("Set"); PlatformUtility.setMnemonic(okBtn, KeyEvent.VK_S); okBtn.addActionListener(new DialogCloseButtonListener(true)); buttonPanel.add(okBtn); getRootPane().setDefaultButton(okBtn); JButton cancelButton = new JButton("Cancel"); PlatformUtility.setMnemonic(cancelButton, KeyEvent.VK_C); cancelButton.addActionListener(new DialogCloseButtonListener(false)); buttonPanel.add(cancelButton); return buttonPanel; } /* * Listener for the OK and Cancel buttons. */ private class DialogCloseButtonListener implements ActionListener { private boolean m_okValue; /* * Create the button listener. okValue is true if this is the listener * for the OK button. */ public DialogCloseButtonListener(boolean okValue) { m_okValue = okValue; } public void actionPerformed(ActionEvent e) { // Set flag in the enclosing class for whether the OK or Cancel button was pressed. m_okPressed = m_okValue; // Check for valid tickmark spacing if OK was clicked if (m_okValue) { try { m_spacing = Float.parseFloat(m_spacingField.getText()); if (m_spacing <= 0) JOptionPane.showMessageDialog(m_frame, "Spacing must be greater than 0.", "Invalid Input", JOptionPane.ERROR_MESSAGE); else dispose(); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(m_frame, "Please enter a number.", "Invalid Input", JOptionPane.ERROR_MESSAGE); } } else dispose(); } } /** * Get the tickmark spacing entered by the user. * * @return tickmark spacing */ public float getTickmarkSpacing() { return m_spacing; } }