1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: public class BasicViewportUI extends ViewportUI
58: {
59:
60: ChangeListener changeListener;
61: Image backingStoreImage;
62: int backingStoreWidth = -1;
63: int backingStoreHeight = -1;
64:
65: class ChangeHandler implements ChangeListener
66: {
67: public void stateChanged(ChangeEvent event)
68: {
69: JViewport v = (JViewport) event.getSource();
70: v.repaint();
71: }
72: }
73:
74: void installDefaults(JComponent c)
75: {
76: c.setOpaque(true);
77: }
78:
79: void uninstallDefaults(JComponent c)
80: {
81: }
82:
83: void installListeners(JComponent c)
84: {
85: ((JViewport)c).addChangeListener(changeListener);
86: }
87:
88: void uninstallListeners(JComponent c)
89: {
90: ((JViewport)c).removeChangeListener(changeListener);
91: }
92:
93: public BasicViewportUI()
94: {
95: changeListener = new ChangeHandler();
96: }
97:
98: public static ComponentUI createUI(JComponent c)
99: {
100: return new BasicViewportUI();
101: }
102:
103: public void installUI(JComponent c)
104: {
105: super.installUI(c);
106: installListeners(c);
107: }
108:
109: public void uninstallUI(JComponent c)
110: {
111: uninstallListeners(c);
112: }
113:
114:
115: public Dimension getPreferredSize(JComponent c)
116: {
117:
118: return null;
119: }
120:
121: public void paint(Graphics g, JComponent c)
122: {
123: JViewport port = (JViewport)c;
124: Component view = port.getView();
125:
126: if (view == null)
127: return;
128:
129: Point pos = port.getViewPosition();
130: Rectangle viewBounds = view.getBounds();
131: Rectangle portBounds = port.getBounds();
132:
133: if (viewBounds.width == 0
134: || viewBounds.height == 0
135: || portBounds.width == 0
136: || portBounds.height == 0)
137: return;
138:
139: switch (port.getScrollMode())
140: {
141:
142: case JViewport.BACKINGSTORE_SCROLL_MODE:
143: paintBackingStore(g, port, view, pos, viewBounds, portBounds);
144: break;
145:
146: case JViewport.BLIT_SCROLL_MODE:
147:
148:
149: case JViewport.SIMPLE_SCROLL_MODE:
150: default:
151: paintSimple(g, port, view, pos, viewBounds, portBounds);
152: break;
153: }
154: }
155:
156: private void paintSimple(Graphics g,
157: JViewport v,
158: Component view,
159: Point pos,
160: Rectangle viewBounds,
161: Rectangle portBounds)
162: {
163: Rectangle oldClip = g.getClipBounds();
164: g.setClip(new Rectangle(0, 0, portBounds.width, portBounds.height));
165: g.translate (-pos.x, -pos.y);
166: try
167: {
168: view.paint(g);
169: }
170: finally
171: {
172: g.translate (pos.x, pos.y);
173: g.setClip (oldClip);
174: }
175: }
176:
177: private void paintBackingStore(Graphics g,
178: JViewport v,
179: Component view,
180: Point pos,
181: Rectangle viewBounds,
182: Rectangle portBounds)
183: {
184: if (backingStoreImage == null
185: || backingStoreWidth != viewBounds.width
186: || backingStoreHeight != viewBounds.height)
187: {
188: backingStoreImage = v.createImage(viewBounds.width, viewBounds.height);
189: backingStoreWidth = viewBounds.width;
190: backingStoreHeight = viewBounds.height;
191: }
192:
193: Graphics g2 = backingStoreImage.getGraphics();
194:
195: if (v.getBackground() != null)
196: {
197:
198: java.awt.Color save = g2.getColor();
199: g2.setColor(v.getBackground());
200: g2.fillRect (0, 0, backingStoreWidth, backingStoreHeight);
201: g2.setColor(save);
202:
203:
204: save = g.getColor();
205: g.setColor(v.getBackground());
206: g.fillRect (0, 0, portBounds.width, portBounds.height);
207: g.setColor(save);
208:
209: }
210: else
211: {
212:
213: g2.clearRect(0, 0, backingStoreWidth, backingStoreHeight);
214:
215:
216: g.clearRect(0, 0, portBounds.width, portBounds.height);
217: }
218:
219: g2.setClip(g.getClipBounds());
220: g2.translate(-pos.x, -pos.y);
221: try
222: {
223: view.paint(g2);
224: }
225: finally
226: {
227: g2.translate(pos.x, pos.y);
228: }
229: g2 = null;
230: g.drawImage(backingStoreImage,
231: 0, 0,
232: (ImageObserver)null);
233: }
234: }