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.

UpdateConfigPanel.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.prefs;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.ClientModule.UserConfig;
  25. import com.dmdirc.DMDircMBassador;
  26. import com.dmdirc.addons.ui_swing.components.GenericTableModel;
  27. import com.dmdirc.addons.ui_swing.components.PackingTable;
  28. import com.dmdirc.config.prefs.PreferencesInterface;
  29. import com.dmdirc.events.UserErrorEvent;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.interfaces.config.ConfigChangeListener;
  32. import com.dmdirc.interfaces.config.ConfigProvider;
  33. import com.dmdirc.interfaces.config.IdentityController;
  34. import com.dmdirc.logger.ErrorLevel;
  35. import com.dmdirc.updater.UpdateChannel;
  36. import com.dmdirc.updater.UpdateChecker;
  37. import com.dmdirc.updater.UpdateComponent;
  38. import com.dmdirc.updater.manager.CachingUpdateManager;
  39. import com.dmdirc.updater.manager.UpdateStatus;
  40. import java.awt.event.ActionEvent;
  41. import java.awt.event.ActionListener;
  42. import javax.inject.Inject;
  43. import javax.swing.DefaultComboBoxModel;
  44. import javax.swing.JButton;
  45. import javax.swing.JCheckBox;
  46. import javax.swing.JComboBox;
  47. import javax.swing.JLabel;
  48. import javax.swing.JPanel;
  49. import javax.swing.JScrollPane;
  50. import net.miginfocom.swing.MigLayout;
  51. /**
  52. * Updates configuration UI.
  53. */
  54. public class UpdateConfigPanel extends JPanel implements ActionListener,
  55. PreferencesInterface, ConfigChangeListener {
  56. /** A version number for this class. */
  57. private static final long serialVersionUID = 1;
  58. /** Global checkbox. */
  59. private JCheckBox enable;
  60. /** Table scroll pane, */
  61. private JScrollPane scrollPane;
  62. /** Component table model. */
  63. private GenericTableModel<UpdateComponentHolder> tableModel;
  64. /** Check now button. */
  65. private JButton checkNow;
  66. /** Update channel. */
  67. private JComboBox<UpdateChannel> updateChannel;
  68. /** The configuration to write settings changes to. */
  69. private final ConfigProvider userConfig;
  70. /** The configuration to read global settings from. */
  71. private final AggregateConfigProvider globalConfig;
  72. /** The manager to read update information from. */
  73. private final CachingUpdateManager updateManager;
  74. /** Controller to pass to the update checker. */
  75. private final IdentityController identityController;
  76. /** The event bus to post errors to. */
  77. private final DMDircMBassador eventBus;
  78. /**
  79. * Instantiates a new update config panel.
  80. *
  81. * @param userConfig The configuration to write settings changes to.
  82. * @param globalConfig The configuration to read global settings from.
  83. * @param updateManager The manager to read update information from.
  84. * @param identityController Controller to pass to the update checker.
  85. * @param eventBus The event bus to post the errors to
  86. */
  87. @Inject
  88. public UpdateConfigPanel(
  89. @UserConfig final ConfigProvider userConfig,
  90. @GlobalConfig final AggregateConfigProvider globalConfig,
  91. final CachingUpdateManager updateManager,
  92. final IdentityController identityController,
  93. final DMDircMBassador eventBus) {
  94. this.userConfig = userConfig;
  95. this.globalConfig = globalConfig;
  96. this.updateManager = updateManager;
  97. this.identityController = identityController;
  98. this.eventBus = eventBus;
  99. initComponents();
  100. loadModel();
  101. addListeners();
  102. layoutComponents();
  103. }
  104. @Override
  105. public void save() {
  106. userConfig.setOption("updater", "enable", enable.isSelected());
  107. userConfig.setOption("updater", "channel", updateChannel.getSelectedItem().toString());
  108. for (int i = 0; i < tableModel.getRowCount(); i++) {
  109. final String componentName = tableModel.getValue(i).getComponent().getName();
  110. if (tableModel.getValue(i).isEnabled()) {
  111. userConfig.unsetOption("updater", "enable-" + componentName);
  112. } else {
  113. userConfig.setOption("updater", "enable-" + componentName, false);
  114. }
  115. }
  116. }
  117. private void loadModel() {
  118. updateManager.getComponents().stream()
  119. .map(this::createUpdateComponentHolder)
  120. .forEach(tableModel::addValue);
  121. }
  122. @SuppressWarnings("TypeMayBeWeakened")
  123. private UpdateComponentHolder createUpdateComponentHolder(final UpdateComponent component) {
  124. return new UpdateComponentHolder(component,
  125. updateManager.getStatus(component) != UpdateStatus.CHECKING_NOT_PERMITTED,
  126. component.getVersion().toString());
  127. }
  128. /**
  129. * Initialises the components.
  130. */
  131. private void initComponents() {
  132. enable = new JCheckBox();
  133. scrollPane = new JScrollPane();
  134. tableModel = new GenericTableModel<>(UpdateComponentHolder.class,
  135. (row, column) -> column == 1,
  136. (v, row, column) -> {
  137. if (column == 1) {
  138. tableModel.getValue(row).setEnabled((Boolean) v);
  139. }
  140. },
  141. "getComponentName", "isEnabled", "getVersion");
  142. tableModel.setHeaderNames("Component", "Enabled?", "Version");
  143. final PackingTable table = new PackingTable(tableModel, scrollPane);
  144. checkNow = new JButton("Check now");
  145. checkNow.setEnabled(globalConfig.getOptionBool("updater", "enable"));
  146. updateChannel = new JComboBox<>(new DefaultComboBoxModel<>(UpdateChannel.values()));
  147. enable.setSelected(globalConfig.getOptionBool("updater", "enable"));
  148. UpdateChannel channel = UpdateChannel.NONE;
  149. try {
  150. channel = UpdateChannel.valueOf(globalConfig.getOption("updater", "channel"));
  151. } catch (IllegalArgumentException e) {
  152. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, e,
  153. "Invalid setting for update channel, defaulting to none.", ""));
  154. }
  155. updateChannel.setSelectedItem(channel);
  156. scrollPane.setViewportView(table);
  157. }
  158. /**
  159. * Adds the listeners.
  160. */
  161. private void addListeners() {
  162. checkNow.addActionListener(this);
  163. globalConfig.addChangeListener("updater", "enable", this);
  164. enable.addActionListener(this);
  165. }
  166. /**
  167. * Lays out the components.
  168. */
  169. private void layoutComponents() {
  170. setLayout(new MigLayout("fill, ins 0, hmax 500"));
  171. add(new JLabel("Update checking:"), "split");
  172. add(enable, "growx");
  173. add(updateChannel, "growx, pushx, wrap");
  174. add(scrollPane, "wrap, grow, push");
  175. add(checkNow, "right");
  176. }
  177. @Override
  178. public void actionPerformed(final ActionEvent e) {
  179. if (enable == e.getSource()) {
  180. checkNow.setEnabled(enable.isSelected());
  181. } else {
  182. UpdateChecker.checkNow(updateManager, identityController);
  183. }
  184. }
  185. @Override
  186. public void configChanged(final String domain, final String key) {
  187. checkNow.setEnabled(globalConfig.getOptionBool("updater", "enable"));
  188. }
  189. }