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

History | View | Annotate | Download (11 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;
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.JPanel;
37
import javax.swing.JTextField;
38
import org.apache.commons.lang3.StringUtils;
39
import org.gvsig.featureform.swing.JFeaturesForm;
40
import org.gvsig.fmap.dal.DALLocator;
41
import org.gvsig.fmap.dal.DataManager;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
44
import org.gvsig.fmap.dal.feature.FeatureQuery;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.dal.swing.DALSwingLocator;
47
import org.gvsig.fmap.dal.swing.DataSwingManager;
48

    
49
import org.gvsig.tools.dynform.JDynFormField;
50
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
51
import org.gvsig.tools.dynobject.DynObject;
52
import org.gvsig.tools.service.spi.ServiceManager;
53
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
54

    
55
public class JDynFormFieldFeatureLink extends AbstractJDynFormField implements JDynFormField {
56

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

    
71
    private String codeFieldName = null;
72
    private String foreingTableName;
73
    private String foreingCodeName;
74
    private String foreingDescriptionMask = null;
75
    private String foreingDescriptionFieldNames[] = null;
76

    
77
    /**
78
     *
79
     * @param parameters
80
     * @param serviceManager
81
     */
82
    public JDynFormFieldFeatureLink(DynObject parameters,
83
            ServiceManager serviceManager) {
84
        super(parameters, serviceManager);
85
        if (this.getParameterValue() != null) {
86
            this.assignedValue = (DynObject) this.getParameterValue();
87
        }
88
    }
89

    
90
    @Override
91
    public Object getAssignedValue() {
92
        return this.assignedValue;
93
    }
94

    
95
    @Override
96
    public void initComponent() {
97
        this.foreingDescriptionFieldNames = null;
98
        this.codeFieldName = getTagValueAsString(DAL_code, null);
99
        this.foreingTableName = getTagValueAsString(DAL_foreingTable, null);
100
        this.foreingCodeName = getTagValueAsString(DAL_foreingCode, null);
101

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

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

    
139
//        this.txtCode.setText("          ");
140
//        this.txtDescforeingCodeNameription.setText("                    ");
141
//        this.txtCode.setEditable(false);
142
        this.txtDescription.setEditable(false);
143

    
144
        if (StringUtils.isEmpty(codeFieldName) || StringUtils.isEmpty(foreingDescriptionMask)
145
                || StringUtils.isEmpty(foreingTableName) || StringUtils.isEmpty(foreingCodeName)
146
                || this.foreingDescriptionFieldNames == null) {
147
            this.btnEdit.setEnabled(false);
148
            this.btnLink.setEnabled(false);
149
            this.btnUnlink.setEnabled(false);
150
        }
151

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

    
191
        this.setValue(this.assignedValue);
192
    }
193

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

    
207
    private void doLink() {
208

    
209
    }
210

    
211
    private void doUnlink() {
212
        this.clear();
213
    }
214

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

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

    
249
    @Override
250
    public void setReadOnly(boolean readonly) {
251
        boolean enabled = !readonly;
252
//        this.txtCode.setEnabled(enabled);
253
        if( this.txtDescription == null ) {
254
            return;
255
        }
256
        this.txtDescription.setEnabled(enabled);
257
        this.btnEdit.setEnabled(enabled);
258
        this.btnLink.setEnabled(enabled);
259
        this.btnUnlink.setEnabled(enabled);
260
    }
261

    
262
    @Override
263
    public void setValue(Object value) {
264
        if (value == null) {
265
            this.clear();
266
            return;
267
        }
268
        this.value = (DynObject) value;
269
//        this.txtCode.setText("");
270
        this.txtDescription.setText("");
271
        if (StringUtils.isEmpty(foreingCodeName) || StringUtils.isEmpty(foreingDescriptionMask)
272
                || this.foreingDescriptionFieldNames == null) {
273
        }
274
//        Object x = this.value.getDynValue(foreingCodeName);
275
//        if (x != null) {
276
//            this.txtCode.setText(x.toString());
277
//        }
278
        String description = this.getDescription();
279
        if (description != null) {
280
            this.txtDescription.setText(description);
281
        }
282
    }
283

    
284
    @Override
285
    public Object getValue() {
286
        return this.value;
287
    }
288

    
289
    @Override
290
    public void fetch(DynObject container) {
291
        if (StringUtils.isEmpty(this.codeFieldName)) {
292
            return;
293
        }
294
        DynObject value = (DynObject) this.getValue();
295
        if (value == null) {
296
            container.setDynValue(this.codeFieldName, null);
297
        } else {
298
            if (StringUtils.isEmpty(this.foreingCodeName)) {
299
                return;
300
            }
301
            container.setDynValue(this.codeFieldName, value.getDynValue(this.foreingCodeName));
302
        }
303
    }
304

    
305
    @Override
306
    public boolean hasValidValue() {
307
        return true;
308
    }
309

    
310
    @Override
311
    public void clear() {
312
        this.value = null;
313
//        this.txtCode.setText("");
314
        this.txtDescription.setText("");
315
    }
316
}