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 @ 44338

History | View | Annotate | Download (13 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 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
            if (this.features == null) {
92
                return 0;
93
            }
94
            return this.columnNames.size();
95
        }
96

    
97
        @Override
98
        public String getColumnName(int columnIndex) {
99
            if (this.features == null) {
100
                return "";
101
            }
102
            String attrName = this.columnNames.get(columnIndex);
103
            if (this.featureType == null) {
104
                return attrName;
105
            }
106
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
107
            if (attrdesc == null) {
108
                return "C" + columnIndex;
109
            }
110
            return attrdesc.getLocalizedShortLabel();
111
        }
112

    
113
        @Override
114
        public Class<?> getColumnClass(int columnIndex) {
115
            if (this.featureType == null) {
116
                return String.class;
117
            }
118
            String attrName = this.columnNames.get(columnIndex);
119
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
120
            if (attrdesc == null) {
121
                return String.class;
122
            }
123
            return attrdesc.getDataType().getDefaultClass();
124
        }
125

    
126
        @Override
127
        public boolean isCellEditable(int rowIndex, int columnIndex) {
128
            return false;
129
        }
130

    
131
        @Override
132
        public Object getValueAt(int rowIndex, int columnIndex) {
133
            if (this.features == null) {
134
                return null;
135
            }
136
            Feature feature = this.features.get(rowIndex);
137
            String attrName = this.columnNames.get(columnIndex);
138
            try {
139
                return feature.get(attrName);
140
            } catch (Throwable th) {
141
                return null;
142
            }
143
        }
144

    
145
        @Override
146
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
147

    
148
        }
149
    }
150

    
151
    private List<Feature> assignedValue = null;
152
    private List<Feature> value = null;
153

    
154
    private FeatureType featureType;
155
    private List<String> columnNames;
156

    
157
    private JTable tblFeatures = null;
158
    private JButton btnEdit = null;
159
    private Dimension preferredSize = null;
160

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

    
174
    @Override
175
    public void loadDefaultValuesFromTags(Tags tags) {
176
        super.loadDefaultValuesFromTags(tags);
177
        int width = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, 100);
178
        int height = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, -1);
179
        if (height > 100) {
180
            this.preferredSize = new Dimension(width, height);
181
        }
182
        this.setReadOnly(true);
183
    }
184

    
185
    @Override
186
    public List<Feature> getAssignedValue() {
187
        return this.assignedValue;
188
    }
189

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

    
196
    private RelatedFeatures getRelatedFeatures() {
197
        RelatedFeatures relatedFeatures = (RelatedFeatures) ToolsLocator.
198
                getComplementsManager().get(
199
                        RelatedFeatures.COMPLEMENT_MANE,
200
                        this.getDefinition()
201
                );
202
        return relatedFeatures;
203
    }
204

    
205
    private StoresRepository getStoresRepository() {
206
        JDynForm.DynFormContext context = this.getForm().getContext();
207
        if (!(context instanceof FeaturesFormContext)) {
208
            return null;
209
        }
210
        StoresRepository repository = ((FeaturesFormContext) context).getStoresRepository();
211
        return repository;
212
    }
213

    
214
    @Override
215
    public void initComponent() {
216
        JPanel panel = new JPanel();
217
        try {
218
            ComponentsFactory components = this.getComponentsFactory();
219
            ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null);
220

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

    
272
    private void doEdit() {
273
        if (this.value == null) {
274
            return;
275
        }
276
        int selectedRow = this.tblFeatures.getSelectedRow();
277
        if (selectedRow < 0) {
278
            return;
279
        }
280
        try {
281
            this.btnEdit.setEnabled(false);
282
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
283

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

    
305
        } catch (Exception ex) {
306
            LOGGER.warn("Can't show linked form", ex);
307
        } finally {
308
            this.btnEdit.setEnabled(true);
309
        }
310
    }
311

    
312
    private JButton initButton(JButton button, final String tip, final String image) {
313
        if (button.getIcon() == null) {
314
            URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image);
315
            Icon icon = new ImageIcon(url);
316
            button.setIcon(icon);
317
        }
318
        if (button.getText().trim().equals("...")) {
319
            button.setText("");
320
        }
321
        button.setBorder(BorderFactory.createEmptyBorder());
322
        button.setBorderPainted(false);
323
        button.setFocusPainted(false);
324
        button.setContentAreaFilled(false);
325
        button.setToolTipText(tip);
326
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
327
        return button;
328
    }
329

    
330
    @Override
331
    public void setValue(Object value) {
332
        initComponentIfNeed();
333
        if (value == null) {
334
            this.clear();
335
            return;
336
        }
337
        this.value = (List<Feature>) value;
338
        this.tblFeatures.setModel(
339
                new FeaturesTableModel(this.featureType, this.columnNames, this.value)
340
        );
341
    }
342

    
343
    @Override
344
    public Object getValue() {
345
        return this.value;
346
    }
347

    
348
    @Override
349
    public void fetch(DynObject container) {
350
    }
351

    
352
    @Override
353
    public boolean hasValidValue() {
354
        return true;
355
    }
356

    
357
    @Override
358
    public void clear() {
359
        initComponentIfNeed();
360
        this.value = null;
361
        this.tblFeatures.setModel(
362
                new FeaturesTableModel(this.featureType, this.columnNames, null)
363
        );
364
    }
365
}