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 |
/**
|
---|---|
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 |
if (this.features == null) { |
92 |
return 0; |
93 |
} |
94 |
return this.columnNames.size(); |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public String getColumnName(int columnIndex) { |
99 |
if (this.features == null) { |
100 |
return ""; |
101 |
} |
102 |
String attrName = this.columnNames.get(columnIndex); |
103 |
if (this.featureType == null) { |
104 |
return attrName;
|
105 |
} |
106 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
107 |
if (attrdesc == null) { |
108 |
return "C" + columnIndex; |
109 |
} |
110 |
return attrdesc.getLocalizedShortLabel();
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public Class<?> getColumnClass(int columnIndex) { |
115 |
if (this.featureType == null) { |
116 |
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 |
|
157 |
private JTable tblFeatures = null; |
158 |
private JButton btnEdit = null; |
159 |
// private JButton btnNew = null;
|
160 |
private Dimension preferredSize = null; |
161 |
|
162 |
public JDynFormFieldRelatedFeatures(
|
163 |
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 |
} |
173 |
} |
174 |
|
175 |
@Override
|
176 |
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 |
if (height > 100) { |
181 |
this.preferredSize = new Dimension(width, height); |
182 |
} |
183 |
this.setReadOnly(true); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public List<Feature> getAssignedValue() { |
188 |
return this.assignedValue; |
189 |
} |
190 |
|
191 |
public void initComponentIfNeed() { |
192 |
if (this.contents == null) { |
193 |
this.initComponent();
|
194 |
} |
195 |
} |
196 |
|
197 |
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 |
@Override
|
216 |
public void initComponent() { |
217 |
JPanel panel = new JPanel(); |
218 |
try {
|
219 |
ComponentsFactory components = this.getComponentsFactory();
|
220 |
ScrolledComponent<JTable> comps = components.getJTable(this.getDefinition(), null); |
221 |
|
222 |
if (this.preferredSize != null) { |
223 |
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 |
} |
230 |
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 |
// 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 |
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 |
// panelButtons.add(this.btnNew);
|
254 |
panelButtons.add(this.btnEdit);
|
255 |
panel.add(panelButtons, BorderLayout.SOUTH);
|
256 |
} |
257 |
this.contents = panel;
|
258 |
final RelatedFeatures relatedFeatures = this.getRelatedFeatures(); |
259 |
if (relatedFeatures == null) { |
260 |
this.problemIndicator().set("Unable to locate the related table."); |
261 |
return;
|
262 |
} |
263 |
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 |
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 |
} |
282 |
this.setReadOnly(true); |
283 |
} |
284 |
|
285 |
private void doEdit() { |
286 |
if (this.value == null) { |
287 |
return;
|
288 |
} |
289 |
int selectedRow = this.tblFeatures.getSelectedRow(); |
290 |
if (selectedRow < 0) { |
291 |
return;
|
292 |
} |
293 |
try {
|
294 |
this.btnEdit.setEnabled(false); |
295 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
296 |
|
297 |
final RelatedFeatures relatedFeatures = this.getRelatedFeatures(); |
298 |
if (relatedFeatures == null) { |
299 |
this.problemIndicator().set("Unable to locate the related table."); |
300 |
return;
|
301 |
} |
302 |
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 |
return;
|
308 |
} |
309 |
Feature f = this.value.get(selectedRow);
|
310 |
FeatureQuery query = relatedFeatures.getUniqueKeyQuery( |
311 |
context, |
312 |
relatedFeatures.getUniqueKey(context, f) |
313 |
); |
314 |
JFeaturesForm form = dataSwingManager.createJFeaturesForm(store); |
315 |
form.setQuery(query); |
316 |
form.showForm(WindowManager.MODE.WINDOW); |
317 |
|
318 |
} catch (Exception ex) { |
319 |
LOGGER.warn("Can't show linked form", ex);
|
320 |
} finally {
|
321 |
this.btnEdit.setEnabled(true); |
322 |
} |
323 |
} |
324 |
|
325 |
// 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 |
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 |
} |
372 |
if (button.getText().trim().equals("...")) { |
373 |
button.setText("");
|
374 |
} |
375 |
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 |
} |
383 |
|
384 |
@Override
|
385 |
public void setValue(Object value) { |
386 |
initComponentIfNeed(); |
387 |
if (value == null) { |
388 |
this.clear();
|
389 |
return;
|
390 |
} |
391 |
this.value = (List<Feature>) value; |
392 |
this.tblFeatures.setModel(
|
393 |
new FeaturesTableModel(this.featureType, this.columnNames, this.value) |
394 |
); |
395 |
} |
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 |
initComponentIfNeed(); |
414 |
this.value = null; |
415 |
this.tblFeatures.setModel(
|
416 |
new FeaturesTableModel(this.featureType, this.columnNames, null) |
417 |
); |
418 |
} |
419 |
} |