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.

AddonPanel.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.addonbrowser.BrowserWindow;
  20. import com.dmdirc.addons.ui_swing.components.addonbrowser.DataLoaderWorkerFactory;
  21. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  22. import com.dmdirc.config.prefs.PreferencesInterface;
  23. import com.dmdirc.events.eventbus.EventBus;
  24. import java.awt.Window;
  25. import javax.swing.JLabel;
  26. import javax.swing.JPanel;
  27. import javax.swing.JScrollPane;
  28. import javax.swing.JTable;
  29. import javax.swing.ListSelectionModel;
  30. import javax.swing.ScrollPaneConstants;
  31. import javax.swing.event.HyperlinkEvent;
  32. import javax.swing.event.HyperlinkEvent.EventType;
  33. import javax.swing.event.HyperlinkListener;
  34. import javax.swing.event.ListSelectionEvent;
  35. import javax.swing.event.ListSelectionListener;
  36. import javax.swing.table.DefaultTableModel;
  37. import net.miginfocom.swing.MigLayout;
  38. /**
  39. * Addon panel, base class for displaying and managing addons.
  40. */
  41. public abstract class AddonPanel extends JPanel implements AddonToggleListener,
  42. ListSelectionListener, PreferencesInterface, HyperlinkListener {
  43. /** Serial version UID. */
  44. private static final long serialVersionUID = 3;
  45. /** List of addons. */
  46. protected JTable addonList;
  47. /** Parent Window. */
  48. private final Window parentWindow;
  49. /** The factory to use to produce data loader workers. */
  50. private final DataLoaderWorkerFactory workerFactory;
  51. /** The event bus to post errors to. */
  52. private final EventBus eventBus;
  53. /** Addon list scroll pane. */
  54. private JScrollPane scrollPane;
  55. /** Blurb label. */
  56. private TextLabel blurbLabel;
  57. /** Get more info link. */
  58. private TextLabel getMoreLabel;
  59. /** Addon info panel. */
  60. private AddonInfoPanel addonInfo;
  61. /** Selected addon. */
  62. private int selectedAddon = -1;
  63. /**
  64. * Creates a new instance of AddonPanel
  65. *
  66. * @param parentWindow Parent window
  67. * @param workerFactory The factory to use to produce data loader workers.
  68. * @param eventBus The event bus to post errors to.
  69. */
  70. public AddonPanel(final Window parentWindow, final DataLoaderWorkerFactory workerFactory,
  71. final EventBus eventBus) {
  72. this.parentWindow = parentWindow;
  73. this.workerFactory = workerFactory;
  74. this.eventBus = eventBus;
  75. initComponents();
  76. layoutComponents();
  77. }
  78. /** Initialises the components. */
  79. private void initComponents() {
  80. addonList = new JTable(new DefaultTableModel(
  81. new Object[]{"Addon",}, 0)) {
  82. /** Serial Version UID. */
  83. private static final long serialVersionUID = 1;
  84. @Override
  85. public boolean isCellEditable(final int row, final int column) {
  86. return false;
  87. }
  88. };
  89. addonList.setDefaultRenderer(Object.class,
  90. new AddonCellRenderer());
  91. addonList.setTableHeader(null);
  92. addonList.setShowGrid(false);
  93. addonList.getSelectionModel().setSelectionMode(
  94. ListSelectionModel.SINGLE_SELECTION);
  95. scrollPane = new JScrollPane(new JLabel("Loading " + getTypeName()
  96. + "..."));
  97. scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  98. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  99. blurbLabel = new TextLabel(getTypeName().substring(0, 1).toUpperCase()
  100. + getTypeName().substring(1) + " allow you to extend the "
  101. + "functionality of DMDirc.");
  102. getMoreLabel = new TextLabel(
  103. "<a href=\"https://addons.dmdirc.com\">Get more addons</a>");
  104. getMoreLabel.addHyperlinkListener(this);
  105. addonInfo = new AddonInfoPanel();
  106. addonInfo.addListener(this);
  107. }
  108. /**
  109. * Populates the list in a background thread.
  110. */
  111. protected void load() {
  112. UIUtilities.invokeOffEDT(() -> populateList(addonList),
  113. value -> {
  114. scrollPane.setViewportView(addonList);
  115. addonList.getSelectionModel().addListSelectionListener(this);
  116. addonList.getSelectionModel().setSelectionInterval(0, 0);
  117. });
  118. }
  119. /** Lays out the dialog. */
  120. private void layoutComponents() {
  121. setLayout(new MigLayout("ins 0, fill, hmax 500"));
  122. add(blurbLabel, "wrap 5, growx, pushx");
  123. add(getMoreLabel, "wrap 5, right");
  124. add(scrollPane, "wrap 5, grow, push");
  125. add(addonInfo, "grow, push");
  126. }
  127. /**
  128. * Populates the addon list returning it when complete.
  129. *
  130. * @param table Table to be populated
  131. *
  132. * @return Populated table
  133. */
  134. protected abstract JTable populateList(final JTable table);
  135. /**
  136. * Returns the name of the type of addon being handled.
  137. *
  138. * @return Addon type name
  139. */
  140. protected abstract String getTypeName();
  141. @Override
  142. public void valueChanged(final ListSelectionEvent e) {
  143. final int newSelection = addonList.getSelectedRow();
  144. if (newSelection == -1) {
  145. addonList.getSelectionModel().setSelectionInterval(0, selectedAddon);
  146. } else if (addonList.getModel().getRowCount() > newSelection) {
  147. addonInfo.setAddonToggle((AddonToggle) ((AddonCell) addonList
  148. .getModel().getValueAt(newSelection, 0)).getObject());
  149. }
  150. selectedAddon = addonList.getSelectedRow();
  151. }
  152. @Override
  153. public void hyperlinkUpdate(final HyperlinkEvent e) {
  154. if (e.getEventType() == EventType.ACTIVATED) {
  155. new BrowserWindow(workerFactory, parentWindow);
  156. }
  157. }
  158. @Override
  159. public void save() {
  160. if (addonList.getRowCount() == 0) {
  161. return;
  162. }
  163. for (int i = 0; i < addonList.getRowCount(); i++) {
  164. addonList.getModel().getColumnCount();
  165. ((AddonToggle) ((AddonCell) addonList.getModel()
  166. .getValueAt(i, 0)).getObject()).apply();
  167. }
  168. }
  169. @Override
  170. public void addonToggled() {
  171. addonList.repaint();
  172. }
  173. }