1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 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 30 public class SearchResultViewEntry extends PlatformObject implements ISearchResultViewEntry { 31 32 private Object fGroupByKey= null; 33 private IResource fResource= null; 34 private IMarker fMarker= null; 35 private ArrayList fMarkers= null; 36 private ArrayList fAttributes; 37 private int fSelectedMarkerIndex; 38 private long fModificationStamp= IResource.NULL_STAMP; 39 private String fMarkerType; 40 41 public SearchResultViewEntry(Object groupByKey, IResource resource) { 42 fGroupByKey= groupByKey; 43 fResource= resource; 44 if (fResource != null) 45 fModificationStamp= fResource.getModificationStamp(); 46 } 47 48 public Object getGroupByKey() { 50 return fGroupByKey; 51 } 52 53 void setGroupByKey(Object 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 getAttributesPerMarker() { 76 if (fAttributes == null) 77 return new ArrayList (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 } 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 (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 getMarkers() { 127 if (fMarkers == null && fMarker == null) 128 return new ArrayList (0); 129 else if (fMarkers == null && fMarker != null) { 130 List markers= new ArrayList (1); 131 markers.add(fMarker); 132 return markers; 133 } 134 return fMarkers; 135 } 136 137 String 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 markers= getMarkers(); 172 fAttributes= new ArrayList (markers.size()); 173 Iterator iter= markers.iterator(); 174 while (iter.hasNext()) { 175 IMarker marker= (IMarker)iter.next(); 176 Map attributes= null; 177 try { 178 attributes= marker.getAttributes(); 179 } catch (CoreException ex) { 180 continue; 182 } 183 fAttributes.add(attributes); 184 } 185 } 186 187 private void addByStartpos(ArrayList 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 201 public Object getAdapter(Class adapter) { 202 return super.getAdapter(adapter); 203 } 204 } 205 | Popular Tags |