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 / linkforeingkey / JDynFormFieldForeingKey.java @ 44488
History | View | Annotate | Download (13 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.linkforeingkey; |
24 |
|
25 |
import java.awt.Cursor; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
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.JComponent; |
39 |
import javax.swing.JList; |
40 |
import javax.swing.JPanel; |
41 |
import javax.swing.JTextField; |
42 |
import javax.swing.ListModel; |
43 |
import javax.swing.event.ListDataListener; |
44 |
import javax.swing.text.JTextComponent; |
45 |
import org.gvsig.featureform.swing.JFeaturesForm; |
46 |
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext; |
47 |
import org.gvsig.fmap.dal.feature.Feature; |
48 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
49 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
50 |
import org.gvsig.fmap.dal.feature.FeatureType; |
51 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
52 |
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey; |
53 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
54 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
55 |
import org.gvsig.tools.dispose.DisposeUtils; |
56 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
57 |
import org.gvsig.tools.dynform.JDynForm; |
58 |
|
59 |
import org.gvsig.tools.dynform.JDynFormField; |
60 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
61 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
62 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
63 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
64 |
import org.gvsig.tools.dynobject.DynObject; |
65 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
66 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
67 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
68 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
69 |
|
70 |
@SuppressWarnings("UseSpecificCatch") |
71 |
public class JDynFormFieldForeingKey extends AbstractJDynFormField implements JDynFormField { |
72 |
|
73 |
private Object assignedValue = null; |
74 |
private Object value = null; |
75 |
private JTextComponent txtDescription = null; |
76 |
private JButton btnLink = null; |
77 |
private JButton btnUnlink = null; |
78 |
private JButton btnEdit = null; |
79 |
|
80 |
public JDynFormFieldForeingKey(
|
81 |
DynFormSPIManager serviceManager, |
82 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
83 |
JDynFormFieldFactory factory, |
84 |
DynFormFieldDefinition definition, |
85 |
Object value
|
86 |
) { |
87 |
super(serviceManager, componentsFactory, factory, definition, value);
|
88 |
if (value != null) { |
89 |
this.assignedValue = (DynObject) value;
|
90 |
} |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public Object getAssignedValue() { |
95 |
return this.assignedValue; |
96 |
} |
97 |
|
98 |
public void initComponentIfNeed() { |
99 |
if (this.contents == null) { |
100 |
this.initComponent();
|
101 |
} |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void initComponent() { |
106 |
ComponentsFactory components = this.getComponentsFactory();
|
107 |
this.txtDescription = components.getJTextField(this.getDefinition(), null); |
108 |
if( this.txtDescription==null ) { |
109 |
this.txtDescription = new JTextField(); |
110 |
} |
111 |
this.btnLink = components.getJButton(this.getDefinition(), "Link"); |
112 |
if( this.btnLink==null ) { |
113 |
this.btnLink = new JButton(); |
114 |
} |
115 |
this.btnUnlink = components.getJButton(this.getDefinition(), "Unlink"); |
116 |
if( this.btnUnlink==null ) { |
117 |
this.btnUnlink = new JButton(); |
118 |
} |
119 |
this.btnEdit = components.getJButton(this.getDefinition(), "Edit"); |
120 |
if( this.btnEdit==null ) { |
121 |
this.btnEdit = new JButton(); |
122 |
} |
123 |
this.initButton(this.btnLink, "Select item to link", "link.png"); |
124 |
this.initButton(this.btnUnlink, "Remove link", "unlink.png"); |
125 |
this.initButton(this.btnEdit, "View linked item", "edit.png"); |
126 |
|
127 |
this.btnLink.addActionListener(new ActionListener() { |
128 |
@Override
|
129 |
public void actionPerformed(ActionEvent ae) { |
130 |
doLink(); |
131 |
} |
132 |
}); |
133 |
this.btnUnlink.addActionListener(new ActionListener() { |
134 |
@Override
|
135 |
public void actionPerformed(ActionEvent ae) { |
136 |
doUnlink(); |
137 |
} |
138 |
}); |
139 |
this.btnEdit.addActionListener(new ActionListener() { |
140 |
@Override
|
141 |
public void actionPerformed(ActionEvent ae) { |
142 |
doEdit(); |
143 |
} |
144 |
}); |
145 |
|
146 |
this.txtDescription.setText(" "); |
147 |
this.txtDescription.setEditable(false); |
148 |
|
149 |
boolean enabled = !this.isReadOnly(); |
150 |
this.btnEdit.setEnabled(enabled);
|
151 |
this.btnLink.setEnabled(enabled);
|
152 |
this.btnUnlink.setEnabled(enabled);
|
153 |
|
154 |
JPanel panel = new JPanel(); |
155 |
if( !components.containsJTextField(this.getDefinition(), null) ) { |
156 |
panel.setLayout(new GridBagLayout()); |
157 |
GridBagConstraints c = new GridBagConstraints(); |
158 |
c.fill = GridBagConstraints.HORIZONTAL;
|
159 |
c.ipadx = 4;
|
160 |
c.ipady = 1;
|
161 |
c.gridx = 1;
|
162 |
c.gridy = 0;
|
163 |
c.weightx = 1;
|
164 |
panel.add(this.txtDescription, c);
|
165 |
c.fill = GridBagConstraints.NONE;
|
166 |
c.ipadx = 4;
|
167 |
c.ipady = 1;
|
168 |
c.gridx = 2;
|
169 |
c.gridy = 0;
|
170 |
c.weightx = 0;
|
171 |
panel.add(this.btnLink, c);
|
172 |
c.fill = GridBagConstraints.NONE;
|
173 |
c.ipadx = 4;
|
174 |
c.ipady = 1;
|
175 |
c.gridx = 3;
|
176 |
c.gridy = 0;
|
177 |
c.weightx = 0;
|
178 |
panel.add(this.btnUnlink, c);
|
179 |
c.fill = GridBagConstraints.NONE;
|
180 |
c.ipadx = 4;
|
181 |
c.ipady = 1;
|
182 |
c.gridx = 4;
|
183 |
c.gridy = 0;
|
184 |
c.weightx = 0;
|
185 |
panel.add(this.btnEdit, c);
|
186 |
} |
187 |
this.contents = panel;
|
188 |
this.setValue(this.assignedValue); |
189 |
|
190 |
} |
191 |
|
192 |
private JButton initButton(JButton button, final String tip, final String image) { |
193 |
if( button.getIcon()==null ) { |
194 |
URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image); |
195 |
Icon icon = new ImageIcon(url); |
196 |
button.setIcon(icon); |
197 |
} |
198 |
if( button.getText().trim().equals("...") ) { |
199 |
button.setText("");
|
200 |
} |
201 |
button.setBorder(BorderFactory.createEmptyBorder());
|
202 |
button.setBorderPainted(false);
|
203 |
button.setFocusPainted(false);
|
204 |
button.setContentAreaFilled(false);
|
205 |
button.setToolTipText(tip); |
206 |
button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
207 |
return button;
|
208 |
} |
209 |
|
210 |
private ForeingKey getForeingKey() {
|
211 |
JDynForm.DynFormContext context = this.getForm().getContext();
|
212 |
if( !(context instanceof FeaturesFormContext) ) { |
213 |
return null; |
214 |
} |
215 |
FeatureType featureType = ((FeaturesFormContext)context).getFeatureType(); |
216 |
if( featureType==null ) { |
217 |
return null; |
218 |
} |
219 |
FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName());
|
220 |
if( attribute == null ) { |
221 |
return null; |
222 |
} |
223 |
ForeingKey foreingKey = attribute.getForeingKey(); |
224 |
return foreingKey;
|
225 |
} |
226 |
|
227 |
private void doLink() { |
228 |
final ForeingKey foreingKey = this.getForeingKey(); |
229 |
if( foreingKey==null ) { |
230 |
return;
|
231 |
} |
232 |
final ContextForeingKey context = foreingKey.createContext();
|
233 |
try {
|
234 |
final List<Feature> features = foreingKey.getFeatures(context); |
235 |
|
236 |
ListModel<String> model = new ListModel<String>() { |
237 |
@Override
|
238 |
public int getSize() { |
239 |
return features.size();
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
public String getElementAt(int index) { |
244 |
Feature feature = features.get(index); |
245 |
ForeingKey foreingKey = getForeingKey(); |
246 |
return foreingKey.getLabel(context, feature);
|
247 |
} |
248 |
|
249 |
@Override
|
250 |
public void addListDataListener(ListDataListener l) { |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void removeListDataListener(ListDataListener l) { |
255 |
} |
256 |
}; |
257 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
258 |
|
259 |
final JList<String> jlist = new JList<>(); |
260 |
jlist.setModel(model); |
261 |
jlist.setPreferredSize(new Dimension(350, 200)); |
262 |
final Dialog dialog = winManager.createDialog( |
263 |
jlist, |
264 |
"Select",
|
265 |
"Select the '"+this.getLabel()+"'.", |
266 |
WindowManager_v2.BUTTONS_OK_CANCEL |
267 |
); |
268 |
dialog.addActionListener(new ActionListener() { |
269 |
@Override
|
270 |
public void actionPerformed(ActionEvent e) { |
271 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
272 |
int n = jlist.getSelectedIndex();
|
273 |
if( n<0 ) { |
274 |
return;
|
275 |
} |
276 |
Feature feature = features.get(n); |
277 |
if( feature!=null ) { |
278 |
setValue(foreingKey.getCode(context, feature)); |
279 |
} |
280 |
} |
281 |
} |
282 |
}); |
283 |
dialog.show(WindowManager.MODE.DIALOG); |
284 |
|
285 |
} catch (Exception ex) { |
286 |
LOGGER.warn("Can't show selector", ex);
|
287 |
} finally {
|
288 |
DisposeUtils.disposeQuietly(context); |
289 |
} |
290 |
} |
291 |
|
292 |
private void doUnlink() { |
293 |
this.clear();
|
294 |
} |
295 |
|
296 |
private void doEdit() { |
297 |
if (this.value == null) { |
298 |
return;
|
299 |
} |
300 |
final ForeingKey foreingKey = this.getForeingKey(); |
301 |
if( foreingKey==null ) { |
302 |
return;
|
303 |
} |
304 |
final ContextForeingKey context = foreingKey.createContext();
|
305 |
try {
|
306 |
this.btnEdit.setEnabled(false); |
307 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
308 |
FeatureQuery query = foreingKey.getQuery(context, value); |
309 |
JFeaturesForm form = dataSwingManager.createJFeaturesForm( |
310 |
foreingKey.getFeatureStore(context) |
311 |
); |
312 |
form.setQuery(query); |
313 |
form.showForm(WindowManager.MODE.WINDOW); |
314 |
|
315 |
} catch (Exception ex) { |
316 |
LOGGER.warn("Can't show linked form", ex);
|
317 |
} finally {
|
318 |
this.btnEdit.setEnabled(true); |
319 |
DisposeUtils.disposeQuietly(context); |
320 |
} |
321 |
} |
322 |
|
323 |
private String getDescription() { |
324 |
if (this.value == null) { |
325 |
return null; |
326 |
} |
327 |
try {
|
328 |
final ForeingKey foreingKey = this.getForeingKey(); |
329 |
if( foreingKey==null ) { |
330 |
return null; |
331 |
} |
332 |
String description = foreingKey.getLabel(null, value); |
333 |
return description;
|
334 |
} catch (Exception ex) { |
335 |
} |
336 |
return null; |
337 |
|
338 |
} |
339 |
|
340 |
@Override
|
341 |
public void setReadOnly(boolean readonly) { |
342 |
initComponentIfNeed(); |
343 |
this.readOnly = readonly;
|
344 |
boolean editable = !readonly;
|
345 |
JComponent jlabel = this.getJLabel(); |
346 |
if( jlabel !=null ) { |
347 |
jlabel.setEnabled(editable); |
348 |
} |
349 |
this.txtDescription.setEditable(false); |
350 |
this.btnEdit.setEnabled(true); |
351 |
this.btnLink.setEnabled(editable);
|
352 |
this.btnUnlink.setEnabled(editable);
|
353 |
} |
354 |
|
355 |
@Override
|
356 |
public void setValue(Object value) { |
357 |
initComponentIfNeed(); |
358 |
if (value == null) { |
359 |
this.clear();
|
360 |
return;
|
361 |
} |
362 |
this.value = value;
|
363 |
this.txtDescription.setText(Objects.toString(this.getDescription(), "")); |
364 |
} |
365 |
|
366 |
@Override
|
367 |
public Object getValue() { |
368 |
return this.value; |
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.txtDescription.setText(""); |
381 |
} |
382 |
} |