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.

AddonSorter.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.util.Comparator;
  19. import javax.swing.ButtonModel;
  20. import javax.swing.table.DefaultTableModel;
  21. import javax.swing.table.TableRowSorter;
  22. /**
  23. * Addon sorter.
  24. */
  25. public class AddonSorter extends TableRowSorter<DefaultTableModel> implements
  26. Comparator<AddonInfoLabel> {
  27. private final ButtonModel sortByDate;
  28. private final ButtonModel sortByName;
  29. private final ButtonModel sortByRating;
  30. private final ButtonModel sortByStatus;
  31. /**
  32. * Creates a new addon sorter.
  33. *
  34. * @param model Table model to sort
  35. * @param sortByDate Sort by date
  36. * @param sortByName Sort by name
  37. * @param sortByRating Sort by rating
  38. * @param sortByStatus Sort by status
  39. * @param filter Addon filter
  40. */
  41. public AddonSorter(final DefaultTableModel model, final ButtonModel sortByDate,
  42. final ButtonModel sortByName, final ButtonModel sortByRating,
  43. final ButtonModel sortByStatus, final AddonFilter filter) {
  44. super(model);
  45. this.sortByDate = sortByDate;
  46. this.sortByName = sortByName;
  47. this.sortByRating = sortByRating;
  48. this.sortByStatus = sortByStatus;
  49. setRowFilter(filter);
  50. setComparator(0, this);
  51. toggleSortOrder(0);
  52. }
  53. @Override
  54. public int compare(final AddonInfoLabel o1, final AddonInfoLabel o2) {
  55. final AddonInfo info1 = o1.getAddonInfo();
  56. final AddonInfo info2 = o2.getAddonInfo();
  57. if (sortByDate.isSelected()) {
  58. return info1.getId() - info2.getId();
  59. } else if (sortByName.isSelected()) {
  60. return info1.getTitle().compareTo(info2.getTitle());
  61. } else if (sortByRating.isSelected()) {
  62. return info1.getRating() - info2.getRating();
  63. } else if (sortByStatus.isSelected()) {
  64. return (info1.isVerified() ? 1 : 0) - (info2.isVerified() ? 1 : 0);
  65. } else {
  66. return 0;
  67. }
  68. }
  69. }