package com.rle.monitor; /* * RLE Surface Crack Detector Graphing Software * * Copyright (C) 2002 RLE Technologies * For more information visit http://www.rletech.com/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ /** * @author Andy Goth * @author Eric Peterson * @author Matt Lane * @version 0.1 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.*; import javax.swing.plaf.metal.*; /** UI for slider with stop values and block incrementing. */ public class MySliderUI extends javax.swing.plaf.metal.MetalSliderUI { public MySliderUI() {} public MySliderUI(MySlider b) {} public static ComponentUI createUI(JComponent b) { return new MySliderUI((MySlider)b); } public void installUI(JComponent c) { super.installUI(c); scrollListener.setScrollByBlock(true); filledSlider = true; } protected void scrollDueToClickInTrack(int dir) { scrollByBlock(dir); } public void scrollByBlock(int dir) { MySlider slider = (MySlider)this.slider; synchronized (slider) { int val = slider.getValue() + slider.getBlockIncrement(dir) * ((dir > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); if (val < slider.getStopMin()) val = slider.getStopMin(); else if (val > slider.getStopMax()) val = slider.getStopMax(); slider.setValue(val); } } public void scrollByUnit(int dir) { MySlider slider = (MySlider)this.slider; synchronized (slider) { int val = slider.getValue() + ((dir > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); if (val < slider.getStopMin()) val = slider.getStopMin(); else if (val > slider.getStopMax()) val = slider.getStopMax(); slider.setValue(val); } } public void paintTrack(Graphics g) { boolean leftToRight = slider.getComponentOrientation(). isLeftToRight(); g.translate(trackRect.x, trackRect.y); int trackLeft = 0; int trackTop = 0; int trackRight = 0; int trackBottom = 0; int minStopPos = 0; int maxStopPos = trackRect.width - 1; /* Draw the track */ if (slider.getOrientation() == JSlider.HORIZONTAL) { trackBottom = (trackRect.height - 1) - getThumbOverhang(); trackTop = trackBottom - (getTrackWidth() - 1); trackLeft = 0; trackRight = trackRect.width - 1; if (((MySlider)slider).getStopMin() != slider.getMinimum()) { minStopPos = xPositionForValue( ((MySlider)slider). getStopMin()) - thumbRect.width / 2 - 1; } if (((MySlider)slider).getStopMax() != slider.getMaximum()) { maxStopPos = xPositionForValue( ((MySlider)slider). getStopMax()) - thumbRect.width / 2 + 1; } } else { if (leftToRight) { trackLeft = trackRect.width - getTrackWidth() - getThumbOverhang(); trackRight = trackRect.width - 1 - getThumbOverhang(); } else { trackLeft = getThumbOverhang(); trackRight = getThumbOverhang() - 1 + getTrackWidth(); } trackTop = 0; trackBottom = trackRect.height - 1; if (((MySlider)slider).getStopMin() != slider.getMinimum()) { minStopPos = yPositionForValue( ((MySlider)slider). getStopMin()) - thumbRect.height / 2 - 1; } if (((MySlider)slider).getStopMax() != slider.getMaximum()) { maxStopPos = yPositionForValue( ((MySlider)slider). getStopMax()) + thumbRect.height / 2 + 1; } } if (slider.isEnabled()) { g.setColor(MetalLookAndFeel.getControlDarkShadow()); g.drawRect(trackLeft, trackTop, trackRight - trackLeft - 1, trackBottom - trackTop - 1); g.setColor(MetalLookAndFeel.getControlHighlight()); g.drawLine(trackLeft + 1, trackBottom, trackRight, trackBottom); g.drawLine(trackRight, trackTop + 1, trackRight, trackBottom); g.setColor(MetalLookAndFeel.getControlShadow()); g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1); g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2); } else { g.setColor(MetalLookAndFeel.getControlShadow()); g.drawRect(trackLeft, trackTop, trackRight - trackLeft - 1, trackBottom - trackTop - 1); } /* Draw the fill */ int fillTop = 0; int fillLeft = 0; int fillBottom = 0; int fillRight = 0; int middleOfThumb = 0; if (slider.getOrientation() == JSlider.HORIZONTAL) { middleOfThumb = thumbRect.x - trackRect.x + (thumbRect.width / 2); fillTop = trackTop + 1; fillBottom = trackBottom - 2; if (!drawInverted()) { fillLeft = minStopPos + 1; fillRight = middleOfThumb; } else { fillLeft = middleOfThumb; fillRight = maxStopPos - 2; } } else { middleOfThumb = thumbRect.y - trackRect.y + (thumbRect.height / 2); fillLeft = trackLeft + 1; fillRight = trackRight - 2; if (!drawInverted()) { fillTop = middleOfThumb; fillBottom = maxStopPos - 2; } else { fillTop = minStopPos + 1; fillBottom = middleOfThumb; } } if (filledSlider) { if (slider.isEnabled()) { g.setColor(slider.getBackground()); g.drawLine(fillLeft, fillTop, fillRight, fillTop); g.drawLine(fillLeft, fillTop, fillLeft, fillBottom); g.setColor(MetalLookAndFeel.getControlShadow()); g.fillRect(fillLeft + 1, fillTop + 1, fillRight - fillLeft, fillBottom - fillTop); } else { g.setColor(MetalLookAndFeel.getControlShadow()); g.fillRect(fillLeft, fillTop, fillRight - fillLeft, trackBottom - trackTop - 2); } } /* Draw the out-of-bounds regions */ if (slider.getOrientation() == JSlider.HORIZONTAL) { if (((MySlider)slider).getStopMin() != slider.getMinimum()) { g.setColor(getStopFill()); g.fillRect(trackLeft + 1, fillTop, minStopPos - trackLeft, fillBottom - fillTop + 1); } if (((MySlider)slider).getStopMax() != slider.getMaximum()) { g.setColor(getStopFill()); g.fillRect(maxStopPos, fillTop, trackRight - maxStopPos, fillBottom - fillTop + 1); } } else { if (((MySlider)slider).getStopMin() != slider.getMinimum()) { g.setColor(getStopFill()); g.fillRect(fillLeft, trackTop + 1, fillRight - fillLeft, minStopPos - trackTop); } if (((MySlider)slider).getStopMax() != slider.getMaximum()) { g.setColor(getStopFill()); g.fillRect(fillLeft, maxStopPos, fillRight - fillLeft, trackBottom - maxStopPos + 1); } } g.translate(-trackRect.x, -trackRect.y); } /** Returns the fill color for the out-of-bounds region. */ private Color getStopFill() { return Color.darkGray; } /** Special keyboard handler. */ protected void installKeyboardActions(JSlider slider) { super.installKeyboardActions(slider); ActionMap am = (ActionMap)UIManager.get("Slider.actionMap"); if (am == null) { am = new ActionMapUIResource(); UIManager.getLookAndFeelDefaults().put( "Slider.actionMap", am); } am.put("positiveUnitIncrement", new AbstractAction() { public void actionPerformed(ActionEvent e) { MySlider slider = (MySlider)e.getSource(); MySliderUI ui = (MySliderUI)slider.getUI(); ui.scrollByUnit(!slider.getInverted() ? POSITIVE_SCROLL : NEGATIVE_SCROLL); } }); am.put("positiveBlockIncrement", new AbstractAction() { public void actionPerformed(ActionEvent e) { MySlider slider = (MySlider)e.getSource(); MySliderUI ui = (MySliderUI)slider.getUI(); ui.scrollByBlock(!slider.getInverted() ? POSITIVE_SCROLL : NEGATIVE_SCROLL); } }); am.put("negativeUnitIncrement", new AbstractAction() { public void actionPerformed(ActionEvent e) { MySlider slider = (MySlider)e.getSource(); MySliderUI ui = (MySliderUI)slider.getUI(); ui.scrollByUnit(!slider.getInverted() ? NEGATIVE_SCROLL : POSITIVE_SCROLL); } }); am.put("negativeBlockIncrement", new AbstractAction() { public void actionPerformed(ActionEvent e) { MySlider slider = (MySlider)e.getSource(); MySliderUI ui = (MySliderUI)slider.getUI(); ui.scrollByBlock(!slider.getInverted() ? NEGATIVE_SCROLL : POSITIVE_SCROLL); } }); am.put("minScroll", new AbstractAction() { public void actionPerformed(ActionEvent e) { MySlider slider = (MySlider)e.getSource(); slider.setValue(!slider.getInverted() ? slider.getStopMin() : slider.getStopMax()); } }); am.put("maxScroll", new AbstractAction() { public void actionPerformed(ActionEvent e) { MySlider slider = (MySlider)e.getSource(); slider.setValue(!slider.getInverted() ? slider.getStopMax() : slider.getStopMin()); } }); SwingUtilities.replaceUIActionMap(slider, am); } /** TrackListener that respects my cool stop values. */ protected TrackListener createTrackListener(JSlider slider) { return new TrackListener() { public void mouseDragged(MouseEvent e) { MySliderUI ui; MySlider slider = (MySlider)e.getSource(); int thumbMiddle = 0; int val; if (!slider.isEnabled()) return; currentMouseX = e.getX(); currentMouseY = e.getY(); slider.setValueIsAdjusting(true); switch (slider.getOrientation()) { case JSlider.VERTICAL: int halfThumbHeight = thumbRect.height / 2; int thumbTop = e.getY() - offset; int trackTop; int trackBottom; int vMin = yPositionForValue( slider.getStopMin() + slider.getExtent()); int vMax = yPositionForValue( slider.getStopMax() - slider.getExtent()); if (!drawInverted()) { trackTop = vMin; trackBottom = vMax; } else { trackTop = vMax; trackBottom = vMin; } thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); setThumbLocation(thumbRect.x, thumbTop); thumbMiddle = thumbTop + halfThumbHeight; val = valueForYPosition( thumbMiddle); if (val < slider.getStopMin()) val = slider.getStopMin(); else if (val > slider.getStopMax()) val = slider.getStopMax(); slider.setValue(val); break; case JSlider.HORIZONTAL: int halfThumbWidth = thumbRect.width / 2; int thumbLeft = e.getX() - offset; int trackLeft; int trackRight; int hMin = xPositionForValue( slider.getStopMin() + slider.getExtent()); int hMax = xPositionForValue( slider.getStopMax() - slider.getExtent()); if (!drawInverted()) { trackLeft = hMin; trackRight = hMax; } else { trackLeft = hMax; trackRight = hMin; } thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); setThumbLocation(thumbLeft, thumbRect.y); thumbMiddle = thumbLeft + halfThumbWidth; val = valueForXPosition( thumbMiddle); if (val < slider.getStopMin()) val = slider.getStopMin(); else if (val > slider.getStopMax()) val = slider.getStopMax(); slider.setValue(val); } } }; } }