Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.daltransform.app / org.gvsig.daltransform.app.join / src / main / java / org / gvsig / app / join / daltransform / SelectParametersWizardPanel.java @ 41067

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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 {Iver T.I.}   {Task}
27
 */
28

    
29
package org.gvsig.app.join.daltransform;
30

    
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.util.ArrayList;
34

    
35
import javax.swing.JOptionPane;
36

    
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.app.ApplicationLocator;
42
import org.gvsig.app.join.utils.TransformUtils;
43
import org.gvsig.daltransform.swing.DataTransformWizardPanel;
44
import org.gvsig.daltransform.swing.impl.AbstractDataTransformWizardPanel;
45
import org.gvsig.daltransform.swing.impl.components.FeatureTypeAttributesCombo;
46
import org.gvsig.daltransform.swing.impl.components.FeatureTypeAttributesList;
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.exception.ParameterMissingException;
49
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.i18n.Messages;
53

    
54

    
55
/**
56
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
57
 */
58
public class SelectParametersWizardPanel extends AbstractDataTransformWizardPanel
59
implements DataTransformWizardPanel, ActionListener{
60
    
61
    private static Logger logger =
62
        LoggerFactory.getLogger(SelectParametersWizardPanel.class);
63
    
64
        private javax.swing.JLabel attributesLabel;
65
        private FeatureTypeAttributesList attributesList;
66
        private javax.swing.JScrollPane attributesScroll;
67
        private FeatureTypeAttributesCombo key1Combo;
68
        private javax.swing.JLabel key1Label;
69
        private FeatureTypeAttributesCombo key2_Combo;
70
        private javax.swing.JLabel key2Label;
71
        private javax.swing.JLabel prefix1Label;
72
        private javax.swing.JTextField prefix1Text;
73
        private javax.swing.JLabel prefix2Label;
74
        private javax.swing.JTextField prefix2Text; 
75
        
76
        private FeatureStore secondFeatStore = null;
77

    
78
        /**
79
         * @param featureTransformWizardModel
80
         */
81
        public SelectParametersWizardPanel() {
82
                super();        
83
        }
84
        
85
           
86
        public void updatePanel() {
87
        initComponents();
88
        initLabels();
89
        try {
90
            updateFeatureStores();
91
        } catch (DataException e) {
92
            logger.error("While updating feature stores", e);
93
        }
94
        }
95
        
96
        private void initLabels(){
97
                key1Label.setText(PluginServices.getText(this,"join_first_key"));
98
                key2Label.setText(PluginServices.getText(this,"join_second_key"));
99
                prefix1Label.setText(PluginServices.getText(this,"join_first_prefix"));
100
                prefix2Label.setText(PluginServices.getText(this,"join_second_prefix"));
101
                attributesLabel.setText(PluginServices.getText(this,"join_select_attributes"));
102
        }
103

    
104
        private void initComponents() {
105
                java.awt.GridBagConstraints gridBagConstraints;
106

    
107
                prefix2Text = new javax.swing.JTextField();
108
                key2_Combo = new FeatureTypeAttributesCombo();
109
                prefix1Text = new javax.swing.JTextField();
110
                key1Label = new javax.swing.JLabel();
111
                
112
                key1Combo = new FeatureTypeAttributesCombo();
113
                // listen to changes in first combo
114
                // we'll update second combo leaving
115
                // fields of same type
116
                key1Combo.addActionListener(this);
117
                
118
                key2Label = new javax.swing.JLabel();
119
                prefix1Label = new javax.swing.JLabel();
120
                prefix2Label = new javax.swing.JLabel();
121
                attributesLabel = new javax.swing.JLabel();
122
                attributesScroll = new javax.swing.JScrollPane();
123
                attributesList = new FeatureTypeAttributesList();
124

    
125
                setLayout(new java.awt.GridBagLayout());
126
                gridBagConstraints = new java.awt.GridBagConstraints();
127
                gridBagConstraints.gridx = 0;
128
                gridBagConstraints.gridy = 7;
129
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
130
                gridBagConstraints.weightx = 1.0;
131
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 3, 2);
132
                add(prefix2Text, gridBagConstraints);
133

    
134
                gridBagConstraints = new java.awt.GridBagConstraints();
135
                gridBagConstraints.gridx = 0;
136
                gridBagConstraints.gridy = 3;
137
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138
                gridBagConstraints.weightx = 1.0;
139
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 3, 2);
140
                add(key2_Combo, gridBagConstraints);
141
                gridBagConstraints = new java.awt.GridBagConstraints();
142
                gridBagConstraints.gridx = 0;
143
                gridBagConstraints.gridy = 5;
144
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
145
                gridBagConstraints.weightx = 1.0;
146
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 3, 2);
147
                add(prefix1Text, gridBagConstraints);
148

    
149
                key1Label.setText("jLabel1");
150
                gridBagConstraints = new java.awt.GridBagConstraints();
151
                gridBagConstraints.gridx = 0;
152
                gridBagConstraints.gridy = 0;
153
                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
154
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
155
                add(key1Label, gridBagConstraints);
156

    
157
                gridBagConstraints = new java.awt.GridBagConstraints();
158
                gridBagConstraints.gridx = 0;
159
                gridBagConstraints.gridy = 1;
160
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
161
                gridBagConstraints.weightx = 1.0;
162
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 3, 2);
163
                add(key1Combo, gridBagConstraints);
164

    
165
                key2Label.setText("jLabel2");
166
                gridBagConstraints = new java.awt.GridBagConstraints();
167
                gridBagConstraints.gridx = 0;
168
                gridBagConstraints.gridy = 2;
169
                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
170
                gridBagConstraints.insets = new java.awt.Insets(3, 2, 2, 2);
171
                add(key2Label, gridBagConstraints);
172

    
173
                prefix1Label.setText("jLabel1");
174
                gridBagConstraints = new java.awt.GridBagConstraints();
175
                gridBagConstraints.gridx = 0;
176
                gridBagConstraints.gridy = 4;
177
                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
178
                gridBagConstraints.insets = new java.awt.Insets(3, 2, 2, 2);
179
                add(prefix1Label, gridBagConstraints);
180

    
181
                prefix2Label.setText("jLabel2");
182
                gridBagConstraints = new java.awt.GridBagConstraints();
183
                gridBagConstraints.gridx = 0;
184
                gridBagConstraints.gridy = 6;
185
                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
186
                gridBagConstraints.insets = new java.awt.Insets(3, 2, 2, 2);
187
                add(prefix2Label, gridBagConstraints);
188

    
189
                attributesLabel.setText("attributesLabel");
190
                gridBagConstraints = new java.awt.GridBagConstraints();
191
                gridBagConstraints.gridx = 0;
192
                gridBagConstraints.gridy = 8;
193
                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
194
                gridBagConstraints.insets = new java.awt.Insets(3, 2, 2, 2);
195
                add(attributesLabel, gridBagConstraints);
196

    
197
                attributesScroll.setViewportView(attributesList);
198

    
199
                gridBagConstraints = new java.awt.GridBagConstraints();
200
                gridBagConstraints.gridx = 0;
201
                gridBagConstraints.gridy = 9;
202
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203
                gridBagConstraints.weightx = 1.0;
204
                gridBagConstraints.weighty = 1.0;
205
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
206
                add(attributesScroll, gridBagConstraints);
207
        }
208
        
209

    
210
        /**
211
         * @return
212
         */
213
        public String getKeyAttr1() {
214
                return key1Combo.getSelectedFeatureAttributeDescriptor().getName();
215
        }
216

    
217
        /**
218
         * @return
219
         */
220
        public String getkeyAtrr2() {
221
                return key2_Combo.getSelectedFeatureAttributeDescriptor().getName();
222
        }
223

    
224
        /**
225
         * @return
226
         */
227
        public String getPrefix1() {
228
                return prefix1Text.getText();
229
        }
230

    
231
        /**
232
         * @return
233
         */
234
        public String getPrefix2() {
235
                return prefix2Text.getText();
236
        }
237

    
238
        /**
239
         * @return
240
         */
241
        public String[] getAttributes() {
242
                return attributesList.getAttributesName();
243
        }
244
        
245
        
246
        public void setSecondFeatureStore(FeatureStore selectedFeatureStore) {
247
            secondFeatStore = selectedFeatureStore;
248
            
249
        }
250
        /**
251
         * @param selectedFeatureStore
252
         * @throws DataException 
253
         */
254
        public void updateFeatureStores() throws DataException {
255
            
256
            if (secondFeatStore == null) {
257
                throw new ParameterMissingException("Second feature store");
258
            }
259
            
260
                key1Combo.addFeatureAttributes(getFeatureStore().getDefaultFeatureType());
261
                attributesList.addFeatureAttributes(secondFeatStore.getDefaultFeatureType());
262
                
263
                if (key1Combo.getItemCount() == 0) {
264
                    
265
            String _msg =
266
                Messages.getText("_First_table_has_no_fields");
267
            String _tit = Messages.getText("_Join");
268
            
269
            JOptionPane.showMessageDialog(
270
                this, _msg, _tit,
271
                JOptionPane.ERROR_MESSAGE);
272
            
273
                    getDataTransformWizard().setApplicable(false);
274
        } else {
275
            key1Combo.setSelectedIndex(0);
276
                }
277
        }
278

    
279

    
280
        /**
281
         * Listen to changes in first combo box (key1)
282
         */
283
    public void actionPerformed(ActionEvent e) {
284
        
285
        Object src = e.getSource();
286
        if (src == key1Combo
287
            && key1Combo != null
288
            && key2_Combo != null
289
            && secondFeatStore != null) {
290
            
291
            FeatureAttributeDescriptor att =
292
                key1Combo.getSelectedFeatureAttributeDescriptor();
293
            if (att != null) {
294
                
295
                int the_type = att.getType();
296
                key2_Combo.removeAllItems();
297
                FeatureType ft = null;
298
                
299
                try {
300
                    ft = secondFeatStore.getDefaultFeatureType();
301
                } catch (DataException de) {
302
                    logger.info("While getting feat type: " + de.getMessage());
303
                    getDataTransformWizard().setApplicable(false);
304
                    ApplicationLocator.getManager().message(
305
                        Messages.getText("_Error_getting_attributes"),
306
                        JOptionPane.ERROR_MESSAGE);
307
                    return;
308
                }
309
                
310
                ArrayList<Integer> comparable_types =
311
                    TransformUtils.getComparableDataTypes(ft, the_type);
312
                
313
                if (key2_Combo.addFeatureAttributes(ft, comparable_types) == 0) {
314

    
315
                    String _msg =
316
                        Messages.getText("_No_compatible_field_in_second_table");
317
                    String _tit = Messages.getText("_Join");
318
                    
319
                    JOptionPane.showMessageDialog(
320
                        this, _msg, _tit,
321
                        JOptionPane.ERROR_MESSAGE);
322
                    getDataTransformWizard().setApplicable(false);
323
                    
324
                } else {
325
                    getDataTransformWizard().setApplicable(true);
326
                    // JOptionPane.showMessageDialog(this, "(2) OK !!");
327
                }
328
            }
329
        }
330
        
331
    }
332
}
333