Index: JDynFormFieldForeingKey.java
===================================================================
--- JDynFormFieldForeingKey.java	(revision 44261)
+++ JDynFormFieldForeingKey.java	(revision 44262)
@@ -41,21 +41,22 @@
 import javax.swing.ListModel;
 import javax.swing.event.ListDataListener;
 import javax.swing.text.JTextComponent;
-import org.gvsig.expressionevaluator.Expression;
-import org.gvsig.expressionevaluator.ExpressionBuilder;
-import org.gvsig.expressionevaluator.ExpressionUtils;
 import org.gvsig.featureform.swing.JFeaturesForm;
 import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
-import org.gvsig.fmap.dal.DALLocator;
-import org.gvsig.fmap.dal.DataManager;
-import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
+import org.gvsig.fmap.dal.StoresRepository;
 import org.gvsig.fmap.dal.feature.Feature;
+import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
 import org.gvsig.fmap.dal.feature.FeatureQuery;
 import org.gvsig.fmap.dal.feature.FeatureStore;
+import org.gvsig.fmap.dal.feature.FeatureType;
+import org.gvsig.fmap.dal.feature.ForeingKey;
+import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey;
 import org.gvsig.fmap.dal.swing.DALSwingLocator;
 import org.gvsig.fmap.dal.swing.DataSwingManager;
+import org.gvsig.tools.ToolsLocator;
+import org.gvsig.tools.dispose.DisposeUtils;
 import org.gvsig.tools.dynform.DynFormFieldDefinition;
-import org.gvsig.tools.dynform.JDynForm.DynFormContext;
+import org.gvsig.tools.dynform.JDynForm;
 
 import org.gvsig.tools.dynform.JDynFormField;
 import org.gvsig.tools.dynform.spi.DynFormSPIManager;
@@ -71,10 +72,6 @@
 @SuppressWarnings("UseSpecificCatch")
 public class JDynFormFieldForeingKey extends AbstractJDynFormField implements JDynFormField {
 
-    private static final String DAL_FOREING_CODE = "DAL.foreingCode";
-    private static final String DAL_FOREING_TABLE = "DAL.foreingTable";
-    private static final String DAL_FOREING_LABEL = "DAL.foreingLabel";
-
     private Object assignedValue = null;
     private Object value = null;
     private JTextComponent txtDescription = null;
@@ -212,18 +209,32 @@
         return button;
     }
 
+    private ForeingKey getForeingKey() {
+        JDynForm.DynFormContext context = this.getForm().getContext();
+        if( !(context instanceof FeaturesFormContext) ) {
+            return null;
+        }
+        FeatureType featureType = ((FeaturesFormContext)context).getFeatureType();
+        if( featureType==null ) {
+            return null;
+        }
+        FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName());
+        if( attribute == null ) {
+            return null;
+        }
+        ForeingKey foreingKey = attribute.getForeingKey();
+        return foreingKey;
+    }
+    
     private void doLink() {
-        final String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null);
-        final String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null);
+        final ForeingKey foreingKey = this.getForeingKey();
+        if( foreingKey==null ) {
+            return;
+        }
+        final ContextForeingKey context = foreingKey.createContext();
         try {
-            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
-
-            FeatureStore store = null;
-            DynFormContext context = this.getForm().getContext();
-            if( context instanceof FeaturesFormContext ) {
-                store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName);
-            }
-            final List<Feature> features = store.getFeatures();
+            final List<Feature> features = foreingKey.getFeatures(context);
+            
             ListModel<String> model = new ListModel<String>() {
                 @Override
                 public int getSize() {
@@ -233,7 +244,8 @@
                 @Override
                 public String getElementAt(int index) {
                     Feature feature = features.get(index);
-                    return getDescription(feature);
+                    ForeingKey foreingKey = getForeingKey();
+                    return foreingKey.getLabel(context, feature);
                 }
 
                 @Override
@@ -244,6 +256,8 @@
                 public void removeListDataListener(ListDataListener l) {
                 }
             };
+            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
+
             final JList<String> jlist = new JList<>();
             jlist.setModel(model);
             jlist.setPreferredSize(new Dimension(350, 200));
@@ -263,7 +277,7 @@
                         }
                         Feature feature = features.get(n);
                         if( feature!=null ) {
-                            setValue(feature.get(foreingCodeName));
+                            setValue(foreingKey.getCode(context, feature));
                         }
                     }
                 }
@@ -272,6 +286,8 @@
 
         } catch (Exception ex) {
             LOGGER.warn("Can't show selector", ex);
+        } finally {
+            DisposeUtils.disposeQuietly(context);
         }
     }
 
@@ -283,31 +299,18 @@
         if (this.value == null) {
             return;
         }
-        String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null);
-        String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null);
+        final ForeingKey foreingKey = this.getForeingKey();
+        if( foreingKey==null ) {
+            return;
+        }
+        final ContextForeingKey context = foreingKey.createContext();
         try {
             this.btnEdit.setEnabled(false);
-            DataManager dataManager = DALLocator.getDataManager();
             DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
-
-            FeatureStore store = null;
-            DynFormContext context = this.getForm().getContext();
-            if( context instanceof FeaturesFormContext ) {
-                store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName);
-            }
-            if( store == null ) {
-                this.problemIndicator().set("Unable to locate the related table '" + foreingTableName + "'.");
-                return;
-            }
-            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
-            FeatureQuery query = store.createFeatureQuery();
-            query.setFilter(builder.eq(
-                    builder.variable(foreingCodeName),
-                    builder.constant(value)
-                ).toString()
+            FeatureQuery query = foreingKey.getQuery(context, value);
+            JFeaturesForm form = dataSwingManager.createJFeaturesForm(
+                    foreingKey.getFeatureStore(context)
             );
-            query.retrievesAllAttributes();
-            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
             form.setQuery(query);
             form.showForm(WindowManager.MODE.WINDOW);
 
@@ -315,6 +318,7 @@
             LOGGER.warn("Can't show linked form", ex);
         } finally {
             this.btnEdit.setEnabled(true);
+            DisposeUtils.disposeQuietly(context);
         }
     }
 
@@ -322,52 +326,19 @@
         if (this.value == null) {
             return null;
         }
-        String foreingLabel = getTagValueAsString(DAL_FOREING_LABEL, null);
-        String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null);
-        String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null);
         try {
-            FeatureStore store = null;
-            DynFormContext context = this.getForm().getContext();
-            if( context instanceof FeaturesFormContext ) {
-                store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName);
+            final ForeingKey foreingKey = this.getForeingKey();
+            if( foreingKey==null ) {
+                return null;
             }
-            if ( store == null ) {
-                this.problemIndicator().set("Unable to locate the related table '" + foreingTableName + "'.");
-                return Objects.toString(value, "");
-            }
-            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
-            Feature feature = store.findFirst(
-                    builder.eq(
-                            builder.variable(foreingCodeName),
-                            builder.constant(value)
-                    ).toString()
-            );
-            return this.getDescription(feature);
-
+            String description = foreingKey.getLabel(null, value);
+            return description;
         } catch (Exception ex) {
-            LOGGER.warn("Can't get description from table '" + foreingTableName + "' for field '" + this.getForm().getDefinition().getName() + ":" + this.getName() + "'.", ex);
         }
         return null;
 
     }
     
-    private String getDescription(Feature feature) {
-        if (feature == null) {
-            return null;
-        }
-        String foreingLabel = getTagValueAsString(DAL_FOREING_LABEL, null);
-        DataManager dataManager = DALLocator.getDataManager();
-
-        Expression labelExpression = ExpressionUtils.createExpression(foreingLabel);
-        FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
-        symbolTable.setFeature(feature);
-        Object x = labelExpression.execute(symbolTable.createParent());
-        if (x == null) {
-            return null;
-        }
-        return x.toString();
-    }
-
     @Override
     public void setReadOnly(boolean readonly) {
         initComponentIfNeed();
