Package org.erlide.tracing.core.mvc.view

Source Code of org.erlide.tracing.core.mvc.view.MatchSpecCellEditor$MatchSpecValidator

package org.erlide.tracing.core.mvc.view;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.erlide.engine.util.OtpRpcFactory;
import org.erlide.runtime.api.IOtpRpc;
import org.erlide.tracing.core.Constants;
import org.erlide.tracing.core.mvc.model.MatchSpec;
import org.erlide.util.ErlLogger;


* Cell editor for specifying match specifications.
* @author Piotr Dorobisz
public class MatchSpecCellEditor extends DialogCellEditor {

    public MatchSpecCellEditor(final Composite parent) {

    protected Object openDialogBox(final Control cellEditorWindow) {
        final MatchSpecInputDialog dialog = new MatchSpecInputDialog(
                cellEditorWindow.getShell(), "Create match spec", "Literal fun:",
                ((MatchSpec) getValue()).getFunctionString(), new MatchSpecValidator());;
        return getValue();

    private class MatchSpecInputDialog extends InputDialog {

        public MatchSpecInputDialog(final Shell parentShell, final String dialogTitle,
                final String dialogMessage, final String initialValue,
                final IInputValidator validator) {
            super(parentShell, dialogTitle, dialogMessage, initialValue, validator);

        protected int getInputTextStyle() {
            return SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL;

    private class MatchSpecValidator implements IInputValidator {

        public IOtpRpc getBackend() {
            return OtpRpcFactory.getOtpRpc();

        public String isValid(final String newText) {
            if (newText == null || newText.length() == 0) {
                // no match spec
                ((MatchSpec) getValue()).setFunctionString("");
                ((MatchSpec) getValue()).setMsObject(null);
                return null;
            } else if ("x".equals(newText)) {
                // shortcut for matching exceptions and return values
                ((MatchSpec) getValue()).setFunctionString("x");
                ((MatchSpec) getValue()).setMsObject(new OtpErlangAtom("x"));
                return null;
            try {

                final IOtpRpc backend = getBackend();
                final OtpErlangTuple tuple = (OtpErlangTuple)
                        Constants.ERLANG_HELPER_MODULE, Constants.FUN_STR2MS, "s",
                        new OtpErlangString(newText));
                if (((OtpErlangAtom) tuple.elementAt(0)).atomValue().equals("ok")) {
                    // correct match spec - update
                    ((MatchSpec) getValue()).setFunctionString(newText);
                    ((MatchSpec) getValue()).setMsObject(tuple.elementAt(1));
                    return null;
                // incorrect match spec
                final OtpErlangAtom errorType = (OtpErlangAtom) tuple.elementAt(1);
                if (errorType.atomValue().equals("standard_info")) {
                    final OtpErlangTuple errorTuple = (OtpErlangTuple) tuple.elementAt(2);
                    final StringBuilder builder = new StringBuilder("Line ");
                    builder.append(errorTuple.elementAt(0)).append(": ");
                    final OtpErlangList errorList = (OtpErlangList) errorTuple
                    builder.append(((OtpErlangString) errorList.elementAt(0))
                    if (errorList.elementAt(1) instanceof OtpErlangString) {
                        builder.append(((OtpErlangString) errorList.elementAt(1))
                    return builder.toString();
                } else if (errorType.atomValue().equals("not_fun")) {
                    return "Given expression is not a function";
                } else if (errorType.atomValue().equals("unbound_var")) {
                    final StringBuilder builder = new StringBuilder("Unbound variable: ");
                    return builder.toString();
                } else {
                    return tuple.elementAt(2).toString();
            } catch (final Exception e) {
                return "Backend problem: " + e.getMessage();

Related Classes of org.erlide.tracing.core.mvc.view.MatchSpecCellEditor$MatchSpecValidator

Copyright © 2018 All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact