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 @ 44458
History | View | Annotate | Download (15.3 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 | 44458 | jjdelcerro | // private JButton btnNew = null;
|
160 | 44253 | jjdelcerro | private Dimension preferredSize = null; |
161 | 44128 | jjdelcerro | |
162 | 44262 | jjdelcerro | public JDynFormFieldRelatedFeatures(
|
163 | 44253 | jjdelcerro | DynFormSPIManager serviceManager, |
164 | DynFormSPIManager.ComponentsFactory componentsFactory, |
||
165 | JDynFormFieldFactory factory, |
||
166 | DynFormFieldDefinition definition, |
||
167 | Object value
|
||
168 | ) { |
||
169 | super(serviceManager, componentsFactory, factory, definition, value);
|
||
170 | if (value != null) { |
||
171 | this.assignedValue = (List<Feature>) value; |
||
172 | 44128 | jjdelcerro | } |
173 | } |
||
174 | |||
175 | @Override
|
||
176 | 44253 | jjdelcerro | public void loadDefaultValuesFromTags(Tags tags) { |
177 | super.loadDefaultValuesFromTags(tags);
|
||
178 | int width = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, 100); |
||
179 | int height = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, -1); |
||
180 | 44262 | jjdelcerro | if (height > 100) { |
181 | 44253 | jjdelcerro | this.preferredSize = new Dimension(width, height); |
182 | } |
||
183 | 44290 | jjdelcerro | this.setReadOnly(true); |
184 | 44253 | jjdelcerro | } |
185 | 44262 | jjdelcerro | |
186 | 44253 | jjdelcerro | @Override
|
187 | public List<Feature> getAssignedValue() { |
||
188 | 44128 | jjdelcerro | return this.assignedValue; |
189 | } |
||
190 | |||
191 | 44253 | jjdelcerro | public void initComponentIfNeed() { |
192 | if (this.contents == null) { |
||
193 | this.initComponent();
|
||
194 | } |
||
195 | } |
||
196 | |||
197 | 44262 | jjdelcerro | private RelatedFeatures getRelatedFeatures() {
|
198 | RelatedFeatures relatedFeatures = (RelatedFeatures) ToolsLocator. |
||
199 | getComplementsManager().get( |
||
200 | RelatedFeatures.COMPLEMENT_MANE, |
||
201 | this.getDefinition()
|
||
202 | ); |
||
203 | return relatedFeatures;
|
||
204 | } |
||
205 | |||
206 | private StoresRepository getStoresRepository() {
|
||
207 | JDynForm.DynFormContext context = this.getForm().getContext();
|
||
208 | if (!(context instanceof FeaturesFormContext)) { |
||
209 | return null; |
||
210 | } |
||
211 | StoresRepository repository = ((FeaturesFormContext) context).getStoresRepository(); |
||
212 | return repository;
|
||
213 | } |
||
214 | |||
215 | 44128 | jjdelcerro | @Override
|
216 | public void initComponent() { |
||
217 | 44253 | jjdelcerro | JPanel panel = new JPanel(); |
218 | try {
|
||
219 | ComponentsFactory components = this.getComponentsFactory();
|
||
220 | ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null); |
||
221 | 44128 | jjdelcerro | |
222 | 44262 | jjdelcerro | if (this.preferredSize != null) { |
223 | 44253 | jjdelcerro | comps.getScrollPane().setPreferredSize(this.preferredSize);
|
224 | } |
||
225 | this.tblFeatures = comps.getComponent();
|
||
226 | this.btnEdit = components.getJButton(this.getDefinition(), "Edit"); |
||
227 | if (this.btnEdit == null) { |
||
228 | this.btnEdit = new JButton(); |
||
229 | 44128 | jjdelcerro | } |
230 | 44253 | jjdelcerro | this.initButton(this.btnEdit, "View selected item", "edit.png"); |
231 | this.btnEdit.addActionListener(new ActionListener() { |
||
232 | @Override
|
||
233 | public void actionPerformed(ActionEvent ae) { |
||
234 | doEdit(); |
||
235 | } |
||
236 | }); |
||
237 | 44458 | jjdelcerro | // this.btnNew = components.getJButton(this.getDefinition(), "New");
|
238 | // if (this.btnNew == null) {
|
||
239 | // this.btnNew = new JButton();
|
||
240 | // }
|
||
241 | // this.initButton(this.btnNew, "Create new item", "new.png");
|
||
242 | // this.btnNew.addActionListener(new ActionListener() {
|
||
243 | // @Override
|
||
244 | // public void actionPerformed(ActionEvent ae) {
|
||
245 | // doNew();
|
||
246 | // }
|
||
247 | // });
|
||
248 | 44253 | jjdelcerro | if (!components.containsComponents(this.getDefinition())) { |
249 | panel.setLayout(new BorderLayout()); |
||
250 | panel.add(comps.getScrollPane(), BorderLayout.CENTER);
|
||
251 | JPanel panelButtons = new JPanel(); |
||
252 | panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 1)); |
||
253 | 44458 | jjdelcerro | // panelButtons.add(this.btnNew);
|
254 | 44253 | jjdelcerro | panelButtons.add(this.btnEdit);
|
255 | panel.add(panelButtons, BorderLayout.SOUTH);
|
||
256 | 44128 | jjdelcerro | } |
257 | 44253 | jjdelcerro | this.contents = panel;
|
258 | 44262 | jjdelcerro | final RelatedFeatures relatedFeatures = this.getRelatedFeatures(); |
259 | if (relatedFeatures == null) { |
||
260 | this.problemIndicator().set("Unable to locate the related table."); |
||
261 | 44253 | jjdelcerro | return;
|
262 | } |
||
263 | 44262 | jjdelcerro | RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext(); |
264 | context.setStoresRepository(this.getStoresRepository());
|
||
265 | try {
|
||
266 | FeatureStore store = relatedFeatures.getFeatureStore(context); |
||
267 | if (store == null) { |
||
268 | this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'."); |
||
269 | return;
|
||
270 | } |
||
271 | this.columnNames = relatedFeatures.getColumns(context);
|
||
272 | this.featureType = store.getDefaultFeatureType();
|
||
273 | } finally {
|
||
274 | DisposeUtils.disposeQuietly(context); |
||
275 | } |
||
276 | 44253 | jjdelcerro | this.tblFeatures.setModel(
|
277 | new FeaturesTableModel(this.featureType, this.columnNames, this.assignedValue) |
||
278 | ); |
||
279 | } catch (Throwable th) { |
||
280 | LOGGER.warn("Can't initialize components of '" + this.getName() + "'.", th); |
||
281 | 44128 | jjdelcerro | } |
282 | 44290 | jjdelcerro | this.setReadOnly(true); |
283 | 44128 | jjdelcerro | } |
284 | |||
285 | 44253 | jjdelcerro | private void doEdit() { |
286 | if (this.value == null) { |
||
287 | return;
|
||
288 | } |
||
289 | int selectedRow = this.tblFeatures.getSelectedRow(); |
||
290 | 44262 | jjdelcerro | if (selectedRow < 0) { |
291 | 44253 | jjdelcerro | return;
|
292 | } |
||
293 | try {
|
||
294 | this.btnEdit.setEnabled(false); |
||
295 | DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
||
296 | 44128 | jjdelcerro | |
297 | 44262 | jjdelcerro | final RelatedFeatures relatedFeatures = this.getRelatedFeatures(); |
298 | if (relatedFeatures == null) { |
||
299 | this.problemIndicator().set("Unable to locate the related table."); |
||
300 | return;
|
||
301 | 44253 | jjdelcerro | } |
302 | 44262 | jjdelcerro | RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext(); |
303 | context.setStoresRepository(this.getStoresRepository());
|
||
304 | FeatureStore store = relatedFeatures.getFeatureStore(context); |
||
305 | if (store == null) { |
||
306 | this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'."); |
||
307 | 44253 | jjdelcerro | return;
|
308 | } |
||
309 | 44262 | jjdelcerro | Feature f = this.value.get(selectedRow);
|
310 | FeatureQuery query = relatedFeatures.getUniqueKeyQuery( |
||
311 | context, |
||
312 | relatedFeatures.getUniqueKey(context, f) |
||
313 | 44253 | jjdelcerro | ); |
314 | JFeaturesForm form = dataSwingManager.createJFeaturesForm(store); |
||
315 | 44128 | jjdelcerro | form.setQuery(query); |
316 | form.showForm(WindowManager.MODE.WINDOW); |
||
317 | 44253 | jjdelcerro | |
318 | 44128 | jjdelcerro | } catch (Exception ex) { |
319 | 44253 | jjdelcerro | LOGGER.warn("Can't show linked form", ex);
|
320 | } finally {
|
||
321 | this.btnEdit.setEnabled(true); |
||
322 | 44128 | jjdelcerro | } |
323 | } |
||
324 | |||
325 | 44458 | jjdelcerro | // private void doNew() {
|
326 | // if (this.value == null) {
|
||
327 | // return;
|
||
328 | // }
|
||
329 | // try {
|
||
330 | // this.btnNew.setEnabled(false);
|
||
331 | // DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
|
||
332 | //
|
||
333 | // final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
|
||
334 | // if (relatedFeatures == null) {
|
||
335 | // this.problemIndicator().set("Unable to locate the related table.");
|
||
336 | // return;
|
||
337 | // }
|
||
338 | // RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
|
||
339 | // context.setStoresRepository(this.getStoresRepository());
|
||
340 | // FeatureStore store = relatedFeatures.getFeatureStore(context);
|
||
341 | // if (store == null) {
|
||
342 | // this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
|
||
343 | // return;
|
||
344 | // }
|
||
345 | // String myPkName = ...
|
||
346 | // String myPkValue = ...
|
||
347 | // FeatureQuery query = relatedFeatures.getForeingKeyQuery(
|
||
348 | // context,
|
||
349 | // myPkName
|
||
350 | // );
|
||
351 | // JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
|
||
352 | // form.setQuery(query);
|
||
353 | // form.getFormset().fireEvent(ACTION_NEW,null);
|
||
354 | // JDynForm jDynForm = form.getFormset().getForm();
|
||
355 | // jDynForm.setValue(myPkName, myPkValue);
|
||
356 | //
|
||
357 | // form.showForm(WindowManager.MODE.WINDOW);
|
||
358 | //
|
||
359 | // } catch (Exception ex) {
|
||
360 | // LOGGER.warn("Can't show linked form", ex);
|
||
361 | // } finally {
|
||
362 | // this.btnEdit.setEnabled(true);
|
||
363 | // }
|
||
364 | // }
|
||
365 | |||
366 | 44253 | jjdelcerro | private JButton initButton(JButton button, final String tip, final String image) { |
367 | if (button.getIcon() == null) { |
||
368 | URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image); |
||
369 | Icon icon = new ImageIcon(url); |
||
370 | button.setIcon(icon); |
||
371 | 44128 | jjdelcerro | } |
372 | 44253 | jjdelcerro | if (button.getText().trim().equals("...")) { |
373 | button.setText("");
|
||
374 | 44128 | jjdelcerro | } |
375 | 44253 | jjdelcerro | button.setBorder(BorderFactory.createEmptyBorder());
|
376 | button.setBorderPainted(false);
|
||
377 | button.setFocusPainted(false);
|
||
378 | button.setContentAreaFilled(false);
|
||
379 | button.setToolTipText(tip); |
||
380 | button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
||
381 | return button;
|
||
382 | 44128 | jjdelcerro | } |
383 | |||
384 | @Override
|
||
385 | public void setValue(Object value) { |
||
386 | 44253 | jjdelcerro | initComponentIfNeed(); |
387 | 44128 | jjdelcerro | if (value == null) { |
388 | this.clear();
|
||
389 | return;
|
||
390 | } |
||
391 | 44253 | jjdelcerro | this.value = (List<Feature>) value; |
392 | this.tblFeatures.setModel(
|
||
393 | new FeaturesTableModel(this.featureType, this.columnNames, this.value) |
||
394 | ); |
||
395 | 44128 | jjdelcerro | } |
396 | |||
397 | @Override
|
||
398 | public Object getValue() { |
||
399 | return this.value; |
||
400 | } |
||
401 | |||
402 | @Override
|
||
403 | public void fetch(DynObject container) { |
||
404 | } |
||
405 | |||
406 | @Override
|
||
407 | public boolean hasValidValue() { |
||
408 | return true; |
||
409 | } |
||
410 | |||
411 | @Override
|
||
412 | public void clear() { |
||
413 | 44253 | jjdelcerro | initComponentIfNeed(); |
414 | 44128 | jjdelcerro | this.value = null; |
415 | 44253 | jjdelcerro | this.tblFeatures.setModel(
|
416 | new FeaturesTableModel(this.featureType, this.columnNames, null) |
||
417 | ); |
||
418 | 44128 | jjdelcerro | } |
419 | } |