Skip to content

Commit

Permalink
Fix UI issue when moving the HTML Editor window around the main window.
Browse files Browse the repository at this point in the history
Reload page when html file is saved.
  • Loading branch information
cjmach committed Jun 12, 2024
1 parent bb6b6d8 commit edffe7a
Showing 1 changed file with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

import chrriis.dj.nativeswing.NSComponentOptions;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import java.awt.EventQueue;
import java.util.Collection;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
Expand All @@ -45,46 +46,40 @@
public class HtmlPreviewMultiViewElement extends MultiViewEditorElement {

private JWebBrowser browser;
Lookup.Result<SaveCookie> saveCookieResult;

public HtmlPreviewMultiViewElement(Lookup lookup) {
super(lookup);
DataObject htmlObject = getLookup().lookup(DataObject.class);
htmlObject.addPropertyChangeListener((pce) -> {
if (DataObject.PROP_MODIFIED.equals(pce.getPropertyName())) {
updateName();
EventQueue.invokeLater(() -> updateName());
}
});
saveCookieResult = getLookup().lookupResult(SaveCookie.class);
saveCookieResult.addLookupListener((e) -> {
Collection<? extends SaveCookie> cookies = saveCookieResult.allInstances();
if (cookies.isEmpty()) {
EventQueue.invokeLater(() -> {
getVisualRepresentation().reloadPage();
});
}
});
}

@Override
public void componentShowing() {
// TODO: Update content only when there's actually a change.
// It's currently updating the content everytime we select the view.
getVisualRepresentation().setHTMLContent(getHTMLContent());
}

private String getHTMLContent() {
DataObject htmlObject = getLookup().lookup(DataObject.class);
EditorCookie cookie = htmlObject.getCookie(EditorCookie.class);
StyledDocument document = cookie.getDocument();
if (document == null) {
return null;
}
try {
return document.getText(0, document.getLength());
} catch (BadLocationException ex) {
return "";
}

}

@Override
public JWebBrowser getVisualRepresentation() {
if (browser == null) {
browser = new JWebBrowser(NSComponentOptions.destroyOnFinalization());
browser = new JWebBrowser(
NSComponentOptions.destroyOnFinalization(),
NSComponentOptions.proxyComponentHierarchy());
browser.setJavascriptEnabled(true);
browser.setDefaultPopupMenuRegistered(false);
browser.setBarsVisible(false);

DataObject htmlObject = getLookup().lookup(DataObject.class);
FileObject file = htmlObject.getPrimaryFile();
browser.navigate(file.toURL().toString());
}
return browser;
}
Expand Down

0 comments on commit edffe7a

Please sign in to comment.