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

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