1 19 20 package org.openidex.search; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.List ; 27 28 45 public final class SearchHistory { 46 47 48 private SearchPattern lastSelected; 49 50 51 private PropertyChangeSupport pcs; 52 53 54 private static final int MAX_SEARCH_PATTERNS_ITEMS = 50; 55 56 57 private List searchPatternsList = new ArrayList (MAX_SEARCH_PATTERNS_ITEMS); 58 59 60 private static SearchHistory INSTANCE = null; 61 62 67 public final static String LAST_SELECTED = "last-selected"; 69 74 public final static String ADD_TO_HISTORY = "add-to-history"; 76 77 private SearchHistory() { 78 } 79 80 81 public synchronized static SearchHistory getDefault(){ 82 if (INSTANCE == null) { 83 INSTANCE = new SearchHistory(); 84 } 85 return INSTANCE; 86 } 87 88 89 public SearchPattern getLastSelected(){ 90 return lastSelected; 91 } 92 93 96 public void setLastSelected(SearchPattern pattern){ 97 SearchPattern oldPattern = this.lastSelected; 98 this.lastSelected = pattern; 99 if (pcs != null){ 100 pcs.firePropertyChange(LAST_SELECTED, oldPattern, pattern); 101 } 102 } 103 104 private synchronized PropertyChangeSupport getPropertyChangeSupport(){ 105 if (pcs == null){ 106 pcs = new PropertyChangeSupport (this); 107 } 108 return pcs; 109 } 110 111 114 public void addPropertyChangeListener(PropertyChangeListener pcl){ 115 getPropertyChangeSupport().addPropertyChangeListener(pcl); 116 } 117 118 121 public void removePropertyChangeListener(PropertyChangeListener pcl){ 122 if (pcs != null){ 123 pcs.removePropertyChangeListener(pcl); 124 } 125 } 126 127 128 public synchronized List getSearchPatterns(){ 129 return Collections.unmodifiableList(searchPatternsList); 130 } 131 132 135 public synchronized void add(SearchPattern pattern){ 136 if (pattern == null || pattern.getSearchExpression() == null || pattern.getSearchExpression().length() == 0){ 137 return; 138 } 139 if (searchPatternsList.size()>0 && pattern.equals(searchPatternsList.get(0))){ 140 return; 141 } 142 if (searchPatternsList.size() == MAX_SEARCH_PATTERNS_ITEMS){ 143 searchPatternsList.remove(MAX_SEARCH_PATTERNS_ITEMS-1); 144 } 145 searchPatternsList.add(0, pattern); 146 if (pcs != null){ 147 pcs.firePropertyChange(ADD_TO_HISTORY, null, pattern); 148 } 149 } 150 151 } 152 | Popular Tags |