Search in sources :

Example 1 with ScriptData

use of org.csstudio.opibuilder.script.ScriptData in project yamcs-studio by yamcs.

the class ScriptProperty method readValueFromXML.

@Override
public ScriptsInput readValueFromXML(Element propElement) {
    ScriptsInput result = new ScriptsInput();
    for (Object oe : propElement.getChildren(XML_ELEMENT_PATH)) {
        Element se = (Element) oe;
        ScriptData sd = new ScriptData();
        if (se.getAttributeValue(XML_ATTRIBUTE_PATHSTRING).equals(EMBEDDEDJS)) {
            sd.setEmbedded(true);
            sd.setScriptType(ScriptType.JAVASCRIPT);
            sd.setScriptText(se.getChildText(XML_ELEMENT_SCRIPT_TEXT));
            sd.setScriptName(se.getChildText(XML_ELEMENT_SCRIPT_NAME));
        } else if (se.getAttributeValue(XML_ATTRIBUTE_PATHSTRING).equals(EMBEDDEDPY)) {
            sd.setEmbedded(true);
            sd.setScriptType(ScriptType.PYTHON);
            sd.setScriptText(se.getChildText(XML_ELEMENT_SCRIPT_TEXT));
            sd.setScriptName(se.getChildText(XML_ELEMENT_SCRIPT_NAME));
        } else
            sd = new ScriptData(new Path(se.getAttributeValue(XML_ATTRIBUTE_PATHSTRING)));
        if (se.getAttributeValue(XML_ATTRIBUTE_CHECKCONNECT) != null)
            sd.setCheckConnectivity(Boolean.parseBoolean(se.getAttributeValue(XML_ATTRIBUTE_CHECKCONNECT)));
        if (se.getAttributeValue(XML_ATTRIBUTE_SKIP_FIRST_EXECUTION) != null)
            sd.setSkipPVsFirstConnection(Boolean.parseBoolean(se.getAttributeValue(XML_ATTRIBUTE_SKIP_FIRST_EXECUTION)));
        if (se.getAttributeValue(XML_ATTRIBUTE_STOP_EXECUTE_ON_ERROR) != null)
            sd.setStopExecuteOnError(Boolean.parseBoolean(se.getAttributeValue(XML_ATTRIBUTE_STOP_EXECUTE_ON_ERROR)));
        for (Object o : se.getChildren(XML_ELEMENT_PV)) {
            Element pve = (Element) o;
            boolean trig = true;
            if (pve.getAttribute(XML_ATTRIBUTE_TRIGGER) != null)
                trig = Boolean.parseBoolean(pve.getAttributeValue(XML_ATTRIBUTE_TRIGGER));
            sd.addPV(new PVTuple(pve.getText(), trig));
        }
        result.getScriptList().add(sd);
    }
    return result;
}
Also used : Path(org.eclipse.core.runtime.Path) Element(org.jdom.Element) ScriptsInput(org.csstudio.opibuilder.script.ScriptsInput) ScriptData(org.csstudio.opibuilder.script.ScriptData) PVTuple(org.csstudio.opibuilder.script.PVTuple)

Example 2 with ScriptData

use of org.csstudio.opibuilder.script.ScriptData in project yamcs-studio by yamcs.

the class AbstractBaseEditPart method deactivate.

@Override
public void deactivate() {
    if (isActive()) {
        doDeActivate();
        ActionsInput input = getWidgetModel().getActionsInput();
        for (AbstractWidgetAction a : input.getActionsList()) {
            a.dispose();
        }
        super.deactivate();
        // remove listener from all properties.
        for (String id : getWidgetModel().getAllPropertyIDs()) {
            getWidgetModel().getProperty(id).removeAllPropertyChangeListeners();
        }
        if (executionMode == ExecutionMode.RUN_MODE) {
            // remove script listeners before stopping PV.
            for (ScriptData scriptData : scriptDataList) {
                ScriptService.getInstance().unRegisterScript(scriptData);
            }
            if (hasStartedPVs) {
                // we should not attempt to stop those pvs; this can happen with linking container
                for (Object pv : pvMap.values().toArray()) {
                    ((IPV) pv).stop();
                }
            }
        }
        propertyListenerMap.clear();
    // propertyListenerMap = null;
    }
}
Also used : ActionsInput(org.csstudio.opibuilder.widgetActions.ActionsInput) ScriptData(org.csstudio.opibuilder.script.ScriptData) AbstractWidgetAction(org.csstudio.opibuilder.widgetActions.AbstractWidgetAction) IPV(org.csstudio.simplepv.IPV)

Example 3 with ScriptData

use of org.csstudio.opibuilder.script.ScriptData in project yamcs-studio by yamcs.

the class ScriptsInputDialog method refreshGUIOnSelection.

/**
 * Refreshes the enabled-state of the actions.
 */
private void refreshGUIOnSelection() {
    IStructuredSelection selection = (IStructuredSelection) scriptsViewer.getSelection();
    if (!selection.isEmpty() && selection.getFirstElement() instanceof ScriptData) {
        removeAction.setEnabled(true);
        moveUpAction.setEnabled(true);
        moveDownAction.setEnabled(true);
        convertToEmbedAction.setEnabled(!((ScriptData) selection.getFirstElement()).isEmbedded());
        editAction.setEnabled(true);
        pvsEditor.updateInput(((ScriptData) selection.getFirstElement()).getPVList());
        pvsEditor.setEnabled(true);
        checkConnectivityButton.setSelection(!((ScriptData) selection.getFirstElement()).isCheckConnectivity());
        checkConnectivityButton.setEnabled(true);
        skipFirstExecutionButton.setSelection(((ScriptData) selection.getFirstElement()).isSkipPVsFirstConnection());
        skipFirstExecutionButton.setEnabled(true);
        stopExecuteOnErrorButton.setSelection(((ScriptData) selection.getFirstElement()).isStopExecuteOnError());
        stopExecuteOnErrorButton.setEnabled(true);
    } else {
        removeAction.setEnabled(false);
        moveUpAction.setEnabled(false);
        moveDownAction.setEnabled(false);
        convertToEmbedAction.setEnabled(false);
        pvsEditor.setEnabled(false);
        editAction.setEnabled(false);
        checkConnectivityButton.setEnabled(false);
        skipFirstExecutionButton.setEnabled(false);
        stopExecuteOnErrorButton.setEnabled(false);
    }
}
Also used : ScriptData(org.csstudio.opibuilder.script.ScriptData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 4 with ScriptData

use of org.csstudio.opibuilder.script.ScriptData in project yamcs-studio by yamcs.

the class ScriptsInputDialog method okPressed.

@Override
protected void okPressed() {
    pvsEditor.forceFocus();
    for (ScriptData scriptData : scriptDataList) {
        boolean hasTrigger = false;
        for (PVTuple pvTuple : scriptData.getPVList()) {
            hasTrigger |= pvTuple.trigger;
        }
        if (!hasTrigger) {
            MessageDialog.openWarning(getShell(), "Warning", NLS.bind("At least one trigger PV must be selected for the script:\n{0}", scriptData.getPath().toString()));
            return;
        }
    }
    super.okPressed();
}
Also used : ScriptData(org.csstudio.opibuilder.script.ScriptData) PVTuple(org.csstudio.opibuilder.script.PVTuple)

Example 5 with ScriptData

use of org.csstudio.opibuilder.script.ScriptData in project yamcs-studio by yamcs.

the class ScriptsInputDialog method createActions.

/**
 * Creates the actions.
 */
private void createActions() {
    addAction = new Action("Add") {

        @Override
        public void run() {
            ScriptChoiceDialog scriptChoiceDialog = new ScriptChoiceDialog(getShell());
            if (scriptChoiceDialog.open() == Window.CANCEL)
                return;
            ScriptData scriptData = null;
            if (scriptChoiceDialog.isEmbedded()) {
                EmbeddedScriptEditDialog scriptEditDialog = new EmbeddedScriptEditDialog(getShell(), null);
                if (scriptEditDialog.open() == Window.OK)
                    scriptData = scriptEditDialog.getResult();
            } else {
                IPath path;
                RelativePathSelectionDialog rsd = new RelativePathSelectionDialog(Display.getCurrent().getActiveShell(), startPath, "Select a script file", new String[] { ScriptService.JS });
                // $NON-NLS-1$
                rsd.setSelectedResource(new Path("./"));
                if (rsd.open() == Window.OK) {
                    if (rsd.getSelectedResource() != null) {
                        path = rsd.getSelectedResource();
                        scriptData = new ScriptData(path);
                    }
                }
            }
            if (scriptData != null) {
                scriptDataList.add(scriptData);
                setScriptsViewerSelection(scriptData);
            }
        }
    };
    addAction.setToolTipText("Add a script");
    addAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
    "icons/add.gif"));
    editAction = new Action("Edit") {

        @Override
        public void run() {
            IPath path;
            IStructuredSelection selection = (IStructuredSelection) scriptsViewer.getSelection();
            if (!selection.isEmpty() && selection.getFirstElement() instanceof ScriptData) {
                ScriptData sd = (ScriptData) selection.getFirstElement();
                if (sd.isEmbedded()) {
                    EmbeddedScriptEditDialog scriptEditDialog = new EmbeddedScriptEditDialog(getShell(), sd);
                    if (scriptEditDialog.open() == Window.OK) {
                        ScriptData newSd = scriptEditDialog.getResult();
                        sd.setScriptName(newSd.getScriptName());
                        sd.setScriptType(newSd.getScriptType());
                        sd.setScriptText(newSd.getScriptText());
                        setScriptsViewerSelection(sd);
                    }
                } else {
                    RelativePathSelectionDialog rsd = new RelativePathSelectionDialog(getShell(), startPath, "Select a script file", new String[] { ScriptService.JS });
                    rsd.setSelectedResource(((ScriptData) selection.getFirstElement()).getPath());
                    if (rsd.open() == Window.OK) {
                        if (rsd.getSelectedResource() != null) {
                            path = rsd.getSelectedResource();
                            sd.setPath(path);
                            setScriptsViewerSelection(sd);
                        }
                    }
                }
            }
        }
    };
    editAction.setToolTipText("Edit/Change script path");
    editAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
    "icons/edit.gif"));
    editAction.setEnabled(false);
    removeAction = new Action() {

        @Override
        public void run() {
            IStructuredSelection selection = (IStructuredSelection) scriptsViewer.getSelection();
            if (!selection.isEmpty() && selection.getFirstElement() instanceof ScriptData) {
                scriptDataList.remove((ScriptData) selection.getFirstElement());
                setScriptsViewerSelection(null);
                this.setEnabled(false);
            }
        }
    };
    removeAction.setText("Remove Script");
    removeAction.setToolTipText("Remove the selected script 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) scriptsViewer.getSelection();
            if (!selection.isEmpty() && selection.getFirstElement() instanceof ScriptData) {
                ScriptData scriptData = (ScriptData) selection.getFirstElement();
                int i = scriptDataList.indexOf(scriptData);
                if (i > 0) {
                    scriptDataList.remove(scriptData);
                    scriptDataList.add(i - 1, scriptData);
                    setScriptsViewerSelection(scriptData);
                }
            }
        }
    };
    moveUpAction.setText("Move Script Up");
    moveUpAction.setToolTipText("Move selected script 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) scriptsViewer.getSelection();
            if (!selection.isEmpty() && selection.getFirstElement() instanceof ScriptData) {
                ScriptData scriptData = (ScriptData) selection.getFirstElement();
                int i = scriptDataList.indexOf(scriptData);
                if (i < scriptDataList.size() - 1) {
                    scriptDataList.remove(scriptData);
                    scriptDataList.add(i + 1, scriptData);
                    setScriptsViewerSelection(scriptData);
                }
            }
        }
    };
    moveDownAction.setText("Move Script Down");
    moveDownAction.setToolTipText("Move selected script down");
    moveDownAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
    "icons/search_next.gif"));
    moveDownAction.setEnabled(false);
    convertToEmbedAction = new Action() {

        @Override
        public void run() {
            IStructuredSelection selection = (IStructuredSelection) scriptsViewer.getSelection();
            if (!selection.isEmpty() && selection.getFirstElement() instanceof ScriptData) {
                ScriptData sd = (ScriptData) selection.getFirstElement();
                if (!sd.isEmbedded()) {
                    IPath absoluteScriptPath = sd.getPath();
                    if (!absoluteScriptPath.isAbsolute()) {
                        absoluteScriptPath = ResourceUtil.buildAbsolutePath(widgetModel, absoluteScriptPath);
                        if (!ResourceUtil.isExsitingFile(absoluteScriptPath, true)) {
                            // search from OPI search path
                            absoluteScriptPath = ResourceUtil.getFileOnSearchPath(sd.getPath(), true);
                        }
                    }
                    try {
                        String text = FileUtil.readTextFile(absoluteScriptPath.toString());
                        String ext = absoluteScriptPath.getFileExtension().trim().toLowerCase();
                        if (ext.equals(ScriptService.JS))
                            sd.setScriptType(ScriptType.JAVASCRIPT);
                        else if (ext.equals(ScriptService.PY))
                            sd.setScriptType(ScriptType.PYTHON);
                        else {
                            MessageDialog.openError(getShell(), "Failed", "The script type is not recognized.");
                            return;
                        }
                        sd.setEmbedded(true);
                        sd.setScriptText(text);
                        sd.setScriptName(absoluteScriptPath.removeFileExtension().lastSegment());
                        setScriptsViewerSelection(sd);
                    } catch (Exception e) {
                        MessageDialog.openError(getShell(), "Failed", "Failed to read script file");
                    }
                }
            }
        }
    };
    convertToEmbedAction.setText("Convert to Embedded Script");
    convertToEmbedAction.setToolTipText("Convert to Embedded Script");
    convertToEmbedAction.setImageDescriptor(CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(OPIBuilderPlugin.PLUGIN_ID, // $NON-NLS-1$
    "icons/convertToEmbedded.png"));
    convertToEmbedAction.setEnabled(false);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Action(org.eclipse.jface.action.Action) IPath(org.eclipse.core.runtime.IPath) ScriptData(org.csstudio.opibuilder.script.ScriptData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Aggregations

ScriptData (org.csstudio.opibuilder.script.ScriptData)9 PVTuple (org.csstudio.opibuilder.script.PVTuple)6 ScriptsInput (org.csstudio.opibuilder.script.ScriptsInput)5 RuleData (org.csstudio.opibuilder.script.RuleData)2 IPV (org.csstudio.simplepv.IPV)2 Path (org.eclipse.core.runtime.Path)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 Element (org.jdom.Element)2 AbstractWidgetProperty (org.csstudio.opibuilder.properties.AbstractWidgetProperty)1 WidgetPropertyChangeListener (org.csstudio.opibuilder.properties.WidgetPropertyChangeListener)1 RulesInput (org.csstudio.opibuilder.script.RulesInput)1 AbstractWidgetAction (org.csstudio.opibuilder.widgetActions.AbstractWidgetAction)1 ActionsInput (org.csstudio.opibuilder.widgetActions.ActionsInput)1 IPath (org.eclipse.core.runtime.IPath)1 Point (org.eclipse.draw2d.geometry.Point)1 Action (org.eclipse.jface.action.Action)1 CDATA (org.jdom.CDATA)1