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