This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 5009
Collapse All | Expand All

(-)api.search/src/org/netbeans/modules/search/OpenFilesSearchScopeProvider.java (+164 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.search;
43
44
import java.util.ArrayList;
45
import java.util.Collection;
46
import java.util.LinkedHashSet;
47
import java.util.List;
48
import java.util.Set;
49
import javax.swing.Icon;
50
import javax.swing.JEditorPane;
51
import org.netbeans.api.annotations.common.StaticResource;
52
import org.netbeans.api.search.provider.SearchInfo;
53
import org.netbeans.api.search.provider.SearchInfoUtils;
54
import org.netbeans.spi.search.SearchScopeDefinition;
55
import org.netbeans.spi.search.SearchScopeDefinitionProvider;
56
import org.openide.cookies.EditorCookie;
57
import org.openide.filesystems.FileObject;
58
import org.openide.loaders.DataObject;
59
import org.openide.nodes.Node;
60
import org.openide.util.ImageUtilities;
61
import org.openide.util.NbBundle;
62
import org.openide.util.lookup.ServiceProvider;
63
import org.openide.windows.TopComponent;
64
65
/**
66
 * SearchScopeProvider for all the open documents.
67
 *
68
 * @author markiewb
69
 */
70
@ServiceProvider(service = SearchScopeDefinitionProvider.class)
71
public class OpenFilesSearchScopeProvider extends SearchScopeDefinitionProvider {
72
73
    @Override
74
    public List<SearchScopeDefinition> createSearchScopeDefinitions() {
75
	List<SearchScopeDefinition> list = new ArrayList<SearchScopeDefinition>(1);
76
	list.add(new OpenFilesScope());
77
	return list;
78
    }
79
80
    @NbBundle.Messages({
81
	"# {0} - number of files",
82
	"LBL_OpenFileScope=Open {0,choice,0#Documents|1#Document|1<Documents} ({0,choice,0#0 files|1#1 file|1<{0,number,integer} files})"
83
    })
84
    private static class OpenFilesScope extends SearchScopeDefinition {
85
86
	//icon taken from http://hg.netbeans.org/main-golden/rev/edbd736e674e
87
	@StaticResource
88
	private static final String ICON_PATH =
89
		"org/netbeans/modules/search/res/multi_selection.png"; //NOI18N
90
	private static final Icon ICON;
91
92
	static {
93
	    ICON = ImageUtilities.loadImageIcon(ICON_PATH, false);
94
	}
95
96
	@Override
97
	public String getTypeId() {
98
	    return "openFiles";//NOI18N
99
	}
100
101
	@Override
102
	public String getDisplayName() {
103
	    return Bundle.LBL_OpenFileScope(getCurrentlyOpenedFiles().size());
104
	}
105
106
	@Override
107
	public boolean isApplicable() {
108
	    Collection<FileObject> files = getCurrentlyOpenedFiles();
109
	    return files != null && !files.isEmpty();
110
	}
111
112
	@Override
113
	public SearchInfo getSearchInfo() {
114
	    Collection<FileObject> files = getCurrentlyOpenedFiles();
115
	    //use all current open files
116
	    return SearchInfoUtils.createSearchInfoForRoots(files.toArray(new FileObject[files.size()]));
117
	}
118
119
	@Override
120
	public int getPriority() {
121
	    //??
122
	    return 1500;
123
	}
124
125
	@Override
126
	public void clean() {
127
	}
128
129
	@Override
130
	public Icon getIcon() {
131
	    return ICON;
132
	}
133
134
	/**
135
	 *
136
	 * @return all the currenlty opened FileObjects
137
	 */
138
	private Collection<FileObject> getCurrentlyOpenedFiles() {
139
	    LinkedHashSet<FileObject> result = new LinkedHashSet<FileObject>();
140
	    for (TopComponent tc : TopComponent.getRegistry().getOpened()) {
141
		for (Node node : tc.getActivatedNodes()) {
142
		    if (isFromEditorWindow(node)) {
143
			DataObject dObj = node.getLookup().lookup(DataObject.class);
144
			if (null != dObj) {
145
			    FileObject primaryFile = dObj.getPrimaryFile();
146
			    if (null != primaryFile) {
147
				result.add(primaryFile);
148
			    }
149
			}
150
		    }
151
		}
152
	    }
153
	    return result;
154
	}
155
156
	protected boolean isFromEditorWindow(Node node) {
157
	    final EditorCookie editor = node.getLookup().lookup(EditorCookie.class);
158
	    if (editor != null) {
159
		return editor.getOpenedPanes() != null;
160
	    }
161
	    return false;
162
	}
163
    }
164
}

Return to bug 5009