KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > DrawComponentPaintable


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer;
28
29 import java.awt.Graphics2D JavaDoc;
30 import java.awt.geom.Rectangle2D JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.eclipse.swt.graphics.GC;
35 import org.eclipse.swt.widgets.Control;
36
37 import org.nightlabs.base.util.GeomUtil;
38 import org.nightlabs.editor2d.DrawComponent;
39 import org.nightlabs.editor2d.DrawComponentContainer;
40 import org.nightlabs.editor2d.Layer;
41 import org.nightlabs.editor2d.render.Renderer;
42 import org.nightlabs.editor2d.viewer.j2dswt.SWTGraphics2D;
43
44 public class DrawComponentPaintable
45 //implements IPaintable
46
{
47     public static final Logger LOGGER = Logger.getLogger(DrawComponentPaintable.class.getName());
48     
49     protected DrawComponent dc;
50     public DrawComponentPaintable(DrawComponent dc)
51     {
52         super();
53         this.dc = dc;
54     }
55
56     /**
57      * calls paintDrawComponent with the given DrawComponent and the given
58      * Graphics2D
59      *
60      * @see org.holongate.j2d.IPaintable#paint(org.eclipse.swt.widgets.Control, java.awt.Graphics2D)
61      */

62     public void paint(Control control, Graphics2D JavaDoc g2d)
63     {
64         LOGGER.debug("paint called!");
65         long startTime = System.currentTimeMillis();
66         paintDrawComponent(dc, g2d);
67         long endTime = System.currentTimeMillis() - startTime;
68         LOGGER.debug("paint took = "+endTime+" ms!");
69     }
70
71     /**
72      * does nothing
73      * @see org.holongate.j2d.IPaintable#redraw(org.eclipse.swt.widgets.Control, org.eclipse.swt.graphics.GC)
74      */

75     public void redraw(Control control, GC gc)
76     {
77         LOGGER.debug("redraw called!");
78         long startTime = System.currentTimeMillis();
79         paintDrawComponent(dc, new SWTGraphics2D(gc));
80         long endTime = System.currentTimeMillis() - startTime;
81         LOGGER.debug("redraw took = "+endTime+" ms!");
82     }
83
84     /**
85      * returns the bounds of the Control
86      * @see org.holongate.j2d.IPaintable#getBounds(org.eclipse.swt.widgets.Control)
87      */

88     public Rectangle2D JavaDoc getBounds(Control control)
89     {
90         return GeomUtil.toRectangle2D(control.getBounds());
91     }
92
93     /**
94      * Checks if the given DrawComponent is a
95      * DrawComponentContainer and if so, it paints all its children recursivly.
96      * If the given DrawComponent is no DrawComponentContainer it just paint
97      * its renderer (this also happens to the children of a DrawComponentContainer,
98      * if these are not DrawComponentContainer themselves)
99      *
100      * @param dc The DrawComponent to paint
101      * @param g2d the Graphics2D to draw on
102      *
103      * @see org.nightlabs.editor2d.render.Renderer
104      * @see org.nightlabs.editor2d.DrawComponent.getRenderer()
105      */

106     public static void paintDrawComponent(DrawComponent dc, Graphics2D JavaDoc g2d)
107     {
108         if (dc instanceof DrawComponentContainer)
109         {
110             if (dc instanceof Layer)
111             {
112                 Layer l = (Layer) dc;
113                 if (!l.isVisible())
114                     return;
115             }
116             DrawComponentContainer dcContainer = (DrawComponentContainer) dc;
117             for (Iterator JavaDoc it = dcContainer.getDrawComponents().iterator(); it.hasNext(); ) {
118                 DrawComponent drawComponent = (DrawComponent) it.next();
119                 paintDrawComponent(drawComponent, g2d);
120             }
121         }
122         else {
123             Renderer r = dc.getRenderer();
124             if (r != null)
125                 r.paint(dc, g2d);
126         }
127     }
128 }
129
130
Popular Tags