PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/jobs/Job.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 126 lines | 81 code | 8 blank | 37 comment | 23 complexity | 6bc1957511502748687b08a2a94441e8 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.jobs;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import javax.swing.JProgressBar;
  22. import javax.swing.SwingWorker;
  23. import mpv5.logging.Log;
  24. import mpv5.ui.frames.MPView;
  25. /**
  26. *
  27. * This class helps to manage background tasks which result e.g. in the creation of a file or similar
  28. */
  29. public class Job extends SwingWorker<Object, Object> {
  30. private final List<Waitable> objects;
  31. private final Waiter recipient;
  32. private JProgressBar bar;
  33. private String message;
  34. private final List<Exception> code = new ArrayList<Exception>();
  35. /**
  36. * Creates a new job
  37. * @param waitable
  38. * @param waiter
  39. */
  40. public Job(Waitable waitable, Waiter waiter) {
  41. this.objects = Collections.singletonList(waitable);
  42. this.recipient = waiter;
  43. this.bar = mpv5.YabsViewProxy.instance().getProgressbar();
  44. }
  45. /**
  46. * Creates a new job
  47. * @param waitable
  48. * @param waiter
  49. * @param message
  50. */
  51. public Job(Waitable waitable, Waiter waiter, String message) {
  52. this.objects = Collections.singletonList(waitable);
  53. this.recipient = waiter;
  54. this.message = message;
  55. }
  56. /**
  57. * Creates a new job
  58. * @param waitables
  59. * @param waiter
  60. * @param message
  61. */
  62. public Job(List<Waitable> waitables, Waiter waiter, String message) {
  63. this.objects = waitables;
  64. this.recipient = waiter;
  65. this.message = message;
  66. this.bar = mpv5.YabsViewProxy.instance().getProgressbar();
  67. }
  68. @Override
  69. public Object doInBackground() throws Exception {
  70. if (bar != null) {
  71. bar.setIndeterminate(true);
  72. }
  73. try {
  74. for (int i = 0; i < objects.size(); i++) {
  75. Waitable waitable = objects.get(i);
  76. code.add(waitable.waitFor());
  77. }
  78. } catch (Exception e) {
  79. if (bar != null) {
  80. bar.setIndeterminate(false);
  81. }
  82. throw e;
  83. } finally {
  84. if (bar != null) {
  85. bar.setIndeterminate(false);
  86. }
  87. }
  88. return objects;
  89. }
  90. @Override
  91. public void done() {
  92. if (recipient != null) {
  93. try {
  94. if (code != null && code.size() > 0) {
  95. if (objects.size() == 1) {
  96. recipient.set(objects.get(0), code.get(0));
  97. } else {
  98. recipient.set(objects, code.get(0));
  99. }
  100. } else {
  101. if (objects.size() == 1) {
  102. recipient.set(objects.get(0), null);
  103. } else {
  104. recipient.set(objects, null);
  105. }
  106. }
  107. } catch (Exception ex) {
  108. Log.Debug(ex);
  109. }
  110. }
  111. if (bar != null) {
  112. bar.setIndeterminate(false);
  113. }
  114. if (message != null) {
  115. mpv5.YabsViewProxy.instance().addMessage(message);
  116. }
  117. }
  118. }