1 package org.apache.batik.ext.swing; 18 19 import java.awt.BorderLayout ; 20 import java.awt.Component ; 21 import java.awt.Container ; 22 import java.awt.FlowLayout ; 23 import java.awt.Window ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.ComponentAdapter ; 27 import java.awt.event.ComponentEvent ; 28 import java.awt.event.KeyAdapter ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.event.WindowAdapter ; 31 import java.awt.event.WindowEvent ; 32 import java.awt.geom.AffineTransform ; 33 import java.io.Serializable ; 34 35 import javax.swing.BorderFactory ; 36 import javax.swing.JButton ; 37 import javax.swing.JComponent ; 38 import javax.swing.JDialog ; 39 import javax.swing.JLabel ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 import javax.swing.JTextField ; 43 import javax.swing.border.Border ; 44 import javax.swing.text.Document ; 45 46 54 public class JAffineTransformChooser extends JGridBagPanel{ 55 public static final String LABEL_ANGLE 56 = "JAffineTransformChooser.label.angle"; 57 58 public static final String LABEL_DEGREE 59 = "JAffineTransformChooser.label.degree"; 60 61 public static final String LABEL_PERCENT 62 = "JAffineTransformChooser.label.percent"; 63 64 public static final String LABEL_ROTATE 65 = "JAffineTransformChooser.label.rotate"; 66 67 public static final String LABEL_SCALE 68 = "JAffineTransformChooser.label.scale"; 69 70 public static final String LABEL_RX 71 = "JAffineTransformChooser.label.rx"; 72 73 public static final String LABEL_RY 74 = "JAffineTransformChooser.label.ry"; 75 76 public static final String LABEL_SX 77 = "JAffineTransformChooser.label.sx"; 78 79 public static final String LABEL_SY 80 = "JAffineTransformChooser.label.sy"; 81 82 public static final String LABEL_TRANSLATE 83 = "JAffineTransformChooser.label.translate"; 84 85 public static final String LABEL_TX 86 = "JAffineTransformChooser.label.tx"; 87 88 public static final String LABEL_TY 89 = "JAffineTransformChooser.label.ty"; 90 91 public static final String CONFIG_TEXT_FIELD_WIDTH 92 = "JAffineTransformChooser.config.text.field.width"; 93 94 public static final String CONFIG_TOP_PAD 95 = "JAffineTransformChooser.config.top.pad"; 96 97 public static final String CONFIG_LEFT_PAD 98 = "JAffineTransformChooser.config.left.pad"; 99 100 public static final String CONFIG_BOTTOM_PAD 101 = "JAffineTransformChooser.config.bottom.pad"; 102 103 public static final String CONFIG_RIGHT_PAD 104 = "JAffineTransformChooser.config.right.pad"; 105 106 110 protected AffineTransform txf; 111 112 115 protected DoubleDocument txModel = new DoubleDocument(); 116 117 120 protected DoubleDocument tyModel = new DoubleDocument(); 121 122 125 protected DoubleDocument sxModel = new DoubleDocument(); 126 127 130 protected DoubleDocument syModel = new DoubleDocument(); 131 132 135 protected DoubleDocument rxModel = new DoubleDocument(); 136 137 140 protected DoubleDocument ryModel = new DoubleDocument(); 141 142 145 protected DoubleDocument rotateModel = new DoubleDocument(); 146 147 protected static final double RAD_TO_DEG = 180./Math.PI; 148 protected static final double DEG_TO_RAD = Math.PI/180.; 149 150 153 public JAffineTransformChooser(){ 154 build(); 155 setAffineTransform(new AffineTransform ()); 156 } 157 158 161 protected void build(){ 162 Component txyCmp = buildPanel(Resources.getString(LABEL_TRANSLATE), 163 Resources.getString(LABEL_TX), 164 txModel, 165 Resources.getString(LABEL_TY), 166 tyModel, 167 "", 168 "", 169 true); 170 171 Component sxyCmp = buildPanel(Resources.getString(LABEL_SCALE), 172 Resources.getString(LABEL_SX), 173 sxModel, 174 Resources.getString(LABEL_SY), 175 syModel, 176 Resources.getString(LABEL_PERCENT), 177 Resources.getString(LABEL_PERCENT), 178 true); 179 180 Component rCmp = buildRotatePanel(); 181 182 add(txyCmp, 0, 0, 1, 1, CENTER, BOTH, 1, 1); 183 add(sxyCmp, 1, 0, 1, 1, CENTER, BOTH, 1, 1); 184 add(rCmp, 0, 1, 2, 1, CENTER, BOTH, 1, 1); 185 } 186 187 protected Component buildRotatePanel(){ 188 JGridBagPanel panel = new JGridBagPanel(); 189 190 Component anglePanel = buildPanel(Resources.getString(LABEL_ROTATE), 191 Resources.getString(LABEL_ANGLE), 192 rotateModel, 193 null, 194 null, 195 Resources.getString(LABEL_DEGREE), 196 null, 197 false); 198 199 Component centerPanel = buildPanel("", 200 Resources.getString(LABEL_RX), 201 rxModel, 202 Resources.getString(LABEL_RY), 203 ryModel, 204 null, 205 null, 206 false); 207 208 panel.add(anglePanel, 0, 0, 1, 1, CENTER, BOTH, 1, 1); 209 panel.add(centerPanel, 1, 0, 1, 1, CENTER, BOTH, 1, 1); 210 211 setPanelBorder(panel, Resources.getString(LABEL_ROTATE)); 212 213 return panel; 214 } 215 216 protected Component buildPanel(String panelName, 217 String tfALabel, 218 Document tfAModel, 219 String tfBLabel, 220 Document tfBModel, 221 String tfASuffix, 222 String tfBSuffix, 223 boolean setBorder){ 224 JGridBagPanel panel = new JGridBagPanel(); 225 226 addToPanelAtRow(tfALabel, tfAModel, tfASuffix, panel, 0); 227 if(tfBLabel != null){ 228 addToPanelAtRow(tfBLabel, tfBModel, tfBSuffix, panel, 1); 229 } 230 231 if(setBorder){ 233 setPanelBorder(panel, panelName); 234 } 235 236 return panel; 237 238 } 239 240 public void setPanelBorder(JComponent panel, String panelName){ 241 Border border 242 = BorderFactory.createTitledBorder 243 (BorderFactory.createEtchedBorder(), panelName); 244 245 int topPad = Resources.getInteger(CONFIG_TOP_PAD); 246 int leftPad = Resources.getInteger(CONFIG_LEFT_PAD); 247 int bottomPad = Resources.getInteger(CONFIG_BOTTOM_PAD); 248 int rightPad = Resources.getInteger(CONFIG_RIGHT_PAD); 249 250 border 251 = BorderFactory.createCompoundBorder 252 (border, 253 BorderFactory.createEmptyBorder(topPad, leftPad, 254 bottomPad, rightPad)); 255 256 panel.setBorder(border); 257 } 258 259 protected void addToPanelAtRow(String label, 260 Document model, 261 String suffix, 262 JGridBagPanel p, 263 int row){ 264 JTextField tf = new JTextField (Resources.getInteger(CONFIG_TEXT_FIELD_WIDTH)); 265 tf.setDocument(model); 266 p.add(new JLabel (label), 0, row, 1, 1, WEST, HORIZONTAL, 0, 0); 267 p.add(tf, 1, row, 1, 1, CENTER, HORIZONTAL, 1, 0); 268 p.add(new JLabel (suffix), 2, row, 1, 1, WEST, HORIZONTAL, 0, 0); 269 } 270 271 public AffineTransform getAffineTransform(){ 272 double sx = sxModel.getValue()/100.; 273 double sy = syModel.getValue()/100.; 274 double theta = rotateModel.getValue()*DEG_TO_RAD; 275 double rx = rxModel.getValue(); 276 double ry = ryModel.getValue(); 277 double tx = txModel.getValue(); 278 double ty = tyModel.getValue(); 279 280 double[] m = new double[6]; 281 282 m[0] = sx*Math.cos(theta); 283 m[1] = sx*Math.sin(theta); 284 m[2] = -sy*Math.sin(theta); 285 m[3] = sy*Math.cos(theta); 286 m[4] = tx + rx - rx*Math.cos(theta) + ry*Math.sin(theta); 287 m[5] = ty + ry - rx*Math.sin(theta) - ry*Math.cos(theta); 288 289 txf = new AffineTransform (m); 290 291 return txf; 292 } 293 294 public void setAffineTransform(AffineTransform txf){ 295 if(txf == null){ 296 txf = new AffineTransform (); 297 } 298 299 this.txf = txf; 300 301 304 double[] m = new double[6]; 305 txf.getMatrix(m); 306 307 txModel.setValue(m[4]); 309 tyModel.setValue(m[5]); 310 311 double sx = Math.sqrt(m[0]*m[0] + m[1]*m[1]); 313 double sy = Math.sqrt(m[2]*m[2] + m[3]*m[3]); 314 sxModel.setValue(100*sx); 315 syModel.setValue(100*sy); 316 317 double theta = 0; 319 if(m[0] > 0){ 320 theta = Math.atan2(m[1], m[0]); 321 } 322 323 rotateModel.setValue(RAD_TO_DEG*theta); 325 rxModel.setValue(0); 326 ryModel.setValue(0); 327 } 328 329 337 public static AffineTransform showDialog(Component cmp, 338 String title){ 339 final JAffineTransformChooser pane 340 = new JAffineTransformChooser(); 341 342 AffineTransformTracker tracker = new AffineTransformTracker(pane); 343 JDialog dialog = new Dialog (cmp, title, true, pane, tracker, null); 344 dialog.addWindowListener(new Closer()); 345 dialog.addComponentListener(new DisposeOnClose()); 346 347 dialog.show(); 349 return tracker.getAffineTransform(); 350 } 351 352 360 public static Dialog createDialog(Component cmp, 361 String title){ 362 final JAffineTransformChooser pane 363 = new JAffineTransformChooser(); 364 365 AffineTransformTracker tracker = new AffineTransformTracker(pane); 366 Dialog dialog = new Dialog (cmp, title, true, pane, tracker, null); 367 dialog.addWindowListener(new Closer()); 368 dialog.addComponentListener(new DisposeOnClose()); 369 370 return dialog; 371 } 372 373 374 public static void main(String args[]){ 375 AffineTransform t = showDialog(null, "Hello"); 376 if(t == null){ 380 System.out.println("Cancelled"); 381 } 382 else{ 383 System.out.println("t = " + t); 384 } 385 } 386 387 393 public static class Dialog extends JDialog { 394 private JAffineTransformChooser chooserPane; 395 private AffineTransformTracker tracker; 396 397 public static final String LABEL_OK 398 = "JAffineTransformChooser.label.ok"; 399 400 public static final String LABEL_CANCEL 401 = "JAffineTransformChooser.label.cancel"; 402 403 public static final String LABEL_RESET 404 = "JAffineTransformChooser.label.reset"; 405 406 public static final String ACTION_COMMAND_OK 407 = "OK"; 408 409 public static final String ACTION_COMMAND_CANCEL 410 = "cancel"; 411 412 413 public Dialog(Component c, String title, boolean modal, 414 JAffineTransformChooser chooserPane, 415 AffineTransformTracker okListener, ActionListener cancelListener) { 416 super(JOptionPane.getFrameForComponent(c), title, modal); 417 418 this.chooserPane = chooserPane; 419 this.tracker = okListener; 420 421 String okString = Resources.getString(LABEL_OK); 422 String cancelString = Resources.getString(LABEL_CANCEL); 423 String resetString = Resources.getString(LABEL_RESET); 424 425 Container contentPane = getContentPane(); 426 contentPane.setLayout(new BorderLayout ()); 427 contentPane.add(chooserPane, BorderLayout.CENTER); 428 429 432 JPanel buttonPane = new JPanel (); 433 buttonPane.setLayout(new FlowLayout (FlowLayout.CENTER)); 434 JButton okButton = new JButton (okString); 435 getRootPane().setDefaultButton(okButton); 436 okButton.setActionCommand(ACTION_COMMAND_OK); 437 if (okListener != null) { 438 okButton.addActionListener(okListener); 439 } 440 okButton.addActionListener(new ActionListener () { 441 public void actionPerformed(ActionEvent e) { 442 hide(); 443 } 444 }); 445 buttonPane.add(okButton); 446 447 JButton cancelButton = new JButton (cancelString); 448 449 addKeyListener(new KeyAdapter (){ 450 public void keyPressed(KeyEvent evt){ 451 if(evt.getKeyCode() == KeyEvent.VK_ESCAPE){ 452 hide(); 453 } 454 } 455 }); 456 457 cancelButton.addActionListener(new ActionListener () { 458 public void actionPerformed(ActionEvent e) { 459 hide(); 460 } 461 }); 462 463 buttonPane.add(cancelButton); 464 465 JButton resetButton = new JButton (resetString); 466 resetButton.addActionListener(new ActionListener () { 467 public void actionPerformed(ActionEvent e) { 468 reset(); 469 } 470 }); 471 buttonPane.add(resetButton); 472 contentPane.add(buttonPane, BorderLayout.SOUTH); 473 474 pack(); 475 setLocationRelativeTo(c); 476 } 477 478 public void show() { 479 tracker.reset(); 480 super.show(); 481 } 482 483 public AffineTransform showDialog(){ 484 this.show(); 485 return tracker.getAffineTransform(); 486 } 487 488 public void reset() { 489 chooserPane.setAffineTransform(new AffineTransform ()); 490 } 491 492 public void setTransform(AffineTransform at){ 493 if(at == null){ 494 at = new AffineTransform (); 495 } 496 497 chooserPane.setAffineTransform(at); 498 } 499 500 501 502 } 503 504 static class Closer extends WindowAdapter implements Serializable { 505 public void windowClosing(WindowEvent e) { 506 Window w = e.getWindow(); 507 w.hide(); 508 } 509 } 510 511 static class DisposeOnClose extends ComponentAdapter implements Serializable { 512 public void componentHidden(ComponentEvent e) { 513 Window w = (Window )e.getComponent(); 514 w.dispose(); 515 } 516 } 517 518 } 519 520 521 class AffineTransformTracker implements ActionListener , Serializable { 522 JAffineTransformChooser chooser; 523 AffineTransform txf; 524 525 public AffineTransformTracker(JAffineTransformChooser c) { 526 chooser = c; 527 } 528 529 public void actionPerformed(ActionEvent e) { 530 txf = chooser.getAffineTransform(); 531 } 532 533 public AffineTransform getAffineTransform() { 534 return txf; 535 } 536 537 public void reset(){ 538 txf = null; 539 } 540 } 541 542 | Popular Tags |