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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2006-2007 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.ui.swing.dialogs.wizard;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.ui.swing.MainFrame;
  25. import static com.dmdirc.ui.swing.UIUtilities.SMALL_BORDER;
  26. import java.awt.BorderLayout;
  27. import java.awt.Color;
  28. import java.awt.Dimension;
  29. import java.awt.GraphicsConfiguration;
  30. import java.awt.GraphicsDevice;
  31. import java.awt.Insets;
  32. import java.awt.MouseInfo;
  33. import java.awt.PointerInfo;
  34. import java.awt.Rectangle;
  35. import java.awt.event.ActionEvent;
  36. import java.awt.event.ActionListener;
  37. import java.io.Serializable;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import javax.swing.BorderFactory;
  41. import javax.swing.Box;
  42. import javax.swing.BoxLayout;
  43. import javax.swing.JButton;
  44. import javax.swing.JDialog;
  45. import javax.swing.JFrame;
  46. import javax.swing.JLabel;
  47. import javax.swing.JPanel;
  48. import javax.swing.JSeparator;
  49. /**
  50. * Basic wizard container.
  51. */
  52. public final class WizardDialog extends JFrame implements ActionListener,
  53. Serializable {
  54. /**
  55. * A version number for this class. It should be changed whenever the class
  56. * structure is changed (or anything else that would prevent serialized
  57. * objects being unserialized with the new class).
  58. */
  59. private static final long serialVersionUID = 1;
  60. /** Step panel list. */
  61. private final List<Step> steps;
  62. /** Wizard title. */
  63. private final String title;
  64. /** Wizard. */
  65. private final transient Wizard wizard;
  66. /** Does this wizard use mainframe. */
  67. private final boolean hasMainframe;
  68. /** Button panel. */
  69. private JPanel buttonsPanel;
  70. /** Title panel. */
  71. private JPanel titlePanel;
  72. /** Current step. */
  73. private int currentStep;
  74. /** Prevous step button. */
  75. private JButton prev;
  76. /** Next step button. */
  77. private JButton next;
  78. /** Progress label. */
  79. private JLabel progressLabel;
  80. /**
  81. * Creates a new instance of WizardFrame that requires a mainframe.
  82. *
  83. * @param title Title for the wizard
  84. * @param steps Steps for the wizard
  85. * @param wizard Wizard to inform of changes
  86. * @param modal Whether the wizard should be modal
  87. */
  88. public WizardDialog(final String title, final List<Step> steps,
  89. final Wizard wizard, final boolean modal) {
  90. super();
  91. hasMainframe = true;
  92. this.title = title;
  93. this.steps = new ArrayList<Step>(steps);
  94. this.wizard = wizard;
  95. initComponents();
  96. layoutComponents();
  97. }
  98. /**
  99. * Creates a new instance of WizardFrame that doesn't require a mainframe.
  100. *
  101. * @param title Title for the wizard
  102. * @param steps Steps for the wizard
  103. * @param wizard Wizard to inform of changes
  104. */
  105. public WizardDialog(final String title, final List<Step> steps,
  106. final Wizard wizard) {
  107. super();
  108. hasMainframe = false;
  109. this.title = title;
  110. this.steps = new ArrayList<Step>(steps);
  111. this.wizard = wizard;
  112. initComponents();
  113. layoutComponents();
  114. }
  115. /** Initialises the components. */
  116. private void initComponents() {
  117. final JLabel titleLabel = new JLabel(title);
  118. titlePanel = new JPanel();
  119. titlePanel.setLayout(new BorderLayout());
  120. titlePanel.setBackground(Color.WHITE);
  121. titleLabel.setFont(titleLabel.getFont().deriveFont(
  122. (float) (titleLabel.getFont().getSize() * 1.5)));
  123. titleLabel.setBorder(BorderFactory.createEmptyBorder(SMALL_BORDER,
  124. SMALL_BORDER, SMALL_BORDER, SMALL_BORDER));
  125. titlePanel.add(titleLabel, BorderLayout.CENTER);
  126. titlePanel.add(new JSeparator(), BorderLayout.PAGE_END);
  127. initButtonsPanel();
  128. }
  129. /** Lays out the components. */
  130. private void layoutComponents() {
  131. this.setLayout(new BorderLayout());
  132. this.add(titlePanel, BorderLayout.PAGE_START);
  133. this.add(buttonsPanel, BorderLayout.PAGE_END);
  134. }
  135. /** Initialises the button panel. */
  136. private void initButtonsPanel() {
  137. final JPanel buttonPanel = new JPanel();
  138. buttonsPanel = new JPanel();
  139. progressLabel = new JLabel();
  140. prev = new JButton("<< Previous");
  141. next = new JButton("Next >>");
  142. prev.setPreferredSize(new Dimension(110, 30));
  143. next.setPreferredSize(new Dimension(110, 30));
  144. prev.setMargin(new Insets(prev.getMargin().top, 0,
  145. prev.getMargin().bottom, 0));
  146. next.setMargin(new Insets(next.getMargin().top, 0,
  147. next.getMargin().bottom, 0));
  148. prev.addActionListener(this);
  149. next.addActionListener(this);
  150. buttonPanel.setBorder(BorderFactory.createEmptyBorder(SMALL_BORDER,
  151. SMALL_BORDER, SMALL_BORDER, SMALL_BORDER));
  152. buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
  153. buttonPanel.add(progressLabel);
  154. buttonPanel.add(Box.createHorizontalStrut(SMALL_BORDER));
  155. buttonPanel.add(Box.createHorizontalGlue());
  156. buttonPanel.add(prev);
  157. buttonPanel.add(Box.createHorizontalStrut(SMALL_BORDER));
  158. buttonPanel.add(next);
  159. buttonsPanel.setLayout(new BorderLayout());
  160. buttonsPanel.add(new JSeparator(), BorderLayout.PAGE_START);
  161. buttonsPanel.add(buttonPanel, BorderLayout.CENTER);
  162. }
  163. /** Displays the wizard. */
  164. public void display() {
  165. if (!steps.isEmpty()) {
  166. add(steps.get(0), BorderLayout.CENTER);
  167. currentStep = 0;
  168. prev.setEnabled(false);
  169. if (steps.size() == 1) {
  170. next.setText("Finish");
  171. }
  172. updateProgressLabel();
  173. setTitle(title);
  174. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  175. pack();
  176. if (hasMainframe) {
  177. setLocationRelativeTo((MainFrame) Main.getUI().getMainWindow());
  178. } else {
  179. // Position wizard center-screen on the correct monitor of a
  180. // multi-monitor system. (See MainFrame constructor for more info)
  181. final PointerInfo myPointerInfo = MouseInfo.getPointerInfo();
  182. final GraphicsDevice myDevice = myPointerInfo.getDevice();
  183. final GraphicsConfiguration myGraphicsConfig = myDevice.getDefaultConfiguration();
  184. final Rectangle gcBounds = myGraphicsConfig.getBounds();
  185. final int xPos = gcBounds.x + ((gcBounds.width - getWidth()) / 2);
  186. final int yPos = gcBounds.y + ((gcBounds.height - getHeight()) / 2);
  187. setLocation(xPos, yPos);
  188. }
  189. setResizable(false);
  190. setVisible(true);
  191. }
  192. }
  193. /** {@inheritDoc} */
  194. public void actionPerformed(final ActionEvent e) {
  195. if (e.getSource() == next) {
  196. nextStep();
  197. } else if (e.getSource() == prev) {
  198. prevStep();
  199. }
  200. }
  201. /**
  202. * Adds a step to the wizard.
  203. *
  204. * @param step Step to add
  205. */
  206. public void addStep(final Step step) {
  207. steps.add(step);
  208. }
  209. /**
  210. * Enables or disables the "next step" button.
  211. *
  212. * @param newValue boolean true to make "next" button enabled, else false
  213. */
  214. public void enableNextStep(final boolean newValue) {
  215. next.setEnabled(newValue);
  216. }
  217. /**
  218. * Enables or disables the "previous step" button.
  219. *
  220. * @param newValue boolean true to make "previous" button enabled, else false
  221. */
  222. public void enablePreviousStep(final boolean newValue) {
  223. prev.setEnabled(newValue);
  224. }
  225. /** Moves to the next step. */
  226. private void nextStep() {
  227. steps.get(currentStep).setVisible(false);
  228. if ("Next >>".equals(next.getText())) {
  229. remove(steps.get(currentStep));
  230. add(steps.get(++currentStep), BorderLayout.CENTER);
  231. prev.setEnabled(true);
  232. if (currentStep == steps.size() - 1) {
  233. next.setText("Finish");
  234. }
  235. updateProgressLabel();
  236. wizard.stepChanged(currentStep - 1, currentStep);
  237. } else if ("Finish".equals(next.getText())) {
  238. this.dispose();
  239. wizard.wizardFinished();
  240. }
  241. final Step newstep = steps.get(currentStep);
  242. if (newstep instanceof SpecialStep) {
  243. ((SpecialStep) newstep).showStep();
  244. }
  245. newstep.setVisible(true);
  246. }
  247. /** Moves to the previous step. */
  248. private void prevStep() {
  249. steps.get(currentStep).setVisible(false);
  250. remove(steps.get(currentStep));
  251. add(steps.get(--currentStep), BorderLayout.CENTER);
  252. if (currentStep == 0) {
  253. prev.setEnabled(false);
  254. }
  255. next.setText("Next >>");
  256. steps.get(currentStep).setVisible(true);
  257. updateProgressLabel();
  258. wizard.stepChanged(currentStep + 1, currentStep);
  259. }
  260. /**
  261. * Returns the step at the specified index.
  262. *
  263. * @param stepNumber step number
  264. *
  265. * @return Specified step.
  266. */
  267. public Step getStep(final int stepNumber) {
  268. return steps.get(stepNumber);
  269. }
  270. /** Updates the progress label. */
  271. private void updateProgressLabel() {
  272. progressLabel.setText("Step " + (currentStep + 1) + " of " + steps.size());
  273. }
  274. }