use of org.csstudio.opibuilder.script.Expression in project yamcs-studio by yamcs.
the class RulesProperty method readValueFromXML.
@Override
public RulesInput readValueFromXML(Element propElement) throws Exception {
RulesInput result = new RulesInput();
for (Object oe : propElement.getChildren(XML_ELEMENT_RULE)) {
Element se = (Element) oe;
RuleData ruleData = new RuleData(widgetModel);
ruleData.setName(se.getAttributeValue(XML_ATTRIBUTE_NAME));
ruleData.setPropId(se.getAttributeValue(XML_ATTRIBUTE_PROPID));
ruleData.setOutputExpValue(Boolean.parseBoolean(se.getAttributeValue(XML_ATTRIBUTE_OUTPUTEXPRESSION)));
for (Object eo : se.getChildren(XML_ELEMENT_EXPRESSION)) {
Element ee = (Element) eo;
String booleanExpression = ee.getAttributeValue(XML_ATTRIBUTE_BOOLEXP);
Object value = "null";
Element valueElement = ee.getChild(XML_ELEMENT_VALUE);
if (ruleData.isOutputExpValue())
value = valueElement.getText();
else {
value = ruleData.getProperty().readValueFromXML(valueElement);
}
Expression exp = new Expression(booleanExpression, value);
ruleData.addExpression(exp);
}
for (Object pvo : se.getChildren(XML_ELEMENT_PV)) {
Element pve = (Element) pvo;
boolean trig = true;
if (pve.getAttribute(XML_ATTRIBUTE_TRIGGER) != null)
trig = Boolean.parseBoolean(pve.getAttributeValue(XML_ATTRIBUTE_TRIGGER));
ruleData.addPV(new PVTuple(pve.getText(), trig));
}
result.getRuleDataList().add(ruleData);
}
return result;
}
use of org.csstudio.opibuilder.script.Expression in project yamcs-studio by yamcs.
the class RuleDataEditDialog method createActions.
/**
* Creates the actions.
*/
private void createActions() {
addAction = new Action("Add") {
@Override
public void run() {
Expression expression = new Expression("", ruleData.isOutputExpValue() ? "" : ruleData.getProperty().getDefaultValue());
expressionList.add(expression);
expressionViewer.refresh();
setExpressionViewerSelection(expression);
}
};
addAction.setToolTipText("Add an Expression");
addAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
"icons/add.gif"));
copyAction = new Action() {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) expressionViewer.getSelection();
if (!selection.isEmpty() && selection.getFirstElement() instanceof Expression) {
Expression expression = ((Expression) selection.getFirstElement()).getCopy();
expressionList.add(expression);
setExpressionViewerSelection(expression);
}
}
};
copyAction.setText("Copy");
copyAction.setToolTipText("Copy selected expression");
copyAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
"icons/copy.gif"));
copyAction.setEnabled(false);
removeAction = new Action() {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) expressionViewer.getSelection();
if (!selection.isEmpty() && selection.getFirstElement() instanceof Expression) {
expressionList.remove((Expression) selection.getFirstElement());
setExpressionViewerSelection(null);
this.setEnabled(false);
}
}
};
removeAction.setText("Remove Expression");
removeAction.setToolTipText("Remove the selected expression from the list");
removeAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
"icons/delete.gif"));
removeAction.setEnabled(false);
moveUpAction = new Action() {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) expressionViewer.getSelection();
if (!selection.isEmpty() && selection.getFirstElement() instanceof Expression) {
Expression expression = (Expression) selection.getFirstElement();
int i = expressionList.indexOf(expression);
if (i > 0) {
expressionList.remove(expression);
expressionList.add(i - 1, expression);
setExpressionViewerSelection(expression);
}
}
}
};
moveUpAction.setText("Move Expression Up");
moveUpAction.setToolTipText("Move selected expression up");
moveUpAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
"icons/search_prev.gif"));
moveUpAction.setEnabled(false);
moveDownAction = new Action() {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) expressionViewer.getSelection();
if (!selection.isEmpty() && selection.getFirstElement() instanceof Expression) {
Expression expression = (Expression) selection.getFirstElement();
int i = expressionList.indexOf(expression);
if (i < expressionList.size() - 1) {
expressionList.remove(expression);
expressionList.add(i + 1, expression);
setExpressionViewerSelection(expression);
}
}
}
};
moveDownAction.setText("Move Expression Down");
moveDownAction.setToolTipText("Move selected expression down");
moveDownAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
"icons/search_next.gif"));
moveDownAction.setEnabled(false);
}
use of org.csstudio.opibuilder.script.Expression in project yamcs-studio by yamcs.
the class RuleDataEditDialog method refreshActionBarOnSelection.
/**
* Refreshes the enabled-state of the actions.
*/
private void refreshActionBarOnSelection() {
IStructuredSelection selection = (IStructuredSelection) expressionViewer.getSelection();
boolean enabled = !selection.isEmpty() && selection.getFirstElement() instanceof Expression;
copyAction.setEnabled(enabled);
removeAction.setEnabled(enabled);
moveUpAction.setEnabled(enabled);
moveDownAction.setEnabled(enabled);
}
use of org.csstudio.opibuilder.script.Expression in project yamcs-studio by yamcs.
the class RuleDataEditDialog method createExpressionsTableViewer.
/**
* Creates and configures a {@link TableViewer}.
*
* @param parent
* The parent for the table
* @return The {@link TableViewer}
*/
private TableViewer createExpressionsTableViewer(final Composite parent) {
final TableViewer viewer = new TableViewer(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
viewer.getTable().setLinesVisible(true);
viewer.getTable().setHeaderVisible(true);
TableViewerColumn expressionColumn = new TableViewerColumn(viewer, SWT.NONE);
expressionColumn.getColumn().setText("Boolean Expression");
expressionColumn.getColumn().setMoveable(false);
expressionColumn.getColumn().setWidth(200);
expressionColumn.setEditingSupport(new EditingSupport(viewer) {
@Override
protected void setValue(Object element, Object value) {
if (element instanceof Expression)
((Expression) element).setBooleanExpression(value.toString());
viewer.refresh();
}
@Override
protected Object getValue(Object element) {
if (element instanceof Expression)
return ((Expression) element).getBooleanExpression();
return null;
}
@Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor(viewer.getTable());
}
@Override
protected boolean canEdit(Object element) {
return true;
}
});
valueColumn = new TableViewerColumn(viewer, SWT.NONE);
valueColumn.getColumn().setText(ruleData.isOutputExpValue() ? "Output Expression" : "Output Value");
valueColumn.getColumn().setMoveable(false);
valueColumn.getColumn().setWidth(200);
EditingSupport editingSupport = new EditingSupport(viewer) {
@Override
protected void setValue(Object element, Object value) {
if (element instanceof Expression) {
((Expression) element).setValue(value);
}
viewer.refresh();
}
@Override
protected Object getValue(Object element) {
if (element instanceof Expression) {
if (((Expression) element).getValue() == null)
// $NON-NLS-1$
return "";
return ((Expression) element).getValue();
}
return null;
}
@Override
protected CellEditor getCellEditor(Object element) {
if (element instanceof Expression) {
if (ruleData.isOutputExpValue() || ruleData.getProperty().getPropertyDescriptor() == null)
return new TextCellEditor(viewer.getTable());
else
return ruleData.getProperty().getPropertyDescriptor().createPropertyEditor(viewer.getTable());
}
return null;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
};
valueColumn.setEditingSupport(editingSupport);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new ExpressionLabelProvider());
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
viewer.getTable().setLayoutData(gd);
return viewer;
}
use of org.csstudio.opibuilder.script.Expression in project yamcs-studio by yamcs.
the class RuleDataEditDialog method createDialogArea.
@Override
protected Control createDialogArea(Composite parent) {
final Composite parent_Composite = (Composite) super.createDialogArea(parent);
// Parent composite has GridLayout with 1 columns.
// Create embedded composite w/ 2 columns
final Composite mainComposite = new Composite(parent_Composite, SWT.None);
mainComposite.setLayout(new GridLayout(2, false));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData.heightHint = 500;
mainComposite.setLayoutData(gridData);
final Composite topComposite = new Composite(mainComposite, SWT.None);
topComposite.setLayout(new GridLayout(2, false));
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
gd.horizontalSpan = 2;
topComposite.setLayoutData(gd);
createLabel(topComposite, "Rule Name: ");
nameText = new Text(topComposite, SWT.BORDER);
nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
nameText.setText(ruleData.getName());
nameText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
ruleData.setName(nameText.getText());
}
});
createLabel(topComposite, "Property: ");
propCombo = new Combo(topComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
propCombo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
String[] comboItems = new String[propIDList.size()];
int i = 0;
for (String id : propIDList) {
comboItems[i++] = ruleData.getWidgetModel().getProperty(id).getDescription() + " (" + id + // $NON-NLS-1$ //$NON-NLS-2$
")";
}
propCombo.setItems(comboItems);
propCombo.select(propIDList.indexOf(ruleData.getPropId()));
propCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (propIDList.get(propCombo.getSelectionIndex()).equals(AbstractPVWidgetModel.PROP_PVVALUE)) {
MessageDialog.openWarning(propCombo.getShell(), "Warning", "Note: Changing pv_value property with rule or " + "script will not write value to the PV. " + "It only change the graphical value on the widget! " + "If you need to write a PV, please call PV.setValue() from script.");
}
ruleData.setPropId(propIDList.get(propCombo.getSelectionIndex()));
if (ruleData.getProperty().getPropertyDescriptor() == null || ruleData.getProperty().onlyAcceptExpressionInRule()) {
ruleData.setOutputExpValue(true);
outPutExpButton.setSelection(true);
outPutExpButton.setEnabled(false);
for (Expression exp : expressionList) // $NON-NLS-1$
exp.setValue("");
valueColumn.getColumn().setText("Output Expression");
} else
outPutExpButton.setEnabled(true);
if (!ruleData.isOutputExpValue()) {
for (Expression exp : expressionList) exp.setValue(ruleData.isOutputExpValue() ? "" : // $NON-NLS-1$
ruleData.getProperty().getDefaultValue());
}
expressionViewer.refresh();
}
});
outPutExpButton = new Button(topComposite, SWT.CHECK);
gd = new GridData();
gd.horizontalSpan = 2;
outPutExpButton.setLayoutData(gd);
outPutExpButton.setText("Output Expression");
if (ruleData.getProperty().getPropertyDescriptor() == null || ruleData.getProperty().onlyAcceptExpressionInRule()) {
ruleData.setOutputExpValue(true);
outPutExpButton.setEnabled(false);
}
outPutExpButton.setSelection(ruleData.isOutputExpValue());
outPutExpButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ruleData.setOutputExpValue(outPutExpButton.getSelection());
for (Expression exp : expressionList) exp.setValue(ruleData.isOutputExpValue() ? "" : // $NON-NLS-1$
ruleData.getProperty().getDefaultValue());
valueColumn.getColumn().setText(ruleData.isOutputExpValue() ? "Output Expression" : "Output Value");
expressionViewer.refresh();
}
});
// Left Panel: List of scripts
final Composite leftComposite = new Composite(mainComposite, SWT.NONE);
leftComposite.setLayout(new GridLayout(1, false));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.widthHint = 350;
leftComposite.setLayoutData(gd);
createLabel(leftComposite, "Expressions");
Composite toolBarComposite = new Composite(leftComposite, SWT.BORDER);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginLeft = 0;
gridLayout.marginRight = 0;
gridLayout.marginBottom = 0;
gridLayout.marginTop = 0;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
toolBarComposite.setLayout(gridLayout);
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
toolBarComposite.setLayoutData(gd);
ToolBarManager toolbarManager = new ToolBarManager(SWT.FLAT);
ToolBar toolBar = toolbarManager.createControl(toolBarComposite);
GridData grid = new GridData();
grid.horizontalAlignment = GridData.FILL;
grid.verticalAlignment = GridData.BEGINNING;
toolBar.setLayoutData(grid);
createActions();
toolbarManager.add(addAction);
toolbarManager.add(copyAction);
toolbarManager.add(removeAction);
toolbarManager.add(moveUpAction);
toolbarManager.add(moveDownAction);
toolbarManager.update(true);
expressionViewer = createExpressionsTableViewer(toolBarComposite);
expressionViewer.setInput(expressionList);
expressionViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
refreshActionBarOnSelection();
}
});
// Right panel: Input PVs for selected script
final Composite rightComposite = new Composite(mainComposite, SWT.NONE);
gridLayout = new GridLayout(1, false);
rightComposite.setLayout(gridLayout);
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
// Account for the StringTableEditor's minimum size
gd.minimumWidth = 250;
rightComposite.setLayoutData(gd);
this.createLabel(rightComposite, "Input PVs");
pvsEditor = new PVTupleTableEditor(rightComposite, ruleData.getPVList(), SWT.BORDER);
pvsEditor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
if (expressionList.size() > 0)
setExpressionViewerSelection(expressionList.get(0));
final Composite bottomComposite = new Composite(mainComposite, SWT.None);
bottomComposite.setLayout(new GridLayout(1, false));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.horizontalSpan = 2;
bottomComposite.setLayoutData(gd);
Button generateScriptButton = new Button(bottomComposite, SWT.PUSH);
generateScriptButton.setText("See Generated Script");
gd = new GridData(SWT.LEFT, SWT.TOP, false, false);
generateScriptButton.setLayoutData(gd);
final Text scriptText = new Text(bottomComposite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY);
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
scriptText.setLayoutData(gd);
generateScriptButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
scriptText.setText(ruleData.generateScript());
}
});
return parent_Composite;
}
Aggregations