KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > SearchProjectSettings


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 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.List JavaDoc;
25 import org.openide.options.SystemOption;
26 import org.openide.util.NbBundle;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.Lookup;
29 import org.openide.util.SharedClassObject;
30 import org.openidex.search.SearchType;
31
32 /**
33  * <!-- PENDING -->
34  *
35  * @author Marian Petras
36  */

37 public class SearchProjectSettings extends SystemOption {
38
39     // static .....................................................................................
40

41     private static final long serialVersionUID = 6955446757377175182L;
42
43     /**
44      * Name of property &quot;search criteria&quot;.
45      *
46      * @see #getCriteria
47      * @see #setCriteria
48      */

49     public static final String JavaDoc PROP_CRITERIA = "search criteria"; //NOI18N
50

51     /** [PENDING] */
52     private static SearchCriterion[] searchCriteria;
53     
54     // SystemOption implementation ..................................................................
55

56     /**
57     * Returns name of these settings.
58     */

59     public String JavaDoc displayName() {
60         return NbBundle.getBundle(SearchProjectSettings.class)
61                .getString("TEXT_Search_settings"); //NOI18N
62
}
63
64     public HelpCtx getHelpCtx () {
65         return new HelpCtx(SearchProjectSettings.class);
66     }
67
68     // properties .................................................................................
69

70     /** */
71     private void importOldSettings() {
72         
73         /* import old search criteria: */
74         List JavaDoc oldCriteria = new ArrayList JavaDoc();
75         Collection JavaDoc instances = Lookup.getDefault()
76                                .lookup(new Lookup.Template(SearchType.class))
77                                .allInstances();
78         for (java.util.Iterator JavaDoc i = instances.iterator(); i.hasNext(); ) {
79             SearchType instance = (SearchType) i.next();
80             if (instance.isValid()) {
81                 try {
82                     oldCriteria.add(new SearchCriterion(instance));
83                 } catch (java.io.IOException JavaDoc ex) {
84                     org.openide.ErrorManager.getDefault().notify(
85                             org.openide.ErrorManager.EXCEPTION,
86                             ex);
87                 }
88             }
89         }
90         if (oldCriteria.isEmpty()) {
91             searchCriteria = new SearchCriterion[0];
92         } else {
93             searchCriteria = new SearchCriterion[oldCriteria.size()];
94             oldCriteria.toArray(searchCriteria);
95         }
96     }
97
98     /**
99      * <!-- PENDING -->
100      *
101      * @see #setCriteria
102      * @see #PROP_CRITERIA
103      */

104     public SearchCriterion[] getSearchCriteria() {
105         if (searchCriteria == null) {
106             importOldSettings();
107         }
108         return searchCriteria;
109     }
110
111     /**
112      * <!-- PENDING -->
113      *
114      * @see #getCriteria
115      * @see #PROP_CRITERIA
116      */

117     public void setSearchCriteria(SearchCriterion[] criteria) {
118         if (criteria == null) {
119             criteria = new SearchCriterion[0];
120         }
121         SearchCriterion[] old = searchCriteria;
122         searchCriteria = criteria;
123         firePropertyChange(PROP_CRITERIA, old, searchCriteria);
124     }
125     
126     /** */
127     static final SearchProjectSettings getInstance() {
128         return (SearchProjectSettings)
129                SharedClassObject.findObject(SearchProjectSettings.class, true);
130     }
131     
132     /**
133      * Adds new saved search criterion.
134      *
135      * @param c search criterion to be added
136      */

137     void addSearchCriterion(SearchCriterion c) {
138         if (searchCriteria == null) {
139             searchCriteria = new SearchCriterion[1];
140             searchCriteria[0] = c;
141             firePropertyChange(PROP_CRITERIA, null, searchCriteria);
142         } else {
143             SearchCriterion[] old = searchCriteria;
144             searchCriteria = new SearchCriterion[old.length + 1];
145             System.arraycopy(old, 0, searchCriteria, 0, old.length);
146             searchCriteria[old.length] = c;
147             firePropertyChange(PROP_CRITERIA, old, searchCriteria);
148         }
149     }
150
151     /**
152      * Finds and replaces an existing search criterion with another one.
153      * Search criterion to be replaced is found by fields
154      * {@link #name} and {@link #searchTypeClassName}.
155      * If no matching search criterion is found, no change is done.
156      *
157      * @param name name of a search criterion to search for
158      * @param className class name of a search criterion to search for
159      * @param c search criterion to replace the found criterion by
160      * @return <code>true</code> if a search criterion was found and replaced
161      * (including the case that the criterion passed as the argument
162      * was found);
163      * <code>false</code> otherwise
164      */

165     boolean replaceSearchCriterion(String JavaDoc name,
166                                    String JavaDoc className,
167                                    SearchCriterion c) {
168         if (searchCriteria == null) {
169             return false;
170         }
171         for (int i = 0; i < searchCriteria.length; i++) {
172             SearchCriterion candidate = searchCriteria[i];
173             if (candidate.name.equals(name)
174                     && candidate.searchTypeClassName.equals(className)) {
175                 searchCriteria[i] = c;
176                 markSearchCriteriaChanged();
177                 return true;
178             }
179         }
180         return false;
181     }
182     
183     /** */
184     void markSearchCriteriaChanged() {
185         firePropertyChange(PROP_CRITERIA, null, null);
186     }
187     
188 }
189
Popular Tags