Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PluginPanel.java 5.9KB

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