Debian Codegraph @

source/org/jfree/report/util/ComponentDrawable.java

raw
1 /**
2 * ========================================
3 * JFreeReport : a free Java report library
4 * ========================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * $Id: ComponentDrawable.java 10756 2009-12-02 15:58:24Z tmorgner $
27 * ------------
28 * (C) Copyright 2000-2005, by Object Refinery Limited.
29 * (C) Copyright 2005-2007, by Pentaho Corporation.
30 */
31
32 package org.jfree.report.util;
33
34 import java.awt.Component;
35 import java.awt.Dimension;
36 import java.awt.Graphics2D;
37 import java.awt.Window;
38 import java.awt.geom.Rectangle2D;
39 import javax.swing.JComponent;
40 import javax.swing.JFrame;
41 import javax.swing.JPanel;
42 import javax.swing.RepaintManager;
43 import javax.swing.SwingUtilities;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48 /**
49 * Creation-Date: 11.10.2005, 14:03:15
50 *
51 * @author Thomas Morgner
52 */
53 public class ComponentDrawable
54 {
55 private static final Log logger = LogFactory.getLog (ComponentDrawable.class);
56
57 /**
58 * A runnable that executes the drawing operation on the event-dispatcher thread.
59 */
60 private class PainterRunnable implements Runnable
61 {
62 /**
63 * The draw-area as defined by the drawable.
64 */
65 private Rectangle2D area;
66 /**
67 * The graphics-context to which the runnable draws to.
68 */
69 private Graphics2D graphics;
70
71 /**
72 * Default constructor.
73 */
74 protected PainterRunnable()
75 {
76 }
77
78 /**
79 * Returns the Graphics2D to which the drawable should be rendered.
80 *
81 * @return the graphics.
82 */
83 public Graphics2D getGraphics()
84 {
85 return graphics;
86 }
87
88 /**
89 * Defines the Graphics2D to which the drawable should be rendered.
90 *
91 * @param graphics the graphics.
92 */
93 public void setGraphics(final Graphics2D graphics)
94 {
95 this.graphics = graphics;
96 }
97
98 /**
99 * Returns the draw-area to which the drawable should be rendered.
100 *
101 * @return the graphics.
102 */
103 public Rectangle2D getArea()
104 {
105 return area;
106 }
107
108 /**
109 * Defines the draw-area to which the drawable should be rendered.
110 *
111 * @param area the graphics.
112 */
113 public void setArea(final Rectangle2D area)
114 {
115 this.area = area;
116 }
117
118 /**
119 * Draws the drawable.
120 */
121 public void run()
122 {
123 try
124 {
125 final Component component = getComponent();
126 if (component instanceof Window)
127 {
128 final Window w = (Window) component;
129 w.validate();
130 }
131 else if (isOwnPeerConnected())
132 {
133 final Window w = ComponentDrawable.getWindowAncestor(component);
134 if (w != null)
135 {
136 w.validate();
137 }
138 }
139 else
140 {
141 peerSupply.pack();
142 contentPane.add(component);
143 }
144
145 component.setBounds
146 ((int) area.getX(), (int) area.getY(),
147 (int) area.getWidth(), (int) area.getHeight());
148 component.validate();
149 component.paint(graphics);
150 }
151 finally
152 {
153 cleanUp();
154 }
155 }
156 }
157
158 /**
159 * A runnable that queries the preferred size. As this may involve font-computations, this has to be done on the
160 * EventDispatcher thread.
161 */
162 private class PreferredSizeRunnable implements Runnable
163 {
164 /**
165 * The return value.
166 */
167 private Dimension retval;
168
169 /**
170 * Default Constructor.
171 */
172 protected PreferredSizeRunnable()
173 {
174 }
175
176 /**
177 * Returns the dimension that has been computed.
178 *
179 * @return the preferred size.
180 */
181 public Dimension getRetval()
182 {
183 return retval;
184 }
185
186 /**
187 * Computes the preferred size on the EventDispatcherThread.
188 */
189 public void run()
190 {
191 retval = null;
192 try
193 {
194 final Component component = getComponent();
195 if (component instanceof Window == false && isOwnPeerConnected() == false)
196 {
197 peerSupply.pack();
198 contentPane.add(component);
199 contentPane.validate();
200 component.validate();
201 }
202 else if (isOwnPeerConnected())
203 {
204 retval = component.getSize();
205 return;
206 }
207 else
208 {
209 component.validate();
210 }
211 retval = component.getPreferredSize();
212 }
213 finally
214 {
215 cleanUp();
216 }
217 }
218 }
219
220 /**
221 * A runnable that queries the defined size. As this may involve font-computations, this has to be done on the
222 * EventDispatcher thread.
223 */
224 private class DefinedSizeRunnable implements Runnable
225 {
226 /**
227 * The return value.
228 */
229 private Dimension retval;
230
231 /**
232 * Default Constructor.
233 */
234 protected DefinedSizeRunnable()
235 {
236 }
237
238 /**
239 * Returns the dimension that has been computed.
240 *
241 * @return the declared size.
242 */
243 public Dimension getRetval()
244 {
245 return retval;
246 }
247
248 /**
249 * Computes the declared size on the EventDispatcherThread.
250 */
251 public void run()
252 {
253 retval = null;
254 try
255 {
256 final Component component = getComponent();
257 if (component instanceof Window == false && isOwnPeerConnected() == false)
258 {
259 peerSupply.pack();
260 contentPane.add(component);
261 }
262 retval = component.getSize();
263 }
264 finally
265 {
266 cleanUp();
267 }
268 }
269 }
270
271 /**
272 * A flag indicating whether the aspect ratio should be preserved by the layouter.
273 */
274 private boolean preserveAspectRatio;
275 /**
276 * The component that should be drawn.
277 */
278 private Component component;
279 /**
280 * The Frame that connects the component to the native Window-System.
281 */
282 private JFrame peerSupply;
283 /**
284 * The content pane of the frame.
285 */
286 private JPanel contentPane;
287 /**
288 * The runnable that paints the component.
289 */
290 private PainterRunnable runnable;
291 /**
292 * The runnable that computes the preferred size.
293 */
294 private PreferredSizeRunnable preferredSizeRunnable;
295 /**
296 * The runnable that computes the declared size.
297 */
298 private DefinedSizeRunnable sizeRunnable;
299 /**
300 * A flag indicating whether all paint-operations should be performed on the Event-Dispatcher thread.
301 */
302 private boolean paintSynchronized;
303 /**
304 * A flag indicating whether components are allowed to provide their own AWT-Window.
305 */
306 private boolean allowOwnPeer;
307
308 /**
309 * Default Constructor.
310 */
311 public ComponentDrawable()
312 {
313 this(new JFrame());
314 }
315
316 /**
317 * Creates a new ComponentDrawable with the given Frame as peer-supply.
318 *
319 * @param peerSupply the frame that should be used as peer-source.
320 */
321 public ComponentDrawable(final JFrame peerSupply)
322 {
323 if (peerSupply == null)
324 {
325 throw new NullPointerException();
326 }
327 this.peerSupply = peerSupply;
328 this.contentPane = new JPanel();
329 this.contentPane.setLayout(null);
330 peerSupply.setContentPane(contentPane);
331 this.runnable = new PainterRunnable();
332 this.preferredSizeRunnable = new PreferredSizeRunnable();
333 this.sizeRunnable = new DefinedSizeRunnable();
334 }
335
336 /**
337 * Returns, whether components are allowed to provide their own AWT-Window.
338 *
339 * @return true, if foreign peers are allowed, false otherwise.
340 */
341 public boolean isAllowOwnPeer()
342 {
343 return allowOwnPeer;
344 }
345
346 /**
347 * Defines, whether components are allowed to provide their own AWT-Window.
348 *
349 * @param allowOwnPeer true, if components can provide their own peers, false otherwise.
350 */
351 public void setAllowOwnPeer(final boolean allowOwnPeer)
352 {
353 this.allowOwnPeer = allowOwnPeer;
354 }
355
356 /**
357 * Returns, whether component operations will happen on the Event-Dispatcher threads. As the AWT is not synchronized,
358 * weird things can happen if AWT functionality is executed on user threads.
359 *
360 * @return true, if all operations will be done on the AWT-EventDispatcher thread, false if they should be done in the
361 * local thread.
362 */
363 public boolean isPaintSynchronized()
364 {
365 return paintSynchronized;
366 }
367
368 /**
369 * Defines, whether component operations will happen on the Event-Dispatcher threads. As the AWT is not synchronized,
370 * weird things can happen if AWT functionality is executed on user threads.
371 *
372 * @param paintSynchronized true, if all operations will be done on the AWT-EventDispatcher thread, false if they
373 * should be done in the local thread.
374 */
375 public void setPaintSynchronized(final boolean paintSynchronized)
376 {
377 this.paintSynchronized = paintSynchronized;
378 }
379
380 /**
381 * A helper method that performs some cleanup and disconnects the component from the AWT and the Swing-Framework to
382 * avoid memory-leaks.
383 */
384 protected final void cleanUp()
385 {
386 if (component instanceof JComponent && isOwnPeerConnected() == false)
387 {
388 final JComponent jc = (JComponent) component;
389 RepaintManager.currentManager(jc).removeInvalidComponent(jc);
390 RepaintManager.currentManager(jc).markCompletelyClean(jc);
391 }
392 contentPane.removeAll();
393 RepaintManager.currentManager(contentPane).removeInvalidComponent(contentPane);
394 RepaintManager.currentManager(contentPane).markCompletelyClean(contentPane);
395 peerSupply.dispose();
396 }
397
398 /**
399 * Returns the component that should be drawn.
400 *
401 * @return the component.
402 */
403 public Component getComponent()
404 {
405 return component;
406 }
407
408 /**
409 * Defines the component that should be drawn.
410 *
411 * @param component the component.
412 */
413 public void setComponent(final Component component)
414 {
415 this.component = component;
416 prepareComponent(component);
417 }
418
419 /**
420 * Returns the preferred size of the drawable. If the drawable is aspect ratio aware, these bounds should be used to
421 * compute the preferred aspect ratio for this drawable.
422 * <p/>
423 * This calls {@link java.awt.Component#getPreferredSize()} on the given component.
424 *
425 * @return the preferred size.
426 */
427 public Dimension getPreferredSize()
428 {
429 if (component == null)
430 {
431 return new Dimension(0, 0);
432 }
433 if (SwingUtilities.isEventDispatchThread() || paintSynchronized == false)
434 {
435 preferredSizeRunnable.run();
436 return preferredSizeRunnable.getRetval();
437 }
438
439 try
440 {
441 SwingUtilities.invokeAndWait(preferredSizeRunnable);
442 return preferredSizeRunnable.getRetval();
443 }
444 catch (Exception e)
445 {
446 ComponentDrawable.logger.warn("Failed to compute the preferred size.");
447 }
448 return new Dimension(0, 0);
449 }
450
451 /**
452 * Returns the declared size of the drawable. If the drawable is aspect ratio aware, these bounds should be used to
453 * compute the declared aspect ratio for this drawable.
454 * <p/>
455 * This calls {@link java.awt.Component#getSize()} on the given component.
456 *
457 * @return the preferred size.
458 */
459 public Dimension getSize()
460 {
461 if (component == null)
462 {
463 return new Dimension(0, 0);
464 }
465 if (SwingUtilities.isEventDispatchThread() || paintSynchronized == false)
466 {
467 sizeRunnable.run();
468 return sizeRunnable.getRetval();
469 }
470
471 try
472 {
473 SwingUtilities.invokeAndWait(sizeRunnable);
474 return sizeRunnable.getRetval();
475 }
476 catch (Exception e)
477 {
478 ComponentDrawable.logger.warn("Failed to compute the defined size.");
479 }
480 return new Dimension(0, 0);
481 }
482
483 /**
484 * A private helper method that checks, whether the component provides an own peer.
485 *
486 * @return true, if the component has an own peer, false otherwise.
487 */
488 protected final boolean isOwnPeerConnected()
489 {
490 if (allowOwnPeer == false)
491 {
492 return false;
493 }
494 final Window windowAncestor = ComponentDrawable.getWindowAncestor(component);
495 return (windowAncestor != null && windowAncestor != peerSupply);
496 }
497
498 /**
499 * A private helper method that locates the Window to which the component is currently added.
500 *
501 * @param component the component for which the Window should be returned.
502 * @return the AWT-Window that is the (possibly indirect) parent of this component.
503 */
504 protected static Window getWindowAncestor(final Component component)
505 {
506 Component parent = component.getParent();
507 while (parent != null)
508 {
509 if (parent instanceof Window)
510 {
511 return (Window) parent;
512 }
513 parent = parent.getParent();
514 }
515 return null;
516 }
517
518 /**
519 * Defines whether the layouter should preserve the aspect ratio of the component's preferred size.
520 *
521 * @param preserveAspectRatio true, if the aspect ratio should be preserved, false otherwise.
522 */
523 public void setPreserveAspectRatio(final boolean preserveAspectRatio)
524 {
525 this.preserveAspectRatio = preserveAspectRatio;
526 }
527
528 /**
529 * Returns true, if this drawable will preserve an aspect ratio during the drawing.
530 *
531 * @return true, if an aspect ratio is preserved, false otherwise.
532 */
533 public boolean isPreserveAspectRatio()
534 {
535 return preserveAspectRatio;
536 }
537
538 /**
539 * Draws the component.
540 *
541 * @param g2 the graphics device.
542 * @param area the area inside which the object should be drawn.
543 */
544 public void draw(final Graphics2D g2, final Rectangle2D area)
545 {
546 if (component == null)
547 {
548 return;
549 }
550
551 runnable.setArea(area);
552 runnable.setGraphics(g2);
553
554 if (SwingUtilities.isEventDispatchThread() || paintSynchronized == false)
555 {
556 runnable.run();
557 }
558 else
559 {
560 try
561 {
562 SwingUtilities.invokeAndWait(runnable);
563 }
564 catch (Exception e)
565 {
566 ComponentDrawable.logger.warn("Failed to redraw the component.");
567 }
568 }
569 }
570
571 /**
572 * Prepares the component for drawing. This recursively disables the double-buffering as this would interfere
573 * with the drawing.
574 *
575 * @param c the component that should be prepared.
576 */
577 private void prepareComponent(final Component c)
578 {
579 if (c instanceof JComponent)
580 {
581 final JComponent jc = (JComponent) c;
582 jc.setDoubleBuffered(false);
583 final Component[] childs = jc.getComponents();
584 for (int i = 0; i < childs.length; i++)
585 {
586 final Component child = childs[i];
587 prepareComponent(child);
588 }
589 }
590 }
591 }