File: ShearDialog.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,304 kB
  • ctags: 3,737
  • sloc: java: 31,190; sh: 238; xml: 30; makefile: 19
file content (165 lines) | stat: -rw-r--r-- 3,632 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * ShearDialog
 * 
 * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.gui.awt.dialogs;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import net.sourceforge.jiu.apps.Strings;
import net.sourceforge.jiu.geometry.Shear;

/**
 * An AWT dialog to enter the angle for a shearing operation.
 * @author Marco Schmidt
 */
public class ShearDialog extends Dialog implements ActionListener, KeyListener
{
	private Button ok;
	private Button cancel;
	private TextField angleTextField;
	private boolean pressedOk;
	private Double result;
	private Label newWidthLabel;
	private int imageWidth;
	private int imageHeight;

	/**
	 * Creates a ShearDialog.
	 * @param owner the Frame this dialog will belong to
	 */
	public ShearDialog(Frame owner, Strings strings, double initialValue, int imageWidth, int imageHeight)
	{
		super(owner, strings.get(Strings.SHEAR_IMAGE) + " (" + imageWidth + " x " + imageHeight + ")", true);
		this.imageWidth = imageWidth;
		this.imageHeight = imageHeight;

		Panel panel = new Panel(new GridLayout(0, 2));

		panel.add(new Label(strings.get(Strings.SHEAR_ENTER_ANGLE)));
		angleTextField = new TextField(Double.toString(initialValue));
		angleTextField.addKeyListener(this);
		panel.add(angleTextField);

		panel.add(new Label(strings.get(Strings.NEW_WIDTH)));
		newWidthLabel = new Label("");
		panel.add(newWidthLabel);

		add(panel, BorderLayout.CENTER);

		panel = new Panel();
		ok = new Button(strings.get(Strings.OK));
		ok.addActionListener(this);
		cancel = new Button(strings.get(Strings.CANCEL));
		cancel.addActionListener(this);
		panel.add(ok);
		panel.add(cancel);
		add(panel, BorderLayout.SOUTH);

		handleKeys(null);
		pack();
		Dialogs.center(this);
	}

	/**
	 * Hides (closes) this dialog if the OK button was source of the action event
	 * (e.g. if the button was pressed).
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == ok)
		{
			pressedOk = true;
			result = getValue(angleTextField);
			setVisible(false);
		}
		else
		if (e.getSource() == cancel)
		{
			setVisible(false);
		}
	}

	private Double getValue(TextField tf)
	{
		if (tf == null)
		{
			return null;
		}
		double d;
		try
		{
			d = (Double.valueOf(tf.getText())).doubleValue();
		}
		catch(NumberFormatException nfe)
		{
			return null;
		}
		if (d <= -90.0 || d >= 90.0)
		{
			return null;
		}
		return new Double(d);
	}

	public Double getValue()
	{
		return result;
	}

	public boolean hasPressedOk()
	{
		return pressedOk;
	}

	public void handleKeys(KeyEvent e)
	{
		Double d = getValue(angleTextField);
		double angle = -90.0;
		if (d != null)
		{
			angle = d.doubleValue();
		}
		String labelText;
		if (angle > -90.0 && angle < 90.0)
		{
			ok.setEnabled(true);
			int newWidth = Shear.computeNewImageWidth(imageWidth, imageHeight, angle);
			labelText = Integer.toString(newWidth);
		}
		else
		{
			ok.setEnabled(false);
			labelText = "-";
		}
		newWidthLabel.setText(labelText);
	}

	public void keyPressed(KeyEvent e)
	{
		handleKeys(e);
	}

	public void keyReleased(KeyEvent e)
	{
		handleKeys(e);
	}

	public void keyTyped(KeyEvent e)
	{
		handleKeys(e);
	}
}