KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > SearchResultViewEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.PlatformObject;
22
23 import org.eclipse.search.ui.ISearchResultViewEntry;
24 import org.eclipse.search.ui.SearchUI;
25
26 /**
27  * Represents an entry in the search result view
28  * @deprecated old search
29  */

30 public class SearchResultViewEntry extends PlatformObject implements ISearchResultViewEntry {
31
32     private Object JavaDoc fGroupByKey= null;
33     private IResource fResource= null;
34     private IMarker fMarker= null;
35     private ArrayList JavaDoc fMarkers= null;
36     private ArrayList JavaDoc fAttributes;
37     private int fSelectedMarkerIndex;
38     private long fModificationStamp= IResource.NULL_STAMP;
39     private String JavaDoc fMarkerType;
40     
41     public SearchResultViewEntry(Object JavaDoc groupByKey, IResource resource) {
42         fGroupByKey= groupByKey;
43         fResource= resource;
44         if (fResource != null)
45             fModificationStamp= fResource.getModificationStamp();
46     }
47     
48     //---- Accessors ------------------------------------------------
49
public Object JavaDoc getGroupByKey() {
50         return fGroupByKey;
51     }
52
53     void setGroupByKey(Object JavaDoc groupByKey) {
54         fGroupByKey= groupByKey;
55     }
56     
57     public IResource getResource() {
58         return fResource;
59     }
60     
61     public int getMatchCount() {
62         if (fMarkers != null)
63             return fMarkers.size();
64         if (fMarkers == null && fMarker != null)
65             return 1;
66         return 0;
67     }
68
69     boolean isPotentialMatch() {
70         if (fMarker != null)
71             return fMarker.getAttribute(SearchUI.POTENTIAL_MATCH, false);
72         return false;
73     }
74     
75     List JavaDoc getAttributesPerMarker() {
76         if (fAttributes == null)
77             return new ArrayList JavaDoc(0);
78         return fAttributes;
79     }
80     
81     public long getModificationStamp() {
82         return fModificationStamp;
83     }
84     
85     void clearMarkerList() {
86         fMarker= null;
87         if (fMarkers != null)
88             fMarkers.clear();
89     }
90         
91     void add(IMarker marker) {
92         if (marker != null && fMarkerType == null) {
93             try {
94                 fMarkerType= marker.getType();
95             } catch (CoreException ex) {
96                 // will default to org.eclipse.search.searchmarker
97
}
98         }
99
100         if (fMarker == null) {
101             fMarker= marker;
102             if (fMarkers != null)
103                 fMarkers.add(marker);
104             return;
105         }
106         if (fMarkers == null) {
107             fMarkers= new ArrayList JavaDoc(10);
108             addByStartpos(fMarkers, fMarker);
109         }
110         addByStartpos(fMarkers, marker);
111     }
112     
113     void setSelectedMarkerIndex(int index) {
114         fSelectedMarkerIndex= index;
115     }
116     
117     public IMarker getSelectedMarker() {
118         fSelectedMarkerIndex= Math.min(fSelectedMarkerIndex, getMatchCount() - 1);
119         if (fMarkers == null && fMarker == null)
120             return null;
121         if (fMarkers != null && fSelectedMarkerIndex >= 0)
122             return (IMarker)fMarkers.get(fSelectedMarkerIndex);
123         return fMarker;
124     }
125     
126     public List JavaDoc getMarkers() {
127         if (fMarkers == null && fMarker == null)
128             return new ArrayList JavaDoc(0);
129         else if (fMarkers == null && fMarker != null) {
130             List JavaDoc markers= new ArrayList JavaDoc(1);
131             markers.add(fMarker);
132             return markers;
133         }
134         return fMarkers;
135     }
136
137     String JavaDoc getMarkerType() {
138         if (fMarkerType == null)
139             return SearchUI.SEARCH_MARKER;
140         return fMarkerType;
141     }
142     
143     boolean contains(IMarker marker) {
144         if (fMarkers == null && fMarker == null)
145             return false;
146         if (fMarkers == null)
147             return fMarker.equals(marker);
148         return fMarkers.contains(marker);
149     }
150     
151     void remove(IMarker marker) {
152         if (marker == null)
153             return;
154             
155         if (fMarkers == null) {
156             if (fMarker != null && fMarker.equals(marker))
157                 fMarker= null;
158         }
159         else {
160             fMarkers.remove(marker);
161             if (fMarkers.size() == 1) {
162                 fMarker= (IMarker)fMarkers.get(0);
163                 fMarkers= null;
164             }
165         }
166     }
167     
168     void backupMarkers() {
169         if (fResource != null)
170             fModificationStamp= fResource.getModificationStamp();
171         List JavaDoc markers= getMarkers();
172         fAttributes= new ArrayList JavaDoc(markers.size());
173         Iterator JavaDoc iter= markers.iterator();
174         while (iter.hasNext()) {
175             IMarker marker= (IMarker)iter.next();
176             Map JavaDoc attributes= null;
177             try {
178                 attributes= marker.getAttributes();
179             } catch (CoreException ex) {
180                 // don't backup corrupt marker
181
continue;
182             }
183             fAttributes.add(attributes);
184         }
185     }
186     
187     private void addByStartpos(ArrayList JavaDoc markers, IMarker marker) {
188         int startPos= marker.getAttribute(IMarker.CHAR_START, -1);
189         int i= 0;
190         int markerCount= markers.size();
191         while (i < markerCount && startPos >= ((IMarker)markers.get(i)).getAttribute(IMarker.CHAR_START, -1))
192             i++;
193         markers.add(i, marker);
194         if (i == 0)
195             fMarker= marker;
196     }
197     
198     /* (non-Javadoc)
199      * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
200      */

201     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
202         return super.getAdapter(adapter);
203     }
204 }
205
Popular Tags