Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FirstRunWizardExecutor.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.wizard.firstrun;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.wizard.WizardListener;
  20. import java.util.concurrent.Semaphore;
  21. import javax.inject.Inject;
  22. import javax.inject.Provider;
  23. /**
  24. * Utility to create and run a first-run wizard.
  25. */
  26. public class FirstRunWizardExecutor {
  27. /** Semaphore used to signal when the dialog is complete. */
  28. private final Semaphore semaphore = new Semaphore(0);
  29. /** The provider of actual first-run wizards. */
  30. private final Provider<SwingFirstRunWizard> wizardProvider;
  31. /**
  32. * Creates a new instance of {@link FirstRunWizardExecutor}.
  33. *
  34. * @param wizardProvider The provider of first-run wizards to use.
  35. */
  36. @Inject
  37. public FirstRunWizardExecutor(final Provider<SwingFirstRunWizard> wizardProvider) {
  38. this.wizardProvider = wizardProvider;
  39. }
  40. /**
  41. * Shows the wizard and waits for it to finish.
  42. */
  43. public void showWizardAndWait() {
  44. UIUtilities.invokeLater(new FirstRunWizardRunnable());
  45. semaphore.acquireUninterruptibly();
  46. }
  47. /**
  48. * Creates a new wizard, adds a listener, and displays the wizard.
  49. */
  50. private class FirstRunWizardRunnable implements Runnable {
  51. @Override
  52. public void run() {
  53. final SwingFirstRunWizard wizard = wizardProvider.get();
  54. wizard.getWizardDialog().addWizardListener(new FirstRunWizardListener());
  55. wizard.display();
  56. }
  57. }
  58. /**
  59. * Listens for wizard finished events and releases the semaphore.
  60. */
  61. private class FirstRunWizardListener implements WizardListener {
  62. @Override
  63. public void wizardFinished() {
  64. semaphore.release();
  65. }
  66. @Override
  67. public void wizardCancelled() {
  68. semaphore.release();
  69. }
  70. }
  71. }