KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > LineEnumRule


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import java.awt.*;
37 import javax.swing.*;
38
39 import edu.rice.cs.drjava.DrJava;
40 import edu.rice.cs.drjava.config.OptionConstants;
41
42 /**
43  * The row header of the DefinitionsPane which displays the line numbers
44  * @version $Id: LineEnumRule.java 4020 2006-10-15 22:39:27Z mgricken $
45  */

46 public class LineEnumRule extends JComponent {
47   /** The magic number for Swing's JTextPane border padding. */
48   private static final int BORDER_PADDING = 3;
49
50   /** Width of the rule */
51   static int SIZE = 35;
52
53   /** Vertical increment between line numbers */
54   private int _increment;
55
56   /** DefinitionsPane that this rule is displayed for */
57   protected DefinitionsPane _pane;
58
59   /** font metrics for the DefPane's font */
60   protected FontMetrics _fm;
61   /** custom font for the line numbers */
62   protected Font _newFont;
63   /** font metrics for the new font */
64   protected FontMetrics _nfm;
65
66   /**
67    * Create a new component to display line numbers along the left of
68    * the definitions pane.
69    * @param p the pane to show line numbers on
70    */

71   public LineEnumRule(DefinitionsPane p) {
72     _pane = p;
73     _fm = _pane.getFontMetrics(_pane.getFont());
74     _increment = _fm.getHeight();
75
76     _newFont = _getLineNumFont();
77     _nfm = getFontMetrics(_newFont);
78     // XXX: 3 is the magic number for Swing's JTextPane border padding.
79
SIZE = (int) _nfm.getStringBounds("99999", getGraphics()).getWidth() + 3;
80   }
81
82   /**
83    * Return a new Dimension using our set width, and the height of the def. pane
84    * return Dimension
85    */

86   public Dimension getPreferredSize() {
87     return new Dimension( SIZE, (int)_pane.getPreferredSize().getHeight());
88   }
89
90   /**
91    * Updates the row header's font information.
92    * Uses a custom config setting for this purpose.
93    */

94   public void updateFont() {
95     _fm = _pane.getFontMetrics(_pane.getFont());
96     _newFont = _getLineNumFont();
97       //_pane.getFont().deriveFont( 8f );
98
_nfm = getFontMetrics(_newFont);
99     // XXX: 3 is the magic number for Swing's JTextPane border padding.
100
SIZE = (int) _nfm.getStringBounds("99999", getGraphics()).getWidth() + 3;
101   }
102
103   /**
104    * Paints the line enumeration component.
105    */

106   public void paintComponent(Graphics g) {
107     Rectangle drawHere = g.getClipBounds();
108
109     // Set a white background
110
Color backg = DrJava.getConfig().getSetting
111       (OptionConstants.DEFINITIONS_BACKGROUND_COLOR);
112     g.setColor(backg);
113     g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
114
115     // Do the ruler labels in a small font that's black.
116
g.setFont(_newFont);
117     Color foreg = DrJava.getConfig().getSetting
118       (OptionConstants.DEFINITIONS_NORMAL_COLOR);
119     g.setColor(foreg);
120
121     // Use clipping bounds to calculate first tick and last tick location.
122
int start = (drawHere.y / _increment) * _increment;
123     int end = (((drawHere.y + drawHere.height) / _increment) + 1) * _increment;
124
125
126     int baseline = (int) (( _nfm.getAscent() + _fm.getHeight() - _fm.getDescent())/2.0 );
127
128     // ticks and labels
129
// final OpenDefinitionsDocument odd = _pane.getOpenDefDocument();
130
// final int endOffset = odd.getEndPosition().getOffset()-1;
131
// int lastLine = odd.getDefaultRootElement().getElementIndex(endOffset);
132
//
133
// if (odd.getLineStartPos(endOffset)!=odd.getLineEndPos(endOffset)) { ++lastLine; }
134
for (int i = start; i < end; i += _increment) {
135 // final int lineNo = i/_increment +1;
136
// if (lineNo>lastLine) break;
137
// String text = Integer.toString(lineNo);
138
String JavaDoc text = Integer.toString(i/_increment +1);
139
140       // When we paint, we get a good look at the Graphics hints.
141
// Use them to update our estimate of total width.
142
SIZE = (int) _nfm.getStringBounds("99999", g).getWidth() + BORDER_PADDING;
143       int offset = SIZE - ((int) (_nfm.getStringBounds(text, g).getWidth() + 1));
144
145       //g.drawLine(SIZE-1, i, SIZE-tickLength-1, i);
146
if (text != null) {
147         // Add an arbitrary 3 pixels to line up the text properly with the
148
// def pane text baseline.
149
g.drawString(text, offset, i + baseline + 3);
150       }
151     }
152   }
153
154   /**
155    * Get the font for line numbers, making sure that it is vertically smaller
156    * than the definitions pane font.
157    * @return a valid font for displaying line numbers
158    */

159   private Font _getLineNumFont() {
160     Font lnf = DrJava.getConfig().getSetting(OptionConstants.FONT_LINE_NUMBERS);
161     FontMetrics mets = getFontMetrics(lnf);
162     Font mainFont = _pane.getFont();
163
164     // Check the height of the line num font against the def pane font.
165
if (mets.getHeight() > _fm.getHeight()) {
166       // If the line num font has a larger size than the main font, try deriving
167
// a new version with the same size. (This may or may not produce a height
168
// smaller than the main font.)
169
float newSize;
170       if (lnf.getSize() > mainFont.getSize()) {
171         newSize = mainFont.getSize2D();
172       }
173       // Otherwise, just reduce the current size by one and try that.
174
else {
175         newSize = lnf.getSize2D() - 1f;
176       }
177
178       // If that doesn't work, try reducing the size by one until it fits.
179
do {
180         lnf = lnf.deriveFont(newSize);
181         mets = getFontMetrics(lnf);
182         newSize -= 1f;
183       } while (mets.getHeight() > _fm.getHeight());
184     }
185
186     return lnf;
187   }
188 }
189
Popular Tags