Visual View for Schema Based Editor

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Thu, 27 Sep 2012 23:19:32 +0000 Indexed on 2012/09/28 3:45 UTC
Read the original article Hit count: 198

Filed under:

Starting from yesterday's blog entry, make the following change in the DataObject's constructor:

registerEditor("text/x-sample+xml", true);

I.e., the MultiDataObject.registerEditor method turns the editor into a multiview component.

Now, again, within the DataObject, add the following, to register a source editor in the multiview component:

@MultiViewElement.Registration(
        displayName = "#LBL_Sample_Source",
        mimeType = "text/x-sample+xml",
        persistenceType = TopComponent.PERSISTENCE_NEVER,
        preferredID = "ShipOrderSourceView",
        position = 1000)
@NbBundle.Messages({
    "LBL_Sample_Source=Source"
})
public static MultiViewElement createEditor(Lookup lkp){
    return new MultiViewEditorElement(lkp);
}

Result:


Next, let's create a visual editor in the multiview component. This could be within the same module as the above or within a completely separate module. That makes it possible for external contributors to provide modules with new editors in an existing multiview component:

@MultiViewElement.Registration(displayName = "#LBL_Sample_Visual",
    mimeType = "text/x-sample+xml",
    persistenceType = TopComponent.PERSISTENCE_NEVER,
    preferredID = "VisualEditorComponent",
    position = 500)
@NbBundle.Messages({
    "LBL_Sample_Visual=Visual"
})
public class VisualEditorComponent extends JPanel implements MultiViewElement {

    public VisualEditorComponent() {
        initComponents();
    }
 
    @Override
    public String getName() {
        return "VisualEditorComponent";
    }
 
    @Override
    public JComponent getVisualRepresentation() {
        return this;
    }

    @Override
    public JComponent getToolbarRepresentation() {
        return new JToolBar();
    }

    @Override
    public Action[] getActions() {
        return new Action[0];
    }

    @Override
    public Lookup getLookup() {
        return Lookup.EMPTY;
    }

    @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) {
    }

    @Override
    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }

} 

Result:


Next, the DataObject is automatically returned from the Lookup of DataObject. Therefore, you can go back to your visual editor, add a LookupListener, listen for DataObjects, parse the underlying XML file, and display values in GUI components within the visual editor.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE