X

Geertjan's Blog

JavaFX MultiViewElement

Geertjan Wielenga
Product Manager

Here's my first attempt at integrating the visual JavaFX HTMLEditor on top of HTML files on the NetBeans Platform, e.g., NetBeans IDE.

All the code and note that the two sides aren't synchronized (yet):

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.HTMLEditor;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.filesystems.FileObject;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
@MultiViewElement.Registration(displayName = "Visual",
mimeType = {"text/xhtml", "text/html"},
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "Visual",
position = 1)
public class JavaFXMVE extends JPanel implements MultiViewElement {
private FileObject obj;
private JToolBar tb = new JToolBar();
private static JFXPanel fxContainer;
public JavaFXMVE(Lookup lkp) {
this.obj = lkp.lookup(FileObject.class);
assert obj != null;
fxContainer = new JFXPanel();
Platform.setImplicitExit(false);
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
private void createScene() {
HTMLEditor htmlEditor = new HTMLEditor();
try {
htmlEditor.setHtmlText(obj.asText());
} catch (IOException ex) {
Logger.getLogger(JavaFXMVE.class.getName()).log(Level.SEVERE, null, ex);
}
StackPane root = new StackPane();
root.getChildren().add(htmlEditor);
fxContainer.setScene(new Scene(root));
}
@Override
public JComponent getVisualRepresentation() {
return fxContainer;
}
@Override
public JComponent getToolbarRepresentation() {
tb.setFloatable(false);
return tb;
}
@Override
public Action[] getActions() {
return new Action[0];
}
@Override
public Lookup getLookup() {
return obj.getLookup();
}
@Override
public void componentOpened() {
}
@Override
public void componentClosed() {
}
@Override
public void componentShowing() {
}
@Override
public void componentHidden() {
}
@Override
public void componentActivated() {
}
@Override
public void componentDeactivated() {
}
@Override
public UndoRedo getUndoRedo() {
return UndoRedo.NONE;
}
@Override
public void setMultiViewCallback(MultiViewElementCallback callback) {
callback.getTopComponent().setDisplayName(obj.getNameExt());
}
@Override
public CloseOperationState canCloseElement() {
return CloseOperationState.STATE_OK;
}
}

If someone has ideas or code for synchronizing the two sides, don't be shy, leave a comment at the end of this blog entry!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.