001    /*
002     * ScaleDialog
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.Checkbox;
013    import java.awt.Choice;
014    import java.awt.Dialog;
015    import java.awt.Dimension;
016    import java.awt.Frame;
017    import java.awt.GridLayout;
018    import java.awt.Label;
019    import java.awt.Panel;
020    import java.awt.Rectangle;
021    import java.awt.TextComponent;
022    import java.awt.TextField;
023    import java.awt.Toolkit;
024    import java.awt.event.ActionEvent;
025    import java.awt.event.ActionListener;
026    import java.awt.event.KeyEvent;
027    import java.awt.event.KeyListener;
028    import net.sourceforge.jiu.apps.Strings;
029    
030    /**
031     * A dialog to enter the parameters for an image scaling operation.
032     * @author Marco Schmidt
033     */
034    public class ScaleDialog extends Dialog implements ActionListener, KeyListener
035    {
036            private Button ok;
037            private Button cancel;
038            private TextComponent heightTextField;
039            private TextComponent widthTextField;
040            private Checkbox maintainAspectRatio;
041            private Choice types;
042            private boolean pressedOk;
043            private String oldWidthString;
044            private String oldHeightString;
045            private int oldWidth;
046            private int oldHeight;
047            private int type;
048    
049            /**
050             * Creates an InfoDialog, a modal dialog to display a text message, centered on the desktop.
051             * @param owner the Frame this dialog will belong to
052             * @param strings the Strings resource used for text messages
053             * @param width the current width of the image
054             * @param height the current height of the image
055             * @param pickType determines whether the will be a Choice box for picking the type of scaling algorithm
056             * @param typeNames names of the image scaling algorithms
057             * @param initialType algorithm type to be initially selected
058             */
059            public ScaleDialog(Frame owner, Strings strings, int width, int height, boolean pickType, String[] typeNames, int initialType)
060            {
061                    super(owner, strings.get(Strings.SCALE_IMAGE), true);
062                    pressedOk = false;
063                    oldWidth = width;
064                    oldWidthString = Integer.toString(oldWidth);
065                    oldHeight = height;
066                    oldHeightString = Integer.toString(oldHeight);
067    
068                    Panel panel = new Panel();
069                    panel.setLayout(new GridLayout(0, 2));
070    
071                    Label widthLabel = new Label(strings.get(Strings.NEW_WIDTH));
072                    widthTextField = new TextField(Integer.toString(width), 6);
073                    widthTextField.addKeyListener(this);
074                    Label heightLabel = new Label(strings.get(Strings.NEW_HEIGHT));
075                    heightTextField = new TextField(Integer.toString(height), 6);
076                    heightTextField.addKeyListener(this);
077    
078                    panel.add(widthLabel);
079                    panel.add(widthTextField);
080    
081                    panel.add(heightLabel);
082                    panel.add(heightTextField);
083    
084                    panel.add(new Label(""));
085                    maintainAspectRatio = new Checkbox(strings.get(Strings.MAINTAIN_ASPECT_RATIO), true);
086                    panel.add(maintainAspectRatio);
087    
088                    type = initialType;
089                    if (pickType)
090                    {
091                            panel.add(new Label(strings.get(Strings.METHOD)));
092                            types = new Choice();
093                            for (int i = 0; i < typeNames.length; i++)
094                            {
095                                    types.add(typeNames[i]);
096                            }
097                            types.select(initialType);
098                            panel.add(types);
099                    }
100                    add(panel, BorderLayout.CENTER);
101    
102                    ok = new Button(strings.get(Strings.OK));
103                    ok.addActionListener(this);
104                    cancel = new Button(strings.get(Strings.CANCEL));
105                    cancel.addActionListener(this);
106    
107                    panel = new Panel();
108                    panel.add(ok);
109                    panel.add(cancel);
110                    add(panel, BorderLayout.SOUTH);
111    
112                    pack();
113                    center();
114            }
115    
116            /**
117             * Hides (closes) this dialog if the OK button was source of the action event
118             * (e.g. if the button was pressed).
119             */
120            public void actionPerformed(ActionEvent e)
121            {
122                    if (e.getSource() == ok)
123                    {
124                            pressedOk = true;
125                            setVisible(false);
126                    }
127                    else
128                    if (e.getSource() == cancel)
129                    {
130                            setVisible(false);
131                    }
132            }
133    
134            /**
135             * Centers the dialog on screen.
136             */
137            public void center()
138            {
139                    Rectangle rect = getBounds();
140                    int width = rect.width;
141                    int height = rect.height;
142                    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
143                    setLocation((screenSize.width / 2) - (width / 2),
144                            (screenSize.height / 2) - (height / 2));
145            }
146    
147            public int getHeightValue()
148            {
149                    return getValue(heightTextField);
150            }
151    
152            public int getType()
153            {
154                    if (types == null)
155                    {
156                            return type;
157                    }
158                    else
159                    {
160                            return types.getSelectedIndex();
161                    }
162            }
163    
164            private int getValue(TextComponent textField)
165            {
166                    try
167                    {
168                            return Integer.parseInt(textField.getText());
169                    }
170                    catch (NumberFormatException nfe)
171                    {
172                            return -1;
173                    }
174            }       
175    
176            public int getWidthValue()
177            {
178                    return getValue(widthTextField);
179            }
180                            
181            public boolean hasPressedOk()
182            {
183                    return pressedOk;
184            }
185    
186            public void handleKeys(KeyEvent e)
187            {
188                    if (e.getSource() == widthTextField)
189                    {
190                            String text = widthTextField.getText();
191                            if (maintainAspectRatio.getState() && (!text.equals(oldWidthString)))
192                            {
193                                    // compute height from current width
194                                    int w = getValue(widthTextField);
195                                    if (w > 0)
196                                    {
197                                            oldHeightString = Integer.toString((int)(w * (float)oldHeight / (float)oldWidth));
198                                            heightTextField.setText(oldHeightString);
199                                    }
200                            }
201                    }
202                    else
203                    if (e.getSource() == heightTextField)
204                    {
205                            String text = heightTextField.getText();
206                            if (maintainAspectRatio.getState() && (!text.equals(oldHeightString)))
207                            {
208                                    // compute width from current height
209                                    int h = getValue(heightTextField);
210                                    if (h > 0)
211                                    {
212                                            oldWidthString = Integer.toString((int)(h * (float)oldWidth / (float)oldHeight));
213                                            widthTextField.setText(oldWidthString);
214                                    }
215                            }
216                    }
217                    oldWidthString = widthTextField.getText();
218                    oldHeightString = heightTextField.getText();
219            }
220    
221            public void keyPressed(KeyEvent e)
222            {
223                    handleKeys(e);
224            }
225    
226            public void keyReleased(KeyEvent e)
227            {
228                    handleKeys(e);
229            }
230    
231            public void keyTyped(KeyEvent e)
232            {
233                    handleKeys(e);
234            }
235    }