KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openidex > search > SearchHistory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2005 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openidex.search;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Shareable search history. Known implementations are explorer search
30  * dialog and editor find&replace dialog.
31  *
32  * Typical use case:
33  * Editor registers a listener to listen on lastSelected SearchPattern. If user
34  * opens explorer's search dialog and perform search, a search expression is added
35  * into SearchHistory and lastSelected SearchPattern is setted. The event is fired,
36  * editor can retrieve lastSelected SearchPattern and in accordance with its parameters
37  * it can highlight(in yellow) all matched patterns. If editor dialog is open,
38  * it contains shareable SearchHistory. Another direction is search in editor, that
39  * adds a SearchPattern in SearchHistory, thus the new item is available also in
40  * explorer's search dialog.
41  *
42  * @since org.openidex.util/3 3.5, NB 4.1
43  * @author Martin Roskanin
44  */

45 public final class SearchHistory {
46
47     /** Last selected SearchPattern. */
48     private SearchPattern lastSelected;
49     
50     /** Support for listeners */
51     private PropertyChangeSupport JavaDoc pcs;
52
53     /** Maximum items allowed in searchPatternsList */
54     private static final int MAX_SEARCH_PATTERNS_ITEMS = 50;
55
56     /** Shareable SearchPattern history. It is a List of SearchPatterns */
57     private List JavaDoc searchPatternsList = new ArrayList JavaDoc(MAX_SEARCH_PATTERNS_ITEMS);
58
59     /** Singleton instance */
60     private static SearchHistory INSTANCE = null;
61     
62     /** Property name for last selected search pattern
63      * Firing:
64      * oldValue - old selected pattern
65      * newValue - new selected pattern
66      */

67     public final static String JavaDoc LAST_SELECTED = "last-selected"; //NOI18N
68

69     /** Property name for adding pattern to history
70      * Firing:
71      * oldValue - null
72      * newValue - added pattern
73      */

74     public final static String JavaDoc ADD_TO_HISTORY = "add-to-history"; //NOI18N
75

76     /** Creates a new instance of SearchHistory */
77     private SearchHistory() {
78     }
79
80     /** @return singleton instance of SearchHistory */
81     public synchronized static SearchHistory getDefault(){
82         if (INSTANCE == null) {
83             INSTANCE = new SearchHistory();
84         }
85         return INSTANCE;
86     }
87     
88     /** @return last selected SearchPattern */
89     public SearchPattern getLastSelected(){
90         return lastSelected;
91     }
92     
93     /** Sets last selected SearchPattern
94      * @param pattern last selected pattern
95      */

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 JavaDoc getPropertyChangeSupport(){
105         if (pcs == null){
106             pcs = new PropertyChangeSupport JavaDoc(this);
107         }
108         return pcs;
109     }
110
111     /** Adds a property change listener.
112      * @param pcl the listener to add
113      */

114     public void addPropertyChangeListener(PropertyChangeListener JavaDoc pcl){
115         getPropertyChangeSupport().addPropertyChangeListener(pcl);
116     }
117     
118     /** Removes a property change listener.
119      * @param pcl the listener to remove
120      */

121     public void removePropertyChangeListener(PropertyChangeListener JavaDoc pcl){
122         if (pcs != null){
123             pcs.removePropertyChangeListener(pcl);
124         }
125     }
126
127     /** @return unmodifiable List of SearchPatterns */
128     public synchronized List JavaDoc/*<SearchPattern>*/ getSearchPatterns(){
129         return Collections.unmodifiableList(searchPatternsList);
130     }
131     
132     /** Adds SearchPattern to SearchHistory
133      * @param pattern the SearchPattern to add
134      */

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