001: package jimm.datavision.gui;
002:
003: import jimm.datavision.gui.cmd.SectionResizeCommand;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.*;
007: import javax.swing.event.*;
008: import javax.swing.border.BevelBorder;
009:
010: /**
011: * A section resizer is a bar that the user can drag to resize a section.
012: *
013: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
014: */
015: class SectionResizer extends JPanel implements MouseInputListener {
016:
017: public static final int HEIGHT = 6;
018:
019: protected static final Color BEVEL_HILIGHT = new Color(0xdd, 0xdd,
020: 0xdd);
021: protected static final Color SPLITTER_COLOR = new Color(0x99, 0x99,
022: 0x99);
023: protected static final Color BEVEL_SHADOW = new Color(0x66, 0x66,
024: 0x66);
025: protected static final Color GHOST_COLOR = new Color(0x99, 0x99,
026: 0xff, 0x80);
027: protected static boolean someoneDragging = false;
028:
029: protected SectionWidget target;
030: protected Point start;
031: protected int minY;
032: protected Point mousePos;
033: protected JPanel dragGhost;
034: protected int localY;
035: protected JLayeredPane parentWhileDragging;
036: protected int parentWhileDraggingScreenY;
037: protected SectionResizeCommand sectionResizeCommand;
038:
039: /**
040: * Constructor.
041: *
042: * @param target the section widget we will be resizing
043: * @param parentWhileDragging the widget that will be our parent while
044: * we are being dragged
045: */
046: SectionResizer(SectionWidget target,
047: JLayeredPane parentWhileDragging) {
048: super ();
049: this .target = target;
050: this .parentWhileDragging = parentWhileDragging;
051: setBackground(SPLITTER_COLOR);
052: setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,
053: BEVEL_HILIGHT, BEVEL_SHADOW));
054: addMouseListener(this );
055: addMouseMotionListener(this );
056: }
057:
058: /**
059: * Handles mouse clicks. Nothing to do.
060: *
061: * @param e a mouse event
062: */
063: public void mouseClicked(MouseEvent e) {
064: }
065:
066: /**
067: * Handles mouse entered events by changing the cursor.
068: *
069: * @param e a mouse event
070: */
071: public void mouseEntered(MouseEvent e) {
072: if (!someoneDragging)
073: setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
074: }
075:
076: /**
077: * Handles mouse exited events by changing the cursor.
078: *
079: * @param e a mouse event
080: */
081: public void mouseExited(MouseEvent e) {
082: if (!someoneDragging)
083: setCursor(null);
084: }
085:
086: /**
087: * Handles mouse moved events. Nothing to do.
088: *
089: * @param e a mouse event
090: */
091: public void mouseMoved(MouseEvent e) {
092: }
093:
094: /**
095: * Handles mouse presses by starting to drag.
096: *
097: * @param e a mouse event
098: */
099: public void mousePressed(MouseEvent e) {
100: if (target.designer.isPlacingNewTextField()) {
101: target.designer.rejectNewTextField();
102: return;
103: }
104:
105: // Set mousePos to screen position of click
106: mousePos = e.getPoint();
107: localY = mousePos.y;
108: java.awt.Point screenPos = getLocationOnScreen();
109: mousePos.translate(screenPos.x, screenPos.y);
110:
111: setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
112: someoneDragging = true;
113:
114: start = new Point(mousePos);
115: parentWhileDraggingScreenY = parentWhileDragging
116: .getLocationOnScreen().y;
117: minY = target.getLocationOnScreen().y
118: - parentWhileDraggingScreenY
119: + target.getMinSectionHeight();
120:
121: dragGhost = new JPanel();
122: dragGhost.setBackground(GHOST_COLOR);
123: parentWhileDragging.add(dragGhost, JLayeredPane.DRAG_LAYER);
124: dragGhost.setBounds(0, start.y - localY
125: - parentWhileDraggingScreenY, parentWhileDragging
126: .getWidth(), HEIGHT);
127:
128: sectionResizeCommand = new SectionResizeCommand(target);
129: }
130:
131: /**
132: * Handles mouse drag events by moving this resizer.
133: *
134: * @param e a mouse event
135: */
136: public void mouseDragged(MouseEvent e) {
137: // Set ePos to screen position of click
138: java.awt.Point ePos = e.getPoint();
139: java.awt.Point screenPos = getLocationOnScreen();
140: ePos.translate(screenPos.x, screenPos.y);
141:
142: if (mousePos == null || ePos.y == mousePos.y)
143: return; // Nothing to do
144:
145: int newY = ePos.y - localY - parentWhileDraggingScreenY;
146: // Make sure we don't get too small.
147: if (newY < minY)
148: newY = minY;
149:
150: dragGhost.setLocation(0, newY);
151: mousePos = ePos;
152: }
153:
154: /**
155: * Handles mouse released events by repositioning self and asking the
156: * target section widget to resize itself.
157: *
158: * @param e a mouse event
159: */
160: public void mouseReleased(MouseEvent e) {
161: someoneDragging = false;
162:
163: // Set ePos to screen position of click
164: java.awt.Point ePos = e.getPoint();
165: java.awt.Point screenPos = getLocationOnScreen();
166: ePos.translate(screenPos.x, screenPos.y);
167:
168: parentWhileDragging.remove(dragGhost);
169:
170: // Tell the target to resize itself. Do not call SectionWidget.growBy()
171: // directly. See SectionWidget.resizedBy().
172: target
173: .resizeBy(ePos.y - start.y - localY,
174: sectionResizeCommand);
175:
176: // When dragging bottom-most section, the ghost is left behind in the
177: // remaining gray section of the window. Force a repaint (sigh).
178: parentWhileDragging.repaint();
179: }
180:
181: }
|