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 7.7KB

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