/*========================================================================= Program: FusionViewer Module: $RCSfile: WindowLevelDialog.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.Component; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; 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 WindowLevelDialog *\brief Dialog box for entering window and level values. */ public class WindowLevelDialog extends JDialog { private boolean m_okPressed = false; private JTextField m_windowField, m_levelField; private Frame m_frame; private float m_window, m_level; /** * Create a dialog box with the indicated owner * * @param owner parent frame of this dialog box * @param window Display window * @param level window level * @param minValue minimum pixel value * @param maxValue maximum pixel value */ public WindowLevelDialog(Frame owner, float window, float level, float minValue, float maxValue) { super(owner, true); m_frame = owner; m_window = window; m_level = level; final int BORDER = 10; Box panel = Box.createVerticalBox(); panel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER)); panel.add(createImageInfo(minValue, maxValue)); panel.add(Box.createVerticalStrut(5)); panel.add(createFields(window, level)); 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("Window and Level"); 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 a panel to show the image minimum and maximum values. */ private Component createImageInfo(float minValue, float maxValue) { JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Image")); panel.setLayout(new GridBagLayout()); final int BORDER = 15; Insets insets = panel.getInsets(); insets.left = insets.right = insets.top = insets.bottom = BORDER; GridBagConstraints constraints = new GridBagConstraints(); final int INTERNAL_BORDER = 5; constraints.insets = new Insets(INTERNAL_BORDER, INTERNAL_BORDER, INTERNAL_BORDER, INTERNAL_BORDER); constraints.anchor = GridBagConstraints.LINE_START; constraints.fill = GridBagConstraints.NONE; constraints.gridx = 0; constraints.gridy = 0; panel.add(new JLabel("Minimum"), constraints); constraints.gridy++; panel.add(new JLabel("Maximum"), constraints); constraints.gridy = 0; constraints.gridx = 1; panel.add(new JLabel(FloatFormatter.formatFloat(minValue)), constraints); constraints.gridy++; panel.add(new JLabel(FloatFormatter.formatFloat(maxValue)), constraints); return panel; } /** * @return */ private Component createFields(float window, float level) { JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); final int BORDER = 15; panel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER)); final int NUM_COLUMNS = 10; m_windowField = new JTextField(FloatFormatter.formatFloat(window), NUM_COLUMNS); m_levelField = new JTextField(FloatFormatter.formatFloat(level), NUM_COLUMNS); GridBagConstraints constraints = new GridBagConstraints(); final int INTERNAL_BORDER = 5; constraints.insets = new Insets(INTERNAL_BORDER, INTERNAL_BORDER, INTERNAL_BORDER, INTERNAL_BORDER); constraints.anchor = GridBagConstraints.LINE_START; constraints.fill = GridBagConstraints.NONE; constraints.gridx = 0; constraints.gridy = 0; panel.add(new JLabel("Window"), constraints); constraints.gridy++; panel.add(new JLabel("Level"), constraints); constraints.gridy = 0; constraints.gridx = 1; panel.add(m_windowField, constraints); constraints.gridy++; panel.add(m_levelField, constraints); 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; } /** * @return Returns the level. */ public float getLevel() { return m_level; } /** * @return Returns the window. */ public float getWindow() { return m_window; } /* * Listener for the OK and Cancel buttons. */ private class DialogCloseButtonListener implements ActionListener { private boolean m_okValue; public DialogCloseButtonListener(boolean okValue) { m_okValue = okValue; } public void actionPerformed(ActionEvent e) { m_okPressed = m_okValue; if (m_okValue) { try { m_window = Float.parseFloat(m_windowField.getText()); m_level = Float.parseFloat(m_levelField.getText()); dispose(); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(m_frame, "Please enter a number.", "Invalid Input", JOptionPane.ERROR_MESSAGE); } } else dispose(); } } }