1 package devisor2.grid.GUI.dialogs;
2 
3 import java.util.*;
4 
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.awt.geom.*;
8 import javax.swing.*;
9 import javax.swing.border.*;
10 
11 import devisor2.grid.options.*;
12 import devisor2.grid.GUI.framework.*;
13 import devisor2.grid.GUI.event.*;
14 import devisor2.foundation.*;
15 import devisor2.foundation.elements.*;
16 import devisor2.grid.backend.*;
17 
18 
19 /**
20  *  This class is the DockableDialog instance to get the rotate angle. <br>
21  *  See the GridListener and MainActionListener for a detailed description
22  *  of how the dialog is connected to the application.
23  *
24  *  @author Dominik Goeddeke
25  *  @version done
26  *
27  */
28 
29 public class RotateDialog extends DockableDialog implements ActionListener
30 {
31 
32     // the angle through which to rotate
33     private double angle;
34 
35     /**
36      *  this method can be used to update the GUI of the dialog dynamically.
37      *  it is called by the DialogManager e.g. when some comboboxes containing
38      *  boundary information need to be refreshed
39      */
refreshGUI()40     public void refreshGUI ()
41     {
42 	// no need fort this in this class :-)
43 	return;
44     }
45 
46 
47 
48     /**
49      *  returns the initial Position of the dialog
50      */
getInitialPosition()51     public Point getInitialPosition ()
52     {
53 	return new Point (parent.getLocationOnScreen().x - 50, parent.getLocationOnScreen().y);
54     }
55 
56     /**
57      *  the constructor just sets references correctly and creates the GUI
58      */
RotateDialog(MainFrame parent)59     public RotateDialog (MainFrame parent)
60     {
61 	super (parent);
62 	setTitle ((String)cc.rb.getObject("dialogs_rotate_caption"));
63 	JPanel content = new JPanel ();
64 	content.setBorder (new EmptyBorder (10,10,10,10));
65 	// just ordinary GUI layouting
66 	GridBagLayout layout = new GridBagLayout ();
67 	GridBagConstraints constraints = new GridBagConstraints ();
68 	content.setLayout (layout);
69 	constraints.fill = GridBagConstraints.BOTH;
70 	constraints.weightx = constraints.weighty = 1.0;
71 	// edit field
72 	JLabel label = new JLabel ((String)cc.rb.getObject ("dialogs_rotate_label"));
73 	constraints.gridy = 0;
74 	constraints.gridx = 0;
75 	layout.setConstraints (label, constraints);
76 	content.add (label);
77 	angleTF = new JTextField (6);
78 	constraints.gridx = 1;
79 	layout.setConstraints (angleTF, constraints);
80 	content.add (angleTF);
81 	// buttons
82 	JPanel buttons = new JPanel ();
83 	buttons.setLayout (new FlowLayout ());
84 	okButton = new JButton ((String)cc.rb.getObject ("general_Ok"));
85 	okButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Ok_mnemonic")).intValue());
86 	okButton.setActionCommand (OK);
87 	okButton.addActionListener (this);
88 	buttons.add (okButton);
89 	applyButton = new JButton ((String)cc.rb.getObject ("general_Apply"));
90 	applyButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Apply_mnemonic")).intValue());
91 	applyButton.setActionCommand (APPLY);
92 	applyButton.addActionListener (this);
93 	buttons.add (applyButton);
94 	cancelButton = new JButton ((String)cc.rb.getObject ("general_Cancel"));
95 	cancelButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Cancel_mnemonic")).intValue());
96 	cancelButton.setActionCommand (CANCEL);
97 	cancelButton.addActionListener (this);
98 	buttons.add (cancelButton);
99 	// add them all to the dialog
100 	getRootPane().setDefaultButton (applyButton);
101 	constraints.gridy = 3;
102 	constraints.gridx = 0;
103 	constraints.gridwidth = 2;
104 	layout.setConstraints(buttons, constraints);
105 	content.add (buttons);
106 	// finish the GUI
107 	getContentPane ().add (content);
108 	addComponentListener (listener);
109 	pack ();
110     }
111 
112 
113     /**
114      *  This class handles its own action event.
115      */
actionPerformed(ActionEvent e)116     public void actionPerformed (ActionEvent e)
117     {
118 	if (e.getActionCommand().equals (OK))
119 	{
120 	    if (verify ())
121 	    {
122 		rotate ();
123 		cc.dm.hide (cc.dm.ROTATEDIALOG);
124 	    }
125 	    return;
126 	}
127 	if (e.getActionCommand().equals (APPLY))
128 	{
129 	    if (verify ())
130 	    {
131 		rotate ();
132 	    }
133 	    return;
134 	}
135 	if (e.getActionCommand().equals (CANCEL))
136 	{
137 	    cc.dm.hide (cc.dm.ROTATEDIALOG);
138 	    return;
139 	}
140     }
141 
rotate()142     private void rotate ()
143     {
144 	GridListener listener = parent.getDrawingArea().getListener();
145   	GridSelector selector = listener.getSelector ();
146   	Point anchor = selector.getCenterHotspot ();
147   	int[] world = cc.gc.fromScreenToWorld(anchor.x, anchor.y);
148   	AffineTransform newtrafo = AffineTransform.getRotateInstance (Math.toRadians(-angle), world[0], world[1]);
149 	GridTransformation trafo = new GridTransformation (newtrafo, -angle, 1, 1, 0, 0, world[0], world[1]);
150 	// prepare undo
151 	Vector infos = new Vector ();
152 	infos.add (listener.getSelector().getHotspotMask ());
153 	Enumeration enum = listener.getSelectedItems().elements ();
154 	while (enum.hasMoreElements ())
155 	{
156 	    GridItem item = (GridItem)enum.nextElement ();
157 	    Object[] info = {item, item.getParameters()};
158 	    infos.add (info);
159 	}
160 	cc.um.notifyUndo (cc.um.UNDO_ROTATE, listener.getSelectedItems(), infos);
161 	// perform trafo
162   	listener.applyTransformation (trafo, true);
163     }
164 
165 
166 
167     /**
168      *  the verifivation code is all put into this method
169      */
verify()170     private boolean verify ()
171     {
172 	boolean allOK = true;
173 	try
174 	{
175 	    angle = Double.parseDouble (angleTF.getText());
176 	}
177 	catch (Exception ex)
178 	{
179 	    JOptionPane.showMessageDialog (parent, (String)cc.rb.getObject("dialogs_rotate_error"), (String)cc.rb.getObject ("errors_parseerror"), JOptionPane.ERROR_MESSAGE);
180 	    allOK = false;
181 	}
182 	return allOK;
183     }
184 
185 
186     // keys for the action commands
187 
188     private static final String OK = "OK";
189     private static final String CANCEL = "CANCEL";
190     private static final String APPLY = "APPLY";
191 
192     // buttons
193 
194     private JButton okButton;
195     private JButton cancelButton;
196     private JButton applyButton;
197     private JTextField angleTF;
198 
199 
200 }
201