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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.SwingController;
  24. import com.dmdirc.config.ConfigManager;
  25. import com.dmdirc.config.Identity;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.config.prefs.PreferencesInterface;
  28. import com.dmdirc.addons.ui_swing.components.PackingTable;
  29. import com.dmdirc.interfaces.ConfigChangeListener;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.updater.UpdateChannel;
  33. import com.dmdirc.updater.UpdateChecker;
  34. import java.awt.event.ActionEvent;
  35. import java.awt.event.ActionListener;
  36. import javax.swing.DefaultComboBoxModel;
  37. import javax.swing.JButton;
  38. import javax.swing.JCheckBox;
  39. import javax.swing.JComboBox;
  40. import javax.swing.JLabel;
  41. import javax.swing.JPanel;
  42. import javax.swing.JScrollPane;
  43. import net.miginfocom.swing.MigLayout;
  44. /**
  45. * Updates configuration UI.
  46. */
  47. public class UpdateConfigPanel extends JPanel implements ActionListener,
  48. PreferencesInterface, ConfigChangeListener {
  49. /**
  50. * A version number for this class. It should be changed whenever the class
  51. * structure is changed (or anything else that would prevent serialized
  52. * objects being unserialized with the new class).
  53. */
  54. private static final long serialVersionUID = 1;
  55. /** Global checkbox. */
  56. private JCheckBox enable;
  57. /** Table scroll pane, */
  58. private JScrollPane scrollPane;
  59. /** Component table model. */
  60. private UpdateTableModel tableModel;
  61. /** Component table. */
  62. private PackingTable table;
  63. /** Check now button. */
  64. private JButton checkNow;
  65. /** Update channel. */
  66. private JComboBox updateChannel;
  67. /** Swing controller. */
  68. private SwingController controller;
  69. /**
  70. * Instantiates a new update config panel.
  71. *
  72. * @param controller Swing controller
  73. */
  74. public UpdateConfigPanel(final SwingController controller) {
  75. this.controller = controller;
  76. initComponents();
  77. addListeners();
  78. layoutComponents();
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public void save() {
  83. final Identity identity = IdentityManager.getConfigIdentity();
  84. identity.setOption("updater", "enable", enable.isSelected());
  85. IdentityManager.getConfigIdentity().setOption("updater", "channel",
  86. updateChannel.getSelectedItem().toString());
  87. for (int i = 0; i < tableModel.getRowCount(); i++) {
  88. final String componentName = tableModel.getComponent(i).getName();
  89. if ((Boolean) tableModel.getValueAt(i, 1)) {
  90. identity.unsetOption("updater", "enable-" + componentName);
  91. } else {
  92. identity.setOption("updater", "enable-" + componentName, false);
  93. }
  94. }
  95. }
  96. /**
  97. * Initialises the components.
  98. */
  99. private void initComponents() {
  100. final ConfigManager config = IdentityManager.getGlobalConfig();
  101. enable = new JCheckBox();
  102. scrollPane = new JScrollPane();
  103. tableModel = new UpdateTableModel(UpdateChecker.getComponents());
  104. table = new PackingTable(tableModel, false, scrollPane);
  105. checkNow = new JButton("Check now");
  106. checkNow.setEnabled(IdentityManager.getGlobalConfig().getOptionBool(
  107. "updater", "enable"));
  108. updateChannel = new JComboBox(new DefaultComboBoxModel(UpdateChannel.
  109. values()));
  110. enable.setSelected(config.getOptionBool("updater", "enable"));
  111. UpdateChannel channel = UpdateChannel.NONE;
  112. try {
  113. channel = UpdateChannel.valueOf(
  114. config.getOption("updater", "channel"));
  115. } catch (IllegalArgumentException e) {
  116. Logger.userError(ErrorLevel.LOW, "Invalid setting for update "
  117. + "channel, defaulting to none.");
  118. }
  119. updateChannel.setSelectedItem(channel);
  120. scrollPane.setViewportView(table);
  121. }
  122. /**
  123. * Adds the listeners.
  124. */
  125. private void addListeners() {
  126. checkNow.addActionListener(this);
  127. IdentityManager.getGlobalConfig().addChangeListener("updater",
  128. "enable", this);
  129. enable.addActionListener(this);
  130. }
  131. /**
  132. * Lays out the components.
  133. */
  134. private void layoutComponents() {
  135. setLayout(new MigLayout("fill, ins 0, hmax " + controller.getPrefsDialog().
  136. getPanelHeight()));
  137. add(new JLabel("Update checking:"), "split");
  138. add(enable, "growx");
  139. add(updateChannel, "growx, pushx, wrap");
  140. add(scrollPane, "wrap, grow, push");
  141. add(checkNow, "right");
  142. }
  143. /**
  144. * {@inheritDoc}
  145. *
  146. * @param e Action event
  147. */
  148. @Override
  149. public void actionPerformed(final ActionEvent e) {
  150. if (enable == e.getSource()) {
  151. checkNow.setEnabled(enable.isSelected());
  152. } else {
  153. UpdateChecker.checkNow();
  154. }
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public void configChanged(final String domain, final String key) {
  159. checkNow.setEnabled(IdentityManager.getGlobalConfig().getOptionBool(
  160. "updater", "enable"));
  161. }
  162. }