Tuesday Oct 01, 2013

JavaFX MultiViewElement

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!

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« October 2013 »
SunMonTueWedThuFriSat
  
5
6
  
       
Today