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.

PluginPanel.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2006-2014 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.components.addonpanel;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.ClientModule.UserConfig;
  25. import com.dmdirc.DMDircMBassador;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import com.dmdirc.addons.ui_swing.components.addonbrowser.DataLoaderWorkerFactory;
  28. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  29. import com.dmdirc.events.PluginRefreshEvent;
  30. import com.dmdirc.interfaces.config.ConfigProvider;
  31. import com.dmdirc.plugins.PluginInfo;
  32. import com.dmdirc.plugins.PluginManager;
  33. import com.dmdirc.ui.IconManager;
  34. import com.dmdirc.updater.manager.CachingUpdateManager;
  35. import java.awt.Window;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.List;
  39. import javax.inject.Inject;
  40. import javax.swing.JTable;
  41. import javax.swing.table.DefaultTableModel;
  42. import net.engio.mbassy.listener.Handler;
  43. /**
  44. * Lists known plugins, enabling the end user to enable/disable these as well as download new ones.
  45. */
  46. public class PluginPanel extends AddonPanel {
  47. /** A version number for this class. */
  48. private static final long serialVersionUID = 1;
  49. /** Manager to retrieve plugin information from. */
  50. private final PluginManager pluginManager;
  51. /** Manager to use to retrieve addon-related icons. */
  52. private final IconManager iconManager;
  53. /** Manager to use to retrieve update information. */
  54. private final CachingUpdateManager updateManager;
  55. /** Configuration to write update-related settings to. */
  56. private final ConfigProvider userConfig;
  57. /**
  58. * Creates a new instance of PluginPanel.
  59. *
  60. * @param eventBus Event bus to subscribe to events on
  61. * @param parentWindow Parent window
  62. * @param pluginManager Manager to retrieve plugins from.
  63. * @param workerFactory Factory to use to create data workers.
  64. * @param iconManager Manager to use to retrieve addon-related icons.
  65. * @param updateManager Manager to use to retrieve update information.
  66. * @param userConfig Configuration to write update-related settings to.
  67. */
  68. @Inject
  69. public PluginPanel(
  70. final DMDircMBassador eventBus,
  71. @MainWindow final Window parentWindow,
  72. final PluginManager pluginManager,
  73. final DataLoaderWorkerFactory workerFactory,
  74. @GlobalConfig final IconManager iconManager,
  75. final CachingUpdateManager updateManager,
  76. @UserConfig final ConfigProvider userConfig) {
  77. super(parentWindow, workerFactory, eventBus);
  78. this.pluginManager = pluginManager;
  79. this.iconManager = iconManager;
  80. this.updateManager = updateManager;
  81. this.userConfig = userConfig;
  82. eventBus.subscribe(this);
  83. pluginManager.refreshPlugins();
  84. load();
  85. }
  86. @Override
  87. protected JTable populateList(final JTable table) {
  88. final List<PluginInfo> list = new ArrayList<>();
  89. final List<PluginInfo> sortedList = new ArrayList<>();
  90. list.addAll(pluginManager.getPluginInfos());
  91. Collections.sort(list);
  92. for (final PluginInfo plugin : list) {
  93. if (plugin.getMetaData().getParent() == null) {
  94. final List<PluginInfo> childList = new ArrayList<>();
  95. sortedList.add(plugin);
  96. for (final PluginInfo child : plugin.getChildren()) {
  97. if (!childList.contains(child)) {
  98. childList.add(child);
  99. }
  100. }
  101. Collections.sort(childList);
  102. sortedList.addAll(childList);
  103. }
  104. }
  105. UIUtilities.invokeLater(() -> {
  106. ((DefaultTableModel) table.getModel()).setNumRows(0);
  107. for (final PluginInfo plugin : sortedList) {
  108. ((DefaultTableModel) table.getModel()).addRow(
  109. new AddonCell[]{
  110. new AddonCell(
  111. new AddonToggle(
  112. updateManager,
  113. userConfig,
  114. pluginManager,
  115. plugin),
  116. iconManager),});
  117. }
  118. table.repaint();
  119. });
  120. return table;
  121. }
  122. @Handler
  123. public void handlePluginRefresh(final PluginRefreshEvent event) {
  124. populateList(addonList);
  125. }
  126. @Override
  127. protected String getTypeName() {
  128. return "plugins";
  129. }
  130. }