KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > SectionResizer


1 package jimm.datavision.gui;
2 import jimm.datavision.gui.cmd.SectionResizeCommand;
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.border.BevelBorder JavaDoc;
8
9 /**
10  * A section resizer is a bar that the user can drag to resize a section.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 class SectionResizer extends JPanel implements MouseInputListener {
15
16 public static final int HEIGHT = 6;
17
18 protected static final Color BEVEL_HILIGHT = new Color(0xdd, 0xdd, 0xdd);
19 protected static final Color SPLITTER_COLOR = new Color(0x99, 0x99, 0x99);
20 protected static final Color BEVEL_SHADOW = new Color(0x66, 0x66, 0x66);
21 protected static final Color GHOST_COLOR =
22     new Color(0x99, 0x99, 0xff, 0x80);
23 protected static boolean someoneDragging = false;
24
25 protected SectionWidget target;
26 protected Point start;
27 protected int minY;
28 protected Point mousePos;
29 protected JPanel dragGhost;
30 protected int localY;
31 protected JLayeredPane parentWhileDragging;
32 protected int parentWhileDraggingScreenY;
33 protected SectionResizeCommand sectionResizeCommand;
34
35 /**
36  * Constructor.
37  *
38  * @param target the section widget we will be resizing
39  * @param parentWhileDragging the widget that will be our parent while
40  * we are being dragged
41  */

42 SectionResizer(SectionWidget target, JLayeredPane parentWhileDragging) {
43     super();
44     this.target = target;
45     this.parentWhileDragging = parentWhileDragging;
46     setBackground(SPLITTER_COLOR);
47     setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,
48                           BEVEL_HILIGHT, BEVEL_SHADOW));
49     addMouseListener(this);
50     addMouseMotionListener(this);
51 }
52
53 /**
54  * Handles mouse clicks. Nothing to do.
55  *
56  * @param e a mouse event
57  */

58 public void mouseClicked(MouseEvent e) {}
59
60 /**
61  * Handles mouse entered events by changing the cursor.
62  *
63  * @param e a mouse event
64  */

65 public void mouseEntered(MouseEvent e) {
66     if (!someoneDragging)
67     setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
68 }
69
70 /**
71  * Handles mouse exited events by changing the cursor.
72  *
73  * @param e a mouse event
74  */

75 public void mouseExited(MouseEvent e) {
76     if (!someoneDragging)
77     setCursor(null);
78 }
79
80 /**
81  * Handles mouse moved events. Nothing to do.
82  *
83  * @param e a mouse event
84  */

85 public void mouseMoved(MouseEvent e) {}
86
87 /**
88  * Handles mouse presses by starting to drag.
89  *
90  * @param e a mouse event
91  */

92 public void mousePressed(MouseEvent e) {
93     if (target.designer.isPlacingNewTextField()) {
94     target.designer.rejectNewTextField();
95     return;
96     }
97
98     // Set mousePos to screen position of click
99
mousePos = e.getPoint();
100     localY = mousePos.y;
101     java.awt.Point JavaDoc screenPos = getLocationOnScreen();
102     mousePos.translate(screenPos.x, screenPos.y);
103
104     setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
105     someoneDragging = true;
106
107     start = new Point(mousePos);
108     parentWhileDraggingScreenY =
109     parentWhileDragging.getLocationOnScreen().y;
110     minY = target.getLocationOnScreen().y - parentWhileDraggingScreenY
111     + target.getMinSectionHeight();
112
113     dragGhost = new JPanel();
114     dragGhost.setBackground(GHOST_COLOR);
115     parentWhileDragging.add(dragGhost, JLayeredPane.DRAG_LAYER);
116     dragGhost.setBounds(0, start.y - localY - parentWhileDraggingScreenY,
117             parentWhileDragging.getWidth(), HEIGHT);
118
119     sectionResizeCommand = new SectionResizeCommand(target);
120 }
121
122 /**
123  * Handles mouse drag events by moving this resizer.
124  *
125  * @param e a mouse event
126  */

127 public void mouseDragged(MouseEvent e) {
128     // Set ePos to screen position of click
129
java.awt.Point JavaDoc ePos = e.getPoint();
130     java.awt.Point JavaDoc screenPos = getLocationOnScreen();
131     ePos.translate(screenPos.x, screenPos.y);
132
133     if (mousePos == null || ePos.y == mousePos.y)
134     return; // Nothing to do
135

136     int newY = ePos.y - localY - parentWhileDraggingScreenY;
137     // Make sure we don't get too small.
138
if (newY < minY)
139     newY = minY;
140
141     dragGhost.setLocation(0, newY);
142     mousePos = ePos;
143 }
144
145 /**
146  * Handles mouse released events by repositioning self and asking the
147  * target section widget to resize itself.
148  *
149  * @param e a mouse event
150  */

151 public void mouseReleased(MouseEvent e) {
152     someoneDragging = false;
153
154     // Set ePos to screen position of click
155
java.awt.Point JavaDoc ePos = e.getPoint();
156     java.awt.Point JavaDoc screenPos = getLocationOnScreen();
157     ePos.translate(screenPos.x, screenPos.y);
158
159     parentWhileDragging.remove(dragGhost);
160
161     // Tell the target to resize itself. Do not call SectionWidget.growBy()
162
// directly. See SectionWidget.resizedBy().
163
target.resizeBy(ePos.y - start.y - localY, sectionResizeCommand);
164
165     // When dragging bottom-most section, the ghost is left behind in the
166
// remaining gray section of the window. Force a repaint (sigh).
167
parentWhileDragging.repaint();
168 }
169
170 }
171
Popular Tags