use of java.text.BreakIterator in project iNGAGE by davis123123.
the class RecentCommentsAdapter method truncateText.
private String truncateText(String text, int maxLen) {
if (text != null && text.length() > maxLen) {
BreakIterator bi = BreakIterator.getWordInstance();
bi.setText(text);
if (bi.isBoundary(maxLen - 1)) {
text = text.substring(0, maxLen - 2);
} else {
int preceding = bi.preceding(maxLen - 1);
text = text.substring(0, preceding - 1);
}
text = text.concat("...");
return text;
} else {
return text;
}
}
use of java.text.BreakIterator in project jdk8u_jdk by JetBrains.
the class GlyphView method getBreakSpot.
/**
* Returns a location to break at in the passed in region, or
* BreakIterator.DONE if there isn't a good location to break at
* in the specified region.
*/
private int getBreakSpot(int p0, int p1) {
if (breakSpots == null) {
// Re-calculate breakpoints for the whole view
int start = getStartOffset();
int end = getEndOffset();
int[] bs = new int[end + 1 - start];
int ix = 0;
// Breaker should work on the parent element because there may be
// a valid breakpoint at the end edge of the view (space, etc.)
Element parent = getElement().getParentElement();
int pstart = (parent == null ? start : parent.getStartOffset());
int pend = (parent == null ? end : parent.getEndOffset());
Segment s = getText(pstart, pend);
s.first();
BreakIterator breaker = getBreaker();
breaker.setText(s);
// Backward search should start from end+1 unless there's NO end+1
int startFrom = end + (pend > end ? 1 : 0);
for (; ; ) {
startFrom = breaker.preceding(s.offset + (startFrom - pstart)) + (pstart - s.offset);
if (startFrom > start) {
// The break spot is within the view
bs[ix++] = startFrom;
} else {
break;
}
}
SegmentCache.releaseSharedSegment(s);
breakSpots = new int[ix];
System.arraycopy(bs, 0, breakSpots, 0, ix);
}
int breakSpot = BreakIterator.DONE;
for (int i = 0; i < breakSpots.length; i++) {
int bsp = breakSpots[i];
if (bsp <= p1) {
if (bsp > p0) {
breakSpot = bsp;
}
break;
}
}
return breakSpot;
}
use of java.text.BreakIterator in project webanno by webanno.
the class ImportExportServiceImpl method splitSentences.
public static void splitSentences(JCas aJCas) {
BreakIterator bi = BreakIterator.getSentenceInstance(Locale.US);
bi.setText(aJCas.getDocumentText());
int last = bi.first();
int cur = bi.next();
while (cur != BreakIterator.DONE) {
int[] span = new int[] { last, cur };
trim(aJCas.getDocumentText(), span);
if (!isEmpty(span[0], span[1])) {
Sentence seg = new Sentence(aJCas, span[0], span[1]);
seg.addToIndexes(aJCas);
}
last = cur;
cur = bi.next();
}
}
use of java.text.BreakIterator in project webanno by webanno.
the class ImportExportServiceImpl method tokenize.
public static void tokenize(JCas aJCas) {
BreakIterator bi = BreakIterator.getWordInstance(Locale.US);
for (Sentence s : select(aJCas, Sentence.class)) {
bi.setText(s.getCoveredText());
int last = bi.first();
int cur = bi.next();
while (cur != BreakIterator.DONE) {
int[] span = new int[] { last, cur };
trim(s.getCoveredText(), span);
if (!isEmpty(span[0], span[1])) {
Token seg = new Token(aJCas, span[0] + s.getBegin(), span[1] + s.getBegin());
seg.addToIndexes(aJCas);
}
last = cur;
cur = bi.next();
}
}
}
use of java.text.BreakIterator in project cytoscape-impl by cytoscape.
the class FormulaBuilderDialog method wrapLabelText.
private void wrapLabelText(final JLabel label, final String text) {
if (label.getPreferredSize() == null)
return;
final int maxWidth = label.getPreferredSize().width;
final BreakIterator boundary = BreakIterator.getWordInstance();
boundary.setText(text);
final FontMetrics fm = label.getFontMetrics(label.getFont());
final StringBuilder trial = new StringBuilder();
final StringBuilder real = new StringBuilder("<html>");
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
final String word = text.substring(start, end);
trial.append(word);
int trialWidth = SwingUtilities.computeStringWidth(fm, trial.toString());
if (trialWidth > maxWidth) {
trial.setLength(0);
trial.append(word);
real.append("<br>");
}
real.append(word);
}
real.append("</html>");
label.setText(real.toString());
}
Aggregations