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.

WizardPanel.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.components.EtchedLineBorder;
  24. import com.dmdirc.addons.ui_swing.components.TitlePanel;
  25. import com.dmdirc.addons.ui_swing.components.EtchedLineBorder.BorderSide;
  26. import com.dmdirc.util.ListenerList;
  27. import java.awt.Color;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import java.util.List;
  31. import javax.swing.BorderFactory;
  32. import javax.swing.JButton;
  33. import javax.swing.JLabel;
  34. import javax.swing.JPanel;
  35. import javax.swing.border.EtchedBorder;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Wizard panel.
  39. */
  40. public class WizardPanel extends JPanel implements ActionListener {
  41. /**
  42. * A version number for this class. It should be changed whenever the class
  43. * structure is changed (or anything else that would prevent serialized
  44. * objects being unserialized with the new class).
  45. */
  46. private static final long serialVersionUID = 2;
  47. /** Step panel list. */
  48. private final StepLayout steps;
  49. /** Wizard title. */
  50. private final String title;
  51. /** Wizard. */
  52. private final transient WizardListener wizard;
  53. /** Step panel. */
  54. private JPanel stepsPanel;
  55. /** Title panel. */
  56. private TitlePanel titleLabel;
  57. /** Current step. */
  58. private int currentStep;
  59. /** Prevous step button. */
  60. private JButton prev;
  61. /** Next step button. */
  62. private JButton next;
  63. /** Progress label. */
  64. private JLabel progressLabel;
  65. /** Step Listeners. */
  66. private final ListenerList stepListeners;
  67. /**
  68. * Creates a new instance of WizardPanel.
  69. *
  70. * @param title Title for the wizard
  71. * @param steps Steps for the wizard
  72. * @param wizard Wizard to inform of changes
  73. */
  74. public WizardPanel(final String title, final List<Step> steps,
  75. final WizardListener wizard) {
  76. super();
  77. stepListeners = new ListenerList();
  78. this.title = title;
  79. this.steps = new StepLayout();
  80. this.wizard = wizard;
  81. initComponents();
  82. layoutComponents();
  83. for (Step step : steps) {
  84. addStep(step);
  85. }
  86. }
  87. /** Initialises the components. */
  88. private void initComponents() {
  89. titleLabel = new TitlePanel(new EtchedLineBorder(EtchedBorder.LOWERED,
  90. BorderSide.BOTTOM), title);
  91. stepsPanel = new JPanel(steps);
  92. progressLabel = new JLabel();
  93. next = new JButton();
  94. prev = new JButton("\u00AB Previous");
  95. next.setText("Next \u00BB");
  96. next.addActionListener(this);
  97. prev.addActionListener(this);
  98. }
  99. /** Lays out the components. */
  100. private void layoutComponents() {
  101. final JPanel progressPanel = new JPanel(new MigLayout("fill"));
  102. progressPanel.add(progressLabel, "growx, pushx");
  103. progressPanel.add(prev, "sg button");
  104. progressPanel.add(next, "sg button");
  105. progressPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
  106. Color.BLACK));
  107. progressPanel.setBorder(new EtchedLineBorder(EtchedBorder.LOWERED,
  108. BorderSide.TOP));
  109. setLayout(new MigLayout("fill, wrap 1, ins 0"));
  110. add(titleLabel, "growx, pushx");
  111. add(stepsPanel, "grow, push");
  112. add(progressPanel, "growx, pushx");
  113. }
  114. /** Displays the wizard. */
  115. public void display() {
  116. if (!steps.isEmpty()) {
  117. steps.first(stepsPanel);
  118. currentStep = 0;
  119. titleLabel.setText(steps.getStep(currentStep).getTitle());
  120. prev.setEnabled(false);
  121. if (steps.size() == 1) {
  122. next.setText("Finish");
  123. }
  124. updateProgressLabel();
  125. }
  126. }
  127. /**
  128. * {@inheritDoc}
  129. *
  130. * @param e Action event
  131. */
  132. @Override
  133. public void actionPerformed(final ActionEvent e) {
  134. if (e.getSource() == next) {
  135. nextStep();
  136. } else if (e.getSource() == prev) {
  137. prevStep();
  138. }
  139. }
  140. /**
  141. * Adds a step to the wizard.
  142. *
  143. * @param step Step to add
  144. */
  145. public void addStep(final Step step) {
  146. stepsPanel.add(step, step.toString());
  147. }
  148. /**
  149. * Enables or disables the "next step" button.
  150. *
  151. * @param newValue boolean true to make "next" button enabled, else false
  152. */
  153. public void enableNextStep(final boolean newValue) {
  154. next.setEnabled(newValue);
  155. }
  156. /**
  157. * Enables or disables the "previous step" button.
  158. *
  159. * @param newValue boolean true to make "previous" button enabled, else false
  160. */
  161. public void enablePreviousStep(final boolean newValue) {
  162. prev.setEnabled(newValue);
  163. }
  164. /** Moves to the next step. */
  165. protected void nextStep() {
  166. if ("Next \u00BB".equals(next.getText())) {
  167. prev.setEnabled(true);
  168. fireStepAboutToBeDisplayed(steps.getStep(currentStep + 1));
  169. steps.next(stepsPanel);
  170. fireStepHidden(steps.getStep(currentStep));
  171. currentStep++;
  172. if (currentStep == steps.size() - 1) {
  173. next.setText("Finish");
  174. }
  175. titleLabel.setText(steps.getStep(currentStep).getTitle());
  176. updateProgressLabel();
  177. } else if ("Finish".equals(next.getText())) {
  178. fireWizardFinished();
  179. }
  180. }
  181. /** Moves to the previous step. */
  182. protected void prevStep() {
  183. fireStepAboutToBeDisplayed(steps.getStep(currentStep - 1));
  184. steps.previous(stepsPanel);
  185. fireStepHidden(steps.getStep(currentStep));
  186. currentStep--;
  187. if (currentStep == 0) {
  188. prev.setEnabled(false);
  189. }
  190. next.setText("Next \u00BB");
  191. titleLabel.setText(steps.getStep(currentStep).getTitle());
  192. updateProgressLabel();
  193. }
  194. /**
  195. * Returns the step at the specified index.
  196. *
  197. * @param stepNumber step number
  198. *
  199. * @return Specified step.
  200. */
  201. public Step getStep(final int stepNumber) {
  202. return steps.getStep(stepNumber);
  203. }
  204. /**
  205. * Returns the current step.
  206. *
  207. * @return Current step number
  208. */
  209. public int getCurrentStep() {
  210. return currentStep;
  211. }
  212. /** Updates the progress label. */
  213. private void updateProgressLabel() {
  214. progressLabel.setText("Step " + (currentStep + 1) + " of " +
  215. steps.size());
  216. }
  217. /**
  218. * Adds a step listener to the list.
  219. *
  220. * @param listener
  221. */
  222. public void addStepListener(final StepListener listener) {
  223. stepListeners.add(StepListener.class, listener);
  224. }
  225. /**
  226. * Removes a step listener from the list.
  227. *
  228. * @param listener
  229. */
  230. public void removeStepListener(final StepListener listener) {
  231. stepListeners.remove(StepListener.class, listener);
  232. }
  233. /**
  234. * Adds a wizard listener to the list.
  235. *
  236. * @param listener
  237. */
  238. public void addWizardListener(final WizardListener listener) {
  239. stepListeners.add(WizardListener.class, listener);
  240. }
  241. /**
  242. * Removes a wizard listener from the list.
  243. *
  244. * @param listener
  245. */
  246. public void removeWizardListener(final WizardListener listener) {
  247. stepListeners.remove(WizardListener.class, listener);
  248. }
  249. /**
  250. * Fires step about to be displayed events.
  251. *
  252. * @param step Step to be displayed
  253. */
  254. private void fireStepAboutToBeDisplayed(final Step step) {
  255. for (StepListener listener : stepListeners.get(StepListener.class)) {
  256. listener.stepAboutToDisplay(step);
  257. }
  258. }
  259. /**
  260. * Fires step hidden events.
  261. *
  262. * @param step step thats been hidden
  263. */
  264. private void fireStepHidden(final Step step) {
  265. for (StepListener listener : stepListeners.get(StepListener.class)) {
  266. listener.stepHidden(step);
  267. }
  268. }
  269. /**
  270. * Fires wizard finished events.
  271. */
  272. private void fireWizardFinished() {
  273. for (WizardListener listener : stepListeners.get(WizardListener.class)) {
  274. listener.wizardFinished();
  275. }
  276. }
  277. /**
  278. * Fires wizard cancelled events.
  279. */
  280. protected void fireWizardCancelled() {
  281. for (WizardListener listener : stepListeners.get(WizardListener.class)) {
  282. listener.wizardCancelled();
  283. }
  284. }
  285. }