1. Fix #1589403 - wrap double/single quoted strings
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / editors / NonRuleBasedDamagerRepairer.java
1 package com.quantum.editors;
2
3 import org.eclipse.jface.text.BadLocationException;
4 import org.eclipse.jface.text.DocumentEvent;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.IRegion;
7 import org.eclipse.jface.text.ITypedRegion;
8 import org.eclipse.jface.text.Region;
9 import org.eclipse.jface.text.TextAttribute;
10 import org.eclipse.jface.text.TextPresentation;
11 import org.eclipse.jface.text.presentation.IPresentationDamager;
12 import org.eclipse.jface.text.presentation.IPresentationRepairer;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.custom.StyleRange;
15
16 public class NonRuleBasedDamagerRepairer
17         implements IPresentationDamager, IPresentationRepairer {
18
19         /** The document this object works on */
20         protected IDocument fDocument;
21         /** The default text attribute if non is returned as data by the current token */
22         protected TextAttribute fDefaultTextAttribute;
23
24         /**
25          * Constructor for NonRuleBasedDamagerRepairer.
26          */
27         public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
28                 Assert.isNotNull(defaultTextAttribute);
29
30                 fDefaultTextAttribute = defaultTextAttribute;
31         }
32
33         /**
34          * @see IPresentationRepairer#setDocument(IDocument)
35          */
36         public void setDocument(IDocument document) {
37                 fDocument = document;
38         }
39
40         public void setTextAttribute(TextAttribute attr) {
41                 fDefaultTextAttribute = attr;
42         }
43
44         /**
45          * Returns the end offset of the line that contains the specified offset or
46          * if the offset is inside a line delimiter, the end offset of the next line.
47          *
48          * @param offset the offset whose line end offset must be computed
49          * @return the line end offset for the given offset
50          * @exception BadLocationException if offset is invalid in the current document
51          */
52         protected int endOfLineOf(int offset) throws BadLocationException {
53
54                 IRegion info = fDocument.getLineInformationOfOffset(offset);
55                 if (offset <= info.getOffset() + info.getLength())
56                         return info.getOffset() + info.getLength();
57
58                 int line = fDocument.getLineOfOffset(offset);
59                 try {
60                         info = fDocument.getLineInformation(line + 1);
61                         return info.getOffset() + info.getLength();
62                 } catch (BadLocationException x) {
63                         return fDocument.getLength();
64                 }
65         }
66
67         /**
68          * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
69          */
70         public IRegion getDamageRegion(
71                 ITypedRegion partition,
72                 DocumentEvent event,
73                 boolean documentPartitioningChanged) {
74                 if (!documentPartitioningChanged) {
75                         try {
76
77                                 IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
78                                 int start = Math.max(partition.getOffset(), info.getOffset());
79
80                                 int end =
81                                         event.getOffset()
82                                                 + (event.getText() == null ? event.getLength() : event.getText().length());
83
84                                 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
85                                         // optimize the case of the same line
86                                         end = info.getOffset() + info.getLength();
87                                 } else
88                                         end = endOfLineOf(end);
89
90                                 end = Math.min(partition.getOffset() + partition.getLength(), end);
91                                 return new Region(start, end - start);
92
93                         } catch (BadLocationException x) {
94                         }
95                 }
96
97                 return partition;
98         }
99
100         /**
101          * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
102          */
103         public void createPresentation(
104                 TextPresentation presentation,
105                 ITypedRegion region) {
106                 addRange(
107                         presentation,
108                         region.getOffset(),
109                         region.getLength(),
110                         fDefaultTextAttribute);
111         }
112
113         /**
114          * Adds style information to the given text presentation.
115          *
116          * @param presentation the text presentation to be extended
117          * @param offset the offset of the range to be styled
118          * @param length the length of the range to be styled
119          * @param attr the attribute describing the style of the range to be styled
120          */
121         protected void addRange(
122                 TextPresentation presentation,
123                 int offset,
124                 int length,
125                 TextAttribute attr) {
126                 if (attr != null)
127                         presentation.addStyleRange(
128                                 new StyleRange(
129                                         offset,
130                                         length,
131                                         attr.getForeground(),
132                                         attr.getBackground(),
133                                         attr.getStyle()));
134         }
135 }