001: package org.swingml.tablebrowser.rowheader;
002:
003: import java.awt.*;
004: import java.beans.*;
005: import java.io.*;
006:
007: import javax.swing.*;
008: import javax.swing.event.*;
009:
010: public class RowHeaderScrollPaneAdjuster implements
011: PropertyChangeListener, Serializable {
012:
013: private static class Adjuster implements ChangeListener, Runnable {
014:
015: public static final int X = 1, Y = 2;
016:
017: private int type;
018: private JViewport viewport, header;
019:
020: public Adjuster(JViewport viewport, JViewport header, int type) {
021: this .viewport = viewport;
022: this .header = header;
023: this .type = type;
024:
025: if (header != null)
026: header.addChangeListener(this );
027: }
028:
029: public void dispose() {
030: if (header != null)
031: header.removeChangeListener(this );
032:
033: viewport = header = null;
034: }
035:
036: public void run() {
037: if (viewport == null || header == null)
038: return;
039:
040: Point v = viewport.getViewPosition(), h = header
041: .getViewPosition();
042:
043: if (type == X) {
044: if (v.x != h.x)
045: viewport.setViewPosition(new Point(h.x, v.y));
046: } else {
047: if (v.y != h.y)
048: viewport.setViewPosition(new Point(v.x, h.y));
049: }
050: }
051:
052: public void setHeader(JViewport newHeader) {
053: if (header != null)
054: header.removeChangeListener(this );
055:
056: header = newHeader;
057:
058: if (header != null)
059: header.addChangeListener(this );
060: }
061:
062: public void setViewport(JViewport newViewport) {
063: viewport = newViewport;
064: }
065:
066: public void stateChanged(ChangeEvent e) {
067: if (viewport == null || header == null)
068: return;
069:
070: if (type == X) {
071: if (viewport.getViewPosition().x != header
072: .getViewPosition().x)
073: SwingUtilities.invokeLater(this );
074: } else {
075: if (viewport.getViewPosition().y != header
076: .getViewPosition().y)
077: SwingUtilities.invokeLater(this );
078: }
079: }
080: }
081:
082: private JScrollPane pane;
083:
084: private transient Adjuster x, y;
085:
086: public RowHeaderScrollPaneAdjuster(JScrollPane pane) {
087: this .pane = pane;
088:
089: this .x = new Adjuster(pane.getViewport(), pane
090: .getColumnHeader(), Adjuster.X);
091: this .y = new Adjuster(pane.getViewport(), pane.getRowHeader(),
092: Adjuster.Y);
093:
094: pane.addPropertyChangeListener(this );
095: }
096:
097: public void dispose() {
098: x.dispose();
099: y.dispose();
100:
101: pane.removePropertyChangeListener(this );
102: pane = null;
103: }
104:
105: public void propertyChange(PropertyChangeEvent e) {
106: String name = e.getPropertyName();
107:
108: if (name.equals("viewport")) {
109: x.setViewport((JViewport) e.getNewValue());
110: y.setViewport((JViewport) e.getNewValue());
111: } else if (name.equals("rowHeader")) {
112: y.setHeader((JViewport) e.getNewValue());
113: } else if (name.equals("columnHeader")) {
114: x.setHeader((JViewport) e.getNewValue());
115: }
116: }
117:
118: private void readObject(ObjectInputStream in) throws IOException,
119: ClassNotFoundException {
120: in.defaultReadObject();
121:
122: x = new Adjuster(pane.getViewport(), pane.getColumnHeader(),
123: Adjuster.X);
124: y = new Adjuster(pane.getViewport(), pane.getRowHeader(),
125: Adjuster.Y);
126: }
127: }
|