edu.stanford.nlp.swing.TooltipJList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-corenlp Show documentation
Show all versions of stanford-corenlp Show documentation
Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.
package edu.stanford.nlp.swing;
import java.awt.event.MouseEvent;
import javax.swing.JList;
import javax.swing.ListModel;
/**
* Simple list class that extends JList and adds tool tip functionality to the list. Tool tips are automatically
* wrapped to a specific length (default 80 chars) while preserving word boundaries.
*
* @author Anna Rafferty
*
*/
@SuppressWarnings("serial")
public class TooltipJList extends JList {
// todo: generify once we move to Java 8, but JList wasn't generic in Java 6 so can't do now.
private static int PROBLEM_LINE_LENGTH = 80;
public TooltipJList() {
super();
}
public TooltipJList(ListModel model) {
this(model, PROBLEM_LINE_LENGTH);
}
public TooltipJList(ListModel model, int lineWrapLength) {
super(model);
PROBLEM_LINE_LENGTH = lineWrapLength;
}
@Override
public String getToolTipText(MouseEvent evt) {
int index = locationToIndex(evt.getPoint());
if (-1 < index) {
StringBuilder s = new StringBuilder();
String text = getModel().getElementAt(index).toString();
s.append("");
//separate out into lines
String textLeft = text;
boolean isFirstLine = true;
while(textLeft.length() > 0) {
String curLine = "";
if(textLeft.length() > PROBLEM_LINE_LENGTH) {
curLine = textLeft.substring(0, PROBLEM_LINE_LENGTH);
textLeft = textLeft.substring(PROBLEM_LINE_LENGTH, textLeft.length());
//check if we're at the end of a word - if not, get us there
while(curLine.charAt(curLine.length()-1) != ' ' && textLeft.length() >0) {
curLine = curLine + textLeft.substring(0,1);
textLeft = textLeft.substring(1,textLeft.length());
}
} else {
curLine = textLeft;
textLeft = "";
}
if(!isFirstLine)
s.append("
");
s.append(curLine);
if(!isFirstLine)
s.append("");
else
isFirstLine = false;
}
s.append("");
return s.toString();
} else {
return null;
}
}
}