/plugins/PHPParser/tags/PHPParser-1.3.2/src/gatchan/phpparser/project/itemfinder/SimpleListModel.java

# · Java · 105 lines · 84 code · 13 blank · 8 comment · 20 complexity · a8d2a91ffe822cfb131ed199a016f42c MD5 · raw file

  1. package gatchan.phpparser.project.itemfinder;
  2. import javax.swing.*;
  3. import java.util.Comparator;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.TreeSet;
  7. /**
  8. * @author Matthieu Casanova
  9. */
  10. public final class SimpleListModel extends AbstractListModel {
  11. private TreeSet list = new TreeSet(new SimpleComparator());
  12. private int mode;
  13. private Object[] items;
  14. public SimpleListModel() {
  15. }
  16. public int getSize() {
  17. if (items == null) return 0;
  18. return Math.min(items.length, 25);
  19. }
  20. public Object getElementAt(int index) {
  21. return items[index];
  22. }
  23. public void setList(List list, String searchString) {
  24. int oldSize = this.list.size();
  25. int size = list.size();
  26. if (oldSize != 0) {
  27. fireIntervalRemoved(this, 0, Math.min(oldSize - 1, 25));
  28. }
  29. this.list.clear();
  30. for (int i = 0; i < list.size(); i++) {
  31. PHPItem phpItem = (PHPItem) list.get(i);
  32. if (accept(phpItem, searchString)) {
  33. this.list.add(phpItem);
  34. }
  35. }
  36. items = this.list.toArray();
  37. if (size != 0) {
  38. fireIntervalRemoved(this, 0, Math.min(size - 1, 25));
  39. }
  40. }
  41. public void filter(String searchString) {
  42. if (getSize() != 0) {
  43. boolean modified = false;
  44. Iterator iterator = list.iterator();
  45. while (iterator.hasNext()) {
  46. PHPItem phpItem = (PHPItem) iterator.next();
  47. if (!accept(phpItem, searchString)) {
  48. modified = true;
  49. iterator.remove();
  50. }
  51. }
  52. if (modified) {
  53. int oldSize = items.length;
  54. items = list.toArray();
  55. fireIntervalRemoved(this, items.length, oldSize);
  56. }
  57. fireContentsChanged(this, 0, items.length);
  58. }
  59. }
  60. public void setMode(int mode) {
  61. this.mode = mode;
  62. }
  63. private boolean accept(PHPItem phpItem, String searchText) {
  64. return (mode & phpItem.getItemType()) == phpItem.getItemType() && phpItem.getNameLowerCase().indexOf(searchText) != -1;
  65. }
  66. public void clear() {
  67. list.clear();
  68. items = null;
  69. }
  70. /**
  71. * Comparator that will compare PHPItems.
  72. *
  73. * @author Matthieu Casanova
  74. */
  75. private static class SimpleComparator implements Comparator {
  76. public int compare(Object o1, Object o2) {
  77. PHPItem item1 = (PHPItem) o1;
  78. PHPItem item2 = (PHPItem) o2;
  79. String name1 = item1.getName();
  80. String name2 = item2.getName();
  81. int l1 = name1.length();
  82. int l2 = name2.length();
  83. if (l1 != l2)
  84. return l1 - l2;
  85. int comp = name1.compareTo(name2);
  86. if (comp == 0) {
  87. return item1.getSourceStart() - item2.getSourceStart();
  88. }
  89. return comp;
  90. }
  91. }
  92. }