use of org.csstudio.apputil.ui.formula.InputItem in project org.csstudio.display.builder by kasemir.
the class EditFormulaDialog method determineInputs.
/**
* @return List of inputs for formula: Each model item is a possible input,
* mapped to a variable name that's either already used in the
* formula for that model item, or a simple "x1", "x2", ... when
* not already used
*/
@SuppressWarnings("nls")
private InputItem[] determineInputs() {
final Model model = formula.getModel().get();
// Create list of inputs.
final ArrayList<InputItem> inputs = new ArrayList<InputItem>();
// Every model item is a possible input.
model_loop: for (ModelItem model_item : model.getItems()) {
// Formula cannot be an input to itself
if (model_item == formula)
continue;
// Create InputItem for that ModelItem
InputItem input = null;
// See if model item is already used in the formula
for (FormulaInput existing_input : formula.getInputs()) {
if (existing_input.getItem() == model_item) {
// Yes, use the existing variable name
input = new InputItem(model_item.getName(), existing_input.getVariableName());
break;
}
}
// If input is unused, assign variable name x1, x2, ...
if (input == null) {
for (InputItem existing_item : inputs) if (existing_item.getInputName().equals(model_item.getName())) {
// The item with the same name was already added to the input list.
continue model_loop;
}
// Try "x1", then "xx1", "xxx1" until an unused name is found
String var_name = Integer.toString(inputs.size() + 1);
boolean name_in_use;
do {
name_in_use = false;
var_name = "x" + var_name;
for (final FormulaInput existing_input : formula.getInputs()) if (existing_input.getVariableName().equals(var_name)) {
name_in_use = true;
break;
}
} while (name_in_use);
input = new InputItem(model_item.getName(), var_name);
}
inputs.add(input);
}
return inputs.toArray(new InputItem[inputs.size()]);
}
use of org.csstudio.apputil.ui.formula.InputItem in project org.csstudio.display.builder by kasemir.
the class EditFormulaDialog method open.
/**
* Open, i.e. display the dialog.
* @return <code>true</code> when the item was updated,
* <code>false</code> for 'cancel'
*/
public boolean open() {
try {
// Edit
dialog = new FormulaDialog(shell, formula.getExpression(), determineInputs());
if (dialog.open() != Window.OK)
return false;
// Update model item with new formula from dialog
final Model model = formula.getModel().get();
final ArrayList<FormulaInput> new_inputs = new ArrayList<FormulaInput>();
for (final InputItem input : dialog.getInputs()) {
final ModelItem item = model.getItem(input.getInputName());
if (item == null)
// $NON-NLS-1$
throw new Exception("Cannot locate formula input " + input.getInputName());
new_inputs.add(new FormulaInput(item, input.getVariableName()));
}
// Update formula via undo-able command
new ChangeFormulaCommand(shell, operations_manager, formula, dialog.getFormula(), new_inputs.toArray(new FormulaInput[new_inputs.size()]));
} catch (final Exception ex) {
ExceptionDetailsErrorDialog.openError(shell, Messages.Error, ex);
return false;
}
return true;
}
Aggregations