View Javadoc

1   /********************************************************************************
2    * Copyright (c) 2005 Prashant Deva.
3    
4    * All rights reserved. This program and the accompanying materials 
5    * are made available under the terms of the Eclipse Public License - v 1.0
6    * which is available at http://www.eclipse.org/legal/epl-v10.html
7    *******************************************************************************/
8   
9   package org.wsmostudio.grounding.sawsdl.ui.text;
10  
11  import org.eclipse.jface.text.BadLocationException;
12  import org.eclipse.jface.text.DocumentEvent;
13  import org.eclipse.jface.text.IDocument;
14  import org.eclipse.jface.text.IRegion;
15  import org.eclipse.jface.text.ITypedRegion;
16  import org.eclipse.jface.text.Region;
17  import org.eclipse.jface.text.TextAttribute;
18  import org.eclipse.jface.text.TextPresentation;
19  import org.eclipse.jface.text.presentation.IPresentationDamager;
20  import org.eclipse.jface.text.presentation.IPresentationRepairer;
21  import org.eclipse.jface.util.Assert;
22  import org.eclipse.swt.custom.StyleRange;
23  
24  public class NonRuleBasedDamagerRepairer implements IPresentationDamager, IPresentationRepairer {
25  
26      /*** The document this object works on */
27      protected IDocument fDocument;
28  
29      /***
30       * The default text attribute if non is returned as data by the current
31       * token
32       */
33      protected TextAttribute fDefaultTextAttribute;
34  
35      /***
36       * Constructor for NonRuleBasedDamagerRepairer.
37       */
38      public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
39          Assert.isNotNull(defaultTextAttribute);
40  
41          fDefaultTextAttribute = defaultTextAttribute;
42      }
43  
44      /***
45       * @see IPresentationRepairer#setDocument(IDocument)
46       */
47      public void setDocument(IDocument document) {
48          fDocument = document;
49      }
50  
51      /***
52       * Returns the end offset of the line that contains the specified offset or
53       * if the offset is inside a line delimiter, the end offset of the next
54       * line.
55       * 
56       * @param offset
57       *            the offset whose line end offset must be computed
58       * @return the line end offset for the given offset
59       * @exception BadLocationException
60       *                if offset is invalid in the current document
61       */
62      protected int endOfLineOf(int offset) throws BadLocationException {
63  
64          IRegion info = fDocument.getLineInformationOfOffset(offset);
65          if (offset <= info.getOffset() + info.getLength())
66              return info.getOffset() + info.getLength();
67  
68          int line = fDocument.getLineOfOffset(offset);
69          try {
70              info = fDocument.getLineInformation(line + 1);
71              return info.getOffset() + info.getLength();
72          }
73          catch (BadLocationException x) {
74              return fDocument.getLength();
75          }
76      }
77  
78      /***
79       * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent,
80       *      boolean)
81       */
82      public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event,
83              boolean documentPartitioningChanged) {
84          if (!documentPartitioningChanged) {
85              try {
86  
87                  IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
88                  int start = Math.max(partition.getOffset(), info.getOffset());
89  
90                  int end = event.getOffset()
91                          + (event.getText() == null ? event.getLength() : event.getText().length());
92  
93                  if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
94                      // optimize the case of the same line
95                      end = info.getOffset() + info.getLength();
96                  }
97                  else
98                      end = endOfLineOf(end);
99  
100                 end = Math.min(partition.getOffset() + partition.getLength(), end);
101                 return new Region(start, end - start);
102 
103             }
104             catch (BadLocationException x) {
105             }
106         }
107 
108         return partition;
109     }
110 
111     /***
112      * @see IPresentationRepairer#createPresentation(TextPresentation,
113      *      ITypedRegion)
114      */
115     public void createPresentation(TextPresentation presentation, ITypedRegion region) {
116         addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
117     }
118 
119     /***
120      * Adds style information to the given text presentation.
121      * 
122      * @param presentation
123      *            the text presentation to be extended
124      * @param offset
125      *            the offset of the range to be styled
126      * @param length
127      *            the length of the range to be styled
128      * @param attr
129      *            the attribute describing the style of the range to be styled
130      */
131     protected void addRange(TextPresentation presentation, int offset, int length,
132             TextAttribute attr) {
133         if (attr != null)
134             presentation.addStyleRange(new StyleRange(offset, length, attr.getForeground(), attr
135                     .getBackground(), attr.getStyle()));
136     }
137 }
138 
139 /*
140  * $Log$
141  * Revision 1.2  2006/10/27 08:41:08  alex_simov
142  * no message
143  * 
144  * Revision 1.1 2006/10/26 13:55:07
145  * alex_simov SA-WSDL Text Editor with syntax highlighting added
146  * 
147  * 
148  */