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 / AddFilesPanelImpl.java @ 2191

History | View | Annotate | Download (19.9 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.Arrays;
13
import java.util.List;
14

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

    
28
import org.gvsig.andami.IconThemeHelper;
29
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
30
import org.gvsig.gui.beans.swing.JFileChooser;
31
import org.gvsig.i18n.Messages;
32
import org.gvsig.raster.mosaic.swing.main.AddFilesEvent;
33
import org.gvsig.raster.mosaic.swing.main.AddFilesListener;
34
import org.gvsig.raster.mosaic.swing.main.AddFilesPanel;
35

    
36
/**
37
 * Main panel to add files in a mosaic
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class AddFilesPanelImpl extends JPanel implements ActionListener, AddFilesPanel {
41
        private static final long            serialVersionUID       = 1L;
42
        private String                       pathToImagesForTest    = "/src/main/resources/images/";
43
        private String                       lastFolder             = System.getProperty("user.home");
44
        
45
        private final String                 BYTE                   = "Byte";
46
        private final String                 SHORT                  = "Short";
47
        private final String                 INTEGER                = "Integer";
48
        private final String                 FLOAT                  = "Float";
49
        private final String                 DOUBLE                 = "Double";
50
        
51
        private JPanel                       arrowButtonsPanel      = null;
52
        private JButton                      downArrowButton        = null;
53
        private JButton                      upArrowButton          = null;
54
        private JButton                      addAllButton           = null;
55
        private JButton                      removeAllButton        = null;
56
        
57
        private String[]                     columnNames            = null;
58
        private JScrollPane                  scrollPanelSrc         = null;
59
        //private PagedTable                   pagedSrcTable          = null;
60
        private JTable                       tableSrc               = null;
61
        private DefaultTableModel            modelSrc               = null;
62
        
63
        private JScrollPane                  scrollPanelDst         = null;
64
        //private PagedTable                   pagedDstTable          = null;
65
        private JTable                       tableDst               = null;
66
        private DefaultTableModel            modelDst               = null;
67
        
68
        private JPanel                       buttonsPanel           = null;
69
        private JButton                      addDirButton           = null;
70
        private JButton                      addFilesButton         = null;
71
        private JButton                      optionsButton          = null;
72
        
73
        private JPanel                       optionsPanel           = null;
74
        private JComboBox                    nbandsCombo            = null;
75
        private JTextField                   datatypeTextField      = null;
76
        private JComboBox                    rgbTypeCombo           = null;
77
        private JCheckBox                    rgbCheckBox            = null;
78
        private List<TableEntry>             entriesSrc             = null;
79
        private List<TableEntry>             entriesDst             = null;
80
        
81
        private AddFilesListener             listener               = null;
82
        private List<String>                 suffixList             = null;
83
        
84
        public AddFilesPanelImpl(List<String> suffixList) {
85
                columnNames = new String[]{ Messages.getText("bands"), Messages.getText("file")};
86
                entriesSrc = new ArrayList<TableEntry>();
87
                entriesDst = new ArrayList<TableEntry>();
88
                this.suffixList = suffixList;
89
                initialize();
90
        }
91
        
92
        public AddFilesPanelImpl(String[] suffixList) {
93
                columnNames = new String[]{ Messages.getText("bands"), Messages.getText("file")};
94
                entriesSrc = new ArrayList<TableEntry>();
95
                entriesDst = new ArrayList<TableEntry>();
96
                this.suffixList = Arrays.asList(suffixList);
97
                initialize();
98
        }
99
        
100
        class TableEntry {
101
                Object obj   = null;
102
                String text  = null;
103
                int bandCount = 0;
104
                int dataType = 0;
105
                
106
                public TableEntry(String text, Object obj, int bandCount, int dataType) {
107
                        this.obj = obj;
108
                        this.text = text;
109
                        this.bandCount = bandCount;
110
                        this.dataType = dataType;
111
                }
112
                
113
                public String toString() {
114
                        return text;
115
                }
116
        }
117
        
118
        //*******************************************************
119
        //***************API Implementation**********************
120
        
121
        public void addSrcFile(Object data, String text, int nBands, int datatype) {
122
                TableEntry entry = new TableEntry(text, data, nBands, datatype);
123
                entriesSrc.add(entry);
124
                updateUISrcTable();
125
        }
126
        
127
        public void addDstFile(Object data, String text, int nBands, int datatype) {
128
                TableEntry entry = new TableEntry(text, data, nBands, datatype);
129
                entriesDst.add(entry);
130
                updateUIDstTable();
131
        }
132
        
133
        public void addListener(AddFilesListener filesListener) {
134
                this.listener = filesListener;
135
        }
136
        
137
        public JComponent getJComponent() {
138
                return this;
139
        }
140
        
141
        //*****************************************************
142
        
143
        private void updateUISrcTable() {
144
                modelSrc = new DefaultTableModel(columnNames, entriesSrc.size());
145
                getFileListSrc().setModel(modelSrc);
146
                getFileListSrc().getColumnModel().getColumn(0).setMaxWidth(55);
147
                for (int i = 0; i < entriesSrc.size(); i++) {
148
                        getFileListSrc().getModel().setValueAt(entriesSrc.get(i).bandCount, i, 0);
149
                        getFileListSrc().getModel().setValueAt(entriesSrc.get(i), i, 1);
150
                }
151
        }
152
        
153
        private void updateUIDstTable() {
154
                modelDst = new DefaultTableModel(columnNames, entriesDst.size());
155
                getFileListDst().setModel(modelDst);
156
                getFileListDst().getColumnModel().getColumn(0).setMaxWidth(55);
157
                for (int i = 0; i < entriesDst.size(); i++) {
158
                        getFileListDst().getModel().setValueAt(entriesDst.get(i).bandCount, i, 0);
159
                        getFileListDst().getModel().setValueAt(entriesDst.get(i), i, 1);
160
                }
161
                updateOutputDataTypes();
162
                updateOutputNumBands();
163
        }
164
        
165
        private void updateOutputNumBands() {
166
                if(entriesDst.size() == 0) {
167
                        setUIOutputNumBands(0);
168
                        return;
169
                }
170
                
171
                for (int i = 0; i < entriesDst.size(); i++) {
172
                        int currentOutputBands = getOutputNumBands();
173
                        if(entriesDst.get(i).bandCount > currentOutputBands)
174
                                setUIOutputNumBands(entriesDst.get(i).bandCount);
175
                }
176
                
177
                enableUIRGBOutput();
178
        }
179
        
180
        /**
181
         * Update output data types depending on the layer type.
182
         */
183
        private void updateOutputDataTypes() {
184
                if(entriesDst.size() == 0) {
185
                        getDatatypeJTextField().setText("");
186
                        return;
187
                }
188
                
189
                if(getDatatypeJTextField().getText().compareTo("") == 0)
190
                        getDatatypeJTextField().setText(BYTE);
191
                
192
                for (int i = 0; i < entriesDst.size(); i++) {
193
                        int currentDatatype = getDataTypeFromText(getDatatypeJTextField().getText());
194
                        int dataType = entriesDst.get(i).dataType;
195
                        switch (dataType) {
196
                        case Buffer.TYPE_DOUBLE:
197
                                setDataType(Buffer.TYPE_DOUBLE);
198
                                return;
199
                        case Buffer.TYPE_FLOAT:
200
                                if(currentDatatype == Buffer.TYPE_BYTE || currentDatatype == Buffer.TYPE_FLOAT)
201
                                        setDataType(Buffer.TYPE_FLOAT);
202
                                else
203
                                        setDataType(Buffer.TYPE_DOUBLE);
204
                                break;
205
                        case Buffer.TYPE_BYTE:
206
                                if(currentDatatype == Buffer.TYPE_BYTE)
207
                                        setDataType(Buffer.TYPE_BYTE);
208
                                break;
209
                        case Buffer.TYPE_SHORT:
210
                                if(currentDatatype < Buffer.TYPE_SHORT)
211
                                        setDataType(Buffer.TYPE_SHORT);
212
                                break;
213
                        case Buffer.TYPE_INT:
214
                                if(currentDatatype < Buffer.TYPE_INT)
215
                                        setDataType(Buffer.TYPE_INT);
216
                                break;
217
                        }
218
                }
219
        }
220
        
221
        private int getDataTypeFromText(String text) {
222
                if(text.compareTo(BYTE) == 0) 
223
                        return Buffer.TYPE_BYTE;
224
                if(text.compareTo(SHORT) == 0) 
225
                        return Buffer.TYPE_SHORT;
226
                if(text.compareTo(INTEGER) == 0) 
227
                        return Buffer.TYPE_INT;
228
                if(text.compareTo(FLOAT) == 0) 
229
                        return Buffer.TYPE_FLOAT;
230
                if(text.compareTo(DOUBLE) == 0) 
231
                        return Buffer.TYPE_DOUBLE;
232
                return -1;
233
        }
234
        
235
        private void setDataType(int datatype) {
236
                switch (datatype) {
237
                case Buffer.TYPE_BYTE:
238
                        getDatatypeJTextField().setText(BYTE);
239
                        break;
240
                case Buffer.TYPE_SHORT:
241
                        getDatatypeJTextField().setText(SHORT);
242
                        break;
243
                case Buffer.TYPE_INT:
244
                        getDatatypeJTextField().setText(INTEGER);
245
                        break;
246
                case Buffer.TYPE_FLOAT:
247
                        getDatatypeJTextField().setText(FLOAT);
248
                        break;
249
                case Buffer.TYPE_DOUBLE:
250
                        getDatatypeJTextField().setText(DOUBLE);
251
                        break;
252
                }
253
        }
254
        
255
        private void setUIOutputNumBands(int numBands) {
256
                getNbandsJComboBox().removeAllItems();
257
                for (int i = 0; i < numBands; i++) {
258
                        getNbandsJComboBox().addItem((i + 1) + "");
259
                }
260
                getNbandsJComboBox().setSelectedIndex(getNbandsJComboBox().getItemCount() - 1);
261
        }
262
        
263
        private int getOutputNumBands() {
264
                String value = (String)getNbandsJComboBox().getSelectedItem();
265
                try {
266
                        return Integer.valueOf(value);
267
                } catch (NumberFormatException e) {
268
                        return -1;
269
                }
270
        }
271
        
272
        private void enableUIRGBOutput() {
273
                int outputNumBands = getOutputNumBands();
274
                boolean enable = outputNumBands >= 3;
275
                
276
                getRGBOutputCheckBox().setEnabled(enable);
277
                if(!enable) {
278
                        getRGBTypeComboBox().setSelectedIndex(0);
279
                        getRGBTypeComboBox().setEnabled(enable);
280
                } else {
281
                        if(getRGBOutputCheckBox().isSelected()) {
282
                                //solo con 4 bandas o m?s se puede elegir el tipo de interpretaci?n de color
283
                                if(outputNumBands >= 4) { 
284
                                        if(getRGBTypeComboBox().getSelectedIndex() == 0)
285
                                                getRGBTypeComboBox().setSelectedIndex(1);
286
                                        getRGBTypeComboBox().setEnabled(true);
287
                                } else { 
288
                                        getRGBTypeComboBox().setSelectedIndex(2);
289
                                        getRGBTypeComboBox().setEnabled(false);
290
                                }
291
                        } else {
292
                                getRGBTypeComboBox().setSelectedIndex(0);
293
                                getRGBTypeComboBox().setEnabled(false);
294
                        }
295
                }
296
                
297
        }
298
        
299
        protected void initialize() {
300
                enableUIRGBOutput();
301
                setLayout(new GridBagLayout());
302
                
303
                GridBagConstraints gbc = new GridBagConstraints();
304
                gbc.insets = new Insets(2, 2, 2, 2);
305
                gbc.fill = GridBagConstraints.BOTH;
306
                gbc.weightx = 1.0;
307
                gbc.weighty = 1.0;
308
                gbc.gridwidth = 2;
309
                add(getScrollTableSrc(), gbc);
310
                
311
                gbc.gridy = 2;
312
                add(getScrollTableDst(), gbc);
313
                
314
                gbc.fill = GridBagConstraints.HORIZONTAL;
315
                gbc.weighty = 0.0;
316
                
317
                gbc.gridy = 1;
318
                add(getIconsPanel(), gbc);
319
                
320
                gbc.gridy = 3;
321
                add(getButtonsPanel(), gbc);
322
                
323
                gbc.gridwidth = 1;
324
                gbc.gridx = 0;
325
                gbc.gridy = 4;
326
                add(new JPanel(), gbc);
327
                
328
                gbc.fill = GridBagConstraints.NONE;
329
                gbc.weightx = 0.0;
330
                gbc.gridx = 1;
331
                gbc.gridy = 4;
332
                add(getOptionsPanel(), gbc);
333
        }
334
        
335
        //******************************************************
336
        
337
        public JPanel getOptionsPanel() {
338
                if(optionsPanel == null) {
339
                        optionsPanel = new JPanel(new GridBagLayout());
340
                        //optionsPanel.setBorder(BorderFactory.createLineBorder(Color.red));
341
                        JLabel l1 = new JLabel(Messages.getText("n_output_bands"));
342
                        JLabel l2 = new JLabel(Messages.getText("output_datatype"));
343
                        
344
                        GridBagConstraints gbc = new GridBagConstraints();
345
                        gbc.insets = new Insets(0, 0, 2, 20);
346
                        gbc.fill = GridBagConstraints.HORIZONTAL;
347

    
348
                        gbc.gridx = 0;
349
                        gbc.gridy = 0;
350
                        optionsPanel.add(l1, gbc);
351
                        
352
                        gbc.gridx = 1;
353
                        gbc.gridy = 0;
354
                        optionsPanel.add(getNbandsJComboBox(), gbc);
355
                        
356
                        gbc.gridx = 0;
357
                        gbc.gridy = 1;
358
                        optionsPanel.add(l2, gbc);
359
                        
360
                        gbc.gridx = 1;
361
                        gbc.gridy = 1;
362
                        optionsPanel.add(getDatatypeJTextField(), gbc);
363
                        
364
                        gbc.gridx = 0;
365
                        gbc.gridy = 2;
366
                        optionsPanel.add(getRGBOutputCheckBox(), gbc);
367
                        
368
                        gbc.gridx = 1;
369
                        gbc.gridy = 2;
370
                        optionsPanel.add(getRGBTypeComboBox(), gbc);
371
                }
372
                return optionsPanel;
373
        }
374
        
375
        public JComboBox getNbandsJComboBox() {
376
                if(nbandsCombo == null) {
377
                        nbandsCombo = new JComboBox();
378
                        nbandsCombo.addItem("0");
379
                        nbandsCombo.addActionListener(this);
380
                }
381
                return nbandsCombo;
382
        }
383
        
384
        public JTextField getDatatypeJTextField() {
385
                if(datatypeTextField == null) {
386
                        datatypeTextField = new JTextField();
387
                        datatypeTextField.setEditable(false);
388
                        datatypeTextField.setPreferredSize(new Dimension(30, 30));
389
                }
390
                return datatypeTextField;
391
        }
392
        
393
        public JComboBox getRGBTypeComboBox() {
394
                if(rgbTypeCombo == null) {
395
                        rgbTypeCombo = new JComboBox();
396
                        rgbTypeCombo.addItem("------");
397
                        rgbTypeCombo.addItem("ARGB");
398
                        rgbTypeCombo.addItem("RGB");
399
                        rgbTypeCombo.addItem("BGR IR");
400
                }
401
                return rgbTypeCombo;
402
        }
403
        
404
        public JCheckBox getRGBOutputCheckBox() {
405
                if(rgbCheckBox == null) {
406
                        rgbCheckBox = new JCheckBox(Messages.getText("rgb_output"));
407
                        rgbCheckBox.addActionListener(this);
408
                }
409
                return rgbCheckBox;
410
        }
411
        
412
        //******************************************************
413
        
414
        public JPanel getButtonsPanel() {
415
                if(buttonsPanel == null) {
416
                        FlowLayout fl = new FlowLayout();
417
                        fl.setAlignment(FlowLayout.LEFT);
418
                        buttonsPanel = new JPanel(fl);
419
                        //iconsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
420
                        buttonsPanel.add(getAddDirButton());
421
                        buttonsPanel.add(getAddFilesButton());
422
                        buttonsPanel.add(getOptionsButton());
423
                }
424
                return buttonsPanel;
425
        }
426
        
427
        public JButton getAddDirButton() {
428
                if(addDirButton == null) {
429
                        addDirButton = new JButton(Messages.getText("add_dir"));
430
                        addDirButton.addActionListener(this);
431
                }
432
                return addDirButton;
433
        }
434
        
435
        public JButton getAddFilesButton() {
436
                if(addFilesButton == null) {
437
                        addFilesButton = new JButton(Messages.getText("add_files"));
438
                        addFilesButton.addActionListener(this);
439
                }
440
                return addFilesButton;
441
        }
442
        
443
        public JButton getOptionsButton() {
444
                if(optionsButton == null) {
445
                        optionsButton = new JButton(Messages.getText("options"));
446
                }
447
                return optionsButton;
448
        }
449
        
450
        //******************************************************
451

    
452
        public JScrollPane getScrollTableDst() {
453
                if(scrollPanelDst == null) {
454
                        scrollPanelDst = new JScrollPane();
455
                        scrollPanelDst.setViewportView(getFileListDst());
456
                }
457
                return scrollPanelDst;
458
        }
459
        
460
        public JTable getFileListDst() {
461
                if(tableDst == null) {
462
                        tableDst = new JTable(getModelDst());
463
                        TableColumnModel columnModel = tableDst.getColumnModel();
464
                        columnModel.getColumn(0).setMaxWidth(55);
465
                }
466
                return tableDst;
467
        }
468
        
469
        public DefaultTableModel getModelDst() {
470
                if(modelDst == null)
471
                        modelDst = new DefaultTableModel(columnNames, 0);
472
                return modelDst;
473
        }
474
        
475
        
476
        //******************************************************
477
        
478
        
479
        public JScrollPane getScrollTableSrc() {
480
                if(scrollPanelSrc == null) {
481
                        scrollPanelSrc = new JScrollPane();
482
                        scrollPanelSrc.setViewportView(getFileListSrc());
483
                }
484
                return scrollPanelSrc;
485
        }
486
        
487
        public JTable getFileListSrc() {
488
                if(tableSrc == null) {
489
                        tableSrc = new JTable(getModelSrc());
490
                        TableColumnModel columnModel = tableSrc.getColumnModel();
491
                        columnModel.getColumn(0).setMaxWidth(55);
492
                }
493
                return tableSrc;
494
        }
495
        
496
        public DefaultTableModel getModelSrc() {
497
                if(modelSrc == null)
498
                        modelSrc = new DefaultTableModel(columnNames, 0);
499
                return modelSrc;
500
        }
501
        
502
        //******************************************************
503

    
504
        public JPanel getIconsPanel() {
505
                if(arrowButtonsPanel == null) {
506
                        FlowLayout fl = new FlowLayout();
507
                        fl.setAlignment(FlowLayout.CENTER);
508
                        arrowButtonsPanel = new JPanel(fl);
509
                        //iconsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
510
                        fl.setHgap(2);
511
                        arrowButtonsPanel.add(getUpArrowButton());
512
                        arrowButtonsPanel.add(getDownArrowButton());
513
                        arrowButtonsPanel.add(getAddAllButton());
514
                        arrowButtonsPanel.add(getRemoveAllButton());
515
                }
516
                return arrowButtonsPanel;
517
        }
518
        
519
        public JButton getUpArrowButton() {
520
                if(upArrowButton == null) {
521
                        upArrowButton = new JButton(loadIcon("up-16x16-icon"));
522
                        upArrowButton.addActionListener(this);
523
                }
524
                return upArrowButton;
525
        }
526
        
527
        public JButton getDownArrowButton() {
528
                if(downArrowButton == null) {
529
                        downArrowButton = new JButton(loadIcon("down-16x16-icon"));
530
                        downArrowButton.addActionListener(this);
531
                }
532
                return downArrowButton;
533
        }
534
        
535
        public JButton getAddAllButton() {
536
                if(addAllButton == null) {
537
                        addAllButton = new JButton(loadIcon("downred-16x16-icon"));
538
                        addAllButton.addActionListener(this);
539
                }
540
                return addAllButton;
541
        }
542
        
543
        public JButton getRemoveAllButton() {
544
                if(removeAllButton == null) {
545
                        removeAllButton = new JButton(loadIcon("upred-16x16-icon"));
546
                        removeAllButton.addActionListener(this);
547
                }
548
                return removeAllButton;
549
        }
550
        
551
        //******************************************************
552
        
553
        private ImageIcon loadIcon(String iconName) {
554
                ImageIcon icon = null;
555
                try {
556
                        icon = IconThemeHelper.getImageIcon(iconName);
557
                } catch(NullPointerException e) {}
558
                String path = System.getProperty("user.dir") + pathToImagesForTest + iconName + ".png"; 
559
                if(new File(path).exists())
560
                        icon = new ImageIcon(path, "");
561
                if(icon == null) 
562
                        icon = new ImageIcon(System.getProperty("user.dir") + pathToImagesForTest + iconName + ".gif", "");
563
                return icon;
564
        }
565

    
566
        public void actionPerformed(ActionEvent e) {
567
                //Cambio del n?mero de bandas de las salida
568
                if(e.getSource() == getNbandsJComboBox()) {
569
                        enableUIRGBOutput();
570
                }
571
                
572
                //Activar/desactivar el check de RGB
573
                if(e.getSource() == getRGBOutputCheckBox()) {
574
                        enableUIRGBOutput();
575
                }
576
                
577
                //Eliminar todas de las capas de salida
578
                if(e.getSource() == getRemoveAllButton()) {
579
                        for (int i = 0; i < entriesDst.size(); i++) {
580
                                entriesSrc.add(entriesDst.get(i));
581
                        }
582
                        entriesDst.clear();
583
                        updateUISrcTable();
584
                        updateUIDstTable();
585
                }
586

    
587
                //A?adir todas de las capas de salida
588
                if(e.getSource() == getAddAllButton()) {
589
                        for (int i = 0; i < entriesSrc.size(); i++) {
590
                                entriesDst.add(entriesSrc.get(i));
591
                        }
592
                        entriesSrc.clear();
593
                        updateUISrcTable();
594
                        updateUIDstTable();
595
                }
596
                
597
                //A?adir la seleccionada a la salida
598
                if(e.getSource() == getDownArrowButton()) {
599
                        int[] rows = getFileListSrc().getSelectedRows();
600
                        
601
                        for (int i = 0; i < rows.length; i++) {
602
                                TableEntry entry = entriesSrc.remove(rows[i]);
603
                                entriesDst.add(entry);
604
                        }
605
                        
606
                        updateUISrcTable();
607
                        updateUIDstTable();
608
                }
609
                
610
                //Eliminar la seleccionada de la salida
611
                if(e.getSource() == getUpArrowButton()) {
612
                        int[] rows = getFileListDst().getSelectedRows();
613
                        
614
                        for (int i = 0; i < rows.length; i++) {
615
                                TableEntry entry = entriesDst.remove(rows[i]);
616
                                entriesSrc.add(entry);
617
                        }
618
                        
619
                        updateUISrcTable();
620
                        updateUIDstTable();
621
                }
622
                
623
                //A?ade ficheros desde disco
624
                if(e.getSource() == getAddFilesButton()) {
625
                        if(listener != null) {
626
                                JFileChooser fileChooser = createJFileChooser(false);
627
                                int result = fileChooser.showOpenDialog(this);
628

    
629
                                if (result == JFileChooser.APPROVE_OPTION) {
630
                                        File[] files = fileChooser.getSelectedFiles();
631
                                        listener.actionAddFiles(new AddFilesEvent(getAddFilesButton(), files));
632
                                }
633
                        }
634
                }
635
                
636
                //Elimina ficheros desde disco
637
                if(e.getSource() == getAddDirButton()) {
638
                        if(listener != null) {
639
                                JFileChooser fileChooser = createJFileChooser(true);
640
                                int result = fileChooser.showOpenDialog(this);
641

    
642
                                if (result == JFileChooser.APPROVE_OPTION) {
643
                                        File folder = fileChooser.getSelectedFile();
644
                                        String[] list = folder.list();
645
                                        File[] files = filterFileList(list, folder);
646
                                        listener.actionAddFolder(new AddFilesEvent(getAddFilesButton(), files));
647
                                }
648
                        }
649
                }
650
        }
651
        
652
        private File[] filterFileList(String[] f, File folder) {
653
                List<File> list = new ArrayList<File>();
654
                for (int i = 0; i < f.length; i++) {
655
                        if(isInList(f[i]))
656
                                list.add(new File(folder.getAbsolutePath() + File.separator + f[i]));
657
                }
658
                return list.toArray(new File[list.size()]);
659
        }
660
        
661
        private boolean isInList(String name) {
662
                for (int i = 0; i < suffixList.size(); i++) {
663
                        String suffix = suffixList.get(i);
664
                        if(name.endsWith("." + suffix))
665
                                return true;
666
                }
667
                return false;
668
        }
669
        
670
        protected JFileChooser createJFileChooser(boolean folder) {
671
                JFileChooser fileChooser = new JFileChooser(lastFolder, JFileChooser.getLastPath(lastFolder, null));
672
                fileChooser.setMultiSelectionEnabled(true);
673
                fileChooser.setAcceptAllFileFilterUsed(false);
674
                fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
675
                if(folder) {
676
                        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
677
                        fileChooser.addChoosableFileFilter(new RasterFormatFileFilter());
678
                } else {
679
                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
680
                        fileChooser.addChoosableFileFilter(new RasterFormatFileFilter(suffixList));
681
                }
682
                return fileChooser;
683
        }
684
}