001 /* 002 * ShearDialog 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.TextField; 018 import java.awt.event.ActionEvent; 019 import java.awt.event.ActionListener; 020 import java.awt.event.KeyEvent; 021 import java.awt.event.KeyListener; 022 import net.sourceforge.jiu.apps.Strings; 023 import net.sourceforge.jiu.geometry.Shear; 024 025 /** 026 * An AWT dialog to enter the angle for a shearing operation. 027 * @author Marco Schmidt 028 */ 029 public class ShearDialog extends Dialog implements ActionListener, KeyListener 030 { 031 private Button ok; 032 private Button cancel; 033 private TextField angleTextField; 034 private boolean pressedOk; 035 private Double result; 036 private Label newWidthLabel; 037 private int imageWidth; 038 private int imageHeight; 039 040 /** 041 * Creates a ShearDialog. 042 * @param owner the Frame this dialog will belong to 043 */ 044 public ShearDialog(Frame owner, Strings strings, double initialValue, int imageWidth, int imageHeight) 045 { 046 super(owner, strings.get(Strings.SHEAR_IMAGE) + " (" + imageWidth + " x " + imageHeight + ")", true); 047 this.imageWidth = imageWidth; 048 this.imageHeight = imageHeight; 049 050 Panel panel = new Panel(new GridLayout(0, 2)); 051 052 panel.add(new Label(strings.get(Strings.SHEAR_ENTER_ANGLE))); 053 angleTextField = new TextField(Double.toString(initialValue)); 054 angleTextField.addKeyListener(this); 055 panel.add(angleTextField); 056 057 panel.add(new Label(strings.get(Strings.NEW_WIDTH))); 058 newWidthLabel = new Label(""); 059 panel.add(newWidthLabel); 060 061 add(panel, BorderLayout.CENTER); 062 063 panel = new Panel(); 064 ok = new Button(strings.get(Strings.OK)); 065 ok.addActionListener(this); 066 cancel = new Button(strings.get(Strings.CANCEL)); 067 cancel.addActionListener(this); 068 panel.add(ok); 069 panel.add(cancel); 070 add(panel, BorderLayout.SOUTH); 071 072 handleKeys(null); 073 pack(); 074 Dialogs.center(this); 075 } 076 077 /** 078 * Hides (closes) this dialog if the OK button was source of the action event 079 * (e.g. if the button was pressed). 080 */ 081 public void actionPerformed(ActionEvent e) 082 { 083 if (e.getSource() == ok) 084 { 085 pressedOk = true; 086 result = getValue(angleTextField); 087 setVisible(false); 088 } 089 else 090 if (e.getSource() == cancel) 091 { 092 setVisible(false); 093 } 094 } 095 096 private Double getValue(TextField tf) 097 { 098 if (tf == null) 099 { 100 return null; 101 } 102 double d; 103 try 104 { 105 d = (Double.valueOf(tf.getText())).doubleValue(); 106 } 107 catch(NumberFormatException nfe) 108 { 109 return null; 110 } 111 if (d <= -90.0 || d >= 90.0) 112 { 113 return null; 114 } 115 return new Double(d); 116 } 117 118 public Double getValue() 119 { 120 return result; 121 } 122 123 public boolean hasPressedOk() 124 { 125 return pressedOk; 126 } 127 128 public void handleKeys(KeyEvent e) 129 { 130 Double d = getValue(angleTextField); 131 double angle = -90.0; 132 if (d != null) 133 { 134 angle = d.doubleValue(); 135 } 136 String labelText; 137 if (angle > -90.0 && angle < 90.0) 138 { 139 ok.setEnabled(true); 140 int newWidth = Shear.computeNewImageWidth(imageWidth, imageHeight, angle); 141 labelText = Integer.toString(newWidth); 142 } 143 else 144 { 145 ok.setEnabled(false); 146 labelText = "-"; 147 } 148 newWidthLabel.setText(labelText); 149 } 150 151 public void keyPressed(KeyEvent e) 152 { 153 handleKeys(e); 154 } 155 156 public void keyReleased(KeyEvent e) 157 { 158 handleKeys(e); 159 } 160 161 public void keyTyped(KeyEvent e) 162 { 163 handleKeys(e); 164 } 165 }