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.

WizardDialog.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.wizard;
  23. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  24. import com.dmdirc.ui.CoreUIUtils;
  25. import java.awt.Window;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.WindowAdapter;
  29. import java.awt.event.WindowEvent;
  30. import java.util.List;
  31. import javax.swing.JButton;
  32. /**
  33. * Basic wizard container.
  34. */
  35. public class WizardDialog extends StandardDialog implements ActionListener {
  36. /** Serial version UID. */
  37. private static final long serialVersionUID = 2;
  38. /** Wizard. */
  39. private final WizardPanel wizard;
  40. /** Parent container. */
  41. private final Window parentWindow;
  42. /**
  43. * Creates a new instance of WizardFrame that requires a mainframe.
  44. *
  45. * @param title Title for the wizard
  46. * @param steps Steps for the wizard
  47. * @param parentWindow Parent component
  48. * @param modality Modality
  49. */
  50. public WizardDialog(final String title, final List<Step> steps, final Window parentWindow,
  51. final ModalityType modality) {
  52. super(parentWindow, modality);
  53. setTitle(title);
  54. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  55. orderButtons(new JButton(), new JButton());
  56. this.wizard = new WizardPanel(title, steps);
  57. this.parentWindow = parentWindow;
  58. layoutComponents();
  59. }
  60. /** Lays out the components. */
  61. private void layoutComponents() {
  62. setContentPane(wizard);
  63. }
  64. /** Displays the wizard. */
  65. @Override
  66. public void display() {
  67. wizard.display();
  68. if (parentWindow != null) {
  69. setLocationRelativeTo(parentWindow);
  70. } else {
  71. CoreUIUtils.centreWindow(this);
  72. }
  73. addWindowListener(new WindowAdapter() {
  74. /** {@inheritDoc} */
  75. @Override
  76. public void windowClosed(final WindowEvent e) {
  77. removeWindowListener(this);
  78. wizard.fireWizardCancelled();
  79. }
  80. });
  81. setResizable(false);
  82. setVisible(true);
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public void validate() {
  87. super.validate();
  88. setLocationRelativeTo(parentWindow);
  89. }
  90. /**
  91. * {@inheritDoc}
  92. *
  93. * @param e Action event
  94. */
  95. @Override
  96. public void actionPerformed(final ActionEvent e) {
  97. if (e.getSource() == getOkButton()) {
  98. wizard.nextStep();
  99. } else if (e.getSource() == getCancelButton()) {
  100. wizard.fireWizardCancelled();
  101. }
  102. }
  103. /**
  104. * Adds a step to the wizard.
  105. *
  106. * @param step Step to add
  107. */
  108. public void addStep(final Step step) {
  109. wizard.addStep(step);
  110. }
  111. /**
  112. * Returns the step at the specified index.
  113. *
  114. * @param stepNumber step number
  115. *
  116. * @return Specified step.
  117. */
  118. public Step getStep(final int stepNumber) {
  119. return wizard.getStep(stepNumber);
  120. }
  121. /**
  122. * Returns the current step.
  123. *
  124. * @return Current step number
  125. */
  126. public int getCurrentStep() {
  127. return wizard.getCurrentStep();
  128. }
  129. /**
  130. * Enables or disables the "next step" button.
  131. *
  132. * @param newValue boolean true to make "next" button enabled, else false
  133. */
  134. public void enableNextStep(final boolean newValue) {
  135. wizard.enableNextStep(newValue);
  136. }
  137. /**
  138. * Enables or disables the "previous step" button.
  139. *
  140. * @param newValue boolean true to make "previous" button enabled, else false
  141. */
  142. public void enablePreviousStep(final boolean newValue) {
  143. wizard.enablePreviousStep(newValue);
  144. }
  145. /**
  146. * Adds a step listener to the list.
  147. *
  148. * @param listener Listener to add
  149. */
  150. public void addStepListener(final StepListener listener) {
  151. wizard.addStepListener(listener);
  152. }
  153. /**
  154. * Removes a step listener from the list.
  155. *
  156. * @param listener Listener to remove
  157. */
  158. public void removeStepListener(final StepListener listener) {
  159. wizard.removeStepListener(listener);
  160. }
  161. /**
  162. * Adds a wizard listener to the list.
  163. *
  164. * @param listener Listener to add
  165. */
  166. public void addWizardListener(final WizardListener listener) {
  167. wizard.addWizardListener(listener);
  168. }
  169. /**
  170. * Removes a wizard listener from the list.
  171. *
  172. * @param listener Listener to remove
  173. */
  174. public void removeWizardListener(final WizardListener listener) {
  175. wizard.removeWizardListener(listener);
  176. }
  177. }