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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.components.addonpanel;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.IconManager;
  25. import com.dmdirc.addons.ui_swing.components.addonbrowser.DataLoaderWorkerFactory;
  26. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  27. import com.dmdirc.config.UserConfig;
  28. import com.dmdirc.events.PluginRefreshEvent;
  29. import com.dmdirc.interfaces.EventBus;
  30. import com.dmdirc.interfaces.config.ConfigProvider;
  31. import com.dmdirc.plugins.PluginInfo;
  32. import com.dmdirc.plugins.PluginManager;
  33. import com.dmdirc.updater.manager.CachingUpdateManager;
  34. import java.awt.Window;
  35. import java.util.ArrayList;
  36. import java.util.Collection;
  37. import java.util.Comparator;
  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. /** Comparator to use to sort plugins in the panel. */
  50. private static final Comparator<? super PluginInfo> COMPARATOR = new PluginInfoComparator();
  51. /** Manager to retrieve plugin information from. */
  52. private final PluginManager pluginManager;
  53. /** Manager to use to retrieve addon-related icons. */
  54. private final IconManager iconManager;
  55. /** Manager to use to retrieve update information. */
  56. private final CachingUpdateManager updateManager;
  57. /** Configuration to write update-related settings to. */
  58. private final ConfigProvider userConfig;
  59. /**
  60. * Creates a new instance of PluginPanel.
  61. *
  62. * @param eventBus Event bus to subscribe to events on
  63. * @param parentWindow Parent window
  64. * @param pluginManager Manager to retrieve plugins from.
  65. * @param workerFactory Factory to use to create data workers.
  66. * @param iconManager Manager to use to retrieve addon-related icons.
  67. * @param updateManager Manager to use to retrieve update information.
  68. * @param userConfig Configuration to write update-related settings to.
  69. */
  70. @Inject
  71. public PluginPanel(
  72. final EventBus eventBus,
  73. @MainWindow final Window parentWindow,
  74. final PluginManager pluginManager,
  75. final DataLoaderWorkerFactory workerFactory,
  76. final IconManager iconManager,
  77. final CachingUpdateManager updateManager,
  78. @UserConfig final ConfigProvider userConfig) {
  79. super(parentWindow, workerFactory, eventBus);
  80. this.pluginManager = pluginManager;
  81. this.iconManager = iconManager;
  82. this.updateManager = updateManager;
  83. this.userConfig = userConfig;
  84. eventBus.subscribe(this);
  85. pluginManager.refreshPlugins();
  86. load();
  87. }
  88. @Override
  89. protected JTable populateList(final JTable table) {
  90. final List<PluginInfo> list = new ArrayList<>();
  91. final Collection<PluginInfo> sortedList = new ArrayList<>();
  92. list.addAll(pluginManager.getPluginInfos());
  93. list.sort(COMPARATOR);
  94. list.stream().filter(plugin -> plugin.getMetaData().getParent() == null).forEach(plugin -> {
  95. final List<PluginInfo> childList = new ArrayList<>();
  96. sortedList.add(plugin);
  97. plugin.getChildren().stream()
  98. .filter(child -> !childList.contains(child))
  99. .forEach(childList::add);
  100. childList.sort(COMPARATOR);
  101. sortedList.addAll(childList);
  102. });
  103. UIUtilities.invokeLater(() -> {
  104. ((DefaultTableModel) table.getModel()).setNumRows(0);
  105. for (final PluginInfo plugin : sortedList) {
  106. ((DefaultTableModel) table.getModel()).addRow(
  107. new AddonCell[]{
  108. new AddonCell(
  109. new AddonToggle(
  110. updateManager,
  111. userConfig,
  112. pluginManager,
  113. plugin),
  114. iconManager),});
  115. }
  116. table.repaint();
  117. });
  118. return table;
  119. }
  120. @Handler
  121. public void handlePluginRefresh(final PluginRefreshEvent event) {
  122. populateList(addonList);
  123. }
  124. @Override
  125. protected String getTypeName() {
  126. return "plugins";
  127. }
  128. private static class PluginInfoComparator implements Comparator<PluginInfo> {
  129. @Override
  130. public int compare(final PluginInfo o1, final PluginInfo o2) {
  131. return o1.getMetaData().getName().compareTo(o2.getMetaData().getName());
  132. }
  133. }
  134. }