Java Code Examples for javax.swing.text.EditorKit#createDefaultDocument()

The following examples show how to use javax.swing.text.EditorKit#createDefaultDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrowserPane.java    From SwingBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Document createDocument(EditorKit kit, URL page)
{
    // we have pageProperties, because we can be in situation that
    // old page is being removed & new page is not yet created...
    // we need somewhere store important data.
    Document doc = kit.createDefaultDocument();
    if (pageProperties != null)
    {
        // transfer properties discovered in stream to the
        // document property collection.
        for (Enumeration<String> e = pageProperties.keys(); e
                .hasMoreElements();)
        {
            Object key = e.nextElement();
            doc.putProperty(key, pageProperties.get(key));
        }
    }
    if (doc.getProperty(Document.StreamDescriptionProperty) == null)
    {
        doc.putProperty(Document.StreamDescriptionProperty, page);
    }
    return doc;
}
 
Example 2
Source File: StringUtils.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Given some html, extracts its text.
 */
public static String extractTextFromHTML(String html) {
    try {
        EditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();

        // The Document class does not yet handle charset's properly.
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);

        // Create a reader on the HTML content.
        Reader rd = new StringReader(html);

        // Parse the HTML.
        kit.read(rd, doc, 0);

        //  The HTML text is now stored in the document
        return doc.getText(0, doc.getLength());
    } catch (Exception e) {
    }
    return "";
}
 
Example 3
Source File: IndentFileEntry.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private StyledDocument createDocument(EditorKit kit) {
    Document doc = kit.createDefaultDocument();
    if (doc instanceof StyledDocument) {
        return (StyledDocument)doc;
    } else {
        return new org.openide.text.FilterDocument(doc);
    }
}
 
Example 4
Source File: EncodedReaderFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** @return The writer or <code>null</code>. */
private Writer getWriterFromKit(File file, FileObject fo, FileLock lock, String mimeType) throws FileNotFoundException {
    EditorKit kit = CloneableEditorSupport.getEditorKit(mimeType);
    if (kit.getContentType().equalsIgnoreCase("text/plain") && "text/x-dtd".equalsIgnoreCase(mimeType)) {
         // Use XML kit for DTDs if not defined otherwise
        kit = CloneableEditorSupport.getEditorKit("text/xml");
    }
    //System.out.println("  KIT for "+mimeType+" = "+kit);
    if (kit != null) {
        Document doc = kit.createDefaultDocument();
        return new DocWriter(doc, fo, lock, file, kit, null, null);
    }
    return null;
}
 
Example 5
Source File: Navigator.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private Document initializeModel(EditorKit kit, URL page) {
    Document doc = kit.createDefaultDocument();

    if (doc.getProperty(Document.StreamDescriptionProperty) == null) {
        doc.putProperty(Document.StreamDescriptionProperty, page);
    }
    return doc;
}
 
Example 6
Source File: InfoPane.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setText(String text)
{
	//This is done so the vertical scroll bar goes back up to the top when the text is changed
	EditorKit kit = textPane.getEditorKit();
	Document newDoc = kit.createDefaultDocument();
	try
	{
		kit.read(new StringReader(text), newDoc, 0);
	}
	catch (IOException | BadLocationException ex)
	{
		throw new UnreachableError(ex);
	}
	textPane.setDocument(newDoc);
}
 
Example 7
Source File: InfoPane.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setText(String text)
{
	//This is done so the vertical scroll bar goes back up to the top when the text is changed
	EditorKit kit = textPane.getEditorKit();
	Document newDoc = kit.createDefaultDocument();
	try
	{
		kit.read(new StringReader(text), newDoc, 0);
	}
	catch (IOException | BadLocationException ex)
	{
		throw new UnreachableError(ex);
	}
	textPane.setDocument(newDoc);
}
 
Example 8
Source File: WebUtilsTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Source getSourceForMimeType(String mimeType) {
    EditorKit kit = new DefaultEditorKit();
    Document doc = kit.createDefaultDocument();
    doc.putProperty("mimeType", mimeType);
    return Source.create(doc);
}
 
Example 9
Source File: EncodedReaderFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** @return The reader or <code>null</code>. */
private Reader getReaderFromKit(File file, FileObject fo, String mimeType) throws FileNotFoundException {
    EditorKit kit = CloneableEditorSupport.getEditorKit(mimeType);
    if (kit.getContentType().equalsIgnoreCase("text/plain") && "text/x-dtd".equalsIgnoreCase(mimeType)) {
         // Use XML kit for DTDs if not defined otherwise
        kit = CloneableEditorSupport.getEditorKit("text/xml");
    }
    //System.out.println("  KIT for "+mimeType+" = "+kit);
    if (kit != null) {
        Document doc = kit.createDefaultDocument();
        InputStream stream = null;
        try {
            if (file != null) {
                stream = new FileInputStream(file);
            } else {
                stream = fo.getInputStream();
            }
            kit.read(stream, doc, 0);
            String text = doc.getText(0, doc.getLength());
            //System.out.println("  TEXT = "+text);
            doc = null; // Release it, we have the text
            return new StringReader(text);
        } catch (IOException ioex) {
            FileNotFoundException fnfex;
            if (file != null) {
                fnfex = new FileNotFoundException("Can not read file "+file.getAbsolutePath());
            } else {
                fnfex = new FileNotFoundException("Can not read file "+fo);
            }
            fnfex.initCause(ioex);
            throw fnfex;
        } catch (BadLocationException blex) { // Something wrong???
            ErrorManager.getDefault().notify(blex);
        } finally {
            if (stream != null) {
                try { stream.close(); } catch (IOException e) {}
            }
        }
    }
    return null;
}
 
Example 10
Source File: UndoFixedEditorPane.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private void setText(String t, String contentType) {
    synchronized (setTextLock) {
        if (t == null) {
            t = "";
        }

        if (!t.equals(getText())) {
            boolean plain = t.length() > Configuration.syntaxHighlightLimit.get();
            if (plain) {
                contentType = "text/plain";
                originalContentType = getContentType();
                changeContentType(contentType);
            } else if (originalContentType != null) {
                changeContentType(originalContentType);
                originalContentType = null;
            }

            Stopwatch sw = Stopwatch.startNew();
            try {
                Reader r = new StringReader(t);
                EditorKit kit = createEditorKitForContentType(contentType);
                Document doc = kit.createDefaultDocument();
                if (doc instanceof SyntaxDocument) {
                    ((SyntaxDocument) doc).setIgnoreUpdate(true);
                }

                kit.read(r, doc, 0);

                if (doc instanceof SyntaxDocument) {
                    ((SyntaxDocument) doc).setIgnoreUpdate(false);
                }

                setDocument(doc);
            } catch (BadLocationException | IOException ex) {
                Logger.getLogger(UndoFixedEditorPane.class.getName()).log(Level.SEVERE, null, ex);
            }

            sw.stop();
            if (!plain && sw.getElapsedMilliseconds() > 5000) {
                Logger.getLogger(UndoFixedEditorPane.class.getName()).log(Level.WARNING, "Syntax highlightig took long time. You can try to decrease the syntax highlight limit in advanced settings.");
            }

            clearUndos();
        }
    }
}