package devisor2.grid.GUI.dialogs; import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.border.*; import devisor2.grid.options.*; import devisor2.grid.GUI.framework.*; import devisor2.grid.GUI.event.*; import devisor2.foundation.*; import devisor2.foundation.elements.*; import devisor2.grid.backend.*; /** * This class is the DockableDialog instance to get the rotate angle.
* See the GridListener and MainActionListener for a detailed description * of how the dialog is connected to the application. * * @author Dominik Goeddeke * @version done * */ public class RotateDialog extends DockableDialog implements ActionListener { // the angle through which to rotate private double angle; /** * this method can be used to update the GUI of the dialog dynamically. * it is called by the DialogManager e.g. when some comboboxes containing * boundary information need to be refreshed */ public void refreshGUI () { // no need fort this in this class :-) return; } /** * returns the initial Position of the dialog */ public Point getInitialPosition () { return new Point (parent.getLocationOnScreen().x - 50, parent.getLocationOnScreen().y); } /** * the constructor just sets references correctly and creates the GUI */ public RotateDialog (MainFrame parent) { super (parent); setTitle ((String)cc.rb.getObject("dialogs_rotate_caption")); JPanel content = new JPanel (); content.setBorder (new EmptyBorder (10,10,10,10)); // just ordinary GUI layouting GridBagLayout layout = new GridBagLayout (); GridBagConstraints constraints = new GridBagConstraints (); content.setLayout (layout); constraints.fill = GridBagConstraints.BOTH; constraints.weightx = constraints.weighty = 1.0; // edit field JLabel label = new JLabel ((String)cc.rb.getObject ("dialogs_rotate_label")); constraints.gridy = 0; constraints.gridx = 0; layout.setConstraints (label, constraints); content.add (label); angleTF = new JTextField (6); constraints.gridx = 1; layout.setConstraints (angleTF, constraints); content.add (angleTF); // buttons JPanel buttons = new JPanel (); buttons.setLayout (new FlowLayout ()); okButton = new JButton ((String)cc.rb.getObject ("general_Ok")); okButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Ok_mnemonic")).intValue()); okButton.setActionCommand (OK); okButton.addActionListener (this); buttons.add (okButton); applyButton = new JButton ((String)cc.rb.getObject ("general_Apply")); applyButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Apply_mnemonic")).intValue()); applyButton.setActionCommand (APPLY); applyButton.addActionListener (this); buttons.add (applyButton); cancelButton = new JButton ((String)cc.rb.getObject ("general_Cancel")); cancelButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Cancel_mnemonic")).intValue()); cancelButton.setActionCommand (CANCEL); cancelButton.addActionListener (this); buttons.add (cancelButton); // add them all to the dialog getRootPane().setDefaultButton (applyButton); constraints.gridy = 3; constraints.gridx = 0; constraints.gridwidth = 2; layout.setConstraints(buttons, constraints); content.add (buttons); // finish the GUI getContentPane ().add (content); addComponentListener (listener); pack (); } /** * This class handles its own action event. */ public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equals (OK)) { if (verify ()) { rotate (); cc.dm.hide (cc.dm.ROTATEDIALOG); } return; } if (e.getActionCommand().equals (APPLY)) { if (verify ()) { rotate (); } return; } if (e.getActionCommand().equals (CANCEL)) { cc.dm.hide (cc.dm.ROTATEDIALOG); return; } } private void rotate () { GridListener listener = parent.getDrawingArea().getListener(); GridSelector selector = listener.getSelector (); Point anchor = selector.getCenterHotspot (); int[] world = cc.gc.fromScreenToWorld(anchor.x, anchor.y); AffineTransform newtrafo = AffineTransform.getRotateInstance (Math.toRadians(-angle), world[0], world[1]); GridTransformation trafo = new GridTransformation (newtrafo, -angle, 1, 1, 0, 0, world[0], world[1]); // prepare undo Vector infos = new Vector (); infos.add (listener.getSelector().getHotspotMask ()); Enumeration enum = listener.getSelectedItems().elements (); while (enum.hasMoreElements ()) { GridItem item = (GridItem)enum.nextElement (); Object[] info = {item, item.getParameters()}; infos.add (info); } cc.um.notifyUndo (cc.um.UNDO_ROTATE, listener.getSelectedItems(), infos); // perform trafo listener.applyTransformation (trafo, true); } /** * the verifivation code is all put into this method */ private boolean verify () { boolean allOK = true; try { angle = Double.parseDouble (angleTF.getText()); } catch (Exception ex) { JOptionPane.showMessageDialog (parent, (String)cc.rb.getObject("dialogs_rotate_error"), (String)cc.rb.getObject ("errors_parseerror"), JOptionPane.ERROR_MESSAGE); allOK = false; } return allOK; } // keys for the action commands private static final String OK = "OK"; private static final String CANCEL = "CANCEL"; private static final String APPLY = "APPLY"; // buttons private JButton okButton; private JButton cancelButton; private JButton applyButton; private JTextField angleTF; }