KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > swing > JAffineTransformChooser


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */
package org.apache.batik.ext.swing;
18
19 import java.awt.BorderLayout JavaDoc;
20 import java.awt.Component JavaDoc;
21 import java.awt.Container JavaDoc;
22 import java.awt.FlowLayout JavaDoc;
23 import java.awt.Window JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.ComponentAdapter JavaDoc;
27 import java.awt.event.ComponentEvent JavaDoc;
28 import java.awt.event.KeyAdapter JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.WindowAdapter JavaDoc;
31 import java.awt.event.WindowEvent JavaDoc;
32 import java.awt.geom.AffineTransform JavaDoc;
33 import java.io.Serializable JavaDoc;
34
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JTextField JavaDoc;
43 import javax.swing.border.Border JavaDoc;
44 import javax.swing.text.Document JavaDoc;
45
46 /**
47  * The <tt>JAffineTransformChooser</tt> is a pane that contains
48  * controls to let a user select the various components that
49  * make up an <tt>AffineTransform</tt>
50  *
51  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
52  * @version $Id: JAffineTransformChooser.java,v 1.7 2004/08/18 07:14:18 vhardy Exp $
53  */

54 public class JAffineTransformChooser extends JGridBagPanel{
55     public static final String JavaDoc LABEL_ANGLE
56         = "JAffineTransformChooser.label.angle";
57
58     public static final String JavaDoc LABEL_DEGREE
59         = "JAffineTransformChooser.label.degree";
60
61     public static final String JavaDoc LABEL_PERCENT
62         = "JAffineTransformChooser.label.percent";
63
64     public static final String JavaDoc LABEL_ROTATE
65         = "JAffineTransformChooser.label.rotate";
66
67     public static final String JavaDoc LABEL_SCALE
68         = "JAffineTransformChooser.label.scale";
69
70     public static final String JavaDoc LABEL_RX
71         = "JAffineTransformChooser.label.rx";
72
73     public static final String JavaDoc LABEL_RY
74         = "JAffineTransformChooser.label.ry";
75
76     public static final String JavaDoc LABEL_SX
77         = "JAffineTransformChooser.label.sx";
78
79     public static final String JavaDoc LABEL_SY
80         = "JAffineTransformChooser.label.sy";
81
82     public static final String JavaDoc LABEL_TRANSLATE
83         = "JAffineTransformChooser.label.translate";
84
85     public static final String JavaDoc LABEL_TX
86         = "JAffineTransformChooser.label.tx";
87
88     public static final String JavaDoc LABEL_TY
89         = "JAffineTransformChooser.label.ty";
90     
91     public static final String JavaDoc CONFIG_TEXT_FIELD_WIDTH
92         = "JAffineTransformChooser.config.text.field.width";
93
94     public static final String JavaDoc CONFIG_TOP_PAD
95         = "JAffineTransformChooser.config.top.pad";
96
97     public static final String JavaDoc CONFIG_LEFT_PAD
98         = "JAffineTransformChooser.config.left.pad";
99
100     public static final String JavaDoc CONFIG_BOTTOM_PAD
101         = "JAffineTransformChooser.config.bottom.pad";
102
103     public static final String JavaDoc CONFIG_RIGHT_PAD
104         = "JAffineTransformChooser.config.right.pad";
105
106     /**
107      * The <tt>AffineTransform</tt> value represented by the
108      * chooser
109      */

110     protected AffineTransform JavaDoc txf;
111
112     /**
113      * The Model for the x-axis translate component
114      */

115     protected DoubleDocument txModel = new DoubleDocument();
116
117     /**
118      * The Model for the y-axis translate component
119      */

120     protected DoubleDocument tyModel = new DoubleDocument();
121     
122     /**
123      * The Model for the x-axis scale component
124      */

125     protected DoubleDocument sxModel = new DoubleDocument();
126
127     /**
128      * The Model for the y-axis scale component
129      */

130     protected DoubleDocument syModel = new DoubleDocument();
131
132     /**
133      * The Model for the x-axis rotation center
134      */

135     protected DoubleDocument rxModel = new DoubleDocument();
136
137     /**
138      * The Model for the y-axis rotation center
139      */

140     protected DoubleDocument ryModel = new DoubleDocument();
141
142     /**
143      * The Model for the rotation
144      */

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     /**
151      * Default constructor
152      */

153     public JAffineTransformChooser(){
154         build();
155         setAffineTransform(new AffineTransform JavaDoc());
156     }
157       
158     /**
159      * Adds the control components into this panel.
160      */

161     protected void build(){
162         Component JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc buildRotatePanel(){
188         JGridBagPanel panel = new JGridBagPanel();
189
190         Component JavaDoc 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 JavaDoc 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 JavaDoc buildPanel(String JavaDoc panelName,
217                                    String JavaDoc tfALabel,
218                                    Document JavaDoc tfAModel,
219                                    String JavaDoc tfBLabel,
220                                    Document JavaDoc tfBModel,
221                                    String JavaDoc tfASuffix,
222                                    String JavaDoc 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         // Create a border
232
if(setBorder){
233             setPanelBorder(panel, panelName);
234         }
235
236         return panel;
237
238     }
239
240     public void setPanelBorder(JComponent JavaDoc panel, String JavaDoc panelName){
241         Border JavaDoc 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 JavaDoc label,
260                                    Document JavaDoc model,
261                                    String JavaDoc suffix,
262                                    JGridBagPanel p,
263                                    int row){
264         JTextField JavaDoc tf = new JTextField JavaDoc(Resources.getInteger(CONFIG_TEXT_FIELD_WIDTH));
265         tf.setDocument(model);
266         p.add(new JLabel JavaDoc(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 JavaDoc(suffix), 2, row, 1, 1, WEST, HORIZONTAL, 0, 0);
269     }
270
271     public AffineTransform JavaDoc 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 JavaDoc(m);
290
291         return txf;
292     }
293
294     public void setAffineTransform(AffineTransform JavaDoc txf){
295         if(txf == null){
296             txf = new AffineTransform JavaDoc();
297         }
298
299         this.txf = txf;
300         
301         /**
302          * Now, update model
303          */

304         double[] m = new double[6];
305         txf.getMatrix(m);
306         
307         // Translation
308
txModel.setValue(m[4]);
309         tyModel.setValue(m[5]);
310         
311         // Scale, in percentages
312
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         // Rotation
318
double theta = 0;
319         if(m[0] > 0){
320             theta = Math.atan2(m[1], m[0]);
321         }
322         
323         // Rotate
324
rotateModel.setValue(RAD_TO_DEG*theta);
325         rxModel.setValue(0);
326         ryModel.setValue(0);
327     }
328
329     /**
330      * Displays the panel in a modal dialog box.
331      * @param cmp the dialog's parent component
332      * @param title the dialog's title
333      *
334      * @return null if the dialog was cancelled. Otherwise, the value entered
335      * by the user.
336      */

337     public static AffineTransform JavaDoc showDialog(Component JavaDoc cmp,
338                                              String JavaDoc title){
339         final JAffineTransformChooser pane
340             = new JAffineTransformChooser();
341
342         AffineTransformTracker tracker = new AffineTransformTracker(pane);
343         JDialog JavaDoc dialog = new Dialog JavaDoc(cmp, title, true, pane, tracker, null);
344         dialog.addWindowListener(new Closer());
345         dialog.addComponentListener(new DisposeOnClose());
346
347         dialog.show(); // blocks until user brings dialog down...
348

349         return tracker.getAffineTransform();
350     }
351     
352     /**
353      * Creates a new modal dialog box that can be used to
354      * configure an <tt>AffineTransform</tt>
355      *
356      * @param cmp the dialog's parent component
357      * @param title the dialog's title
358      *
359      */

360     public static Dialog JavaDoc createDialog(Component JavaDoc cmp,
361                                       String JavaDoc title){
362         final JAffineTransformChooser pane
363             = new JAffineTransformChooser();
364         
365         AffineTransformTracker tracker = new AffineTransformTracker(pane);
366         Dialog JavaDoc dialog = new Dialog JavaDoc(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 JavaDoc args[]){
375         AffineTransform JavaDoc t = showDialog(null, "Hello");
376         // AffineTransform.getScaleInstance(.25, .25));
377
// new AffineTransform());
378
// AffineTransform.getShearInstance(1, 1));
379
if(t == null){
380             System.out.println("Cancelled");
381         }
382         else{
383             System.out.println("t = " + t);
384         }
385     }
386
387     /*
388      * Class which builds a AffineTransform chooser dialog consisting of
389      * a JAffineTransformChooser with "Ok", "Cancel", and "Reset" buttons.
390      *
391      * Note: This needs to be fixed to deal with localization!
392      */

393     public static class Dialog extends JDialog JavaDoc {
394         private JAffineTransformChooser chooserPane;
395         private AffineTransformTracker tracker;
396
397         public static final String JavaDoc LABEL_OK
398             = "JAffineTransformChooser.label.ok";
399
400         public static final String JavaDoc LABEL_CANCEL
401             = "JAffineTransformChooser.label.cancel";
402
403         public static final String JavaDoc LABEL_RESET
404             = "JAffineTransformChooser.label.reset";
405
406         public static final String JavaDoc ACTION_COMMAND_OK
407             = "OK";
408
409         public static final String JavaDoc ACTION_COMMAND_CANCEL
410             = "cancel";
411
412
413         public Dialog(Component JavaDoc c, String JavaDoc title, boolean modal,
414                       JAffineTransformChooser chooserPane,
415                       AffineTransformTracker okListener, ActionListener JavaDoc cancelListener) {
416             super(JOptionPane.getFrameForComponent(c), title, modal);
417         
418             this.chooserPane = chooserPane;
419             this.tracker = okListener;
420
421             String JavaDoc okString = Resources.getString(LABEL_OK);
422             String JavaDoc cancelString = Resources.getString(LABEL_CANCEL);
423             String JavaDoc resetString = Resources.getString(LABEL_RESET);
424
425             Container JavaDoc contentPane = getContentPane();
426             contentPane.setLayout(new BorderLayout JavaDoc());
427             contentPane.add(chooserPane, BorderLayout.CENTER);
428
429             /*
430              * Create Lower button panel
431              */

432             JPanel JavaDoc buttonPane = new JPanel JavaDoc();
433             buttonPane.setLayout(new FlowLayout JavaDoc(FlowLayout.CENTER));
434             JButton JavaDoc okButton = new JButton JavaDoc(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 JavaDoc() {
441                     public void actionPerformed(ActionEvent JavaDoc e) {
442                         hide();
443                     }
444                 });
445             buttonPane.add(okButton);
446
447             JButton JavaDoc cancelButton = new JButton JavaDoc(cancelString);
448
449             addKeyListener(new KeyAdapter JavaDoc(){
450                     public void keyPressed(KeyEvent JavaDoc evt){
451                         if(evt.getKeyCode() == KeyEvent.VK_ESCAPE){
452                             hide();
453                         }
454                     }
455                 });
456             
457             cancelButton.addActionListener(new ActionListener JavaDoc() {
458                     public void actionPerformed(ActionEvent JavaDoc e) {
459                         hide();
460                     }
461                 });
462             
463             buttonPane.add(cancelButton);
464
465             JButton JavaDoc resetButton = new JButton JavaDoc(resetString);
466             resetButton.addActionListener(new ActionListener JavaDoc() {
467                     public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc showDialog(){
484             this.show();
485             return tracker.getAffineTransform();
486         }
487
488         public void reset() {
489             chooserPane.setAffineTransform(new AffineTransform JavaDoc());
490         }
491
492         public void setTransform(AffineTransform JavaDoc at){
493             if(at == null){
494                 at = new AffineTransform JavaDoc();
495             }
496
497             chooserPane.setAffineTransform(at);
498         }
499
500
501
502     }
503
504     static class Closer extends WindowAdapter JavaDoc implements Serializable JavaDoc{
505         public void windowClosing(WindowEvent JavaDoc e) {
506             Window JavaDoc w = e.getWindow();
507             w.hide();
508         }
509     }
510     
511     static class DisposeOnClose extends ComponentAdapter JavaDoc implements Serializable JavaDoc{
512         public void componentHidden(ComponentEvent JavaDoc e) {
513             Window JavaDoc w = (Window JavaDoc)e.getComponent();
514             w.dispose();
515         }
516     }
517     
518 }
519
520
521 class AffineTransformTracker implements ActionListener JavaDoc, Serializable JavaDoc {
522     JAffineTransformChooser chooser;
523     AffineTransform JavaDoc txf;
524
525     public AffineTransformTracker(JAffineTransformChooser c) {
526         chooser = c;
527     }
528
529     public void actionPerformed(ActionEvent JavaDoc e) {
530         txf = chooser.getAffineTransform();
531     }
532
533     public AffineTransform JavaDoc getAffineTransform() {
534         return txf;
535     }
536
537     public void reset(){
538         txf = null;
539     }
540 }
541
542
Popular Tags