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 / linkedentity / JDynFormFieldFeatureLink.java @ 44338

History | View | Annotate | Download (11.4 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.linkedentity;
24

    
25
import java.awt.Cursor;
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.net.URL;
31
import java.util.Formatter;
32
import javax.swing.BorderFactory;
33
import javax.swing.Icon;
34
import javax.swing.ImageIcon;
35
import javax.swing.JButton;
36
import javax.swing.JComponent;
37
import javax.swing.JPanel;
38
import javax.swing.JTextField;
39
import org.apache.commons.lang3.StringUtils;
40
import org.gvsig.featureform.swing.JFeaturesForm;
41
import org.gvsig.fmap.dal.DALLocator;
42
import org.gvsig.fmap.dal.DataManager;
43
import org.gvsig.fmap.dal.feature.Feature;
44
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
45
import org.gvsig.fmap.dal.feature.FeatureQuery;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.swing.DALSwingLocator;
48
import org.gvsig.fmap.dal.swing.DataSwingManager;
49
import org.gvsig.tools.dynform.DynFormFieldDefinition;
50

    
51
import org.gvsig.tools.dynform.JDynFormField;
52
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
53
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
54
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
55
import org.gvsig.tools.dynobject.DynObject;
56
import org.gvsig.tools.service.spi.ServiceManager;
57
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
58

    
59
public class JDynFormFieldFeatureLink extends AbstractJDynFormField implements JDynFormField {
60

    
61
    private static final String DAL_code = "DAL.code";
62
    private static final String DAL_foreingTable = "DAL.foreingTable";
63
    private static final String DAL_foreingCode = "DAL.foreingCode";
64
    private static final String DAL_foreingDescriptionMask = "DAL.foreingDescriptionMask";
65
    private static final String DAL_foreingDescriptionFields = "DAL.foreingDescriptionFields";
66
    
67
    private DynObject assignedValue = null;
68
    private DynObject value = null;
69
//    private JTextField txtCode = null;
70
    private JTextField txtDescription = null;
71
    private JButton btnLink = null;
72
    private JButton btnUnlink = null;
73
    private JButton btnEdit = null;
74

    
75
    private String codeFieldName = null;
76
    private String foreingTableName;
77
    private String foreingCodeName;
78
    private String foreingDescriptionMask = null;
79
    private String foreingDescriptionFieldNames[] = null;
80

    
81
    public JDynFormFieldFeatureLink(
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
    @Override
100
    public void initComponent() {
101
        this.foreingDescriptionFieldNames = null;
102
        this.codeFieldName = getTagValueAsString(DAL_code, null);
103
        this.foreingTableName = getTagValueAsString(DAL_foreingTable, null);
104
        this.foreingCodeName = getTagValueAsString(DAL_foreingCode, null);
105

    
106
        this.foreingDescriptionMask = getTagValueAsString(DAL_foreingDescriptionMask, null);
107
        String fieldNames = getTagValueAsString(DAL_foreingDescriptionFields, null);
108
        if (!StringUtils.isEmpty(fieldNames)) {
109
            this.foreingDescriptionFieldNames = fieldNames.split(",");
110
        }
111
        
112
        
113
        if (StringUtils.isEmpty(this.foreingDescriptionMask)
114
                && this.foreingDescriptionFieldNames != null
115
                && this.foreingDescriptionFieldNames.length == 1) {
116
            this.foreingDescriptionMask = "%s";
117
        }
118
//        this.txtCode = new JTextField();
119
        this.txtDescription = new JTextField();
120
        this.btnLink = createButton("Select item to link", "link.png");
121
        this.btnUnlink = createButton("Remove link", "unlink.png");
122
        this.btnEdit = createButton("View linked item", "edit.png");
123

    
124
        this.btnLink.addActionListener(new ActionListener() {
125
            @Override
126
            public void actionPerformed(ActionEvent ae) {
127
                doLink();
128
            }
129
        });
130
        this.btnUnlink.addActionListener(new ActionListener() {
131
            @Override
132
            public void actionPerformed(ActionEvent ae) {
133
                doUnlink();
134
            }
135
        });
136
        this.btnEdit.addActionListener(new ActionListener() {
137
            @Override
138
            public void actionPerformed(ActionEvent ae) {
139
                doEdit();
140
            }
141
        });
142

    
143
//        this.txtCode.setText("          ");
144
//        this.txtDescforeingCodeNameription.setText("                    ");
145
//        this.txtCode.setEditable(false);
146
        this.txtDescription.setEditable(false);
147

    
148
        if (StringUtils.isEmpty(codeFieldName) || StringUtils.isEmpty(foreingDescriptionMask)
149
                || StringUtils.isEmpty(foreingTableName) || StringUtils.isEmpty(foreingCodeName)
150
                || this.foreingDescriptionFieldNames == null) {
151
            this.btnEdit.setEnabled(false);
152
            this.btnLink.setEnabled(false);
153
            this.btnUnlink.setEnabled(false);
154
        }
155

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

    
195
        this.setValue(this.assignedValue);
196
    }
197

    
198
    private JButton createButton(final String tip, final String image) {
199
        URL url = this.getClass().getClassLoader().getResource("org/gvsig/featureform/swing/impl/" + image);
200
        Icon icon = new ImageIcon(url);
201
        JButton button = new JButton(icon);
202
        button.setBorder(BorderFactory.createEmptyBorder());
203
        button.setBorderPainted(false);
204
        button.setFocusPainted(false);
205
        button.setContentAreaFilled(false);
206
        button.setToolTipText(tip);
207
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
208
        return button;
209
    }
210

    
211
    private void doLink() {
212

    
213
    }
214

    
215
    private void doUnlink() {
216
        this.clear();
217
    }
218

    
219
    private void doEdit() {
220
        try {
221
            DataManager dalmanager = DALLocator.getDataManager();
222
            DataSwingManager daluimanager = DALSwingLocator.getSwingManager();
223
            Feature feature = ((FacadeOfAFeature)(this.value)).getFeature();
224
            FeatureStore store = feature.getStore();
225
            FeatureQuery query = store.createFeatureQuery();
226
            query.addFilter(dalmanager.createExpresion(foreingCodeName + " = " + feature.getString(foreingCodeName)));
227
            JFeaturesForm form = daluimanager.createJFeaturesForm(store);
228
            form.setQuery(query);
229
            form.showForm(WindowManager.MODE.WINDOW);
230
        } catch (Exception ex) {
231
            LOGGER.warn("Can't show linked form",ex);
232
        }
233
    }
234

    
235
    private String getDescription() {
236
        if (StringUtils.isEmpty(foreingDescriptionMask) || this.foreingDescriptionFieldNames == null) {
237
            return "";
238
        }
239
        Object[] values = new Object[this.foreingDescriptionFieldNames.length];
240
        for( int i=0; i<values.length; i++ ) {
241
            values[i] = this.value.getDynValue(this.foreingDescriptionFieldNames[i]);
242
        }
243
        Formatter f = new Formatter();
244
        try {
245
            String description = f.format(this.foreingDescriptionMask, values).toString();
246
            return description;
247
        } catch (Exception ex) {
248
            // TODO: log error
249
            return "";
250
        }
251
    }
252

    
253
    @Override
254
    public void setReadOnly(boolean readonly) {
255
        boolean enabled = !readonly;
256
        JComponent theJlabel = this.getJLabel();
257
        if( theJlabel !=null ) {
258
            theJlabel.setEnabled(enabled);
259
        }
260
        //        this.txtCode.setEnabled(enabled);
261
        if( this.txtDescription == null ) {
262
            return;
263
        }
264
        this.txtDescription.setEnabled(enabled);
265
        this.btnEdit.setEnabled(enabled);
266
        this.btnLink.setEnabled(enabled);
267
        this.btnUnlink.setEnabled(enabled);
268
    }
269

    
270
    @Override
271
    public void setValue(Object value) {
272
        if (value == null) {
273
            this.clear();
274
            return;
275
        }
276
        this.value = (DynObject) value;
277
//        this.txtCode.setText("");
278
        this.txtDescription.setText("");
279
        if (StringUtils.isEmpty(foreingCodeName) || StringUtils.isEmpty(foreingDescriptionMask)
280
                || this.foreingDescriptionFieldNames == null) {
281
        }
282
//        Object x = this.value.getDynValue(foreingCodeName);
283
//        if (x != null) {
284
//            this.txtCode.setText(x.toString());
285
//        }
286
        String description = this.getDescription();
287
        if (description != null) {
288
            this.txtDescription.setText(description);
289
        }
290
    }
291

    
292
    @Override
293
    public Object getValue() {
294
        return this.value;
295
    }
296

    
297
    @Override
298
    public void fetch(DynObject container) {
299
        if (StringUtils.isEmpty(this.codeFieldName)) {
300
            return;
301
        }
302
        DynObject value = (DynObject) this.getValue();
303
        if (value == null) {
304
            container.setDynValue(this.codeFieldName, null);
305
        } else {
306
            if (StringUtils.isEmpty(this.foreingCodeName)) {
307
                return;
308
            }
309
            container.setDynValue(this.codeFieldName, value.getDynValue(this.foreingCodeName));
310
        }
311
    }
312

    
313
    @Override
314
    public boolean hasValidValue() {
315
        return true;
316
    }
317

    
318
    @Override
319
    public void clear() {
320
        this.value = null;
321
//        this.txtCode.setText("");
322
        this.txtDescription.setText("");
323
    }
324
}