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

History | View | Annotate | Download (13.7 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
            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
}