Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/symboltable/SQLSymbolTable.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/symboltable/SQLSymbolTable.java	(revision 44079)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/symboltable/SQLSymbolTable.java	(revision 44080)
@@ -3,6 +3,8 @@
 import org.gvsig.expressionevaluator.Function;
 import org.gvsig.expressionevaluator.impl.function.date.DateFunction;
 import org.gvsig.expressionevaluator.impl.function.date.NowFunction;
+import org.gvsig.expressionevaluator.impl.function.date.RelativeInstantFunction;
+import org.gvsig.expressionevaluator.impl.function.date.RelativeIntervalFunction;
 import org.gvsig.expressionevaluator.impl.function.date.TimeFunction;
 import org.gvsig.expressionevaluator.impl.function.date.TimestampFunction;
 import org.gvsig.expressionevaluator.impl.function.numeric.ACosFunction;
@@ -167,6 +169,9 @@
 
         this.addFunction(new IIFFunction());
         this.addFunction(new IFNULLFunction());
+
+        this.addFunction(new RelativeInstantFunction());
+        this.addFunction(new RelativeIntervalFunction());
     }
 
     private void addOperator(Function operator) {
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/date/RelativeInstantFunction.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/date/RelativeInstantFunction.java	(revision 0)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/date/RelativeInstantFunction.java	(revision 44080)
@@ -0,0 +1,68 @@
+package org.gvsig.expressionevaluator.impl.function.date;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Objects;
+import org.apache.commons.lang3.Range;
+import org.gvsig.expressionevaluator.Interpreter;
+import org.gvsig.expressionevaluator.spi.AbstractFunction;
+import org.gvsig.timesupport.DataTypes;
+import org.gvsig.timesupport.RelativeInstant;
+import org.gvsig.timesupport.TimeSupportLocator;
+import org.gvsig.tools.ToolsLocator;
+import org.gvsig.tools.dataTypes.CoercionException;
+import org.gvsig.tools.dataTypes.DataTypesManager;
+
+public class RelativeInstantFunction extends AbstractFunction {
+
+    private final DataTypesManager.Coercion toInstant;
+
+    public RelativeInstantFunction() {
+        super("Date", "RelativeInstant",Range.is(1),
+            "Returns a relative instant from the arguments",
+            "RelativeInstant({{date}}, format)",
+            new String[]{
+                "date - a string with a date",
+                "format - Optional. Format to use to parse the date"
+            },
+            "RelativeInstant",
+            false
+        );
+        this.toInstant = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.INSTANT);
+    }
+
+    @Override
+    public boolean allowConstantFolding() {
+        return true;
+    }
+    
+    @Override
+    public Object call(Interpreter interpreter, Object[] args) {
+        if( args.length==1 ) {
+            Object arg0 = getObject(args, 0);
+            try {
+                Object instant = this.toInstant.coerce(arg0);
+                return instant;
+            } catch (CoercionException ex) {
+                throw new RuntimeException("Can't convert value '"+Objects.toString(arg0, "")+"' to instant.", ex);
+            }
+        }
+        String format = getStr(args, 1);
+        String date = getStr(args, 0);
+        SimpleDateFormat df = new SimpleDateFormat();
+        if( format != null ) {
+            df.applyPattern(format);
+        }
+        try {
+            Date x = df.parse(date);
+            RelativeInstant instant = TimeSupportLocator.getManager().createRelativeInstant(x);
+            return instant;
+        } catch (ParseException ex) {
+            throw new RuntimeException("Can't parse date value '"+date+"' with format '"+format==null?"":format+"'", ex);
+        }
+        
+    }
+    
+
+}
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/date/RelativeIntervalFunction.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/date/RelativeIntervalFunction.java	(revision 0)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/date/RelativeIntervalFunction.java	(revision 44080)
@@ -0,0 +1,61 @@
+package org.gvsig.expressionevaluator.impl.function.date;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Objects;
+import org.apache.commons.lang3.Range;
+import org.gvsig.expressionevaluator.Interpreter;
+import org.gvsig.expressionevaluator.spi.AbstractFunction;
+import org.gvsig.timesupport.DataTypes;
+import org.gvsig.timesupport.Instant;
+import org.gvsig.timesupport.RelativeInstant;
+import org.gvsig.timesupport.RelativeInterval;
+import org.gvsig.timesupport.TimeSupportLocator;
+import org.gvsig.tools.ToolsLocator;
+import org.gvsig.tools.dataTypes.CoercionException;
+import org.gvsig.tools.dataTypes.DataTypesManager;
+
+public class RelativeIntervalFunction extends AbstractFunction {
+
+    private final DataTypesManager.Coercion toInstant;
+
+    public RelativeIntervalFunction() {
+        super("Date", "RelativeInterval",Range.is(2),
+            "Returns a relative interval from the arguments",
+            "RelativeInterval({{date1}}, data2)",
+            new String[]{
+                "date1 - An object that represents a moment of time.",
+                "date2 - An object that represents a moment of time."
+            },
+            "RelativeInterval",
+            false
+        );
+        this.toInstant = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.INSTANT);
+    }
+
+    @Override
+    public boolean allowConstantFolding() {
+        return true;
+    }
+    
+    @Override
+    public Object call(Interpreter interpreter, Object[] args) {
+            Object arg0 = getObject(args, 0);
+            Object arg1 = getObject(args, 1);
+            try {
+                RelativeInstant instant0 = (RelativeInstant) this.toInstant.coerce(arg0);
+                RelativeInstant instant1 = (RelativeInstant) this.toInstant.coerce(arg1);
+                RelativeInterval interval = TimeSupportLocator.getManager().createRelativeInterval(
+                    instant0,
+                    instant1
+                );
+                return interval;
+            } catch (CoercionException ex) {
+                throw new RuntimeException("Can't create an interval from '"+Objects.toString(arg0, "")+":"+Objects.toString(arg1, "")+"'.", ex);
+            }
+        }        
+    }
+    
+
+
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/pom.xml
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/pom.xml	(revision 44079)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/pom.xml	(revision 44080)
@@ -44,5 +44,10 @@
             <artifactId>${org.gvsig.proj.lib.impl}</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.gvsig</groupId>
+            <artifactId>org.gvsig.timesupport.lib.api</artifactId>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionEvaluatorSwingManager.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionEvaluatorSwingManager.java	(revision 44079)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionEvaluatorSwingManager.java	(revision 44080)
@@ -177,7 +177,7 @@
 
     @Override
     public CalculatorController createCalculatorController(JTextComponent text, int dataType) {
-        DefaultCalculatorController controller = new DefaultCalculatorController(text,dataType);
+        DefaultCalculatorController controller = new DefaultCalculatorController(this,text,dataType);
         return controller;
     }
 
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultCalculatorController.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultCalculatorController.java	(revision 44079)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultCalculatorController.java	(revision 44080)
@@ -1,38 +1,118 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
 package org.gvsig.expressionevaluator.swing.impl;
 
+import java.awt.Color;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
 import org.gvsig.expressionevaluator.swing.CalculatorController;
 import java.util.Objects;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JPopupMenu;
 import javax.swing.text.JTextComponent;
+import org.apache.commons.lang3.StringUtils;
+import org.gvsig.expressionevaluator.Expression;
 import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
 import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
 import org.gvsig.expressionevaluator.MutableSymbolTable;
 import org.gvsig.expressionevaluator.SymbolTable;
+import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
 import org.gvsig.tools.dataTypes.DataTypes;
 import org.gvsig.tools.ToolsLocator;
 import org.gvsig.tools.dataTypes.CoercionException;
 import org.gvsig.tools.dataTypes.DataTypesManager;
+import org.gvsig.tools.i18n.I18nManager;
+import org.gvsig.tools.swing.api.ToolsSwingLocator;
+import org.gvsig.tools.swing.api.ToolsSwingManager;
+import org.gvsig.tools.swing.api.windowmanager.Dialog;
+import org.gvsig.tools.swing.api.windowmanager.WindowManager;
+import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
 
 /**
  *
  * @author jjdelcerro
  */
+@SuppressWarnings("UseSpecificCatch")
 class DefaultCalculatorController<T> implements CalculatorController<T> {
     
+    private static final Color COLOR_ERROR = new Color(255,225,232);
+    
     private final JTextComponent text;
     private final int dataType;
     private final MutableSymbolTable defaultSymbolTable;
+    private final Color backgroundColor;
+    private final ExpressionEvaluatorSwingManager manager;
+    private DefaultExpressionBuilderConfig config;
+    private Expression expression;
 
-    public DefaultCalculatorController(JTextComponent text, int dataType) {
+    public DefaultCalculatorController(ExpressionEvaluatorSwingManager manager, final JTextComponent text, int dataType) {
+        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
+        I18nManager i18n = ToolsLocator.getI18nManager();
+
+        this.manager = manager;
+        this.config = null;
+        this.expression = null;
+
         this.text = text;
         this.dataType = dataType;
+        this.backgroundColor = this.text.getBackground();
         this.defaultSymbolTable = ExpressionEvaluatorLocator.getManager().createSymbolTable();
+        this.text.addFocusListener(new FocusListener() {
+            @Override
+            public void focusLost(FocusEvent e) {
+                doCalculate();
+            }
+
+            @Override
+            public void focusGained(FocusEvent e) {
+                doCalculate();
+            }
+        });
+        swingManager.addClearButton(this.text, new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                set(null);
+            }
+        });
+        swingManager.setDefaultPopupMenu(this.text);
+        JPopupMenu popupMenu = this.text.getComponentPopupMenu();
+        Action showExpressionBuilderAction = new AbstractAction(i18n.getTranslation("_Show_expression_builder")) {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                doShowExpressionBuilder();
+            }
+        };  
+        popupMenu.addSeparator();
+        popupMenu.add(showExpressionBuilderAction);
     }
 
+    private void doShowExpressionBuilder() {
+        if( this.config == null ) {
+            this.config = new DefaultExpressionBuilderConfig(this.manager);
+            this.config.addSymbolTable(this.defaultSymbolTable);
+        }
+        if( this.expression == null ) {
+            this.expression = ExpressionEvaluatorLocator.getManager().createExpression();
+        }
+        expression.setPhrase(this.text.getText());
+
+        DefaultJExpressionBuilder panel = new DefaultJExpressionBuilder(this.manager, this.config);
+        panel.setExpression(this.expression);
+        
+        WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
+        Dialog dialog = windowManager.createDialog(
+                panel.asJComponent(),
+                "Expression builder",
+                null, 
+                WindowManager_v2.BUTTONS_OK_CANCEL
+        );
+        dialog.show(WindowManager.MODE.DIALOG);
+        if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
+            this.text.setText(panel.getExpression().getPhrase());
+        }        
+    }
+    
     @Override
     public void addSymbolTable(SymbolTable symbolTable) {
         this.defaultSymbolTable.addSymbolTable(symbolTable);
@@ -43,21 +123,32 @@
         return this.defaultSymbolTable;
     }
 
+    private void doCalculate() {
+        if( !this.text.isEditable() ) {
+            return;
+        }
+        String source = this.text.getText();
+        try {
+            if( !StringUtils.isBlank(source) ) {
+                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
+                Object value = manager.evaluate(this.defaultSymbolTable, source);
+                this.text.setText(Objects.toString(value, ""));
+            }
+            this.text.setBackground(this.backgroundColor);
+        } catch (Exception ex) {
+            this.text.setBackground(COLOR_ERROR);
+        }
+    }
+    
     @Override
     public T get() {
-        String source = this.text.getText();
+        doCalculate();
+        Object value = this.text.getText();
         try {
-            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
-            Object value = manager.evaluate(this.defaultSymbolTable, source);
-            if (this.dataType != DataTypes.OBJECT) {
-                DataTypesManager dataManager = ToolsLocator.getDataTypesManager();
-                value = dataManager.coerce(this.dataType, value);
-            }
+            DataTypesManager dataManager = ToolsLocator.getDataTypesManager();
+            value = dataManager.coerce(this.dataType, value);
             return (T) value;
         } catch (CoercionException ex) {
-            if (this.dataType != DataTypes.STRING) {
-                return (T) source;
-            }
             return null;
         }
     }
@@ -65,6 +156,7 @@
     @Override
     public void set(T value) {
         this.text.setText(Objects.toString(value, ""));
+        this.text.setBackground(this.backgroundColor);
     }
 
     @Override
@@ -77,6 +169,7 @@
             }
         }
         this.text.setText(Objects.toString(value, ""));
+        this.text.setBackground(this.backgroundColor);
     }
 
     @Override
@@ -89,4 +182,14 @@
         return this.text.isEnabled();
     }
     
+    @Override
+    public void setEditable(boolean editable) {
+        this.text.setEditable(editable);
+    }
+
+    @Override
+    public boolean isEditable() {
+        return this.text.isEditable();
+    }
+    
 }
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionPickerController.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionPickerController.java	(revision 44079)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionPickerController.java	(revision 44080)
@@ -5,8 +5,11 @@
 import java.net.URL;
 import java.util.Collection;
 import java.util.List;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
+import javax.swing.JPopupMenu;
 import javax.swing.text.JTextComponent;
 import org.apache.commons.lang3.StringUtils;
 import org.gvsig.expressionevaluator.DataTypes;
@@ -24,6 +27,8 @@
 import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig;
 import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
 import org.gvsig.expressionevaluator.swing.ExpressionPickerController;
+import org.gvsig.tools.i18n.I18nManager;
+import org.gvsig.tools.swing.api.ToolsSwingManager;
 import org.gvsig.tools.swing.api.windowmanager.Dialog;
 import org.gvsig.tools.swing.api.windowmanager.WindowManager;
 import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
@@ -54,6 +59,9 @@
     private final ExpressionEvaluatorSwingManager manager;
     
     public DefaultExpressionPickerController(ExpressionEvaluatorSwingManager manager, JTextComponent text, JButton button) {
+        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
+        I18nManager i18n = ToolsLocator.getI18nManager();
+
         this.manager = manager;
         this.config = new DefaultExpressionBuilderConfig(manager);
         this.text = text;
@@ -67,6 +75,17 @@
             }
         });
 
+        swingManager.addClearButton(this.text);
+        swingManager.setDefaultPopupMenu(this.text);
+        JPopupMenu popupMenu = this.text.getComponentPopupMenu();
+        Action showExpressionBuilderAction = new AbstractAction(i18n.getTranslation("_Show_expression_builder")) {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                doSelectExpression();
+            }
+        };  
+        popupMenu.addSeparator();
+        popupMenu.add(showExpressionBuilderAction);
     }
     
     private void doSelectExpression() {
@@ -96,10 +115,11 @@
 
     @Override
     public void set(Expression value) {
-        this.value = value; 
         if( value==null ) {
             this.text.setText("");
+            this.value.setPhrase("");
         } else {
+            this.value = value;
             this.text.setText(this.value.getPhrase());
         }
     }
Index: trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.api/src/main/java/org/gvsig/expressionevaluator/swing/CalculatorController.java
===================================================================
--- trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.api/src/main/java/org/gvsig/expressionevaluator/swing/CalculatorController.java	(revision 44079)
+++ trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.api/src/main/java/org/gvsig/expressionevaluator/swing/CalculatorController.java	(revision 44080)
@@ -22,5 +22,9 @@
     public boolean isEnabled();
 
     public void setEnabled(boolean enabled);
+
+    public void setEditable(boolean editable);
     
+    public boolean isEditable();
+    
 }
