No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockingActionFactory.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package org.insa.graphics;
  2. import java.awt.Component;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import javax.swing.JOptionPane;
  7. public class BlockingActionFactory {
  8. // List of running actions.
  9. private ArrayList<RunningAction> actions = new ArrayList<>();
  10. // Parent component.
  11. private Component parentComponent;
  12. public BlockingActionFactory(Component parentComponent) {
  13. this.parentComponent = parentComponent;
  14. }
  15. public void addAction(RunningAction action) {
  16. actions.add(action);
  17. }
  18. public ActionListener createBlockingAction(ActionListener listener) {
  19. return new ActionListener() {
  20. @Override
  21. public void actionPerformed(ActionEvent e) {
  22. boolean accepted = true;
  23. // Check if actions...
  24. for (int i = 0; i < actions.size() && accepted; ++i) {
  25. RunningAction action = actions.get(i);
  26. // If action is running, ask user...
  27. if (action.isRunning()) {
  28. if (JOptionPane.showConfirmDialog(parentComponent, "Action {"
  29. + action.getInformation()
  30. + "} is running, do you want to stop it?") == JOptionPane.OK_OPTION) {
  31. action.interrupt();
  32. }
  33. else {
  34. accepted = false;
  35. }
  36. }
  37. }
  38. // If action is accepted, run it...
  39. if (accepted) {
  40. listener.actionPerformed(e);
  41. }
  42. }
  43. };
  44. }
  45. }