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

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