Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.swing / org.gvsig.raster.wmts.swing.impl / src / main / java / org / gvsig / raster / wmts / swing / impl / wizard / WMTSParamsPanel.java @ 4272

History | View | Annotate | Download (32.5 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
package org.gvsig.raster.wmts.swing.impl.wizard;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.awt.event.FocusAdapter;
29
import java.awt.event.FocusEvent;
30
import java.awt.event.FocusListener;
31
import java.awt.event.ItemEvent;
32
import java.awt.event.ItemListener;
33
import java.awt.event.KeyEvent;
34
import java.awt.event.KeyListener;
35
import java.awt.event.MouseAdapter;
36
import java.awt.event.MouseEvent;
37
import java.awt.event.MouseListener;
38
import java.net.URI;
39
import java.net.URISyntaxException;
40
import java.util.ArrayList;
41
import java.util.HashMap;
42
import java.util.List;
43
import java.util.Vector;
44
import java.util.prefs.Preferences;
45

    
46
import javax.swing.JComponent;
47
import javax.swing.JList;
48
import javax.swing.JOptionPane;
49
import javax.swing.JTabbedPane;
50
import javax.swing.ListModel;
51
import javax.swing.event.ListSelectionEvent;
52
import javax.swing.event.ListSelectionListener;
53
import javax.swing.event.TreeSelectionEvent;
54
import javax.swing.event.TreeSelectionListener;
55
import javax.swing.tree.TreePath;
56

    
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59

    
60
import org.gvsig.fmap.dal.DataParameters;
61
import org.gvsig.fmap.dal.DataStoreParameters;
62
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
63
import org.gvsig.fmap.dal.coverage.store.parameter.TileDataParameters;
64
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
65
import org.gvsig.gui.beans.Messages;
66
import org.gvsig.gui.beans.controls.dnd.JDnDListModel;
67
import org.gvsig.gui.beans.listeners.BeanListener;
68
import org.gvsig.raster.wmts.io.WMTSDataParameters;
69
import org.gvsig.raster.wmts.io.WMTSServerExplorer;
70
import org.gvsig.raster.wmts.ogc.struct.WMTSDimension;
71
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle;
72
import org.gvsig.raster.wmts.ogc.struct.WMTSTheme;
73
import org.gvsig.raster.wmts.swing.WMTSWizardListener;
74
import org.gvsig.raster.wmts.swing.WmtsGUI;
75
import org.gvsig.raster.wmts.swing.impl.panel.dimension.DimensionPanel;
76
import org.gvsig.raster.wmts.swing.impl.panel.format.IFormatsPanel;
77
import org.gvsig.raster.wmts.swing.impl.panel.layer.ILayerPanel;
78
import org.gvsig.raster.wmts.swing.impl.panel.layer.LayerList;
79
import org.gvsig.raster.wmts.swing.impl.panel.layer.LayerTree;
80

    
81

    
82
/**
83
 * This class implements the map option panel.
84
 *
85
 * It includes a set of Listeners that implement some control rules which
86
 * refresh the component values that depends on those selected in the other
87
 * components to avoid to choose an invalid set of options. It also includes a
88
 * method (isCorrectlyConfigured()) that checks if the current set of values is
89
 * enough to correctly launch a GetMap request.
90
 *
91
 * The information is obtained from a WMTSWizardDataSource object.
92
 *
93
 * @author Nacho Brodin (nachobrodin@gmail.com)
94
 */
95
public class WMTSParamsPanel extends WmtsGUI implements ActionListener, ItemListener, KeyListener, ListSelectionListener, MouseListener, TreeSelectionListener {
96
        private static final long        serialVersionUID           = 1L;
97

    
98
        private InfoPanel                tabInfo                    = null;
99
        private ILayerPanel              layerPanel                 = null;
100
        private StylesPanel              tabStyle                   = null;
101
        private IFormatsPanel            formatsPanel               = null;
102
        private DimensionPanel           dimensionPanel             = null;
103

    
104
        private ArrayList<TreePath>      selectedPaths              = new ArrayList<TreePath>();
105
        private WMTSWizardListener       listenerSupport            = null;
106
        private JTabbedPane              jTabbedPane                = null;
107
        protected WMTSServerExplorer     explorer                   = null;
108
        public static Preferences        fPrefs                     = Preferences.userRoot().node("gvsig.wmts-wizard");
109
        private boolean                  deleteCache                = false;
110
        private HashMap<Object, Integer> tabsIndexes                = new HashMap<Object, Integer>();
111
        private static final Logger         logger                    = LoggerFactory.getLogger(WMTSParamsPanel.class);
112

    
113

    
114
        /**
115
         * Layer to add to the selected layers box
116
         * @author Nacho Brodin (nachobrodin@gmail.com)
117
         */
118
        class LayerUI {
119
                public WMTSTheme       theme                = null;
120
                public int             imageFormatSelected  = 0;
121
                public int             infoFormatSelected   = 0;
122
                public int             srsFormatSelected    = 0;
123
                public WMTSStyle       styleSelected        = null;
124

    
125
                public LayerUI(WMTSTheme theme) {
126
                        this.theme = theme;
127
                }
128

    
129
                public String toString() {
130
                        return theme.toString();
131
                }
132
        }
133

    
134
        public JComponent getComponent() {
135
                return this;
136
        }
137

    
138
        public WMTSParamsPanel() {
139
                super();
140
                initialize();
141
        }
142

    
143
        public WMTSParamsPanel(WMTSServerExplorer data) {
144
                super();
145
                setWizardData(data);
146
                this.setLayout(null);
147
                this.setVisible(false);
148
                if (jTabbedPane == null) {
149
                        jTabbedPane = new JTabbedPane();
150
                        jTabbedPane.setBounds(4, 4, 502, 415);
151

    
152
                        jTabbedPane.addTab(Messages.getText("capas"), null, getLayerPanel(), null);
153
                        jTabbedPane.addTab(Messages.getText("estilos"), null, getTabStyle(), null);
154
                        jTabbedPane.addTab(Messages.getText("dimensions"), null, getJPanelDimension(), null);
155
                        jTabbedPane.addTab(Messages.getText("formatos"), null, getJPanelFormatsAndSrs(), null);
156

    
157
                        tabsIndexes.put(getLayerPanel(), 0);
158
                        tabsIndexes.put(getTabStyle(), 1);
159
                        tabsIndexes.put(getJPanelDimension(), 2);
160
                        tabsIndexes.put(getJPanelFormatsAndSrs(), 3);
161
                }
162
                this.add(jTabbedPane, null);
163

    
164
                jTabbedPane.setEnabledAt(1, false);
165
                jTabbedPane.setEnabledAt(2, false);
166
                jTabbedPane.setEnabledAt(3, false);
167
        }
168

    
169
        /**
170
         * This method initializes panelPage2
171
         *
172
         * @return Panel
173
         */
174
        private void initialize() {
175
                this.setLayout(new BorderLayout());
176
                this.setVisible(false);
177
                this.add(getJTabbedPane(), BorderLayout.CENTER);
178
        }
179

    
180
        /**
181
         * This method initializes the main panel with tabs
182
         *
183
         * @return JTabbedPane
184
         */
185
        public JTabbedPane getJTabbedPane() {
186
                if (jTabbedPane == null) {
187
                        jTabbedPane = new JTabbedPane();
188
                        jTabbedPane.setBounds(4, 4, 502, 415);
189
                        InfoPanel ip = getTabInfo();
190
                        ip.addFocusListener(new FocusListener() {
191
                                public void focusGained(FocusEvent e) {
192
                                        refreshInfoPanel();
193
                                }
194

    
195
                                public void focusLost(FocusEvent e) {
196
                                }
197
                        });
198

    
199
                        jTabbedPane.addTab(Messages.getText("info"), null, ip, null);
200
                        jTabbedPane.addTab(Messages.getText("capas"), null, getLayerPanel(), null);
201
                        jTabbedPane.addTab(Messages.getText("estilos"), null, getTabStyle(), null);
202
                        jTabbedPane.addTab(Messages.getText("dimensions"), null, getJPanelDimension(), null);
203
                        jTabbedPane.addTab(Messages.getText("formatos"), null, getJPanelFormatsAndSrs(), null);
204

    
205
                        tabsIndexes.clear();
206
                        tabsIndexes.put(ip, 0);
207
                        tabsIndexes.put(getLayerPanel(), 1);
208
                        tabsIndexes.put(getTabStyle(), 2);
209
                        tabsIndexes.put(getJPanelDimension(), 3);
210
                        tabsIndexes.put(getJPanelFormatsAndSrs(), 4);
211

    
212
                        jTabbedPane.setEnabledAt(2, false);
213
                        jTabbedPane.setEnabledAt(3, false);
214
                        jTabbedPane.setEnabledAt(4, false);
215
                }
216
                return jTabbedPane;
217
        }
218

    
219
        /**
220
         * This method initializes the information panel
221
         * @return Panel
222
         */
223
        private InfoPanel getTabInfo() {
224
                if (tabInfo == null) {
225
                        tabInfo = new InfoPanel();
226
                }
227
                return tabInfo;
228
        }
229

    
230
        /**
231
         * This method initializes the panel with the layer list
232
         * @return
233
         */
234
        private ILayerPanel getLayerPanel() {
235
                if (layerPanel == null) {
236
                        layerPanel = new ILayerPanel();
237
                        layerPanel.getLstSelectedLayers().addMouseListener(this);
238
                        layerPanel.getLstSelectedLayers().addListSelectionListener(this);
239
                        layerPanel.getChkExtendedNames().addItemListener(this);
240
                        layerPanel.getBtnAdd().addActionListener(this);
241
                        layerPanel.getBtnDel().addActionListener(this);
242
                        layerPanel.getBtnDown().addActionListener(this);
243
                        layerPanel.getBtnUp().addActionListener(this);
244
                        layerPanel.getChkExtendedNames().addActionListener(this);
245
                        layerPanel.getTreeLayers().addMouseListener(this);
246
                        layerPanel.getTreeLayers().addTreeSelectionListener(this);
247
                        layerPanel.getLayerListViewSelection().addActionListener(this);
248
                        layerPanel.getThemesViewSelection().addActionListener(this);
249
                }
250
                return layerPanel;
251
        }
252

    
253
        /**
254
         * This method initializes the panel with the list of styles
255
         * @return Panel
256
         */
257
        private StylesPanel getTabStyle() {
258
                if (tabStyle == null) {
259
                        tabStyle = new StylesPanel();
260
                        tabStyle.setEnabled(false);
261
                        tabStyle.addListener(new BeanListener() {
262
                                public void beanValueChanged(Object value) {
263
                                        fireWizardComplete();
264
                                };
265
                        });
266
                        tabStyle.addFocusListener(new FocusAdapter() {
267
                                public void focusGained(FocusEvent e) {
268
                                        refreshStyleTree();
269
                                }
270
                        });
271
                }
272
                return tabStyle;
273
        }
274

    
275
        /**
276
         * This method initializes the panel with the formats and SRSs
277
         * @return Panel
278
         */
279
        private IFormatsPanel getJPanelFormatsAndSrs() {
280
                if (formatsPanel == null) {
281
                        formatsPanel = new IFormatsPanel();
282
                        formatsPanel.getLstImageFormats().addListSelectionListener(this);
283
                        formatsPanel.getLstInfoFormats().addListSelectionListener(this);
284
                        formatsPanel.getLstSRSs().addListSelectionListener(this);
285
                        formatsPanel.getChkTransparency().addItemListener(this);
286
                }
287
                return formatsPanel;
288
        }
289

    
290
        /**
291
         * This method initializes the panel with the formats and SRSs
292
         * @return Panel
293
         */
294
        private DimensionPanel getJPanelDimension() {
295
                if (dimensionPanel == null) {
296
                        dimensionPanel = new DimensionPanel();
297
                }
298
                return dimensionPanel;
299
        }
300

    
301
        /**
302
         * Fires a notification to this wizard listeners telling them if the
303
         * configuration is fair enough to send a GetMap request.
304
         *
305
         * @param b
306
         */
307
        private void fireWizardComplete() {
308
                listenerSupport.correctlyConfiguredEvent(isCorrectlyConfigured());
309
        }
310

    
311
        public void setListenerSupport(WMTSWizardListener support) {
312
                listenerSupport = support;
313
        }
314

    
315
        /**
316
         * Returns the index of the CRS within the CRS list.
317
         *
318
         * @param crs
319
         * @return The CRS's index if it exists, -1 if it not exists.
320
         */
321
        public int getSRSIndex(String crs) {
322
                for (int i = 0; i < getLstSRSs().getModel().getSize(); i++) {
323
                        if (crs != null
324
                                        && crs.compareTo(getLstSRSs().getModel().getElementAt(i).toString()) == 0) {
325
                                return i;
326
                        }
327
                }
328
                return -1;
329
        }
330

    
331
        /**
332
         * Returns the index of the format within the formats list.
333
         * @param format
334
         * @return The format's index if it exists, -1 if it not exists.
335
         */
336
        public int getImageFormatIndex(String format) {
337
                for (int i = 0; i < getImageFormats().getModel().getSize(); i++) {
338
                        if (format != null &&
339
                                        format.compareTo(getImageFormats().getModel().getElementAt(i).toString()) == 0) {
340
                                return i;
341
                        }
342
                }
343
                return -1;
344
        }
345

    
346
        /**
347
         * Returns the index of the format within the formats list.
348
         * @param format
349
         * @return The format's index if it exists, -1 if it not exists.
350
         */
351
        public int getInfoFormatIndex(String format) {
352
                for (int i = 0; i < getInfoFormats().getModel().getSize(); i++) {
353
                        if (format != null &&
354
                                        format.compareTo(getInfoFormats().getModel().getElementAt(i).toString()) == 0) {
355
                                return i;
356
                        }
357
                }
358
                return -1;
359
        }
360

    
361
        public void updateSelectedInfo(HashMap<String, Object> info) {
362
                setVisible(true);
363

    
364
                String selectedLayer = (String) info.get("selectedLayer");
365
                setSelectedLayer(selectedLayer);
366

    
367
                // srs
368
                String srs = (String) info.get("srs");
369
                int index = getSRSIndex( srs );
370
                if (index != -1) {
371
                        getLstSRSs().setSelectedIndex(index);
372
                }
373

    
374
                // image format
375
                String imageFormat = (String) info.get("imageformat");
376
                index = getImageFormatIndex(imageFormat);
377
                if (index != -1) {
378
                        getImageFormats().setSelectedIndex(index);
379
                }
380

    
381
                // info format
382
                String infoFormat = (String) info.get("infoformat");
383
                index = getInfoFormatIndex(infoFormat);
384
                if (index != -1) {
385
                        getInfoFormats().setSelectedIndex(index);
386
                }
387

    
388
                refreshInfoPanel();
389
        }
390

    
391
        private void setSelectedLayer(String name) {
392
                LayerTreeModel treeModel = (LayerTreeModel)getLayerPanel().getTreeLayers().getModel();
393
                WMTSTheme theme = (WMTSTheme)treeModel.getNodeByName(name);
394

    
395
                JDnDListModel modelo = (JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel();
396
                modelo.addElement(0, new LayerUI(theme));
397
                getLayerPanel().getLstSelectedLayers().setSelectedIndex(0);
398

    
399
                refreshSelectedLayerData();
400
        }
401

    
402
        /**
403
         * Gets the available layers in WMTS maintaining the tree structure
404
         * @return
405
         */
406
        public LayerTree getLayerStructure() {
407
                return layerPanel.getTreeLayers();
408
        }
409

    
410
        /**
411
         * Gets the list of the styles
412
         * @return
413
         */
414
        @SuppressWarnings("rawtypes")
415
        public Vector getStyleTitles() {
416
                return getTabStyle().getStyleTree().getStyleSelectionTitles();
417
        }
418

    
419
        /**
420
         * Gets the list of the selected layers
421
         * @return
422
         */
423
        @SuppressWarnings({ "rawtypes", "unchecked" })
424
        public Vector getSelectedLayersVector() {
425
                JDnDListModel mod = (JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel();
426
                Vector lyrs = new Vector();
427
                for (int i = 0; i < mod.getSize(); i++) {
428
                        lyrs.add(mod.getElementAt(i));
429
                }
430
                return lyrs;
431
        }
432

    
433
        /**
434
         * Gets the currently shown style tree.
435
         * @return
436
         */
437
        public StyleTree getStyleTree() {
438
                return getTabStyle().getStyleTree();
439
        }
440

    
441
        /**
442
         * Gets an array with the list of layers
443
         * @return
444
         */
445
        @SuppressWarnings({ "unchecked", "rawtypes" })
446
        public LayerUI[] selectedLayersToArray() {
447
                JDnDListModel modelo = ((JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel());
448
                ArrayList elements = modelo.getElements();
449
                LayerUI[] layers = (LayerUI[]) elements.toArray(new LayerUI[0]);
450
                return layers;
451
        }
452

    
453
        /**
454
         * Gets the SRS list
455
         * @return
456
         */
457
        public JList getLstSRSs() {
458
                return getJPanelFormatsAndSrs().getLstSRSs();
459
        }
460

    
461
        /**
462
         * Gets the image format list
463
         * @return
464
         */
465
        public JList getImageFormats() {
466
                return getJPanelFormatsAndSrs().getLstImageFormats();
467
        }
468

    
469
        /**
470
         * Gets the info format list
471
         * @return
472
         */
473
        public JList getInfoFormats() {
474
                return getJPanelFormatsAndSrs().getLstInfoFormats();
475
        }
476

    
477
        //*****************************************************************
478

    
479
        /**
480
         * This method checks for the options selected within the configuration
481
         * dialog are correct and enough to perform a GetMap request.
482
         *
483
         * @return true if you're done, false if not.
484
         */
485
        public boolean isCorrectlyConfigured() {
486
                this.repaint();
487
                Object[] values = getLayerPanel().getLstSelectedLayers().getSelectedValues();
488
                getLayerPanel().getBtnDel().setEnabled(values != null && values.length > 0);
489
                return (values == null || values.length == 0) ? false : true;
490
        }
491

    
492
        /**
493
         * Method called when pressing the Add layer button or when double-clicking
494
         * on a layer from the server's layer tree.
495
         */
496
        public void addLayer() {
497
                boolean alguno = false;
498

    
499
                TreePath[] selecciones = getLayerPanel().getTreeLayers().getSelectionPaths();
500
                if (selecciones == null) {
501
                        return;
502
                }
503

    
504
                //**************
505
                //Limpiar la lista.
506
                //Quitar este bloque si se quiere a?adir una lista de elementos y no solo uno
507
                try {
508
                        LayerUI[] infos = selectedLayersToArray();
509
                        JDnDListModel modelo = ((JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel());
510
                        modelo.delIndices(new int[infos.length]);
511
                        layerPanel.getLstSelectedLayers().clearSelection();
512
                        selectedPaths.clear();
513
                } catch (ArrayIndexOutOfBoundsException ex) {
514
                        ex.printStackTrace();
515
                }
516
                //**************
517

    
518
                for (int i = 0; i < selecciones.length && i < 1 /*Se a?ade solo uno: quitar para lista*/; i++) {
519
                        if(!(selecciones[i].getLastPathComponent() instanceof WMTSTheme))
520
                                continue;
521
                        selectedPaths.add(selecciones[i]);
522
                        WMTSTheme nodo = (WMTSTheme) selecciones[i].getLastPathComponent();
523

    
524
                        if (nodo.getTitle() == null || nodo.getTitle().equals("")) {
525
                                continue;
526
                        }
527

    
528
                        JDnDListModel modelo = (JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel();
529
                        if(getLayerPanel().getThemesViewSelection().isSelected() && nodo.getLayerRef().size() == 0) {
530
                                JOptionPane.showOptionDialog(this,
531
                                                Messages.getText("theme_without_reference"),
532
                                                Messages.getText("confirmacion"),
533
                                                JOptionPane.OK_OPTION,
534
                                                JOptionPane.INFORMATION_MESSAGE,
535
                                                null,
536
                                                new Object[]{Messages.getText("accept")},
537
                                                Messages.getText("accept"));
538
                                return;
539
                        }
540

    
541
                        if (modelo.addElement(0, new LayerUI(nodo))) {
542
                                alguno = true;
543
                        }
544
                        getLayerPanel().getLstSelectedLayers().setSelectedIndex(0);
545
                }
546

    
547
                if (alguno) {
548
                        refreshSelectedLayerData();
549
                }
550
                refreshInfoPanel();
551
                fireWizardComplete();
552
                listenerSupport.addLayerEvent();
553
        }
554

    
555

    
556
        @SuppressWarnings({ "rawtypes", "unchecked" })
557
        private void delLayer() {
558
                int[] indices = getLayerPanel().getLstSelectedLayers().getSelectedIndices();
559

    
560
                ArrayList removeList = new ArrayList();
561
                LayerUI[] infos = selectedLayersToArray();
562
                JDnDListModel modelo = ((JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel());
563
                for (int i = 0; i < indices.length; i++) {
564
                        removeList.add(infos[indices[i]]);
565
                }
566
                modelo.delIndices(indices);
567
                layerPanel.getLstSelectedLayers().clearSelection();
568

    
569
                // delete from selectedPaths as well
570
                WMTSTheme elemento = null;
571
                WMTSTheme last = null;
572
                TreePath path = null;
573
                for (int i = removeList.size() - 1; i >= 0; i--) {
574
                        elemento = ((LayerUI)removeList.get(i)).theme;
575
                        for (int j = 0; j < selectedPaths.size(); j++) {
576
                                path = (TreePath) selectedPaths.get(j);
577
                                last = (WMTSTheme) path.getLastPathComponent();
578
                                if (last.getTitle().compareTo(elemento.getTitle()) == 0) {
579
                                        selectedPaths.remove(path);
580
                                }
581
                        }
582
                }
583

    
584
                refreshSelectedLayerData();
585
                refreshInfoPanel();
586
                fireWizardComplete();
587
                listenerSupport.removeLayerEvent();
588
        }
589

    
590
        /**
591
         * Refreshes the content shown by this component and dependent of the selected layer.
592
         * If the layer selected is changed then the list of srs, image formats, information formats and so on,
593
         * will change.
594
         */
595
        public void refreshSelectedLayerData() {
596
                refreshSRS();
597
                refreshImageFormats();
598
                refreshInfoFormats();
599
                getLayerPanel().getLstSelectedLayers().repaint();
600
                refreshStyleTree();
601
                refreshDimension();
602
        }
603

    
604
        /**
605
         * Refreshes the info tab
606
         */
607
        public void refreshInfoPanel() {
608
                getTabInfo().refresh(explorer, getSelectedLayersVector(),
609
                                getStyleTitles(), getSelectedImageFormat(), getSelectedSRS());
610
        }
611

    
612
        /**
613
         * Refreshes the style tab
614
         */
615
        private void refreshStyleTree() {
616
                StyleTreeModel model = new StyleTreeModel(Messages.getText("selected_layers"));
617
                int tabIndex = tabsIndexes.get(getTabStyle());
618
                jTabbedPane.setEnabledAt(tabIndex, false);
619

    
620
                Object[] selectedValues = getLayerPanel().getLstSelectedLayers().getSelectedValues();
621
                if(selectedValues != null && selectedValues.length == 1) {
622
                        int s = getJPanelFormatsAndSrs().getLstSRSs().getSelectedIndex();
623
                        if(s != -1)
624
                                ((LayerUI)selectedValues[0]).srsFormatSelected = s;
625
                }
626

    
627
                ListModel listModel = getLayerPanel().getLstSelectedLayers().getModel();
628
                for (int i = 0; i < listModel.getSize(); i++) {
629
                        LayerUI layerUI = ((LayerUI)listModel.getElementAt(i));
630
                        // If any of the layers defines styles, the tab will be enabled.
631
                        if (model.addLayerBranch(layerUI)) {
632
                                jTabbedPane.setEnabledAt(tabIndex, true);
633
                        }
634
                }
635

    
636
                getStyleTree().setModel(model);
637
                getStyleTree().expandAll();
638
                getStyleTree().repaint();
639
        }
640

    
641
        /**
642
         * Refreshes the Image formats list
643
         */
644
        private void refreshImageFormats() {
645
                Object[] selectedValues = getLayerPanel().getLstSelectedLayers().getSelectedValues();
646

    
647
                int tabIndex = tabsIndexes.get(getJPanelFormatsAndSrs());
648
                jTabbedPane.setEnabledAt(tabIndex, false);
649

    
650
                if( (selectedValues == null || selectedValues.length == 0) &&
651
                                getImageFormats().getModel() != null &&
652
                                getImageFormats().getModel() instanceof WMTSParametersListModel) {
653
                        ((WMTSParametersListModel)getImageFormats().getModel()).removeAllItems();
654
                }
655
                if(selectedValues != null && selectedValues.length >= 1) {
656
                        List<String> formats = ((LayerUI)selectedValues[0]).theme.getLayer().getFormat();
657
                        WMTSParametersListModel model = new WMTSParametersListModel(formats);
658
                        getImageFormats().setModel(model);
659
                        getImageFormats().setSelectedIndex(((LayerUI)selectedValues[0]).imageFormatSelected);
660
                        jTabbedPane.setEnabledAt(tabIndex, true);
661
                }
662
        }
663

    
664
        /**
665
         * Refreshes the Information formats list
666
         */
667
        private void refreshInfoFormats() {
668
                Object[] selectedValues = getLayerPanel().getLstSelectedLayers().getSelectedValues();
669
                if( (selectedValues == null || selectedValues.length == 0) &&
670
                                getInfoFormats().getModel() != null &&
671
                                getInfoFormats().getModel() instanceof WMTSParametersListModel) {
672
                        ((WMTSParametersListModel)getInfoFormats().getModel()).removeAllItems();
673
                }
674
                if(selectedValues != null && selectedValues.length == 1) {
675
                        List<String> formats = ((LayerUI)selectedValues[0]).theme.getLayer().getInfoFormat();
676
                        WMTSParametersListModel model = new WMTSParametersListModel(formats);
677
                        getInfoFormats().setModel(model);
678
                        getInfoFormats().setSelectedIndex(((LayerUI)selectedValues[0]).infoFormatSelected);
679
                }
680
        }
681

    
682
        /**
683
         * Refreshes the SRS list
684
         */
685
        private void refreshSRS() {
686
                Object[] selectedValues = getLayerPanel().getLstSelectedLayers().getSelectedValues();
687
                if( (selectedValues == null || selectedValues.length == 0) &&
688
                                getLstSRSs().getModel() != null &&
689
                                getLstSRSs().getModel() instanceof WMTSParametersListModel) {
690
                        ((WMTSParametersListModel)getLstSRSs().getModel()).removeAllItems();
691
                }
692
                if(selectedValues != null && selectedValues.length == 1) {
693
                        List<String> formats = ((LayerUI)selectedValues[0]).theme.getLayer().getSrsList();
694
                        WMTSParametersListModel model = new WMTSParametersListModel(formats);
695
                        getLstSRSs().setModel(model);
696
                        getLstSRSs().setSelectedIndex(((LayerUI)selectedValues[0]).srsFormatSelected);
697
                }
698
        }
699

    
700
        private void refreshDimension() {
701
                Object[] selectedValues = getLayerPanel().getLstSelectedLayers().getSelectedValues();
702
                int tabIndex = tabsIndexes.get(getJPanelDimension());
703
                jTabbedPane.setEnabledAt(tabIndex, false);
704

    
705
                if(selectedValues != null && selectedValues.length >= 1) {
706
                        List<WMTSDimension> dimensions = ((LayerUI)selectedValues[0]).theme.getLayer().getDimensions();
707
                        if(dimensions == null || dimensions.size() == 0) {
708
                                return;
709
                        }
710
                        jTabbedPane.setEnabledAt(tabIndex, true);
711
                        getJPanelDimension().setDimensions(dimensions);
712
                }
713
        }
714

    
715
        /**
716
         * Sets the explorer
717
         * @param data
718
         */
719
        public void setWizardData(WMTSServerExplorer data) {
720
                explorer = data;
721
                ((JDnDListModel)getLayerPanel().getLstSelectedLayers().getModel()).clear();
722
                layerPanel.getLstSelectedLayers().repaint();
723

    
724
                getLayerPanel().getTreeLayers().setModel(new LayerTreeModel(explorer.getOGCClient().getLayerListAsThemes()));
725
                refreshInfoPanel();
726
        }
727

    
728

    
729
        @SuppressWarnings({ "rawtypes", "unchecked" })
730
        public String getLayersQuery() {
731
                ArrayList elements = ((JDnDListModel) getLayerPanel().getLstSelectedLayers().getModel()).getElements();
732
                LayerUI[] layers = (LayerUI[]) elements.toArray(new LayerUI[0]);
733

    
734
                // Nombre separado por comas de las capas
735
                String layersQuery = layers[layers.length - 1].theme.getTitle();
736

    
737
                for (int i = layers.length - 2; i >= 0; i--) {
738
                        layersQuery += ("," + layers[i].theme.getTitle());
739
                }
740

    
741
                return layersQuery;
742
        }
743

    
744
        /**
745
         * Sets the focus to the tab next to the current one.
746
         */
747
        public void setNextTab() {
748
                int currentPage = currentPage();
749
                int nPages = getNumTabs();
750
                if (nPages - 1 > currentPage) {
751
                        getJTabbedPane().setSelectedIndex(nextEnabledPage());
752
                }
753
        }
754

    
755
        /**
756
         * Sets the focus to the tab previous to the current one.
757
         */
758
        public void setPreviousTab() {
759
                this.getJTabbedPane().setSelectedIndex(previousEnabledPage());
760

    
761
        }
762

    
763
        /**
764
         * Returns the index of the current tab.
765
         * @return
766
         */
767
        public int currentPage() {
768
                return getJTabbedPane().getSelectedIndex();
769
        }
770

    
771
        /**
772
         * Returns the tab amount that the WMTSParamsPanel currently have
773
         * @return int
774
         */
775
        public int getNumTabs() {
776
                return getJTabbedPane().getTabCount();
777
        }
778

    
779
        /**
780
         * <p>
781
         * Returns the index of the previous enabled tab.
782
         * </p>
783
         * @return The index, or -1 if there is no one.
784
         */
785
        public int previousEnabledPage() {
786
                int currentPage = currentPage();
787
                int j = 0;
788
                if (currentPage == 0) {
789
                        j = -1;
790
                }
791
                for (int i = currentPage - 1; i > -1; i--) {
792
                        if (getJTabbedPane().isEnabledAt(i)) {
793
                                j = i;
794
                                break;
795
                        }
796
                }
797
                return j;
798
        }
799

    
800
        /**
801
         * <p>
802
         * Returns the previous of the previous enabled tab.
803
         * </p>
804
         * @return The index, or -1 if there is no one.
805
         */
806
        public int nextEnabledPage() {
807
                int currentPage = currentPage();
808
                int nPages = getNumTabs();
809
                if (currentPage == nPages) {
810
                        return -1;
811
                }
812
                for (int i = currentPage + 1; i < nPages; i++) {
813
                        if (getJTabbedPane().isEnabledAt(i)) {
814
                                return i;
815
                        }
816
                }
817
                return -1;
818
        }
819

    
820
        /**
821
         * Selects/deselects the transparency checkbox.
822
         * @param b
823
         */
824
        public void setTransparent(boolean b) {
825
                getJPanelFormatsAndSrs().getChkTransparency().setSelected(b);
826
        }
827

    
828
        public RasterDataParameters[] getDataParameters() { // throws LoadLayerException {
829
                ListModel model = getLayerPanel().getLstSelectedLayers().getModel();
830
                RasterDataParameters[] paramList = new RasterDataParameters[model.getSize()];
831

    
832
                for (int i = 0; i < model.getSize(); i++) {
833
                        WMTSTheme theme = ((LayerUI)model.getElementAt(i)).theme;
834
                        DataStoreParameters parameters = explorer.getStoredParameters();
835
                        WMTSDataParameters par = null;
836
                        if(parameters instanceof WMTSDataParameters) {
837
                                par = (WMTSDataParameters) parameters;
838
                        }
839
                        if(parameters instanceof TileDataParameters) {
840
                                DataParameters p = ((TileDataParameters) parameters).getDataParameters();
841
                                if(p instanceof WMTSDataParameters)
842
                                        par = (WMTSDataParameters) p;
843
                        }
844
                        String host = explorer.getHost();
845
            try {
846
                par.setURI(new URI(host));
847
            } catch (URISyntaxException e) {
848
                logger.warn("Can't create URI from "+host, e);
849
            }
850
                        par.setImageFormat(getSelectedImageFormat());
851
                        par.setInfoFormat(getSelectedInfoFormat());
852
                        par.setSRS(getSelectedSRS());
853
                        par.setLayer(theme.getLayer());
854
                        par.setDimension(getSelectedDimension());
855
                        par.setDimensionSelectedValue(getSelectedValueForDimension());
856
                        WMTSStyle style = getSelectedStyle(theme.getLayer().getTitle());
857
                        if(style != null)
858
                                par.setStyle(style);
859

    
860
                        paramList[i] = (RasterDataParameters)parameters;
861
                }
862
                return paramList;
863
        }
864

    
865
        private String getSelectedValueForDimension() {
866
                return getJPanelDimension().getSelectedValuePanel().getSelectedValue().getText();
867
        }
868

    
869
        private String getSelectedDimension() {
870
                return getJPanelDimension().getDimensionPanel().getSelectedDimension();
871
        }
872

    
873
        /**
874
         * Returns the current selected SRS.
875
         * @return
876
         */
877
        public String getSelectedSRS() {
878
                return (String) getJPanelFormatsAndSrs().getLstSRSs().getSelectedValue();
879
        }
880

    
881
        private String getSelectedImageFormat() {
882
                return (String) getJPanelFormatsAndSrs().getLstImageFormats().getSelectedValue();
883
        }
884

    
885
        private String getSelectedInfoFormat() {
886
                return (String) getJPanelFormatsAndSrs().getLstInfoFormats().getSelectedValue();
887
        }
888

    
889
        private WMTSStyle getSelectedStyle(String layerName) {
890
                return (WMTSStyle) getStyleTree().getSelectedStyle(layerName);
891
        }
892

    
893
        @SuppressWarnings("unused")
894
        private boolean getSelectedTransparency() {
895
                return getJPanelFormatsAndSrs().getChkTransparency().isSelected();
896
        }
897

    
898
        public void initWizard() {
899
        }
900

    
901
        public void execute() {
902
        }
903

    
904
        protected boolean nodeSelected(TreePath[] selection, WMTSTheme node) {
905
                for (int i = 0; i < selection.length; i++) {
906
                        Object[] components = selection[i].getPath();
907
                        for (int j = 0; j < components.length; j++) {
908
                                if (components[j] instanceof WMTSTheme) {
909
                                        if (((WMTSTheme) components[j]).getTitle().compareTo(
910
                                                        node.getTitle()) == 0) {
911
                                                return true;
912
                                        }
913
                                }
914
                        }
915
                }
916
                return false;
917
        }
918

    
919
        public List<TreePath> getSelectedPaths() {
920
                return selectedPaths;
921
        }
922

    
923
        public LayerTree getJustTreeLayer() {
924
                return getLayerPanel().getTreeLayers();
925
        }
926

    
927
        public DataStoreParameters[] getParameters() {
928
                return null;
929
        }
930

    
931
        public void close() {
932
        }
933

    
934
        public WMTSServerExplorer getExplorer() {
935
                return explorer;
936
        }
937

    
938
        public void actionPerformed(ActionEvent e) {
939
                if(e.getSource() == getLayerPanel().getBtnDown()) {
940
                        LayerList list = getLayerPanel().getLstSelectedLayers();
941
                        int[] selectedIndices = list.getSelectedIndices();
942
                        int index = list.getSelectedIndex();
943
                        if (selectedIndices != null
944
                                        && selectedIndices.length == 1
945
                                        && index < getLayerPanel().getLstSelectedLayers().getModel().getSize() - 1) {
946
                                JDnDListModel m = (JDnDListModel) list.getModel();
947
                                m.itemsMoved(index + 1, new int[] { index });
948
                                list.setSelectedIndex(index + 1);
949
                        }
950
                }
951

    
952
                if(e.getSource() == getLayerPanel().getBtnUp()) {
953
                        LayerList list = getLayerPanel().getLstSelectedLayers();
954
                        int[] selectedIndices = list.getSelectedIndices();
955
                        int index = list.getSelectedIndex();
956
                        if (selectedIndices != null && selectedIndices.length == 1
957
                                        && index > 0) {
958
                                JDnDListModel m = (JDnDListModel) list.getModel();
959
                                m.itemsMoved(index - 1, new int[] { index });
960
                                list.setSelectedIndex(index - 1);
961
                        }
962
                }
963

    
964
                if(e.getSource() == getLayerPanel().getBtnAdd()) {
965
                        addLayer();
966
                }
967

    
968
                if(e.getSource() == getLayerPanel().getBtnDel()) {
969
                        delLayer();
970
                }
971

    
972
                if(e.getSource() == getLayerPanel().getChkExtendedNames()) {
973
                        fPrefs.putBoolean("show_layer_names", getLayerPanel().getChkExtendedNames().isSelected());
974
                }
975

    
976
                if(e.getSource() == getLayerPanel().getLayerListViewSelection()) {
977
                        ((JDnDListModel)getLayerPanel().getLstSelectedLayers().getModel()).clear();
978
                        layerPanel.getLstSelectedLayers().repaint();
979

    
980
                        getLayerPanel().getTreeLayers().setModel(new LayerTreeModel(explorer.getOGCClient().getLayerListAsThemes()));
981
                        refreshInfoPanel();
982
                }
983

    
984
                if(e.getSource() == getLayerPanel().getThemesViewSelection()) {
985
                        if(explorer.getOGCClient().getThemes().hasThemes()) {
986
                                ((JDnDListModel)getLayerPanel().getLstSelectedLayers().getModel()).clear();
987
                                layerPanel.getLstSelectedLayers().repaint();
988

    
989
                                getLayerPanel().getTreeLayers().setModel(new LayerTreeModel(explorer.getOGCClient().getThemes()));
990
                                refreshInfoPanel();
991
                        } else {
992
                                getLayerPanel().getLayerListViewSelection().setSelected(true);
993
                                JOptionPane.showOptionDialog(this,
994
                                                Messages.getText("view_not_disposable"),
995
                                                Messages.getText("confirmacion"),
996
                                                JOptionPane.OK_OPTION,
997
                                                JOptionPane.INFORMATION_MESSAGE,
998
                                                null,
999
                                                new Object[]{Messages.getText("accept")},
1000
                                                Messages.getText("accept"));
1001
                        }
1002
                }
1003
        }
1004

    
1005
        public void itemStateChanged(ItemEvent e) {
1006
                if(e.getSource() == getLayerPanel().getChkExtendedNames()) {
1007
                        boolean b = getLayerPanel().getChkExtendedNames().isSelected();
1008
                        getLayerPanel().getTreeLayers().showLayerNames = b;
1009
                        getLayerPanel().getTreeLayers().repaint();
1010
                        getLayerPanel().getLstSelectedLayers().showLayerNames = b;
1011
                        getLayerPanel().getLstSelectedLayers().repaint();
1012
                        getStyleTree().showLayerNames = b;
1013
                }
1014

    
1015
                if(e.getSource() == getJPanelFormatsAndSrs().getChkTransparency()) {
1016
                        fireWizardComplete();
1017
                }
1018
        }
1019

    
1020
        public void keyTyped(KeyEvent e) {
1021
        }
1022

    
1023
        public void keyPressed(KeyEvent e) {
1024
        }
1025

    
1026
        public void keyReleased(KeyEvent e) {
1027
        }
1028

    
1029
        public void valueChanged(ListSelectionEvent e) {
1030
                fireWizardComplete();
1031
        }
1032

    
1033
        public void mouseClicked(MouseEvent e) {
1034
                if(e.getSource() == getLayerPanel().getTreeLayers()) {
1035
                        if (e.getClickCount() == 2) {
1036
                                addLayer();
1037
                        }
1038
                }
1039
        }
1040

    
1041
        public void mouseEntered(MouseEvent e) {
1042
        }
1043

    
1044
        public void mouseExited(MouseEvent e) {
1045
        }
1046

    
1047
        public void mousePressed(MouseEvent e) {
1048
        }
1049

    
1050
        public void mouseReleased(MouseEvent e) {
1051
                if(e.getSource() == getLayerPanel().getLstSelectedLayers()) {
1052
                        refreshSelectedLayerData();
1053
                }
1054
        }
1055

    
1056
        public void valueChanged(TreeSelectionEvent e) {
1057
                if(e.getSource() == getLayerPanel().getTreeLayers()) {
1058
                        boolean isGetMapable = false;
1059
                        TreePath[] selecciones = getLayerPanel().getTreeLayers().getSelectionPaths();
1060

    
1061
                        if (selecciones != null) {
1062
                                for (int i = 0; i < selecciones.length && !isGetMapable; i++) {
1063
                                        if(selecciones[i].getLastPathComponent() instanceof WMTSTheme) {
1064
                                                WMTSTheme layer = ((WMTSTheme) selecciones[i].getLastPathComponent());
1065
                                                isGetMapable = layer.getChildCount() == 0 || layer.getTitle() != null;
1066
                                        }
1067
                                }
1068

    
1069
                        }
1070
                        getLayerPanel().getBtnAdd().setEnabled(isGetMapable);
1071
                        fireWizardComplete();
1072
                }
1073
        }
1074

    
1075
        public void setDeleteCache(boolean deleteCache) {
1076
                this.deleteCache = deleteCache;
1077
        }
1078

    
1079
        public boolean getDeleteCache() {
1080
                return this.deleteCache;
1081
        }
1082

    
1083
        public void addChangeTabListener(MouseAdapter eventMouse) {
1084
                getJTabbedPane().addMouseListener(eventMouse);
1085
        }
1086

    
1087
}