001    /*
002     * GammaCorrectionDialog
003     * 
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.gui.awt.dialogs;
009    
010    import java.awt.BorderLayout;
011    import java.awt.Button;
012    import java.awt.Dialog;
013    import java.awt.Frame;
014    import java.awt.Label;
015    import java.awt.Panel;
016    import java.awt.TextField;
017    import java.awt.event.ActionEvent;
018    import java.awt.event.ActionListener;
019    import java.awt.event.KeyEvent;
020    import java.awt.event.KeyListener;
021    import net.sourceforge.jiu.apps.Strings;
022    
023    /**
024     * A dialog to enter the parameters for a gamma correction operation.
025     * @author Marco Schmidt
026     */
027    public class GammaCorrectionDialog extends Dialog implements ActionListener, KeyListener
028    {
029            private Button ok;
030            private Button cancel;
031            private TextField gammaTextField;
032            private boolean pressedOk;
033            private Double result;
034            private final double MAX_GAMMA;
035    
036            /**
037             * Creates a GammaCorrectionDialog.
038             * @param owner the Frame this dialog will belong to
039             * @param strings Strings resource used for text messages
040             * @param initialValue the value to be set when the dialog pops up
041             * @param maxGamma the maximum allowed gamma value
042             */
043            public GammaCorrectionDialog(Frame owner, Strings strings, double initialValue, double maxGamma)
044            {
045                    super(owner, strings.get(Strings.ADJUST_GAMMA), true);
046                    MAX_GAMMA = maxGamma;
047    
048                    Panel panel = new Panel(new BorderLayout());
049                    panel.add(new Label(strings.get(Strings.ENTER_GAMMA_VALUE)), BorderLayout.CENTER);
050                    gammaTextField = new TextField(Double.toString(initialValue));
051                    gammaTextField.addKeyListener(this);
052                    panel.add(gammaTextField, BorderLayout.EAST);
053                    add(panel, BorderLayout.CENTER);
054    
055                    panel = new Panel();
056                    ok = new Button(strings.get(Strings.OK));
057                    ok.addActionListener(this);
058                    cancel = new Button(strings.get(Strings.CANCEL));
059                    cancel.addActionListener(this);
060                    panel.add(ok);
061                    panel.add(cancel);
062                    add(panel, BorderLayout.SOUTH);
063    
064                    pack();
065                    Dialogs.center(this);
066            }
067    
068            /**
069             * Hides (closes) this dialog if the OK button was source of the action event
070             * (e.g. if the button was pressed).
071             */
072            public void actionPerformed(ActionEvent e)
073            {
074                    if (e.getSource() == ok)
075                    {
076                            pressedOk = true;
077                            result = getValue(gammaTextField);
078                            setVisible(false);
079                    }
080                    else
081                    if (e.getSource() == cancel)
082                    {
083                            setVisible(false);
084                    }
085            }
086    
087            private Double getValue(TextField tf)
088            {
089                    if (tf == null)
090                    {
091                            return null;
092                    }
093                    double d;
094                    try
095                    {
096                            d = (Double.valueOf(tf.getText())).doubleValue();
097                    }
098                    catch(NumberFormatException nfe)
099                    {
100                            return null;
101                    }
102                    if (d <= 0.0 || d > MAX_GAMMA)
103                    {
104                            return null;
105                    }
106                    return new Double(d);
107            }
108    
109            public Double getValue()
110            {
111                    return result;
112            }
113    
114            public boolean hasPressedOk()
115            {
116                    return pressedOk;
117            }
118    
119            public void handleKeys(KeyEvent e)
120            {
121                    ok.setEnabled(getValue(gammaTextField) != null);
122            }
123    
124            public void keyPressed(KeyEvent e)
125            {
126                    handleKeys(e);
127            }
128    
129            public void keyReleased(KeyEvent e)
130            {
131                    handleKeys(e);
132            }
133    
134            public void keyTyped(KeyEvent e)
135            {
136                    handleKeys(e);
137            }
138    }