use of javax.swing.text.Element in project blue by kunstmusik.
the class CsoundOrcCompletionProvider method getRowFirstNonWhite.
// if (isCsoundVariable(word)) {
// ArrayList varMatches = findMatches(getTextBeforeWord(word),
// word);
// options.addAll(varMatches);
// }
//
// Collections.<String>sort(options);
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException {
Element lineElement = doc.getParagraphElement(offset);
int start = lineElement.getStartOffset();
while (start + 1 < lineElement.getEndOffset()) {
try {
if (doc.getText(start, 1).charAt(0) != ' ') {
break;
}
} catch (BadLocationException ex) {
throw (BadLocationException) new BadLocationException("calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex);
}
start++;
}
return start;
}
use of javax.swing.text.Element in project energy3d by concord-consortium.
the class MyEditorPane method setText.
public void setText(final String text) {
editorPane.setText(text);
if (editorPane.getDocument() instanceof HTMLDocument) {
final HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
final ElementIterator it = new ElementIterator(doc);
Element element;
while ((element = it.next()) != null) {
final AttributeSet as = element.getAttributes();
final Enumeration<?> en = as.getAttributeNames();
DefaultButtonModel buttonModel = null;
PlainDocument document = null;
String action = null;
String question = null;
String choice = null;
String key = null;
String dataName = null;
while (en.hasMoreElements()) {
final Object n = en.nextElement();
final Object v = as.getAttribute(n);
if (v instanceof DefaultButtonModel) {
buttonModel = (DefaultButtonModel) v;
} else if (v instanceof PlainDocument) {
document = (PlainDocument) v;
} else if (n.toString().equals("action")) {
action = v.toString();
} else if (n.toString().equals("question")) {
question = v.toString();
} else if (n.toString().equals("choice")) {
choice = v.toString();
} else if (n.toString().equals("key")) {
key = v.toString();
} else if (n.toString().equals("data")) {
dataName = v.toString();
}
}
if (action != null) {
final String a = action;
if (document != null) {
final String n = dataName;
final PlainDocument d = document;
document.addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(final DocumentEvent e) {
textFieldUpdated(a, n, d);
}
@Override
public void insertUpdate(final DocumentEvent e) {
textFieldUpdated(a, n, d);
}
@Override
public void changedUpdate(final DocumentEvent e) {
textFieldUpdated(a, n, d);
}
});
}
if (buttonModel != null) {
final QuestionnaireModel qm;
if (question != null && choice != null) {
boolean isKey = false;
if ("yes".equalsIgnoreCase(key) || "true".equalsIgnoreCase(key)) {
isKey = true;
}
qm = new QuestionnaireModel(question, choice, isKey);
} else {
qm = null;
}
final DefaultButtonModel bm = buttonModel;
if (buttonModel instanceof JToggleButton.ToggleButtonModel) {
buttonModel.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
if (qm != null) {
// fire only one for questionnaires
if (e.getStateChange() == ItemEvent.SELECTED) {
buttonActionPerformed(a, qm, bm);
}
} else {
buttonActionPerformed(a, qm, bm);
}
}
});
} else {
buttonModel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
buttonActionPerformed(a, qm, bm);
}
});
}
}
}
}
}
}
use of javax.swing.text.Element in project sonarqube by SonarSource.
the class TextLineNumber method setPreferredWidth.
/**
* Calculate the width needed to display the maximum line number
*/
private void setPreferredWidth() {
Element root = component.getDocument().getDefaultRootElement();
int lines = root.getElementCount();
int digits = Math.max(String.valueOf(lines).length(), minimumDisplayDigits);
if (lastDigits != digits) {
lastDigits = digits;
FontMetrics fontMetrics = getFontMetrics(getFont());
int width = fontMetrics.charWidth('0') * digits;
Insets insets = getInsets();
int preferredWidth = insets.left + insets.right + width;
Dimension d = getPreferredSize();
d.setSize(preferredWidth, HEIGHT);
setPreferredSize(d);
setSize(d);
}
}
use of javax.swing.text.Element in project sonarqube by SonarSource.
the class TextLineNumber method caretUpdate.
//
// Implement CaretListener interface
//
@Override
public void caretUpdate(CaretEvent e) {
// Get the line the caret is positioned on
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
int currentLine = root.getElementIndex(caretPosition);
if (lastLine != currentLine) {
repaint();
lastLine = currentLine;
}
}
use of javax.swing.text.Element in project omegat by omegat-org.
the class DictionariesTextArea method callDictionary.
/**
* Move position in pane to the currently selected word.
*/
protected void callDictionary(String word) {
UIThreadsUtil.mustBeSwingThread();
HTMLDocument doc = (HTMLDocument) getDocument();
// multiple entry can be existed for each query words,
// try to get first one.
int index = displayedWords.indexOf(word.toLowerCase());
if (index == -1 && manager.doFuzzyMatching()) {
// when not found and fuzzy matching allowed,retry with stemmed word
String[] stemmed = manager.getStemmedWords(word);
if (stemmed.length == 0) {
return;
}
index = displayedWords.indexOf(stemmed[0]);
if (index == -1) {
return;
}
}
Element el = doc.getElement(Integer.toString(index));
if (el == null) {
return;
}
int start = el.getStartOffset();
// When trying to select the last word, Swing cannot make the rectangle
// if the end is pointing past the last character.
int end = Math.max(el.getEndOffset() - 1, start);
try {
// Start position of article
Rectangle startRect = Java8Compat.modelToView(this, start);
// End position of article
Rectangle endRect = Java8Compat.modelToView(this, end);
// when initiating scroll from below the target article.
if (endRect != null) {
scrollRectToVisible(endRect);
}
if (startRect != null) {
scrollRectToVisible(startRect);
}
} catch (BadLocationException ex) {
Log.log(ex);
}
}
Aggregations