001: /*
002: * @(#)BasicRangeSliderUI.java 11/22/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.plaf.basic;
008:
009: import com.jidesoft.plaf.UIDefaultsLookup;
010: import com.jidesoft.swing.RangeSlider;
011:
012: import javax.swing.*;
013: import javax.swing.plaf.ComponentUI;
014: import javax.swing.plaf.basic.BasicSliderUI;
015: import java.awt.*;
016: import java.awt.event.MouseEvent;
017:
018: /**
019: * JideButtonUI implementation
020: */
021: public class BasicRangeSliderUI extends BasicSliderUI {
022: protected Icon _lowerIcon;
023: protected Icon _upperIcon;
024: protected Icon _middleIcon;
025: protected Icon _lowerIconV;
026: protected Icon _upperIconV;
027: protected Icon _middleIconV;
028:
029: public BasicRangeSliderUI(JSlider slider) {
030: super (slider);
031: }
032:
033: // ********************************
034: // Create PLAF
035: // ********************************
036: public static ComponentUI createUI(JComponent slider) {
037: return new BasicRangeSliderUI((JSlider) slider);
038: }
039:
040: @Override
041: protected void installDefaults(JSlider slider) {
042: super .installDefaults(slider);
043: resetAllIcons();
044: }
045:
046: protected void resetAllIcons() {
047: _lowerIcon = UIDefaultsLookup.getIcon("RangeSlider.lowerIcon");
048: _upperIcon = UIDefaultsLookup.getIcon("RangeSlider.upperIcon");
049: _middleIcon = UIDefaultsLookup
050: .getIcon("RangeSlider.middleIcon");
051: _lowerIconV = UIDefaultsLookup
052: .getIcon("RangeSlider.lowerVIcon");
053: _upperIconV = UIDefaultsLookup
054: .getIcon("RangeSlider.upperVIcon");
055: _middleIconV = UIDefaultsLookup
056: .getIcon("RangeSlider.middleVIcon");
057: }
058:
059: @Override
060: public void uninstallUI(JComponent c) {
061: super .uninstallUI(c);
062: _lowerIcon = null;
063: _upperIcon = null;
064: _middleIcon = null;
065: _lowerIconV = null;
066: _upperIconV = null;
067: _middleIconV = null;
068: }
069:
070: @Override
071: protected void calculateTrackRect() {
072: super .calculateTrackRect();
073: if (slider.getOrientation() == JSlider.VERTICAL) {
074: trackRect.x += 4;
075: } else {
076: trackRect.y += 4;
077: }
078: }
079:
080: @Override
081: protected TrackListener createTrackListener(JSlider slider) {
082: return new RangeTrackListener();
083: }
084:
085: protected class RangeTrackListener extends TrackListener {
086: int handle;
087: int handleOffset;
088: int mouseStartLocation;
089:
090: public RangeTrackListener() {
091:
092: }
093:
094: /**
095: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
096: */
097: @Override
098: public void mousePressed(MouseEvent e) {
099: if (!slider.isEnabled()) {
100: return;
101: }
102:
103: handle = getMouseHandle(e.getX(), e.getY());
104:
105: handleOffset = (slider.getOrientation() == JSlider.VERTICAL) ? e
106: .getY()
107: - yPositionForValue(((RangeSlider) slider)
108: .getLowValue())
109: : e.getX()
110: - xPositionForValue(((RangeSlider) slider)
111: .getLowValue());
112:
113: mouseStartLocation = (slider.getOrientation() == JSlider.VERTICAL) ? e
114: .getY()
115: : e.getX();
116:
117: slider.getModel().setValueIsAdjusting(true);
118: }
119:
120: /**
121: * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
122: */
123: @Override
124: public void mouseDragged(MouseEvent e) {
125: if (!slider.isEnabled()) {
126: return;
127: }
128:
129: int newLocation = (slider.getOrientation() == JSlider.VERTICAL) ? e
130: .getY()
131: : e.getX();
132:
133: int newValue = (slider.getOrientation() == JSlider.VERTICAL) ? valueForYPosition(newLocation)
134: : valueForXPosition(newLocation);
135:
136: if (newValue < slider.getModel().getMinimum()) {
137: newValue = slider.getModel().getMinimum();
138: }
139:
140: if (newValue > slider.getModel().getMaximum()) {
141: newValue = slider.getModel().getMaximum();
142: }
143:
144: if (handle == (MOUSE_HANDLE_MIN | MOUSE_HANDLE_MAX)) {
145: if ((newLocation - mouseStartLocation) > 2) {
146: handle = MOUSE_HANDLE_MAX;
147: } else if ((newLocation - mouseStartLocation) < -2) {
148: handle = MOUSE_HANDLE_MIN;
149: } else {
150: return;
151: }
152: }
153:
154: RangeSlider rangeSlider = (RangeSlider) slider;
155: switch (handle) {
156: case MOUSE_HANDLE_MIN:
157: rangeSlider.setLowValue(Math.min(newValue, rangeSlider
158: .getHighValue()));
159: break;
160: case MOUSE_HANDLE_MAX:
161: rangeSlider.setHighValue(Math.max(rangeSlider
162: .getLowValue(), newValue));
163: break;
164: case MOUSE_HANDLE_MIDDLE:
165: int delta = (slider.getOrientation() == JSlider.VERTICAL) ? valueForYPosition(newLocation
166: - handleOffset)
167: - rangeSlider.getLowValue()
168: : valueForXPosition(newLocation - handleOffset)
169: - rangeSlider.getLowValue();
170: if ((delta < 0)
171: && ((rangeSlider.getLowValue() + delta) < rangeSlider
172: .getMinimum())) {
173: delta = rangeSlider.getMinimum()
174: - rangeSlider.getLowValue();
175: }
176:
177: if ((delta > 0)
178: && ((rangeSlider.getHighValue() + delta) > rangeSlider
179: .getMaximum())) {
180: delta = rangeSlider.getMaximum()
181: - rangeSlider.getHighValue();
182: }
183:
184: if (delta != 0) {
185: offset(delta);
186: }
187: break;
188: }
189: }
190:
191: /**
192: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
193: */
194: @Override
195: public void mouseReleased(MouseEvent e) {
196: slider.getModel().setValueIsAdjusting(false);
197: }
198:
199: private void setCursor(int c) {
200: Cursor cursor = Cursor.getPredefinedCursor(c);
201:
202: if (slider.getCursor() != cursor) {
203: slider.setCursor(cursor);
204: }
205: }
206:
207: /**
208: * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
209: */
210: @Override
211: public void mouseMoved(MouseEvent e) {
212: if (!slider.isEnabled()) {
213: return;
214: }
215:
216: switch (getMouseHandle(e.getX(), e.getY())) {
217: case MOUSE_HANDLE_MIN:
218: setMouseRollover(MOUSE_HANDLE_MIN);
219: setCursor((slider.getOrientation() == JSlider.VERTICAL) ? Cursor.N_RESIZE_CURSOR
220: : Cursor.W_RESIZE_CURSOR);
221: break;
222: case MOUSE_HANDLE_MAX:
223: setMouseRollover(MOUSE_HANDLE_MAX);
224: setCursor((slider.getOrientation() == JSlider.VERTICAL) ? Cursor.S_RESIZE_CURSOR
225: : Cursor.E_RESIZE_CURSOR);
226: break;
227: case MOUSE_HANDLE_MIDDLE:
228: setMouseRollover(MOUSE_HANDLE_MIDDLE);
229: setCursor(Cursor.MOVE_CURSOR);
230: break;
231: case MOUSE_HANDLE_NONE:
232: setMouseRollover(MOUSE_HANDLE_NONE);
233: setCursor(Cursor.DEFAULT_CURSOR);
234: break;
235: }
236: }
237:
238: /**
239: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
240: */
241: @Override
242: public void mouseClicked(MouseEvent e) {
243: if (e.getClickCount() == 2) {
244: slider.getModel().setValue(
245: slider.getModel().getMinimum());
246: slider.getModel().setExtent(
247: slider.getModel().getMaximum()
248: - slider.getModel().getMinimum());
249: slider.repaint();
250: }
251: }
252:
253: /**
254: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
255: */
256: @Override
257: public void mouseEntered(MouseEvent e) {
258: }
259:
260: /**
261: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
262: */
263: @Override
264: public void mouseExited(MouseEvent e) {
265: setCursor(Cursor.DEFAULT_CURSOR);
266: }
267: }
268:
269: private void offset(int delta) {
270: slider.getModel().setValue(
271: ((RangeSlider) slider).getLowValue() + delta);
272: }
273:
274: protected static final int MOUSE_HANDLE_NONE = 0;
275:
276: protected static final int MOUSE_HANDLE_MIN = 1;
277:
278: protected static final int MOUSE_HANDLE_MAX = 2;
279:
280: protected static final int MOUSE_HANDLE_MIDDLE = 4;
281:
282: protected int getMouseHandle(int x, int y) {
283: Rectangle rect = trackRect;
284:
285: if (slider.getOrientation() == JSlider.VERTICAL) {
286: int minY = yPositionForValue(((RangeSlider) slider)
287: .getLowValue());
288: int maxY = yPositionForValue(((RangeSlider) slider)
289: .getHighValue());
290:
291: Rectangle minRect = new Rectangle(rect.x + rect.width / 2,
292: minY - _lowerIconV.getIconHeight() / 2, _lowerIcon
293: .getIconWidth(), _lowerIconV
294: .getIconHeight());
295: if (minRect.contains(x, y)) {
296: return MOUSE_HANDLE_MIN;
297: }
298: Rectangle maxRect = new Rectangle(rect.x, maxY
299: - _upperIconV.getIconHeight() / 2, _upperIconV
300: .getIconWidth(), _upperIconV.getIconHeight());
301: if (maxRect.contains(x, y)) {
302: return MOUSE_HANDLE_MAX;
303: }
304:
305: Rectangle midRect = new Rectangle(rect.x
306: - _middleIcon.getIconWidth(), maxY
307: - _middleIconV.getIconHeight() / 2, _middleIconV
308: .getIconWidth(), _middleIconV.getIconHeight());
309: if (midRect.contains(x, y)) {
310: return MOUSE_HANDLE_MIDDLE;
311: }
312: return MOUSE_HANDLE_NONE;
313: } else {
314: int minX = xPositionForValue(((RangeSlider) slider)
315: .getLowValue());
316: int maxX = xPositionForValue(((RangeSlider) slider)
317: .getHighValue());
318:
319: Rectangle minRect = new Rectangle(minX
320: - _lowerIcon.getIconWidth() / 2, rect.y
321: + rect.height / 2, _lowerIcon.getIconWidth(),
322: _lowerIcon.getIconHeight());
323: if (minRect.contains(x, y)) {
324: return MOUSE_HANDLE_MIN;
325: }
326: Rectangle maxRect = new Rectangle(maxX
327: - _upperIcon.getIconWidth() / 2, rect.y, _upperIcon
328: .getIconWidth(), _upperIcon.getIconHeight());
329: if (maxRect.contains(x, y)) {
330: return MOUSE_HANDLE_MAX;
331: }
332:
333: Rectangle midRect = new Rectangle(maxX
334: - _middleIcon.getIconWidth() / 2, rect.y
335: - _middleIcon.getIconHeight(), _middleIcon
336: .getIconWidth(), _middleIcon.getIconHeight());
337: if (midRect.contains(x, y)) {
338: return MOUSE_HANDLE_MIDDLE;
339: }
340: return MOUSE_HANDLE_NONE;
341: }
342: }
343:
344: @Override
345: public void paintThumb(Graphics g) {
346: Rectangle rect = trackRect;
347:
348: RangeSlider rangeSlider = (RangeSlider) slider;
349:
350: if (slider.getOrientation() == JSlider.VERTICAL) {
351: int minY = yPositionForValue(rangeSlider.getLowValue());
352: int maxY = yPositionForValue(rangeSlider.getHighValue());
353:
354: _lowerIconV.paintIcon(slider, g, rect.x + rect.width / 2,
355: minY - _lowerIconV.getIconHeight() / 2);
356: _upperIconV.paintIcon(slider, g, rect.x, maxY
357: - _upperIconV.getIconHeight() / 2);
358: _middleIconV.paintIcon(slider, g, rect.x
359: - _middleIconV.getIconWidth(), maxY
360: - _middleIconV.getIconHeight() / 2);
361: } else {
362: int minX = xPositionForValue(rangeSlider.getLowValue());
363: int maxX = xPositionForValue(rangeSlider.getHighValue());
364:
365: _lowerIcon.paintIcon(slider, g, minX
366: - _lowerIcon.getIconWidth() / 2, rect.y
367: + rect.height / 2);
368: _upperIcon.paintIcon(slider, g, maxX
369: - _upperIcon.getIconWidth() / 2, rect.y);
370: _middleIcon.paintIcon(slider, g, maxX
371: - _middleIcon.getIconWidth() / 2, rect.y
372: - _middleIcon.getIconHeight());
373: }
374: }
375:
376: protected void setMouseRollover(int handle) {
377: }
378: }
|