Search in sources :

Example 1 with ExtendedHTMLDocument

use of gmgen.gui.ExtendedHTMLDocument in project pcgen by PCGen.

the class NotesView method insertLocalImage.

//Image insertion methods
/**
	 *  Method for inserting an image from a file
	 *
	 *@param  whatImage                 pointer to file
	 *@exception  IOException           if the file can't be read
	 *@exception  BadLocationException  if the file does not exist
	 *@exception  RuntimeException      cause
	 */
private void insertLocalImage(File whatImage) throws IOException, BadLocationException, RuntimeException {
    File image = whatImage;
    if (whatImage == null) {
        File dir = getCurrentDir();
        File newImage = getImageFromChooser(dir.getPath(), extsIMG, "Image File");
        //null possible if user cancelled
        if (newImage != null && newImage.exists()) {
            image = new File(dir.getAbsolutePath() + File.separator + newImage.getName());
            if (!image.exists()) {
                MiscUtilities.copy(newImage, image);
            }
        }
    }
    if (image != null) {
        int caretPos = editor.getCaretPosition();
        ExtendedHTMLEditorKit htmlKit = (ExtendedHTMLEditorKit) editor.getEditorKit();
        ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
        htmlKit.insertHTML(htmlDoc, caretPos, "<IMG SRC=\"" + image + "\">", 0, 0, HTML.Tag.IMG);
        editor.setCaretPosition(caretPos + 1);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) ExtendedHTMLEditorKit(gmgen.gui.ExtendedHTMLEditorKit) File(java.io.File) Point(java.awt.Point)

Example 2 with ExtendedHTMLDocument

use of gmgen.gui.ExtendedHTMLDocument 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);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) Element(javax.swing.text.Element) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Example 3 with ExtendedHTMLDocument

use of gmgen.gui.ExtendedHTMLDocument in project pcgen by PCGen.

the class NotesView method handleBackspace.

//methods dealing with Key Events
private void handleBackspace() {
    // TODO: This sucks, clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    StyledDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
    try {
        if (pos > 0) {
            if ((editor.getSelectedText()) != null) {
                ExtendedHTMLEditorKit.delete(editor);
                return;
            }
            int sOffset = htmlDoc.getParagraphElement(pos).getStartOffset();
            if (sOffset == editor.getSelectionStart()) {
                if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.LI)) {
                    elem = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    boolean content = false;
                    int so = elem.getStartOffset();
                    int eo = elem.getEndOffset();
                    if ((so + 1) < eo) {
                        char[] temp = editor.getText(so, eo - so).toCharArray();
                        for (char aTemp : temp) {
                            if (!Character.isWhitespace(aTemp)) {
                                content = true;
                            }
                        }
                    }
                    if (!content) {
                        elem.getParentElement();
                        ExtendedHTMLEditorKit.removeTag(editor, elem, true);
                        editor.setCaretPosition(sOffset - 1);
                        return;
                    }
                    editor.setCaretPosition(editor.getCaretPosition() - 1);
                    editor.moveCaretPosition(editor.getCaretPosition() - 2);
                    editor.replaceSelection("");
                    return;
                }
            }
            editor.replaceSelection("");
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) Element(javax.swing.text.Element) StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)3 Point (java.awt.Point)3 BadLocationException (javax.swing.text.BadLocationException)2 Element (javax.swing.text.Element)2 ExtendedHTMLEditorKit (gmgen.gui.ExtendedHTMLEditorKit)1 File (java.io.File)1 StyledDocument (javax.swing.text.StyledDocument)1