use of javax.swing.text.Element in project jdk8u_jdk by JetBrains.
the class bug8048110 method createAndShowGUI.
private static void createAndShowGUI() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
HTMLEditorKit editorKit = new HTMLEditorKit();
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setEditorKit(editorKit);
textPane.setText("Initial text without table");
JFrame frame = new JFrame("bug8048110");
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setVisible(true);
textPane.setDocument(textPane.getEditorKit().createDefaultDocument());
HTMLDocument htmlDocument = (HTMLDocument) textPane.getDocument();
Element firstParagraph = findFirstElement(textPane.getDocument().getDefaultRootElement(), "p");
try {
htmlDocument.setInnerHTML(firstParagraph, htmlText);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of javax.swing.text.Element in project pcgen by PCGen.
the class ExtendedHTMLEditorKit method insertListElement.
/**
* Insert a list element
* @param pane
* @param content
*/
public static void insertListElement(JTextPane pane, String content) {
int pos = pane.getCaretPosition();
ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) pane.getStyledDocument();
String source = pane.getText();
boolean hit;
String idString;
int counter = 0;
do {
hit = false;
idString = "diesisteineidzumsuchenimsource" + counter;
if (source.contains(idString)) {
counter++;
hit = true;
if (counter > 10000) {
return;
}
}
} while (hit);
Element element = getListItemParent(htmlDoc.getCharacterElement(pane.getCaretPosition()));
if (element == null) {
return;
}
SimpleAttributeSet sa = new SimpleAttributeSet(element.getAttributes());
sa.addAttribute("id", idString);
((ExtendedHTMLDocument) pane.getStyledDocument()).replaceAttributes(element, sa, HTML.Tag.LI);
source = pane.getText();
StringBuilder newHtmlString = new StringBuilder();
int[] positions = getPositions(element, source, true, idString);
newHtmlString.append(source.substring(0, positions[3]));
newHtmlString.append("<li>");
newHtmlString.append(content);
newHtmlString.append("</li>");
newHtmlString.append(source.substring(positions[3] + 1, source.length()));
pane.setText(newHtmlString.toString());
pane.setCaretPosition(pos - 1);
element = getListItemParent(htmlDoc.getCharacterElement(pane.getCaretPosition()));
sa = new SimpleAttributeSet(element.getAttributes());
sa = removeAttributeByKey(sa, "id");
((ExtendedHTMLDocument) pane.getStyledDocument()).replaceAttributes(element, sa, HTML.Tag.LI);
}
use of javax.swing.text.Element in project pcgen by PCGen.
the class NotesView method manageListElement.
private void manageListElement(ExtendedHTMLDocument htmlDoc) {
Element h = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
h.getParentElement();
ExtendedHTMLEditorKit.removeTag(editor, h, true);
}
use of javax.swing.text.Element in project pcgen by PCGen.
the class NotesView method handleEnter.
private void handleEnter() {
// TODO: this sucks. clean it up
Element elem;
int pos = editor.getCaretPosition();
ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
try {
if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.UL) || ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.OL)) {
elem = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
int so = elem.getStartOffset();
int eo = elem.getEndOffset();
char[] temp = editor.getText(so, eo - so).toCharArray();
boolean content = false;
for (char aTemp : temp) {
if (!Character.isWhitespace(aTemp)) {
content = true;
}
}
int repos = -1;
if (content) {
int end = -1;
int j = temp.length;
do {
j--;
if (Character.isLetterOrDigit(temp[j])) {
end = j;
}
} while ((end == -1) && (j >= 0));
j = end;
do {
j++;
if (!Character.isSpaceChar(temp[j])) {
repos = j - end - 1;
}
} while ((repos == -1) && (j < temp.length));
if (repos == -1) {
repos = 0;
}
}
if ((elem.getStartOffset() == elem.getEndOffset()) || !content) {
manageListElement(htmlDoc);
} else {
if ((editor.getCaretPosition() + 1) == elem.getEndOffset()) {
ExtendedHTMLEditorKit.insertListElement(editor, "");
editor.setCaretPosition(pos - repos);
} else {
int caret = editor.getCaretPosition();
String tempString = editor.getText(caret, eo - caret);
editor.select(caret, eo - 1);
editor.replaceSelection("");
ExtendedHTMLEditorKit.insertListElement(editor, tempString);
Element newLi = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
editor.setCaretPosition(newLi.getEndOffset());
}
}
}
} catch (BadLocationException ble) {
Logging.errorPrint(ble.getMessage(), ble);
}
}
use of javax.swing.text.Element in project enclojure by EricThorsen.
the class ClojureCodeCompletion_Provider method getRowFirstNonWhite.
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;
}
Aggregations