001: package tide.outputtabs.search;
002:
003: import tide.editor.MainEditorFrame;
004: import snow.utils.storage.StorageVector;
005: import snow.utils.storage.FileUtils;
006: import java.io.File;
007: import tide.project.ProjectSettings;
008: import java.awt.EventQueue;
009: import tide.sources.TypeLocator;
010: import java.io.StringReader;
011: import java.io.BufferedReader;
012: import java.util.*;
013: import tide.sources.FileItem;
014:
015: /** Stored per file.
016: */
017: public final class SearchResultsManager {
018: static SearchResultsManager instance;
019:
020: // the hits, stored per file (key=javaName)
021: final Map<String, ArrayList<SearchHit>> hitsForFile = new HashMap<String, ArrayList<SearchHit>>();
022:
023: private SearchResultsManager() {
024: }
025:
026: public void clear() {
027: hitsForFile.clear();
028: SearchTab.getInstance().refresh();
029: SearchTab.getInstance().setDescription("");
030: SearchTab.getInstance().setHits(-1, 0, false);
031: }
032:
033: public List<SearchHit> getHits(String javaName) {
034: return hitsForFile.get(javaName);
035: }
036:
037: public int getTotalHitsCount() {
038: int n = 0;
039: for (ArrayList<SearchHit> sh : hitsForFile.values()) {
040: n += sh.size();
041: }
042: return n;
043: }
044:
045: public int getMaxNumberOfRegexSubgroups() {
046: int n = 0;
047: for (ArrayList<SearchHit> sh : hitsForFile.values()) {
048: for (SearchHit shit : sh) {
049: n = Math.max(shit.supplementaryRegexCaptureGroups
050: .size(), n);
051: }
052: }
053: return n;
054: }
055:
056: public boolean hasHits(String javaName) {
057: if (!hitsForFile.containsKey(javaName))
058: return false;
059: return hitsForFile.get(javaName).size() > 0;
060: }
061:
062: public static void add(SearchHit mess) {
063: synchronized (SearchResultsManager.getInstance()) {
064: SearchResultsManager.getInstance().add_(
065: mess.fileItem.getJavaName(), mess);
066: }
067: }
068:
069: public void remove(SearchHit mess) {
070: hitsForFile.get(mess.fileItem.getJavaName()).remove(mess);
071: }
072:
073: private void add_(String javaName, SearchHit mess) {
074: if (!hitsForFile.containsKey(javaName))
075: hitsForFile.put(javaName, new ArrayList<SearchHit>());
076: hitsForFile.get(javaName).add(mess);
077: }
078:
079: public void lineInsertOccured(String javaName, int linePos, int diff) {
080: boolean has = false;
081: List<SearchHit> hs = getHits(javaName);
082: if (hs == null)
083: return;
084:
085: for (SearchHit hi : hs) {
086: if (hi.line > linePos) {
087: has = true;
088: hi.shiftLine(diff);
089: }
090: }
091:
092: if (has) {
093: // no refresh needed...
094: }
095: }
096:
097: public void lineRemoved(String javaName, int lineStart, int lineEnd) {
098: List<SearchHit> messs = getHits(javaName);
099: if (messs == null)
100: return;
101:
102: List<SearchHit> toRemove = new ArrayList<SearchHit>();
103: for (SearchHit mess : messs) {
104: if (mess.line >= lineStart && mess.line <= lineEnd) {
105: // has = true;
106: toRemove.add(mess);
107: }
108: }
109:
110: if (toRemove.size() > 0) {
111: for (SearchHit m : toRemove) {
112: remove(m);
113: }
114: SearchTab.getInstance().refresh();
115: }
116: }
117:
118: public static synchronized SearchResultsManager getInstance() {
119: if (instance == null)
120: instance = new SearchResultsManager();
121: return instance;
122: }
123:
124: /** Isn't that luxury ? even the oldest tIDE format is recognized.
125: */
126: public void setOldSearchResults(String st) {
127:
128: System.out
129: .println("initialiting new search tab with old results");
130: clear();
131: try {
132: BufferedReader br = new BufferedReader(new StringReader(st));
133: String li;
134: int pos;
135: boolean first = true;
136: while ((li = br.readLine()) != null) {
137: if (first)
138: SearchTab.getInstance().setDescription(li);
139: first = false;
140: pos = li.indexOf(".java:");
141: if (pos > 0) {
142: String jn = li.substring(0, pos);
143: Scanner sc = new Scanner(li.substring(pos + 6));
144: sc.useDelimiter(":");
145: int line = sc.nextInt();
146: int col = sc.hasNextInt() ? sc.nextInt() : -1;
147: int len = sc.hasNextInt() ? sc.nextInt() : -1;
148: sc.useDelimiter("\n");
149: String mt = sc.hasNext() ? sc.next() : "";
150: if (mt.startsWith(": "))
151: mt = mt.substring(2);
152: //System.out.println(""+jn+";"+line+" "+col+" "+len+" "+mt);
153:
154: FileItem fi = TypeLocator.locateQuick(jn);
155: if (fi == null) {
156: System.out.println("search hit not found: "
157: + li);
158: } else {
159: // tricky conversion...
160: add(new SearchHit(fi, line, col, line, col
161: + len, mt));
162: }
163: }
164: }
165: } catch (Exception e) {
166: e.printStackTrace();
167: }
168:
169: EventQueue.invokeLater(new Runnable() {
170: public void run() {
171: SearchTab.getInstance().refresh();
172: }
173: });
174:
175: }
176:
177: /** Called after project close (on exit or when loading another project)
178: */
179: public void storeToFile_(ProjectSettings settings) throws Exception {
180:
181: File projFile = MainEditorFrame.instance.getActualProjectFile();
182: //new Throwable("store messages to "+projFile).printStackTrace();
183:
184: if (projFile == null)
185: return; // can occur, when not saving...
186:
187: File dir = settings.getProjectSettingsFolder();
188: if (!dir.exists())
189: dir.mkdirs();
190:
191: File fs = new File(dir, ".searchesz");
192: StorageVector vr = getStorageRepresentation();
193:
194: FileUtils.saveZippedVectorToFile(fs, vr);
195:
196: }
197:
198: /** Should be called when all the types have been loaded !
199: * @return true only if a valid restoration was performed (file existing, ...)
200: */
201: public boolean restoreFromFile(ProjectSettings settings)
202: throws Exception {
203:
204: File fs = new File(settings.getProjectSettingsFolder(),
205: ".searchesz");
206: // new Throwable(""+fs).printStackTrace();
207:
208: this .clear();
209: //refreshView();
210:
211: if (fs.exists()) {
212: try {
213: StorageVector sv = FileUtils
214: .loadZippedVectorFromFile3(fs);
215: this .createFromStorageVector(sv);
216: return true;
217: } catch (Exception e) {
218: throw e;
219: } finally {
220: SearchTab.getInstance().setHits(getTotalHitsCount(),
221: getMaxNumberOfRegexSubgroups(), false);
222: SearchTab.getInstance().refresh();
223: //refreshView();
224: }
225: } else {
226: SearchTab.getInstance().setHits(-1, 0, false);
227: SearchTab.getInstance().refresh();
228: }
229:
230: return false;
231: }
232:
233: private StorageVector getStorageRepresentation() {
234: StorageVector sv = new StorageVector();
235: sv.add(1);
236: StorageVector svMess = new StorageVector();
237: sv.add(svMess);
238: sv.add(SearchTab.getInstance().searchLabel.getText());
239:
240: for (ArrayList<SearchHit> lms : hitsForFile.values()) {
241: for (SearchHit lm : lms) {
242: svMess.add(lm.getStorageRepresentation());
243: }
244: }
245: return sv;
246: }
247:
248: private void createFromStorageVector(StorageVector sv) {
249: synchronized (instance) {
250: int version = (Integer) sv.get(0);
251: StorageVector messV = (StorageVector) sv.get(1);
252: for (Object o : messV) {
253: StorageVector mv = (StorageVector) o;
254: SearchHit shit = SearchHit.createFromStorageVector(mv);
255: if (shit != null) {
256: add(shit);
257: }
258: }
259:
260: if (sv.size() > 2) {
261: SearchTab.getInstance().searchLabel.setText((String) sv
262: .get(2));
263: }
264:
265: }
266: }
267:
268: public static void main(String[] args) {
269: SearchResultsManager srm = new SearchResultsManager();
270: srm
271: .setOldSearchResults("nnnn\ntide.editor.MainEditorFrame.java:2043:59:11: NO FILES TO\nhh");
272: }
273:
274: }
|