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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components.addonpanel;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.IconManager;
  20. import com.dmdirc.addons.ui_swing.components.addonbrowser.DataLoaderWorkerFactory;
  21. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  22. import com.dmdirc.config.UserConfig;
  23. import com.dmdirc.events.PluginRefreshEvent;
  24. import com.dmdirc.events.eventbus.EventBus;
  25. import com.dmdirc.interfaces.config.ConfigProvider;
  26. import com.dmdirc.plugins.PluginInfo;
  27. import com.dmdirc.plugins.PluginManager;
  28. import com.dmdirc.updater.manager.CachingUpdateManager;
  29. import java.awt.Window;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.Comparator;
  33. import java.util.List;
  34. import javax.inject.Inject;
  35. import javax.swing.JTable;
  36. import javax.swing.table.DefaultTableModel;
  37. import net.engio.mbassy.listener.Handler;
  38. /**
  39. * Lists known plugins, enabling the end user to enable/disable these as well as download new ones.
  40. */
  41. public class PluginPanel extends AddonPanel {
  42. /** A version number for this class. */
  43. private static final long serialVersionUID = 1;
  44. /** Comparator to use to sort plugins in the panel. */
  45. private static final Comparator<? super PluginInfo> COMPARATOR = new PluginInfoComparator();
  46. /** Manager to retrieve plugin information from. */
  47. private final PluginManager pluginManager;
  48. /** Manager to use to retrieve addon-related icons. */
  49. private final IconManager iconManager;
  50. /** Manager to use to retrieve update information. */
  51. private final CachingUpdateManager updateManager;
  52. /** Configuration to write update-related settings to. */
  53. private final ConfigProvider userConfig;
  54. /**
  55. * Creates a new instance of PluginPanel.
  56. *
  57. * @param eventBus Event bus to subscribe to events on
  58. * @param parentWindow Parent window
  59. * @param pluginManager Manager to retrieve plugins from.
  60. * @param workerFactory Factory to use to create data workers.
  61. * @param iconManager Manager to use to retrieve addon-related icons.
  62. * @param updateManager Manager to use to retrieve update information.
  63. * @param userConfig Configuration to write update-related settings to.
  64. */
  65. @Inject
  66. public PluginPanel(
  67. final EventBus eventBus,
  68. @MainWindow final Window parentWindow,
  69. final PluginManager pluginManager,
  70. final DataLoaderWorkerFactory workerFactory,
  71. final IconManager iconManager,
  72. final CachingUpdateManager updateManager,
  73. @UserConfig final ConfigProvider userConfig) {
  74. super(parentWindow, workerFactory, eventBus);
  75. this.pluginManager = pluginManager;
  76. this.iconManager = iconManager;
  77. this.updateManager = updateManager;
  78. this.userConfig = userConfig;
  79. eventBus.subscribe(this);
  80. pluginManager.refreshPlugins();
  81. load();
  82. }
  83. @Override
  84. protected JTable populateList(final JTable table) {
  85. final List<PluginInfo> list = new ArrayList<>();
  86. final Collection<PluginInfo> sortedList = new ArrayList<>();
  87. list.addAll(pluginManager.getPluginInfos());
  88. list.sort(COMPARATOR);
  89. list.stream().filter(plugin -> plugin.getMetaData().getParent() == null).forEach(plugin -> {
  90. final List<PluginInfo> childList = new ArrayList<>();
  91. sortedList.add(plugin);
  92. plugin.getChildren().stream()
  93. .filter(child -> !childList.contains(child))
  94. .forEach(childList::add);
  95. childList.sort(COMPARATOR);
  96. sortedList.addAll(childList);
  97. });
  98. UIUtilities.invokeLater(() -> {
  99. ((DefaultTableModel) table.getModel()).setNumRows(0);
  100. for (final PluginInfo plugin : sortedList) {
  101. ((DefaultTableModel) table.getModel()).addRow(
  102. new AddonCell[]{
  103. new AddonCell(
  104. new AddonToggle(
  105. updateManager,
  106. userConfig,
  107. pluginManager,
  108. plugin),
  109. iconManager),});
  110. }
  111. table.repaint();
  112. });
  113. return table;
  114. }
  115. @Handler
  116. public void handlePluginRefresh(final PluginRefreshEvent event) {
  117. populateList(addonList);
  118. }
  119. @Override
  120. protected String getTypeName() {
  121. return "plugins";
  122. }
  123. private static class PluginInfoComparator implements Comparator<PluginInfo> {
  124. @Override
  125. public int compare(final PluginInfo o1, final PluginInfo o2) {
  126. return o1.getMetaData().getName().compareTo(o2.getMetaData().getName());
  127. }
  128. }
  129. }