Java Code Examples for javax.swing.text.StyledDocument#getProperty()

The following examples show how to use javax.swing.text.StyledDocument#getProperty() . 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: DocumentTitlePropertyTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Test updating document property Document.TitleProperty when dataobject is renamed */
public void testRename () throws IOException {
    FileUtil.createData(FileUtil.getConfigRoot(), "someFolder/someFile.obj");
    
    DataObject obj = DataObject.find(FileUtil.getConfigFile("someFolder/someFile.obj"));
    assertEquals( MyDataObject.class, obj.getClass());
    assertTrue( "we need UniFileLoader", obj.getLoader() instanceof UniFileLoader );

    EditorCookie ec = obj.getCookie(EditorCookie.class);
    
    StyledDocument doc = ec.openDocument();

    String val = (String) doc.getProperty(Document.TitleProperty);
    assertTrue("Test property value", val.startsWith("someFolder/someFile.obj"));

    obj.rename("newFile");

    val = (String) doc.getProperty(Document.TitleProperty);
    assertTrue("Test property value", val.startsWith("someFolder/newFile.obj"));
}
 
Example 2
Source File: DocumentTitlePropertyTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Test updating document property Document.TitleProperty when dataobject is moved */
public void testMove () throws IOException {
    FileUtil.createData(FileUtil.getConfigRoot(), "someFolder/someFile.obj");
    FileUtil.createFolder(FileUtil.getConfigRoot(), "newFolder");

    DataObject obj = DataObject.find(FileUtil.getConfigFile("someFolder/someFile.obj"));
    DataFolder dFolder = (DataFolder) DataObject.find(FileUtil.getConfigFile("newFolder"));

    assertEquals( MyDataObject.class, obj.getClass());
    assertTrue( "we need UniFileLoader", obj.getLoader() instanceof UniFileLoader );

    EditorCookie ec = obj.getCookie(EditorCookie.class);

    StyledDocument doc = ec.openDocument();

    String val = (String) doc.getProperty(Document.TitleProperty);
    assertTrue("Test property value", val.startsWith("someFolder/someFile.obj"));

    obj.move(dFolder);

    val = (String) doc.getProperty(Document.TitleProperty);
    assertTrue("Test property value", val.startsWith("newFolder/someFile.obj"));
}
 
Example 3
Source File: BaseJspEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected StyledDocument createStyledDocument(EditorKit kit) {
    StyledDocument doc = super.createStyledDocument(kit);

    //#174763 workaround - there isn't any elegant place where to place
    //a code which needs to be run after document's COMPLETE initialization.
    //DataEditorSupport.createStyledDocument() creates the document via the
    //EditorKit.createDefaultDocument(), but some of the important properties
    //like Document.StreamDescriptionProperty or mimetype are set as the
    //document properties later.
    //A hacky solution is that a Runnable can be set to the postInitRunnable property
    //in the EditorKit.createDefaultDocument() and the runnable is run
    //once the document is completely initialized.
    Runnable postInitRunnable = (Runnable)doc.getProperty("postInitRunnable"); //NOI18N
    if(postInitRunnable != null) {
        postInitRunnable.run();
    }

    return doc;
}
 
Example 4
Source File: BaseJspEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException {
    Parameters.notNull("doc", doc);
    Parameters.notNull("kit", kit);

    String foundEncoding = (String) doc.getProperty(DOCUMENT_SAVE_ENCODING);
    String encoding = foundEncoding != null ? foundEncoding : defaulEncoding;
    Charset charset = Charset.forName("UTF-8"); //NOI18N
    try {
        charset = Charset.forName(encoding);
    } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
        LOGGER.log(Level.INFO, "Illegal charset found: {0}, defaulted to UTF-8 as warned by dialog", encoding);
    }
    writeByteOrderMark(charset, stream);
    super.saveFromKitToStream(doc, kit, stream);
}
 
Example 5
Source File: TplEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException {
    String foundEncoding = (String) doc.getProperty(DOCUMENT_SAVE_ENCODING);
    String usedEncoding = foundEncoding != null ? foundEncoding : UTF_8_ENCODING;
    final Charset c = Charset.forName(usedEncoding);
    final Writer w = new OutputStreamWriter(stream, c);
    try {
        kit.write(w, doc, 0, doc.getLength());
    } finally {
        w.close();
    }
}
 
Example 6
Source File: TplEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected StyledDocument createStyledDocument(EditorKit kit) {
    StyledDocument doc = super.createStyledDocument(kit);

    // see TplKit.createDefaultDocument;
    Runnable postInitRunnable = (Runnable) doc.getProperty("postInitRunnable"); //NOI18N
    if (postInitRunnable != null) {
        postInitRunnable.run();
    }

    return doc;
}
 
Example 7
Source File: HtmlEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException {
    String foundEncoding = (String) doc.getProperty(DOCUMENT_SAVE_ENCODING);
    String usedEncoding = foundEncoding != null ? foundEncoding : UTF_8_ENCODING;
    final Charset c = Charset.forName(usedEncoding);
    final Writer w = new OutputStreamWriter(stream, c);
    try {
        kit.write(w, doc, 0, doc.getLength());
    } finally {
        w.close();
    }
}
 
Example 8
Source File: GuardedSectionManager.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the manager instance.
 * @param doc a document containing guarded sections
 * @return the manager instance or <code>null</code>.
 */
public static GuardedSectionManager getInstance(StyledDocument doc) {
    return (GuardedSectionManager) doc.getProperty(GuardedSectionManager.class);
}