Search in sources :

Example 1 with Expression

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;
}
Also used : RuleData(org.csstudio.opibuilder.script.RuleData) RulesInput(org.csstudio.opibuilder.script.RulesInput) Expression(org.csstudio.opibuilder.script.Expression) Element(org.jdom.Element) PVTuple(org.csstudio.opibuilder.script.PVTuple)

Example 2 with Expression

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);
}
Also used : Action(org.eclipse.jface.action.Action) Expression(org.csstudio.opibuilder.script.Expression) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 3 with Expression

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);
}
Also used : Expression(org.csstudio.opibuilder.script.Expression) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 4 with Expression

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;
}
Also used : Expression(org.csstudio.opibuilder.script.Expression) CellEditor(org.eclipse.jface.viewers.CellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) EditingSupport(org.eclipse.jface.viewers.EditingSupport) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) TableViewer(org.eclipse.jface.viewers.TableViewer) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn)

Example 5 with Expression

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;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ModifyListener(org.eclipse.swt.events.ModifyListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) ToolBarManager(org.eclipse.jface.action.ToolBarManager) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Expression(org.csstudio.opibuilder.script.Expression) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ToolBar(org.eclipse.swt.widgets.ToolBar)

Aggregations

Expression (org.csstudio.opibuilder.script.Expression)6 PVTuple (org.csstudio.opibuilder.script.PVTuple)2 RuleData (org.csstudio.opibuilder.script.RuleData)2 RulesInput (org.csstudio.opibuilder.script.RulesInput)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 GridData (org.eclipse.swt.layout.GridData)2 Element (org.jdom.Element)2 Action (org.eclipse.jface.action.Action)1 ToolBarManager (org.eclipse.jface.action.ToolBarManager)1 ArrayContentProvider (org.eclipse.jface.viewers.ArrayContentProvider)1 CellEditor (org.eclipse.jface.viewers.CellEditor)1 EditingSupport (org.eclipse.jface.viewers.EditingSupport)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 TableViewerColumn (org.eclipse.jface.viewers.TableViewerColumn)1 TextCellEditor (org.eclipse.jface.viewers.TextCellEditor)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1