public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (boundedRangeModel != null) { int value = boundedRangeModel.getValue(); if (source == forwardButton) { boundedRangeModel.setValue( value == boundedRangeModel.getMaximum() ? boundedRangeModel.getMinimum() : value + 1); } else if (source == rewindButton) { boundedRangeModel.setValue( value == boundedRangeModel.getMinimum() ? boundedRangeModel.getMaximum() : value - 1); } else if (source == startButton) { if (startButton.isSelected() != player.isActive()) { if (startButton.isSelected()) { player.start(); } else { player.stop(); } } } else if (source == audioButton) { player.setAudioEnabled(audioButton.isSelected()); } else if (source == colorCyclingButton) { if (player instanceof ColorCyclePlayer) { ((ColorCyclePlayer) player).setColorCyclingStarted(colorCyclingButton.isSelected()); } } } }
public synchronized void setPlayer(Player player) { if (this.player != null) { this.player.removeChangeListener(this); player.removePropertyChangeListener(this); } this.player = player; // boundedRangeModel = player == null ? null : player.getBoundedRangeModel(); boundedRangeModel = player == null ? null : player.getTimeModel(); slider.setModel(boundedRangeModel); if (player != null) { if (player.getState() >= Player.REALIZED && boundedRangeModel != null && boundedRangeModel.getMaximum() == 0) { setPlayerControlsVisible(false); } slider.setProgressModel(player.getCachingModel()); startButton.setSelected(player.isActive()); player.addChangeListener(this); player.addPropertyChangeListener(this); audioButton.setVisible(player.isAudioAvailable()); audioButton.setSelected(player.isAudioEnabled()); colorCyclingButton.setVisible( (player instanceof ColorCyclePlayer) ? ((ColorCyclePlayer) player).isColorCyclingAvailable() : false); colorCyclingButton.setSelected( (player instanceof ColorCyclePlayer) ? ((ColorCyclePlayer) player).isColorCyclingStarted() : false); validate(); repaint(); BoundedRangeModel cachingControlModel = slider.getProgressModel(); } }
public float getProgress() { BoundedRangeModel model = progressBar.getModel(); float progress = (float) (model.getValue() - model.getMinimum()); float limit = (float) model.getMaximum(); return progress / limit; }
/** * Set the models value to the position of the top/left of the thumb relative to the origin of * the track. */ public void mouseDragged(MouseEvent e) { if (!XTraScrollBarUI.this.scrollbar().isEnabled() || !XTraScrollBarUI.this.isDragging()) { return; } Insets sbInsets = XTraScrollBarUI.this.scrollbar().getInsets(); BoundedRangeModel model = XTraScrollBarUI.this.scrollbar().getModel(); Rectangle thumbR = XTraScrollBarUI.this.getThumbBounds(); float trackLength; int thumbMin, thumbMax, thumbPos; if (XTraScrollBarUI.this.scrollbar().getOrientation() == JScrollBar.VERTICAL) { thumbMin = sbInsets.top; // decrButton.getY() + decrButton.getHeight(); thumbMax = XTraScrollBarUI.this.decrButton().getY() /*incrButton.getY()*/ - XTraScrollBarUI.this.getThumbBounds().height; thumbPos = Math.min(thumbMax, Math.max(thumbMin, (e.getY() - offset))); XTraScrollBarUI.this.setThumbBounds(thumbR.x, thumbPos, thumbR.width, thumbR.height); trackLength = XTraScrollBarUI.this.getTrackBounds().height; } else { thumbMin = sbInsets.left; // decrButton.getX() + decrButton.getWidth(); thumbMax = XTraScrollBarUI.this.decrButton().getX() /*incrButton.getX()*/ - XTraScrollBarUI.this.getThumbBounds().width; thumbPos = Math.min(thumbMax, Math.max(thumbMin, (e.getX() - offset))); XTraScrollBarUI.this.setThumbBounds(thumbPos, thumbR.y, thumbR.width, thumbR.height); trackLength = XTraScrollBarUI.this.getTrackBounds().width; } /* Set the scrollbars value. If the thumb has reached the end of * the scrollbar, then just set the value to its maximum. Otherwise * compute the value as accurately as possible. */ if (thumbPos == thumbMax) { XTraScrollBarUI.this.scrollbar().setValue(model.getMaximum() - model.getExtent()); } else { float valueMax = model.getMaximum() - model.getExtent(); float valueRange = valueMax - model.getMinimum(); float thumbValue = thumbPos - thumbMin; float thumbRange = thumbMax - thumbMin; int value = (int) (0.5 + ((thumbValue / thumbRange) * valueRange)); XTraScrollBarUI.this.scrollbar().setValue(value + model.getMinimum()); } }
/** * This determines the amount of the progress bar that should be filled based on the percent done * gathered from the model. This is a common operation so it was abstracted out. It assumes that * your progress bar is linear. That is, if you are making a circular progress indicator, you will * want to override this method. */ protected int getAmountFull(Insets b, int width, int height) { int amountFull = 0; BoundedRangeModel model = progressBar.getModel(); if ((model.getMaximum() - model.getMinimum()) != 0) { if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { amountFull = (int) Math.round(width * progressBar.getPercentComplete()); } else { amountFull = (int) Math.round(height * progressBar.getPercentComplete()); } } return amountFull; }
/** * Creates a JSlider built using a {@link DefaultBoundedRangeModel} and containing a {@link * MouseWheelListener} and some usual default settings * * @param model * @return the corresponding {@link JSlider} */ public static JSlider createJSlider(final BoundedRangeModel model) { final JSlider slider = new JSlider(model); slider.addMouseWheelListener( new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent e) { slider.setValue(-e.getWheelRotation() * model.getExtent() + model.getValue()); } }); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setMajorTickSpacing(model.getMaximum() / 2); slider.setMinorTickSpacing(model.getExtent()); slider.setSnapToTicks(true); return slider; }
// ChangeListener public void stateChanged(ChangeEvent e) { BoundedRangeModel model = progressBar.getModel(); int newRange = model.getMaximum() - model.getMinimum(); int newPercent; int oldPercent = getCachedPercent(); if (newRange > 0) { newPercent = (int) ((100 * (long) model.getValue()) / newRange); } else { newPercent = 0; } if (newPercent != oldPercent) { setCachedPercent(newPercent); progressBar.repaint(); } }
public void adjustmentValueChanged(AdjustmentEvent e) { if (!brm.getValueIsAdjusting()) { if (wasAtBottom) brm.setValue(brm.getMaximum()); } else wasAtBottom = ((brm.getValue() + brm.getExtent()) == brm.getMaximum()); }