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 44128 jjdelcerro
/**
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 44253 jjdelcerro
package org.gvsig.featureform.swing.impl.dynformfield.features;
24 44128 jjdelcerro
25 44253 jjdelcerro
import java.awt.BorderLayout;
26 44128 jjdelcerro
import java.awt.Cursor;
27 44253 jjdelcerro
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29 44128 jjdelcerro
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.net.URL;
32 44253 jjdelcerro
import java.util.List;
33 44128 jjdelcerro
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 44253 jjdelcerro
import javax.swing.JTable;
39
import javax.swing.table.AbstractTableModel;
40 44128 jjdelcerro
import org.gvsig.featureform.swing.JFeaturesForm;
41 44253 jjdelcerro
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
42 44262 jjdelcerro
import org.gvsig.fmap.dal.StoresRepository;
43
import org.gvsig.fmap.dal.complements.RelatedFeatures;
44 44128 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
45 44253 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46 44128 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48 44253 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
49 44128 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
50
import org.gvsig.fmap.dal.swing.DataSwingManager;
51 44262 jjdelcerro
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.dispose.DisposeUtils;
53 44253 jjdelcerro
import org.gvsig.tools.dynform.DynFormFieldDefinition;
54 44262 jjdelcerro
import org.gvsig.tools.dynform.JDynForm;
55 44128 jjdelcerro
56
import org.gvsig.tools.dynform.JDynFormField;
57 44253 jjdelcerro
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 44128 jjdelcerro
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
61 44253 jjdelcerro
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
62 44128 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
63 44253 jjdelcerro
import org.gvsig.tools.dynobject.Tags;
64 44128 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
65
66 44253 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
67 44262 jjdelcerro
public class JDynFormFieldRelatedFeatures extends AbstractJDynFormField implements JDynFormField {
68 44128 jjdelcerro
69 44253 jjdelcerro
    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 44277 jjdelcerro
            if (this.features == null) {
92
                return 0;
93
            }
94 44253 jjdelcerro
            return this.columnNames.size();
95
        }
96
97
        @Override
98
        public String getColumnName(int columnIndex) {
99 44277 jjdelcerro
            if (this.features == null) {
100
                return "";
101
            }
102 44253 jjdelcerro
            String attrName = this.columnNames.get(columnIndex);
103 44262 jjdelcerro
            if (this.featureType == null) {
104 44253 jjdelcerro
                return attrName;
105
            }
106
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
107
            if (attrdesc == null) {
108
                return "C" + columnIndex;
109
            }
110 44338 jjdelcerro
            return attrdesc.getLocalizedShortLabel();
111 44253 jjdelcerro
        }
112
113
        @Override
114
        public Class<?> getColumnClass(int columnIndex) {
115 44262 jjdelcerro
            if (this.featureType == null) {
116 44253 jjdelcerro
                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 44262 jjdelcerro
157 44253 jjdelcerro
    private JTable tblFeatures = null;
158 44128 jjdelcerro
    private JButton btnEdit = null;
159 44253 jjdelcerro
    private Dimension preferredSize = null;
160 44128 jjdelcerro
161 44262 jjdelcerro
    public JDynFormFieldRelatedFeatures(
162 44253 jjdelcerro
            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 44128 jjdelcerro
        }
172
    }
173
174
    @Override
175 44253 jjdelcerro
    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 44262 jjdelcerro
        if (height > 100) {
180 44253 jjdelcerro
            this.preferredSize = new Dimension(width, height);
181
        }
182 44290 jjdelcerro
        this.setReadOnly(true);
183 44253 jjdelcerro
    }
184 44262 jjdelcerro
185 44253 jjdelcerro
    @Override
186
    public List<Feature> getAssignedValue() {
187 44128 jjdelcerro
        return this.assignedValue;
188
    }
189
190 44253 jjdelcerro
    public void initComponentIfNeed() {
191
        if (this.contents == null) {
192
            this.initComponent();
193
        }
194
    }
195
196 44262 jjdelcerro
    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 44128 jjdelcerro
    @Override
215
    public void initComponent() {
216 44253 jjdelcerro
        JPanel panel = new JPanel();
217
        try {
218
            ComponentsFactory components = this.getComponentsFactory();
219
            ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null);
220 44128 jjdelcerro
221 44262 jjdelcerro
            if (this.preferredSize != null) {
222 44253 jjdelcerro
                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 44128 jjdelcerro
            }
229 44253 jjdelcerro
            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 44128 jjdelcerro
            }
244 44253 jjdelcerro
            this.contents = panel;
245 44262 jjdelcerro
            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
246
            if (relatedFeatures == null) {
247
                this.problemIndicator().set("Unable to locate the related table.");
248 44253 jjdelcerro
                return;
249
            }
250 44262 jjdelcerro
            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 44253 jjdelcerro
            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 44128 jjdelcerro
        }
269 44290 jjdelcerro
        this.setReadOnly(true);
270 44128 jjdelcerro
    }
271
272 44253 jjdelcerro
    private void doEdit() {
273
        if (this.value == null) {
274
            return;
275
        }
276
        int selectedRow = this.tblFeatures.getSelectedRow();
277 44262 jjdelcerro
        if (selectedRow < 0) {
278 44253 jjdelcerro
            return;
279
        }
280
        try {
281
            this.btnEdit.setEnabled(false);
282
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
283 44128 jjdelcerro
284 44262 jjdelcerro
            final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
285
            if (relatedFeatures == null) {
286
                this.problemIndicator().set("Unable to locate the related table.");
287
                return;
288 44253 jjdelcerro
            }
289 44262 jjdelcerro
            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 44253 jjdelcerro
                return;
295
            }
296 44262 jjdelcerro
            Feature f = this.value.get(selectedRow);
297
            FeatureQuery query = relatedFeatures.getUniqueKeyQuery(
298
                    context,
299
                    relatedFeatures.getUniqueKey(context, f)
300 44253 jjdelcerro
            );
301
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
302 44128 jjdelcerro
            form.setQuery(query);
303
            form.showForm(WindowManager.MODE.WINDOW);
304 44253 jjdelcerro
305 44128 jjdelcerro
        } catch (Exception ex) {
306 44253 jjdelcerro
            LOGGER.warn("Can't show linked form", ex);
307
        } finally {
308
            this.btnEdit.setEnabled(true);
309 44128 jjdelcerro
        }
310
    }
311
312 44253 jjdelcerro
    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 44128 jjdelcerro
        }
318 44253 jjdelcerro
        if (button.getText().trim().equals("...")) {
319
            button.setText("");
320 44128 jjdelcerro
        }
321 44253 jjdelcerro
        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 44128 jjdelcerro
    }
329
330
    @Override
331
    public void setValue(Object value) {
332 44253 jjdelcerro
        initComponentIfNeed();
333 44128 jjdelcerro
        if (value == null) {
334
            this.clear();
335
            return;
336
        }
337 44253 jjdelcerro
        this.value = (List<Feature>) value;
338
        this.tblFeatures.setModel(
339
                new FeaturesTableModel(this.featureType, this.columnNames, this.value)
340
        );
341 44128 jjdelcerro
    }
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 44253 jjdelcerro
        initComponentIfNeed();
360 44128 jjdelcerro
        this.value = null;
361 44253 jjdelcerro
        this.tblFeatures.setModel(
362
                new FeaturesTableModel(this.featureType, this.columnNames, null)
363
        );
364 44128 jjdelcerro
    }
365
}