Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UpdateConfigPanel.java 7.8KB

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