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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2006-2015 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.dialogs.updater;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.PackingTable;
  25. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  26. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  27. import com.dmdirc.addons.ui_swing.injection.DialogModule.ForUpdates;
  28. import com.dmdirc.addons.ui_swing.injection.DialogProvider;
  29. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  30. import com.dmdirc.interfaces.EventBus;
  31. import com.dmdirc.updater.UpdateComponent;
  32. import com.dmdirc.updater.manager.CachingUpdateManager;
  33. import com.dmdirc.updater.manager.UpdateManager;
  34. import com.dmdirc.updater.manager.UpdateManagerListener;
  35. import com.dmdirc.updater.manager.UpdateManagerStatus;
  36. import com.dmdirc.updater.manager.UpdateStatus;
  37. import java.awt.Dimension;
  38. import java.awt.Window;
  39. import java.awt.event.ActionEvent;
  40. import java.awt.event.ActionListener;
  41. import java.util.List;
  42. import java.util.stream.Collectors;
  43. import javax.inject.Inject;
  44. import javax.swing.JButton;
  45. import javax.swing.JScrollPane;
  46. import javax.swing.JTable;
  47. import javax.swing.ListSelectionModel;
  48. import javax.swing.WindowConstants;
  49. import javax.swing.table.TableCellRenderer;
  50. import net.miginfocom.swing.MigLayout;
  51. /**
  52. * The updater dialog informs the user of the new update that is available, and walks them through
  53. * the process of downloading the update.
  54. */
  55. public class SwingUpdaterDialog extends StandardDialog implements
  56. ActionListener, UpdateManagerListener {
  57. /** Serial version UID. */
  58. private static final long serialVersionUID = 3;
  59. /** The update manager to use. */
  60. private final CachingUpdateManager updateManager;
  61. /** Update table. */
  62. private JTable table;
  63. /** Table scrollpane. */
  64. private JScrollPane scrollPane;
  65. /** The label we use for the dialog header. */
  66. private TextLabel header;
  67. /** UpdateComponent renderer. */
  68. private UpdateComponentTableCellRenderer updateComponentRenderer;
  69. /** Update.Status renderer. */
  70. private UpdateStatusTableCellRenderer updateStatusRenderer;
  71. /** Provider of restart dialogs. */
  72. private final DialogProvider<SwingRestartDialog> restartDialogProvider;
  73. /** The event bus to post errors to. */
  74. private final EventBus eventBus;
  75. /**
  76. * Creates a new instance of the updater dialog.
  77. *
  78. * @param updateManager The update manager to use for information
  79. * @param parentWindow Parent window
  80. * @param restartDialogProvider Provider of restart dialogs.
  81. * @param eventBus The event bus to post errors to
  82. */
  83. @Inject
  84. public SwingUpdaterDialog(
  85. final CachingUpdateManager updateManager,
  86. @MainWindow final Window parentWindow,
  87. @ForUpdates final DialogProvider<SwingRestartDialog> restartDialogProvider,
  88. final EventBus eventBus) {
  89. super(parentWindow, ModalityType.MODELESS);
  90. this.updateManager = updateManager;
  91. this.restartDialogProvider = restartDialogProvider;
  92. this.eventBus = eventBus;
  93. initComponents();
  94. layoutComponents();
  95. updateManager.addUpdateManagerListener(this);
  96. getOkButton().addActionListener(this);
  97. getCancelButton().addActionListener(this);
  98. setTitle("Update available");
  99. setSize(new Dimension(450, 400));
  100. }
  101. /**
  102. * Initialises the components.
  103. */
  104. private void initComponents() {
  105. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  106. updateStatusRenderer = new UpdateStatusTableCellRenderer();
  107. updateComponentRenderer = new UpdateComponentTableCellRenderer();
  108. header = new TextLabel("An update is available for one or more "
  109. + "components of DMDirc:");
  110. final List<UpdateComponent> updates = updateManager.getComponents().stream()
  111. .filter(component -> updateManager.getStatus(component) != UpdateStatus.IDLE
  112. && updateManager.getStatus(component) != UpdateStatus.CHECKING_NOT_PERMITTED)
  113. .collect(Collectors.toList());
  114. scrollPane = new JScrollPane();
  115. table = new PackingTable(new UpdateTableModel(updateManager, updates), scrollPane) {
  116. /** Serialisation version ID. */
  117. private static final long serialVersionUID = 1;
  118. @Override
  119. public TableCellRenderer getCellRenderer(final int row,
  120. final int column) {
  121. switch (column) {
  122. case 1:
  123. return updateComponentRenderer;
  124. case 3:
  125. return updateStatusRenderer;
  126. default:
  127. return super.getCellRenderer(row, column);
  128. }
  129. }
  130. };
  131. table.setAutoCreateRowSorter(true);
  132. table.setAutoCreateColumnsFromModel(true);
  133. table.setColumnSelectionAllowed(false);
  134. table.setCellSelectionEnabled(false);
  135. table.setFillsViewportHeight(false);
  136. table.setRowSelectionAllowed(true);
  137. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  138. table.getRowSorter().toggleSortOrder(0);
  139. scrollPane.setViewportView(table);
  140. orderButtons(new JButton(), new JButton());
  141. }
  142. /**
  143. * Lays out the components.
  144. */
  145. private void layoutComponents() {
  146. setLayout(new MigLayout("fill, wmin 450, hmin 400, wmax 450, hmax 400, "
  147. + "hidemode 3"));
  148. add(header, "wrap 1.5*unrel, growx, pushx");
  149. add(scrollPane, "grow, push, wrap");
  150. add(getLeftButton(), "split 2, right");
  151. add(getRightButton(), "right");
  152. }
  153. @Override
  154. public void actionPerformed(final ActionEvent e) {
  155. if (e.getSource().equals(getOkButton())) {
  156. getOkButton().setEnabled(false);
  157. getCancelButton().setVisible(false);
  158. header.setText("DMDirc is updating the following components:");
  159. UIUtilities.invokeOffEDT(
  160. () -> ((UpdateTableModel) table.getModel()).getUpdates().stream()
  161. .filter(((UpdateTableModel) table.getModel())::isEnabled)
  162. .forEach(updateManager::install));
  163. if (updateManager.getManagerStatus() == UpdateManagerStatus.IDLE_RESTART_NEEDED) {
  164. restartDialogProvider.displayOrRequestFocus();
  165. dispose();
  166. }
  167. } else if (e.getSource().equals(getCancelButton())) {
  168. dispose();
  169. }
  170. }
  171. @Override
  172. public boolean enterPressed() {
  173. executeAction(getOkButton());
  174. return true;
  175. }
  176. @Override
  177. public void updateManagerStatusChanged(final UpdateManager manager,
  178. final UpdateManagerStatus status) {
  179. UIUtilities.invokeLater(() -> {
  180. getOkButton().setEnabled(status != UpdateManagerStatus.WORKING);
  181. if (status == UpdateManagerStatus.IDLE_RESTART_NEEDED) {
  182. if (isVisible()) {
  183. restartDialogProvider.displayOrRequestFocus();
  184. }
  185. dispose();
  186. } else {
  187. getCancelButton().setVisible(true);
  188. }
  189. });
  190. }
  191. }