001    /*
002     * CropDialog
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 the parameters for an image crop operation.
027     * @author Marco Schmidt
028     */
029    public class CropDialog extends Dialog implements ActionListener, KeyListener
030    {
031            private Button ok;
032            private Button cancel;
033            private TextField x1;
034            private TextField y1;
035            private TextField x2;
036            private TextField y2;
037            private int width;
038            private int height;
039            private Label newWidth;
040            private Label newHeight;
041            private boolean pressedOk;
042    
043            /**
044             * @param owner the Frame this dialog will belong to
045             * @param strings Strings resource to be used for text messages
046             * @param width width of the original image, before cropping; this is used to 
047             *  determine the valid values for left and right column, from 0 to width - 1
048             * @param height height of the original image, before cropping; this is used to 
049             *  determine the valid values for top and bottom row, from 0 to height - 1
050             */
051            public CropDialog(Frame owner, Strings strings, int width, int height)
052            {
053                    super(owner, strings.get(Strings.CROP_IMAGE) + " (" + width + " x " + height + ")", true);
054                    pressedOk = false;
055                    this.width = width;
056                    this.height = height;
057                    Panel panel = new Panel();
058                    panel.setLayout(new GridLayout(0, 6));
059    
060                    panel.add(new Label(strings.get(Strings.LEFT_COLUMN)));
061                    x1 = new TextField("0");
062                    x1.addKeyListener(this);
063                    panel.add(x1);
064    
065                    panel.add(new Label(strings.get(Strings.RIGHT_COLUMN)));
066                    x2 = new TextField(Integer.toString(width - 1));
067                    x2.addKeyListener(this);
068                    panel.add(x2);
069    
070                    panel.add(new Label(strings.get(Strings.NEW_WIDTH)));
071                    newWidth = new Label();
072                    panel.add(newWidth);
073    
074                    panel.add(new Label(strings.get(Strings.TOP_ROW)));
075                    y1 = new TextField("0");
076                    y1.addKeyListener(this);
077                    panel.add(y1);
078    
079                    panel.add(new Label(strings.get(Strings.BOTTOM_ROW)));
080                    y2 = new TextField(Integer.toString(height - 1));
081                    y2.addKeyListener(this);
082                    panel.add(y2);
083    
084                    panel.add(new Label(strings.get(Strings.NEW_HEIGHT)));
085                    newHeight = new Label();
086                    panel.add(newHeight);
087    
088                    add(panel, BorderLayout.CENTER);
089    
090                    ok = new Button(strings.get(Strings.OK));
091                    ok.addActionListener(this);
092                    cancel = new Button(strings.get(Strings.CANCEL));
093                    cancel.addActionListener(this);
094    
095                    panel = new Panel();
096                    panel.add(ok);
097                    panel.add(cancel);
098                    add(panel, BorderLayout.SOUTH);
099    
100                    updateLabels();
101                    pack();
102                    Dialogs.center(this);
103            }
104    
105            /**
106             * Hides (closes) this dialog if the OK button was source of the action event
107             * (e.g. if the button was pressed).
108             */
109            public void actionPerformed(ActionEvent e)
110            {
111                    if (e.getSource() == ok)
112                    {
113                            pressedOk = true;
114                            setVisible(false);
115                    }
116                    else
117                    if (e.getSource() == cancel)
118                    {
119                            setVisible(false);
120                    }
121            }
122    
123            public int getHeight()
124            {
125                    int y1 = getY1();
126                    int y2 = getY2();
127                    if (y1 != -1 && y2 != -1 && y1 >= 0 && y2 >= y1 && y2 < height && y1 <= y2)
128                    {
129                            return y2 - y1 + 1;
130                    }
131                    else
132                    {
133                            return -1;
134                    }
135            }
136    
137            /**
138             * Attempts to convert the content of the argument text component
139             * to an <code>int</code>; if successful, returns that int, otherwise
140             * -1 is returned.
141             * @param textField the text component that is supposed to hold an int value
142             * @return int representation of the text component's data
143             */
144            private int getValue(TextComponent textField)
145            {
146                    try
147                    {
148                            return Integer.parseInt(textField.getText());
149                    }
150                    catch (NumberFormatException nfe)
151                    {
152                            return -1;
153                    }
154            }       
155    
156            /**
157             * Returns the width of the to-be-cropped image as given by the 
158             * current values in the text fields for left column and right column.
159             * Computes the width from those values and returns it or returns -1
160             * if the data in the text fields is not valid for some reason.
161             * @return width of cropped image or -1 if information is invalid
162             */
163            public int getWidth()
164            {
165                    int x1 = getX1();
166                    int x2 = getX2();
167                    if (x1 != -1 && x2 != -1 && x1 >= 0 && x2 >= x1 && x2 < width && x1 <= x2)
168                    {
169                            return x2 - x1 + 1;
170                    }
171                    else
172                    {
173                            return -1;
174                    }
175            }
176    
177            public int getX1()
178            {
179                    return getValue(x1);
180            }
181    
182            public int getX2()
183            {
184                    return getValue(x2);
185            }
186    
187            public int getY1()
188            {
189                    return getValue(y1);
190            }
191    
192            public int getY2()
193            {
194                    return getValue(y2);
195            }
196    
197            public boolean hasPressedOk()
198            {
199                    return pressedOk;
200            }
201    
202            /**
203             * Computes width and height of new image and updates the
204             * corresponding labels.
205             * The labels will either display width and height or a single
206             * dash if the data in the text fields is invalid.
207             */
208            private void updateLabels()
209            {
210                    String text;
211                    boolean enabled = true;
212    
213                    int valueWidth = getWidth();
214                    if (valueWidth == -1)
215                    {
216                            text = "-";
217                            enabled = false;
218                    }
219                    else
220                    {
221                            text = Integer.toString(valueWidth);
222                    }
223                    newWidth.setText(text);
224    
225                    int valueHeight = getHeight();
226                    if (valueHeight == -1)
227                    {
228                            text = "-";
229                            enabled = false;
230                    }
231                    else
232                    {
233                            text = Integer.toString(valueHeight);
234                    }
235                    newHeight.setText(text);
236                    
237                    ok.setEnabled(enabled);
238            }
239    
240            public void keyPressed(KeyEvent e)
241            {
242                    updateLabels();
243            }
244    
245            public void keyReleased(KeyEvent e)
246            {
247                    updateLabels();
248            }
249    
250            public void keyTyped(KeyEvent e)
251            {
252                    updateLabels();
253            }
254    }