001    /*
002     * WindowSizeDialog
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.GridLayout;
015    import java.awt.Label;
016    import java.awt.Panel;
017    import java.awt.TextComponent;
018    import java.awt.TextField;
019    import java.awt.event.ActionEvent;
020    import java.awt.event.ActionListener;
021    import java.awt.event.KeyEvent;
022    import java.awt.event.KeyListener;
023    import net.sourceforge.jiu.apps.Strings;
024    
025    /**
026     * A dialog to enter values for the width and height of a window (typically
027     * for a spatial filter like median or mean.
028     *
029     * @author Marco Schmidt
030     */
031    public class WindowSizeDialog extends Dialog implements
032            ActionListener, KeyListener
033    {
034            private Button ok;
035            private Button cancel;
036            private TextField width;
037            private TextField height;
038            private boolean pressedOk;
039    
040            /**
041             * @param owner the Frame this dialog will belong to
042             */
043            public WindowSizeDialog(Frame owner, Strings strings, int titleIndex, int initialWidth, int initialHeight)
044            {
045                    super(owner, strings.get(titleIndex), true);
046                    pressedOk = false;
047                    Panel panel = new Panel();
048                    panel.setLayout(new GridLayout(0, 2));
049    
050                    panel.add(new Label(strings.get(Strings.ENTER_WINDOW_SIZE)));
051                    panel.add(new Label(""));
052    
053                    panel.add(new Label(strings.get(Strings.WINDOW_WIDTH)));
054                    width = new TextField(Integer.toString(initialWidth));
055                    width.addKeyListener(this);
056                    panel.add(width);
057    
058                    panel.add(new Label(strings.get(Strings.WINDOW_HEIGHT)));
059                    height = new TextField(Integer.toString(initialHeight));
060                    height.addKeyListener(this);
061                    panel.add(height);
062    
063                    add(panel, BorderLayout.CENTER);
064    
065                    ok = new Button(strings.get(Strings.OK));
066                    ok.addActionListener(this);
067                    cancel = new Button(strings.get(Strings.CANCEL));
068                    cancel.addActionListener(this);
069    
070                    panel = new Panel();
071                    panel.add(ok);
072                    panel.add(cancel);
073                    add(panel, BorderLayout.SOUTH);
074    
075                    updateOkButton();
076    
077                    pack();
078                    Dialogs.center(this);
079            }
080    
081            /**
082             * Hides (closes) this dialog if the OK button was source of the action event
083             * (e.g. if the button was pressed).
084             */
085            public void actionPerformed(ActionEvent e)
086            {
087                    if (e.getSource() == ok)
088                    {
089                            pressedOk = true;
090                            setVisible(false);
091                    }
092                    else
093                    if (e.getSource() == cancel)
094                    {
095                            setVisible(false);
096                    }
097            }
098    
099            public int getHeightValue()
100            {
101                    return getValue(height);
102            }
103    
104            public int getWidthValue()
105            {
106                    return getValue(width);
107            }
108    
109            /**
110             * Attempts to convert the content of the argument text component
111             * to an <code>int</code>; if successful, returns that int, otherwise
112             * -1000 is returned.
113             * @param textField the text component that is supposed to hold an int value
114             * @return int representation of the text component's data
115             */
116            private int getValue(TextComponent textField)
117            {
118                    try
119                    {
120                            return Integer.parseInt(textField.getText());
121                    }
122                    catch (NumberFormatException nfe)
123                    {
124                            return -1000;
125                    }
126            }       
127    
128            public boolean hasPressedOk()
129            {
130                    return pressedOk;
131            }
132    
133            private void updateOkButton()
134            {
135                    int w = getWidthValue();
136                    int h = getHeightValue();
137                    ok.setEnabled(w >= 1 && h >= 1 && ((w % 2) == 1) && ((h % 2) == 1));
138            }
139    
140            public void keyPressed(KeyEvent e)
141            {
142                    updateOkButton();
143            }
144    
145            public void keyReleased(KeyEvent e)
146            {
147                    updateOkButton();
148            }
149    
150            public void keyTyped(KeyEvent e)
151            {
152                    updateOkButton();
153            }
154    }