Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / copytable / CopyTablePanel.java @ 46581

History | View | Annotate | Download (13.6 KB)

1
package org.gvsig.app.extension.copytable;
2

    
3
import java.awt.Component;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Objects;
10
import javax.swing.AbstractCellEditor;
11
import javax.swing.JComboBox;
12
import javax.swing.JTable;
13
import javax.swing.ListSelectionModel;
14
import javax.swing.SwingUtilities;
15
import javax.swing.event.TreeSelectionEvent;
16
import javax.swing.table.AbstractTableModel;
17
import javax.swing.table.DefaultTableColumnModel;
18
import javax.swing.table.TableCellEditor;
19
import javax.swing.table.TableColumn;
20
import org.apache.commons.lang3.StringUtils;
21
import org.gvsig.expressionevaluator.Expression;
22
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator;
23
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
24
import org.gvsig.expressionevaluator.swing.ExpressionPickerController;
25
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.dal.feature.FeatureStore;
27
import org.gvsig.fmap.dal.feature.FeatureType;
28
import org.gvsig.fmap.dal.swing.DALSwingLocator;
29
import org.gvsig.fmap.dal.swing.DataSwingManager;
30
import org.gvsig.fmap.dal.swing.featuretype.AttributeDescriptorPickerController;
31
import org.gvsig.tools.ToolsLocator;
32
import org.gvsig.tools.swing.api.ToolsSwingLocator;
33
import org.gvsig.tools.swing.api.ToolsSwingUtils;
34
import org.gvsig.tools.swing.api.task.TaskStatusController;
35
import org.gvsig.tools.task.SimpleTaskStatus;
36

    
37
/**
38
 *
39
 * @author jjdelcerro
40
 */
41
public class CopyTablePanel extends CopyTablePanelView {
42

    
43
    private AttributeDescriptorPickerController pickerSourceFieldJoin;
44
    private FeatureStore source;
45
    private FeatureStore target;
46
    private AttributeDescriptorPickerController pickerTargetFieldJoin;
47
    private ExpressionPickerController pickerSourceFilter;
48
    private ExpressionPickerController pickerTargetUpdateExpression;
49
    private DefaultTableColumnModel fieldsColumnModel;
50
    private SimpleTaskStatus taskStatus;
51
    private TaskStatusController status;
52
    private StoresRepositoryController treeSourceTableController;
53
    private StoresRepositoryController treeTargetTableController;
54

    
55
    private class FeatureAttributeCellEditor extends AbstractCellEditor implements TableCellEditor {
56

    
57
        private final AttributeDescriptorPickerController picker;
58
        private final JComboBox combo;
59
        
60
        public FeatureAttributeCellEditor() {
61
            DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
62
            this.combo = new JComboBox();
63
            this.picker = dataSwingManager.createAttributeDescriptorPickerController(this.combo);
64
        }
65

    
66
        @Override
67
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
68
            if( value == null ) {
69
                this.picker.clean();
70
                return this.combo;
71
            }
72
            this.picker.setFeatureType(source.getDefaultFeatureTypeQuietly());
73
            this.picker.setAllowNull(true);
74
            this.picker.set(Objects.toString(value,"")); 
75
            return this.combo;
76
        }
77

    
78
        @Override
79
        public Object getCellEditorValue() {
80
            return this.picker.getName();
81
        }        
82
    }
83
    
84
    private class MyTableModel extends AbstractTableModel implements Iterable<MyTableModel.Row>{
85

    
86
        private final List<Row> rows;
87
        private final String[] columnNames;
88
        private final Class[] columnClass;
89
        
90
        private class Row {
91
            String source;
92
            String target;
93
            boolean update;
94
            
95
            public Object get(int index) {
96
                switch(index) {
97
                    case 0:
98
                        return target;
99
                    case 1:
100
                        return source;
101
                    case 2:
102
                        return update;
103
                    default:
104
                        return null;
105
                }
106
            }
107
            
108
            public void set(int index, Object value) {
109
                switch(index) {
110
                    case 0:
111
                        this.target = (String) value;
112
                        break;
113
                    case 1:
114
                        this.source = (String) value;
115
                        break;
116
                    case 2:
117
                        this.update = (boolean) value;
118
                        break;
119
                    default:
120
                        break;
121
                }
122
            }
123
        }
124
        
125
        public MyTableModel() {
126
            this.rows = new ArrayList<>();
127
            FeatureType targetType = target.getDefaultFeatureTypeQuietly();
128
            FeatureType sourceType = source.getDefaultFeatureTypeQuietly();
129
            for (FeatureAttributeDescriptor targetAttr : targetType) {
130
                Row row = new Row();
131
                row.target = targetAttr.getName();
132
                row.source = null;
133
                if( sourceType.getAttributeDescriptor(targetAttr.getName())!=null ) {
134
                    row.source = row.target;
135
                }
136
                if( targetAttr.isAutomatic() || targetAttr.isReadOnly() || targetAttr.isPrimaryKey() ) {
137
                    row.update = false;
138
                } else {
139
                    row.update = true;
140
                }
141
                this.rows.add(row);
142
            }
143
            this.columnNames = new String[] {
144
                "Campo destino",
145
                "Campo origen",
146
                "Actualizar"
147
            };
148
            this.columnClass = new Class[] {
149
                String.class,
150
                String.class,
151
                Boolean.class
152
            };
153
        }
154

    
155
        @Override
156
        public String getColumnName(int column) {
157
            return this.columnNames[column];
158
        }
159

    
160
        @Override
161
        public Class<?> getColumnClass(int columnIndex) {
162
            return this.columnClass[columnIndex];
163
        }
164
        
165
        @Override
166
        public int getRowCount() {
167
            return this.rows.size();
168
        }
169

    
170
        @Override
171
        public int getColumnCount() {
172
            return 3;
173
        }
174

    
175
        @Override
176
        public Object getValueAt(int rowIndex, int columnIndex) {
177
            return this.rows.get(rowIndex).get(columnIndex);
178
        }
179

    
180
        @Override
181
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
182
            this.rows.get(rowIndex).set(columnIndex, aValue);
183
        }
184
        
185
        @Override
186
        public boolean isCellEditable(int rowIndex, int columnIndex) {
187
            switch(columnIndex) {
188
                case 0:
189
                default:
190
                    return false;
191
                case 1:
192
                case 2:
193
                    return true;
194
            }
195
        }
196

    
197
        @Override
198
        public Iterator<Row> iterator() {
199
            return this.rows.iterator();
200
        }
201
    }
202
    
203
    public CopyTablePanel() {
204
        this.initComponents();
205
    }
206

    
207
    private void initComponents() {
208
//        DataManager dataManager = DALLocator.getDataManager();
209
        DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
210
        ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager();
211
        this.status = ToolsSwingLocator.getTaskStatusSwingManager().createTaskStatusController(lblStatusLabel, lblStatusMessage, pbStatus);
212
        
213
        this.treeSourceTableController = new StoresRepositoryController(this.treeSourceTable);
214
        this.treeSourceTableController.addTreeSelectionListener((TreeSelectionEvent e) -> {
215
            doChangeSource();
216
        });
217
        this.pickerSourceFieldJoin = dataSwingManager.createAttributeDescriptorPickerController(cboSourceJoinField);
218

    
219
        this.treeTargetTableController = new StoresRepositoryController(this.treeTargetTable);
220
        this.treeTargetTableController.addTreeSelectionListener((TreeSelectionEvent e) -> {
221
            doChangeTarget();
222
        });
223
        this.pickerTargetFieldJoin = dataSwingManager.createAttributeDescriptorPickerController(cboTargetJoinField);
224
        
225
        this.pickerSourceFilter = expressionSwingManager.createExpressionPickerController(
226
                txtSourceFilter, 
227
                btnSourceFilter, 
228
                btnSourceFilterBookmarks, 
229
                btnSourceFilterHistory
230
        );
231

    
232
        this.pickerTargetUpdateExpression = expressionSwingManager.createExpressionPickerController(
233
                txtTargetUpdateWhenExpression, 
234
                btnTargetUpdateWhenExpression, 
235
                btnTargetUpdateWhenExpressionBookmarks, 
236
                btnTargetUpdateWhenExpressionHistory
237
        );
238
        this.chkTargetInsertIfNotExistsInTarget.setSelected(false);
239

    
240
        this.tableFields.setAutoCreateColumnsFromModel(false);
241
        this.tableFields.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
242
        this.fieldsColumnModel = new DefaultTableColumnModel();
243
            
244
        TableColumn column = new TableColumn();
245
        column.setHeaderValue("Campo destino");
246
        column.setCellRenderer(null);
247
        column.setCellEditor(new FeatureAttributeCellEditor());
248
        column.setResizable(true);
249
        column.setModelIndex(0);
250
        this.fieldsColumnModel.addColumn(column);
251

    
252
        column = new TableColumn();
253
        column.setHeaderValue("Campo origen");
254
        column.setCellRenderer(null);
255
        column.setCellEditor(new FeatureAttributeCellEditor());
256
        column.setResizable(true);
257
        column.setModelIndex(1);
258
        this.fieldsColumnModel.addColumn(column);
259

    
260
        column = new TableColumn();
261
        column.setHeaderValue("Actualizar");
262
        column.setCellRenderer(null);
263
        column.setResizable(true);
264
        column.setModelIndex(2);
265
        this.fieldsColumnModel.addColumn(column);
266
        
267
        this.tableFields.setColumnModel(this.fieldsColumnModel);
268

    
269
        SwingUtilities.invokeLater(() -> {
270
            lblStatusLabel.setText("");
271
            lblStatusMessage.setText("");
272
            pbStatus.setVisible(false);
273
        });
274
        
275
        ToolsSwingUtils.ensureRowsCols(this, 20, 80, 24, 100);
276
    }
277
    
278
    private void doChangeSource() {
279
        this.source = this.treeSourceTableController.getSelectedFeatureStore();
280
        if( this.source==null ) {
281
            return;
282
        }
283
        DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
284
        dataSwingManager.resetExpressionBuilder(this.pickerTargetUpdateExpression);
285
        dataSwingManager.addToExpressionBuilder(this.pickerTargetUpdateExpression, this.source, "source");
286

    
287
        this.pickerSourceFieldJoin.setFeatureType(this.source.getDefaultFeatureTypeQuietly());
288
        
289
        if( this.target!=null ) {
290
            this.tableFields.setModel(new MyTableModel());
291
            dataSwingManager.addToExpressionBuilder(this.pickerTargetUpdateExpression, this.target, "target");
292
        }
293
    }
294

    
295
    private void doChangeTarget() {
296
        this.target = this.treeTargetTableController.getSelectedFeatureStore();
297
        if( this.target==null ) {
298
            return;
299
        }
300
        DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
301
        dataSwingManager.resetExpressionBuilder(this.pickerTargetUpdateExpression);
302
        dataSwingManager.addToExpressionBuilder(this.pickerTargetUpdateExpression, this.target, "target");
303
        
304
        this.pickerTargetFieldJoin.setFeatureType(this.target.getDefaultFeatureTypeQuietly());
305

    
306
        if( this.source!=null ) {
307
            this.tableFields.setModel(new MyTableModel());
308
            dataSwingManager.addToExpressionBuilder(this.pickerTargetUpdateExpression, this.source, "source");
309
        }
310
    }
311

    
312
    public FeatureStore getSource() {
313
        return this.source;
314
    }
315

    
316
    public String getSourceFieldJoin() {
317
        return this.pickerSourceFieldJoin.getName();
318
    }
319

    
320
    public Expression getSourceFilter() {
321
        return this.pickerSourceFilter.get();
322
    }
323

    
324
    public FeatureStore getTarget() {
325
        return this.target;
326
    }
327

    
328
    public String getTargetFieldJoin() {
329
        return this.pickerTargetFieldJoin.getName();
330
    }
331

    
332
    public Expression getUpdateIfExistsInTargetCondition() {
333
        return this.pickerTargetUpdateExpression.get();
334
    }
335

    
336
    public boolean getInsertIfNotExistsTarget() {
337
        return this.chkTargetInsertIfNotExistsInTarget.isSelected();
338
    }
339

    
340
    public boolean getUpdateIfExistsInTarget() {
341
        return this.chkTargetUpdateIfExistsInTarget.isSelected();
342
    }
343

    
344
    public boolean getDeleteIfNotExiststInSource() {
345
        return this.chkTargetDeleteIfNotExiststInSource.isSelected();
346
    }
347

    
348
    public SimpleTaskStatus getStatus() {
349
        if( this.taskStatus == null ) {
350
            this.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Copy table");
351
            this.status.bind(this.taskStatus);
352
        }
353
        lblStatusLabel.setVisible(true);
354
        lblStatusMessage.setVisible(true);
355
        pbStatus.setVisible(true);
356
        
357
        return this.taskStatus;
358
    }
359

    
360
    public Map<String/*target*/, String/*source*/> getFields() {
361
        Map<String/*target*/, String/*source*/> fields = new HashMap<>();
362
        MyTableModel model = (MyTableModel) this.tableFields.getModel();
363
        for (MyTableModel.Row row : model) {
364
            if( StringUtils.isNotBlank(row.source) ) {
365
                fields.put(row.target, row.source);
366
            }
367
        }
368
        return fields;
369
    }
370

    
371
    public List<String/*target*/> getFieldsToUpdate() {
372
        List<String> fields = new ArrayList<>();
373
        MyTableModel model = (MyTableModel) this.tableFields.getModel();
374
        for (MyTableModel.Row row : model) {
375
            if( StringUtils.isNotBlank(row.source) && row.update) {
376
                fields.add(row.target);
377
            }
378
        }
379
        return fields;
380
    }
381
}