svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / featureform / swing / impl / dynformfield / selectableforeingkey / JDynFormFieldSelectableForeingKey.java @ 44259
History | View | Annotate | Download (4.48 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.featureform.swing.impl.dynformfield.selectableforeingkey; |
24 |
|
25 |
import java.util.Objects; |
26 |
import org.gvsig.expressionevaluator.Expression; |
27 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
28 |
import org.gvsig.expressionevaluator.SymbolTable; |
29 |
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_CODE; |
33 |
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_LABEL; |
34 |
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_TABLE; |
35 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
38 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
39 |
import org.gvsig.tools.dynform.JDynForm; |
40 |
import org.gvsig.tools.dynform.JDynFormField; |
41 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
42 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormFieldWithValueList; |
43 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
44 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
45 |
|
46 |
@SuppressWarnings("UseSpecificCatch") |
47 |
public class JDynFormFieldSelectableForeingKey extends AbstractJDynFormFieldWithValueList implements JDynFormField { |
48 |
|
49 |
public JDynFormFieldSelectableForeingKey(
|
50 |
DynFormSPIManager serviceManager, |
51 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
52 |
JDynFormFieldFactory factory, |
53 |
DynFormFieldDefinition definition, |
54 |
Object value
|
55 |
) { |
56 |
super(serviceManager, componentsFactory, factory, definition, value);
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
protected DynObjectValueItem[] getAvailableValues() { |
61 |
String foreingLabel = getTagValueAsString(DAL_FOREING_LABEL, null); |
62 |
String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null); |
63 |
String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null); |
64 |
try {
|
65 |
|
66 |
DataManager dataManager = DALLocator.getDataManager(); |
67 |
Expression labelExpression = ExpressionUtils.createExpression(foreingLabel);
|
68 |
FeatureSymbolTable featureSymbolTable = dataManager.createFeatureSymbolTable(); |
69 |
SymbolTable symbolTable = featureSymbolTable.createParent(); |
70 |
|
71 |
|
72 |
FeatureStore store = null;
|
73 |
JDynForm.DynFormContext context = this.getForm().getContext();
|
74 |
if( context instanceof FeaturesFormContext ) { |
75 |
store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName); |
76 |
} |
77 |
if( store == null ) { |
78 |
LOGGER.warn("Can't locate store '"+foreingTableName+"' to get available values of field '"+this.getName()+"'."); |
79 |
return null; |
80 |
} |
81 |
|
82 |
int count = (int) store.getFeatureCount(); |
83 |
DynObjectValueItem[] values = new DynObjectValueItem[count]; |
84 |
int n = 0; |
85 |
for (Feature feature : store.getFeatureSet()) {
|
86 |
featureSymbolTable.setFeature(feature); |
87 |
Object code = feature.get(foreingCodeName);
|
88 |
Object label = labelExpression.execute(symbolTable);
|
89 |
values[n++] = new DynObjectValueItem(code, Objects.toString(label, Objects.toString(code, "##ERROR##"))); |
90 |
} |
91 |
return values;
|
92 |
} catch (Exception ex) { |
93 |
LOGGER.warn("Can't get values from table '" + foreingTableName + "' for field '" + this.getForm().getDefinition().getName() + ":" + this.getName() + "'.", ex); |
94 |
} |
95 |
return null; |
96 |
} |
97 |
|
98 |
} |