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 @ 44262

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.JList;
39
import javax.swing.JPanel;
40
import javax.swing.JTextField;
41
import javax.swing.ListModel;
42
import javax.swing.event.ListDataListener;
43
import javax.swing.text.JTextComponent;
44
import org.gvsig.featureform.swing.JFeaturesForm;
45
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext;
46
import org.gvsig.fmap.dal.StoresRepository;
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.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.dal.feature.ForeingKey;
53
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey;
54
import org.gvsig.fmap.dal.swing.DALSwingLocator;
55
import org.gvsig.fmap.dal.swing.DataSwingManager;
56
import org.gvsig.tools.ToolsLocator;
57
import org.gvsig.tools.dispose.DisposeUtils;
58
import org.gvsig.tools.dynform.DynFormFieldDefinition;
59
import org.gvsig.tools.dynform.JDynForm;
60

    
61
import org.gvsig.tools.dynform.JDynFormField;
62
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
63
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
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.swing.api.ToolsSwingLocator;
68
import org.gvsig.tools.swing.api.windowmanager.Dialog;
69
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
70
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
71

    
72
@SuppressWarnings("UseSpecificCatch")
73
public class JDynFormFieldForeingKey extends AbstractJDynFormField implements JDynFormField {
74

    
75
    private Object assignedValue = null;
76
    private Object value = null;
77
    private JTextComponent txtDescription = null;
78
    private JButton btnLink = null;
79
    private JButton btnUnlink = null;
80
    private JButton btnEdit = null;
81

    
82
    public JDynFormFieldForeingKey(
83
            DynFormSPIManager serviceManager,
84
            DynFormSPIManager.ComponentsFactory componentsFactory,
85
            JDynFormFieldFactory factory,
86
            DynFormFieldDefinition definition,
87
            Object value
88
    ) {
89
        super(serviceManager, componentsFactory, factory, definition, value);
90
        if (value != null) {
91
            this.assignedValue = (DynObject) value;
92
        }
93
    }
94

    
95
    @Override
96
    public Object getAssignedValue() {
97
        return this.assignedValue;
98
    }
99

    
100
    public void initComponentIfNeed() {
101
        if (this.contents == null) {
102
            this.initComponent();
103
        }
104
    }
105

    
106
    @Override
107
    public void initComponent() {
108
        ComponentsFactory components = this.getComponentsFactory();
109
        this.txtDescription = components.getJTextField(this.getDefinition(), null);
110
        if( this.txtDescription==null ) {
111
            this.txtDescription = new JTextField();
112
        }
113
        this.btnLink = components.getJButton(this.getDefinition(), "Link");
114
        if( this.btnLink==null ) {
115
            this.btnLink = new JButton();
116
        }
117
        this.btnUnlink = components.getJButton(this.getDefinition(), "Unlink");
118
        if( this.btnUnlink==null ) {
119
            this.btnUnlink = new JButton();
120
        }
121
        this.btnEdit = components.getJButton(this.getDefinition(), "Edit");
122
        if( this.btnEdit==null ) {
123
            this.btnEdit = new JButton();
124
        }
125
        this.initButton(this.btnLink, "Select item to link", "link.png");
126
        this.initButton(this.btnUnlink, "Remove link", "unlink.png");
127
        this.initButton(this.btnEdit, "View linked item", "edit.png");
128

    
129
        this.btnLink.addActionListener(new ActionListener() {
130
            @Override
131
            public void actionPerformed(ActionEvent ae) {
132
                doLink();
133
            }
134
        });
135
        this.btnUnlink.addActionListener(new ActionListener() {
136
            @Override
137
            public void actionPerformed(ActionEvent ae) {
138
                doUnlink();
139
            }
140
        });
141
        this.btnEdit.addActionListener(new ActionListener() {
142
            @Override
143
            public void actionPerformed(ActionEvent ae) {
144
                doEdit();
145
            }
146
        });
147

    
148
        this.txtDescription.setText("                        ");
149
        this.txtDescription.setEditable(false);
150

    
151
        boolean enabled = !this.isReadOnly();
152
        this.btnEdit.setEnabled(enabled);
153
        this.btnLink.setEnabled(enabled);
154
        this.btnUnlink.setEnabled(enabled);
155

    
156
        JPanel panel = new JPanel();
157
        if( !components.containsJTextField(this.getDefinition(), null) ) {
158
            panel.setLayout(new GridBagLayout());
159
            GridBagConstraints c = new GridBagConstraints();
160
            c.fill = GridBagConstraints.HORIZONTAL;
161
            c.ipadx = 4;
162
            c.ipady = 1;
163
            c.gridx = 1;
164
            c.gridy = 0;
165
            c.weightx = 1;
166
            panel.add(this.txtDescription, c);
167
            c.fill = GridBagConstraints.NONE;
168
            c.ipadx = 4;
169
            c.ipady = 1;
170
            c.gridx = 2;
171
            c.gridy = 0;
172
            c.weightx = 0;
173
            panel.add(this.btnLink, c);
174
            c.fill = GridBagConstraints.NONE;
175
            c.ipadx = 4;
176
            c.ipady = 1;
177
            c.gridx = 3;
178
            c.gridy = 0;
179
            c.weightx = 0;
180
            panel.add(this.btnUnlink, c);
181
            c.fill = GridBagConstraints.NONE;
182
            c.ipadx = 4;
183
            c.ipady = 1;
184
            c.gridx = 4;
185
            c.gridy = 0;
186
            c.weightx = 0;
187
            panel.add(this.btnEdit, c);
188
        }
189
        this.contents = panel;
190
        this.setValue(this.assignedValue);
191

    
192
    }
193

    
194
    private JButton initButton(JButton button, final String tip, final String image) {
195
        if( button.getIcon()==null ) {
196
            URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image);
197
            Icon icon = new ImageIcon(url);
198
            button.setIcon(icon);
199
        }
200
        if( button.getText().trim().equals("...") ) {
201
            button.setText("");
202
        }
203
        button.setBorder(BorderFactory.createEmptyBorder());
204
        button.setBorderPainted(false);
205
        button.setFocusPainted(false);
206
        button.setContentAreaFilled(false);
207
        button.setToolTipText(tip);
208
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
209
        return button;
210
    }
211

    
212
    private ForeingKey getForeingKey() {
213
        JDynForm.DynFormContext context = this.getForm().getContext();
214
        if( !(context instanceof FeaturesFormContext) ) {
215
            return null;
216
        }
217
        FeatureType featureType = ((FeaturesFormContext)context).getFeatureType();
218
        if( featureType==null ) {
219
            return null;
220
        }
221
        FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName());
222
        if( attribute == null ) {
223
            return null;
224
        }
225
        ForeingKey foreingKey = attribute.getForeingKey();
226
        return foreingKey;
227
    }
228
    
229
    private void doLink() {
230
        final ForeingKey foreingKey = this.getForeingKey();
231
        if( foreingKey==null ) {
232
            return;
233
        }
234
        final ContextForeingKey context = foreingKey.createContext();
235
        try {
236
            final List<Feature> features = foreingKey.getFeatures(context);
237
            
238
            ListModel<String> model = new ListModel<String>() {
239
                @Override
240
                public int getSize() {
241
                    return features.size();
242
                }
243

    
244
                @Override
245
                public String getElementAt(int index) {
246
                    Feature feature = features.get(index);
247
                    ForeingKey foreingKey = getForeingKey();
248
                    return foreingKey.getLabel(context, feature);
249
                }
250

    
251
                @Override
252
                public void addListDataListener(ListDataListener l) {
253
                }
254

    
255
                @Override
256
                public void removeListDataListener(ListDataListener l) {
257
                }
258
            };
259
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
260

    
261
            final JList<String> jlist = new JList<>();
262
            jlist.setModel(model);
263
            jlist.setPreferredSize(new Dimension(350, 200));
264
            final Dialog dialog = winManager.createDialog(
265
                    jlist,
266
                    "Select",
267
                    "Select the '"+this.getLabel()+"'.", 
268
                    WindowManager_v2.BUTTONS_OK_CANCEL
269
            );
270
            dialog.addActionListener(new ActionListener() {
271
                @Override
272
                public void actionPerformed(ActionEvent e) {
273
                    if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
274
                        int n = jlist.getSelectedIndex();
275
                        if( n<0 ) {
276
                            return;
277
                        }
278
                        Feature feature = features.get(n);
279
                        if( feature!=null ) {
280
                            setValue(foreingKey.getCode(context, feature));
281
                        }
282
                    }
283
                }
284
            });
285
            dialog.show(WindowManager.MODE.DIALOG);
286

    
287
        } catch (Exception ex) {
288
            LOGGER.warn("Can't show selector", ex);
289
        } finally {
290
            DisposeUtils.disposeQuietly(context);
291
        }
292
    }
293

    
294
    private void doUnlink() {
295
        this.clear();
296
    }
297

    
298
    private void doEdit() {
299
        if (this.value == null) {
300
            return;
301
        }
302
        final ForeingKey foreingKey = this.getForeingKey();
303
        if( foreingKey==null ) {
304
            return;
305
        }
306
        final ContextForeingKey context = foreingKey.createContext();
307
        try {
308
            this.btnEdit.setEnabled(false);
309
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
310
            FeatureQuery query = foreingKey.getQuery(context, value);
311
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(
312
                    foreingKey.getFeatureStore(context)
313
            );
314
            form.setQuery(query);
315
            form.showForm(WindowManager.MODE.WINDOW);
316

    
317
        } catch (Exception ex) {
318
            LOGGER.warn("Can't show linked form", ex);
319
        } finally {
320
            this.btnEdit.setEnabled(true);
321
            DisposeUtils.disposeQuietly(context);
322
        }
323
    }
324

    
325
    private String getDescription() {
326
        if (this.value == null) {
327
            return null;
328
        }
329
        try {
330
            final ForeingKey foreingKey = this.getForeingKey();
331
            if( foreingKey==null ) {
332
                return null;
333
            }
334
            String description = foreingKey.getLabel(null, value);
335
            return description;
336
        } catch (Exception ex) {
337
        }
338
        return null;
339

    
340
    }
341
    
342
    @Override
343
    public void setReadOnly(boolean readonly) {
344
        initComponentIfNeed();
345
        boolean editable = !readonly;
346
        this.txtDescription.setEditable(false);
347
        this.btnEdit.setEnabled(true);
348
        this.btnLink.setEnabled(editable);
349
        this.btnUnlink.setEnabled(editable);
350
    }
351

    
352
    @Override
353
    public void setValue(Object value) {
354
        initComponentIfNeed();
355
        if (value == null) {
356
            this.clear();
357
            return;
358
        }
359
        this.value = value;
360
        this.txtDescription.setText(Objects.toString(this.getDescription(), ""));
361
    }
362

    
363
    @Override
364
    public Object getValue() {
365
        return this.value;
366
    }
367

    
368
    @Override
369
    public void fetch(DynObject container) {
370
    }
371

    
372
    @Override
373
    public boolean hasValidValue() {
374
        return true;
375
    }
376

    
377
    @Override
378
    public void clear() {
379
        initComponentIfNeed();
380
        this.value = null;
381
        this.txtDescription.setText("");
382
    }
383
}