Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Move document parsing out of Swing EDT

AuthorDaveJarvis <email>
Date2021-11-06 16:28:41 GMT-0700
Commitec5a9355aab44dc5239c9dbcf92047860a7bc109
Parentdfdfe10
Delta18 lines added, 26 lines removed, 8-line decrease
src/main/java/com/keenwrite/preview/HtmlPanelImpl.java
package com.keenwrite.preview;
-import com.keenwrite.dom.DocumentConverter;
import com.keenwrite.ui.adapters.DocumentAdapter;
import javafx.beans.property.BooleanProperty;
import java.net.URI;
-import static com.keenwrite.events.DocumentChangedEvent.fireDocumentChangedEvent;
import static com.keenwrite.events.FileOpenEvent.fireFileOpenEvent;
import static com.keenwrite.events.HyperlinkOpenEvent.fireHyperlinkOpenEvent;
import static com.keenwrite.events.StatusEvent.clue;
import static com.keenwrite.util.ProtocolScheme.getProtocol;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static javax.swing.SwingUtilities.invokeLater;
-import static javax.swing.SwingUtilities.isEventDispatchThread;
-import static org.jsoup.Jsoup.parse;
/**
}
- private static final DocumentConverter CONVERTER = new DocumentConverter();
private static final XhtmlNamespaceHandler XNH = new XhtmlNamespaceHandler();
private final ChainedReplacedElementFactory mFactory;
* updates the HTML document to provide new content.
*
- * @param html A complete HTML5 document, including doctype.
+ * @param doc A complete HTML5 document, including doctype.
* @param baseUri URI to use for finding relative files, such as images.
*/
- void render( final String html, final String baseUri ) {
- final var soup = parse( html );
- final var doc = CONVERTER.fromJsoup( soup );
- final Runnable renderDocument = () -> setDocument( doc, baseUri, XNH );
- doc.setDocumentURI( baseUri );
-
- // Access to a Swing component must occur from the Event Dispatch
- // Thread (EDT) according to Swing threading restrictions. Setting a new
- // document invokes a Swing repaint operation.
- if( isEventDispatchThread() ) {
- renderDocument.run();
- }
- else {
- invokeLater( renderDocument );
- }
-
- // When the text changes, let subscribers know. This allows for text
- // analysis to occur on a separate thread.
- fireDocumentChangedEvent( soup );
+ void render( final org.w3c.dom.Document doc, final String baseUri ) {
+ invokeLater( () -> setDocument( doc, baseUri, XNH ) );
}
src/main/java/com/keenwrite/preview/HtmlPreview.java
package com.keenwrite.preview;
+import com.keenwrite.dom.DocumentConverter;
import com.keenwrite.events.ScrollLockEvent;
import com.keenwrite.preferences.LocaleProperty;
import static com.keenwrite.constants.Constants.*;
import static com.keenwrite.events.Bus.register;
+import static com.keenwrite.events.DocumentChangedEvent.fireDocumentChangedEvent;
import static com.keenwrite.events.ScrollLockEvent.fireScrollLockEvent;
import static com.keenwrite.events.StatusEvent.clue;
import static org.controlsfx.glyphfont.FontAwesome.Glyph.LOCK;
import static org.controlsfx.glyphfont.FontAwesome.Glyph.UNLOCK_ALT;
+import static org.jsoup.Jsoup.parse;
/**
* Responsible for parsing an HTML document.
*/
public final class HtmlPreview extends SwingNode implements ComponentListener {
+ /**
+ * Converts a text string to a structured HTML document.
+ */
+ private static final DocumentConverter CONVERTER = new DocumentConverter();
+
/**
* Used to populate the {@link #HTML_HEAD} with stylesheet file references.
*/
public void render( final String html ) {
- mPreview.render( decorate( html ), getBaseUri() );
+ final var soup = parse( decorate( html ) );
+ final var doc = CONVERTER.fromJsoup( soup );
+ doc.setDocumentURI( getBaseUri() );
+
+ invokeLater( () -> mPreview.render( doc, getBaseUri() ) );
+
+ fireDocumentChangedEvent(html);
}