Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InstallerDialog.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. *
  3. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.installer.ui;
  24. import com.dmdirc.installer.Step;
  25. import com.dmdirc.installer.StepListener;
  26. import com.dmdirc.installer.WizardListener;
  27. import com.dmdirc.ui.CoreUIUtils;
  28. import com.dmdirc.util.ListenerList;
  29. import java.awt.BorderLayout;
  30. import java.awt.Dimension;
  31. import java.awt.Toolkit;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.event.WindowAdapter;
  35. import java.awt.event.WindowEvent;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import javax.swing.BorderFactory;
  39. import javax.swing.JFrame;
  40. import javax.swing.JOptionPane;
  41. import javax.swing.UIManager;
  42. import javax.swing.UnsupportedLookAndFeelException;
  43. /**
  44. * Installer dialog, showing the steps required to install DMDirc.
  45. */
  46. public class InstallerDialog extends JFrame implements ActionListener {
  47. private static final long serialVersionUID = -2001827768443747849L;
  48. private final TitlePanel title;
  49. private final WizardPanel wizard;
  50. private final WizardControlPanel control;
  51. private final ListenerList listeners;
  52. /** Small UI Gap. */
  53. public static final int SMALL_GAP = 5;
  54. /**
  55. * Instantiates a new installer dialog
  56. *
  57. * @param dialogTitle
  58. */
  59. public InstallerDialog(final String dialogTitle) {
  60. super(dialogTitle);
  61. title = new TitlePanel(null);
  62. wizard = new WizardPanel(this);
  63. control = new WizardControlPanel();
  64. listeners = new ListenerList();
  65. setLayout(new BorderLayout(SMALL_GAP, SMALL_GAP));
  66. title.setBorder(BorderFactory.createCompoundBorder(
  67. BorderFactory.createEmptyBorder(SMALL_GAP, SMALL_GAP, SMALL_GAP,
  68. SMALL_GAP), title.getBorder()));
  69. wizard.setBorder(BorderFactory.createCompoundBorder(
  70. BorderFactory.createEmptyBorder(SMALL_GAP, SMALL_GAP, SMALL_GAP,
  71. SMALL_GAP), wizard.getBorder()));
  72. control.setBorder(BorderFactory.createCompoundBorder(
  73. BorderFactory.createEmptyBorder(SMALL_GAP, SMALL_GAP, SMALL_GAP,
  74. SMALL_GAP), control.getBorder()));
  75. add(title, BorderLayout.NORTH);
  76. add(wizard, BorderLayout.CENTER);
  77. add(control, BorderLayout.SOUTH);
  78. setIconImage(Toolkit.getDefaultToolkit().createImage(Thread.
  79. currentThread().getContextClassLoader().getResource(
  80. "com/dmdirc/res/icon.png")));
  81. setPreferredSize(new Dimension(400, 350));
  82. setMaximumSize(new Dimension(400, 350));
  83. control.getPrevButton().addActionListener(this);
  84. control.getNextButton().addActionListener(this);
  85. }
  86. /**
  87. * Adds a step.
  88. *
  89. * @param step Step to add
  90. */
  91. public void addStep(final SwingStep step) {
  92. wizard.addStep(step);
  93. }
  94. /**
  95. * Displays the installer.
  96. */
  97. public void display() {
  98. wizard.display();
  99. title.setStep(wizard.getCurrentStep());
  100. control.setTotal(wizard.getTotalSteps());
  101. control.setProgress(wizard.getCurrentStepIndex());
  102. addWindowListener(new WindowAdapter() {
  103. /** {@inheritDoc} */
  104. @Override
  105. public void windowClosing(final WindowEvent e) {
  106. fireWizardCancelled();
  107. }
  108. });
  109. pack();
  110. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  111. CoreUIUtils.centreWindow(this);
  112. setVisible(true);
  113. fireStepAboutToBeDisplayed(wizard.getStep(wizard.getCurrentStepIndex()));
  114. }
  115. /**
  116. * Displays the installer with these steps added.
  117. *
  118. * @param steps Steps to add
  119. */
  120. public void display(final List<Step> steps) {
  121. final List<SwingStep> swingSteps = new ArrayList<SwingStep>();
  122. for (Step step : steps) {
  123. if (step instanceof SwingStep) {
  124. swingSteps.add((SwingStep) step);
  125. }
  126. }
  127. display();
  128. }
  129. /**
  130. * Enables the next step.
  131. *
  132. * @param enable true to enable false to disable
  133. */
  134. public void enableNextStep(final boolean enable) {
  135. control.getNextButton().setEnabled(enable);
  136. }
  137. /**
  138. * Enables the previous step.
  139. *
  140. * @param enable true to enable false to disable
  141. */
  142. public void enablePreviousStep(final boolean enable) {
  143. control.getPrevButton().setEnabled(enable);
  144. }
  145. /**
  146. * shows the cancel confirmation.
  147. *
  148. * @return true if confirmed
  149. */
  150. public boolean showCancelConfirmation() {
  151. return JOptionPane.showConfirmDialog(this,
  152. "Are you sure you want to cancel?",
  153. "Cancel confirmation",
  154. JOptionPane.YES_NO_OPTION,
  155. JOptionPane.WARNING_MESSAGE) ==
  156. JOptionPane.YES_OPTION;
  157. }
  158. /**
  159. * Gets the specified step for the installer.
  160. *
  161. * @param step Index of the step
  162. *
  163. * @return Requested step
  164. */
  165. public Step getStep(final int step) {
  166. return wizard.getStep(step);
  167. }
  168. /**
  169. * Gets the specified step for the installer.
  170. *
  171. * @param name Name of the step
  172. *
  173. * @return Requested step
  174. */
  175. public Step getStep(final String name) {
  176. return wizard.getStep(name);
  177. }
  178. /**
  179. * Returns the current step.
  180. *
  181. * @return Current step
  182. */
  183. public Step getCurrentStep() {
  184. return wizard.getCurrentStep();
  185. }
  186. /**
  187. * Returns the index of the current step.
  188. *
  189. * @return Current step's index
  190. */
  191. public int getCurrentStepIndex() {
  192. return wizard.getCurrentStepIndex();
  193. }
  194. /**
  195. * Returns the name of the current step.
  196. *
  197. * @return Current step's name
  198. */
  199. public String getCurrentStepName() {
  200. return wizard.getCurrentStepName();
  201. }
  202. /**
  203. * Informs listeners a step is about to be displayed.
  204. *
  205. * @param step Step to be displayed
  206. */
  207. void fireStepAboutToBeDisplayed(final Step step) {
  208. for (StepListener listener : listeners.get(StepListener.class)) {
  209. listener.stepAboutToDisplay(step);
  210. }
  211. }
  212. /**
  213. * Informs listeners a step is about to be hidden.
  214. *
  215. * @param step Step to be hidden
  216. */
  217. void fireStepHidden(final Step step) {
  218. for (StepListener listener : listeners.get(StepListener.class)) {
  219. listener.stepHidden(step);
  220. }
  221. }
  222. /**
  223. * Adds a step listener to this installer.
  224. *
  225. * @param listener Listener to add
  226. */
  227. public void addStepListener(final StepListener listener) {
  228. listeners.add(StepListener.class, listener);
  229. }
  230. /**
  231. * Removes a step listener from this installer.
  232. *
  233. * @param listener Listener to remove
  234. */
  235. public void removeStepListener(final StepListener listener) {
  236. listeners.remove(StepListener.class, listener);
  237. }
  238. /**
  239. * Informs listeners this installer has been cancelled.
  240. */
  241. void fireWizardCancelled() {
  242. for (WizardListener listener : listeners.get(WizardListener.class)) {
  243. listener.wizardCancelled();
  244. }
  245. }
  246. /**
  247. * Informs listeners this installer has been completed.
  248. */
  249. void fireWizardFinished() {
  250. for (WizardListener listener : listeners.get(WizardListener.class)) {
  251. listener.wizardFinished();
  252. }
  253. }
  254. /**
  255. * Adds a wizard listeners to this installer.
  256. *
  257. * @param listener Listener to add
  258. */
  259. public void addWizardListener(final WizardListener listener) {
  260. listeners.add(WizardListener.class, listener);
  261. }
  262. /**
  263. * Removes a wizard listener from this installer
  264. *
  265. * @param listener Listener to remove
  266. */
  267. public void removeWizardListener(final WizardListener listener) {
  268. listeners.remove(WizardListener.class, listener);
  269. }
  270. /**
  271. * Initialises any settings required by this UI (this is always called
  272. * before any aspect of the UI is instansiated).
  273. *
  274. * @throws UnsupportedOperationException If unable to switch to the system
  275. * look and feel
  276. */
  277. public static void initUISettings() {
  278. try {
  279. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  280. } catch (InstantiationException ex) {
  281. throw new UnsupportedOperationException("Unable to switch to the " +
  282. "system look and feel", ex);
  283. } catch (ClassNotFoundException ex) {
  284. throw new UnsupportedOperationException("Unable to switch to the " +
  285. "system look and feel", ex);
  286. } catch (UnsupportedLookAndFeelException ex) {
  287. throw new UnsupportedOperationException("Unable to switch to the " +
  288. "system look and feel", ex);
  289. } catch (IllegalAccessException ex) {
  290. throw new UnsupportedOperationException("Unable to switch to the " +
  291. "system look and feel", ex);
  292. }
  293. UIManager.put("swing.useSystemFontSettings", true);
  294. UIManager.put("swing.boldMetal", false);
  295. }
  296. /**
  297. * {@inheritDoc}
  298. *
  299. * @param e Action performed
  300. */
  301. @Override
  302. public void actionPerformed(final ActionEvent e) {
  303. final int currentStep = wizard.getCurrentStepIndex();
  304. Step hiddenStep = null;
  305. Step shownStep = null;
  306. if (e.getSource() == control.getPrevButton()) {
  307. wizard.previousStep();
  308. hiddenStep = wizard.getStep(currentStep);
  309. shownStep = wizard.getStep(currentStep - 1);
  310. } else if (e.getSource() == control.getNextButton()) {
  311. if ("Finish".equals(control.getNextButton().getText())) {
  312. fireWizardFinished();
  313. shownStep = wizard.getStep(currentStep);
  314. dispose();
  315. } else {
  316. wizard.nextStep();
  317. hiddenStep = wizard.getStep(currentStep);
  318. shownStep = wizard.getStep(currentStep + 1);
  319. }
  320. } else {
  321. return;
  322. }
  323. title.setStep(shownStep);
  324. if (shownStep != null) {
  325. fireStepAboutToBeDisplayed(shownStep);
  326. }
  327. if (hiddenStep != null) {
  328. fireStepHidden(hiddenStep);
  329. }
  330. control.setProgress(wizard.getCurrentStepIndex());
  331. }
  332. }