Statistics
| Revision:

root / trunk / extensions / extTableImport / src / org / gvsig / tableImport / importfields / ui / ImportFieldPanel.java @ 28459

History | View | Annotate | Download (8.33 KB)

1
package org.gvsig.tableImport.importfields.ui;
2

    
3
import java.awt.Dimension;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.Insets;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.beans.PropertyChangeEvent;
10
import java.beans.PropertyChangeListener;
11
import java.util.ArrayList;
12
import java.util.Iterator;
13

    
14
import javax.swing.JButton;
15
import javax.swing.JLabel;
16
import javax.swing.JPanel;
17
import javax.swing.JScrollPane;
18

    
19
import jwizardcomponent.JWizardComponents;
20
import jwizardcomponent.JWizardPanel;
21

    
22
import org.gvsig.tableImport.importfields.ImportFieldParams;
23
import org.gvsig.tableImport.importfields.ImportFieldParams.FielToImport;
24

    
25
import com.iver.andami.PluginServices;
26

    
27
public class ImportFieldPanel extends JWizardPanel implements PropertyChangeListener, ActionListener {
28

    
29
        private static final long serialVersionUID = 1L;
30
        private JPanel pLista = null;
31
        private JPanel pButtons = null;
32
        private JButton bAll = null;
33
        private JButton bNone = null;
34
        private JLabel lblSpace = null;
35
        private JScrollPane jScrollPane = null;
36
        private JLabel lblMessage = null;
37

    
38
        private boolean updating;
39
        private ImportFieldParams params = null;
40
        private GridBagConstraints pLista_gbContraints;
41
        private ArrayList fields = new ArrayList();
42

    
43

    
44
        /**
45
         * This is the default constructor
46
         */
47
        public ImportFieldPanel(JWizardComponents wizardComponents, ImportFieldParams params) {
48
                super(wizardComponents);
49
                initialize();
50
                this.params = params;
51
                initialize();
52
                this.update();
53
        }
54

    
55
        public ImportFieldPanel() {
56
                super(null);
57
                initialize();
58
        }
59

    
60
        /**
61
         * This method initializes this
62
         *
63
         * @return void
64
         */
65
        private void initialize() {
66
                GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
67
                gridBagConstraints31.gridx = 0;
68
                gridBagConstraints31.fill = GridBagConstraints.HORIZONTAL;
69
                gridBagConstraints31.gridy = 1;
70
                GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
71
                gridBagConstraints11.fill = GridBagConstraints.BOTH;
72
                gridBagConstraints11.weighty = 1.0;
73
                gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
74
                gridBagConstraints11.weightx = 1.0;
75
                gridBagConstraints11.anchor = GridBagConstraints.FIRST_LINE_START;
76
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
77
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
78
                gridBagConstraints.gridy = 2;
79
                gridBagConstraints.weightx = 1.0;
80
                gridBagConstraints.weighty = 0.0D;
81
                gridBagConstraints.gridheight = 1;
82
                gridBagConstraints.gridx = 0;
83
                this.setSize(400, 300);
84
                this.setLayout(new GridBagLayout());
85
                this.setPreferredSize(new Dimension(400, 300));
86
                this.add(getJScrollPane(), gridBagConstraints11);
87
                this.add(getPButtons(), gridBagConstraints);
88
                this.add(lblMessage, gridBagConstraints31);
89
        }
90

    
91
        /**
92
         * This method initializes lLista
93
         *
94
         * @return javax.swing.JList
95
         */
96
        private JPanel getLLista() {
97
                if (pLista == null) {
98
                        pLista = new JPanel();
99
                        pLista.setName("lista");
100
                        pLista.setLayout(new GridBagLayout());
101
                        pLista_gbContraints = new GridBagConstraints();
102
                        pLista_gbContraints.anchor = GridBagConstraints.FIRST_LINE_START;
103
                        pLista_gbContraints.fill = GridBagConstraints.HORIZONTAL;
104
                        pLista_gbContraints.weightx = 1;
105
                        pLista_gbContraints.gridx=1;
106
//                        pLista_gbContraints.gridy=GridBagConstraints.REMAINDER;
107

    
108
                }
109
                return pLista;
110
        }
111

    
112
        /**
113
         * This method initializes pButtons
114
         *
115
         * @return javax.swing.JPanel
116
         */
117
        private JPanel getPButtons() {
118
                if (pButtons == null) {
119
                        lblMessage = new JLabel();
120
                        if (this.params == null){
121
                                lblMessage.setText("Message");
122
                        } else{
123
                                lblMessage.setText(" ");
124
                        }
125
                        GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
126
                        gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
127
                        gridBagConstraints4.weightx = 1.0D;
128
                        gridBagConstraints4.gridy = 1;
129
                        gridBagConstraints4.gridx = 0;
130
                        lblSpace = new JLabel();
131
                        lblSpace.setText(" ");
132
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
133
                        gridBagConstraints3.anchor = GridBagConstraints.EAST;
134
                        gridBagConstraints3.ipady = 0;
135
                        gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
136
                        gridBagConstraints3.gridy = 1;
137
                        gridBagConstraints3.ipadx = 0;
138
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
139
                        gridBagConstraints2.anchor = GridBagConstraints.EAST;
140
                        gridBagConstraints2.ipady = 0;
141
                        gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
142
                        gridBagConstraints2.gridy = 1;
143
                        gridBagConstraints2.ipadx = 0;
144
                        pButtons = new JPanel();
145
                        pButtons.setLayout(new GridBagLayout());
146
                        pButtons.add(lblSpace, gridBagConstraints4);
147
                        pButtons.add(getBAll(), gridBagConstraints2);
148
                        pButtons.add(getBNone(), gridBagConstraints3);
149
                }
150
                return pButtons;
151
        }
152

    
153
        /**
154
         * This method initializes bAll
155
         *
156
         * @return javax.swing.JButton
157
         */
158
        private JButton getBAll() {
159
                if (bAll == null) {
160
                        bAll = new JButton();
161
                        bAll.setText(PluginServices.getText(null,"select_all"));
162
                        bAll.setActionCommand("all");
163
                        bAll.setName("all");
164
                        bAll.addActionListener(this);
165
                }
166
                return bAll;
167
        }
168

    
169
        /**
170
         * This method initializes bNone
171
         *
172
         * @return javax.swing.JButton
173
         */
174
        private JButton getBNone() {
175
                if (bNone == null) {
176
                        bNone = new JButton();
177
                        bNone.setText(PluginServices.getText(null,"clear_selection"));
178
                        bNone.setActionCommand("none");
179
                        bNone.setName("none");
180
                        bNone.addActionListener(this);
181
                }
182
                return bNone;
183
        }
184

    
185
        /**
186
         * This method initializes jScrollPane
187
         *
188
         * @return javax.swing.JScrollPane
189
         */
190
        private JScrollPane getJScrollPane() {
191
                if (jScrollPane == null) {
192
                        jScrollPane = new JScrollPane();
193
                        jScrollPane.setViewportView(getLLista());
194
                }
195
                return jScrollPane;
196
        }
197

    
198

    
199
        public void update() {
200
                try{
201
                        if (this.updating){
202
                                return;
203
                        }
204
                        this.updating= true;
205
                        if (this.params == null){
206
                                return;
207
                        }
208
                        ArrayList fieldsToImport = this.params.getFieldsToImport();
209
                        if (fieldsToImport == null){
210
                                this.getLLista().removeAll();
211
                                return;
212
                        }
213
                        this.fillList(fieldsToImport);
214

    
215
                        if (this.params.isValid()){
216
                                this.setFinishButtonEnabled(true);
217
                                this.lblMessage.setText(" ");
218
                        }else{
219
                                this.setFinishButtonEnabled(false);
220
                                this.lblMessage.setText(params.getValidationMsg());
221
                        }
222
                        super.update();
223
                } finally {
224
                        this.updating=false;
225
                }
226
        }
227

    
228
        private void fillList(ArrayList fieldsToImport) {
229
                FielToImport fieldDef;
230
                FieldPanel fieldElement;
231
                int i;
232
                for (i=0;i<fieldsToImport.size();i++){
233
                        fieldDef = (FielToImport) fieldsToImport.get(i);
234
                        if (this.fields.size()> i){
235
                                fieldElement = (FieldPanel) this.fields.get(i);
236
                        } else{
237
                                fieldElement = this.addNewFieldPanel();
238
                        }
239
                        fieldElement.setToImpor(fieldDef.toImport);
240
                        fieldElement.setSourceField(fieldDef.originalFieldName);
241
                        fieldElement.setTargetFieldName(fieldDef.fieldNameToUse);
242
                }
243
                for (i=this.fields.size()-1;i>=fieldsToImport.size();i--){
244
                        fieldElement= (FieldPanel) this.fields.remove(i);;
245
                        fieldElement.removeFrom(this.getLLista());
246
                }
247
        }
248

    
249

    
250
        private FieldPanel addNewFieldPanel() {
251
                FieldPanel fieldElement = new FieldPanel();
252
                fieldElement.addPropertyChangeListener(this);
253
                fieldElement.loadIn(this.getLLista());
254
                this.fields.add(fieldElement);
255
                this.doLayout();
256
                return fieldElement;
257
        }
258

    
259
        public void propertyChange(PropertyChangeEvent evt) {
260
                if (this.updating){
261
                        return;
262
                }
263
                if (!(evt.getSource() instanceof FieldPanel)){
264
                        return;
265
                }
266
                String srcField = ((FieldPanel) evt.getSource()).getSourceField();
267
                Iterator iter = this.params.getFieldsToImport().iterator();
268
                while (iter.hasNext()){
269
                        FielToImport field = (FielToImport) iter.next();
270
                        if (field.originalFieldName.equals(srcField)){
271
                                if (evt.getPropertyName().equals("toImport")){
272
                                        field.toImport = ((Boolean)evt.getNewValue()).booleanValue();
273
//                                        System.out.println("set to " +field.toImport +" toImpor of "+ srcField);
274
                                } else if (evt.getPropertyName().equals("targetFieldName")){
275
                                        field.fieldNameToUse = (String) evt.getNewValue();
276
//                                        System.out.println("set to " +field.fieldNameToUse +" nameToUse of "+ srcField);
277
                                }
278
                                break;
279
                        }
280

    
281
                }
282
                this.update();
283
        }
284

    
285
        public void actionPerformed(ActionEvent e) {
286
                boolean toImpor=false;
287
                if (e.getActionCommand().equals(this.getBAll().getActionCommand())){
288
                        toImpor=true;
289
                } else if (e.getActionCommand().equals(this.getBNone().getActionCommand())){
290
                        toImpor=false;
291
                }
292
                Iterator iter = this.params.getFieldsToImport().iterator();
293
                while (iter.hasNext()){
294
                        ((FielToImport)iter.next()).toImport=toImpor;
295
                }
296
                this.update();
297
        }
298

    
299
}