/plugins/ErrorList/tags/release-1-2-2/errorlist/ErrorHighlight.java

# · Java · 165 lines · 108 code · 27 blank · 30 comment · 28 complexity · 83079c5580cc9fde5c37fb62d8843795 MD5 · raw file

  1. /*
  2. * ErrorHighlight.java - "Wavy red underlines"
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package errorlist;
  23. //{{{ Imports
  24. import javax.swing.text.Segment;
  25. import java.awt.event.*;
  26. import java.awt.*;
  27. import org.gjt.sp.jedit.syntax.*;
  28. import org.gjt.sp.jedit.textarea.*;
  29. import org.gjt.sp.jedit.*;
  30. //}}}
  31. public class ErrorHighlight extends TextAreaExtension
  32. {
  33. //{{{ ErrorHighlight constructor
  34. public ErrorHighlight(JEditTextArea textArea)
  35. {
  36. this.textArea = textArea;
  37. seg = new Segment();
  38. point = new Point();
  39. } //}}}
  40. //{{{ paintValidLine() method
  41. public void paintValidLine(Graphics2D gfx, int screenLine, int physicalLine,
  42. int start, int end, int y)
  43. {
  44. ErrorSource[] errorSources = ErrorSource.getErrorSources();
  45. FontMetrics fm = textArea.getPainter().getFontMetrics();
  46. y += (fm.getHeight() - fm.getDescent() - fm.getLeading());
  47. for(int i = 0; i < errorSources.length; i++)
  48. {
  49. ErrorSource source = errorSources[i];
  50. ErrorSource.Error[] lineErrors = source.getLineErrors(
  51. textArea.getBuffer(),physicalLine);
  52. if(lineErrors != null)
  53. {
  54. paintLineErrors(lineErrors,gfx,physicalLine,
  55. start,end,y);
  56. }
  57. }
  58. } //}}}
  59. //{{{ getToolTipText() method
  60. public String getToolTipText(int x, int y)
  61. {
  62. ErrorSource[] errorSources = ErrorSource.getErrorSources();
  63. if(!textArea.getBuffer().isLoaded())
  64. return null;
  65. int offset = textArea.xyToOffset(x,y);
  66. if(offset == -1)
  67. return null;
  68. int line = textArea.getLineOfOffset(offset);
  69. for(int i = 0; i < errorSources.length; i++)
  70. {
  71. ErrorSource.Error[] lineErrors =
  72. errorSources[i].getLineErrors(
  73. textArea.getBuffer(),line);
  74. if(lineErrors == null)
  75. continue;
  76. for(int j = 0; j < lineErrors.length; j++)
  77. {
  78. ErrorSource.Error error = lineErrors[j];
  79. int start = error.getStartOffset();
  80. int end = error.getEndOffset();
  81. if(offset >= start && offset <= end
  82. || (start == 0 && end == 0))
  83. return error.getErrorMessage();
  84. }
  85. }
  86. return null;
  87. } //}}}
  88. //{{{ Private members
  89. private JEditTextArea textArea;
  90. private Segment seg;
  91. private Point point;
  92. //{{{ paintLineErrors() method
  93. private void paintLineErrors(ErrorSource.Error[] lineErrors,
  94. Graphics2D gfx, int line, int _start, int _end, int y)
  95. {
  96. int lineStart = textArea.getLineStartOffset(line);
  97. for(int i = 0; i < lineErrors.length; i++)
  98. {
  99. ErrorSource.Error error = lineErrors[i];
  100. int start = error.getStartOffset();
  101. int end = error.getEndOffset();
  102. if(start == 0 && end == 0)
  103. {
  104. textArea.getLineText(line,seg);
  105. for(int j = 0; j < seg.count; j++)
  106. {
  107. if(Character.isWhitespace(seg.array[seg.offset + j]))
  108. start++;
  109. else
  110. break;
  111. }
  112. end = seg.count;
  113. }
  114. if(start + lineStart >= _end || end + lineStart <= _start)
  115. continue;
  116. if(start + lineStart >= _start)
  117. start = textArea.offsetToXY(line,start,point).x;
  118. else
  119. start = 0;
  120. if(end + lineStart >= _end)
  121. end = textArea.offsetToXY(line,_end - 1,point).x;
  122. else
  123. end = textArea.offsetToXY(line,end,point).x;
  124. gfx.setColor(ErrorListPlugin.getErrorColor(error.getErrorType()));
  125. paintWavyLine(gfx,y,start,end);
  126. }
  127. } //}}}
  128. //{{{ paintWavyLine() method
  129. private void paintWavyLine(Graphics2D gfx, int y, int start, int end)
  130. {
  131. for(int i = start; i < end; i+= 6)
  132. {
  133. gfx.drawLine(i,y + 3,i + 3,y + 1);
  134. gfx.drawLine(i + 3,y + 1,i + 6,y + 3);
  135. }
  136. } //}}}
  137. //}}}
  138. }