Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerWizardPanel.java @ 38262

History | View | Annotate | Download (22 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.NotificationManager;
56
import org.gvsig.app.ApplicationLocator;
57
import org.gvsig.app.ApplicationManager;
58
import org.gvsig.app.gui.WizardPanel;
59
import org.gvsig.app.prepareAction.PrepareContext;
60
import org.gvsig.fmap.dal.DALLocator;
61
import org.gvsig.fmap.dal.DataManager;
62
import org.gvsig.fmap.dal.DataStoreParameters;
63
import org.gvsig.fmap.dal.exception.DataException;
64
import org.gvsig.fmap.dal.exception.FileNotFoundException;
65
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter;
66
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
67
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
68
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
69
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
70
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectViewer;
71
import org.gvsig.gui.beans.swing.JFileChooser;
72
import org.gvsig.tools.dynobject.DynObject;
73
import org.gvsig.tools.service.ServiceException;
74
import org.gvsig.tools.swing.api.ToolsSwingLocator;
75

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

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

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

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

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

    
100
    private static String lastPath = null;
101
    private static MyFileFilter lastFilter = null;
102
    private static final String DEFAULT_FILTER = "All supporteds";
103

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

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

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

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

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

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

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

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

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

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

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

    
200
        this.updateButtons();
201

    
202
    }
203

    
204
    protected class FilesystemStoreListModel extends AbstractListModel {
205

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

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

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

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

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

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

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

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

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

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

    
252
        }
253

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

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

    
277
    }
278

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

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

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

    
297
        }
298
        return fileListScroll;
299
    }
300

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

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

    
307
            GridBagConstraints constr = new GridBagConstraints();
308

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

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

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

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

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

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

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

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

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

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

    
406
            getFileList().setModel(model);
407

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

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

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

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

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

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

    
476
    }
477

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

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

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

    
495
    public class MyFileFilter extends FileFilter {
496

    
497
        public FilesystemFileFilter filter = null;
498

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

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

    
512
        }
513

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

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

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

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

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

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

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

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

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

    
593
    protected abstract PrepareContext getPrepareDataStoreContext();
594

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

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

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