summaryrefslogtreecommitdiff
path: root/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/MatchConstraintsSink.java
blob: 2036c83d56f4964781e4fc4ad0925ff0eac8877a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.intellij.structuralsearch.impl.matcher;

import com.intellij.psi.PsiElement;
import com.intellij.structuralsearch.MatchResultSink;
import com.intellij.structuralsearch.MatchingProcess;
import com.intellij.structuralsearch.MatchResult;
import com.intellij.structuralsearch.SSRBundle;
import com.intellij.psi.PsiFile;
import com.intellij.openapi.progress.ProgressIndicator;

import javax.swing.*;
import java.util.HashMap;

/**
 * Sink to detect
 */
class MatchConstraintsSink implements MatchResultSink {
  private final MatchResultSink delegate;
  private MatchingProcess process;
  private final boolean distinct;
  private final boolean caseSensitive;
  private int matchCount;
  private final int maxMatches;
  private final HashMap<Object, MatchResult> matches = new HashMap<Object, MatchResult>();

  MatchConstraintsSink(MatchResultSink _delegate, int _maxMatches,boolean distinct, boolean _caseSensitive) {
    delegate = _delegate;
    maxMatches = _maxMatches;
    this.distinct = distinct;
    caseSensitive = _caseSensitive;
  }

  public void newMatch(MatchResult result) {
    if (distinct) {
      String matchImage = result.getMatchImage();

      if (!caseSensitive) matchImage = matchImage.toLowerCase();

      if (matches.get(matchImage)!=null) {
        return;
      }

      matches.put(matchImage,result);
    }
    else {
      final PsiElement match = result.getMatch();
      if (matches.containsKey(match)) {
        return;
      }
      matches.put(match, result);
    }

    delegate.newMatch(result);
    ++matchCount;

    if (matchCount==maxMatches) {
      JOptionPane.showMessageDialog(null, SSRBundle.message("search.produced.too.many.results.message"));
      process.stop();
    }
  }

  /* Notifies sink about starting the matching for given element
   * @param element the current file
   * @param task process continuation reference
   */
  public void processFile(PsiFile element) {
    delegate.processFile(element);
  }

  public void setMatchingProcess(MatchingProcess matchingProcess) {
    process = matchingProcess;
    delegate.setMatchingProcess(process);
  }

  public void matchingFinished() {
    matchCount = 0;
    matches.clear();
    delegate.matchingFinished();
  }

  public ProgressIndicator getProgressIndicator() {
    return delegate.getProgressIndicator();
  }

}