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.impl.model;
042:
043: import java.util.List;
044: import org.netbeans.modules.xml.xam.Component;
045: import org.netbeans.modules.xml.xam.Model;
046: import org.netbeans.modules.xml.xam.Named;
047: import org.netbeans.modules.bpel.editors.api.utils.Util;
048:
049: import org.netbeans.modules.bpel.search.api.SearchException;
050: import org.netbeans.modules.bpel.search.api.SearchMatch;
051: import org.netbeans.modules.bpel.search.api.SearchOption;
052: import org.netbeans.modules.bpel.search.api.SearchTarget;
053: import org.netbeans.modules.bpel.search.spi.SearchEngine;
054: import static org.netbeans.modules.soa.ui.util.UI.*;
055:
056: /**
057: * @author Vladimir Yaroslavskiy
058: * @version 2006.11.13
059: */
060: public final class Engine extends SearchEngine.Adapter {
061:
062: public void search(SearchOption option) throws SearchException {
063: myClazz = null;
064: myOption = option;
065: myList = (List) option.getSource();
066: SearchTarget target = option.getTarget();
067:
068: if (target != null) {
069: myClazz = target.getClazz();
070: }
071: //out();
072: fireSearchStarted(option);
073: search(Util.getRoot((Model) myList.get(0)), ""); // NOI18N
074: fireSearchFinished(option);
075: }
076:
077: private void search(Object object, String indent) {
078: if (!(object instanceof Component)) {
079: return;
080: }
081: Component component = (Component) object;
082: process(component, indent);
083: List children = component.getChildren();
084:
085: for (Object child : children) {
086: search(child, indent + " "); // NOI18N
087: }
088: }
089:
090: private void process(Component component, String indent) {
091: //out(indent + " see: " + component);
092: if (checkClazz(component) && checkName(component)) {
093: //out(indent + " add.");
094: fireSearchFound(new Element(component, myList.get(1),
095: myList.get(2)));
096: }
097: }
098:
099: private boolean checkClazz(Object object) {
100: if (myClazz == null) {
101: return true;
102: }
103: return myClazz.isAssignableFrom(object.getClass());
104: }
105:
106: private boolean checkName(Component component) {
107: if (anyName()) {
108: return true;
109: }
110: if (!(component instanceof Named)) {
111: return false;
112: }
113: return accepts(((Named) component).getName());
114: }
115:
116: private boolean anyName() {
117: String text = myOption.getText();
118: SearchMatch match = myOption.getSearchMatch();
119:
120: if (match == SearchMatch.PATTERN && text.equals("*")) { // NOI18N
121: return true;
122: }
123: if (match == SearchMatch.REGULAR_EXPRESSION
124: && text.equals("\\.*")) { // NOI18N
125: return true;
126: }
127: if (match == null && text.equals("")) { // NOI18N
128: return true;
129: }
130: return false;
131: }
132:
133: public boolean accepts(Object source) {
134: return source instanceof List;
135: }
136:
137: public String getDisplayName() {
138: return i18n(Engine.class, "LBL_Engine_Display_Name"); // NOI18N
139: }
140:
141: public String getShortDescription() {
142: return i18n(Engine.class, "LBL_Engine_Short_Description"); // NOI18N
143: }
144:
145: private List myList;
146: private SearchOption myOption;
147: private Class<? extends Component> myClazz;
148: }
|