/plugins/TaskList/tags/TaskList_0_4_2/tasklist/TaskHighlight.java

# · Java · 246 lines · 114 code · 21 blank · 111 comment · 18 complexity · 173843ef497022b413eb6aece3695845 MD5 · raw file

  1. /*
  2. * TaskHighlight.java - TaskList plugin
  3. * Copyright (C) 2001 Oliver Rutherfurd
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * $Id: TaskHighlight.java 11758 2003-03-10 13:37:47Z fruhstuck $
  20. */
  21. package tasklist;
  22. //{{{ imports
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import java.util.Enumeration;
  26. import java.util.Hashtable;
  27. import javax.swing.text.Segment;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.jedit.syntax.*;
  30. import org.gjt.sp.jedit.textarea.*;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. /**
  34. * A class extending jEdit's TextAreaExtension class
  35. * that, when enabled, draws a wavy line underscoring a task item
  36. * appearing in the text of a buffer.
  37. */
  38. public class TaskHighlight extends TextAreaExtension
  39. {
  40. //{{{ constructor
  41. /**
  42. * Constructs a TextHighlight object
  43. * @param textArea The text area
  44. */
  45. public TaskHighlight(JEditTextArea textArea)
  46. {
  47. super();
  48. this.textArea = textArea;
  49. this.highlightEnabled = jEdit.getBooleanProperty("tasklist.highlight.tasks");
  50. this.seg = new Segment();
  51. this.point = new Point();
  52. }//}}}
  53. //{{{ isEnabled() method
  54. /**
  55. * Returns whether highlighting of task items is currently enabled.
  56. *
  57. * @return the value true or false indicating whther highlighting
  58. * is enabled
  59. */
  60. public boolean isEnabled()
  61. {
  62. return highlightEnabled;
  63. }//}}}
  64. //{{{ setEnabled(boolean enabled) method
  65. /**
  66. * Set whether highlighting of task items is enabled; does not
  67. * redraw the text area after a change in state.
  68. *
  69. * @param the new state for task highlighting
  70. */
  71. public void setEnabled(boolean enabled)
  72. {
  73. highlightEnabled = enabled;
  74. }//}}}
  75. //{{{ paintValidLine() method
  76. /**
  77. * Called by the text area to paint a highlight on a task line
  78. * (when highlighting is enabled)
  79. * @param gfx The graphics context
  80. * @param screenLine The screen line number
  81. * @param physicalLine The physical line number
  82. * @param start The offset where the screen line begins, from the start of the buffer
  83. * @param end The offset where the screen line ends, from the start of the buffer
  84. * @param y The y co-ordinate of the top of the line's bounding box
  85. */
  86. public void paintValidLine(Graphics2D gfx, int screenLine,
  87. int physicalLine, int start, int end, int y)
  88. {
  89. // Log.log(Log.DEBUG,this,"paintValidLine() for line " +
  90. // String.valueOf(physicalLine));
  91. Buffer buffer = textArea.getBuffer();
  92. if(!highlightEnabled || !buffer.isLoaded() ||
  93. physicalLine >= buffer.getLineCount())
  94. {
  95. return;
  96. }
  97. Hashtable taskMap =
  98. TaskListPlugin.requestTasksForBuffer(buffer);
  99. if(taskMap != null)
  100. {
  101. Task task = null;
  102. Integer _line = new Integer(physicalLine);
  103. if(!buffer.isDirty())
  104. {
  105. task = (Task)taskMap.get(_line);
  106. }
  107. else
  108. {
  109. Enumeration enum = taskMap.elements();
  110. while(enum.hasMoreElements())
  111. {
  112. Task _task = (Task)enum.nextElement();
  113. if(_task.getLineNumber() == _line.intValue())
  114. {
  115. task = _task;
  116. break;
  117. }
  118. }
  119. }
  120. if(task != null)
  121. {
  122. // Log.log(Log.DEBUG,this,"Found task where physical line = "
  123. // + String.valueOf(physicalLine));
  124. FontMetrics fm = textArea.getPainter().getFontMetrics();
  125. y -= (fm.getDescent() + fm.getLeading());
  126. underlineTask(task, gfx, physicalLine, start, end, y);
  127. }
  128. }
  129. }//}}}
  130. //{{{ getToolTipText() method
  131. /**
  132. * Returns the tool tip to display at the specified location.
  133. * @param x The x-coordinate
  134. * @param y The y-coordinate
  135. */
  136. public java.lang.String getToolTipText(int x, int y)
  137. {
  138. return super.getToolTipText(x, y);
  139. }//}}}
  140. //{{{ private members
  141. /**
  142. * The textArea on which the highlight will be drawn.
  143. */
  144. private JEditTextArea textArea;
  145. /**
  146. * A flag indicating whether highlighting of task items
  147. * is currently enabled.
  148. */
  149. private boolean highlightEnabled;
  150. /**
  151. *
  152. * A portion of text to be highlighted.
  153. */
  154. private Segment seg;
  155. /**
  156. * A point for anchor the highlighting of taks text
  157. */
  158. private Point point;
  159. //}}}
  160. //{{{ underlineTask() method
  161. /**
  162. * Implements underlining of task items through a call to
  163. * paintWavyLine()
  164. *
  165. * @param task the Task that is the subject of highlighting
  166. * @param gfx The graphics context
  167. * @param line The physical line number
  168. * @param _start The offset where the line begins
  169. * @param _end The offset where the line ends
  170. * @param y The y co-ordinate of the line
  171. */
  172. private void underlineTask(Task task,
  173. Graphics2D gfx, int line, int _start, int _end, int y)
  174. {
  175. // Log.log(Log.DEBUG,this,"Calling underlineTask() for line "
  176. // + String.valueOf(line) + "....");
  177. int start = task.getStartOffset();
  178. int end = task.getEndOffset();
  179. if(start == 0 && end == 0)
  180. {
  181. // Log.log(Log.DEBUG,this,"Reseting start and end in underlineTask()....");
  182. textArea.getLineText(line,seg);
  183. for(int j = 0; j < seg.count; j++)
  184. {
  185. if(Character.isWhitespace(seg.array[seg.offset + j]))
  186. start++;
  187. else
  188. break;
  189. }
  190. end = seg.count;
  191. }
  192. //if(start >= _end || end <= _start)
  193. // return;
  194. if(start + textArea.getLineStartOffset(line) >= _start)
  195. start = textArea.offsetToXY(line,start,point).x;
  196. else
  197. start = 0;
  198. if(end + textArea.getLineStartOffset(line) >= _end)
  199. end = textArea.offsetToXY(line,_end - 1,point).x;
  200. else
  201. end = textArea.offsetToXY(line,end,point).x;
  202. gfx.setColor(TaskListPlugin.getHighlightColor());
  203. paintWavyLine(gfx,y,start,end);
  204. }//}}}
  205. //{{{ paintWavyLine() method
  206. /**
  207. * Draws a wavy line at the indicated coordinates
  208. *
  209. * @param gfx The graphics context
  210. * @param y The y-coordinate representing the lower bound of the wavy line
  211. * @param start The x-coordinate of the start of the line
  212. * @param end The x-coordinate of the end of the line
  213. */
  214. private void paintWavyLine(Graphics2D gfx, int y, int start, int end)
  215. {
  216. // Log.log(Log.DEBUG,this,"Calling paintWavyLine()....");
  217. y += textArea.getPainter().getFontMetrics().getHeight();
  218. for(int i = start; i < end; i+= 6)
  219. {
  220. gfx.drawLine(i,y + 3,i + 3,y + 1 );
  221. gfx.drawLine(i + 3,y + 1,i + 6,y + 3);
  222. }
  223. }//}}}
  224. }
  225. // :collapseFolds=1:folding=explicit:indentSize=4:lineSeparator=\n:noTabs=false:tabSize=4: