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.

SwingUpdaterDialog.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.updater;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.PackingTable;
  20. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  21. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  22. import com.dmdirc.addons.ui_swing.injection.DialogModule.ForUpdates;
  23. import com.dmdirc.addons.ui_swing.injection.DialogProvider;
  24. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.updater.UpdateComponent;
  27. import com.dmdirc.updater.manager.CachingUpdateManager;
  28. import com.dmdirc.updater.manager.UpdateManager;
  29. import com.dmdirc.updater.manager.UpdateManagerListener;
  30. import com.dmdirc.updater.manager.UpdateManagerStatus;
  31. import com.dmdirc.updater.manager.UpdateStatus;
  32. import java.awt.Dimension;
  33. import java.awt.Window;
  34. import java.awt.event.ActionEvent;
  35. import java.awt.event.ActionListener;
  36. import java.util.List;
  37. import java.util.stream.Collectors;
  38. import javax.inject.Inject;
  39. import javax.swing.JButton;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JTable;
  42. import javax.swing.ListSelectionModel;
  43. import javax.swing.WindowConstants;
  44. import javax.swing.table.TableCellRenderer;
  45. import net.miginfocom.swing.MigLayout;
  46. /**
  47. * The updater dialog informs the user of the new update that is available, and walks them through
  48. * the process of downloading the update.
  49. */
  50. public class SwingUpdaterDialog extends StandardDialog implements
  51. ActionListener, UpdateManagerListener {
  52. /** Serial version UID. */
  53. private static final long serialVersionUID = 3;
  54. /** The update manager to use. */
  55. private final CachingUpdateManager updateManager;
  56. /** Update table. */
  57. private JTable table;
  58. /** Table scrollpane. */
  59. private JScrollPane scrollPane;
  60. /** The label we use for the dialog header. */
  61. private TextLabel header;
  62. /** UpdateComponent renderer. */
  63. private UpdateComponentTableCellRenderer updateComponentRenderer;
  64. /** Update.Status renderer. */
  65. private UpdateStatusTableCellRenderer updateStatusRenderer;
  66. /** Provider of restart dialogs. */
  67. private final DialogProvider<SwingRestartDialog> restartDialogProvider;
  68. /** The event bus to post errors to. */
  69. private final EventBus eventBus;
  70. /**
  71. * Creates a new instance of the updater dialog.
  72. *
  73. * @param updateManager The update manager to use for information
  74. * @param parentWindow Parent window
  75. * @param restartDialogProvider Provider of restart dialogs.
  76. * @param eventBus The event bus to post errors to
  77. */
  78. @Inject
  79. public SwingUpdaterDialog(
  80. final CachingUpdateManager updateManager,
  81. @MainWindow final Window parentWindow,
  82. @ForUpdates final DialogProvider<SwingRestartDialog> restartDialogProvider,
  83. final EventBus eventBus) {
  84. super(parentWindow, ModalityType.MODELESS);
  85. this.updateManager = updateManager;
  86. this.restartDialogProvider = restartDialogProvider;
  87. this.eventBus = eventBus;
  88. initComponents();
  89. layoutComponents();
  90. updateManager.addUpdateManagerListener(this);
  91. getOkButton().addActionListener(this);
  92. getCancelButton().addActionListener(this);
  93. setTitle("Update available");
  94. setSize(new Dimension(450, 400));
  95. }
  96. /**
  97. * Initialises the components.
  98. */
  99. private void initComponents() {
  100. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  101. updateStatusRenderer = new UpdateStatusTableCellRenderer();
  102. updateComponentRenderer = new UpdateComponentTableCellRenderer();
  103. header = new TextLabel("An update is available for one or more "
  104. + "components of DMDirc:");
  105. final List<UpdateComponent> updates = updateManager.getComponents().stream()
  106. .filter(component -> updateManager.getStatus(component) != UpdateStatus.IDLE
  107. && updateManager.getStatus(component) != UpdateStatus.CHECKING_NOT_PERMITTED)
  108. .collect(Collectors.toList());
  109. scrollPane = new JScrollPane();
  110. table = new PackingTable(new UpdateTableModel(updateManager, updates), scrollPane) {
  111. /** Serialisation version ID. */
  112. private static final long serialVersionUID = 1;
  113. @Override
  114. public TableCellRenderer getCellRenderer(final int row,
  115. final int column) {
  116. switch (column) {
  117. case 1:
  118. return updateComponentRenderer;
  119. case 3:
  120. return updateStatusRenderer;
  121. default:
  122. return super.getCellRenderer(row, column);
  123. }
  124. }
  125. };
  126. table.setAutoCreateRowSorter(true);
  127. table.setAutoCreateColumnsFromModel(true);
  128. table.setColumnSelectionAllowed(false);
  129. table.setCellSelectionEnabled(false);
  130. table.setFillsViewportHeight(false);
  131. table.setRowSelectionAllowed(true);
  132. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  133. table.getRowSorter().toggleSortOrder(0);
  134. scrollPane.setViewportView(table);
  135. orderButtons(new JButton(), new JButton());
  136. }
  137. /**
  138. * Lays out the components.
  139. */
  140. private void layoutComponents() {
  141. setLayout(new MigLayout("fill, wmin 450, hmin 400, wmax 450, hmax 400, "
  142. + "hidemode 3"));
  143. add(header, "wrap 1.5*unrel, growx, pushx");
  144. add(scrollPane, "grow, push, wrap");
  145. add(getLeftButton(), "split 2, right");
  146. add(getRightButton(), "right");
  147. }
  148. @Override
  149. public void actionPerformed(final ActionEvent e) {
  150. if (e.getSource().equals(getOkButton())) {
  151. getOkButton().setEnabled(false);
  152. getCancelButton().setVisible(false);
  153. header.setText("DMDirc is updating the following components:");
  154. UIUtilities.invokeOffEDT(
  155. () -> ((UpdateTableModel) table.getModel()).getUpdates().stream()
  156. .filter(((UpdateTableModel) table.getModel())::isEnabled)
  157. .forEach(updateManager::install));
  158. if (updateManager.getManagerStatus() == UpdateManagerStatus.IDLE_RESTART_NEEDED) {
  159. restartDialogProvider.displayOrRequestFocus();
  160. dispose();
  161. }
  162. } else if (e.getSource().equals(getCancelButton())) {
  163. dispose();
  164. }
  165. }
  166. @Override
  167. public boolean enterPressed() {
  168. executeAction(getOkButton());
  169. return true;
  170. }
  171. @Override
  172. public void updateManagerStatusChanged(final UpdateManager manager,
  173. final UpdateManagerStatus status) {
  174. UIUtilities.invokeLater(() -> {
  175. getOkButton().setEnabled(status != UpdateManagerStatus.WORKING);
  176. if (status == UpdateManagerStatus.IDLE_RESTART_NEEDED) {
  177. if (isVisible()) {
  178. restartDialogProvider.displayOrRequestFocus();
  179. }
  180. dispose();
  181. } else {
  182. getCancelButton().setVisible(true);
  183. }
  184. });
  185. }
  186. }