Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AddonPanel.java 7.6KB

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