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