1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.scenegraph.g2d;
\r
14 import java.awt.Component;
\r
15 import java.awt.Container;
\r
16 import java.awt.Dimension;
\r
17 import java.awt.Image;
\r
18 import java.awt.Rectangle;
\r
20 import javax.swing.JComponent;
\r
21 import javax.swing.RepaintManager;
\r
24 public class G2DRepaintManager extends RepaintManager {
\r
25 private RepaintManager delegate;
\r
26 private Class<?> rootClass;
\r
27 private final static Rectangle NULL_RECTANGLE = new Rectangle();
\r
28 private final static Dimension NULL_DIMENSION = new Dimension();
\r
29 public G2DRepaintManager(Class<?> rootClass, RepaintManager repaintManager) {
\r
30 //System.out.println("G2DRepaintManager(" + System.identityHashCode(this) + ", " + rootClass + ", " + repaintManager.getClass() + ")");
\r
31 delegate = repaintManager;
\r
32 this.rootClass = rootClass;
\r
35 void setDelegate(RepaintManager manager) {
\r
36 //System.out.println("G2DRepaintManager(" + System.identityHashCode(this) + ").setDelegate(" + manager + ")");
\r
37 this.delegate = manager;
\r
40 public RepaintManager getDelegate() {
\r
41 return this.delegate;
\r
45 public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
\r
46 final JComponent root = (JComponent) getAncestorOfClass(rootClass, c);
\r
48 delegate.markCompletelyDirty(root);
\r
49 } else if (delegate != null) {
\r
50 delegate.addDirtyRegion(c, x, y, w, h);
\r
55 public void addInvalidComponent(JComponent invalidComponent) {
\r
56 if (delegate != null) {
\r
57 delegate.addInvalidComponent(invalidComponent);
\r
62 public Rectangle getDirtyRegion(JComponent component) {
\r
63 if (delegate != null) {
\r
64 return delegate.getDirtyRegion(component);
\r
66 return NULL_RECTANGLE;
\r
71 public Dimension getDoubleBufferMaximumSize() {
\r
72 if (delegate != null) {
\r
73 return delegate.getDoubleBufferMaximumSize();
\r
76 return NULL_DIMENSION;
\r
81 public Image getOffscreenBuffer(Component c, int proposedWidth,
\r
82 int proposedHeight) {
\r
83 if (delegate != null) {
\r
84 return delegate.getOffscreenBuffer(c, proposedWidth, proposedHeight);
\r
92 public Image getVolatileOffscreenBuffer(Component c, int proposedWidth,
\r
93 int proposedHeight) {
\r
94 if (delegate != null) {
\r
95 return delegate.getVolatileOffscreenBuffer(c, proposedWidth,
\r
104 public boolean isCompletelyDirty(JComponent component) {
\r
105 if (delegate != null) {
\r
106 return delegate.isCompletelyDirty(component);
\r
114 public boolean isDoubleBufferingEnabled() {
\r
115 if (delegate != null) {
\r
116 return delegate.isDoubleBufferingEnabled();
\r
124 public void markCompletelyClean(JComponent component) {
\r
125 if (delegate != null) {
\r
126 delegate.markCompletelyClean(component);
\r
130 public static Container getAncestorOfClass(Class<?> c, Component comp)
\r
132 if(comp == null || c == null)
\r
135 if (c.isInstance(comp) && comp instanceof Container)
\r
136 return (Container) comp;
\r
138 Container parent = comp.getParent();
\r
139 while(parent != null && !(c.isInstance(parent)))
\r
140 parent = parent.getParent();
\r
145 public void markCompletelyDirty(JComponent component) {
\r
146 final JComponent root = (JComponent) getAncestorOfClass(rootClass, component);
\r
147 if (root != null) {
\r
148 delegate.markCompletelyDirty(root);
\r
149 } else if (delegate != null) {
\r
150 delegate.markCompletelyDirty(component);
\r
155 public void paintDirtyRegions() {
\r
156 // new Exception("paintDirtyRegions " + delegate).printStackTrace();
\r
157 if (delegate != null) {
\r
158 delegate.paintDirtyRegions();
\r
165 public void removeInvalidComponent(JComponent component) {
\r
166 if (delegate != null) {
\r
167 delegate.removeInvalidComponent(component);
\r
174 public void setDoubleBufferingEnabled(boolean flag) {
\r
175 if (delegate != null) {
\r
176 delegate.setDoubleBufferingEnabled(flag);
\r
183 public void setDoubleBufferMaximumSize(Dimension d) {
\r
184 if (delegate != null) {
\r
185 delegate.setDoubleBufferMaximumSize(d);
\r
192 public void validateInvalidComponents() {
\r
193 if (delegate != null) {
\r
194 delegate.validateInvalidComponents();
\r