summaryrefslogtreecommitdiffstats
path: root/src/demos/xtrans/OffscreenDesktopPane.java
blob: 780066671d71425b24d4e46b9940aded4521d23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package demos.xtrans;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

// Internal JOGL API references
import com.sun.opengl.impl.Debug;

/** A subclass of JDesktopPane which performs all of the rendering of
    its child components into an off-screen buffer. Provides access to
    this back buffer so subclasses can determine how to draw the
    contents of the child windows to the screen. */

public class OffscreenDesktopPane extends JDesktopPane {
  //
  // Debugging functionality only
  //
  private static final boolean DEBUG = Debug.debug("OffscreenDesktopPane");
  private static final Color[] colors = {
    Color.LIGHT_GRAY,
    Color.CYAN,
    Color.PINK,
    Color.GRAY,
    Color.MAGENTA,
    Color.BLUE,
    Color.ORANGE,
    Color.DARK_GRAY,
    Color.RED,
    Color.YELLOW
  };
  private int colorIdx;

  private Color getNextColor() {
    Color c = colors[colorIdx];
    colorIdx = (colorIdx + 1) % colors.length;
    return c;
  }

  private Map/*<Component, Color>*/ componentColorMap;

  /** Constructs a new OffscreenDesktopPane. */
  public OffscreenDesktopPane() {
    super();
    if (DEBUG) {
      componentColorMap = new WeakHashMap();
    }
    // Only use an OffscreenDesktopManager instance directly if we
    // have not been subclassed
    if (getClass() == OffscreenDesktopPane.class) {
      setDesktopManager(new OffscreenDesktopManager());
    }
  }

  /** Overrides superclass's addImpl to insert an
      OffscreenComponentWrapper between the added component and this
      one. The wrapper component produces Graphics objects used when
      children repaint themselves directly. */
  protected void addImpl(Component c, Object constraints, int index) {
    if (c instanceof OffscreenComponentWrapper) {
      throw new RuntimeException("Should not add OffscreenComponentWrappers directly");
    }
    OffscreenComponentWrapper wrapper = new OffscreenComponentWrapper(c);
    // Note: this is essential in order to keep mouse events
    // propagating correctly down the hierarchy
    wrapper.setBounds(getBounds());
    OffscreenDesktopManager.switchDoubleBuffering(wrapper, false);
    super.addImpl(wrapper, constraints, index);
    if (DEBUG) {
      componentColorMap.put(c, getNextColor());
    }
    getOffscreenDesktopManager().setNeedsReLayout();
    repaint();
  }

  // In order to hide the presence of the OffscreenComponentWrapper a
  // little more, we override remove to make it look like we can
  // simply pass in the JInternalFrames inside the
  // OffscreenComponentWrappers. There are some situations where we
  // can't hide the presence of the OffscreenComponentWrapper (such as
  // when calling getParent() of the JInternalFrame) so to avoid
  // incorrect behavior of the rest of the toolkit we don't override
  // getComponent() to skip the OffscreenComponentWrappers.

  /** Removes the component at the given index. */
  public void remove(int index) {
    Component c = getComponent(index);
    super.remove(index);
    OffscreenDesktopManager.switchDoubleBuffering(c, true);
  }

  /** Removes the specified component from this
      OffscreenDesktopPane. This method accepts either the components
      added by the application (which are not direct children of this
      one) or the OffscreenComponentWrappers added implicitly by the
      add() method. */
  public void remove(Component comp) {
    comp = getWrapper(comp);
    if (comp == null) {
      // This component must not be one of our children
      return;
    }
    super.remove(comp);
    OffscreenDesktopManager.switchDoubleBuffering(comp, true);
  }

  public void reshape(int x, int y, int w, int h) {
    super.reshape(x, y, w, h);
    Rectangle rect = new Rectangle(x, y, w, h);
    Component[] cs = getComponents();
    for (int i = 0; i < cs.length; i++) {
      // Note: this is essential in order to keep mouse events
      // propagating correctly down the hierarchy
      ((OffscreenComponentWrapper) cs[i]).setBounds(rect);
    }
  }

  /** Overridden from JLayeredPane for convenience when manipulating
      child components. Accepts either the component added by the
      application or the OffscreenComponentWrapper added implicitly by
      the add() method. */
  public void setPosition(Component c, int position) {
    super.setPosition(getWrapper(c), position);
  }

  /** Paints all children of this OffscreenDesktopPane to the internal
      off-screen buffer. Does no painting to the passed Graphics
      object; this is the responsibility of subclasses. */
  protected void paintChildren(Graphics g) {
    // Update desktop manager's offscreen buffer if necessary
    getOffscreenDesktopManager().updateOffscreenBuffer(this);

    if (DEBUG) {
      // Subclasses will need to override this behavior anyway, so
      // only enable an on-screen representation if debugging is
      // enabled. For now, simply paint colored rectangles indicating
      // the on-screen locations of the windows.
      final Component[] components = getRealChildComponents();
      int compCount = components.length;
      for (int i = compCount - 1; i >= 0; i--) {
        Component c = components[i];
        Rectangle r = c.getBounds();
        Color col = (Color) componentColorMap.get(c);
        g.setColor(col);
        g.fillRect(r.x, r.y, r.width, r.height);
      }
    }
  }

  /** Fetches the real child components of this OffscreenDesktopPane,
      skipping all OffscreenComponentWrappers implicitly added. */
  protected Component[] getRealChildComponents() {
    Component[] cs = getComponents();
    for (int i = 0; i < cs.length; i++) {
      cs[i] = ((OffscreenComponentWrapper) cs[i]).getChild();
    }
    return cs;
  }

  /** Returns the OffscreenDesktopManager associated with this
      pane. */
  public OffscreenDesktopManager getOffscreenDesktopManager() {
    return (OffscreenDesktopManager) getDesktopManager();
  }

  /** Returns the real child component of this OffscreenDesktopPane,
      skipping the OffscreenComponentWrapper implicitly added. */
  protected static Component getRealComponent(Component c) {
    if (c instanceof OffscreenComponentWrapper) {
      return ((OffscreenComponentWrapper) c).getChild();
    }    
    return c;
  }

  /** Returns the OffscreenComponentWrapper corresponding to the given
      child component, or the passed component if it is already the
      wrapper. */
  protected static Component getWrapper(Component c) {
    if (c instanceof OffscreenComponentWrapper) {
      return c;
    }
    Component parent = c.getParent();
    if (parent instanceof OffscreenComponentWrapper) {
      return parent;
    }
    return null;
  }
}