| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.drawing; |
| 16 | |
| 17 | import java.awt.Font; |
| 18 | import java.awt.FontMetrics; |
| 19 | |
| 20 | import javax.swing.JTextArea; |
| 21 | import javax.swing.text.BadLocationException; |
| 22 | |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.commons.logging.LogFactory; |
| 25 | |
| 26 | final public class TextAreaInfo |
| 27 | { |
| 28 | static private final Log _logger = LogFactory.getLog(TextAreaInfo.class); |
| 29 | |
| 30 | public TextAreaInfo(JTextArea area) |
| 31 | { |
| 32 | try |
| 33 | { |
| 34 | int count = area.getLineCount(); |
| 35 | _lines = new String[count]; |
| 36 | for (int i = 0; i < count; i++) |
| 37 | { |
| 38 | int start = area.getLineStartOffset(i); |
| 39 | int end = area.getLineEndOffset(i); |
| 40 | _lines[i] = area.getText(start, end - start); |
| 41 | } |
| 42 | Font font = area.getFont(); |
| 43 | FontMetrics metrics = area.getFontMetrics(font); |
| 44 | _lineHeight = metrics.getHeight(); |
| 45 | } |
| 46 | catch (BadLocationException exc) |
| 47 | { |
| 48 | _logger.error("calculateText", exc); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public String[] getLines() |
| 53 | { |
| 54 | return _lines; |
| 55 | } |
| 56 | |
| 57 | public int getLineHeight() |
| 58 | { |
| 59 | return _lineHeight; |
| 60 | } |
| 61 | |
| 62 | private String[] _lines = null; |
| 63 | private int _lineHeight = 0; |
| 64 | } |