您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WizardPanel.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.EtchedLineBorder;
  25. import com.dmdirc.addons.ui_swing.components.EtchedLineBorder.BorderSide;
  26. import com.dmdirc.addons.ui_swing.components.TitlePanel;
  27. import com.dmdirc.addons.ui_swing.components.XScrollablePanel;
  28. import com.dmdirc.util.collections.ListenerList;
  29. import java.awt.Color;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import javax.swing.BorderFactory;
  35. import javax.swing.JButton;
  36. import javax.swing.JLabel;
  37. import javax.swing.JPanel;
  38. import javax.swing.JScrollPane;
  39. import javax.swing.border.EtchedBorder;
  40. import net.miginfocom.swing.MigLayout;
  41. /**
  42. * Wizard panel.
  43. */
  44. public class WizardPanel extends JPanel implements ActionListener {
  45. /** Serial version UID. */
  46. private static final long serialVersionUID = 2;
  47. /** Wizard title. */
  48. private final String title;
  49. /** Step Listeners. */
  50. private final ListenerList stepListeners;
  51. /** List of steps. */
  52. private final List<Step> steps;
  53. /** Step scroll pane. */
  54. private JScrollPane sp;
  55. /** Step panel. */
  56. private JPanel stepsPanel;
  57. /** Title panel. */
  58. private TitlePanel titleLabel;
  59. /** Current step. */
  60. private int currentStep;
  61. /** Prevous step button. */
  62. private JButton prev;
  63. /** Next step button. */
  64. private JButton next;
  65. /** Progress label. */
  66. private JLabel progressLabel;
  67. /**
  68. * Creates a new instance of WizardPanel.
  69. *
  70. * @param title Title for the wizard
  71. * @param steps Steps for the wizard
  72. */
  73. public WizardPanel(final String title, final List<Step> steps) {
  74. super();
  75. stepListeners = new ListenerList();
  76. this.steps = new ArrayList<>(steps);
  77. this.title = title;
  78. initComponents();
  79. layoutComponents();
  80. for (Step step : steps) {
  81. addStep(step);
  82. }
  83. }
  84. /** Initialises the components. */
  85. private void initComponents() {
  86. titleLabel = new TitlePanel(new EtchedLineBorder(EtchedBorder.LOWERED,
  87. BorderSide.BOTTOM), title);
  88. stepsPanel = new XScrollablePanel(new MigLayout("fillx"));
  89. sp = new JScrollPane(stepsPanel);
  90. progressLabel = new JLabel();
  91. next = new JButton();
  92. prev = new JButton("\u00AB Previous");
  93. next.setText("Next \u00BB");
  94. next.addActionListener(this);
  95. prev.addActionListener(this);
  96. }
  97. /** Lays out the components. */
  98. private void layoutComponents() {
  99. final JPanel progressPanel = new JPanel(new MigLayout("fill"));
  100. progressPanel.add(progressLabel, "growx, pushx");
  101. progressPanel.add(prev, "sg button");
  102. progressPanel.add(next, "sg button");
  103. progressPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
  104. Color.BLACK));
  105. progressPanel.setBorder(new EtchedLineBorder(EtchedBorder.LOWERED,
  106. BorderSide.TOP));
  107. setLayout(new MigLayout("fill, wrap 1, ins 0"));
  108. add(titleLabel, "growx, pushx");
  109. add(sp, "grow, push");
  110. add(progressPanel, "growx, pushx");
  111. }
  112. /** Displays the wizard. */
  113. public void display() {
  114. if (!steps.isEmpty()) {
  115. stepsPanel.removeAll();
  116. stepsPanel.add(steps.get(0));
  117. currentStep = 0;
  118. titleLabel.setText(steps.get(currentStep).getTitle());
  119. UIUtilities.resetScrollPane(sp);
  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. steps.add(step);
  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. switch (next.getText()) {
  167. case "Next \u00BB":
  168. prev.setEnabled(true);
  169. fireStepAboutToBeDisplayed(steps.get(currentStep + 1));
  170. stepsPanel.setVisible(false);
  171. stepsPanel.removeAll();
  172. stepsPanel.add(steps.get(currentStep + 1));
  173. stepsPanel.setVisible(true);
  174. fireStepHidden(steps.get(currentStep));
  175. currentStep++;
  176. if (currentStep == steps.size() - 1) {
  177. next.setText("Finish");
  178. }
  179. titleLabel.setText(steps.get(currentStep).getTitle());
  180. updateProgressLabel();
  181. break;
  182. case "Finish":
  183. fireWizardFinished();
  184. break;
  185. }
  186. }
  187. /** Moves to the previous step. */
  188. protected void prevStep() {
  189. fireStepAboutToBeDisplayed(steps.get(currentStep - 1));
  190. stepsPanel.setVisible(false);
  191. stepsPanel.removeAll();
  192. stepsPanel.add(steps.get(currentStep - 1));
  193. stepsPanel.setVisible(true);
  194. fireStepHidden(steps.get(currentStep));
  195. currentStep--;
  196. if (currentStep == 0) {
  197. prev.setEnabled(false);
  198. }
  199. next.setText("Next \u00BB");
  200. titleLabel.setText(steps.get(currentStep).getTitle());
  201. updateProgressLabel();
  202. }
  203. /**
  204. * Returns the step at the specified index.
  205. *
  206. * @param stepNumber step number
  207. *
  208. * @return Specified step.
  209. */
  210. public Step getStep(final int stepNumber) {
  211. return steps.get(stepNumber);
  212. }
  213. /**
  214. * Returns the current step.
  215. *
  216. * @return Current step number
  217. */
  218. public int getCurrentStep() {
  219. return currentStep;
  220. }
  221. /** Updates the progress label. */
  222. private void updateProgressLabel() {
  223. progressLabel.setText("Step " + (currentStep + 1) + " of "
  224. + steps.size());
  225. }
  226. /**
  227. * Adds a step listener to the list.
  228. *
  229. * @param listener Listener to add
  230. */
  231. public void addStepListener(final StepListener listener) {
  232. stepListeners.add(StepListener.class, listener);
  233. }
  234. /**
  235. * Removes a step listener from the list.
  236. *
  237. * @param listener Listener to remove
  238. */
  239. public void removeStepListener(final StepListener listener) {
  240. stepListeners.remove(StepListener.class, listener);
  241. }
  242. /**
  243. * Adds a wizard listener to the list.
  244. *
  245. * @param listener Listener to add
  246. */
  247. public void addWizardListener(final WizardListener listener) {
  248. stepListeners.add(WizardListener.class, listener);
  249. }
  250. /**
  251. * Removes a wizard listener from the list.
  252. *
  253. * @param listener Listener to remove
  254. */
  255. public void removeWizardListener(final WizardListener listener) {
  256. stepListeners.remove(WizardListener.class, listener);
  257. }
  258. /**
  259. * Fires step about to be displayed events.
  260. *
  261. * @param step Step to be displayed
  262. */
  263. private void fireStepAboutToBeDisplayed(final Step step) {
  264. for (StepListener listener : stepListeners.get(StepListener.class)) {
  265. listener.stepAboutToDisplay(step);
  266. }
  267. }
  268. /**
  269. * Fires step hidden events.
  270. *
  271. * @param step step thats been hidden
  272. */
  273. private void fireStepHidden(final Step step) {
  274. for (StepListener listener : stepListeners.get(StepListener.class)) {
  275. listener.stepHidden(step);
  276. }
  277. }
  278. /**
  279. * Fires wizard finished events.
  280. */
  281. private void fireWizardFinished() {
  282. for (WizardListener listener : stepListeners.get(WizardListener.class)) {
  283. listener.wizardFinished();
  284. }
  285. }
  286. /**
  287. * Fires wizard cancelled events.
  288. */
  289. protected void fireWizardCancelled() {
  290. for (WizardListener listener : stepListeners.get(WizardListener.class)) {
  291. listener.wizardCancelled();
  292. }
  293. }
  294. }