001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.bpel.search.spi;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.regex.PatternSyntaxException;
046:
047: import org.netbeans.modules.bpel.search.api.SearchElement;
048: import org.netbeans.modules.bpel.search.api.SearchEvent;
049: import org.netbeans.modules.bpel.search.api.SearchException;
050: import org.netbeans.modules.bpel.search.api.SearchOption;
051: import org.netbeans.modules.bpel.search.api.SearchPattern;
052: import static org.netbeans.modules.soa.ui.util.UI.*;
053:
054: /**
055: * @author Vladimir Yaroslavskiy
056: * @version 2007.05.25
057: */
058: public interface SearchEngine {
059:
060: /**
061: * Performs search for given option.
062: * @throws SearchException if the search resulted in an unexpected error
063: * @param option for search
064: */
065: void search(SearchOption option) throws SearchException;
066:
067: /**
068: * Returns true if search can be performed in source.
069: * @param source where search will be perfromed
070: * @return true if search can be performed in source
071: */
072: boolean accepts(Object source);
073:
074: /**
075: * Addes search listener.
076: * @param listener to be added
077: */
078: void addSearchListener(SearchListener listener);
079:
080: /**
081: * Removes all search listeners.
082: */
083: void removeSearchListeners();
084:
085: /**
086: * Returns display name of engine.
087: * @return display name of engine
088: */
089: String getDisplayName();
090:
091: /**
092: * Returns short description of engine.
093: * @return short description of engine
094: */
095: String getShortDescription();
096:
097: // ----------------------------------------------------
098: public abstract class Adapter implements SearchEngine {
099:
100: public Adapter() {
101: removeSearchListeners();
102: }
103:
104: public Object[] getTargets() {
105: return new Object[] {};
106: }
107:
108: public synchronized void addSearchListener(
109: SearchListener listener) {
110: mySearchListeners.add(listener);
111: }
112:
113: public synchronized void removeSearchListeners() {
114: mySearchListeners = new ArrayList<SearchListener>();
115: }
116:
117: protected synchronized void fireSearchStarted(
118: SearchOption option) throws SearchException {
119: try {
120: createSearchPattern(option);
121: } catch (PatternSyntaxException e) {
122: throw new SearchException(e);
123: }
124: SearchEvent event = new SearchEvent.Adapter(option, null);
125:
126: for (SearchListener listener : mySearchListeners) {
127: listener.searchStarted(event);
128: }
129: }
130:
131: protected synchronized void fireSearchFound(
132: SearchElement element) {
133: SearchEvent event = new SearchEvent.Adapter(null, element);
134:
135: for (SearchListener listener : mySearchListeners) {
136: listener.searchFound(event);
137: }
138: }
139:
140: protected synchronized void fireSearchFinished(
141: SearchOption option) {
142: SearchEvent event = new SearchEvent.Adapter(option, null);
143:
144: for (SearchListener listener : mySearchListeners) {
145: listener.searchFinished(event);
146: }
147: }
148:
149: protected boolean accepts(String text) {
150: if (text == null) {
151: return false;
152: }
153: return mySearchPattern.accepts(text);
154: }
155:
156: private void createSearchPattern(SearchOption option) {
157: mySearchPattern = new SearchPattern(option.getText(),
158: option.getSearchMatch(), option.isCaseSensitive());
159: }
160:
161: private SearchPattern mySearchPattern;
162: private List<SearchListener> mySearchListeners;
163: }
164: }
|