\r
package eu.etaxonomy.taxeditor.annotatedlineeditor;\r
\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.Map;\r
+import java.util.ArrayList;\r
\r
import org.apache.log4j.Logger;\r
import org.eclipse.jface.text.BadLocationException;\r
-import org.eclipse.jface.text.Document;\r
import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IRegion;\r
+import org.eclipse.jface.text.Region;\r
import org.eclipse.jface.text.TextAttribute;\r
-import org.eclipse.jface.text.rules.IPredicateRule;\r
+import org.eclipse.jface.text.rules.IPartitionTokenScanner;\r
import org.eclipse.jface.text.rules.IToken;\r
-import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;\r
-import org.eclipse.jface.text.rules.SingleLineRule;\r
import org.eclipse.jface.text.rules.Token;\r
import org.eclipse.jface.text.source.ISourceViewer;\r
import org.eclipse.swt.SWT;\r
* @created 28.10.2009\r
* @version 1.0\r
*/\r
-public class SupplementalTextScanner extends RuleBasedPartitionScanner {\r
+public class SupplementalTextScanner implements IPartitionTokenScanner { \r
private static final Logger logger = Logger\r
.getLogger(SupplementalTextScanner.class);\r
\r
private ILineDisplayStrategy lineDisplayStrategy;\r
- private Document document;\r
private ISourceViewer sourceViewer;\r
\r
- private Map<Integer, String> supplementalTexts;\r
- private SupplementalTextRule rule;\r
- private int lastLine;\r
+ private IToken supplementalTextToken;\r
+ private ArrayList<IRegion> supplementalTextRegions;\r
+ private int regionsIndex;\r
+\r
+ private int tokenOffset;\r
+ private int tokenLength;\r
\r
/**\r
* @param sourceViewer\r
this.lineDisplayStrategy = lineDisplayStrategy;\r
\r
TextAttribute textAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY), null, SWT.ITALIC);\r
- IToken supplementalTextToken = new Token(textAttribute);\r
- \r
- rule = new SupplementalTextRule(supplementalTextToken);\r
- setPredicateRules(new IPredicateRule[] {rule});\r
+ supplementalTextToken = new Token(textAttribute);\r
}\r
\r
/* (non-Javadoc)\r
- * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)\r
+ * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()\r
*/\r
- @Override\r
- public void setPartialRange(IDocument document, int offset, int length,\r
- String contentType, int partitionOffset) {\r
- \r
- // Get all supplemental texts in the partial range\r
- supplementalTexts = new HashMap<Integer, String>();\r
- LineAnnotationModel model = (LineAnnotationModel) sourceViewer.getAnnotationModel();\r
- Iterator iter = model.getAnnotationIterator(offset, length, true, true);\r
- while (iter.hasNext()) {\r
- Object next = iter.next();\r
- if (next instanceof LineAnnotation) {\r
- Object entity = ((LineAnnotation) next).getEntity();\r
- String supplementalText = lineDisplayStrategy.getSupplementalText(entity);\r
- if (supplementalText != null && supplementalText.length() > 0) {\r
- try {\r
- int line = document.getLineOfOffset(model.getPosition((LineAnnotation) next).getOffset());\r
- supplementalTexts.put(line, supplementalText);\r
- } catch (BadLocationException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
- }\r
- }\r
- this.document = (Document) document; \r
- super.setPartialRange(document, offset, length, contentType, partitionOffset);\r
+ public int getTokenLength() {\r
+ return tokenLength;\r
}\r
- \r
+\r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()\r
+ */\r
+ public int getTokenOffset() {\r
+ return tokenOffset;\r
+ }\r
+\r
/* (non-Javadoc)\r
- * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#nextToken()\r
+ * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()\r
*/\r
- @Override\r
public IToken nextToken() {\r
- try {\r
- int line = document.getLineOfOffset(getTokenOffset());\r
- if (line != lastLine) {\r
- rule.setStartSequence(supplementalTexts.get(line));\r
- }\r
- lastLine = line;\r
- } catch (BadLocationException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
+ if (regionsIndex < supplementalTextRegions.size()) {\r
+ tokenOffset = supplementalTextRegions.get(regionsIndex).getOffset();\r
+ tokenLength = supplementalTextRegions.get(regionsIndex).getLength();\r
+ regionsIndex++;\r
+ return supplementalTextToken;\r
}\r
- return super.nextToken();\r
+ return Token.EOF;\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)\r
+ */\r
+ public void setRange(IDocument document, int offset, int length) {\r
+ setPartialRange(document, offset, length, null, -1);\r
}\r
- \r
- class SupplementalTextRule extends SingleLineRule {\r
\r
- static final String DUMMY_SEQUENCE = "_dummy_sequence";\r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)\r
+ */\r
+ public void setPartialRange(IDocument document, int offset, int length,\r
+ String contentType, int partitionOffset) {\r
\r
- /**\r
- * @param startSequence\r
- * @param endSequence\r
- * @param token\r
- * @param escapeCharacter\r
- * @param breaksOnEOL\r
- */\r
- public SupplementalTextRule(IToken token) {\r
- super(DUMMY_SEQUENCE, null, token, '\\', true);\r
- }\r
- \r
- protected void setStartSequence(String sequence) {\r
- if (sequence != null && sequence.length() > 0) {\r
- fStartSequence = sequence.toCharArray();\r
- }\r
- }\r
+ LineAnnotationModel model = (LineAnnotationModel) sourceViewer.getAnnotationModel();\r
+ supplementalTextRegions = new ArrayList<IRegion>();\r
+ regionsIndex = 0;\r
\r
+ try {\r
+ int lineCount = document.getNumberOfLines(offset, length);\r
+ int lineStart = document.getLineOfOffset(offset);\r
+ for (int i = lineStart; i < lineStart + lineCount; i++) {\r
+ LineAnnotation annotation = (LineAnnotation) model.getAnnotationAtLine(i, document);\r
+ if (annotation != null) {\r
+ String supplementalText = lineDisplayStrategy.getSupplementalText(annotation.getEntity());\r
+ if (supplementalText != null && supplementalText.length() > 0) {\r
+ IRegion lineInfo = document.getLineInformation(i);\r
+ String lineText = document.get(lineInfo.getOffset(), lineInfo.getLength());\r
+ int supplementalTextLineOffset = lineText.indexOf(supplementalText);\r
+ if (supplementalTextLineOffset > -1) {\r
+ supplementalTextRegions.add(new Region(lineInfo.getOffset() + supplementalTextLineOffset, supplementalText.length()));\r
+ }\r
+ }\r
+ }\r
+ } \r
+ } catch (BadLocationException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
}\r
}\r