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.

BrowserWindow.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.addonbrowser;
  23. import java.awt.Window;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.ButtonGroup;
  28. import javax.swing.JCheckBox;
  29. import javax.swing.JDialog;
  30. import javax.swing.JPanel;
  31. import javax.swing.JRadioButton;
  32. import javax.swing.JScrollPane;
  33. import javax.swing.JTextField;
  34. import javax.swing.SwingUtilities;
  35. import javax.swing.UIManager;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * The main window that allows users to browse addons.
  39. */
  40. public class BrowserWindow extends JDialog implements ActionListener {
  41. /**
  42. * A version number for this class. It should be changed whenever the class structure is changed
  43. * (or anything else that would prevent serialized objects being unserialized with the new
  44. * class).
  45. */
  46. private static final long serialVersionUID = 1;
  47. /** The search box. */
  48. private final JTextField searchBox = new JTextField();
  49. /** The plugins check box. */
  50. private final JCheckBox pluginsBox = new JCheckBox("Plugins", true);
  51. /** The themes check box. */
  52. private final JCheckBox themesBox = new JCheckBox("Themes", true);
  53. /** The actions check box. */
  54. private final JCheckBox actionsBox = new JCheckBox("Action Packs", true);
  55. /** The verified check box. */
  56. private final JCheckBox verifiedBox = new JCheckBox("Verified", true);
  57. /** The unverified check box. */
  58. private final JCheckBox unverifiedBox = new JCheckBox("Unverified", false);
  59. /** The installed checkbox. */
  60. private final JCheckBox installedBox = new JCheckBox("Installed", true);
  61. /** The not installed checkbox. */
  62. private final JCheckBox notinstalledBox = new JCheckBox("Not installed",
  63. true);
  64. /** The panel used to list addons. */
  65. private final AddonTable list = new AddonTable();
  66. /** The scrollpane for the list panel. */
  67. private final JScrollPane scrollPane = new JScrollPane(list,
  68. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  69. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  70. /** The sort by name button. */
  71. private final JRadioButton nameButton = new JRadioButton("Name", true);
  72. /** The sort by rating button. */
  73. private final JRadioButton ratingButton = new JRadioButton("Rating", false);
  74. /** The sort by date button. */
  75. private final JRadioButton dateButton = new JRadioButton("Date", false);
  76. /** The sort by status button. */
  77. private final JRadioButton statusButton = new JRadioButton("Status", false);
  78. /** Row sorter. */
  79. private final AddonSorter sorter;
  80. /** Factory to use to produce workers to load addon data. */
  81. private final DataLoaderWorkerFactory loaderFactory;
  82. /**
  83. * Creates and displays a new browser window.
  84. *
  85. * @param loaderFactory Factory to use to produce workers.
  86. * @param parentWindow Parent window
  87. */
  88. public BrowserWindow(
  89. final DataLoaderWorkerFactory loaderFactory,
  90. final Window parentWindow) {
  91. super(parentWindow, "DMDirc Addon Browser", ModalityType.MODELESS);
  92. this.loaderFactory = loaderFactory;
  93. setIconImages(parentWindow.getIconImages());
  94. setResizable(false);
  95. setLayout(new MigLayout("fill, wmin 650, hmin 600"));
  96. scrollPane.getVerticalScrollBar().setUnitIncrement(15);
  97. JPanel panel = new JPanel(new MigLayout("fill"));
  98. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  99. "TitledBorder.border"), "Search"));
  100. panel.add(searchBox, "growx");
  101. add(panel, "width 150!, grow");
  102. panel = new JPanel(new MigLayout("fill"));
  103. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  104. "TitledBorder.border"), "Results"));
  105. panel.add(scrollPane, "grow, push");
  106. add(panel, "wrap, spany 4, grow, push");
  107. panel = new JPanel(new MigLayout("fill, wrap"));
  108. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  109. "TitledBorder.border"), "Types"));
  110. panel.add(pluginsBox, "grow");
  111. panel.add(themesBox, "grow");
  112. panel.add(actionsBox, "grow");
  113. add(panel, "wrap, pushy, growy, width 150!");
  114. panel = new JPanel(new MigLayout("fill, wrap"));
  115. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  116. "TitledBorder.border"), "Status"));
  117. panel.add(verifiedBox, "grow");
  118. panel.add(unverifiedBox, "grow");
  119. panel.add(installedBox, "grow");
  120. panel.add(notinstalledBox, "grow");
  121. add(panel, "wrap, pushy, growy, width 150!");
  122. panel = new JPanel(new MigLayout("fill, wrap"));
  123. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  124. "TitledBorder.border"), "Sort by"));
  125. panel.add(nameButton, "grow");
  126. panel.add(ratingButton, "grow");
  127. panel.add(dateButton, "grow");
  128. panel.add(statusButton, "grow");
  129. add(panel, "wrap, pushy, growy, width 150!");
  130. initListeners();
  131. final AddonFilter filter = new AddonFilter(verifiedBox.getModel(),
  132. unverifiedBox.getModel(), installedBox.getModel(),
  133. notinstalledBox.getModel(), pluginsBox.getModel(),
  134. themesBox.getModel(), actionsBox.getModel(), searchBox);
  135. sorter = new AddonSorter(list.getModel(), dateButton.getModel(),
  136. nameButton.getModel(), ratingButton.getModel(),
  137. statusButton.getModel(), filter);
  138. list.setRowSorter(sorter);
  139. list.setShowGrid(false);
  140. loadData(true);
  141. pack();
  142. setLocationRelativeTo(parentWindow);
  143. setVisible(true);
  144. setLocationRelativeTo(parentWindow);
  145. }
  146. /**
  147. * Registers listeners and sets up button groups.
  148. */
  149. private void initListeners() {
  150. final ButtonGroup group = new ButtonGroup();
  151. group.add(nameButton);
  152. group.add(ratingButton);
  153. group.add(dateButton);
  154. group.add(statusButton);
  155. pluginsBox.addActionListener(this);
  156. themesBox.addActionListener(this);
  157. actionsBox.addActionListener(this);
  158. verifiedBox.addActionListener(this);
  159. unverifiedBox.addActionListener(this);
  160. installedBox.addActionListener(this);
  161. notinstalledBox.addActionListener(this);
  162. searchBox.addActionListener(this);
  163. nameButton.addActionListener(this);
  164. ratingButton.addActionListener(this);
  165. dateButton.addActionListener(this);
  166. statusButton.addActionListener(this);
  167. }
  168. /**
  169. * Loads the addon data into the browser window, either from the local cache or by downloading
  170. * the data from the website.
  171. *
  172. * @param download Download new addon feed?
  173. */
  174. public final void loadData(final boolean download) {
  175. loaderFactory.getDataLoaderWorker(list, download, this, scrollPane).executeInExecutor();
  176. }
  177. /**
  178. * {@inheritDoc}
  179. *
  180. * @param e Action event
  181. */
  182. @Override
  183. public void actionPerformed(final ActionEvent e) {
  184. SwingUtilities.invokeLater(new Runnable() {
  185. /** {@inheritDoc} */
  186. @Override
  187. public void run() {
  188. sorter.sort();
  189. }
  190. });
  191. }
  192. }