Home News Install Screenshots / Demo Documentation Contribute!



Macro and template development

Macro development is easy. LilyPondTool provides some useful methods to make it more easy.


The InputDialog class

You often need to get some input from the user. LilyPondTool provides a class to make it an easy task. First, you instantiate the class:

import lilytool.macrohelp.InputDialog;
dlg = new InputDialog("Set default text fonts", view);

The two parameters are the dialog box title, and the view. For the view parameter you can almost always use view, which is set by default to the currently active view.

When you have the dialog object, you put input fields on it. The various input fields are all put by the same method: addInput, only the parameters change, thus changing the appearance of the input field. First, look at the most simple field, a text box:

dlg.addInput("Type your name:", "name", "Humpty Dumpty");

The three parameters are: Input field text, variable name, default value. The variable name denotes the variable you can later use in your macro or in your template. To use the name variable in a macro, you need the following:

vars = dlg.getContext(); // The vars variable will contain all the variables set via the input fields
nameOfUser = vars.get("name"); // get the variable called "name" from the variables

If you're writing a template, it is more simple, you can just use the variable $name or ${name}. However, it needs an other way of InputDialog instantiation, please see the example below.

There is always a predefined variable: cancelled. It is true if the user has pressed the Cancel button on the input dialog. So after getting input usually you write your code into a block like this:

if (!vars.get("cancelled")) { // don't do anything if user has cancelled the dialog
    // we do here what we do
}

Example: Getting input in a macro
import lilytool.macrohelp.InputDialog;

dlg=new InputDialog("Write the name", view);
dlg.addInput("Your name:", "name", "Humpty Dumpty");
dlg.show();
vars=dlg.getContext();
if (!vars.get("cancelled")) {

yourName = vars.get("name");
textArea.setSelectedText("Your name is: " + yourName);

}

Example: Getting input in a template
#beanshell(false)
import lilytool.macrohelp.InputDialog;

dlg=new InputDialog("Write name", view, context);
dlg.addInput("Your name:", "name", "Humpty Dumpty");
dlg.show();
#end
#if (!$cancelled)
Your name is: ${name}
#end

Input fields variations

You can use some different input fields if you use different parameters for the addInput methods. They are the following:

  • addLabel(String label) - add a simple label
  • addInput(String label, String name, String defaultValue) - input for a text value
  • addInput(String label, String name, boolean defaultValue) - input for a true/false value
  • addInput(String label, String name, int defaultValue, int min, int max) - input for an integer value, with minimum and maximum values
  • addInput(String label, String name, Object[] choices) - Choosable objects, represented as a multiple selection list.
  • addInput(String label, String name, Object defaultValue, Object[] choices, boolean customValue) - represented with a combo box, which is editable depending on customValue.
  • addInput(String label, String name, JComponent input, boolean fill) - will add a custom component to use. The fill parameter, which tells if the component should be extended horizontally to fill the dialog box, can be omitted (default is true).



Go to lilypond.org

Documentation >> Development >> Macros and templates