1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import fr.soleil.comete.awt.util.ColorTool;
import fr.soleil.comete.definition.event.PlayerEvent;
import fr.soleil.comete.definition.event.WheelSwitchEvent;
import fr.soleil.comete.definition.listener.IPlayerListener;
import fr.soleil.comete.definition.listener.ISnapshotListener;
import fr.soleil.comete.definition.listener.IWheelSwitchListener;
import fr.soleil.comete.definition.util.SnapshotEventDelegate;
import fr.soleil.comete.definition.widget.IPlayer;
import fr.soleil.comete.definition.widget.util.CometeColor;
import fr.soleil.comete.definition.widget.util.IPlayerAnimationBehavior;
import fr.soleil.comete.swing.stackviewer.PlayerAnimationComponentBehavior;
import fr.soleil.lib.project.awt.layout.VerticalFlowLayout;
/**
* This class allows to manage a JSlider with buttons : - "previous step" - "next" - "first step" -
* "last step" - "play"
*
* @author MARECHAL
*
*/
public class Player extends AbstractPanel implements IPlayer, ChangeListener {
private static final long serialVersionUID = -2598674782507647972L;
protected static final ImageIcon SNAPSHOT_ICON = new ImageIcon(
Player.class.getResource("/com/famfamfam/silk/film_save.png"));
protected static final String SNAPSHOT_TOOLTIP = "Generate an animated file (.gif, .avi or .mov)";
JPanel panel = new JPanel();
protected Slider slider;
protected JPanel buttonPanel;
protected JButton btFirst;
protected JButton btLast;
protected JButton btPrevious;
protected JButton btNext;
protected JButton btPeriodSetting;
protected WheelSwitch wheelswitch;
protected static JButton btPlay;
protected JButton btStop;
protected JToggleButton tbtRepeat;
protected JButton btGenerateAnimation;
protected IPlayerAnimationBehavior animationBehavior;
protected int periodInMs;
// timer which contains timertask to increment selected value in slider
protected Timer timerIncrementSlider;
// boolean which set repeat mode
// (the reading will continue until the user click on an another button)
protected boolean bRepeat;
protected final List<IPlayerListener> listeners;
protected int index;
protected java.awt.Component videoComponent;
// Snapshot management
protected final SnapshotEventDelegate snapshotEventDelegate;
protected String snapshotDirectory;
protected String snapshotFile;
private boolean wheelswitchVisible =false;
//final WheelSwitch wheelswitch = new WheelSwitch();
/**
* Default constructor
*/
public Player() {
this(null);
}
public Player(Slider slider) {
super();
videoComponent = this;
snapshotEventDelegate = new SnapshotEventDelegate(this, null);
snapshotDirectory = ".";
snapshotFile = null;
if (slider == null) {
this.slider = new Slider();
this.slider.setPreferedMajorTickColor(Color.BLACK);
this.slider.setMinimum(0);
this.slider.setMinorTickSpacing(1);
this.slider.setMajorTickSpacing(5);
this.slider.setPaintTicks(true);
this.slider.setPaintLabels(true);
this.slider.setValue(0);
} else {
this.slider = slider;
}
this.slider.addChangeListener(this);
index = -1;
buttonPanel = null;
btFirst = null;
btLast = null;
btPrevious = null;
btNext = null;
btPeriodSetting = null;
btPlay = null;
btStop = null;
tbtRepeat = null;
periodInMs = 40;
timerIncrementSlider = null;
bRepeat = false;
btGenerateAnimation = null;
animationBehavior = new PlayerAnimationComponentBehavior();
listeners = new ArrayList<IPlayerListener>();
setSliderStep(this.slider.getMinorTickSpacing());
buildGUI();
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
java.awt.Component[] components = getComponents();
if (components != null) {
for (java.awt.Component comp : components) {
setComponentsEnabled(comp, enabled);
}
}
}
protected void setComponentsEnabled(java.awt.Component comp, boolean enabled) {
if ((comp != null) && (comp != this)) {
comp.setEnabled(enabled);
if (comp instanceof Container) {
java.awt.Component[] components = ((Container) comp).getComponents();
for (java.awt.Component child : components) {
setComponentsEnabled(child, enabled);
}
}
}
}
/**
* build all buttons to control slider and add it to the panel
*/
public void buildGUI() {
setLayout(new GridBagLayout());
GridBagConstraints buttonsConstraints = new GridBagConstraints();
buttonsConstraints.fill = GridBagConstraints.HORIZONTAL;
buttonsConstraints.gridx = 0;
buttonsConstraints.gridy = 0;
buttonsConstraints.weightx = 1;
buttonsConstraints.weighty = 0;
add(getButtonPanel(), buttonsConstraints);
GridBagConstraints sliderConstraints = new GridBagConstraints();
sliderConstraints.fill = GridBagConstraints.HORIZONTAL;
sliderConstraints.gridx = 0;
sliderConstraints.gridy = 1;
sliderConstraints.weightx = 1;
sliderConstraints.weighty = 0;
sliderConstraints.anchor = GridBagConstraints.NORTH;
add((JComponent) getSlider(), sliderConstraints);
GridBagConstraints glueConstraints = new GridBagConstraints();
glueConstraints.fill = GridBagConstraints.BOTH;
glueConstraints.gridx = 0;
glueConstraints.gridy = 2;
glueConstraints.weightx = 1;
glueConstraints.weighty = 1;
glueConstraints.anchor = GridBagConstraints.NORTH;
add(Box.createGlue(), glueConstraints);
}
public JPanel getButtonPanel() {
if (buttonPanel == null) {
buttonPanel = new JPanel(new VerticalFlowLayout());
buttonPanel.add(getBtFirstStep());
buttonPanel.add(getBtPreviousStep());
buttonPanel.add(getBtNextStep());
buttonPanel.add(getBtLastStep());
buttonPanel.add(getBtSettingPeriod());
// add extra space (actually 2 default spacing of flowlayout)
buttonPanel.add(Box.createRigidArea(new Dimension(0, 0)));
buttonPanel.add(getBtPlay());
buttonPanel.add(getBtStop());
buttonPanel.add(getTbtRepeat());
buttonPanel.add(getBtGenerateAnimation());
}
return buttonPanel;
}
/**
* Sets this {@link Player}'s {@link Slider}
*
* @param slider the {@link Slider} to set
*/
public void setSlider(Slider slider) {
if (slider != null) {
this.slider = slider;
}
}
/**
* Create and return button which allows toset the period
*
* @return A {@link JButton}
*/
public JButton getBtSettingPeriod() {
if (btPeriodSetting == null) {
btPeriodSetting = new JButton(String.valueOf(periodInMs + " ms"));
btPeriodSetting.setToolTipText("Set the period");
btPeriodSetting.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setPeriod();
}
});
}
return btPeriodSetting;
}
/**
* Create and return button which allows to go to the first record
*
* @return A {@link JButton}
*/
public JButton getBtFirstStep() {
if (btFirst == null) {
btFirst = new JButton(new ImageIcon(this.getClass().getResource(
"/org/tango-project/tango-icon-theme/16x16/actions/media-skip-backward.png")));
btFirst.setEnabled(false);
btFirst.setToolTipText("Go to the first step");
btFirst.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gotoFirst();
}
});
}
return btFirst;
}
/**
* Create and return button which allows to go to the last record
*
* @return A {@link JButton}
*/
public JButton getBtLastStep() {
if (btLast == null) {
btLast = new JButton(new ImageIcon(
Player.class
.getResource("/org/tango-project/tango-icon-theme/16x16/actions/media-skip-forward.png")));
btLast.setToolTipText("Go to the last step");
btLast.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gotoLast();
}
});
}
return btLast;
}
/**
* Create and return button which allows to go to the previous record
*
* @return A {@link JButton}
*/
public JButton getBtPreviousStep() {
if (btPrevious == null) {
btPrevious = new JButton(new ImageIcon(
Player.class
.getResource("/org/tango-project/tango-icon-theme/16x16/actions/media-seek-backward.png")));
btPrevious.setEnabled(false);
btPrevious.setToolTipText("Go to the previous step");
btPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
previous();
}
});
}
return btPrevious;
}
/**
* Create and return button which allows to go to the next record
*
* @return A {@link JButton}
*/
public JButton getBtNextStep() {
if (btNext == null) {
btNext = new JButton(new ImageIcon(
Player.class
.getResource("/org/tango-project/tango-icon-theme/16x16/actions/media-seek-forward.png")));
btNext.setToolTipText("Go to the next step");
btNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
next();
}
});
}
return btNext;
}
/**
* Create and return button which allows to read all values
*
* @return A {@link JButton}
*/
public JButton getBtPlay() {
if (btPlay == null) {
btPlay = new JButton(new ImageIcon(this.getClass().getResource(
"/org/tango-project/tango-icon-theme/16x16/actions/media-playback-start.png")));
btPlay.setToolTipText("Play");
btPlay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
}
return btPlay;
}
/**
* Create timer with timertask which increments selected value in slider to max value
*/
public Timer getTimerIncremtSlider() {
if (timerIncrementSlider == null) {
timerIncrementSlider = new Timer(periodInMs, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// increment selected value to max value
if (slider.getValue() < slider.getMaximum()) {
slider.setValue((int) (slider.getValue() + getSliderStep()));
} else if (slider.getValue() == slider.getMaximum()) {
if (bRepeat) {
slider.setValue(slider.getMinimum());
} else {
timerIncrementSlider.stop();
btStop.setEnabled(false);
}
}
}
});
}
return timerIncrementSlider;
}
/**
* Create and return button which allows to stop "play action( read all values)"
*
* @return a {@link JButton}
*/
public JButton getBtStop() {
if (btStop == null) {
btStop = new JButton(new ImageIcon(this.getClass().getResource(
"/org/tango-project/tango-icon-theme/16x16/actions/media-playback-stop.png")));
btStop.setEnabled(false);
btStop.setToolTipText("Stop");
btStop.addActionListener(getActionStop());
}
return btStop;
}
/**
* Create actionlistener which stops reading and disable bt stop
*
* @return ActionListener
*/
public ActionListener getActionStop() {
ActionListener listenerStop = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stop();
}
};
return listenerStop;
}
public JButton getBtGenerateAnimation() {
if (btGenerateAnimation == null) {
btGenerateAnimation = new JButton(SNAPSHOT_ICON);
btGenerateAnimation.setToolTipText(SNAPSHOT_TOOLTIP);
final IPlayer player = this;
btGenerateAnimation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// This must be done in a separated Thread to avoid UI display lock
new Thread("Generate animation for " + player) {
@Override
public void run() {
if (animationBehavior != null) {
snapshotEventDelegate.changeSnapshotLocation(animationBehavior.generateAnimation(
player, snapshotEventDelegate.getSnapshotDirectory()));
}
};
}.start();
}
});
btGenerateAnimation.setVisible(false);
}
return btGenerateAnimation;
}
/**
* Create and return button which allows to repeat the reading
*
* @return JToggleButton
*/
public JToggleButton getTbtRepeat() {
if (tbtRepeat == null) {
tbtRepeat = new JToggleButton(new ImageIcon(this.getClass().getResource(
"/fr/soleil/comete/icons/repeat.png")));
tbtRepeat.setToolTipText("Repeat");
tbtRepeat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bRepeat = tbtRepeat.isSelected();
}
});
}
return tbtRepeat;
}
/**
* Setting period
*/
public void setPeriod() {
String newValue = JOptionPane.showInputDialog(this, "Set the new period in millisecond",
String.valueOf(periodInMs));
try {
int newPeriod = Integer.parseInt(newValue);
setPeriodInMs(newPeriod);
} catch (Exception e) {
// nothing to do: ignore exception
}
}
/**
* Returns the slider step
*
* @return A <code>double</code>
*/
public double getSliderStep() {
return slider.getStep();
}
/**
* Sets the slider step
*
* @param slider_step The step to set
*/
public void setSliderStep(double slider_step) {
slider.setStep(slider_step);
}
/**
* Update buttons (enable/disable) in function of the specified selected index.
*
* @param index The selected index
*/
public void updateStateButtonsBar(int index) {
if (slider.getMaximum() == slider.getMinimum()) {
// disable all buttons if min = max
getBtNextStep().setEnabled(false);
getBtLastStep().setEnabled(false);
getBtFirstStep().setEnabled(false);
getBtPreviousStep().setEnabled(false);
} else {
if (index == slider.getMaximum()) {
// max value selected
getBtNextStep().setEnabled(false);
getBtLastStep().setEnabled(false);
getBtFirstStep().setEnabled(true);
getBtPreviousStep().setEnabled(true);
} else if (index == slider.getMinimum()) {
// min value selected
getBtNextStep().setEnabled(true);
getBtLastStep().setEnabled(true);
getBtFirstStep().setEnabled(false);
getBtPreviousStep().setEnabled(false);
} else {
// another value selected
getBtNextStep().setEnabled(true);
getBtLastStep().setEnabled(true);
getBtFirstStep().setEnabled(true);
getBtPreviousStep().setEnabled(true);
}
}
}
@Override
public void stateChanged(ChangeEvent event) {
// selected value in slider
int selectedIndex = getIndex();
// update buttons in the associated record bar (previous, next, first, last)
updateStateButtonsBar(selectedIndex);
// update the viewer only if the current slider's value has changed from the previous one
if ((index == -1) || (index != selectedIndex)) {
index = selectedIndex;
fireIndexChanged(new PlayerEvent(this));
}
}
@Override
public void initSlider(int stackSize) {
getSlider().setMaximumDouble(stackSize);
}
@Override
public int getPeriodInMs() {
return periodInMs;
}
@Override
public void setPeriodInMs(int periodInMs) {
this.periodInMs = periodInMs;
if (btPeriodSetting != null) {
btPeriodSetting.setText(periodInMs + " ms");
}
// apply change immediately
if (timerIncrementSlider != null) {
timerIncrementSlider.setInitialDelay(periodInMs);
timerIncrementSlider.setDelay(periodInMs);
}
}
@Override
public Slider getSlider() {
return slider;
}
@Override
public void play() {
slider.setValue(slider.getMinimum());
btStop.setEnabled(true);
getTimerIncremtSlider().start();
}
@Override
public void stop() {
getTimerIncremtSlider().stop();
btStop.setEnabled(false);
}
@Override
public void gotoFirst() {
setIndex(slider.getMinimum());
}
@Override
public void gotoLast() {
setIndex(slider.getMaximum());
}
@Override
public void previous() {
setIndex((int) (slider.getValue() - getSliderStep()));
}
@Override
public void next() {
setIndex((int) (slider.getValue() + getSliderStep()));
}
@Override
public void setIndex(int index) {
stop();
if ((slider.getValue() != index) && (index >= slider.getMinimum()) && (index <= slider.getMaximum())) {
slider.setValue(index);
}
}
@Override
public int getIndex() {
return slider.getValue();
}
@Override
public boolean isRepeatActivated() {
return bRepeat;
}
@Override
public void setRepeatActivated(boolean activated) {
bRepeat = activated;
tbtRepeat.setSelected(activated);
}
@Override
public boolean isEditingData() {
return getBtStop().isEnabled();
}
@Override
public void addPlayerListener(IPlayerListener listener) {
synchronized (listeners) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
}
@Override
public void fireIndexChanged(PlayerEvent event) {
List<IPlayerListener> copy = new ArrayList<IPlayerListener>();
synchronized (listeners) {
copy.addAll(listeners);
}
for (IPlayerListener listener : copy) {
listener.indexChanged(event);
}
copy.clear();
}
@Override
public void removePlayerListener(IPlayerListener listener) {
synchronized (listeners) {
listeners.remove(listener);
}
}
@Override
public void setCometeBackground(final CometeColor color) {
super.setCometeBackground(color);
getButtonPanel().setBackground(ColorTool.getColor(color));
getSlider().setCometeBackground(color);
}
@Override
public void setPlayerAnimationBehavior(IPlayerAnimationBehavior behavior) {
animationBehavior = behavior;
}
@Override
public IPlayerAnimationBehavior getPlayerAnimationBehavior() {
return animationBehavior;
}
@Override
public int getMaximum() {
return slider.getMaximum();
}
@Override
public void setAnimationButtonVisible(boolean visible) {
btGenerateAnimation.setVisible(visible);
}
@Override
public boolean isAnimationButtonVisible() {
return btGenerateAnimation.isVisible();
}
/**
* Returns the {@link java.awt.Component Component} used to make videos and snapshots
*
* @return A {@link java.awt.Component Component}
*/
public java.awt.Component getVideoComponent() {
return videoComponent;
}
/**
* Sets the {@link java.awt.Component Component} to use to make videos and snapshots
*
* @param videoComponent The {@link java.awt.Component Component} to use
*/
public void setVideoComponent(java.awt.Component videoComponent) {
this.videoComponent = (videoComponent == null ? this : videoComponent);
}
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel panel = new JPanel();
final Player player = new Player();
player.getSlider().setMinimumDouble(0);
player.getSlider().setMaximumDouble(1000);
player.getSlider().setStep(1);
// player.getSlider().setMinorTickSpacing(10);
// player.getSlider().setMajorTickSpacing(100);
f.getContentPane().add(player);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
@Override
public String getSnapshotDirectory() {
return snapshotEventDelegate.getSnapshotDirectory();
}
@Override
public void setSnapshotDirectory(String snapshotDirectory) {
snapshotEventDelegate.setSnapshotDirectory(snapshotDirectory);
}
@Override
public String getSnapshotFile() {
return snapshotEventDelegate.getSnapshotFile();
}
@Override
public void addSnapshotListener(ISnapshotListener listener) {
snapshotEventDelegate.addSnapshotListener(listener);
}
@Override
public void removeSnapshotListener(ISnapshotListener listener) {
snapshotEventDelegate.removeSnapshotListener(listener);
}
public void setWheelswitchVisible(boolean wheelswitchVisible) {
this.wheelswitchVisible = wheelswitchVisible;
boolean setWheelswitchVisible = true;
}
public boolean isWheelswitchVisible() {
return wheelswitchVisible;
}
} |
Partager