~ubuntu-branches/ubuntu/vivid/drmips/vivid-backports

« back to all changes in this revision

Viewing changes to src/pc/DrMIPS/src/org/fife/ui/rsyntaxtextarea/FoldingAwareIconRowHeader.java

  • Committer: Package Import Robot
  • Author(s): Bruno Nova
  • Date: 2014-09-27 12:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20140927122417-2gadkwt9k0u7j4zu
Tags: upstream-1.2.3
Import upstream version 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 03/07/2012
 
3
 *
 
4
 * FoldingAwareIconRowHeader - Icon row header that paints itself correctly
 
5
 * even when code folding is enabled.
 
6
 * 
 
7
 * This library is distributed under a modified BSD license.  See the included
 
8
 * RSyntaxTextArea.License.txt file for details.
 
9
 */
 
10
package org.fife.ui.rsyntaxtextarea;
 
11
 
 
12
import java.awt.Color;
 
13
import java.awt.Graphics;
 
14
import java.awt.Point;
 
15
import javax.swing.Icon;
 
16
import javax.swing.text.BadLocationException;
 
17
import javax.swing.text.Document;
 
18
import javax.swing.text.Element;
 
19
 
 
20
import org.fife.ui.rsyntaxtextarea.folding.FoldManager;
 
21
import org.fife.ui.rtextarea.GutterIconInfo;
 
22
import org.fife.ui.rtextarea.IconRowHeader;
 
23
 
 
24
 
 
25
/**
 
26
 * A row header component that takes code folding into account when painting
 
27
 * itself.
 
28
 *
 
29
 * @author Robert Futrell
 
30
 * @version 1.0
 
31
 */
 
32
public class FoldingAwareIconRowHeader extends IconRowHeader {
 
33
 
 
34
 
 
35
        /**
 
36
         * Constructor.
 
37
         *
 
38
         * @param textArea The parent text area.
 
39
         */
 
40
        public FoldingAwareIconRowHeader(RSyntaxTextArea textArea) {
 
41
                super(textArea);
 
42
        }
 
43
 
 
44
 
 
45
        /**
 
46
         * {@inheritDoc}
 
47
         */
 
48
        @Override
 
49
        protected void paintComponent(Graphics g) {
 
50
 
 
51
                // When line wrap is not enabled, take the faster code path.
 
52
                if (textArea==null) {
 
53
                        return;
 
54
                }
 
55
                RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
 
56
                FoldManager fm = rsta.getFoldManager();
 
57
                if (!fm.isCodeFoldingSupportedAndEnabled()) {
 
58
                        super.paintComponent(g);
 
59
                        return;
 
60
                }
 
61
 
 
62
                visibleRect = g.getClipBounds(visibleRect);
 
63
                if (visibleRect==null) { // ???
 
64
                        visibleRect = getVisibleRect();
 
65
                }
 
66
                //System.out.println("IconRowHeader repainting: " + visibleRect);
 
67
                if (visibleRect==null) {
 
68
                        return;
 
69
                }
 
70
 
 
71
                g.setColor(getBackground());
 
72
                g.fillRect(0,visibleRect.y, width,visibleRect.height);
 
73
 
 
74
                if (textArea.getLineWrap()) {
 
75
                        paintComponentWrapped(g);
 
76
                        return;
 
77
                }
 
78
 
 
79
                Document doc = textArea.getDocument();
 
80
                Element root = doc.getDefaultRootElement();
 
81
                textAreaInsets = textArea.getInsets(textAreaInsets);
 
82
 
 
83
                // Get the first line to paint.
 
84
                int cellHeight = textArea.getLineHeight();
 
85
                int topLine = (visibleRect.y-textAreaInsets.top)/cellHeight;
 
86
 
 
87
                // Get where to start painting (top of the row).
 
88
                // We need to be "scrolled up" up just enough for the missing part of
 
89
                // the first line.
 
90
                int y = topLine*cellHeight + textAreaInsets.top;
 
91
 
 
92
                // AFTER calculating visual offset to paint at, account for folding.
 
93
                topLine += fm.getHiddenLineCountAbove(topLine, true);
 
94
 
 
95
                // Paint the active line range.
 
96
                if (activeLineRangeStart>-1 && activeLineRangeEnd>-1) {
 
97
                        Color activeLineRangeColor = getActiveLineRangeColor();
 
98
                        g.setColor(activeLineRangeColor);
 
99
                        try {
 
100
 
 
101
                                int realY1 = rsta.yForLine(activeLineRangeStart);
 
102
                                if (realY1>-1) { // Not in a collapsed fold...
 
103
 
 
104
                                        int  y1 = realY1;//Math.max(y, realY1);
 
105
 
 
106
                                        int y2 = rsta.yForLine(activeLineRangeEnd);
 
107
                                        if (y2==-1) { // In a collapsed fold
 
108
                                                y2 = y1;
 
109
                                        }
 
110
                                        y2 += cellHeight - 1;
 
111
 
 
112
                                        if (y2<visibleRect.y || y1>visibleRect.y+visibleRect.height) {
 
113
                                                //System.out.println("... nothing to paint, bailing...");
 
114
                                                return;
 
115
                                        }
 
116
                                        y1 = Math.max(y, realY1);
 
117
                                        y2 = Math.min(y2, visibleRect.y+visibleRect.height);
 
118
                                        //System.out.println(y1 + "... " + y2 + "; " + realY1 + ", " + visibleRect);
 
119
 
 
120
                                        int j = y1;
 
121
                                        while (j<=y2) {
 
122
                                                int yEnd = Math.min(y2, j+getWidth());
 
123
                                                int xEnd = yEnd-j;
 
124
                                                g.drawLine(0,j, xEnd,yEnd);
 
125
                                                j += 2;
 
126
                                        }
 
127
 
 
128
                                        int i = 2;
 
129
                                        while (i<getWidth()) {
 
130
                                                int yEnd = y1 + getWidth() - i;
 
131
                                                g.drawLine(i,y1, getWidth(),yEnd);
 
132
                                                i += 2;
 
133
                                        }
 
134
 
 
135
                                        if (realY1>=y && realY1<visibleRect.y+visibleRect.height) {
 
136
                                                g.drawLine(0,realY1, getWidth(),realY1);
 
137
                                        }
 
138
                                        if (y2>=y && y2<visibleRect.y+visibleRect.height) {
 
139
                                                g.drawLine(0,y2, getWidth(),y2);
 
140
                                        }
 
141
 
 
142
                                }
 
143
 
 
144
                        } catch (BadLocationException ble) {
 
145
                                ble.printStackTrace(); // Never happens
 
146
                        }
 
147
                }
 
148
 
 
149
                // Paint icons
 
150
                if (trackingIcons!=null) {
 
151
                        int lastLine = textArea.getLineCount() - 1;
 
152
                        for (int i=trackingIcons.size()-1; i>=0; i--) { // Last to first
 
153
                                GutterIconInfo ti = getTrackingIcon(i);
 
154
                                int offs = ti.getMarkedOffset();
 
155
                                if (offs>=0 && offs<=doc.getLength()) {
 
156
                                        int line = root.getElementIndex(offs);
 
157
                                        if (line<=lastLine && line>=topLine) {
 
158
                                                try {
 
159
                                                        Icon icon = ti.getIcon();
 
160
                                                        if (icon!=null) {
 
161
                                                                int lineY = rsta.yForLine(line);
 
162
                                                                if (lineY>=y && lineY<=visibleRect.y+visibleRect.height) {
 
163
                                                                        int y2 = lineY + (cellHeight-icon.getIconHeight())/2;
 
164
                                                                        icon.paintIcon(this, g, 0, y2);
 
165
                                                                        lastLine = line-1; // Paint only 1 icon per line
 
166
                                                                }
 
167
                                                        }
 
168
                                                } catch (BadLocationException ble) {
 
169
                                                        ble.printStackTrace(); // Never happens
 
170
                                                }
 
171
                                        }
 
172
                                        else if (line<topLine) {
 
173
                                                break; // All other lines are above us, so quit now
 
174
                                        }
 
175
                                }
 
176
                        }
 
177
                }
 
178
 
 
179
        }
 
180
 
 
181
 
 
182
        /**
 
183
         * Paints icons when line wrapping is enabled.  Note that this does not
 
184
         * override the parent class's implementation to avoid this version being
 
185
         * called when line wrapping is disabled.
 
186
         */
 
187
        private void paintComponentWrapped(Graphics g) {
 
188
 
 
189
                // The variables we use are as follows:
 
190
                // - visibleRect is the "visible" area of the text area; e.g.
 
191
                // [0,100, 300,100+(lineCount*cellHeight)-1].
 
192
                // actualTop.y is the topmost-pixel in the first logical line we
 
193
                // paint.  Note that we may well not paint this part of the logical
 
194
                // line, as it may be broken into many physical lines, with the first
 
195
                // few physical lines scrolled past.  Note also that this is NOT the
 
196
                // visible rect of this line number list; this line number list has
 
197
                // visible rect == [0,0, insets.left-1,visibleRect.height-1].
 
198
 
 
199
                // We avoid using modelToView/viewToModel where possible, as these
 
200
                // methods trigger a parsing of the line into syntax tokens, which is
 
201
                // costly.  It's cheaper to just grab the child views' bounds.
 
202
 
 
203
                RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
 
204
//              boolean currentLineHighlighted = textArea.getHighlightCurrentLine();
 
205
                Document doc = textArea.getDocument();
 
206
                Element root = doc.getDefaultRootElement();
 
207
                int topPosition = textArea.viewToModel(
 
208
                                                                new Point(visibleRect.x,visibleRect.y));
 
209
                int topLine = root.getElementIndex(topPosition);
 
210
 
 
211
                int topY = visibleRect.y;
 
212
                int bottomY = visibleRect.y + visibleRect.height;
 
213
                int cellHeight = textArea.getLineHeight();
 
214
 
 
215
                // Paint icons
 
216
                if (trackingIcons!=null) {
 
217
                        int lastLine = textArea.getLineCount() - 1;
 
218
                        for (int i=trackingIcons.size()-1; i>=0; i--) { // Last to first
 
219
                                GutterIconInfo ti = getTrackingIcon(i);
 
220
                                int offs = ti.getMarkedOffset();
 
221
                                if (offs>=0 && offs<=doc.getLength()) {
 
222
                                        int line = root.getElementIndex(offs);
 
223
                                        if (line<=lastLine && line>=topLine) {
 
224
                                                try {
 
225
                                                        int lineY = rsta.yForLine(line);
 
226
                                                        if (lineY>=topY && lineY<bottomY) {
 
227
                                                                Icon icon = ti.getIcon();
 
228
                                                                if (icon!=null) {
 
229
                                                                        int y2 = lineY + (cellHeight-icon.getIconHeight())/2;
 
230
                                                                        ti.getIcon().paintIcon(this, g, 0, y2);
 
231
                                                                        lastLine = line-1; // Paint only 1 icon per line
 
232
                                                                }
 
233
                                                        }
 
234
                                                } catch (BadLocationException ble) {
 
235
                                                        ble.printStackTrace(); // Never happens
 
236
                                                }
 
237
                                        }
 
238
                                        else if (line<topLine) {
 
239
                                                break; // All other lines are above us, so quit now
 
240
                                        }
 
241
                                }
 
242
                        }
 
243
                }
 
244
 
 
245
        }
 
246
 
 
247
 
 
248
}
 
 
b'\\ No newline at end of file'