Skip to content
Snippets Groups Projects
Commit 35b9bf08 authored by buczekp's avatar buczekp
Browse files

[LMS-1949] make script client side validation specific to script type

SVN: 19443
parent e7d29381
No related branches found
No related tags found
No related merge requests found
...@@ -16,60 +16,26 @@ ...@@ -16,60 +16,26 @@
package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.field; package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.field;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.Validator;
import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.IMessageProvider; import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.IMessageProvider;
import ch.systemsx.cisd.openbis.plugin.query.client.web.client.application.Dict; import ch.systemsx.cisd.openbis.plugin.query.client.web.client.application.Dict;
/** /**
* A text area to specify a script. * A text area to specify a script.
* <p> * <p>
* If the script is multiline it should contain definition of <code>calculate()</code> function. * It is possible to type tabs in the field area.
* *
* @author Piotr Buczek * @author Piotr Buczek
*/ */
public class ScriptField extends MultilineVarcharField public class ScriptField extends MultilineVarcharField
{ {
private final static String CALCULATE_DEFINITION = "def calculate():";
private final static String CALCULATE_DEFINITION_NOT_FOUND_MSG =
"Multiline script should contain definition of 'calculate()' function.";
private final static String BLANK_TEXT_MSG = "Script text required"; private final static String BLANK_TEXT_MSG = "Script text required";
public ScriptField(IMessageProvider messageProvider) public ScriptField(IMessageProvider messageProvider)
{ {
super(messageProvider.getMessage(Dict.SCRIPT), true, 20); super(messageProvider.getMessage(Dict.SCRIPT), true, 20);
setValidator(new ScriptValidator());
getMessages().setBlankText(BLANK_TEXT_MSG); getMessages().setBlankText(BLANK_TEXT_MSG);
treatTabKeyAsInput(); treatTabKeyAsInput();
} }
/** {@link Validator} for script. */
protected class ScriptValidator implements Validator
{
private static final String NEWLINE = "\n";
public String validate(Field<?> field, final String fieldValue)
{
if (fieldValue.contains(NEWLINE))
{
final String[] lines = fieldValue.split(NEWLINE);
for (String line : lines)
{
if (line.startsWith(CALCULATE_DEFINITION))
{
// validated value is valid
return null;
}
}
return CALCULATE_DEFINITION_NOT_FOUND_MSG;
}
// validated value is valid
return null;
}
}
} }
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.script; package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.script;
import java.util.HashMap;
import java.util.Map;
import com.extjs.gxt.ui.client.event.BaseEvent; import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent; import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events; import com.extjs.gxt.ui.client.event.Events;
...@@ -23,9 +26,11 @@ import com.extjs.gxt.ui.client.event.Listener; ...@@ -23,9 +26,11 @@ import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent; import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener; import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener; import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel; import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboValue; import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;
import com.extjs.gxt.ui.client.widget.form.TextField; import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem; import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar; import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Element;
...@@ -152,6 +157,7 @@ abstract public class AbstractScriptEditRegisterForm extends AbstractRegistratio ...@@ -152,6 +157,7 @@ abstract public class AbstractScriptEditRegisterForm extends AbstractRegistratio
protected void onScriptTypeChanged(ScriptType scriptType) protected void onScriptTypeChanged(ScriptType scriptType)
{ {
rightPanel.setVisible(scriptType == ScriptType.DYNAMIC_PROPERTY); rightPanel.setVisible(scriptType == ScriptType.DYNAMIC_PROPERTY);
scriptField.setValidator(validatorsByScriptType.get(scriptType));
} }
private IValidable asValidable(final FormPanel panel) private IValidable asValidable(final FormPanel panel)
...@@ -237,4 +243,61 @@ abstract public class AbstractScriptEditRegisterForm extends AbstractRegistratio ...@@ -237,4 +243,61 @@ abstract public class AbstractScriptEditRegisterForm extends AbstractRegistratio
layout(); layout();
} }
//
// script validators
//
private static Map<ScriptType, Validator> validatorsByScriptType =
new HashMap<ScriptType, Validator>();
static
{
validatorsByScriptType.put(ScriptType.DYNAMIC_PROPERTY,
new DynamicPropertyScriptValidator());
validatorsByScriptType.put(ScriptType.MANAGED_PROPERTY,
new ManagedPropertyScriptValidator());
}
private final static String NEWLINE = "\n";
/** {@link Validator} for script of type {@link ScriptType#DYNAMIC_PROPERTY}. */
private static class DynamicPropertyScriptValidator implements Validator
{
private final static String CALCULATE_DEFINITION = "def calculate():";
private final static String CALCULATE_DEFINITION_NOT_FOUND_MSG =
"Multiline script should contain definition of 'calculate()' function.";
public String validate(Field<?> field, final String fieldValue)
{
if (fieldValue.contains(NEWLINE))
{
final String[] lines = fieldValue.split(NEWLINE);
for (String line : lines)
{
if (line.startsWith(CALCULATE_DEFINITION))
{
// validated value is valid
return null;
}
}
return CALCULATE_DEFINITION_NOT_FOUND_MSG;
}
// validated value is valid
return null;
}
}
/** {@link Validator} for script of type {@link ScriptType#MANAGED_PROPERTY}. */
private static class ManagedPropertyScriptValidator implements Validator
{
public String validate(Field<?> field, String value)
{
// TODO Auto-generated method stub
return null;
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment