-// $Id$\r
-/**\r
-* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
-* http://www.e-taxonomy.eu\r
-* \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-\r
-package eu.etaxonomy.taxeditor.annotatedlineeditor;\r
-\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.Map;\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.TextAttribute;\r
-import org.eclipse.jface.text.rules.IPredicateRule;\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
-import org.eclipse.swt.widgets.Display;\r
-\r
-/**\r
- * Gives a different color and font style to all text identified as supplemental text by \r
- * its corresponding LineAnnotation. \r
- * \r
- * Note: implementing IPartitionScanner in a straightforward fashion proved buggy, so this more obtuse\r
- * extension of RuleBasedPartitionScanner should be used instead. Uses a matching IRule whose pattern changes\r
- * depending on the current line. \r
- * \r
- * @author p.ciardelli\r
- * @created 28.10.2009\r
- * @see CopyOfSupplementalTextScanner\r
- * @version 1.0\r
- */\r
-public class SupplementalTextScanner extends RuleBasedPartitionScanner {\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
- \r
- /**\r
- * @param sourceViewer\r
- * @param lineDisplayStrategy\r
- */\r
- public SupplementalTextScanner(ISourceViewer sourceViewer, ILineDisplayStrategy lineDisplayStrategy) {\r
- this.sourceViewer = 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
- }\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
- */\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
- }\r
- \r
- /* (non-Javadoc)\r
- * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#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
- logger.debug("Ignoring", e);\r
- }\r
- return super.nextToken();\r
- }\r
- \r
- class SupplementalTextRule extends SingleLineRule {\r
-\r
- static final String DUMMY_SEQUENCE = "_dummy_sequence";\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
- /**\r
- * Set pattern match for rule.\r
- * \r
- * @param sequence\r
- */\r
- protected void setStartSequence(String sequence) {\r
- if (sequence != null && sequence.length() > 0) {\r
- fStartSequence = sequence.toCharArray();\r
- }\r
- }\r
- \r
- }\r
-}\r
+// $Id$
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.annotatedlineeditor;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.TextAttribute;
+import org.eclipse.jface.text.rules.IPredicateRule;
+import org.eclipse.jface.text.rules.IToken;
+import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
+import org.eclipse.jface.text.rules.SingleLineRule;
+import org.eclipse.jface.text.rules.Token;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+
+/**
+ * Gives a different color and font style to all text identified as supplemental text by
+ * its corresponding LineAnnotation.
+ *
+ * Note: implementing IPartitionScanner in a straightforward fashion proved buggy, so this more obtuse
+ * extension of RuleBasedPartitionScanner should be used instead. Uses a matching IRule whose pattern changes
+ * depending on the current line.
+ *
+ * @author p.ciardelli
+ * @created 28.10.2009
+ * @see CopyOfSupplementalTextScanner
+ * @version 1.0
+ */
+public class SupplementalTextScanner extends RuleBasedPartitionScanner {
+ private static final Logger logger = Logger
+ .getLogger(SupplementalTextScanner.class);
+
+ private ILineDisplayStrategy lineDisplayStrategy;
+ private Document document;
+ private ISourceViewer sourceViewer;
+
+ private Map<Integer, String> supplementalTexts;
+ private SupplementalTextRule rule;
+ private int lastLine;
+
+ /**
+ * <p>Constructor for SupplementalTextScanner.</p>
+ *
+ * @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
+ * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
+ */
+ public SupplementalTextScanner(ISourceViewer sourceViewer, ILineDisplayStrategy lineDisplayStrategy) {
+ this.sourceViewer = sourceViewer;
+ this.lineDisplayStrategy = lineDisplayStrategy;
+
+ TextAttribute textAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY), null, SWT.ITALIC);
+ IToken supplementalTextToken = new Token(textAttribute);
+
+ rule = new SupplementalTextRule(supplementalTextToken);
+ setPredicateRules(new IPredicateRule[] {rule});
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
+ */
+ /** {@inheritDoc} */
+ @Override
+ public void setPartialRange(IDocument document, int offset, int length,
+ String contentType, int partitionOffset) {
+
+ // Get all supplemental texts in the partial range
+ supplementalTexts = new HashMap<Integer, String>();
+ LineAnnotationModel model = (LineAnnotationModel) sourceViewer.getAnnotationModel();
+ Iterator iter = model.getAnnotationIterator(offset, length, true, true);
+ while (iter.hasNext()) {
+ Object next = iter.next();
+ if (next instanceof LineAnnotation) {
+ Object entity = ((LineAnnotation) next).getEntity();
+ String supplementalText = lineDisplayStrategy.getSupplementalText(entity);
+ if (supplementalText != null && supplementalText.length() > 0) {
+ try {
+ int line = document.getLineOfOffset(model.getPosition((LineAnnotation) next).getOffset());
+ supplementalTexts.put(line, supplementalText);
+ } catch (BadLocationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+ }
+ }
+ this.document = (Document) document;
+ super.setPartialRange(document, offset, length, contentType, partitionOffset);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#nextToken()
+ */
+ /** {@inheritDoc} */
+ @Override
+ public IToken nextToken() {
+ try {
+ int line = document.getLineOfOffset(getTokenOffset());
+ if (line != lastLine) {
+ rule.setStartSequence(supplementalTexts.get(line));
+ }
+ lastLine = line;
+ } catch (BadLocationException e) {
+ logger.debug("Ignoring", e);
+ }
+ return super.nextToken();
+ }
+
+ class SupplementalTextRule extends SingleLineRule {
+
+ static final String DUMMY_SEQUENCE = "_dummy_sequence";
+
+ /**
+ * @param startSequence
+ * @param endSequence
+ * @param token
+ * @param escapeCharacter
+ * @param breaksOnEOL
+ */
+ public SupplementalTextRule(IToken token) {
+ super(DUMMY_SEQUENCE, null, token, '\\', true);
+ }
+
+ /**
+ * Set pattern match for rule.
+ *
+ * @param sequence
+ */
+ protected void setStartSequence(String sequence) {
+ if (sequence != null && sequence.length() > 0) {
+ fStartSequence = sequence.toCharArray();
+ }
+ }
+
+ }
+}