Statistics
| Revision:

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 / features / JDynFormFieldRelatedFeatures.java @ 46867

History | View | Annotate | Download (16.4 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.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 java.util.Objects;
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 javax.swing.table.TableModel;
42
import org.apache.commons.text.StringEscapeUtils;
43
import org.gvsig.featureform.swing.JFeaturesForm;
44
import org.gvsig.featureform.swing.FeaturesFormContext;
45
import org.gvsig.fmap.dal.StoresRepository;
46
import org.gvsig.fmap.dal.complements.RelatedFeatures;
47
import org.gvsig.fmap.dal.feature.Feature;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
49
import org.gvsig.fmap.dal.feature.FeatureQuery;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.dal.swing.DALSwingLocator;
53
import org.gvsig.fmap.dal.swing.DataSwingManager;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dispose.DisposeUtils;
56
import org.gvsig.tools.dynform.DynFormFieldDefinition;
57
import org.gvsig.tools.dynform.JDynForm;
58

    
59
import org.gvsig.tools.dynform.JDynFormField;
60
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
61
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
62
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory.ScrolledComponent;
63
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
64
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
65
import org.gvsig.tools.dynobject.DynObject;
66
import org.gvsig.tools.dynobject.Tags;
67
import org.gvsig.tools.swing.api.ToolsSwingUtils;
68
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
69

    
70
@SuppressWarnings("UseSpecificCatch")
71
public class JDynFormFieldRelatedFeatures extends AbstractJDynFormField implements JDynFormField {
72

    
73
    private class FeaturesTableModel extends AbstractTableModel {
74

    
75
        private final List<Feature> features;
76
        private final List<String> columnNames;
77
        private final FeatureType featureType;
78

    
79
        public FeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
80
            this.features = features;
81
            this.columnNames = columnNames;
82
            this.featureType = featureType;
83
        }
84

    
85
        @Override
86
        public int getRowCount() {
87
            if (this.features == null) {
88
                return 0;
89
            }
90
            try {
91
                return this.features.size();
92
            } catch (Exception ex) {
93
                LOGGER.warn("Can't read row count from features.",ex);
94
                return 0;
95
            }
96
        }
97

    
98
        @Override
99
        public int getColumnCount() {
100
            if (this.features == null) {
101
                return 0;
102
            }
103
            return this.columnNames.size();
104
        }
105

    
106
        @Override
107
        public String getColumnName(int columnIndex) {
108
            if (this.features == null) {
109
                return "";
110
            }
111
            String attrName = this.columnNames.get(columnIndex);
112
            if (this.featureType == null) {
113
                return attrName;
114
            }
115
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
116
            if (attrdesc == null) {
117
                return "C" + columnIndex;
118
            }
119
            return attrdesc.getLocalizedShortLabel();
120
        }
121

    
122
        @Override
123
        public Class<?> getColumnClass(int columnIndex) {
124
            if (this.featureType == null) {
125
                return String.class;
126
            }
127
            String attrName = this.columnNames.get(columnIndex);
128
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
129
            if (attrdesc == null) {
130
                return String.class;
131
            }
132
            return attrdesc.getDataType().getDefaultClass();
133
        }
134

    
135
        @Override
136
        public boolean isCellEditable(int rowIndex, int columnIndex) {
137
            return false;
138
        }
139

    
140
        @Override
141
        public Object getValueAt(int rowIndex, int columnIndex) {
142
            if (this.features == null) {
143
                return null;
144
            }
145
            Feature feature = this.features.get(rowIndex);
146
            String attrName = this.columnNames.get(columnIndex);
147
            try {
148
                return feature.get(attrName);
149
            } catch (Throwable th) {
150
                return null;
151
            }
152
        }
153

    
154
        @Override
155
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
156

    
157
        }
158
    }
159

    
160
    private List<Feature> assignedValue = null;
161
    private List<Feature> value = null;
162

    
163
    private FeatureType featureType;
164
    private List<String> columnNames;
165

    
166
    private JTable tblFeatures = null;
167
    private JButton btnEdit = null;
168
//    private JButton btnNew = null;
169
    private Dimension preferredSize = null;
170

    
171
    public JDynFormFieldRelatedFeatures(
172
            DynFormSPIManager serviceManager,
173
            DynFormSPIManager.ComponentsFactory componentsFactory,
174
            JDynFormFieldFactory factory,
175
            DynFormFieldDefinition definition,
176
            Object value
177
    ) {
178
        super(serviceManager, componentsFactory, factory, definition, value);
179
        if (value != null) {
180
            this.assignedValue = (List<Feature>) value;
181
        }
182
    }
183

    
184
    @Override
185
    public void loadDefaultValuesFromTags(Tags tags) {
186
        super.loadDefaultValuesFromTags(tags);
187
        int width = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, 100);
188
        int height = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, -1);
189
        if (height > 100) {
190
            this.preferredSize = new Dimension(width, height);
191
        }
192
        this.setReadOnly(true);
193
    }
194

    
195
    @Override
196
    public List<Feature> getAssignedValue() {
197
        return this.assignedValue;
198
    }
199

    
200
    public void initComponentIfNeed() {
201
        if (this.contents == null) {
202
            this.initComponent();
203
        }
204
    }
205

    
206
    private RelatedFeatures getRelatedFeatures() {
207
        RelatedFeatures relatedFeatures = (RelatedFeatures) ToolsLocator.
208
                getComplementsManager().get(
209
                        RelatedFeatures.COMPLEMENT_MANE,
210
                        this.getDefinition()
211
                );
212
        return relatedFeatures;
213
    }
214

    
215
    private StoresRepository getStoresRepository() {
216
        JDynForm.DynFormContext context = this.getForm().getContext();
217
        if (!(context instanceof FeaturesFormContext)) {
218
            return null;
219
        }
220
        StoresRepository repository = ((FeaturesFormContext) context).getStoresRepository();
221
        return repository;
222
    }
223

    
224
    @Override
225
    public void initComponent() {
226
        JPanel panel = new JPanel();
227
        try {
228
            ComponentsFactory components = this.getComponentsFactory();
229
            ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null);
230

    
231
            if (this.preferredSize != null) {
232
                comps.getScrollPane().setPreferredSize(this.preferredSize);
233
            } else {
234
                comps.getScrollPane().setPreferredSize(ToolsSwingUtils.rowscols2dimension(5,60));
235
            }
236
            this.tblFeatures = comps.getComponent();
237
            this.btnEdit = components.getJButton(this.getDefinition(), "Edit");
238
            if (this.btnEdit == null) {
239
                this.btnEdit = new JButton();
240
            }
241
            ToolsSwingUtils.configurePickersButton(
242
                    btnEdit, 
243
                    "_See_related_item", 
244
                    "picker-foreingkey-showform", 
245
                    (ActionEvent ae) -> {doEdit();},
246
                    null
247
            );
248
            
249
//            this.btnNew = components.getJButton(this.getDefinition(), "New");
250
//            if (this.btnNew == null) {
251
//                this.btnNew = new JButton();
252
//            }
253
//            this.initButton(this.btnNew, "Create new item", "new.png");
254
//            this.btnNew.addActionListener(new ActionListener() {
255
//                @Override
256
//                public void actionPerformed(ActionEvent ae) {
257
//                    doNew();
258
//                }
259
//            });
260
            if (!components.containsComponents(this.getDefinition())) {
261
                panel.setLayout(new BorderLayout());
262
                panel.add(comps.getScrollPane(), BorderLayout.CENTER);
263
                JPanel panelButtons = new JPanel();
264
                panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 1));
265
//                panelButtons.add(this.btnNew);
266
                panelButtons.add(this.btnEdit);
267
                panel.add(panelButtons, BorderLayout.SOUTH);
268
            }
269
            this.contents = panel;
270
            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
271
            if (relatedFeatures == null) {
272
                this.problemIndicator().set("Unable to locate the related table.");
273
                return;
274
            }
275
            RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
276
            context.setStoresRepository(this.getStoresRepository());
277
            try {
278
                FeatureStore store = relatedFeatures.getFeatureStore(context);
279
                if (store == null) {
280
                    this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
281
                    return;
282
                }
283
                this.columnNames = relatedFeatures.getColumns(context);
284
                this.featureType = store.getDefaultFeatureType();
285
            } finally {
286
                DisposeUtils.disposeQuietly(context);
287
            }
288
            this.tblFeatures.setModel(
289
                    new FeaturesTableModel(this.featureType, this.columnNames, this.assignedValue)
290
            );
291
        } catch (Throwable th) {
292
            LOGGER.warn("Can't initialize components of '" + this.getName() + "'.", th);
293
        }
294
        this.setReadOnly(true);
295
    }
296

    
297
    private void doEdit() {
298
        if (this.value == null) {
299
            return;
300
        }
301
        int selectedRow = this.tblFeatures.getSelectedRow();
302
        if (selectedRow < 0) {
303
            return;
304
        }
305
        try {
306
            this.btnEdit.setEnabled(false);
307
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
308

    
309
            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
310
            if (relatedFeatures == null) {
311
                this.problemIndicator().set("Unable to locate the related table.");
312
                return;
313
            }
314
            RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
315
            context.setStoresRepository(this.getStoresRepository());
316
            FeatureStore store = relatedFeatures.getFeatureStore(context);
317
            if (store == null) {
318
                this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
319
                return;
320
            }
321
            Feature f = this.value.get(selectedRow);
322
            FeatureQuery query = relatedFeatures.getUniqueKeyQuery(
323
                    context, 
324
                    relatedFeatures.getUniqueKey(context, f)
325
            );
326
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
327
            form.setQuery(query);
328
            form.showForm(WindowManager.MODE.WINDOW);
329

    
330
        } catch (Exception ex) {
331
            LOGGER.warn("Can't show linked form", ex);
332
        } finally {
333
            this.btnEdit.setEnabled(true);
334
        }
335
    }
336

    
337
//    private void doNew() {
338
//        if (this.value == null) {
339
//            return;
340
//        }
341
//        try {
342
//            this.btnNew.setEnabled(false);
343
//            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
344
//
345
//            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
346
//            if (relatedFeatures == null) {
347
//                this.problemIndicator().set("Unable to locate the related table.");
348
//                return;
349
//            }
350
//            RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
351
//            context.setStoresRepository(this.getStoresRepository());
352
//            FeatureStore store = relatedFeatures.getFeatureStore(context);
353
//            if (store == null) {
354
//                this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
355
//                return;
356
//            }
357
//            String myPkName = ... 
358
//            String myPkValue = ...
359
//            FeatureQuery query = relatedFeatures.getForeingKeyQuery(
360
//                    context, 
361
//                    myPkName
362
//            );
363
//            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
364
//            form.setQuery(query);
365
//            form.getFormset().fireEvent(ACTION_NEW,null);          
366
//            JDynForm jDynForm = form.getFormset().getForm();
367
//            jDynForm.setValue(myPkName, myPkValue);
368
//            
369
//            form.showForm(WindowManager.MODE.WINDOW);
370
//
371
//        } catch (Exception ex) {
372
//            LOGGER.warn("Can't show linked form", ex);
373
//        } finally {
374
//            this.btnEdit.setEnabled(true);
375
//        }
376
//    }
377

    
378

    
379
    @Override
380
    public void setValue(Object value) {
381
        initComponentIfNeed();
382
        if (value == null) {
383
            this.clear();
384
            return;
385
        }
386
        this.value = (List<Feature>) value;
387
        this.tblFeatures.setModel(
388
                new FeaturesTableModel(this.featureType, this.columnNames, this.value)
389
        );
390
    }
391

    
392
    @Override
393
    public Object getValue() {
394
        return this.value;
395
    }
396

    
397
    @Override
398
    public void fetch(DynObject container) {
399
    }
400

    
401
    @Override
402
    public boolean hasValidValue() {
403
        return true;
404
    }
405

    
406
    @Override
407
    public void clear() {
408
        initComponentIfNeed();
409
        this.value = null;
410
        this.tblFeatures.setModel(
411
                new FeaturesTableModel(this.featureType, this.columnNames, null)
412
        );
413
    }
414

    
415
    @Override
416
    public boolean isModified() {
417
        return false;
418
    }
419

    
420
    @Override
421
    public String toHTML() {
422
        try {
423
            TableModel model = this.tblFeatures.getModel();
424
            StringBuilder builder = new StringBuilder();
425

    
426
            builder.append("<table style=\"border: none;\" cellspacing=\"2\" cellpadding=\"0\">\n");
427
            builder.append("<tr>\n");
428
            for (int col = 0; col < model.getColumnCount(); col++) {
429
                builder.append("<td style=\"white-space:nowrap;\">\n<i>");
430
                builder.append(StringEscapeUtils.escapeHtml3(model.getColumnName(col)));
431
                builder.append("</i></td>\n");
432
            }
433
            builder.append("</tr>\n");
434
            for (int row = 0; row < model.getRowCount(); row++) {
435
                builder.append("<tr>\n");
436
                for (int col = 0; col < model.getColumnCount(); col++) {
437
                    builder.append("<td style=\"white-space:nowrap;\">\n");
438
                    builder.append(StringEscapeUtils.escapeHtml3(Objects.toString(model.getValueAt(row, col),"#ERROR#")));
439
                    builder.append("</td>\n");
440
                }
441
                builder.append("</tr>\n");
442
            }
443
            builder.append("</table>\n");
444
            return builder.toString();
445
        } catch(Throwable t) {
446
            LOGGER.warn("Can't get HTML from features table.",t);
447
            return null;
448
        }
449
    }
450
    
451

    
452
}