1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d;
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;
20 import javax.swing.JComponent;
21 import javax.swing.RepaintManager;
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;
35 void setDelegate(RepaintManager manager) {
36 //System.out.println("G2DRepaintManager(" + System.identityHashCode(this) + ").setDelegate(" + manager + ")");
37 this.delegate = manager;
40 public RepaintManager getDelegate() {
45 public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
46 final JComponent root = (JComponent) getAncestorOfClass(rootClass, c);
48 delegate.markCompletelyDirty(root);
49 } else if (delegate != null) {
50 delegate.addDirtyRegion(c, x, y, w, h);
55 public void addInvalidComponent(JComponent invalidComponent) {
56 if (delegate != null) {
57 delegate.addInvalidComponent(invalidComponent);
62 public Rectangle getDirtyRegion(JComponent component) {
63 if (delegate != null) {
64 return delegate.getDirtyRegion(component);
66 return NULL_RECTANGLE;
71 public Dimension getDoubleBufferMaximumSize() {
72 if (delegate != null) {
73 return delegate.getDoubleBufferMaximumSize();
76 return NULL_DIMENSION;
81 public Image getOffscreenBuffer(Component c, int proposedWidth,
83 if (delegate != null) {
84 return delegate.getOffscreenBuffer(c, proposedWidth, proposedHeight);
92 public Image getVolatileOffscreenBuffer(Component c, int proposedWidth,
94 if (delegate != null) {
95 return delegate.getVolatileOffscreenBuffer(c, proposedWidth,
104 public boolean isCompletelyDirty(JComponent component) {
105 if (delegate != null) {
106 return delegate.isCompletelyDirty(component);
114 public boolean isDoubleBufferingEnabled() {
115 if (delegate != null) {
116 return delegate.isDoubleBufferingEnabled();
124 public void markCompletelyClean(JComponent component) {
125 if (delegate != null) {
126 delegate.markCompletelyClean(component);
130 public static Container getAncestorOfClass(Class<?> c, Component comp)
132 if(comp == null || c == null)
135 if (c.isInstance(comp) && comp instanceof Container)
136 return (Container) comp;
138 Container parent = comp.getParent();
139 while(parent != null && !(c.isInstance(parent)))
140 parent = parent.getParent();
145 public void markCompletelyDirty(JComponent component) {
146 final JComponent root = (JComponent) getAncestorOfClass(rootClass, component);
148 delegate.markCompletelyDirty(root);
149 } else if (delegate != null) {
150 delegate.markCompletelyDirty(component);
155 public void paintDirtyRegions() {
156 // new Exception("paintDirtyRegions " + delegate).printStackTrace();
157 if (delegate != null) {
158 delegate.paintDirtyRegions();
165 public void removeInvalidComponent(JComponent component) {
166 if (delegate != null) {
167 delegate.removeInvalidComponent(component);
174 public void setDoubleBufferingEnabled(boolean flag) {
175 if (delegate != null) {
176 delegate.setDoubleBufferingEnabled(flag);
183 public void setDoubleBufferMaximumSize(Dimension d) {
184 if (delegate != null) {
185 delegate.setDoubleBufferMaximumSize(d);
192 public void validateInvalidComponents() {
193 if (delegate != null) {
194 delegate.validateInvalidComponents();