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 @ 46917
History | View | Annotate | Download (16.6 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 java.util.Objects; |
34 |
import javax.swing.BorderFactory; |
35 |
import javax.swing.Icon; |
36 |
import javax.swing.ImageIcon; |
37 |
import javax.swing.JButton; |
38 |
import javax.swing.JPanel; |
39 |
import javax.swing.JScrollPane; |
40 |
import javax.swing.JTable; |
41 |
import javax.swing.table.AbstractTableModel; |
42 |
import javax.swing.table.TableModel; |
43 |
import org.apache.commons.text.StringEscapeUtils; |
44 |
import org.gvsig.featureform.swing.JFeaturesForm; |
45 |
import org.gvsig.featureform.swing.FeaturesFormContext; |
46 |
import org.gvsig.fmap.dal.StoresRepository; |
47 |
import org.gvsig.fmap.dal.complements.RelatedFeatures; |
48 |
import org.gvsig.fmap.dal.feature.Feature; |
49 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
50 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
51 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
52 |
import org.gvsig.fmap.dal.feature.FeatureType; |
53 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
54 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.dispose.DisposeUtils; |
57 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
58 |
import org.gvsig.tools.dynform.JDynForm; |
59 |
|
60 |
import org.gvsig.tools.dynform.JDynFormField; |
61 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
62 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
63 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory.ScrolledComponent; |
64 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
65 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
66 |
import org.gvsig.tools.dynobject.DynObject; |
67 |
import org.gvsig.tools.dynobject.Tags; |
68 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
69 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
70 |
|
71 |
@SuppressWarnings("UseSpecificCatch") |
72 |
public class JDynFormFieldRelatedFeatures extends AbstractJDynFormField implements JDynFormField { |
73 |
|
74 |
private class FeaturesTableModel extends AbstractTableModel { |
75 |
|
76 |
private final List<Feature> features; |
77 |
private final List<String> columnNames; |
78 |
private final FeatureType featureType; |
79 |
|
80 |
public FeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
81 |
this.features = features;
|
82 |
this.columnNames = columnNames;
|
83 |
this.featureType = featureType;
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public int getRowCount() { |
88 |
if (this.features == null) { |
89 |
return 0; |
90 |
} |
91 |
try {
|
92 |
return this.features.size(); |
93 |
} catch (Exception ex) { |
94 |
LOGGER.warn("Can't read row count from features.",ex);
|
95 |
return 0; |
96 |
} |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public int getColumnCount() { |
101 |
if (this.features == null) { |
102 |
return 0; |
103 |
} |
104 |
return this.columnNames.size(); |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public String getColumnName(int columnIndex) { |
109 |
if (this.features == null) { |
110 |
return ""; |
111 |
} |
112 |
String attrName = this.columnNames.get(columnIndex); |
113 |
if (this.featureType == null) { |
114 |
return attrName;
|
115 |
} |
116 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
117 |
if (attrdesc == null) { |
118 |
return "C" + columnIndex; |
119 |
} |
120 |
return attrdesc.getLocalizedShortLabel();
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public Class<?> getColumnClass(int columnIndex) { |
125 |
if (this.featureType == null) { |
126 |
return String.class; |
127 |
} |
128 |
String attrName = this.columnNames.get(columnIndex); |
129 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
130 |
if (attrdesc == null) { |
131 |
return String.class; |
132 |
} |
133 |
return attrdesc.getDataType().getDefaultClass();
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
138 |
return false; |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public Object getValueAt(int rowIndex, int columnIndex) { |
143 |
if (this.features == null) { |
144 |
return null; |
145 |
} |
146 |
Feature feature = this.features.get(rowIndex);
|
147 |
String attrName = this.columnNames.get(columnIndex); |
148 |
try {
|
149 |
return feature.get(attrName);
|
150 |
} catch (Throwable th) { |
151 |
return null; |
152 |
} |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
157 |
|
158 |
} |
159 |
} |
160 |
|
161 |
private List<Feature> assignedValue = null; |
162 |
private List<Feature> value = null; |
163 |
|
164 |
private FeatureType featureType;
|
165 |
private List<String> columnNames; |
166 |
|
167 |
private JTable tblFeatures = null; |
168 |
private JButton btnEdit = null; |
169 |
// private JButton btnNew = null;
|
170 |
private Dimension preferredSize = null; |
171 |
|
172 |
public JDynFormFieldRelatedFeatures(
|
173 |
DynFormSPIManager serviceManager, |
174 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
175 |
JDynFormFieldFactory factory, |
176 |
DynFormFieldDefinition definition, |
177 |
Object value
|
178 |
) { |
179 |
super(serviceManager, componentsFactory, factory, definition, value);
|
180 |
if (value != null) { |
181 |
this.assignedValue = (List<Feature>) value; |
182 |
} |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void loadDefaultValuesFromTags(Tags tags) { |
187 |
super.loadDefaultValuesFromTags(tags);
|
188 |
int width = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, 100); |
189 |
int height = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, -1); |
190 |
if (height > 100) { |
191 |
this.preferredSize = new Dimension(width, height); |
192 |
} |
193 |
this.setReadOnly(true); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public List<Feature> getAssignedValue() { |
198 |
return this.assignedValue; |
199 |
} |
200 |
|
201 |
public void initComponentIfNeed() { |
202 |
if (this.contents == null) { |
203 |
this.initComponent();
|
204 |
} |
205 |
} |
206 |
|
207 |
private RelatedFeatures getRelatedFeatures() {
|
208 |
RelatedFeatures relatedFeatures = (RelatedFeatures) ToolsLocator. |
209 |
getComplementsManager().get( |
210 |
RelatedFeatures.COMPLEMENT_MANE, |
211 |
this.getDefinition()
|
212 |
); |
213 |
return relatedFeatures;
|
214 |
} |
215 |
|
216 |
private StoresRepository getStoresRepository() {
|
217 |
JDynForm.DynFormContext context = this.getForm().getContext();
|
218 |
if (!(context instanceof FeaturesFormContext)) { |
219 |
return null; |
220 |
} |
221 |
StoresRepository repository = ((FeaturesFormContext) context).getStoresRepository(); |
222 |
return repository;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public void initComponent() { |
227 |
JPanel panel = new JPanel(); |
228 |
try {
|
229 |
ComponentsFactory components = this.getComponentsFactory();
|
230 |
ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null); |
231 |
|
232 |
JScrollPane scrollPane = comps.getScrollPane();
|
233 |
if(scrollPane != null){ |
234 |
if (this.preferredSize != null) { |
235 |
scrollPane.setPreferredSize(this.preferredSize);
|
236 |
} else {
|
237 |
scrollPane.setPreferredSize(ToolsSwingUtils.rowscols2dimension(5,60)); |
238 |
} |
239 |
} |
240 |
this.tblFeatures = comps.getComponent();
|
241 |
this.btnEdit = components.getJButton(this.getDefinition(), "Edit"); |
242 |
if (this.btnEdit == null) { |
243 |
this.btnEdit = new JButton(); |
244 |
} |
245 |
ToolsSwingUtils.configurePickersButton( |
246 |
btnEdit, |
247 |
"_See_related_item",
|
248 |
"picker-foreingkey-showform",
|
249 |
(ActionEvent ae) -> {doEdit();},
|
250 |
null
|
251 |
); |
252 |
|
253 |
// this.btnNew = components.getJButton(this.getDefinition(), "New");
|
254 |
// if (this.btnNew == null) {
|
255 |
// this.btnNew = new JButton();
|
256 |
// }
|
257 |
// this.initButton(this.btnNew, "Create new item", "new.png");
|
258 |
// this.btnNew.addActionListener(new ActionListener() {
|
259 |
// @Override
|
260 |
// public void actionPerformed(ActionEvent ae) {
|
261 |
// doNew();
|
262 |
// }
|
263 |
// });
|
264 |
if (!components.containsComponents(this.getDefinition())) { |
265 |
panel.setLayout(new BorderLayout()); |
266 |
panel.add(comps.getScrollPane(), BorderLayout.CENTER);
|
267 |
JPanel panelButtons = new JPanel(); |
268 |
panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 1)); |
269 |
// panelButtons.add(this.btnNew);
|
270 |
panelButtons.add(this.btnEdit);
|
271 |
panel.add(panelButtons, BorderLayout.SOUTH);
|
272 |
} |
273 |
this.contents = panel;
|
274 |
final RelatedFeatures relatedFeatures = this.getRelatedFeatures(); |
275 |
if (relatedFeatures == null) { |
276 |
this.problemIndicator().set("Unable to locate the related table."); |
277 |
return;
|
278 |
} |
279 |
RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext(); |
280 |
context.setStoresRepository(this.getStoresRepository());
|
281 |
try {
|
282 |
FeatureStore store = relatedFeatures.getFeatureStore(context); |
283 |
if (store == null) { |
284 |
this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'."); |
285 |
return;
|
286 |
} |
287 |
this.columnNames = relatedFeatures.getColumns(context);
|
288 |
this.featureType = store.getDefaultFeatureType();
|
289 |
} finally {
|
290 |
DisposeUtils.disposeQuietly(context); |
291 |
} |
292 |
this.tblFeatures.setModel(
|
293 |
new FeaturesTableModel(this.featureType, this.columnNames, this.assignedValue) |
294 |
); |
295 |
} catch (Throwable th) { |
296 |
LOGGER.warn("Can't initialize components of '" + this.getName() + "'.", th); |
297 |
} |
298 |
this.setReadOnly(true); |
299 |
} |
300 |
|
301 |
private void doEdit() { |
302 |
if (this.value == null) { |
303 |
return;
|
304 |
} |
305 |
int selectedRow = this.tblFeatures.getSelectedRow(); |
306 |
if (selectedRow < 0) { |
307 |
return;
|
308 |
} |
309 |
try {
|
310 |
this.btnEdit.setEnabled(false); |
311 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
312 |
|
313 |
final RelatedFeatures relatedFeatures = this.getRelatedFeatures(); |
314 |
if (relatedFeatures == null) { |
315 |
this.problemIndicator().set("Unable to locate the related table."); |
316 |
return;
|
317 |
} |
318 |
RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext(); |
319 |
context.setStoresRepository(this.getStoresRepository());
|
320 |
FeatureStore store = relatedFeatures.getFeatureStore(context); |
321 |
if (store == null) { |
322 |
this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'."); |
323 |
return;
|
324 |
} |
325 |
Feature f = this.value.get(selectedRow);
|
326 |
FeatureQuery query = relatedFeatures.getUniqueKeyQuery( |
327 |
context, |
328 |
relatedFeatures.getUniqueKey(context, f) |
329 |
); |
330 |
JFeaturesForm form = dataSwingManager.createJFeaturesForm(store); |
331 |
form.setQuery(query); |
332 |
form.showForm(WindowManager.MODE.WINDOW); |
333 |
|
334 |
} catch (Exception ex) { |
335 |
LOGGER.warn("Can't show linked form", ex);
|
336 |
} finally {
|
337 |
this.btnEdit.setEnabled(true); |
338 |
} |
339 |
} |
340 |
|
341 |
// private void doNew() {
|
342 |
// if (this.value == null) {
|
343 |
// return;
|
344 |
// }
|
345 |
// try {
|
346 |
// this.btnNew.setEnabled(false);
|
347 |
// DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
|
348 |
//
|
349 |
// final RelatedFeatures relatedFeatures = this.getRelatedFeatures();
|
350 |
// if (relatedFeatures == null) {
|
351 |
// this.problemIndicator().set("Unable to locate the related table.");
|
352 |
// return;
|
353 |
// }
|
354 |
// RelatedFeatures.ContextRelatedFeatures context = relatedFeatures.createContext();
|
355 |
// context.setStoresRepository(this.getStoresRepository());
|
356 |
// FeatureStore store = relatedFeatures.getFeatureStore(context);
|
357 |
// if (store == null) {
|
358 |
// this.problemIndicator().set("Unable to locate the related table '" + relatedFeatures.getTableName() + "'.");
|
359 |
// return;
|
360 |
// }
|
361 |
// String myPkName = ...
|
362 |
// String myPkValue = ...
|
363 |
// FeatureQuery query = relatedFeatures.getForeingKeyQuery(
|
364 |
// context,
|
365 |
// myPkName
|
366 |
// );
|
367 |
// JFeaturesForm form = dataSwingManager.createJFeaturesForm(store);
|
368 |
// form.setQuery(query);
|
369 |
// form.getFormset().fireEvent(ACTION_NEW,null);
|
370 |
// JDynForm jDynForm = form.getFormset().getForm();
|
371 |
// jDynForm.setValue(myPkName, myPkValue);
|
372 |
//
|
373 |
// form.showForm(WindowManager.MODE.WINDOW);
|
374 |
//
|
375 |
// } catch (Exception ex) {
|
376 |
// LOGGER.warn("Can't show linked form", ex);
|
377 |
// } finally {
|
378 |
// this.btnEdit.setEnabled(true);
|
379 |
// }
|
380 |
// }
|
381 |
|
382 |
|
383 |
@Override
|
384 |
public void setValue(Object value) { |
385 |
initComponentIfNeed(); |
386 |
if (value == null) { |
387 |
this.clear();
|
388 |
return;
|
389 |
} |
390 |
this.value = (List<Feature>) value; |
391 |
this.tblFeatures.setModel(
|
392 |
new FeaturesTableModel(this.featureType, this.columnNames, this.value) |
393 |
); |
394 |
} |
395 |
|
396 |
@Override
|
397 |
public Object getValue() { |
398 |
return this.value; |
399 |
} |
400 |
|
401 |
@Override
|
402 |
public void fetch(DynObject container) { |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public boolean hasValidValue() { |
407 |
return true; |
408 |
} |
409 |
|
410 |
@Override
|
411 |
public void clear() { |
412 |
initComponentIfNeed(); |
413 |
this.value = null; |
414 |
this.tblFeatures.setModel(
|
415 |
new FeaturesTableModel(this.featureType, this.columnNames, null) |
416 |
); |
417 |
} |
418 |
|
419 |
@Override
|
420 |
public boolean isModified() { |
421 |
return false; |
422 |
} |
423 |
|
424 |
@Override
|
425 |
public String toHTML() { |
426 |
try {
|
427 |
TableModel model = this.tblFeatures.getModel(); |
428 |
StringBuilder builder = new StringBuilder(); |
429 |
|
430 |
builder.append("<div>\n");
|
431 |
builder.append("<table style=\"border: none;\" cellspacing=\"2\" cellpadding=\"0\">\n");
|
432 |
builder.append("<tr>\n");
|
433 |
for (int col = 0; col < model.getColumnCount(); col++) { |
434 |
builder.append("<td style=\"white-space:nowrap;\">\n<i>");
|
435 |
builder.append(StringEscapeUtils.escapeHtml3(model.getColumnName(col))); |
436 |
builder.append("</i></td>\n");
|
437 |
} |
438 |
builder.append("</tr>\n");
|
439 |
for (int row = 0; row < model.getRowCount(); row++) { |
440 |
builder.append("<tr>\n");
|
441 |
for (int col = 0; col < model.getColumnCount(); col++) { |
442 |
builder.append("<td style=\"white-space:nowrap;\">");
|
443 |
builder.append(StringEscapeUtils.escapeHtml3(Objects.toString(model.getValueAt(row, col),"#ERROR#")));
|
444 |
builder.append("</td>\n");
|
445 |
} |
446 |
builder.append("</tr>\n");
|
447 |
} |
448 |
builder.append("</table>\n");
|
449 |
builder.append("</div>\n");
|
450 |
return builder.toString();
|
451 |
} catch(Throwable t) { |
452 |
LOGGER.warn("Can't get HTML from features table.",t);
|
453 |
return null; |
454 |
} |
455 |
} |
456 |
|
457 |
|
458 |
} |