001: package org.swingml.tablebrowser.rowheader;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.io.*;
006:
007: import javax.swing.*;
008: import javax.swing.event.*;
009:
010: public class RowHeaderResizer extends MouseInputAdapter implements
011: Serializable, ContainerListener {
012:
013: private static final int PIXELS = 10;
014: private static final Cursor RESIZE_CURSOR = Cursor
015: .getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
016: private boolean active;
017: private Component corner;
018: private boolean enabled;
019: private int minWidth, maxWidth;
020: private Cursor oldCursor;
021: private JScrollPane pane;
022: private JTable rowHeader;
023: private Dimension size;
024: private int startX, startWidth;
025: private JTable view;
026: private JViewport viewport;
027:
028: public RowHeaderResizer(JScrollPane pane) {
029: this .pane = pane;
030: this .pane.addContainerListener(this );
031: }
032:
033: protected void addListeners() {
034: if (corner != null) {
035: corner.addMouseListener(this );
036: corner.addMouseMotionListener(this );
037: }
038: }
039:
040: public void componentAdded(ContainerEvent e) {
041: componentRemoved(e);
042: }
043:
044: public void componentRemoved(ContainerEvent e) {
045: if (enabled)
046: removeListeners();
047:
048: lookupComponents();
049:
050: if (enabled)
051: addListeners();
052: }
053:
054: protected void lookupComponents() {
055: this .view = (JTable) pane.getViewport().getView();
056: this .viewport = pane.getRowHeader();
057: if (viewport == null)
058: this .rowHeader = null;
059: else
060: this .rowHeader = (JTable) viewport.getView();
061: this .corner = pane.getCorner(JScrollPane.UPPER_LEFT_CORNER);
062: }
063:
064: public void mouseDragged(MouseEvent e) {
065: if (!active)
066: return;
067:
068: size = viewport.getPreferredSize();
069: // int newX = e.getX();
070: size.width = startWidth + e.getX() - startX;
071:
072: if (size.width < minWidth)
073: size.width = minWidth;
074: else if (size.width > maxWidth)
075: size.width = maxWidth;
076:
077: // This isn't too clean, it assumes the width bubbles up to
078: // viewport.getPreferredSize().width without changes.
079: rowHeader.getColumnModel().getColumn(0).setPreferredWidth(
080: size.width);
081:
082: view.sizeColumnsToFit(-1);
083:
084: pane.revalidate();
085: }
086:
087: public void mouseEntered(MouseEvent e) {
088: mouseMoved(e);
089: }
090:
091: public void mouseExited(MouseEvent e) {
092: if (oldCursor != null) {
093: corner.setCursor(oldCursor);
094: oldCursor = null;
095: }
096: }
097:
098: public void mouseMoved(MouseEvent e) {
099: if (corner.getWidth() - e.getX() <= PIXELS) {
100: if (oldCursor == null) {
101: oldCursor = corner.getCursor();
102: corner.setCursor(RESIZE_CURSOR);
103: }
104: } else if (oldCursor != null) {
105: corner.setCursor(oldCursor);
106: oldCursor = null;
107: }
108: }
109:
110: public void mousePressed(MouseEvent e) {
111: startX = e.getX();
112: startWidth = rowHeader.getWidth();
113:
114: if (startWidth - startX > PIXELS)
115: return;
116:
117: active = true;
118:
119: if (oldCursor == null) {
120: oldCursor = corner.getCursor();
121: corner.setCursor(RESIZE_CURSOR);
122: }
123:
124: minWidth = rowHeader.getMinimumSize().width;
125: maxWidth = rowHeader.getMaximumSize().width;
126: }
127:
128: public void mouseReleased(MouseEvent e) {
129: active = false;
130: }
131:
132: protected void removeListeners() {
133: if (corner != null) {
134: corner.removeMouseListener(this );
135: corner.removeMouseMotionListener(this );
136: }
137: }
138:
139: public void setEnabled(boolean what) {
140: if (enabled == what)
141: return;
142:
143: enabled = what;
144:
145: if (enabled)
146: addListeners();
147: else
148: removeListeners();
149: }
150: }
|