Skip to main content

Structure Editor application

Creating a Structure Editor

MathFlow Structure Editor components can be used within Java applications. There are two versions of the Structure Editor component. Note: These pages linked below are offered here for your convenience. If you have installed the MathFlow SDK, it's better if you refer to the copy that was installed with the SDK. These are located in the javadocs folder, which for Windows is specifically C:/Program Files (x86)/MathFlow SDK/2.1/java/docs/javadoc/com/dessci/mathflow/sdk/editor/

The StructureEditorFrame components can be embedded either directly in a Java application or opened in a separate, non-modal window. The StructureEditorDialog components can be opened in a separate modal window. To create an instance of either one of these, the constructor must be called with two or more of the following objects:

  • (Required) Host Application for callbacks and notifications.

  • (Required) License key or FlexNet license file.

  • (Optional) StructureConfigInfo. More information in the javadocs folder, at /webeq3/app/.

  • (Optional) OptionsInfo, which allows you to save the General Editing Preferences to an external file to persist user settings. More information in the javadocs folder, at /webeq3/util/.

Sample Structure Editor application

Overview, Structure Editor application

Included in the MathFlow SDK is a sample Structure Editor application, located at

<path-to-mathflow-sdk>/java/samples/com/yourcompany/samplecode/editor/StructureEditorDemoApplication.java

There is a bat file to run the demo application:

<path-to-mathflow-sdk>/java/samples/structure_editor.bat

This sample application illustrates how to include the MathFlow SDK Structure Editor in your own application. In this case, the StructureEditorDialog class is used to create a modal pop-up MathML editor:

mathflow_structure_editor_application.png

This sample application does the following:

  • Read MathML from a file

  • Write MathML to a file

  • Send MathML to the Structure Editor

  • Retrieve MathML from the Structure Editor

  • Retrieve an image of the MathML equation from the Structure Editor

  • Display the current MathML

  • Display an image of the MathML equation from the Structure Editor once OK has been clicked in the editor

  • Display an about box

Structure Editor classes

The following classes have been written for the sample Structure Editor application:

  • StructureEditorDemoApplication: The host application that launches the customized StructureEditorDialog.

  • SampleStructureEditorDialog: A subclass of the StructureEditorDialog class customized for this application.

  • SampleStructureEditorAboutBox: A subclass of the StructureEditorAboutBox class customized for this application.

StructureEditorDemoApplication

The StructureEditorDemoApplication user interface consists of the following:

  • File Menu

    • Open MathML…

    • Save MathML…

    • Quit

  • Help Menu

    • About MathFlow SDK Structure Editor

  • MathML source panel

  • Preview image panel

  • Edit Equation… button

Caution

NOTE: The code discussed in this topic can be found in StructureEditorDemoApplication.java, located at <[MF] installation folder>/java/samples/com/yourcompany/samplecode/editor/StructureEditorDemoApplication.java.

  1. The application's constructor sets the application title, font preferences and calls initUI() to create the application's UI elements. It also changes the cursor to a wait cursor. For more information on setting the font preferences, see Java rendering engine.

  2. The createEditor method creates two objects, which are then passed into the constructor of the SampleStructureEditorDialog:

    • Creates a StructureConfigurationInfo object to indicate which features are turned on or off in the editor. You can also set the type of end-user help system in this object. See the sample code in StructureEditorDemoApplication.java (mentioned above) and End-user help options for choosing and setting your help system.

    • Sets the key string, which is either the path to a FlexNet license file or a license key string assigned by Wiris. See the sample code in StructureEditorDemoApplication.java (mentioned above) and MathFlow SDK licenses for help setting the license string and more information about the license types.

  3. The method showEditor checks if createEditor() has been successful and displays the Structure Editor as a dialog.

  4. The updateSrcAndImage method retrieves the MathML from the editor using the editor's getMathML method, and displays it in the MathML source panel. The method also retrieves an image of the equation, using the editor's getEquationImage method, as well as its size using the editor's getPreferredWidthAt and getPreferredHeightAt methods. The image is then displayed in the Image preview panel.

  5. The notifyEditorShown method is used by the editorDialog to notify the host application when the editor has been shown. The host application then resets the cursor back to its default state.

  6. The host application can also display a customized About box. See SampleStructureEditorAboutBox.java, located at <[MF] installation folder>/java/samples/com/yourcompany/samplecode/editor/StructureEditorAboutBox.java.

SampleStructureEditorDialog

The SampleStructureEditorDialog class extends the StructureEditorDialog class provided in the MFStructureEditor.jar file. The constructor for this class requires the following:

  • A reference to the host application

  • A license key string, which can either be a path to the license file or a key string provided by Wiris. See MathFlow SDK licenses.

  • A configuration object to indicate which built-in features to turn on or off

One of the host application's functions is to request the MathML of the equation in the Structure Editor. This functionality is already provided by the StructureEditor class; therefore, creating a method to access it is straightforward.

public String getMathML(){ return getFormattedMathML(Equation.PRESENTATION, Equation.PRETTY_PRINT, 0, "", Equation.ENTITY_NAMES); }

Similarly, the host application needs a way to send MathML to the Structure Editor. Again, this functionality is already provided by the StructureEditor class. Note you will need to reset the undo stack since it is a new equation.

public void setMathML(String mathml) { super.setMathML(mathml); // we want to clear the undo stack from previous editing operations clearUndoStack(); }

The host application needs a way to retrieve an image of the MathML equation in the Structure Editor. This functionality is already provided by the class. In this case, only the point size and background color of the equation are passed in, but there are other parameters that can be used to control the look of an equation.

public Image getEquationImage(int pointsize, String bgcolor) { return getEquationImage(pointsize, 0, "black", bgcolor, "left", "top", 0, 0, 0, 0); }

It is also possible to customize the About box to add additional information. See the code for the customized about box in SampleStructureEditorAboutBox.java.

protected void showAboutBox() { if (aboutBox == null) { aboutBox = new SampleStructureEditorAboutBox(this); } Rectangle r1 = getBounds(); Rectangle r2 = aboutBox.getBounds(); aboutBox.setLocation(r1.x + (r1.width - r2.width) / 2, r1.y + (r1.height - r2.height) / 2); aboutBox.show(); }

Finally, when the Structure Editor is closed, it calls back to the host application to tell it to update the MathML Source and Preview Image parts of its screen.

protected void closeEditorWindow() { if (getCallbackInfo() == COMPLETED) { ownerApp.updateSrcAndImage(); } // you MUST write out the users preferences or any changes will be lost writeExportOptions(); hide(); }