1 19 20 package org.netbeans.modules.search; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.List ; 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 37 public class SearchProjectSettings extends SystemOption { 38 39 41 private static final long serialVersionUID = 6955446757377175182L; 42 43 49 public static final String PROP_CRITERIA = "search criteria"; 51 52 private static SearchCriterion[] searchCriteria; 53 54 56 59 public String displayName() { 60 return NbBundle.getBundle(SearchProjectSettings.class) 61 .getString("TEXT_Search_settings"); } 63 64 public HelpCtx getHelpCtx () { 65 return new HelpCtx(SearchProjectSettings.class); 66 } 67 68 70 71 private void importOldSettings() { 72 73 74 List oldCriteria = new ArrayList (); 75 Collection instances = Lookup.getDefault() 76 .lookup(new Lookup.Template(SearchType.class)) 77 .allInstances(); 78 for (java.util.Iterator 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 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 104 public SearchCriterion[] getSearchCriteria() { 105 if (searchCriteria == null) { 106 importOldSettings(); 107 } 108 return searchCriteria; 109 } 110 111 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 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 165 boolean replaceSearchCriterion(String name, 166 String 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 |