Statistics
| Revision:

gvsig-raster / org.gvsig.raster.mosaic / trunk / org.gvsig.raster.mosaic / org.gvsig.raster.mosaic.swing / org.gvsig.raster.mosaic.swing.impl / src / main / java / org / gvsig / raster / mosaic / swing / impl / main / AddFilesPanel.java @ 2188

History | View | Annotate | Download (14.6 KB)

1
package org.gvsig.raster.mosaic.swing.impl.main;
2

    
3
import java.awt.Dimension;
4
import java.awt.FlowLayout;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.Insets;
8
import java.awt.event.ActionEvent;
9
import java.awt.event.ActionListener;
10
import java.io.File;
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
import javax.swing.ImageIcon;
15
import javax.swing.JButton;
16
import javax.swing.JCheckBox;
17
import javax.swing.JComboBox;
18
import javax.swing.JLabel;
19
import javax.swing.JPanel;
20
import javax.swing.JScrollPane;
21
import javax.swing.JTable;
22
import javax.swing.JTextField;
23
import javax.swing.table.DefaultTableModel;
24
import javax.swing.table.TableColumnModel;
25

    
26
import org.gvsig.andami.IconThemeHelper;
27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28
import org.gvsig.i18n.Messages;
29

    
30
public class AddFilesPanel extends JPanel implements ActionListener {
31
        private static final long            serialVersionUID       = 1L;
32
        private String                       pathToImagesForTest    = "/src/main/resources/images/";
33
        
34
        private JPanel                       arrowButtonsPanel      = null;
35
        private JButton                      downArrowButton        = null;
36
        private JButton                      upArrowButton          = null;
37
        private JButton                      addAllButton           = null;
38
        private JButton                      removeAllButton        = null;
39
        
40
        private String[]                     columnNames            = null;
41
        private JScrollPane                  scrollPanelSrc         = null;
42
        //private PagedTable                   pagedSrcTable          = null;
43
        private JTable                       tableSrc               = null;
44
        private DefaultTableModel            modelSrc               = null;
45
        
46
        private JScrollPane                  scrollPanelDst         = null;
47
        //private PagedTable                   pagedDstTable          = null;
48
        private JTable                       tableDst               = null;
49
        private DefaultTableModel            modelDst               = null;
50
        
51
        private JPanel                       buttonsPanel           = null;
52
        private JButton                      addDirButton           = null;
53
        private JButton                      addFilesButton         = null;
54
        private JButton                      optionsButton          = null;
55
        
56
        private JPanel                       optionsPanel           = null;
57
        private JComboBox                    nbandsCombo            = null;
58
        private JTextField                   datatypeTextField      = null;
59
        private JComboBox                    rgbTypeCombo           = null;
60
        private JCheckBox                    rgbCheckBox            = null;
61
        private List<TableEntry>             entriesSrc             = null;
62
        private List<TableEntry>             entriesDst             = null;
63
        
64
        public AddFilesPanel() {
65
                columnNames = new String[]{ Messages.getText("bands"), Messages.getText("file")};
66
                entriesSrc = new ArrayList<TableEntry>();
67
                entriesDst = new ArrayList<TableEntry>();
68
                initialize();
69
        }
70
        
71
        class TableEntry {
72
                Object obj   = null;
73
                String text  = null;
74
                int bandCount = 0;
75
                
76
                public TableEntry(String text, Object obj, int bandCount) {
77
                        this.obj = obj;
78
                        this.text = text;
79
                        this.bandCount = bandCount;
80
                }
81
                
82
                public String toString() {
83
                        return text;
84
                }
85
        }
86
        
87
        public void addFile(Object data, String text, int nBands) {
88
                TableEntry entry = new TableEntry(text, data, nBands);
89
                entriesSrc.add(entry);
90
                updateSrcTable();
91
        }
92
        
93
        private void updateSrcTable() {
94
                modelSrc = new DefaultTableModel(columnNames, entriesSrc.size());
95
                getFileListSrc().setModel(modelSrc);
96
                getFileListSrc().getColumnModel().getColumn(0).setMaxWidth(40);
97
                for (int i = 0; i < entriesSrc.size(); i++) {
98
                        getFileListSrc().getModel().setValueAt(entriesSrc.get(i).bandCount, i, 0);
99
                        getFileListSrc().getModel().setValueAt(entriesSrc.get(i), i, 1);
100
                }
101
        }
102
        
103
        private void updateDstTable() {
104
                modelDst = new DefaultTableModel(columnNames, entriesDst.size());
105
                getFileListDst().setModel(modelDst);
106
                getFileListDst().getColumnModel().getColumn(0).setMaxWidth(40);
107
                for (int i = 0; i < entriesDst.size(); i++) {
108
                        getFileListDst().getModel().setValueAt(entriesDst.get(i).bandCount, i, 0);
109
                        getFileListDst().getModel().setValueAt(entriesDst.get(i), i, 1);
110
                }
111
        }
112
        
113
        public void setDataType(int datatype) {
114
                switch (datatype) {
115
                case Buffer.TYPE_BYTE:
116
                        getDatatypeJTextField().setText("Byte");
117
                        break;
118
                case Buffer.TYPE_SHORT:
119
                        getDatatypeJTextField().setText("Short");
120
                        break;
121
                case Buffer.TYPE_INT:
122
                        getDatatypeJTextField().setText("Integer");
123
                        break;
124
                case Buffer.TYPE_FLOAT:
125
                        getDatatypeJTextField().setText("Float");
126
                        break;
127
                case Buffer.TYPE_DOUBLE:
128
                        getDatatypeJTextField().setText("Double");
129
                        break;
130
                }
131
        }
132
        
133
        public void setOutputNumBands(int numBands) {
134
                getNbandsJComboBox().removeAllItems();
135
                for (int i = 0; i < numBands; i++) {
136
                        getNbandsJComboBox().addItem((i + 1) + "");
137
                }
138
                getNbandsJComboBox().setSelectedIndex(getNbandsJComboBox().getItemCount() - 1);
139
        }
140
        
141
        protected void initialize() {
142
                setLayout(new GridBagLayout());
143
                
144
                GridBagConstraints gbc = new GridBagConstraints();
145
                gbc.insets = new Insets(2, 2, 2, 2);
146
                gbc.fill = GridBagConstraints.BOTH;
147
                gbc.weightx = 1.0;
148
                gbc.weighty = 1.0;
149
                gbc.gridwidth = 2;
150
                add(getScrollTableSrc(), gbc);
151
                
152
                gbc.gridy = 2;
153
                add(getScrollTableDst(), gbc);
154
                
155
                gbc.fill = GridBagConstraints.HORIZONTAL;
156
                gbc.weighty = 0.0;
157
                
158
                gbc.gridy = 1;
159
                add(getIconsPanel(), gbc);
160
                
161
                gbc.gridy = 3;
162
                add(getButtonsPanel(), gbc);
163
                
164
                gbc.gridwidth = 1;
165
                gbc.gridx = 0;
166
                gbc.gridy = 4;
167
                add(new JPanel(), gbc);
168
                
169
                gbc.fill = GridBagConstraints.NONE;
170
                gbc.weightx = 0.0;
171
                gbc.gridx = 1;
172
                gbc.gridy = 4;
173
                add(getOptionsPanel(), gbc);
174
        }
175
        
176
        //******************************************************
177
        
178
        public JPanel getOptionsPanel() {
179
                if(optionsPanel == null) {
180
                        optionsPanel = new JPanel(new GridBagLayout());
181
                        //optionsPanel.setBorder(BorderFactory.createLineBorder(Color.red));
182
                        JLabel l1 = new JLabel(Messages.getText("n_output_bands"));
183
                        JLabel l2 = new JLabel(Messages.getText("output_datatype"));
184
                        
185
                        GridBagConstraints gbc = new GridBagConstraints();
186
                        gbc.insets = new Insets(0, 0, 2, 20);
187
                        gbc.fill = GridBagConstraints.HORIZONTAL;
188

    
189
                        gbc.gridx = 0;
190
                        gbc.gridy = 0;
191
                        optionsPanel.add(l1, gbc);
192
                        
193
                        gbc.gridx = 1;
194
                        gbc.gridy = 0;
195
                        optionsPanel.add(getNbandsJComboBox(), gbc);
196
                        
197
                        gbc.gridx = 0;
198
                        gbc.gridy = 1;
199
                        optionsPanel.add(l2, gbc);
200
                        
201
                        gbc.gridx = 1;
202
                        gbc.gridy = 1;
203
                        optionsPanel.add(getDatatypeJTextField(), gbc);
204
                        
205
                        gbc.gridx = 0;
206
                        gbc.gridy = 2;
207
                        optionsPanel.add(getRGBOutputCheckBox(), gbc);
208
                        
209
                        gbc.gridx = 1;
210
                        gbc.gridy = 2;
211
                        optionsPanel.add(getRGBTypeComboBox(), gbc);
212
                }
213
                return optionsPanel;
214
        }
215
        
216
        public JComboBox getNbandsJComboBox() {
217
                if(nbandsCombo == null) {
218
                        nbandsCombo = new JComboBox();
219
                }
220
                return nbandsCombo;
221
        }
222
        
223
        public JTextField getDatatypeJTextField() {
224
                if(datatypeTextField == null) {
225
                        datatypeTextField = new JTextField();
226
                        datatypeTextField.setEditable(false);
227
                        datatypeTextField.setPreferredSize(new Dimension(30, 30));
228
                }
229
                return datatypeTextField;
230
        }
231
        
232
        public JComboBox getRGBTypeComboBox() {
233
                if(rgbTypeCombo == null) {
234
                        rgbTypeCombo = new JComboBox();
235
                        rgbTypeCombo.addItem("------");
236
                        rgbTypeCombo.addItem("ARGB");
237
                        rgbTypeCombo.addItem("RGB");
238
                        rgbTypeCombo.addItem("BGR IR");
239
                        rgbTypeCombo.setEnabled(false);
240
                }
241
                return rgbTypeCombo;
242
        }
243
        
244
        public JCheckBox getRGBOutputCheckBox() {
245
                if(rgbCheckBox == null) {
246
                        rgbCheckBox = new JCheckBox(Messages.getText("rgb_output"));
247
                        rgbCheckBox.addActionListener(this);
248
                }
249
                return rgbCheckBox;
250
        }
251
        
252
        //******************************************************
253
        
254
        public JPanel getButtonsPanel() {
255
                if(buttonsPanel == null) {
256
                        FlowLayout fl = new FlowLayout();
257
                        fl.setAlignment(FlowLayout.LEFT);
258
                        buttonsPanel = new JPanel(fl);
259
                        //iconsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
260
                        buttonsPanel.add(getAddDirButton());
261
                        buttonsPanel.add(getAddFilesButton());
262
                        buttonsPanel.add(getOptionsButton());
263
                }
264
                return buttonsPanel;
265
        }
266
        
267
        public JButton getAddDirButton() {
268
                if(addDirButton == null) {
269
                        addDirButton = new JButton(Messages.getText("add_dir"));
270
                }
271
                return addDirButton;
272
        }
273
        
274
        public JButton getAddFilesButton() {
275
                if(addFilesButton == null) {
276
                        addFilesButton = new JButton(Messages.getText("add_files"));
277
                }
278
                return addFilesButton;
279
        }
280
        
281
        public JButton getOptionsButton() {
282
                if(optionsButton == null) {
283
                        optionsButton = new JButton(Messages.getText("options"));
284
                }
285
                return optionsButton;
286
        }
287
        
288
        //******************************************************
289
        
290
        /*public PagedTable getPagedTableDst() {
291
                if(pagedDstTable == null) {
292
                        RasterSwingManager manager = null;
293
                        try {
294
                                manager = RasterSwingLocator.getSwingManager();
295
                        } catch (Exception e) {
296
                        }
297
                        
298
                        String[] columnNames = {
299
                                        Messages.getText("bands"), 
300
                                        Messages.getText("files")};
301
                        int[] columnSizes = {50, -1};
302

303
                        FileListModel model = new FileListModel(columnNames);
304
                        ModelLoader loader = null;
305
                        if(manager != null)
306
                                loader = manager.createModelLoader(model);
307
                        else 
308
                                loader = new org.gvsig.raster.swing.impl.pagedtable.ModelLoaderImpl(model);
309
                        
310
                        loader.setColumnNames(columnNames);
311
                        loader.setColumnWidths(columnSizes);
312
                        
313
                        if(manager != null)
314
                                pagedDstTable = manager.createPagedTable(loader);
315
                        else
316
                                pagedDstTable = new org.gvsig.raster.swing.impl.pagedtable.PagedTableImpl(loader);
317
                        pagedDstTable.showControllerTable(false);
318
                }
319
                return pagedDstTable;
320
        }*/
321

    
322
        public JScrollPane getScrollTableDst() {
323
                if(scrollPanelDst == null) {
324
                        scrollPanelDst = new JScrollPane();
325
                        scrollPanelDst.setViewportView(getFileListDst());
326
                }
327
                return scrollPanelDst;
328
        }
329
        
330
        public JTable getFileListDst() {
331
                if(tableDst == null) {
332
                        tableDst = new JTable(getModelDst());
333
                        TableColumnModel columnModel = tableDst.getColumnModel();
334
                        columnModel.getColumn(0).setMaxWidth(40);
335
                }
336
                return tableDst;
337
        }
338
        
339
        public DefaultTableModel getModelDst() {
340
                if(modelDst == null)
341
                        modelDst = new DefaultTableModel(columnNames, 0);
342
                return modelDst;
343
        }
344
        
345
        
346
        //******************************************************
347
        
348
        /*public PagedTable getPagedTableSrc() {
349
                if(pagedSrcTable == null) {
350
                        RasterSwingManager manager = null;
351
                        try {
352
                                manager = RasterSwingLocator.getSwingManager();
353
                        } catch (Exception e) {
354
                        }
355
                        
356
                        String[] columnNames = {
357
                                        Messages.getText("bands"), 
358
                                        Messages.getText("files")};
359
                        int[] columnSizes = {50, -1};
360

361
                        FileListModel model = new FileListModel(columnNames);
362
                        ModelLoader loader = null;
363
                        if(manager != null)
364
                                loader = manager.createModelLoader(model);
365
                        else 
366
                                loader = new org.gvsig.raster.swing.impl.pagedtable.ModelLoaderImpl(model);
367
                        
368
                        loader.setColumnNames(columnNames);
369
                        loader.setColumnWidths(columnSizes);
370
                        
371
                        if(manager != null)
372
                                pagedSrcTable = manager.createPagedTable(loader);
373
                        else
374
                                pagedSrcTable = new org.gvsig.raster.swing.impl.pagedtable.PagedTableImpl(loader);
375
                        pagedSrcTable.showControllerTable(false);
376
                        pagedSrcTable.showMoveRowsControls(false);
377
                }
378
                return pagedSrcTable;
379
        }*/
380
        
381
        public JScrollPane getScrollTableSrc() {
382
                if(scrollPanelSrc == null) {
383
                        scrollPanelSrc = new JScrollPane();
384
                        scrollPanelSrc.setViewportView(getFileListSrc());
385
                }
386
                return scrollPanelSrc;
387
        }
388
        
389
        public JTable getFileListSrc() {
390
                if(tableSrc == null) {
391
                        tableSrc = new JTable(getModelSrc());
392
                        TableColumnModel columnModel = tableSrc.getColumnModel();
393
                        columnModel.getColumn(0).setMinWidth(40);
394
                        columnModel.getColumn(0).setMaxWidth(40);
395
                }
396
                return tableSrc;
397
        }
398
        
399
        public DefaultTableModel getModelSrc() {
400
                if(modelSrc == null)
401
                        modelSrc = new DefaultTableModel(columnNames, 0);
402
                return modelSrc;
403
        }
404
        
405
        //******************************************************
406

    
407
        public JPanel getIconsPanel() {
408
                if(arrowButtonsPanel == null) {
409
                        FlowLayout fl = new FlowLayout();
410
                        fl.setAlignment(FlowLayout.CENTER);
411
                        arrowButtonsPanel = new JPanel(fl);
412
                        //iconsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
413
                        fl.setHgap(2);
414
                        arrowButtonsPanel.add(getUpArrowButton());
415
                        arrowButtonsPanel.add(getDownArrowButton());
416
                        arrowButtonsPanel.add(getAddAllButton());
417
                        arrowButtonsPanel.add(getRemoveAllButton());
418
                }
419
                return arrowButtonsPanel;
420
        }
421
        
422
        public JButton getUpArrowButton() {
423
                if(upArrowButton == null) {
424
                        upArrowButton = new JButton(loadIcon("up-16x16-icon"));
425
                        upArrowButton.addActionListener(this);
426
                }
427
                return upArrowButton;
428
        }
429
        
430
        public JButton getDownArrowButton() {
431
                if(downArrowButton == null) {
432
                        downArrowButton = new JButton(loadIcon("down-16x16-icon"));
433
                        downArrowButton.addActionListener(this);
434
                }
435
                return downArrowButton;
436
        }
437
        
438
        public JButton getAddAllButton() {
439
                if(addAllButton == null) {
440
                        addAllButton = new JButton(loadIcon("downred-16x16-icon"));
441
                        addAllButton.addActionListener(this);
442
                }
443
                return addAllButton;
444
        }
445
        
446
        public JButton getRemoveAllButton() {
447
                if(removeAllButton == null) {
448
                        removeAllButton = new JButton(loadIcon("upred-16x16-icon"));
449
                        removeAllButton.addActionListener(this);
450
                }
451
                return removeAllButton;
452
        }
453
        
454
        //******************************************************
455
        
456
        private ImageIcon loadIcon(String iconName) {
457
                ImageIcon icon = null;
458
                try {
459
                        icon = IconThemeHelper.getImageIcon(iconName);
460
                } catch(NullPointerException e) {}
461
                String path = System.getProperty("user.dir") + pathToImagesForTest + iconName + ".png"; 
462
                if(new File(path).exists())
463
                        icon = new ImageIcon(path, "");
464
                if(icon == null) 
465
                        icon = new ImageIcon(System.getProperty("user.dir") + pathToImagesForTest + iconName + ".gif", "");
466
                return icon;
467
        }
468

    
469
        public void actionPerformed(ActionEvent e) {
470
                if(e.getSource() == getRGBOutputCheckBox()) {
471
                        if(getRGBOutputCheckBox().isSelected())
472
                                getRGBTypeComboBox().setEnabled(true);
473
                        else
474
                                getRGBTypeComboBox().setEnabled(false);
475
                }
476
                
477
                if(e.getSource() == getRemoveAllButton()) {
478
                        for (int i = 0; i < entriesDst.size(); i++) {
479
                                entriesSrc.add(entriesDst.get(i));
480
                        }
481
                        entriesDst.clear();
482
                        updateSrcTable();
483
                        updateDstTable();
484
                }
485
                
486
                if(e.getSource() == getAddAllButton()) {
487
                        for (int i = 0; i < entriesSrc.size(); i++) {
488
                                entriesDst.add(entriesSrc.get(i));
489
                        }
490
                        entriesSrc.clear();
491
                        updateSrcTable();
492
                        updateDstTable();
493
                }
494
                
495
                if(e.getSource() == getDownArrowButton()) {
496
                        int[] rows = getFileListSrc().getSelectedRows();
497
                        
498
                        for (int i = 0; i < rows.length; i++) {
499
                                TableEntry entry = entriesSrc.remove(rows[i]);
500
                                entriesDst.add(entry);
501
                        }
502
                        
503
                        updateSrcTable();
504
                        updateDstTable();
505
                }
506
                
507
                if(e.getSource() == getUpArrowButton()) {
508
                        int[] rows = getFileListDst().getSelectedRows();
509
                        
510
                        for (int i = 0; i < rows.length; i++) {
511
                                TableEntry entry = entriesDst.remove(rows[i]);
512
                                entriesSrc.add(entry);
513
                        }
514
                        
515
                        updateSrcTable();
516
                        updateDstTable();
517
                }
518
        }
519
}