Statistics
| Revision:

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 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.linkforeingkey;
24 44128 jjdelcerro
25
import java.awt.Cursor;
26 44253 jjdelcerro
import java.awt.Dimension;
27 44128 jjdelcerro
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 44253 jjdelcerro
import java.util.List;
33
import java.util.Objects;
34 44128 jjdelcerro
import javax.swing.BorderFactory;
35
import javax.swing.Icon;
36
import javax.swing.ImageIcon;
37
import javax.swing.JButton;
38 44307 jjdelcerro
import javax.swing.JComponent;
39 44253 jjdelcerro
import javax.swing.JList;
40 44128 jjdelcerro
import javax.swing.JPanel;
41
import javax.swing.JTextField;
42 44253 jjdelcerro
import javax.swing.ListModel;
43
import javax.swing.event.ListDataListener;
44
import javax.swing.text.JTextComponent;
45 44128 jjdelcerro
import org.gvsig.featureform.swing.JFeaturesForm;
46 44253 jjdelcerro
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
47 44128 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
48 44262 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
49 44128 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
50 44262 jjdelcerro
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 44128 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
54
import org.gvsig.fmap.dal.swing.DataSwingManager;
55 44262 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
56 44253 jjdelcerro
import org.gvsig.tools.dynform.DynFormFieldDefinition;
57 44262 jjdelcerro
import org.gvsig.tools.dynform.JDynForm;
58 44128 jjdelcerro
59
import org.gvsig.tools.dynform.JDynFormField;
60 44253 jjdelcerro
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
61
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
62 44128 jjdelcerro
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
63 44253 jjdelcerro
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
64 44128 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
65 44253 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
66
import org.gvsig.tools.swing.api.windowmanager.Dialog;
67 44128 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
68 44253 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
69 44128 jjdelcerro
70 44253 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
71
public class JDynFormFieldForeingKey extends AbstractJDynFormField implements JDynFormField {
72 44128 jjdelcerro
73 44253 jjdelcerro
    private Object assignedValue = null;
74
    private Object value = null;
75
    private JTextComponent txtDescription = null;
76 44128 jjdelcerro
    private JButton btnLink = null;
77
    private JButton btnUnlink = null;
78
    private JButton btnEdit = null;
79
80 44253 jjdelcerro
    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 44128 jjdelcerro
        }
91
    }
92
93
    @Override
94
    public Object getAssignedValue() {
95
        return this.assignedValue;
96
    }
97
98 44253 jjdelcerro
    public void initComponentIfNeed() {
99
        if (this.contents == null) {
100
            this.initComponent();
101
        }
102
    }
103
104 44128 jjdelcerro
    @Override
105
    public void initComponent() {
106 44253 jjdelcerro
        ComponentsFactory components = this.getComponentsFactory();
107
        this.txtDescription = components.getJTextField(this.getDefinition(), null);
108
        if( this.txtDescription==null ) {
109
            this.txtDescription = new JTextField();
110 44128 jjdelcerro
        }
111 44253 jjdelcerro
        this.btnLink = components.getJButton(this.getDefinition(), "Link");
112
        if( this.btnLink==null ) {
113
            this.btnLink = new JButton();
114 44128 jjdelcerro
        }
115 44253 jjdelcerro
        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 44128 jjdelcerro
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 44253 jjdelcerro
        this.txtDescription.setText("                        ");
147 44128 jjdelcerro
        this.txtDescription.setEditable(false);
148
149 44253 jjdelcerro
        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 44128 jjdelcerro
        }
187 44253 jjdelcerro
        this.contents = panel;
188
        this.setValue(this.assignedValue);
189 44128 jjdelcerro
190
    }
191
192 44253 jjdelcerro
    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 44128 jjdelcerro
        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 44262 jjdelcerro
    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 44128 jjdelcerro
    private void doLink() {
228 44262 jjdelcerro
        final ForeingKey foreingKey = this.getForeingKey();
229
        if( foreingKey==null ) {
230
            return;
231
        }
232
        final ContextForeingKey context = foreingKey.createContext();
233 44253 jjdelcerro
        try {
234 44262 jjdelcerro
            final List<Feature> features = foreingKey.getFeatures(context);
235
236 44253 jjdelcerro
            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 44262 jjdelcerro
                    ForeingKey foreingKey = getForeingKey();
246
                    return foreingKey.getLabel(context, feature);
247 44253 jjdelcerro
                }
248
249
                @Override
250
                public void addListDataListener(ListDataListener l) {
251
                }
252
253
                @Override
254
                public void removeListDataListener(ListDataListener l) {
255
                }
256
            };
257 44262 jjdelcerro
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
258
259 44253 jjdelcerro
            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 44262 jjdelcerro
                            setValue(foreingKey.getCode(context, feature));
279 44253 jjdelcerro
                        }
280
                    }
281
                }
282
            });
283
            dialog.show(WindowManager.MODE.DIALOG);
284
285
        } catch (Exception ex) {
286
            LOGGER.warn("Can't show selector", ex);
287 44262 jjdelcerro
        } finally {
288
            DisposeUtils.disposeQuietly(context);
289 44253 jjdelcerro
        }
290 44128 jjdelcerro
    }
291
292
    private void doUnlink() {
293
        this.clear();
294
    }
295
296
    private void doEdit() {
297 44253 jjdelcerro
        if (this.value == null) {
298
            return;
299
        }
300 44262 jjdelcerro
        final ForeingKey foreingKey = this.getForeingKey();
301
        if( foreingKey==null ) {
302
            return;
303
        }
304
        final ContextForeingKey context = foreingKey.createContext();
305 44128 jjdelcerro
        try {
306 44253 jjdelcerro
            this.btnEdit.setEnabled(false);
307
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
308 44262 jjdelcerro
            FeatureQuery query = foreingKey.getQuery(context, value);
309
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(
310
                    foreingKey.getFeatureStore(context)
311 44253 jjdelcerro
            );
312 44128 jjdelcerro
            form.setQuery(query);
313
            form.showForm(WindowManager.MODE.WINDOW);
314 44253 jjdelcerro
315 44128 jjdelcerro
        } catch (Exception ex) {
316 44253 jjdelcerro
            LOGGER.warn("Can't show linked form", ex);
317
        } finally {
318
            this.btnEdit.setEnabled(true);
319 44262 jjdelcerro
            DisposeUtils.disposeQuietly(context);
320 44128 jjdelcerro
        }
321
    }
322
323
    private String getDescription() {
324 44253 jjdelcerro
        if (this.value == null) {
325
            return null;
326 44128 jjdelcerro
        }
327
        try {
328 44262 jjdelcerro
            final ForeingKey foreingKey = this.getForeingKey();
329
            if( foreingKey==null ) {
330
                return null;
331 44253 jjdelcerro
            }
332 44262 jjdelcerro
            String description = foreingKey.getLabel(null, value);
333
            return description;
334 44128 jjdelcerro
        } catch (Exception ex) {
335
        }
336 44253 jjdelcerro
        return null;
337
338 44128 jjdelcerro
    }
339 44253 jjdelcerro
340 44128 jjdelcerro
    @Override
341
    public void setReadOnly(boolean readonly) {
342 44253 jjdelcerro
        initComponentIfNeed();
343 44488 jjdelcerro
        this.readOnly = readonly;
344 44253 jjdelcerro
        boolean editable = !readonly;
345 44307 jjdelcerro
        JComponent jlabel = this.getJLabel();
346
        if( jlabel !=null ) {
347
            jlabel.setEnabled(editable);
348
        }
349 44253 jjdelcerro
        this.txtDescription.setEditable(false);
350
        this.btnEdit.setEnabled(true);
351
        this.btnLink.setEnabled(editable);
352
        this.btnUnlink.setEnabled(editable);
353 44128 jjdelcerro
    }
354
355
    @Override
356
    public void setValue(Object value) {
357 44253 jjdelcerro
        initComponentIfNeed();
358 44128 jjdelcerro
        if (value == null) {
359
            this.clear();
360
            return;
361
        }
362 44253 jjdelcerro
        this.value = value;
363
        this.txtDescription.setText(Objects.toString(this.getDescription(), ""));
364 44128 jjdelcerro
    }
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 44253 jjdelcerro
        initComponentIfNeed();
379 44128 jjdelcerro
        this.value = null;
380
        this.txtDescription.setText("");
381
    }
382
}