Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerWizardPanel.java @ 38441

History | View | Annotate | Download (22.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 IVER T.I. S.A.   {{Task}}
26
 */
27
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing;
28

    
29
import java.awt.Component;
30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
32
import java.awt.Insets;
33
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionListener;
35
import java.io.File;
36
import java.util.ArrayList;
37
import java.util.Iterator;
38
import java.util.List;
39
import java.util.prefs.Preferences;
40

    
41
import javax.swing.AbstractListModel;
42
import javax.swing.JButton;
43
import javax.swing.JList;
44
import javax.swing.JPanel;
45
import javax.swing.JScrollPane;
46
import javax.swing.ScrollPaneConstants;
47
import javax.swing.event.ListSelectionEvent;
48
import javax.swing.event.ListSelectionListener;
49
import javax.swing.filechooser.FileFilter;
50

    
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
import org.gvsig.andami.PluginServices;
55
import org.gvsig.andami.messages.Messages;
56
import org.gvsig.andami.messages.NotificationManager;
57
import org.gvsig.app.ApplicationLocator;
58
import org.gvsig.app.ApplicationManager;
59
import org.gvsig.app.gui.WizardPanel;
60
import org.gvsig.app.prepareAction.PrepareContext;
61
import org.gvsig.fmap.dal.DALLocator;
62
import org.gvsig.fmap.dal.DataManager;
63
import org.gvsig.fmap.dal.DataStoreParameters;
64
import org.gvsig.fmap.dal.exception.DataException;
65
import org.gvsig.fmap.dal.exception.FileNotFoundException;
66
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter;
67
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
68
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
69
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
70
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
71
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectViewer;
72
import org.gvsig.gui.beans.swing.JFileChooser;
73
import org.gvsig.tools.dynobject.DynObject;
74
import org.gvsig.tools.service.ServiceException;
75
import org.gvsig.tools.swing.api.ToolsSwingLocator;
76

    
77
/**
78
 * @author jmvivo
79
 * 
80
 */
81
public abstract class FilesystemExplorerWizardPanel extends WizardPanel
82
    implements ActionListener, ListSelectionListener {
83

    
84
    /**
85
         *
86
         */
87
    private static final long serialVersionUID = -3371957786521876903L;
88

    
89
    private static final Logger LOG = LoggerFactory
90
        .getLogger(FilesystemExplorerWizardPanel.class);
91

    
92
    public static final String OPEN_LAYER_FILE_CHOOSER_ID =
93
        "OPEN_LAYER_FILE_CHOOSER_ID";
94

    
95
    protected static final String ADD_COMMAND = "ADD";
96
    protected static final String EDIT_COMMAND = "PROPERTIES";
97
    protected static final String REMOVE_COMMAND = "REMOVE";
98
    protected static final String UP_COMMAND = "UP";
99
    protected static final String DOWN_COMMAND = "DOWN";
100

    
101
    private static String lastPath = null;
102
    private static MyFileFilter lastFilter = null;
103
    private static final String DEFAULT_FILTER = "All_supported";
104

    
105
    private JList fileList;
106
    private JScrollPane fileListScroll;
107
    private JPanel buttonsPanel;
108
    private JButton addButton;
109
    private JButton propertiesButton;
110
    private JButton removeButton;
111
    private JButton upButton;
112
    private JButton downButton;
113

    
114
    protected FilesystemServerExplorer explorer;
115
    private ArrayList<MyFileFilter> filters;
116

    
117
    public void setTabName(String name) {
118
        super.setTabName(name);
119
    }
120

    
121
    /*
122
     * (non-Javadoc)
123
     * 
124
     * @see com.iver.cit.gvsig.gui.WizardPanel#getParameters()
125
     */
126
    @Override
127
    public DataStoreParameters[] getParameters() {
128
        return ((FilesystemStoreListModel) getFileList().getModel())
129
            .getParameters();
130
    }
131

    
132
    /*
133
     * (non-Javadoc)
134
     * 
135
     * @see com.iver.cit.gvsig.gui.WizardPanel#initWizard()
136
     */
137
    @Override
138
    public void initWizard() {
139
        setTabName(PluginServices.getText(this, "Fichero"));
140
        if (lastPath == null) {
141
            Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
142
            lastPath = prefs.get("DataFolder", null);
143
        }
144

    
145
        DataManager dm = DALLocator.getDataManager();
146
        FilesystemServerExplorerParameters param;
147
        try {
148
            param =
149
                (FilesystemServerExplorerParameters) dm
150
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
151
            param.setInitialpath(lastPath);
152
            explorer =
153
                (FilesystemServerExplorer) dm.openServerExplorer(
154
                    FilesystemServerExplorer.NAME, param);
155
        } catch (Exception e) {
156
            throw new RuntimeException(e);
157
        }
158

    
159
        int mode = FilesystemServerExplorer.MODE_ALL;
160
        this.filters = new ArrayList<MyFileFilter>();
161
        if (this.getMapCtrl() == null) {
162
            mode = FilesystemServerExplorer.MODE_FEATURE;
163
        } else {
164
            mode =
165
                FilesystemServerExplorer.MODE_GEOMETRY
166
                    | FilesystemServerExplorer.MODE_RASTER;
167
        }
168
        @SuppressWarnings("unchecked")
169
        Iterator<FilesystemFileFilter> iter = explorer.getFilters(mode);
170
        while (iter.hasNext()) {
171
            this.filters.add(new MyFileFilter(iter.next()));
172
        }
173
        this.filters.add(new MyFileFilter(explorer.getFilter(mode,
174
            Messages.get(DEFAULT_FILTER))));
175
        initUI();
176
    }
177

    
178
    private void initUI() {
179
        this.setLayout(new GridBagLayout());
180
        GridBagConstraints constr = new GridBagConstraints();
181

    
182
        constr.gridwidth = GridBagConstraints.RELATIVE;
183
        constr.gridheight = GridBagConstraints.RELATIVE;
184
        constr.fill = GridBagConstraints.BOTH;
185
        constr.anchor = GridBagConstraints.FIRST_LINE_START;
186
        constr.weightx = 1;
187
        constr.weighty = 1;
188
        constr.ipadx = 3;
189
        constr.ipady = 3;
190

    
191
        this.add(getFileListScroll(), constr);
192

    
193
        constr.gridwidth = GridBagConstraints.REMAINDER;
194
        constr.gridheight = GridBagConstraints.RELATIVE;
195
        constr.fill = GridBagConstraints.NONE;
196
        constr.anchor = GridBagConstraints.FIRST_LINE_END;
197
        constr.weightx = 0;
198
        constr.weighty = 0;
199
        this.add(getButtonsPanel(), constr);
200

    
201
        this.updateButtons();
202

    
203
    }
204

    
205
    protected class FilesystemStoreListModel extends AbstractListModel {
206

    
207
        private static final long serialVersionUID = -726119349962990665L;
208
        private ArrayList<FilesystemStoreParameters> theList;
209

    
210
        public FilesystemStoreListModel() {
211
            theList = new ArrayList<FilesystemStoreParameters>();
212
        }
213

    
214
        public DataStoreParameters[] getParameters() {
215
            return theList.toArray(new DataStoreParameters[0]);
216
        }
217

    
218
        public Object getElementAt(int index) {
219
            return theList.get(index).getFile().getName();
220
        }
221

    
222
        public FilesystemStoreParameters getStoreParameterAt(int index) {
223
            return theList.get(index);
224
        }
225

    
226
        public int getSize() {
227
            return theList.size();
228
        }
229

    
230
        public DynObject getDynObjectAt(int index) {
231
            return (DynObject) theList.get(index);
232
        }
233

    
234
        public void add(DynObject dynObject) {
235
            this.theList.add((FilesystemStoreParameters) dynObject);
236
            this.fireIntervalAdded(this, this.theList.size() - 1,
237
                this.theList.size() - 1);
238
        }
239

    
240
        public void addAll(List<FilesystemStoreParameters> toAdd) {
241
            int index0 = this.getSize() - 1;
242
            if (index0 < 0) {
243
                index0 = 0;
244
            }
245
            this.theList.addAll(toAdd);
246
            this.fireIntervalAdded(this, index0, this.theList.size() - 1);
247
        }
248

    
249
        public void remove(int i) {
250
            this.theList.remove(i);
251
            this.fireIntervalRemoved(this, i, i);
252

    
253
        }
254

    
255
        public void up(FilesystemStoreParameters item) {
256
            int curIndex = this.theList.indexOf(item);
257
            if (curIndex < 1) {
258
                return;
259
            }
260
            this.theList.remove(item);
261
            this.theList.add(curIndex - 1, item);
262
            this.fireContentsChanged(this, curIndex, curIndex - 1);
263
        }
264

    
265
        public void down(FilesystemStoreParameters item) {
266
            int curIndex = this.theList.indexOf(item);
267
            if (curIndex < 0) {
268
                return;
269
            } else
270
                if (curIndex == this.theList.size() - 1) {
271
                    return;
272
                }
273
            this.theList.remove(item);
274
            this.theList.add(curIndex + 1, item);
275
            this.fireContentsChanged(this, curIndex, curIndex + 1);
276
        }
277

    
278
    }
279

    
280
    protected JList getFileList() {
281
        if (fileList == null) {
282
            fileList = new JList(new FilesystemStoreListModel());
283

    
284
            fileList.addListSelectionListener(this);
285
        }
286
        return fileList;
287
    }
288

    
289
    private JScrollPane getFileListScroll() {
290
        if (fileListScroll == null) {
291
            fileListScroll = new JScrollPane();
292
            fileListScroll.setViewportView(getFileList());
293
            fileListScroll
294
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
295
            fileListScroll
296
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
297

    
298
        }
299
        return fileListScroll;
300
    }
301

    
302
    private Component getButtonsPanel() {
303
        if (buttonsPanel == null) {
304
            buttonsPanel = new JPanel();
305

    
306
            buttonsPanel.setLayout(new GridBagLayout());
307

    
308
            GridBagConstraints constr = new GridBagConstraints();
309

    
310
            constr.anchor = GridBagConstraints.CENTER;
311
            constr.fill = GridBagConstraints.NONE;
312
            constr.ipadx = 3;
313
            constr.ipady = 3;
314
            constr.insets = new Insets(3, 3, 3, 3);
315
            constr.gridwidth = GridBagConstraints.REMAINDER;
316

    
317
            buttonsPanel.add(getAddButton(), constr);
318
            buttonsPanel.add(getPropertiesButton(), constr);
319
            buttonsPanel.add(getRemoveButton(), constr);
320
            buttonsPanel.add(getUpButton(), constr);
321
            buttonsPanel.add(getDownButton(), constr);
322
            // buttonsPanel.add(new JLabel(), constrLbl);
323
        }
324
        return buttonsPanel;
325
    }
326

    
327
    private JButton getAddButton() {
328
        if (addButton == null) {
329
            addButton =
330
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
331
            addButton.setText(getLocalizedText("add"));
332
            addButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
333
            addButton.setActionCommand(ADD_COMMAND);
334
            addButton.addActionListener(this);
335
        }
336
        return addButton;
337
    }
338

    
339
    private JButton getPropertiesButton() {
340
        if (propertiesButton == null) {
341
            propertiesButton =
342
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
343
            propertiesButton.setText(getLocalizedText("properties"));
344
            propertiesButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
345
            propertiesButton.setActionCommand(EDIT_COMMAND);
346
            propertiesButton.addActionListener(this);
347
        }
348
        return propertiesButton;
349
    }
350

    
351
    private JButton getRemoveButton() {
352
        if (removeButton == null) {
353
            removeButton =
354
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
355
            removeButton.setText(getLocalizedText("remove"));
356
            removeButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
357
            removeButton.setActionCommand(REMOVE_COMMAND);
358
            removeButton.addActionListener(this);
359
        }
360
        return removeButton;
361
    }
362

    
363
    private JButton getUpButton() {
364
        if (upButton == null) {
365
            upButton =
366
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
367
            upButton.setText(getLocalizedText("up"));
368
            upButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
369
            upButton.setActionCommand(UP_COMMAND);
370
            upButton.addActionListener(this);
371
        }
372
        return upButton;
373
    }
374

    
375
    private JButton getDownButton() {
376
        if (downButton == null) {
377
            downButton =
378
                ToolsSwingLocator.getUsabilitySwingManager().createJButton();
379
            downButton.setText(getLocalizedText("down"));
380
            downButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
381
            downButton.setActionCommand(DOWN_COMMAND);
382
            downButton.addActionListener(this);
383
        }
384
        return downButton;
385
    }
386

    
387
    private String getLocalizedText(String txt) {
388
        try {
389
            return PluginServices.getText(this, txt);
390
        } catch (Exception e) {
391
            return txt;
392
        }
393
    }
394

    
395
    public void actionPerformed(ActionEvent e) {
396
        String command = e.getActionCommand();
397
        FilesystemStoreListModel model =
398
            (FilesystemStoreListModel) getFileList().getModel();
399

    
400
        if (command == ADD_COMMAND) {
401
            List<FilesystemStoreParameters> toAdd = this.addFiles();
402
            if (toAdd.isEmpty()) {
403
                return;
404
            }
405
            model.addAll(toAdd);
406

    
407
            getFileList().setModel(model);
408

    
409
        } else
410
            if (command == EDIT_COMMAND) {
411
                DynObject dynObject =
412
                    model.getDynObjectAt(getFileList().getSelectedIndex());
413

    
414
                try {
415
                    DynObjectEditor editor = new DynObjectEditor(dynObject);
416
                    editor.editObject(true);
417
                } catch (ServiceException ex) {
418
                    LOG.error(
419
                        "Error creating a Swing component for the DynObject: "
420
                            + dynObject, ex);
421
                }
422

    
423
            } else
424
                if (command == REMOVE_COMMAND) {
425
                    int[] selecteds = getFileList().getSelectedIndices();
426
                    for (int i = selecteds.length - 1; i > -1; i--) {
427
                        model.remove(selecteds[i]);
428
                    }
429
                    getFileList().setSelectedIndex(-1);
430

    
431
                } else
432
                    if (command == UP_COMMAND) {
433
                        List<FilesystemStoreParameters> items =
434
                            new ArrayList<FilesystemStoreParameters>();
435
                        int[] selecteds = getFileList().getSelectedIndices();
436
                        if (selecteds.length == 0 || selecteds[0] == 0) {
437
                            return;
438
                        }
439
                        for (int i = 0; i < selecteds.length; i++) {
440
                            items.add(model.getStoreParameterAt(selecteds[i]));
441
                            selecteds[i]--;
442
                        }
443
                        Iterator<FilesystemStoreParameters> iter =
444
                            items.iterator();
445
                        while (iter.hasNext()) {
446
                            model.up(iter.next());
447
                        }
448
                        getFileList().setSelectedIndices(selecteds);
449

    
450
                    } else
451
                        if (command == DOWN_COMMAND) {
452
                            List<FilesystemStoreParameters> items =
453
                                new ArrayList<FilesystemStoreParameters>();
454
                            int[] selecteds =
455
                                getFileList().getSelectedIndices();
456
                            if (selecteds.length == 0
457
                                || selecteds[selecteds.length - 1] == model
458
                                    .getSize() - 1) {
459
                                return;
460
                            }
461
                            for (int i = selecteds.length - 1; i > -1; i--) {
462
                                items.add(model
463
                                    .getStoreParameterAt(selecteds[i]));
464
                                selecteds[i]++;
465
                            }
466
                            Iterator<FilesystemStoreParameters> iter =
467
                                items.iterator();
468
                            while (iter.hasNext()) {
469
                                model.down(iter.next());
470
                            }
471
                            getFileList().setSelectedIndices(selecteds);
472

    
473
                        } else {
474
                            throw new IllegalArgumentException(command);
475
                        }
476

    
477
    }
478

    
479
    public List<DynObject> getSelecteds() {
480
        ArrayList<DynObject> list = new ArrayList<DynObject>();
481
        JList fList = this.getFileList();
482
        int[] selecteds = fList.getSelectedIndices();
483
        FilesystemStoreListModel model =
484
            (FilesystemStoreListModel) fList.getModel();
485

    
486
        for (int index : selecteds) {
487
            list.add((DynObject) model.getStoreParameterAt(index));
488
        }
489
        return list;
490
    }
491

    
492
    public void valueChanged(ListSelectionEvent e) {
493
        this.updateButtons();
494
    }
495

    
496
    public class MyFileFilter extends FileFilter {
497

    
498
        public FilesystemFileFilter filter = null;
499

    
500
        public MyFileFilter(FilesystemFileFilter params) {
501
            this.filter = params;
502
        }
503

    
504
        /**
505
         * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
506
         */
507
        public boolean accept(File f) {
508
            if (f.isDirectory()) {
509
                return true;
510
            }
511
            return filter.accept(f);
512

    
513
        }
514

    
515
        /**
516
         * @see javax.swing.filechooser.FileFilter#getDescription()
517
         */
518
        public String getDescription() {
519
            return filter.getDescription();
520
        }
521

    
522
        public String getName() {
523
            return filter.getDataStoreProviderName();
524
        }
525
    }
526

    
527
    private List<FilesystemStoreParameters> addFiles() {
528
        this.callStateChanged(true);
529
        JFileChooser fileChooser =
530
            new JFileChooser(OPEN_LAYER_FILE_CHOOSER_ID,
531
                explorer.getCurrentPath());
532
        fileChooser.setMultiSelectionEnabled(true);
533
        fileChooser.setAcceptAllFileFilterUsed(false);
534

    
535
        Iterator<MyFileFilter> iter = this.filters.iterator();
536
        while (iter.hasNext()) {
537
            fileChooser.addChoosableFileFilter(iter.next());
538
        }
539

    
540
        if (lastFilter != null && filters.contains(lastFilter)) {
541
            fileChooser.setFileFilter(lastFilter);
542
        }
543

    
544
        int result = fileChooser.showOpenDialog(this);
545

    
546
        List<FilesystemStoreParameters> toAdd =
547
            new ArrayList<FilesystemStoreParameters>();
548

    
549
        if (result == JFileChooser.APPROVE_OPTION) {
550
            lastPath = fileChooser.getCurrentDirectory().getAbsolutePath();
551
            try {
552
                explorer.setCurrentPath(fileChooser.getCurrentDirectory());
553
            } catch (FileNotFoundException e) {
554
                NotificationManager.addError(e);
555
            }
556
            lastFilter = (MyFileFilter) fileChooser.getFileFilter();
557
            ApplicationManager appGvSIGMan = ApplicationLocator.getManager();
558
            PrepareContext context = this.getPrepareDataStoreContext();
559
            List<DataStoreParameters> paramList = new ArrayList<DataStoreParameters>();
560
            List<DataStoreParameters> toAddParamList = null;
561
            for (File aFile : fileChooser.getSelectedFiles()) {
562
                try {
563
                        DataStoreParameters param =
564
                        explorer.createStoreParameters(aFile, lastFilter
565
                            .getDescription().compareTo(DEFAULT_FILTER) == 0
566
                            ? null : lastFilter.getName());
567
                    if (param == null) {
568
                        // TODO show warning
569
                        continue;
570
                    }
571

    
572
                    paramList.add(param);
573
                   
574
                } catch (DataException e) {
575
                    NotificationManager.addError(e);
576
                    return null;
577
                }
578
            }
579
            
580
            try {
581
                toAddParamList =
582
                    appGvSIGMan.prepareOpenDataStoreParameters(paramList, context);
583
            } catch (Exception e) {
584
                NotificationManager.addError(e);
585
            }
586
            
587
            for (int i = 0; i < toAddParamList.size(); i++) {
588
                                toAdd.add((FilesystemStoreParameters)toAddParamList.get(i));
589
                        }
590
        }
591
        return toAdd;
592
    }
593

    
594
    protected abstract PrepareContext getPrepareDataStoreContext();
595

    
596
    private void updateButtons() {
597
        int selectedIndex = this.getFileList().getSelectedIndex();
598
        int size = this.getFileList().getModel().getSize();
599
        int[] selecteds = this.getFileList().getSelectedIndices();
600
        if (size < 1) {
601
            this.getPropertiesButton().setEnabled(false);
602
            this.getRemoveButton().setEnabled(false);
603
            this.getUpButton().setEnabled(false);
604
            this.getDownButton().setEnabled(false);
605
            return;
606
        } else
607
            if (size == 1) {
608
                this.getUpButton().setEnabled(false);
609
                this.getDownButton().setEnabled(false);
610
            } else {
611
                this.getUpButton().setEnabled(selectedIndex > 0);
612

    
613
                this.getDownButton().setEnabled(
614
                    selectedIndex > -1
615
                        && selecteds[selecteds.length - 1] < size - 1);
616
            }
617
        this.getPropertiesButton().setEnabled(
618
            selectedIndex > -1
619
                && this.getFileList().getSelectedIndices().length == 1);
620
        this.getRemoveButton().setEnabled(selectedIndex > -1);
621
    }
622

    
623
    public void close() {
624
        if (explorer != null) {
625
            explorer.dispose();
626
            explorer = null;
627
        }
628
        if (filters != null) {
629
            filters.clear();
630
            filters = null;
631
        }
632
    }
633
}