-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLineEnumRule.java
More file actions
186 lines (160 loc) · 7.22 KB
/
Copy pathLineEnumRule.java
File metadata and controls
186 lines (160 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*BEGIN_COPYRIGHT_BLOCK
*
* Copyright (c) 2001-2010, JavaPLT group at Rice University ([email protected])
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software is Open Source Initiative approved Open Source Software.
* Open Source Initative Approved is a trademark of the Open Source Initiative.
*
* This file is part of DrJava. Download the current version of this project
* from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
*
* END_COPYRIGHT_BLOCK*/
package edu.rice.cs.drjava.ui;
import java.awt.*;
import javax.swing.*;
import edu.rice.cs.drjava.DrJava;
import edu.rice.cs.drjava.config.OptionConstants;
/**
* The row header of the DefinitionsPane which displays the line numbers
* @version $Id$
*/
public class LineEnumRule extends JComponent {
/** The magic number for Swing's JTextPane border padding. */
private static final int BORDER_PADDING = 3;
/** Width of the rule */
static int SIZE = 35;
/** White space between numbers and code*/
static int WHITE_SPACE = 7;
/** Vertical increment between line numbers */
private int _increment;
/** DefinitionsPane that this rule is displayed for */
protected DefinitionsPane _pane;
/** font metrics for the DefPane's font */
protected FontMetrics _fm;
/** custom font for the line numbers */
protected Font _newFont;
/** font metrics for the new font */
protected FontMetrics _nfm;
/** Create a new component to display line numbers along the left of
* the definitions pane.
* @param p the pane to show line numbers on
*/
public LineEnumRule(DefinitionsPane p) {
_pane = p;
_fm = _pane.getFontMetrics(_pane.getFont());
_increment = _fm.getHeight();
_newFont = _getLineNumFont();
_nfm = getFontMetrics(_newFont);
// XXX: 3 is the magic number for Swing's JTextPane border padding.
SIZE = (int) _nfm.getStringBounds("99999", getGraphics()).getWidth() + 3 +10;
}
/** Return a new Dimension using our set width, and the height of the definitions pane */
public Dimension getPreferredSize() {
return new Dimension(SIZE, (int)_pane.getPreferredSize().getHeight());
}
/** Updates the row header's font information.
* Uses a custom config setting for this purpose.
*/
public void updateFont() {
_fm = _pane.getFontMetrics(_pane.getFont());
_newFont = _getLineNumFont();
//_pane.getFont().deriveFont( 8f );
_nfm = getFontMetrics(_newFont);
// XXX: 3 is the magic number for Swing's JTextPane border padding.
SIZE = (int) _nfm.getStringBounds("99999", getGraphics()).getWidth() + 3 + WHITE_SPACE;
}
/** Paints the line enumeration component.*/
public void paintComponent(Graphics g) {
Rectangle drawHere = g.getClipBounds();
// Set a white background
Color backg = DrJava.getConfig().getSetting
(OptionConstants.DEFINITIONS_LINE_NUMBER_BACKGROUND_COLOR);
g.setColor(backg);
g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
// Do the ruler labels in a small font that's black.
g.setFont(_newFont);
Color foreg = DrJava.getConfig().getSetting
(OptionConstants.DEFINITIONS_LINE_NUMBER_COLOR);
g.setColor(foreg);
// Use clipping bounds to calculate first tick and last tick location.
int start = (drawHere.y / _increment) * _increment;
int end = (((drawHere.y + drawHere.height) / _increment) + 1) * _increment;
int baseline = (int) (( _nfm.getAscent() + _fm.getHeight() - _fm.getDescent())/2.0 );
// ticks and labels
// final OpenDefinitionsDocument odd = _pane.getOpenDefDocument();
// final int endOffset = odd.getEndPosition().getOffset()-1;
// int lastLine = odd.getDefaultRootElement().getElementIndex(endOffset);
//
// if (odd.getLineStartPos(endOffset) != odd.getLineEndPos(endOffset)) { ++lastLine; }
for (int i = start; i < end; i += _increment) {
// final int lineNo = i/_increment +1;
// if (lineNo>lastLine) break;
// String text = Integer.toString(lineNo);
String text = Integer.toString(i/_increment +1);
// When we paint, we get a good look at the Graphics hints.
// Use them to update our estimate of total width.
SIZE = (int) _nfm.getStringBounds("99999", g).getWidth() + BORDER_PADDING + WHITE_SPACE;
int offset = SIZE - ((int) (_nfm.getStringBounds(text, g).getWidth() + 3)) - WHITE_SPACE;
//g.drawLine(SIZE-1, i, SIZE-tickLength-1, i);
if (text != null) {
// Add an arbitrary 3 pixels to line up the text properly with the
// def pane text baseline.
g.drawString(text, offset, i + baseline + 3);
}
}
}
/** Get the font for line numbers, making sure that it is vertically smaller
* than the definitions pane font.
* @return a valid font for displaying line numbers
*/
private Font _getLineNumFont() {
Font lnf = DrJava.getConfig().getSetting(OptionConstants.FONT_LINE_NUMBERS);
FontMetrics mets = getFontMetrics(lnf);
Font mainFont = _pane.getFont();
// Check the height of the line num font against the def pane font.
if (mets.getHeight() > _fm.getHeight()) {
// If the line num font has a larger size than the main font, try deriving
// a new version with the same size. (This may or may not produce a height
// smaller than the main font.)
float newSize;
if (lnf.getSize() > mainFont.getSize()) {
newSize = mainFont.getSize2D();
}
// Otherwise, just reduce the current size by one and try that.
else {
newSize = lnf.getSize2D() - 1f;
}
// If that doesn't work, try reducing the size by one until it fits.
do {
lnf = lnf.deriveFont(newSize);
mets = getFontMetrics(lnf);
newSize -= 1f;
} while (mets.getHeight() > _fm.getHeight());
}
return lnf;
}
}