252 if (slider.getOrientation() == JSlider.HORIZONTAL) {
253 int valueHeight = 0;
254 if (paintValue) {
255 SynthContext context = getContext(slider);
256 valueHeight = context.getStyle().getGraphicsUtils(context).
257 getMaximumCharHeight(context);
258 context.dispose();
259 }
260 int tickHeight = 0;
261 if (slider.getPaintTicks()) {
262 tickHeight = getTickLength();
263 }
264 int labelHeight = getHeightOfTallestLabel();
265 int contentHeight = valueHeight + trackHeight +
266 trackInsets.top + trackInsets.bottom +
267 tickHeight + labelHeight + 4;
268 int centerY = height / 2 - contentHeight / 2;
269 centerY += valueHeight + 2;
270 centerY += trackHeight + trackInsets.top + trackInsets.bottom;
271 centerY += tickHeight + 2;
272 JComponent label = (JComponent) slider.getLabelTable().elements().nextElement();
273 Dimension pref = label.getPreferredSize();
274 return centerY + label.getBaseline(pref.width, pref.height);
275 }
276 else { // VERTICAL
277 Integer value = slider.getInverted() ? getLowestValue() :
278 getHighestValue();
279 if (value != null) {
280 int valueY = insetCache.top;
281 int valueHeight = 0;
282 if (paintValue) {
283 SynthContext context = getContext(slider);
284 valueHeight = context.getStyle().getGraphicsUtils(
285 context).getMaximumCharHeight(context);
286 context.dispose();
287 }
288 int contentHeight = height - insetCache.top -
289 insetCache.bottom;
290 int trackY = valueY + valueHeight;
291 int trackHeight = contentHeight - valueHeight;
292 int yPosition = yPositionForValue(value.intValue(), trackY,
293 trackHeight);
294 JComponent label = (JComponent) slider.getLabelTable().get(value);
295 Dimension pref = label.getPreferredSize();
296 return yPosition - pref.height / 2 +
297 label.getBaseline(pref.width, pref.height);
298 }
299 }
300 }
301 return -1;
302 }
303
304 /**
305 * {@inheritDoc}
306 */
307 @Override
308 public Dimension getPreferredSize(JComponent c) {
309 recalculateIfInsetsChanged();
310 Dimension d = new Dimension(contentRect.width, contentRect.height);
311 if (slider.getOrientation() == JSlider.VERTICAL) {
312 d.height = 200;
313 } else {
314 d.width = 200;
375
376 labelRect.height = 0;
377 if (slider.getPaintLabels()) {
378 labelRect.height = getHeightOfTallestLabel();
379 }
380
381 contentRect.height = valueRect.height + trackRect.height
382 + trackInsets.top + trackInsets.bottom
383 + tickRect.height + labelRect.height + 4;
384 contentRect.width = slider.getWidth() - insetCache.left
385 - insetCache.right;
386
387 // Check if any of the labels will paint out of bounds.
388 int pad = 0;
389 if (slider.getPaintLabels()) {
390 // Calculate the track rectangle. It is necessary for
391 // xPositionForValue to return correct values.
392 trackRect.x = insetCache.left;
393 trackRect.width = contentRect.width;
394
395 Dictionary dictionary = slider.getLabelTable();
396 if (dictionary != null) {
397 int minValue = slider.getMinimum();
398 int maxValue = slider.getMaximum();
399
400 // Iterate through the keys in the dictionary and find the
401 // first and last labels indices that fall within the
402 // slider range.
403 int firstLblIdx = Integer.MAX_VALUE;
404 int lastLblIdx = Integer.MIN_VALUE;
405 for (Enumeration keys = dictionary.keys();
406 keys.hasMoreElements(); ) {
407 int keyInt = ((Integer)keys.nextElement()).intValue();
408 if (keyInt >= minValue && keyInt < firstLblIdx) {
409 firstLblIdx = keyInt;
410 }
411 if (keyInt <= maxValue && keyInt > lastLblIdx) {
412 lastLblIdx = keyInt;
413 }
414 }
415 // Calculate the pad necessary for the labels at the first
416 // and last visible indices.
417 pad = getPadForLabel(firstLblIdx);
418 pad = Math.max(pad, getPadForLabel(lastLblIdx));
419 }
420 }
421 // Calculate the painting rectangles for each of the different
422 // slider areas.
423 valueRect.x = trackRect.x = tickRect.x = labelRect.x =
424 (insetCache.left + pad);
425 valueRect.width = trackRect.width = tickRect.width =
426 labelRect.width = (contentRect.width - (pad * 2));
427
500 labelRect.x = startX;
501
502 startX += labelRect.width + 2;
503 tickRect.x = startX;
504 trackRect.x = startX + tickRect.width + trackInsets.left;
505 }
506 }
507 context.dispose();
508 lastSize = slider.getSize();
509 }
510
511 /**
512 * Calculates the pad for the label at the specified index.
513 *
514 * @param i index of the label to calculate pad for.
515 * @return padding required to keep label visible.
516 */
517 private int getPadForLabel(int i) {
518 int pad = 0;
519
520 JComponent c = (JComponent) slider.getLabelTable().get(i);
521 if (c != null) {
522 int centerX = xPositionForValue(i);
523 int cHalfWidth = c.getPreferredSize().width / 2;
524 if (centerX - cHalfWidth < insetCache.left) {
525 pad = Math.max(pad, insetCache.left - (centerX - cHalfWidth));
526 }
527
528 if (centerX + cHalfWidth > slider.getWidth() - insetCache.right) {
529 pad = Math.max(pad, (centerX + cHalfWidth) -
530 (slider.getWidth() - insetCache.right));
531 }
532 }
533 return pad;
534 }
535
536 /**
537 * {@inheritDoc}
538 */
539 @Override
540 protected void calculateThumbLocation() {
|
252 if (slider.getOrientation() == JSlider.HORIZONTAL) {
253 int valueHeight = 0;
254 if (paintValue) {
255 SynthContext context = getContext(slider);
256 valueHeight = context.getStyle().getGraphicsUtils(context).
257 getMaximumCharHeight(context);
258 context.dispose();
259 }
260 int tickHeight = 0;
261 if (slider.getPaintTicks()) {
262 tickHeight = getTickLength();
263 }
264 int labelHeight = getHeightOfTallestLabel();
265 int contentHeight = valueHeight + trackHeight +
266 trackInsets.top + trackInsets.bottom +
267 tickHeight + labelHeight + 4;
268 int centerY = height / 2 - contentHeight / 2;
269 centerY += valueHeight + 2;
270 centerY += trackHeight + trackInsets.top + trackInsets.bottom;
271 centerY += tickHeight + 2;
272 JComponent label = slider.getLabelTable().elements().nextElement();
273 Dimension pref = label.getPreferredSize();
274 return centerY + label.getBaseline(pref.width, pref.height);
275 }
276 else { // VERTICAL
277 Integer value = slider.getInverted() ? getLowestValue() :
278 getHighestValue();
279 if (value != null) {
280 int valueY = insetCache.top;
281 int valueHeight = 0;
282 if (paintValue) {
283 SynthContext context = getContext(slider);
284 valueHeight = context.getStyle().getGraphicsUtils(
285 context).getMaximumCharHeight(context);
286 context.dispose();
287 }
288 int contentHeight = height - insetCache.top -
289 insetCache.bottom;
290 int trackY = valueY + valueHeight;
291 int trackHeight = contentHeight - valueHeight;
292 int yPosition = yPositionForValue(value.intValue(), trackY,
293 trackHeight);
294 JComponent label = slider.getLabelTable().get(value);
295 Dimension pref = label.getPreferredSize();
296 return yPosition - pref.height / 2 +
297 label.getBaseline(pref.width, pref.height);
298 }
299 }
300 }
301 return -1;
302 }
303
304 /**
305 * {@inheritDoc}
306 */
307 @Override
308 public Dimension getPreferredSize(JComponent c) {
309 recalculateIfInsetsChanged();
310 Dimension d = new Dimension(contentRect.width, contentRect.height);
311 if (slider.getOrientation() == JSlider.VERTICAL) {
312 d.height = 200;
313 } else {
314 d.width = 200;
375
376 labelRect.height = 0;
377 if (slider.getPaintLabels()) {
378 labelRect.height = getHeightOfTallestLabel();
379 }
380
381 contentRect.height = valueRect.height + trackRect.height
382 + trackInsets.top + trackInsets.bottom
383 + tickRect.height + labelRect.height + 4;
384 contentRect.width = slider.getWidth() - insetCache.left
385 - insetCache.right;
386
387 // Check if any of the labels will paint out of bounds.
388 int pad = 0;
389 if (slider.getPaintLabels()) {
390 // Calculate the track rectangle. It is necessary for
391 // xPositionForValue to return correct values.
392 trackRect.x = insetCache.left;
393 trackRect.width = contentRect.width;
394
395 Dictionary<Integer, JComponent> dictionary = slider.getLabelTable();
396 if (dictionary != null) {
397 int minValue = slider.getMinimum();
398 int maxValue = slider.getMaximum();
399
400 // Iterate through the keys in the dictionary and find the
401 // first and last labels indices that fall within the
402 // slider range.
403 int firstLblIdx = Integer.MAX_VALUE;
404 int lastLblIdx = Integer.MIN_VALUE;
405 for (Enumeration<Integer> keys = dictionary.keys();
406 keys.hasMoreElements(); ) {
407 int keyInt = keys.nextElement().intValue();
408 if (keyInt >= minValue && keyInt < firstLblIdx) {
409 firstLblIdx = keyInt;
410 }
411 if (keyInt <= maxValue && keyInt > lastLblIdx) {
412 lastLblIdx = keyInt;
413 }
414 }
415 // Calculate the pad necessary for the labels at the first
416 // and last visible indices.
417 pad = getPadForLabel(firstLblIdx);
418 pad = Math.max(pad, getPadForLabel(lastLblIdx));
419 }
420 }
421 // Calculate the painting rectangles for each of the different
422 // slider areas.
423 valueRect.x = trackRect.x = tickRect.x = labelRect.x =
424 (insetCache.left + pad);
425 valueRect.width = trackRect.width = tickRect.width =
426 labelRect.width = (contentRect.width - (pad * 2));
427
500 labelRect.x = startX;
501
502 startX += labelRect.width + 2;
503 tickRect.x = startX;
504 trackRect.x = startX + tickRect.width + trackInsets.left;
505 }
506 }
507 context.dispose();
508 lastSize = slider.getSize();
509 }
510
511 /**
512 * Calculates the pad for the label at the specified index.
513 *
514 * @param i index of the label to calculate pad for.
515 * @return padding required to keep label visible.
516 */
517 private int getPadForLabel(int i) {
518 int pad = 0;
519
520 JComponent c = slider.getLabelTable().get(i);
521 if (c != null) {
522 int centerX = xPositionForValue(i);
523 int cHalfWidth = c.getPreferredSize().width / 2;
524 if (centerX - cHalfWidth < insetCache.left) {
525 pad = Math.max(pad, insetCache.left - (centerX - cHalfWidth));
526 }
527
528 if (centerX + cHalfWidth > slider.getWidth() - insetCache.right) {
529 pad = Math.max(pad, (centerX + cHalfWidth) -
530 (slider.getWidth() - insetCache.right));
531 }
532 }
533 return pad;
534 }
535
536 /**
537 * {@inheritDoc}
538 */
539 @Override
540 protected void calculateThumbLocation() {
|