001: package tide.outputtabs.search;
002:
003: import java.util.*;
004: import tide.sources.TypeLocator;
005: import snow.utils.storage.StorageVector;
006: import tide.sources.FileItem;
007:
008: public final class SearchHit implements Comparable<SearchHit> // sort using increasing line number.
009: {
010: //public boolean isSelectedInTable = false; //TODO
011:
012: public FileItem fileItem;
013: public int line;
014: public int column;
015: // Caution: length is not robust for multiline hits...
016: public int lineEnd, colEnd;
017: public List<String> supplementaryRegexCaptureGroups = new ArrayList<String>();
018:
019: public String capture = ""; // useful when regex search,for example IPs of mails or url's
020:
021: public SearchHit(FileItem fileItem, int lineS, int colS, int lineE,
022: int colE, String capture) {
023: this .fileItem = fileItem;
024: this .line = lineS;
025: this .column = colS;
026: this .lineEnd = lineE;
027: this .colEnd = colE;
028: //this.length = len; // Caution: len is not robust for multiline hits...
029: this .capture = capture;
030: }
031:
032: /** Constructor to use for a hit only on the file name.
033: */
034: public SearchHit(FileItem fileItem) {
035: this (fileItem, -1, -1, -1, -1, "");
036: }
037:
038: public StorageVector getStorageRepresentation() {
039: StorageVector sv = new StorageVector();
040: sv.add(2);
041: sv.add(fileItem.getJavaName());
042: sv.add(line);
043: sv.add(column);
044: sv.add(lineEnd);
045: sv.add(colEnd);
046: sv.add(capture);
047: sv.add(supplementaryRegexCaptureGroups);
048: return sv;
049: }
050:
051: /** Used for clipboard export (supports copy-paste in execution tab with jump/selection recognition)
052: */
053: @Override
054: public String toString() {
055: StringBuilder sb = new StringBuilder(fileItem.getJavaName());
056: sb.append(".java:");
057: if (line >= 0) {
058: sb.append(line);
059: if (column >= 0) {
060: sb.append(":" + column);
061: /* if(length>0)
062: {
063: sb.append(":"+length);
064: }*/
065: }
066: }
067: if (capture.length() > 0) {
068: sb.append(": " + capture);
069: }
070: return sb.toString();
071: }
072:
073: @SuppressWarnings("unchecked")
074: public static SearchHit createFromStorageVector(StorageVector sv) {
075: int version = (Integer) sv.get(0);
076: FileItem fileItem = TypeLocator.locateQuick((String) sv.get(1));
077: if (fileItem == null) {
078: // occurs when some rename has been made...
079: // and rename occured outside... no chance to solve...
080: //new Throwable("not found: "+sv.get(1)).printStackTrace();
081: return null;
082: }
083:
084: if (version == 1) {
085: int len = (Integer) sv.get(4);
086: int lineStart = (Integer) sv.get(2);
087: int colStart = (Integer) sv.get(3);
088: // tricky transition
089: return new SearchHit(fileItem, lineStart, colStart,
090: lineStart, colStart + len, (String) sv.get(5));
091: } else {
092: SearchHit sh = new SearchHit(fileItem, (Integer) sv.get(2),
093: (Integer) sv.get(3), (Integer) sv.get(4),
094: (Integer) sv.get(5), (String) sv.get(6));
095:
096: if (sv.size() > 7) {
097: sh.supplementaryRegexCaptureGroups
098: .addAll((List<String>) sv.get(7));
099: }
100:
101: return sh;
102: }
103: }
104:
105: /** Compares the start positions {name, line, column}
106: */
107: public int compareTo(SearchHit h2) {
108: int cn = fileItem.getJavaName().compareTo(
109: h2.fileItem.getJavaName());
110: if (cn != 0)
111: return cn;
112: cn = Integer.valueOf(line).compareTo(h2.line);
113: if (cn != 0)
114: return cn;
115: cn = Integer.valueOf(column).compareTo(h2.column);
116: if (cn != 0)
117: return cn;
118: //cn = Integer.valueOf(length).compareTo(h2.length);
119: //if(cn!=0) return cn;
120: return 0;
121: }
122:
123: /** Used during editor line inserts, very practical !!
124: * (but not 100% perfect, cols are ignored)...
125: */
126: public final void shiftLine(int inc) {
127: line += inc;
128: lineEnd += inc;
129: }
130:
131: }
|