001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Sergey Burlak, Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.awt.Component;
024: import java.awt.ComponentOrientation;
025: import java.awt.Insets;
026: import java.awt.LayoutManager;
027: import java.awt.Point;
028: import java.awt.Rectangle;
029: import java.beans.PropertyChangeEvent;
030: import java.beans.PropertyChangeListener;
031:
032: import javax.accessibility.Accessible;
033: import javax.accessibility.AccessibleContext;
034: import javax.accessibility.AccessibleRole;
035: import javax.swing.border.Border;
036: import javax.swing.event.ChangeEvent;
037: import javax.swing.event.ChangeListener;
038: import javax.swing.plaf.ScrollPaneUI;
039: import javax.swing.plaf.UIResource;
040:
041: import org.apache.harmony.x.swing.StringConstants;
042:
043: import org.apache.harmony.x.swing.internal.nls.Messages;
044:
045: public class JScrollPane extends JComponent implements
046: ScrollPaneConstants, Accessible {
047: //TODO: implement
048: protected class AccessibleJScrollPane extends AccessibleJComponent
049: implements ChangeListener, PropertyChangeListener {
050: public AccessibleJScrollPane() {
051: }
052:
053: protected JViewport viewPort;
054:
055: public void resetViewPort() {
056: }
057:
058: public AccessibleRole getAccessibleRole() {
059: return AccessibleRole.SCROLL_PANE;
060: }
061:
062: public void stateChanged(final ChangeEvent e) {
063: }
064:
065: public void propertyChange(final PropertyChangeEvent e) {
066: }
067: }
068:
069: protected class ScrollBar extends JScrollBar implements UIResource {
070: private boolean unitIncrementSet;
071: private boolean blockIncrementSet;
072:
073: public ScrollBar(final int orientation) {
074: super (orientation);
075: }
076:
077: public void setUnitIncrement(final int unitIncrement) {
078: super .setUnitIncrement(unitIncrement);
079: unitIncrementSet = true;
080: }
081:
082: public int getUnitIncrement(final int direction) {
083: return !unitIncrementSet
084: && getViewport().getView() instanceof Scrollable ? ((Scrollable) getViewport()
085: .getView()).getScrollableUnitIncrement(
086: getViewport().getViewRect(), getOrientation(),
087: direction)
088: : super .getUnitIncrement(direction);
089: }
090:
091: public void setBlockIncrement(final int blockIncrement) {
092: super .setBlockIncrement(blockIncrement);
093: blockIncrementSet = true;
094: }
095:
096: public int getBlockIncrement(final int direction) {
097: return !blockIncrementSet
098: && getViewport().getView() instanceof Scrollable ? ((Scrollable) getViewport()
099: .getView()).getScrollableBlockIncrement(
100: getViewport().getViewRect(), getOrientation(),
101: direction)
102: : super .getBlockIncrement(direction);
103: }
104: }
105:
106: protected JViewport columnHeader;
107: protected JViewport rowHeader;
108: protected JScrollBar horizontalScrollBar;
109: protected JScrollBar verticalScrollBar;
110: protected int horizontalScrollBarPolicy;
111: protected int verticalScrollBarPolicy;
112: protected Component lowerLeft;
113: protected Component lowerRight;
114: protected Component upperLeft;
115: protected Component upperRight;
116: protected JViewport viewport;
117:
118: private Border viewportBorder;
119: private boolean wheelScrollingEnabled = true;
120: private Insets cachedInsets = new Insets(0, 0, 0, 0);
121:
122: private static final String UI_CLASS_ID = "ScrollPaneUI";
123:
124: private static final String WHEEL_SCROLLING_ENABLED_PROPERTY = "wheelScrollingEnabled";
125: private static final String VIEWPORT_BORDER_PROPERTY = "viewportBorder";
126:
127: public JScrollPane() {
128: this (null, VERTICAL_SCROLLBAR_AS_NEEDED,
129: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
130: }
131:
132: public JScrollPane(final Component view) {
133: this (view, VERTICAL_SCROLLBAR_AS_NEEDED,
134: HORIZONTAL_SCROLLBAR_AS_NEEDED);
135: }
136:
137: public JScrollPane(final int vsbPolicy, final int hsbPolicy) {
138: this (null, vsbPolicy, hsbPolicy);
139: }
140:
141: public JScrollPane(final Component view, final int vsbPolicy,
142: final int hsbPolicy) {
143: setVerticalScrollBarPolicy(vsbPolicy);
144: setHorizontalScrollBarPolicy(hsbPolicy);
145:
146: JViewport vp = createViewport();
147: vp.setView(view);
148: setViewport(vp);
149:
150: setVerticalScrollBar(createVerticalScrollBar());
151: setHorizontalScrollBar(createHorizontalScrollBar());
152:
153: updateUI();
154:
155: int initialViewXPos = getComponentOrientation().isLeftToRight() ? 0
156: : Short.MAX_VALUE;
157: int initialViewYPos = getComponentOrientation().isHorizontal() ? 0
158: : Short.MAX_VALUE;
159:
160: vp.setViewPosition(new Point(initialViewXPos, initialViewYPos));
161: }
162:
163: public ScrollPaneUI getUI() {
164: return (ScrollPaneUI) ui;
165: }
166:
167: public void setUI(final ScrollPaneUI ui) {
168: super .setUI(ui);
169: }
170:
171: public void updateUI() {
172: setUI((ScrollPaneUI) UIManager.getUI(this ));
173: }
174:
175: public String getUIClassID() {
176: return UI_CLASS_ID;
177: }
178:
179: public void setLayout(final LayoutManager layout) {
180: super .setLayout(layout);
181: if (layout != null) {
182: ((ScrollPaneLayout) layout).syncWithScrollPane(this );
183: }
184: }
185:
186: public boolean isValidateRoot() {
187: return true;
188: }
189:
190: public int getVerticalScrollBarPolicy() {
191: return verticalScrollBarPolicy;
192: }
193:
194: public void setVerticalScrollBarPolicy(final int policy) {
195: if (policy != VERTICAL_SCROLLBAR_AS_NEEDED
196: && policy != VERTICAL_SCROLLBAR_NEVER
197: && policy != VERTICAL_SCROLLBAR_ALWAYS) {
198:
199: throw new IllegalArgumentException(Messages
200: .getString("swing.25")); //$NON-NLS-1$
201: }
202: if (this .verticalScrollBarPolicy != policy) {
203: int oldValue = this .verticalScrollBarPolicy;
204: this .verticalScrollBarPolicy = policy;
205: firePropertyChange(
206: StringConstants.VERTICAL_SCROLLBAR_POLICY_PROPERTY,
207: oldValue, policy);
208: }
209: }
210:
211: public int getHorizontalScrollBarPolicy() {
212: return horizontalScrollBarPolicy;
213: }
214:
215: public void setHorizontalScrollBarPolicy(final int policy) {
216: if (policy != HORIZONTAL_SCROLLBAR_AS_NEEDED
217: && policy != HORIZONTAL_SCROLLBAR_NEVER
218: && policy != HORIZONTAL_SCROLLBAR_ALWAYS) {
219:
220: throw new IllegalArgumentException(Messages
221: .getString("swing.25")); //$NON-NLS-1$
222: }
223: if (this .horizontalScrollBarPolicy != policy) {
224: int oldValue = this .horizontalScrollBarPolicy;
225: this .horizontalScrollBarPolicy = policy;
226: firePropertyChange(
227: StringConstants.HORIZONTAL_SCROLLBAR_POLICY_PROPERTY,
228: oldValue, policy);
229: }
230: }
231:
232: public Border getViewportBorder() {
233: return viewportBorder;
234: }
235:
236: public void setViewportBorder(final Border viewportBorder) {
237: if (this .viewportBorder != viewportBorder) {
238: Border oldValue = this .viewportBorder;
239: this .viewportBorder = viewportBorder;
240: firePropertyChange(VIEWPORT_BORDER_PROPERTY, oldValue,
241: viewportBorder);
242: }
243: }
244:
245: public Rectangle getViewportBorderBounds() {
246: Rectangle result = new Rectangle(getBounds());
247: Insets insets = getInsets(cachedInsets);
248: result.x = insets.left;
249: result.y = insets.top;
250: result.width -= insets.left + insets.right;
251: result.height -= insets.top + insets.bottom;
252:
253: if (rowHeader != null) {
254: Rectangle rowHeaderBounds = rowHeader.getBounds();
255: if (getComponentOrientation().isLeftToRight()) {
256: result.x += rowHeaderBounds.width;
257: }
258: result.width -= rowHeaderBounds.width;
259: }
260: if (columnHeader != null) {
261: Rectangle columnHeaderBounds = columnHeader.getBounds();
262: result.y += columnHeaderBounds.height;
263: result.height -= columnHeaderBounds.height;
264: }
265: if (verticalScrollBar != null) {
266: Rectangle verticalScrollbarBounds = verticalScrollBar
267: .getBounds();
268: if (!getComponentOrientation().isLeftToRight()) {
269: result.x += verticalScrollbarBounds.width;
270: }
271: result.width -= verticalScrollbarBounds.width;
272: }
273: if (horizontalScrollBar != null) {
274: Rectangle horizontalScrollbarBounds = horizontalScrollBar
275: .getBounds();
276: result.height -= horizontalScrollbarBounds.height;
277: }
278:
279: return result;
280: }
281:
282: public JScrollBar createHorizontalScrollBar() {
283: return new ScrollBar(JScrollBar.HORIZONTAL);
284: }
285:
286: public JScrollBar createVerticalScrollBar() {
287: return new ScrollBar(JScrollBar.VERTICAL);
288: }
289:
290: public JScrollBar getHorizontalScrollBar() {
291: return horizontalScrollBar;
292: }
293:
294: public JScrollBar getVerticalScrollBar() {
295: return verticalScrollBar;
296: }
297:
298: public void setHorizontalScrollBar(
299: final JScrollBar horizontalScrollBar) {
300: if (this .horizontalScrollBar != horizontalScrollBar) {
301: JScrollBar oldValue = this .horizontalScrollBar;
302: this .horizontalScrollBar = horizontalScrollBar;
303: this .horizontalScrollBar
304: .setComponentOrientation(getComponentOrientation());
305: updateComponent(HORIZONTAL_SCROLLBAR, oldValue,
306: horizontalScrollBar,
307: StringConstants.HORIZONTAL_SCROLLBAR_PROPERTY);
308: }
309: }
310:
311: public void setVerticalScrollBar(final JScrollBar verticalScrollBar) {
312: if (this .verticalScrollBar != verticalScrollBar) {
313: JScrollBar oldValue = this .verticalScrollBar;
314: this .verticalScrollBar = verticalScrollBar;
315: this .verticalScrollBar
316: .setComponentOrientation(getComponentOrientation());
317: updateComponent(VERTICAL_SCROLLBAR, oldValue,
318: verticalScrollBar,
319: StringConstants.VERTICAL_SCROLLBAR_PROPERTY);
320: }
321: }
322:
323: public JViewport getViewport() {
324: return viewport;
325: }
326:
327: public void setViewport(final JViewport viewport) {
328: JViewport oldValue = this .viewport;
329: this .viewport = viewport;
330: updateComponent(VIEWPORT, oldValue, viewport,
331: StringConstants.VIEWPORT_PROPERTY);
332: }
333:
334: public void setViewportView(final Component view) {
335: if (viewport == null) {
336: setViewport(createViewport());
337: }
338: viewport.setView(view);
339: }
340:
341: public JViewport getRowHeader() {
342: return rowHeader;
343: }
344:
345: public void setRowHeader(final JViewport rowHeader) {
346: JViewport oldValue = this .rowHeader;
347: this .rowHeader = rowHeader;
348: updateComponent(ROW_HEADER, oldValue, rowHeader,
349: StringConstants.ROW_HEADER_PROPERTY);
350: }
351:
352: public void setRowHeaderView(final Component view) {
353: if (rowHeader == null) {
354: setRowHeader(createViewport());
355: }
356: rowHeader.setView(view);
357: }
358:
359: public JViewport getColumnHeader() {
360: return columnHeader;
361: }
362:
363: public void setColumnHeader(final JViewport columnHeader) {
364: JViewport oldValue = this .columnHeader;
365: this .columnHeader = columnHeader;
366: updateComponent(COLUMN_HEADER, oldValue, columnHeader,
367: StringConstants.COLUMN_HEADER_PROPERTY);
368: }
369:
370: public void setColumnHeaderView(final Component view) {
371: if (columnHeader == null) {
372: setColumnHeader(createViewport());
373: }
374: columnHeader.setView(view);
375: }
376:
377: public Component getCorner(final String key) {
378: if (JScrollPane.LOWER_LEFT_CORNER.equals(key)
379: || JScrollPane.LOWER_LEADING_CORNER.equals(key)) {
380:
381: return lowerLeft;
382: } else if (JScrollPane.LOWER_RIGHT_CORNER.equals(key)
383: || JScrollPane.LOWER_TRAILING_CORNER.equals(key)) {
384:
385: return lowerRight;
386: } else if (JScrollPane.UPPER_LEFT_CORNER.equals(key)
387: || JScrollPane.UPPER_LEADING_CORNER.equals(key)) {
388:
389: return upperLeft;
390: } else if (JScrollPane.UPPER_RIGHT_CORNER.equals(key)
391: || JScrollPane.UPPER_TRAILING_CORNER.equals(key)) {
392:
393: return upperRight;
394: }
395:
396: return null;
397: }
398:
399: public void setCorner(final String key, final Component corner) {
400: if (JScrollPane.LOWER_LEFT_CORNER.equals(key)
401: || JScrollPane.LOWER_LEADING_CORNER.equals(key)
402: && getComponentOrientation().isLeftToRight()
403: || JScrollPane.LOWER_TRAILING_CORNER.equals(key)
404: && !getComponentOrientation().isLeftToRight()) {
405:
406: Component oldValue = lowerLeft;
407: lowerLeft = corner;
408: updateComponent(JScrollPane.LOWER_LEFT_CORNER, oldValue,
409: corner, JScrollPane.LOWER_LEFT_CORNER);
410: } else if (JScrollPane.LOWER_RIGHT_CORNER.equals(key)
411: || JScrollPane.LOWER_LEADING_CORNER.equals(key)
412: && !getComponentOrientation().isLeftToRight()
413: || JScrollPane.LOWER_TRAILING_CORNER.equals(key)
414: && getComponentOrientation().isLeftToRight()) {
415:
416: Component oldValue = lowerRight;
417: lowerRight = corner;
418: updateComponent(JScrollPane.LOWER_RIGHT_CORNER, oldValue,
419: corner, JScrollPane.LOWER_RIGHT_CORNER);
420: } else if (JScrollPane.UPPER_LEFT_CORNER.equals(key)
421: || JScrollPane.UPPER_LEADING_CORNER.equals(key)
422: && getComponentOrientation().isLeftToRight()
423: || JScrollPane.UPPER_TRAILING_CORNER.equals(key)
424: && !getComponentOrientation().isLeftToRight()) {
425:
426: Component oldValue = upperLeft;
427: upperLeft = corner;
428: updateComponent(JScrollPane.UPPER_LEFT_CORNER, oldValue,
429: corner, JScrollPane.UPPER_LEFT_CORNER);
430: } else if (JScrollPane.UPPER_RIGHT_CORNER.equals(key)
431: || JScrollPane.UPPER_LEADING_CORNER.equals(key)
432: && !getComponentOrientation().isLeftToRight()
433: || JScrollPane.UPPER_TRAILING_CORNER.equals(key)
434: && getComponentOrientation().isLeftToRight()) {
435:
436: Component oldValue = upperRight;
437: upperRight = corner;
438: updateComponent(JScrollPane.UPPER_RIGHT_CORNER, oldValue,
439: corner, JScrollPane.UPPER_RIGHT_CORNER);
440: } else {
441: throw new IllegalArgumentException(Messages.getString(
442: "swing.26", key)); //$NON-NLS-1$
443: }
444: }
445:
446: public void setComponentOrientation(final ComponentOrientation co) {
447: verticalScrollBar.setComponentOrientation(co);
448: horizontalScrollBar.setComponentOrientation(co);
449: super .setComponentOrientation(co);
450: }
451:
452: public boolean isWheelScrollingEnabled() {
453: return wheelScrollingEnabled;
454: }
455:
456: public void setWheelScrollingEnabled(
457: final boolean wheelScrollingEnabled) {
458: if (this .wheelScrollingEnabled != wheelScrollingEnabled) {
459: boolean oldValue = this .wheelScrollingEnabled;
460: this .wheelScrollingEnabled = wheelScrollingEnabled;
461: firePropertyChange(WHEEL_SCROLLING_ENABLED_PROPERTY,
462: oldValue, wheelScrollingEnabled);
463: }
464: }
465:
466: public AccessibleContext getAccessibleContext() {
467: if (accessibleContext == null) {
468: accessibleContext = new AccessibleJScrollPane();
469: }
470:
471: return accessibleContext;
472: }
473:
474: protected JViewport createViewport() {
475: return new JViewport();
476: }
477:
478: protected String paramString() {
479: return super .paramString();
480: }
481:
482: private void updateComponent(final String key,
483: final Component oldComponent, final Component newComponent,
484: final String changedPropertyName) {
485: if (oldComponent != null) {
486: remove(oldComponent);
487: }
488: if (newComponent != null) {
489: add(newComponent, key);
490: }
491:
492: firePropertyChange(changedPropertyName, oldComponent,
493: newComponent);
494: }
495: }
|