]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DRepaintManager.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / G2DRepaintManager.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.g2d;
13
14 import java.awt.Component;
15 import java.awt.Container;
16 import java.awt.Dimension;
17 import java.awt.Image;
18 import java.awt.Rectangle;
19
20 import javax.swing.JComponent;
21 import javax.swing.RepaintManager;
22
23
24 public class G2DRepaintManager extends RepaintManager {
25         private RepaintManager delegate;
26         private Class<?> rootClass;
27         private final static Rectangle NULL_RECTANGLE = new Rectangle();
28         private final static Dimension NULL_DIMENSION = new Dimension();
29         public G2DRepaintManager(Class<?> rootClass, RepaintManager repaintManager) {
30             //System.out.println("G2DRepaintManager(" + System.identityHashCode(this) + ", " + rootClass + ", " + repaintManager.getClass() + ")");
31             delegate = repaintManager;
32             this.rootClass = rootClass;
33         }
34         
35         void setDelegate(RepaintManager manager) {
36             //System.out.println("G2DRepaintManager(" + System.identityHashCode(this) + ").setDelegate(" + manager + ")");
37             this.delegate = manager;
38         }
39         
40         public RepaintManager getDelegate() {
41                 return this.delegate;
42         }
43         
44         @Override
45         public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
46             final JComponent root = (JComponent) getAncestorOfClass(rootClass, c);
47             if (root != null) {
48                 delegate.markCompletelyDirty(root);
49             } else if (delegate != null) {
50                 delegate.addDirtyRegion(c, x, y, w, h);
51             }
52         }
53         
54         @Override
55         public void addInvalidComponent(JComponent invalidComponent) {
56             if (delegate != null) {
57                 delegate.addInvalidComponent(invalidComponent);
58             }
59         }
60
61         @Override
62         public Rectangle getDirtyRegion(JComponent component) {
63             if (delegate != null) {
64                 return delegate.getDirtyRegion(component);
65             } else {
66                 return NULL_RECTANGLE;
67             }
68         }
69
70         @Override
71         public Dimension getDoubleBufferMaximumSize() {
72             if (delegate != null) {
73                 return delegate.getDoubleBufferMaximumSize();
74             } else {
75                 assert false;
76                 return NULL_DIMENSION;
77             }
78         }
79
80         @Override
81         public Image getOffscreenBuffer(Component c, int proposedWidth,
82                 int proposedHeight) {
83             if (delegate != null) {
84                 return delegate.getOffscreenBuffer(c, proposedWidth, proposedHeight);
85             } else {
86                 assert false;
87                 return null;
88             }
89         }
90
91         @Override
92         public Image getVolatileOffscreenBuffer(Component c, int proposedWidth,
93                 int proposedHeight) {
94             if (delegate != null) {
95                 return delegate.getVolatileOffscreenBuffer(c, proposedWidth,
96                         proposedHeight);
97             } else {
98                 assert false;
99                 return null;
100             }
101         }
102
103         @Override
104         public boolean isCompletelyDirty(JComponent component) {
105             if (delegate != null) {
106                 return delegate.isCompletelyDirty(component);
107             } else {
108                 assert false;
109                 return false;
110             }
111         }
112
113         @Override
114         public boolean isDoubleBufferingEnabled() {
115             if (delegate != null) {
116                 return delegate.isDoubleBufferingEnabled();
117             } else {
118                 assert false;
119                 return false;
120             }
121         }
122
123         @Override
124         public void markCompletelyClean(JComponent component) {
125             if (delegate != null) {
126                 delegate.markCompletelyClean(component);
127             }
128         }
129
130         public static Container getAncestorOfClass(Class<?> c, Component comp)
131         {
132             if(comp == null || c == null)
133                 return null;
134
135             if (c.isInstance(comp) && comp instanceof Container)
136                 return (Container) comp;
137
138             Container parent = comp.getParent();
139             while(parent != null && !(c.isInstance(parent)))
140                 parent = parent.getParent();
141             return parent;
142         }
143         
144         @Override
145         public void markCompletelyDirty(JComponent component) {
146             final JComponent root = (JComponent) getAncestorOfClass(rootClass, component);
147             if (root != null) {
148                 delegate.markCompletelyDirty(root);
149             } else if (delegate != null) {
150                 delegate.markCompletelyDirty(component);
151             }
152         }
153
154         @Override
155         public void paintDirtyRegions() {
156 //              new Exception("paintDirtyRegions " + delegate).printStackTrace();
157             if (delegate != null) {
158                 delegate.paintDirtyRegions();
159             } else {
160                 assert false;
161             }
162         }
163         
164         @Override
165         public void removeInvalidComponent(JComponent component) {
166             if (delegate != null) {
167                 delegate.removeInvalidComponent(component);
168             } else {
169                 assert false;
170             }
171         }
172
173         @Override
174         public void setDoubleBufferingEnabled(boolean flag) {
175             if (delegate != null) {
176                 delegate.setDoubleBufferingEnabled(flag);
177             } else {
178                 assert false;
179             }
180         }
181
182         @Override
183         public void setDoubleBufferMaximumSize(Dimension d) {
184             if (delegate != null) {
185                 delegate.setDoubleBufferMaximumSize(d);
186             } else {
187                 assert false;
188             }
189         }
190
191         @Override
192         public void validateInvalidComponents() {
193             if (delegate != null) {
194                 delegate.validateInvalidComponents();
195             } else {
196                 assert false;
197             }
198         }
199     }