Revision 44262

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.api/src/main/java/org/gvsig/featureform/swing/JFeaturesForm.java
33 33

  
34 34
import org.gvsig.fmap.dal.feature.FeatureQuery;
35 35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
36 37
import org.gvsig.tools.dynform.JDynForm;
37 38
import org.gvsig.tools.dynform.JDynForm.DynFormContext;
38 39
import org.gvsig.tools.dynform.JDynFormSet;
......
49 50
    public interface FeaturesFormContext extends DynFormContext {
50 51
        @Override
51 52
        public ResourcesStorage getResourcesStorage();
52
        public FeatureStore getFeatureStore(String storeName);
53
        public DataStore getDataStore(String storeName);
53
        public StoresRepository getStoresRepository();
54
        public FeatureStore getFeatureStore();
55
        public FeatureType getFeatureType();
54 56
    }
55 57

  
56 58
    public void setPreferredSize(Dimension dimension);
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/DefaultJFeaturesForm.java
27 27
import java.awt.event.ActionEvent;
28 28
import java.util.ArrayList;
29 29
import java.util.List;
30
import java.util.logging.Level;
30 31
import javax.swing.AbstractAction;
31 32
import javax.swing.Action;
32 33
import static javax.swing.Action.ACTION_COMMAND_KEY;
......
56 57
import org.gvsig.fmap.dal.feature.FeatureQuery;
57 58
import org.gvsig.fmap.dal.feature.FeatureStore;
58 59
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
60
import org.gvsig.fmap.dal.feature.FeatureType;
59 61
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
60 62
import org.gvsig.fmap.dal.swing.DALSwingLocator;
61 63
import org.gvsig.fmap.dal.swing.DataSwingManager;
......
97 99
        private DefaultFeaturesFormContext() {
98 100
            
99 101
        }
102

  
103
        public FeatureStore getFeatureStore() {
104
            return store;
105
        }
100 106
        
107
        public FeatureType getFeatureType() {
108
            try {
109
                return store.getDefaultFeatureType();
110
            } catch (DataException ex) {
111
                return null;
112
            }
113
        }
114
        
101 115
        @Override
102 116
        public ResourcesStorage getResourcesStorage() {
103 117
            return store.getResourcesStorage();
104 118
        }
105
        
106
        @Override
107
        public FeatureStore getFeatureStore(String storeName) {
108
            return (FeatureStore) this.getDataStore(storeName);
109
        }
110 119

  
111 120
        @Override
112
        public DataStore getDataStore(String storeName) {
113
            FeatureStore theStore = (FeatureStore) store.getChildren().get(storeName);
114
            if (theStore == null) {
115
                DataManager dataManager = DALLocator.getDataManager();
116
                StoresRepository repository = dataManager.getStoresRepository();
117
                if (repository.containsKey(storeName)) {
118
                    theStore = (FeatureStore) repository.get(storeName);
119
                }
120
            }
121
            return theStore;
121
        public StoresRepository getStoresRepository() {
122
            return store.getStoresRepository();
122 123
        }
123

  
124
        
124 125
    }
125 126
    
126 127
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
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/linkedentities/JDynFormFieldFeaturesTableLinkFactory.java
25 25

  
26 26
import org.gvsig.tools.dataTypes.DataTypes;
27 27
import org.gvsig.tools.dynform.DynFormFieldDefinition;
28
import org.gvsig.tools.dynform.JDynForm;
28 29
import org.gvsig.tools.dynform.JDynFormField;
29 30
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
30 31
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
......
47 48
    }
48 49
    
49 50
    @Override
50
    public boolean isApplicableTo(DynFormFieldDefinition fieldDefinition) {
51
        if( super.isApplicableTo(fieldDefinition) ) {
51
    public boolean isApplicableTo(JDynForm.DynFormContext context, DynFormFieldDefinition fieldDefinition) {
52
        if( super.isApplicableTo(context, fieldDefinition) ) {
52 53
            return true;
53 54
        }
54 55
        return false;
......
56 57

  
57 58
    public static void selfRegister() {
58 59
        DynFormSPIManager manager = DynFormSPILocator.getDynFormSPIManager();
59
        manager.registerDynFieldFactory(new JDynFormFieldFeaturesTableLinkFactory());
60
//        manager.registerDynFieldFactory(new JDynFormFieldFeaturesTableLinkFactory());
60 61
    }
61 62
}
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/features/JDynFormFieldFeaturesFactory.java
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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.featureform.swing.impl.dynformfield.features;
25

  
26
import org.apache.commons.lang3.StringUtils;
27
import static org.gvsig.featureform.swing.impl.dynformfield.features.JDynFormFieldFeatures.DAL_FEATURES_CODE_NAME;
28
import static org.gvsig.featureform.swing.impl.dynformfield.features.JDynFormFieldFeatures.DAL_FEATURES_COLUMNS;
29
import static org.gvsig.featureform.swing.impl.dynformfield.features.JDynFormFieldFeatures.DAL_FEATURES_TABLE_NAME;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dataTypes.DataTypes;
32
import org.gvsig.tools.dynform.DynFormFieldDefinition;
33
import org.gvsig.tools.dynform.JDynFormField;
34
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
35
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
36
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormFieldFactory;
37
import org.gvsig.tools.dynobject.DynObjectManager;
38

  
39
public class JDynFormFieldFeaturesFactory extends AbstractJDynFormFieldFactory {
40

  
41
    public JDynFormFieldFeaturesFactory() {
42
        super("FEATURES", DataTypes.UNKNOWN, "DAL.Features");
43
    }
44

  
45
    @Override
46
    public JDynFormField create(
47
            DynFormSPIManager serviceManager,
48
            DynFormSPIManager.ComponentsFactory componentsFactory,
49
            DynFormFieldDefinition fieldDefinition,
50
            Object value
51
    ) {
52
        return new JDynFormFieldFeatures(serviceManager, componentsFactory, this, fieldDefinition, value);
53
    }
54

  
55
    @Override
56
    public boolean isApplicableTo(DynFormFieldDefinition fieldDefinition) {
57
        if( StringUtils.equalsIgnoreCase("DAL.features", fieldDefinition.getSubtype())) {
58
            return true;
59
        }
60
        return false;
61
    }
62
    
63
    public static void selfRegister() {
64
        DynFormSPIManager manager = DynFormSPILocator.getDynFormSPIManager();
65
        manager.registerDynFieldFactory(new JDynFormFieldFeaturesFactory());
66
        DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager();
67
        dynObjectManager.registerTag(DAL_FEATURES_COLUMNS, "Part of a table of features definition, stores the name of the fields to be shown in the table separated by two points.");
68
        dynObjectManager.registerTag(DAL_FEATURES_TABLE_NAME, "Part of a table of features definition, store the table name of features.");
69
        dynObjectManager.registerTag(DAL_FEATURES_CODE_NAME, "Part of a table of features definition, store the name of the field that is a unique key in the features table.");
70
    }
71
    
72
}
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/features/JDynFormFieldFeatures.java
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.features;
24

  
25
import java.awt.BorderLayout;
26
import java.awt.Cursor;
27
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.net.URL;
32
import java.util.Arrays;
33
import java.util.List;
34
import javax.swing.BorderFactory;
35
import javax.swing.Icon;
36
import javax.swing.ImageIcon;
37
import javax.swing.JButton;
38
import javax.swing.JPanel;
39
import javax.swing.JTable;
40
import javax.swing.table.AbstractTableModel;
41
import org.gvsig.expressionevaluator.ExpressionBuilder;
42
import org.gvsig.expressionevaluator.ExpressionUtils;
43
import org.gvsig.featureform.swing.JFeaturesForm;
44
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
45
import org.gvsig.fmap.dal.feature.Feature;
46
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.FeatureQuery;
48
import org.gvsig.fmap.dal.feature.FeatureStore;
49
import org.gvsig.fmap.dal.feature.FeatureType;
50
import org.gvsig.fmap.dal.swing.DALSwingLocator;
51
import org.gvsig.fmap.dal.swing.DataSwingManager;
52
import org.gvsig.tools.dynform.DynFormFieldDefinition;
53
import org.gvsig.tools.dynform.JDynForm.DynFormContext;
54

  
55
import org.gvsig.tools.dynform.JDynFormField;
56
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
57
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
58
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory.ScrolledComponent;
59
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
60
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
61
import org.gvsig.tools.dynobject.DynObject;
62
import org.gvsig.tools.dynobject.Tags;
63
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
64

  
65
@SuppressWarnings("UseSpecificCatch")
66
public class JDynFormFieldFeatures extends AbstractJDynFormField implements JDynFormField {
67

  
68
    private class FeaturesTableModel extends AbstractTableModel {
69

  
70
        private final List<Feature> features;
71
        private final List<String> columnNames;
72
        private final FeatureType featureType;
73

  
74
        public FeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
75
            this.features = features;
76
            this.columnNames = columnNames;
77
            this.featureType = featureType;
78
        }
79

  
80
        @Override
81
        public int getRowCount() {
82
            if (this.features == null) {
83
                return 0;
84
            }
85
            return this.features.size();
86
        }
87

  
88
        @Override
89
        public int getColumnCount() {
90
            return this.columnNames.size();
91
        }
92

  
93
        @Override
94
        public String getColumnName(int columnIndex) {
95
            String attrName = this.columnNames.get(columnIndex);
96
            if( this.featureType==null ) {
97
                return attrName;
98
            }
99
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
100
            if (attrdesc == null) {
101
                return "C" + columnIndex;
102
            }
103
            return attrdesc.getLabel();
104
        }
105

  
106
        @Override
107
        public Class<?> getColumnClass(int columnIndex) {
108
            if( this.featureType==null ) {
109
                return String.class;
110
            }
111
            String attrName = this.columnNames.get(columnIndex);
112
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
113
            if (attrdesc == null) {
114
                return String.class;
115
            }
116
            return attrdesc.getDataType().getDefaultClass();
117
        }
118

  
119
        @Override
120
        public boolean isCellEditable(int rowIndex, int columnIndex) {
121
            return false;
122
        }
123

  
124
        @Override
125
        public Object getValueAt(int rowIndex, int columnIndex) {
126
            if (this.features == null) {
127
                return null;
128
            }
129
            Feature feature = this.features.get(rowIndex);
130
            String attrName = this.columnNames.get(columnIndex);
131
            try {
132
                return feature.get(attrName);
133
            } catch (Throwable th) {
134
                return null;
135
            }
136
        }
137

  
138
        @Override
139
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
140

  
141
        }
142
    }
143

  
144
    public static final String DAL_FEATURES_COLUMNS = "DAL.features.columns";
145
    public static final String DAL_FEATURES_TABLE_NAME = "DAL.features.tableName";
146
    public static final String DAL_FEATURES_CODE_NAME = "DAL.features.codeName";
147

  
148
    private List<Feature> assignedValue = null;
149
    private List<Feature> value = null;
150

  
151
    private FeatureType featureType;
152
    private List<String> columnNames;
153
    
154
    private JTable tblFeatures = null;
155
    private JButton btnUnlink = null;
156
    private JButton btnEdit = null;
157
    private Dimension preferredSize = null;
158

  
159

  
160
    public JDynFormFieldFeatures(
161
            DynFormSPIManager serviceManager,
162
            DynFormSPIManager.ComponentsFactory componentsFactory,
163
            JDynFormFieldFactory factory,
164
            DynFormFieldDefinition definition,
165
            Object value
166
    ) {
167
        super(serviceManager, componentsFactory, factory, definition, value);
168
        if (value != null) {
169
            this.assignedValue = (List<Feature>) value;
170
        }
171
    }
172

  
173
    @Override
174
    public void loadDefaultValuesFromTags(Tags tags) {
175
        super.loadDefaultValuesFromTags(tags);
176
        int width = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, 100);
177
        int height = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, -1);
178
        if( height>100 ) {
179
            this.preferredSize = new Dimension(width, height);
180
        }
181
    }
182
    
183
    @Override
184
    public List<Feature> getAssignedValue() {
185
        return this.assignedValue;
186
    }
187

  
188
    public void initComponentIfNeed() {
189
        if (this.contents == null) {
190
            this.initComponent();
191
        }
192
    }
193

  
194
    @Override
195
    public void initComponent() {
196
        String featuresTableName = getTagValueAsString(DAL_FEATURES_TABLE_NAME, null);
197
        String featuresColumns = getTagValueAsString(DAL_FEATURES_COLUMNS, "C1:C2");
198
        JPanel panel = new JPanel();
199
        try {
200
            ComponentsFactory components = this.getComponentsFactory();
201
            ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null);
202

  
203
            if( this.preferredSize!=null ) {
204
                comps.getScrollPane().setPreferredSize(this.preferredSize);
205
            }
206
            this.tblFeatures = comps.getComponent();
207
            this.columnNames = Arrays.asList(split(featuresColumns, ":/;,|-"));
208

  
209
            this.btnUnlink = components.getJButton(this.getDefinition(), "Unlink");
210
            if (this.btnUnlink == null) {
211
                this.btnUnlink = new JButton();
212
            }
213
            this.initButton(this.btnUnlink, "Unlink selected item", "unlink.png");
214
            this.btnUnlink.addActionListener(new ActionListener() {
215
                @Override
216
                public void actionPerformed(ActionEvent ae) {
217
                    doUnlink();
218
                }
219
            });
220

  
221
            this.btnEdit = components.getJButton(this.getDefinition(), "Edit");
222
            if (this.btnEdit == null) {
223
                this.btnEdit = new JButton();
224
            }
225
            this.initButton(this.btnEdit, "View selected item", "edit.png");
226
            this.btnEdit.addActionListener(new ActionListener() {
227
                @Override
228
                public void actionPerformed(ActionEvent ae) {
229
                    doEdit();
230
                }
231
            });
232
            if (!components.containsComponents(this.getDefinition())) {
233
                panel.setLayout(new BorderLayout());
234
                panel.add(comps.getScrollPane(), BorderLayout.CENTER);
235
                JPanel panelButtons = new JPanel();
236
                panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 1));
237
                panelButtons.add(this.btnUnlink);
238
                panelButtons.add(this.btnEdit);
239
                panel.add(panelButtons, BorderLayout.SOUTH);
240
            }
241
            this.contents = panel;
242

  
243
            FeatureStore store = null;
244
            DynFormContext context = this.getForm().getContext();
245
            if (context instanceof JFeaturesForm.FeaturesFormContext) {
246
                store = ((FeaturesFormContext) context).getFeatureStore(featuresTableName);
247
            }
248
            if (store == null) {
249
                this.problemIndicator().set("Unable to locate the related table '" + featuresTableName + "'.");
250
                return;
251
            }
252
            this.featureType = store.getDefaultFeatureType();
253
            this.tblFeatures.setModel(
254
                    new FeaturesTableModel(this.featureType, this.columnNames, this.assignedValue)
255
            );
256
        } catch (Throwable th) {
257
            LOGGER.warn("Can't initialize components of '" + this.getName() + "'.", th);
258
        }
259
    }
260

  
261
    private String[] split(String value, String separators) {
262
        int firstSeparatorPosition = 1000000;
263
        Character sep = null;
264
        for (char ch : separators.toCharArray()) {
265
            int pos = value.indexOf(ch);
266
            if( pos>0 && pos<firstSeparatorPosition ) {
267
                sep = ch;
268
                firstSeparatorPosition = pos;
269
            }
270
        }
271
        if( sep == null ) {
272
            return new String[] { value };
273
        }
274
        return value.split("["+sep+"]");
275
    }
276
    
277

  
278
    private void doEdit() {
279
        if (this.value == null) {
280
            return;
281
        }
282
        int selectedRow = this.tblFeatures.getSelectedRow();
283
        if( selectedRow<0 ) {
284
            return;
285
        }
286
        String tableName = getTagValueAsString(DAL_FEATURES_TABLE_NAME, null);
287
        String codeName = getTagValueAsString(DAL_FEATURES_CODE_NAME, null);
288
        try {
289
            this.btnEdit.setEnabled(false);
290
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
291

  
292
            Feature feature = this.value.get(selectedRow);
293

  
294
            FeatureStore store = null;
295
            DynFormContext context = this.getForm().getContext();
296
            if( context instanceof FeaturesFormContext ) {
297
                store = ((FeaturesFormContext) context).getFeatureStore(tableName);
298
            }
299
            if( store == null ) {
300
                this.problemIndicator().set("Unable to locate the related table '" + tableName + "'.");
301
                return;
302
            }
303
            Object codeValue = feature.get(codeName);
304
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
305
            FeatureQuery query = store.createFeatureQuery();
306
            query.setFilter(builder.eq(
307
                    builder.variable(codeName),
308
                    builder.constant(codeValue)
309
                ).toString()
310
            );
311
            query.retrievesAllAttributes();
312
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
313
            form.setQuery(query);
314
            form.showForm(WindowManager.MODE.WINDOW);
315

  
316
        } catch (Exception ex) {
317
            LOGGER.warn("Can't show linked form", ex);
318
        } finally {
319
            this.btnEdit.setEnabled(true);
320
        }
321
    }
322

  
323
    private void doUnlink() {
324
        // FIXME: Falta implementar el unlink en el JDynFormFieldFeatures
325
    }
326

  
327
    private JButton initButton(JButton button, final String tip, final String image) {
328
        if (button.getIcon() == null) {
329
            URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image);
330
            Icon icon = new ImageIcon(url);
331
            button.setIcon(icon);
332
        }
333
        if (button.getText().trim().equals("...")) {
334
            button.setText("");
335
        }
336
        button.setBorder(BorderFactory.createEmptyBorder());
337
        button.setBorderPainted(false);
338
        button.setFocusPainted(false);
339
        button.setContentAreaFilled(false);
340
        button.setToolTipText(tip);
341
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
342
        return button;
343
    }
344

  
345
    @Override
346
    public void setReadOnly(boolean readonly) {
347
        initComponentIfNeed();
348
        boolean editable = !readonly;
349
        this.btnUnlink.setEnabled(editable);
350
        this.btnEdit.setEnabled(true);
351
    }
352

  
353
    @Override
354
    public void setValue(Object value) {
355
        initComponentIfNeed();
356
        if (value == null) {
357
            this.clear();
358
            return;
359
        }
360
        this.value = (List<Feature>) value;
361
        this.tblFeatures.setModel(
362
                new FeaturesTableModel(this.featureType, this.columnNames, this.value)
363
        );
364
    }
365

  
366
    @Override
367
    public Object getValue() {
368
        return this.value;
369
    }
370

  
371
    @Override
372
    public void fetch(DynObject container) {
373
    }
374

  
375
    @Override
376
    public boolean hasValidValue() {
377
        return true;
378
    }
379

  
380
    @Override
381
    public void clear() {
382
        initComponentIfNeed();
383
        this.value = null;
384
        this.tblFeatures.setModel(
385
                new FeaturesTableModel(this.featureType, this.columnNames, null)
386
        );
387
    }
388
}
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/features/JDynFormFieldRelatedFeatures.java
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.features;
24

  
25
import java.awt.BorderLayout;
26
import java.awt.Cursor;
27
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.net.URL;
32
import java.util.List;
33
import javax.swing.BorderFactory;
34
import javax.swing.Icon;
35
import javax.swing.ImageIcon;
36
import javax.swing.JButton;
37
import javax.swing.JPanel;
38
import javax.swing.JTable;
39
import javax.swing.table.AbstractTableModel;
40
import org.gvsig.featureform.swing.JFeaturesForm;
41
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
42
import org.gvsig.fmap.dal.StoresRepository;
43
import org.gvsig.fmap.dal.complements.RelatedFeatures;
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.FeatureQuery;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.dal.feature.FeatureType;
49
import org.gvsig.fmap.dal.swing.DALSwingLocator;
50
import org.gvsig.fmap.dal.swing.DataSwingManager;
51
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.dispose.DisposeUtils;
53
import org.gvsig.tools.dynform.DynFormFieldDefinition;
54
import org.gvsig.tools.dynform.JDynForm;
55

  
56
import org.gvsig.tools.dynform.JDynFormField;
57
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
58
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
59
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory.ScrolledComponent;
60
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
61
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
62
import org.gvsig.tools.dynobject.DynObject;
63
import org.gvsig.tools.dynobject.Tags;
64
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
65

  
66
@SuppressWarnings("UseSpecificCatch")
67
public class JDynFormFieldRelatedFeatures extends AbstractJDynFormField implements JDynFormField {
68

  
69
    private class FeaturesTableModel extends AbstractTableModel {
70

  
71
        private final List<Feature> features;
72
        private final List<String> columnNames;
73
        private final FeatureType featureType;
74

  
75
        public FeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
76
            this.features = features;
77
            this.columnNames = columnNames;
78
            this.featureType = featureType;
79
        }
80

  
81
        @Override
82
        public int getRowCount() {
83
            if (this.features == null) {
84
                return 0;
85
            }
86
            return this.features.size();
87
        }
88

  
89
        @Override
90
        public int getColumnCount() {
91
            return this.columnNames.size();
92
        }
93

  
94
        @Override
95
        public String getColumnName(int columnIndex) {
96
            String attrName = this.columnNames.get(columnIndex);
97
            if (this.featureType == null) {
98
                return attrName;
99
            }
100
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
101
            if (attrdesc == null) {
102
                return "C" + columnIndex;
103
            }
104
            return attrdesc.getLabel();
105
        }
106

  
107
        @Override
108
        public Class<?> getColumnClass(int columnIndex) {
109
            if (this.featureType == null) {
110
                return String.class;
111
            }
112
            String attrName = this.columnNames.get(columnIndex);
113
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
114
            if (attrdesc == null) {
115
                return String.class;
116
            }
117
            return attrdesc.getDataType().getDefaultClass();
118
        }
119

  
120
        @Override
121
        public boolean isCellEditable(int rowIndex, int columnIndex) {
122
            return false;
123
        }
124

  
125
        @Override
126
        public Object getValueAt(int rowIndex, int columnIndex) {
127
            if (this.features == null) {
128
                return null;
129
            }
130
            Feature feature = this.features.get(rowIndex);
131
            String attrName = this.columnNames.get(columnIndex);
132
            try {
133
                return feature.get(attrName);
134
            } catch (Throwable th) {
135
                return null;
136
            }
137
        }
138

  
139
        @Override
140
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
141

  
142
        }
143
    }
144

  
145
    private List<Feature> assignedValue = null;
146
    private List<Feature> value = null;
147

  
148
    private FeatureType featureType;
149
    private List<String> columnNames;
150

  
151
    private JTable tblFeatures = null;
152
    private JButton btnUnlink = null;
153
    private JButton btnEdit = null;
154
    private Dimension preferredSize = null;
155

  
156
    public JDynFormFieldRelatedFeatures(
157
            DynFormSPIManager serviceManager,
158
            DynFormSPIManager.ComponentsFactory componentsFactory,
159
            JDynFormFieldFactory factory,
160
            DynFormFieldDefinition definition,
161
            Object value
162
    ) {
163
        super(serviceManager, componentsFactory, factory, definition, value);
164
        if (value != null) {
165
            this.assignedValue = (List<Feature>) value;
166
        }
167
    }
168

  
169
    @Override
170
    public void loadDefaultValuesFromTags(Tags tags) {
171
        super.loadDefaultValuesFromTags(tags);
172
        int width = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, 100);
173
        int height = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, -1);
174
        if (height > 100) {
175
            this.preferredSize = new Dimension(width, height);
176
        }
177
    }
178

  
179
    @Override
180
    public List<Feature> getAssignedValue() {
181
        return this.assignedValue;
182
    }
183

  
184
    public void initComponentIfNeed() {
185
        if (this.contents == null) {
186
            this.initComponent();
187
        }
188
    }
189

  
190
    private RelatedFeatures getRelatedFeatures() {
191
        RelatedFeatures relatedFeatures = (RelatedFeatures) ToolsLocator.
192
                getComplementsManager().get(
193
                        RelatedFeatures.COMPLEMENT_MANE,
194
                        this.getDefinition()
195
                );
196
        return relatedFeatures;
197
    }
198

  
199
    private StoresRepository getStoresRepository() {
200
        JDynForm.DynFormContext context = this.getForm().getContext();
201
        if (!(context instanceof FeaturesFormContext)) {
202
            return null;
203
        }
204
        StoresRepository repository = ((FeaturesFormContext) context).getStoresRepository();
205
        return repository;
206
    }
207

  
208
    @Override
209
    public void initComponent() {
210
        JPanel panel = new JPanel();
211
        try {
212
            ComponentsFactory components = this.getComponentsFactory();
213
            ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null);
214

  
215
            if (this.preferredSize != null) {
216
                comps.getScrollPane().setPreferredSize(this.preferredSize);
217
            }
218
            this.tblFeatures = comps.getComponent();
219

  
220
            this.btnUnlink = components.getJButton(this.getDefinition(), "Unlink");
221
            if (this.btnUnlink == null) {
222
                this.btnUnlink = new JButton();
223
            }
224
            this.initButton(this.btnUnlink, "Unlink selected item", "unlink.png");
225
            this.btnUnlink.addActionListener(new ActionListener() {
226
                @Override
227
                public void actionPerformed(ActionEvent ae) {
228
                    doUnlink();
229
                }
230
            });
231

  
232
            this.btnEdit = components.getJButton(this.getDefinition(), "Edit");
233
            if (this.btnEdit == null) {
234
                this.btnEdit = new JButton();
235
            }
236
            this.initButton(this.btnEdit, "View selected item", "edit.png");
237
            this.btnEdit.addActionListener(new ActionListener() {
238
                @Override
239
                public void actionPerformed(ActionEvent ae) {
240
                    doEdit();
241
                }
242
            });
243
            if (!components.containsComponents(this.getDefinition())) {
244
                panel.setLayout(new BorderLayout());
245
                panel.add(comps.getScrollPane(), BorderLayout.CENTER);
246
                JPanel panelButtons = new JPanel();
247
                panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 1));
248
                panelButtons.add(this.btnUnlink);
249
                panelButtons.add(this.btnEdit);
250
                panel.add(panelButtons, BorderLayout.SOUTH);
251
            }
252
            this.contents = panel;
253
            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
254
            if (relatedFeatures == null) {
255
                this.problemIndicator().set("Unable to locate the related table.");
256
                return;
257
            }
258
            RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
259
            context.setStoresRepository(this.getStoresRepository());
260
            try {
261
                FeatureStore store = relatedFeatures.getFeatureStore(context);
262
                if (store == null) {
263
                    this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
264
                    return;
265
                }
266
                this.columnNames = relatedFeatures.getColumns(context);
267
                this.featureType = store.getDefaultFeatureType();
268
            } finally {
269
                DisposeUtils.disposeQuietly(context);
270
            }
271
            this.tblFeatures.setModel(
272
                    new FeaturesTableModel(this.featureType, this.columnNames, this.assignedValue)
273
            );
274
        } catch (Throwable th) {
275
            LOGGER.warn("Can't initialize components of '" + this.getName() + "'.", th);
276
        }
277
    }
278

  
279
    private void doEdit() {
280
        if (this.value == null) {
281
            return;
282
        }
283
        int selectedRow = this.tblFeatures.getSelectedRow();
284
        if (selectedRow < 0) {
285
            return;
286
        }
287
        try {
288
            this.btnEdit.setEnabled(false);
289
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
290

  
291
            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
292
            if (relatedFeatures == null) {
293
                this.problemIndicator().set("Unable to locate the related table.");
294
                return;
295
            }
296
            RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
297
            context.setStoresRepository(this.getStoresRepository());
298
            FeatureStore store = relatedFeatures.getFeatureStore(context);
299
            if (store == null) {
300
                this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
301
                return;
302
            }
303
            Feature f = this.value.get(selectedRow);
304
            FeatureQuery query = relatedFeatures.getUniqueKeyQuery(
305
                    context, 
306
                    relatedFeatures.getUniqueKey(context, f)
307
            );
308
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
309
            form.setQuery(query);
310
            form.showForm(WindowManager.MODE.WINDOW);
311

  
312
        } catch (Exception ex) {
313
            LOGGER.warn("Can't show linked form", ex);
314
        } finally {
315
            this.btnEdit.setEnabled(true);
316
        }
317
    }
318

  
319
    private void doUnlink() {
320
        // FIXME: Falta implementar el unlink en el JDynFormFieldFeatures
321
    }
322

  
323
    private JButton initButton(JButton button, final String tip, final String image) {
324
        if (button.getIcon() == null) {
325
            URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image);
326
            Icon icon = new ImageIcon(url);
327
            button.setIcon(icon);
328
        }
329
        if (button.getText().trim().equals("...")) {
330
            button.setText("");
331
        }
332
        button.setBorder(BorderFactory.createEmptyBorder());
333
        button.setBorderPainted(false);
334
        button.setFocusPainted(false);
335
        button.setContentAreaFilled(false);
336
        button.setToolTipText(tip);
337
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
338
        return button;
339
    }
340

  
341
    @Override
342
    public void setReadOnly(boolean readonly) {
343
        initComponentIfNeed();
344
        boolean editable = !readonly;
345
        this.btnUnlink.setEnabled(editable);
346
        this.btnEdit.setEnabled(true);
347
    }
348

  
349
    @Override
350
    public void setValue(Object value) {
351
        initComponentIfNeed();
352
        if (value == null) {
353
            this.clear();
354
            return;
355
        }
356
        this.value = (List<Feature>) value;
357
        this.tblFeatures.setModel(
358
                new FeaturesTableModel(this.featureType, this.columnNames, this.value)
359
        );
360
    }
361

  
362
    @Override
363
    public Object getValue() {
364
        return this.value;
365
    }
366

  
367
    @Override
368
    public void fetch(DynObject container) {
369
    }
370

  
371
    @Override
372
    public boolean hasValidValue() {
373
        return true;
374
    }
375

  
376
    @Override
377
    public void clear() {
378
        initComponentIfNeed();
379
        this.value = null;
380
        this.tblFeatures.setModel(
381
                new FeaturesTableModel(this.featureType, this.columnNames, null)
382
        );
383
    }
384
}
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/features/JDynFormFieldRelatedFeaturesFactory.java
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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.featureform.swing.impl.dynformfield.features;
25

  
26
import org.gvsig.fmap.dal.complements.RelatedFeatures;
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.dataTypes.DataTypes;
29
import org.gvsig.tools.dynform.DynFormFieldDefinition;
30
import org.gvsig.tools.dynform.JDynForm;
31
import org.gvsig.tools.dynform.JDynFormField;
32
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
33
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
34
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormFieldFactory;
35

  
36
public class JDynFormFieldRelatedFeaturesFactory extends AbstractJDynFormFieldFactory {
37

  
38
    public JDynFormFieldRelatedFeaturesFactory() {
39
        super("FEATURES", DataTypes.UNKNOWN,null);
40
    }
41

  
42
    @Override
43
    public JDynFormField create(
44
            DynFormSPIManager serviceManager,
45
            DynFormSPIManager.ComponentsFactory componentsFactory,
46
            DynFormFieldDefinition fieldDefinition,
47
            Object value
48
    ) {
49
        return new JDynFormFieldRelatedFeatures(serviceManager, componentsFactory, this, fieldDefinition, value);
50
    }
51

  
52
    @Override
53
    public int getPriority() {
54
        return 500;
55
    }
56
    
57
    @Override
58
    public boolean isApplicableTo(JDynForm.DynFormContext context, DynFormFieldDefinition fieldDefinition) {
59
        RelatedFeatures complement = (RelatedFeatures) ToolsLocator.getComplementsManager().get(
60
                RelatedFeatures.COMPLEMENT_MANE, fieldDefinition
61
        );
62
        if( complement.isRelatedFeatures() ) {
63
            return true;
64
        }
65
        return false;
66
    }
67
    
68
    public static void selfRegister() {
69
        DynFormSPIManager manager = DynFormSPILocator.getDynFormSPIManager();
70
        manager.registerDynFieldFactory(new JDynFormFieldRelatedFeaturesFactory());
71
    }
72
    
73
}
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/linkforeingkey/JDynFormFieldForeingKey.java
41 41
import javax.swing.ListModel;
42 42
import javax.swing.event.ListDataListener;
43 43
import javax.swing.text.JTextComponent;
44
import org.gvsig.expressionevaluator.Expression;
45
import org.gvsig.expressionevaluator.ExpressionBuilder;
46
import org.gvsig.expressionevaluator.ExpressionUtils;
47 44
import org.gvsig.featureform.swing.JFeaturesForm;
48 45
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
49
import org.gvsig.fmap.dal.DALLocator;
50
import org.gvsig.fmap.dal.DataManager;
51
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
46
import org.gvsig.fmap.dal.StoresRepository;
52 47
import org.gvsig.fmap.dal.feature.Feature;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
53 49
import org.gvsig.fmap.dal.feature.FeatureQuery;
54 50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.dal.feature.ForeingKey;
53
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey;
55 54
import org.gvsig.fmap.dal.swing.DALSwingLocator;
56 55
import org.gvsig.fmap.dal.swing.DataSwingManager;
56
import org.gvsig.tools.ToolsLocator;
57
import org.gvsig.tools.dispose.DisposeUtils;
57 58
import org.gvsig.tools.dynform.DynFormFieldDefinition;
58
import org.gvsig.tools.dynform.JDynForm.DynFormContext;
59
import org.gvsig.tools.dynform.JDynForm;
59 60

  
60 61
import org.gvsig.tools.dynform.JDynFormField;
61 62
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
......
71 72
@SuppressWarnings("UseSpecificCatch")
72 73
public class JDynFormFieldForeingKey extends AbstractJDynFormField implements JDynFormField {
73 74

  
74
    private static final String DAL_FOREING_CODE = "DAL.foreingCode";
75
    private static final String DAL_FOREING_TABLE = "DAL.foreingTable";
76
    private static final String DAL_FOREING_LABEL = "DAL.foreingLabel";
77

  
78 75
    private Object assignedValue = null;
79 76
    private Object value = null;
80 77
    private JTextComponent txtDescription = null;
......
212 209
        return button;
213 210
    }
214 211

  
212
    private ForeingKey getForeingKey() {
213
        JDynForm.DynFormContext context = this.getForm().getContext();
214
        if( !(context instanceof FeaturesFormContext) ) {
215
            return null;
216
        }
217
        FeatureType featureType = ((FeaturesFormContext)context).getFeatureType();
218
        if( featureType==null ) {
219
            return null;
220
        }
221
        FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName());
222
        if( attribute == null ) {
223
            return null;
224
        }
225
        ForeingKey foreingKey = attribute.getForeingKey();
226
        return foreingKey;
227
    }
228
    
215 229
    private void doLink() {
216
        final String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null);
217
        final String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null);
230
        final ForeingKey foreingKey = this.getForeingKey();
231
        if( foreingKey==null ) {
232
            return;
233
        }
234
        final ContextForeingKey context = foreingKey.createContext();
218 235
        try {
219
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
220

  
221
            FeatureStore store = null;
222
            DynFormContext context = this.getForm().getContext();
223
            if( context instanceof FeaturesFormContext ) {
224
                store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName);
225
            }
226
            final List<Feature> features = store.getFeatures();
236
            final List<Feature> features = foreingKey.getFeatures(context);
237
            
227 238
            ListModel<String> model = new ListModel<String>() {
228 239
                @Override
229 240
                public int getSize() {
......
233 244
                @Override
234 245
                public String getElementAt(int index) {
235 246
                    Feature feature = features.get(index);
236
                    return getDescription(feature);
247
                    ForeingKey foreingKey = getForeingKey();
248
                    return foreingKey.getLabel(context, feature);
237 249
                }
238 250

  
239 251
                @Override
......
244 256
                public void removeListDataListener(ListDataListener l) {
245 257
                }
246 258
            };
259
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
260

  
247 261
            final JList<String> jlist = new JList<>();
248 262
            jlist.setModel(model);
249 263
            jlist.setPreferredSize(new Dimension(350, 200));
......
263 277
                        }
264 278
                        Feature feature = features.get(n);
265 279
                        if( feature!=null ) {
266
                            setValue(feature.get(foreingCodeName));
280
                            setValue(foreingKey.getCode(context, feature));
267 281
                        }
268 282
                    }
269 283
                }
......
272 286

  
273 287
        } catch (Exception ex) {
274 288
            LOGGER.warn("Can't show selector", ex);
289
        } finally {
290
            DisposeUtils.disposeQuietly(context);
275 291
        }
276 292
    }
277 293

  
......
283 299
        if (this.value == null) {
284 300
            return;
285 301
        }
286
        String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null);
287
        String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null);
302
        final ForeingKey foreingKey = this.getForeingKey();
303
        if( foreingKey==null ) {
304
            return;
305
        }
306
        final ContextForeingKey context = foreingKey.createContext();
288 307
        try {
289 308
            this.btnEdit.setEnabled(false);
290
            DataManager dataManager = DALLocator.getDataManager();
291 309
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
292

  
293
            FeatureStore store = null;
294
            DynFormContext context = this.getForm().getContext();
295
            if( context instanceof FeaturesFormContext ) {
296
                store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName);
297
            }
298
            if( store == null ) {
299
                this.problemIndicator().set("Unable to locate the related table '" + foreingTableName + "'.");
300
                return;
301
            }
302
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
303
            FeatureQuery query = store.createFeatureQuery();
304
            query.setFilter(builder.eq(
305
                    builder.variable(foreingCodeName),
306
                    builder.constant(value)
307
                ).toString()
310
            FeatureQuery query = foreingKey.getQuery(context, value);
311
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(
312
                    foreingKey.getFeatureStore(context)
308 313
            );
309
            query.retrievesAllAttributes();
310
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
311 314
            form.setQuery(query);
312 315
            form.showForm(WindowManager.MODE.WINDOW);
313 316

  
......
315 318
            LOGGER.warn("Can't show linked form", ex);
316 319
        } finally {
317 320
            this.btnEdit.setEnabled(true);
321
            DisposeUtils.disposeQuietly(context);
318 322
        }
319 323
    }
320 324

  
......
322 326
        if (this.value == null) {
323 327
            return null;
324 328
        }
325
        String foreingLabel = getTagValueAsString(DAL_FOREING_LABEL, null);
326
        String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null);
327
        String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null);
328 329
        try {
329
            FeatureStore store = null;
330
            DynFormContext context = this.getForm().getContext();
331
            if( context instanceof FeaturesFormContext ) {
332
                store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName);
330
            final ForeingKey foreingKey = this.getForeingKey();
331
            if( foreingKey==null ) {
332
                return null;
333 333
            }
334
            if ( store == null ) {
335
                this.problemIndicator().set("Unable to locate the related table '" + foreingTableName + "'.");
336
                return Objects.toString(value, "");
337
            }
338
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
339
            Feature feature = store.findFirst(
340
                    builder.eq(
341
                            builder.variable(foreingCodeName),
342
                            builder.constant(value)
343
                    ).toString()
344
            );
345
            return this.getDescription(feature);
346

  
334
            String description = foreingKey.getLabel(null, value);
335
            return description;
347 336
        } catch (Exception ex) {
348
            LOGGER.warn("Can't get description from table '" + foreingTableName + "' for field '" + this.getForm().getDefinition().getName() + ":" + this.getName() + "'.", ex);
349 337
        }
350 338
        return null;
351 339

  
352 340
    }
353 341
    
354
    private String getDescription(Feature feature) {
355
        if (feature == null) {
356
            return null;
357
        }
358
        String foreingLabel = getTagValueAsString(DAL_FOREING_LABEL, null);
359
        DataManager dataManager = DALLocator.getDataManager();
360

  
361
        Expression labelExpression = ExpressionUtils.createExpression(foreingLabel);
362
        FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
363
        symbolTable.setFeature(feature);
364
        Object x = labelExpression.execute(symbolTable.createParent());
365
        if (x == null) {
366
            return null;
367
        }
368
        return x.toString();
369
    }
370

  
371 342
    @Override
372 343
    public void setReadOnly(boolean readonly) {
373 344
        initComponentIfNeed();
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/linkforeingkey/JDynFormFieldForeingKeyFactory.java
23 23
 */
24 24
package org.gvsig.featureform.swing.impl.dynformfield.linkforeingkey;
25 25

  
26
import org.apache.commons.lang3.StringUtils;
26
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
27
import org.gvsig.fmap.dal.DataManager;
28
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
29
import org.gvsig.fmap.dal.feature.FeatureType;
30
import org.gvsig.fmap.dal.feature.ForeingKey;
27 31
import org.gvsig.tools.dataTypes.DataTypes;
28 32
import org.gvsig.tools.dynform.DynFormFieldDefinition;
33
import org.gvsig.tools.dynform.JDynForm;
29 34
import org.gvsig.tools.dynform.JDynFormField;
30 35
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
31 36
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
......
34 39
public class JDynFormFieldForeingKeyFactory extends AbstractJDynFormFieldFactory {
35 40

  
36 41
    public JDynFormFieldForeingKeyFactory() {
37
        super("FOREINGKEY", DataTypes.UNKNOWN, "DAL.ForeingKey");
42
        super("FOREINGKEY", DataTypes.UNKNOWN, null);
38 43
    }
39 44

  
40 45
    @Override
......
48 53
    }
49 54

  
50 55
    @Override
51
    public boolean isApplicableTo(DynFormFieldDefinition fieldDefinition) {
52
        if( StringUtils.equalsIgnoreCase("DAL.ForeingKey", fieldDefinition.getSubtype())) {
53
            return true;
56
    public int getPriority() {
57
        return 500;
58
    }
59
    
60
    @Override
61
    public boolean isApplicableTo(JDynForm.DynFormContext context, DynFormFieldDefinition fieldDefinition) {
62
        if( !(context instanceof FeaturesFormContext) ) {
63
            return false;
54 64
        }
55
        return false;
65
        FeatureType featureType = ((FeaturesFormContext)context).getFeatureType();
66
        FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(fieldDefinition.getName());
67
        if( attribute == null ) {
68
            return false;
69
        }
70
        ForeingKey foreingKey = attribute.getForeingKey();
71
        if( foreingKey==null ) {
72
            return false;
73
        }
74
        return foreingKey.isForeingKey() && !foreingKey.isSelectable();
56 75
    }
57 76
    
58 77
    public static void selfRegister() {
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/linkedentity/JDynFormFieldFeatureLinkFactory.java
25 25

  
26 26
import org.gvsig.tools.dataTypes.DataTypes;
27 27
import org.gvsig.tools.dynform.DynFormFieldDefinition;
28
import org.gvsig.tools.dynform.JDynForm;
28 29
import org.gvsig.tools.dynform.JDynFormField;
29 30
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
30 31
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
......
47 48
    }
48 49

  
49 50
    @Override
50
    public boolean isApplicableTo(DynFormFieldDefinition fieldDefinition) {
51
        if( super.isApplicableTo(fieldDefinition) ) {
51
    public int getPriority() {
52
        return 500;
53
    }
54
    
55
    @Override
56
    public boolean isApplicableTo(JDynForm.DynFormContext context, DynFormFieldDefinition fieldDefinition) {
57
        if( super.isApplicableTo(context, fieldDefinition) ) {
52 58
            return true;
53 59
        }
54 60
        return false;
......
56 62
    
57 63
    public static void selfRegister() {
58 64
        DynFormSPIManager manager = DynFormSPILocator.getDynFormSPIManager();
59
        manager.registerDynFieldFactory(new JDynFormFieldFeatureLinkFactory());
65
//        manager.registerDynFieldFactory(new JDynFormFieldFeatureLinkFactory());
60 66
    }
61 67
    
62 68
}
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
22 22
 */
23 23
package org.gvsig.featureform.swing.impl.dynformfield.selectableforeingkey;
24 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 25
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;
26
import org.gvsig.fmap.dal.StoresRepository;
27
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
28
import org.gvsig.fmap.dal.feature.FeatureType;
29
import org.gvsig.fmap.dal.feature.ForeingKey;
30
import org.gvsig.tools.ToolsLocator;
38 31
import org.gvsig.tools.dynform.DynFormFieldDefinition;
39 32
import org.gvsig.tools.dynform.JDynForm;
40 33
import org.gvsig.tools.dynform.JDynFormField;
......
56 49
        super(serviceManager, componentsFactory, factory, definition, value);
57 50
    }
58 51

  
52

  
53
    private ForeingKey getForeingKey() {
54
        JDynForm.DynFormContext context = this.getForm().getContext();
55
        if( !(context instanceof FeaturesFormContext) ) {
56
            return null;
57
        }
58
        FeatureType featureType = ((FeaturesFormContext)context).getFeatureType();
59
        if( featureType==null ) {
60
            return null;
61
        }
62
        FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName());
63
        if( attribute == null ) {
64
            return null;
65
        }
66
        ForeingKey foreingKey = attribute.getForeingKey();
67
        return foreingKey;
68
    }
69
    
59 70
    @Override
60 71
    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##")));
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff