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

History | View | Annotate | Download (14.7 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.net.URL;
31
import java.util.List;
32
import java.util.Objects;
33
import javax.swing.BorderFactory;
34
import javax.swing.Icon;
35
import javax.swing.ImageIcon;
36
import javax.swing.JButton;
37
import javax.swing.JComponent;
38
import javax.swing.JList;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
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.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.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
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.dynformfield.AbstractJDynFormField;
64
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
65
import org.gvsig.tools.dynobject.DynObject;
66
import org.gvsig.tools.swing.api.ToolsSwingLocator;
67
import org.gvsig.tools.swing.api.windowmanager.Dialog;
68
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
69
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
70

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

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

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

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

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

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

    
128
        this.btnLink.addActionListener((ActionEvent ae) -> {
129
            doLink();
130
        });
131
        this.btnUnlink.addActionListener((ActionEvent ae) -> {
132
            doUnlink();
133
        });
134
        this.btnEdit.addActionListener((ActionEvent ae) -> {
135
            doEdit();
136
        });
137

    
138
        this.txtDescription.setText("                        ");
139
        this.txtDescription.setEditable(false);
140

    
141
        boolean enabled = !this.isReadOnly();
142
        this.btnEdit.setEnabled(enabled);
143
        this.btnLink.setEnabled(enabled);
144
        this.btnUnlink.setEnabled(enabled);
145

    
146
        JPanel panel = new JPanel();
147
        if( !components.containsJTextField(this.getDefinition(), null) ) {
148
            panel.setLayout(new GridBagLayout());
149
            GridBagConstraints c = new GridBagConstraints();
150
            c.fill = GridBagConstraints.HORIZONTAL;
151
            c.ipadx = 4;
152
            c.ipady = 1;
153
            c.gridx = 1;
154
            c.gridy = 0;
155
            c.weightx = 1;
156
            panel.add(this.txtDescription, c);
157
            c.fill = GridBagConstraints.NONE;
158
            c.ipadx = 4;
159
            c.ipady = 1;
160
            c.gridx = 2;
161
            c.gridy = 0;
162
            c.weightx = 0;
163
            panel.add(this.btnLink, c);
164
            c.fill = GridBagConstraints.NONE;
165
            c.ipadx = 4;
166
            c.ipady = 1;
167
            c.gridx = 3;
168
            c.gridy = 0;
169
            c.weightx = 0;
170
            panel.add(this.btnUnlink, c);
171
            c.fill = GridBagConstraints.NONE;
172
            c.ipadx = 4;
173
            c.ipady = 1;
174
            c.gridx = 4;
175
            c.gridy = 0;
176
            c.weightx = 0;
177
            panel.add(this.btnEdit, c);
178
        }
179
        this.contents = panel;
180
        this.setValue(this.assignedValue);
181

    
182
    }
183

    
184
    private JButton initButton(JButton button, final String tip, final String image) {
185
        if( button.getIcon()==null ) {
186
            URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image);
187
            Icon icon = new ImageIcon(url);
188
            button.setIcon(icon);
189
        }
190
        if( button.getText().trim().equals("...") ) {
191
            button.setText("");
192
        }
193
        button.setBorder(BorderFactory.createEmptyBorder());
194
        button.setBorderPainted(false);
195
        button.setFocusPainted(false);
196
        button.setContentAreaFilled(false);
197
        button.setToolTipText(tip);
198
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
199
        return button;
200
    }
201

    
202
    private ForeingKey getForeingKey() {
203
        JDynForm.DynFormContext context = this.getForm().getContext();
204
        if( !(context instanceof FeaturesFormContext) ) {
205
            return null;
206
        }
207
        FeatureType featureType = ((FeaturesFormContext)context).getFeatureType();
208
        if( featureType==null ) {
209
            return null;
210
        }
211
        FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName());
212
        if( attribute == null ) {
213
            return null;
214
        }
215
        ForeingKey foreingKey = attribute.getForeingKey();
216
        return foreingKey;
217
    }
218
    
219
    private void doLink0() {
220
        final ForeingKey foreingKey = this.getForeingKey();
221
        if( foreingKey==null ) {
222
            return;
223
        }
224
        final ContextForeingKey context = foreingKey.createContext();
225
        try {
226
            final List<Feature> features = foreingKey.getFeatures(context);
227
            
228
            ListModel<String> model = new ListModel<String>() {
229
                @Override
230
                public int getSize() {
231
                    return features.size();
232
                }
233

    
234
                @Override
235
                public String getElementAt(int index) {
236
                    Feature feature = features.get(index);
237
                    ForeingKey foreingKey = getForeingKey();
238
                    return foreingKey.getLabel(context, feature);
239
                }
240

    
241
                @Override
242
                public void addListDataListener(ListDataListener l) {
243
                }
244

    
245
                @Override
246
                public void removeListDataListener(ListDataListener l) {
247
                }
248
            };
249
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
250

    
251
            final JList<String> jlist = new JList<>();
252
            JScrollPane jscroll = new JScrollPane(jlist);
253
            jscroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
254
            jscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
255
            jlist.setModel(model);
256
            jlist.setPreferredSize(new Dimension(350, 200));
257
            final Dialog dialog = winManager.createDialog(
258
                    jscroll,
259
                    "Select",
260
                    "Select the '"+this.getLabel()+"'.", 
261
                    WindowManager_v2.BUTTONS_OK_CANCEL
262
            );
263
            dialog.addActionListener((ActionEvent e) -> {
264
                if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
265
                    int n = jlist.getSelectedIndex();
266
                    if( n<0 ) {
267
                        return;
268
                    }
269
                    Feature feature = features.get(n);
270
                    if( feature!=null ) {
271
                        setEditedValue(foreingKey.getCode(context, feature));
272
                        fireFieldChangedEvent();
273
                    }
274
                }
275
            });
276
            dialog.show(WindowManager.MODE.DIALOG);
277

    
278
        } catch (Exception ex) {
279
            LOGGER.warn("Can't show selector", ex);
280
        } finally {
281
            DisposeUtils.disposeQuietly(context);
282
        }
283
    }
284
    
285
    private void doLink() {
286
        final ForeingKey foreingKey = this.getForeingKey();
287
        if( foreingKey==null ) {
288
            return;
289
        }
290
        final ContextForeingKey context = foreingKey.createContext();
291
        try {
292
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
293
            final FeatureStoreSearchPanel searchPanel = dataSwingManager.createFeatureStoreSearchPanel(
294
                    foreingKey.getFeatureStore(context)
295
            );
296
            searchPanel.setFilterOnlyMode(true);
297
            WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
298

    
299
            final Dialog dialog = winManager.createDialog(
300
                    searchPanel.asJComponent(),
301
                    "Select",
302
                    "Select the '"+this.getLabel()+"'.", 
303
                    WindowManager_v2.BUTTONS_OK_CANCEL
304
            );
305
            dialog.addActionListener((ActionEvent e) -> {
306
                if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
307
                    Feature feature = searchPanel.getLastSelectedFeature();
308
                    if( feature!=null ) {
309
                        setEditedValue(foreingKey.getCode(context, feature));
310
                        fireFieldChangedEvent();
311
                    }
312
                }
313
            });
314
            dialog.show(WindowManager.MODE.DIALOG);
315

    
316
        } catch (Exception ex) {
317
            LOGGER.warn("Can't show selector", ex);
318
        } finally {
319
            DisposeUtils.disposeQuietly(context);
320
        }
321
    }
322

    
323
    private void doUnlink() {
324
        this.setEditedValue(null);
325
        fireFieldChangedEvent();
326
    }
327

    
328
    private void doEdit() {
329
        if (this.value == null) {
330
            return;
331
        }
332
        final ForeingKey foreingKey = this.getForeingKey();
333
        if( foreingKey==null ) {
334
            return;
335
        }
336
        final ContextForeingKey context = foreingKey.createContext();
337
        try {
338
            this.btnEdit.setEnabled(false);
339
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
340
            FeatureQuery query = foreingKey.getQuery(context, value);
341
            JFeaturesForm form = dataSwingManager.createJFeaturesForm(
342
                    foreingKey.getFeatureStore(context)
343
            );
344
            form.setQuery(query);
345
            form.showForm(WindowManager.MODE.WINDOW);
346

    
347
        } catch (Exception ex) {
348
            LOGGER.warn("Can't show linked form", ex);
349
        } finally {
350
            this.btnEdit.setEnabled(true);
351
            DisposeUtils.disposeQuietly(context);
352
        }
353
    }
354

    
355
    private String getDescription() {
356
        if (this.value == null) {
357
            return null;
358
        }
359
        try {
360
            final ForeingKey foreingKey = this.getForeingKey();
361
            if( foreingKey==null ) {
362
                return null;
363
            }
364
            String description = foreingKey.getLabel(null, value);
365
            return description;
366
        } catch (Exception ex) {
367
        }
368
        return null;
369

    
370
    }
371
    
372
    @Override
373
    public void setReadOnly(boolean readonly) {
374
        initComponentIfNeed();
375
        this.readOnly = readonly;
376
        boolean editable = !readonly;
377
        JComponent theJLabel = this.getJLabel();
378
        if( theJLabel !=null ) {
379
            theJLabel.setEnabled(editable);
380
        }
381
        this.txtDescription.setEditable(false);
382
        this.btnEdit.setEnabled(true);
383
        this.btnLink.setEnabled(editable);
384
        this.btnUnlink.setEnabled(editable);
385
    }
386

    
387
    @Override
388
    public void setValue(Object value) {
389
        setEditedValue(value);
390
        this.assignedValue = value;
391
       
392
    }
393

    
394
    public void setEditedValue(Object value) {
395
        initComponentIfNeed();
396
        if (value == null) {
397
            this.value = null;
398
            this.txtDescription.setText("");
399
            return;
400
        }
401
        this.value = value;
402
        this.txtDescription.setText(Objects.toString(this.getDescription(), ""));
403
       
404
    }
405
    
406
    @Override
407
    public Object getValue() {
408
        return this.value;
409
    }
410

    
411
    @Override
412
    public boolean hasValidValue() {
413
        return true;
414
    }
415

    
416
    @Override
417
    public void clear() {
418
        this.setValue(null);
419
    }
420

    
421
    @Override
422
    public boolean isModified() {
423
        return !Objects.equals(this.value, this.getAssignedValue());
424
    }
425
    
426
    
427
}