home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
Utilities.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
11KB
|
363 lines
/*
* @(#)Utilities.java 1.13 98/01/31
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.text;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.text.*;
/**
* A collection of methods to deal with various text
* related activities.
*
* @author Timothy Prinzing
* @version 1.13 01/31/98
*/
public class Utilities {
/**
* Draws the given text, expanding any tabs that are contained
* using the given tab expansion technique. This particular
* implementation renders in a 1.1 style coordinate system
* where ints are used and 72dpi is assumed.
*
* @param s the source of the text
* @param x the X origin
* @param y the Y origin
* @param g the graphics context
* @param e how to expand the tabs
* @param startOffset starting offset in the document of the text
* @returns the location at the end of the rendered text
*/
public static final int drawTabbedText(Segment s, int x, int y, Graphics g,
TabExpander e, int startOffset) {
FontMetrics metrics = g.getFontMetrics();
int nextX = x;
char[] txt = s.array;
int flushLen = 0;
int flushIndex = s.offset;
int n = s.offset + s.count;
for (int i = s.offset; i < n; i++) {
if (txt[i] == '\t') {
if (flushLen > 0) {
g.drawChars(txt, flushIndex, flushLen, x, y);
flushLen = 0;
}
flushIndex = i + 1;
nextX = (int) e.nextTabStop((float) nextX, startOffset + i - s.offset);
x = nextX;
} else if ((txt[i] == '\n') || (txt[i] == '\r')) {
if (flushLen > 0) {
g.drawChars(txt, flushIndex, flushLen, x, y);
flushLen = 0;
}
flushIndex = i + 1;
x = nextX;
} else {
flushLen += 1;
nextX += metrics.charWidth(txt[i]);
}
}
if (flushLen > 0) {
g.drawChars(txt, flushIndex, flushLen, x, y);
}
return nextX;
}
/**
* Determines the width of the given segment of text taking tabs
* into consideration. This is implemented in a 1.1 style coordinate
* system where ints are used and 72dpi is assumed.
*
* @param s the source of the text
* @param metrics the font metrics to use for the calculation
* @param x the X origin
* @param e how to expand the tabs
* @param startOffset starting offset in the document of the text
* @returns the width of the text
*/
public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x,
TabExpander e, int startOffset) {
int nextX = x;
char[] txt = s.array;
int n = s.offset + s.count;
for (int i = s.offset; i < n; i++) {
if (txt[i] == '\t') {
nextX = (int) e.nextTabStop((float) nextX,
startOffset + i - s.offset);
} else {
nextX += metrics.charWidth(txt[i]);
}
}
return nextX - x;
}
/**
* Determines the relative offset into the given text that
* best represents the given span in the view coordinate
* system. This is implemented in a 1.1 style coordinate
* system where ints are used and 72dpi is assumed.
*
* @param s the source of the text
* @param metrics the font metrics to use for the calculation
* @param x0 the starting view location representing the start
* of the given text.
* @param x the target view location to translate to an
* offset into the text.
* @param e how to expand the tabs
* @param startOffset starting offset in the document of the text
* @returns the offset into the text
*/
public static final int getTabbedTextOffset(Segment s, FontMetrics metrics,
int x0, int x, TabExpander e,
int startOffset) {
int currX = x0;
int nextX = currX;
char[] txt = s.array;
int n = s.offset + s.count;
for (int i = s.offset; i < n; i++) {
if (txt[i] == '\t') {
nextX = (int) e.nextTabStop((float) nextX,
startOffset + i - s.offset);
} else {
nextX += metrics.charWidth(txt[i]);
}
if ((x >= currX) && (x < nextX)) {
// found the hit position... return the appropriate side
if ((x - currX) < (nextX - x)) {
return i - s.offset;
} else {
return i + 1 - s.offset;
}
}
currX = nextX;
}
// didn't find, return end offset
return s.count;
}
/**
* Determine the position in the model of the row start in the view
* that the given model position resides.
*/
static final int getRowStart(JTextComponent c, int offs) throws BadLocationException {
Rectangle r = c.modelToView(offs);
int lastOffs = offs;
int y = r.y;
while ((r != null) && (y == r.y)) {
offs = lastOffs;
lastOffs -= 1;
r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
}
return offs;
}
/**
* Determine the position in the model of the row end in the view
* that the given model position resides.
*/
static final int getRowEnd(JTextComponent c, int offs) throws BadLocationException {
Rectangle r = c.modelToView(offs);
int n = c.getDocument().getLength();
int lastOffs = offs;
int y = r.y;
while ((r != null) && (y == r.y)) {
offs = lastOffs;
lastOffs += 1;
r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
}
return offs;
}
/**
* Determine the position in the model that is closest to the given
* view location in the row above.
*/
static final int getPositionAbove(JTextComponent c, int offs, int x) throws BadLocationException {
int lastOffs = getRowStart(c, offs) - 1;
int bestSpan = Short.MAX_VALUE;
int y = 0;
Rectangle r = null;
if (lastOffs >= 0) {
r = c.modelToView(lastOffs);
y = r.y;
}
while ((r != null) && (y == r.y)) {
int span = Math.abs(r.x - x);
if (span < bestSpan) {
offs = lastOffs;
bestSpan = span;
}
lastOffs -= 1;
r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
}
return offs;
}
/**
* Determine the position in the model that is closest to the given
* view location in the row below.
*/
static final int getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException {
int lastOffs = getRowEnd(c, offs) + 1;
int bestSpan = Short.MAX_VALUE;
int n = c.getDocument().getLength();
int y = 0;
Rectangle r = null;
if (lastOffs <= n) {
r = c.modelToView(lastOffs);
y = r.y;
}
while ((r != null) && (y == r.y)) {
int span = Math.abs(x - r.x);
if (span < bestSpan) {
offs = lastOffs;
bestSpan = span;
}
lastOffs += 1;
r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
}
return offs;
}
/**
* Determine the start of a word for the given location.
*
* @returns the location in the model of the word start.
*/
static final int getWordStart(JTextComponent c, int offs) throws BadLocationException {
Document doc = c.getDocument();
Element line = getParagraphElement(c, offs);
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
String s = doc.getText(lineStart, lineEnd - lineStart);
if(s != null && s.length() > 0) {
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int wordPosition = offs - lineStart;
if(wordPosition >= words.last()) {
wordPosition = words.last() - 1;
}
words.following(wordPosition);
offs = lineStart + words.previous();
}
return offs;
}
/**
* Determine the end of a word for the given location.
*
* @returns the location in the model of the word end.
*/
static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
Document doc = c.getDocument();
Element line = getParagraphElement(c, offs);
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
String s = doc.getText(lineStart, lineEnd - lineStart);
if(s != null && s.length() > 0) {
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int wordPosition = offs - lineStart;
if(wordPosition >= words.last()) {
wordPosition = words.last() - 1;
}
offs = lineStart + words.following(wordPosition);
}
return offs;
}
/**
* Determine the start of the next word for the given location.
*
* @returns the location in the model of the word start.
*/
static final int getNextWord(JTextComponent c, int offs) throws BadLocationException {
Document doc = c.getDocument();
Element line = getParagraphElement(c, offs);
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
String s = doc.getText(lineStart, lineEnd - lineStart);
if(s != null && s.length() > 0) {
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int wordPosition = offs - lineStart;
if(wordPosition >= words.last()) {
wordPosition = words.last() - 1;
}
words.following(wordPosition);
offs = lineStart + words.next();
}
return offs;
}
/**
* Determine the start of the next word for the given location.
*
* @returns the location in the model of the word start.
*/
static final int getPreviousWord(JTextComponent c, int offs) throws BadLocationException {
Document doc = c.getDocument();
Element line = getParagraphElement(c, offs);
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
String s = doc.getText(lineStart, lineEnd - lineStart);
if(s != null && s.length() > 0) {
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int wordPosition = offs - lineStart;
if(wordPosition >= words.last()) {
wordPosition = words.last() - 1;
}
words.following(wordPosition);
if (offs == (lineStart + words.previous())) {
words.previous();
int o = words.previous();
if (o == BreakIterator.DONE) {
offs = lineStart;
} else {
offs = lineStart + o;
}
}
}
return offs;
}
/**
* Determine the element to use for a paragraph/line
*/
static final Element getParagraphElement(JTextComponent c, int offs) {
Document doc = c.getDocument();
if (doc instanceof StyledDocument) {
return ((StyledDocument)doc).getParagraphElement(offs);
}
Element map = doc.getDefaultRootElement();
int index = map.getElementIndex(offs);
Element paragraph = map.getElement(index);
return paragraph;
}
}