
org.fife.ui.rsyntaxtextarea.spell.SpellingErrorTooltipHtmlGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spellchecker Show documentation
Show all versions of spellchecker Show documentation
A simple spell checker add-on for RSyntaxTextArea. It will spell-check comments in source code, or the entire file if you are editing plain text. Spelling errors are squiggle-underlined with the color of your choice, and tooltips are available offering any spelling suggestions.
The newest version!
/*
* This library is distributed under the LGPL. See the included
* LICENSE.md file for details.
*/
package org.fife.ui.rsyntaxtextarea.spell;
import org.fife.com.swabunga.spell.engine.Configuration;
import org.fife.com.swabunga.spell.engine.Word;
import org.fife.com.swabunga.spell.event.SpellChecker;
import java.awt.*;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Generates the HTML for a tooltip that displays a spelling error and its possible corrections.
*/
class SpellingErrorTooltipHtmlGenerator {
private static final String TOOLTIP_TEXT_FORMAT =
"" +
"
{1}
" +
"
{2}
{3}
";
private static final ResourceBundle MSG = ResourceBundle.getBundle(
"org.fife.ui.rsyntaxtextarea.spell.SpellingParser");
public String get(SpellChecker sc, SpellingParserNotice notice) {
StringBuilder sb = new StringBuilder();
String spacing = " ";
int threshold = sc.getConfiguration().getInteger(Configuration.SPELL_THRESHOLD);
String word = notice.getWord();
List suggestions = sc.getSuggestions(word, threshold);
if (suggestions.isEmpty()) {
sb.append(spacing).append("• ");
sb.append(MSG.getString("None"));
sb.append("
");
}
else {
// If the bad word started with an upper-case letter, make sure all our suggestions do.
if (Character.isUpperCase(word.charAt(0))) {
for (Word suggestion : suggestions) {
String oldSug = suggestion.getWord();
suggestion.setWord(Character.toUpperCase(oldSug.charAt(0)) + oldSug.substring(1));
}
}
sb.append("");
sb.append("");
for (int i = 0; i < suggestions.size(); i++) {
if ((i % 2) == 0) {
sb.append("");
}
sb.append("• ");
Word suggestion = suggestions.get(i);
// Surround with double quotes, not single, since
// replacement words can have single quotes in them.
sb.append("").
append(suggestion.getWord()).
append("").
append(" ");
if ((i & 1) == 1) {
sb.append(" ");
}
}
if ((suggestions.size() % 2) == 0) {
sb.append(" ");
}
sb.append("
");
sb.append(" ");
}
SpellingParser sp = (SpellingParser)notice.getParser();
if (sp.getAllowAdd()) {
sb.append("
").
append("").
append(MSG.getString("ErrorToolTip.AddToDictionary")).
append("
");
}
if (sp.getAllowIgnore()) {
String text = MSG.getString("ErrorToolTip.IgnoreWord");
text = MessageFormat.format(text, word);
sb.append("
").
append("").
append(text).append("");
}
String firstLine = MessageFormat.format(
MSG.getString("ErrorToolTip.DescHtml"),
word);
ComponentOrientation o = ComponentOrientation.getOrientation(
Locale.getDefault());
String dirAttr = o.isLeftToRight() ? "ltr" : "rtl";
return MessageFormat.format(TOOLTIP_TEXT_FORMAT,
dirAttr,
firstLine,
MSG.getString("ErrorToolTip.SuggestionsHtml"),
sb.toString());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy