Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.arcims.feature.extension / src / main / java / org / gvsig / arcims / feature / gui / panels / ServiceNamesPanel.java @ 32436

History | View | Annotate | Download (28.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package org.gvsig.arcims.feature.gui.panels;
44

    
45
import java.awt.Color;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.ItemEvent;
49
import java.awt.event.ItemListener;
50
import java.awt.event.KeyEvent;
51
import java.awt.event.KeyListener;
52
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseListener;
54
import java.net.URL;
55
import java.util.ArrayList;
56
import java.util.List;
57
import java.util.Vector;
58

    
59
import javax.swing.JButton;
60
import javax.swing.JCheckBox;
61
import javax.swing.JComboBox;
62
import javax.swing.JComponent;
63
import javax.swing.JLabel;
64
import javax.swing.JOptionPane;
65
import javax.swing.JPanel;
66
import javax.swing.JScrollPane;
67
import javax.swing.JTable;
68
import javax.swing.JTextField;
69
import javax.swing.ListSelectionModel;
70
import javax.swing.table.TableColumn;
71
import javax.swing.table.TableColumnModel;
72

    
73
import org.gvsig.andami.PluginServices;
74
import org.gvsig.arcims.feature.gui.panels.utils.ServicesTableDataSource;
75
import org.gvsig.arcims.feature.gui.panels.utils.ServicesTableModel;
76
import org.gvsig.arcims.feature.gui.panels.utils.ServicesTableSelectionListener;
77
import org.gvsig.arcims.feature.gui.wizards.ArcImsFeatureWizard;
78
import org.gvsig.fmap.dal.DALLocator;
79
import org.gvsig.fmap.dal.DataManager;
80
import org.gvsig.fmap.dal.serverexplorer.arcims.ArcImsServerExplorer;
81
import org.gvsig.fmap.dal.serverexplorer.arcims.ArcImsServerExplorerParameters;
82
import org.gvsig.fmap.mapcontext.layers.FLayer;
83
import org.gvsig.remoteclient.arcims.ArcImsClient;
84
import org.gvsig.remoteclient.arcims.ArcImsProtocolHandler;
85
import org.gvsig.remoteclient.arcims.exceptions.ArcImsException;
86
import org.gvsig.remoteclient.arcims.utils.ServiceInfoTags;
87
import org.gvsig.remoteclient.arcims.utils.ServiceInformation;
88
import org.gvsig.utils.NotExistInXMLEntity;
89
import org.gvsig.utils.XMLEntity;
90
import org.slf4j.Logger;
91
import org.slf4j.LoggerFactory;
92

    
93
/**
94
 * This class implements the first panels of the ArcIMS wizard. It asks for the
95
 * server's URL and the service name (which can be either an ImageService or a
96
 * FeatureService). Depending on the type of service, one panel (Image Service
97
 * Panel) or the other (Feature Service Panel) will be enabled. All three panels
98
 * are owned by the ArcIMS wizard.
99
 * 
100
 * @see org.gvsig.arcims.feature.gui.panels.FeatureServicePanel
101
 * 
102
 * 
103
 * @author jldominguez
104
 * @author vsanjaime version 2.0
105
 */
106
public class ServiceNamesPanel extends JPanel implements ActionListener,
107
                MouseListener, KeyListener, ItemListener {
108
        private static Logger logger = LoggerFactory
109
                        .getLogger(ServiceNamesPanel.class.getName());
110
        private static final long serialVersionUID = 0;
111
        private ArcImsFeatureWizard parentWizard = null;
112
        private JPanel northPanel = null;
113
        private JLabel serverLabel = null;
114
        private JComboBox serverComboBox = null;
115
        private JButton connectButton = null;
116
        private JScrollPane servicesTableScrollPane = null;
117
        private JButton nextButton = null;
118
        private JButton previousButton = null;
119
        private JTable servicesTable = new JTable();
120
        private String selectedServiceType = "";
121
        private String selectedServiceName = "";
122
        private Vector<String> favoriteServers = new Vector<String>();
123
        private ServicesTableSelectionListener tableSelectionListener;
124
        private JPanel userDefinedServiceNamePanel = null;
125
        private JLabel serviceNameLabel = null;
126
        private JTextField userDefinedServiceNameTextField = null;
127
        private String serviceType = "";
128
        private URL completeURL = null;
129
        private JLabel versionTitleLabel = null;
130
        private JLabel versionLabel = null;
131
        private JCheckBox cacheOverride = null;
132

    
133
        /**
134
         * The owning wizard is used as a parameter to allow easy <i>upwards</i>
135
         * references.
136
         * 
137
         * @param parent
138
         *            the wizard that owns this panel
139
         */
140
        public ServiceNamesPanel(ArcImsFeatureWizard parent) {
141
                super();
142
                parentWizard = parent;
143
                initialize();
144
        }
145

    
146
        /**
147
         * initialize components
148
         */
149
        private void initialize() {
150
                this.setSize(501, 470);
151
                this.setLayout(null);
152
                this.add(getNorthPanel(), null);
153
                tableSelectionListener = new ServicesTableSelectionListener(this);
154
                this.add(getServicesTableScrollPane(), null);
155
                this.add(getNextButton(), null);
156
                this.add(getPreviousButton(), null);
157
                this.add(getUserDefinedServiceNamePanel(), null);
158
                this.nextButton.setEnabled(false);
159

    
160
                versionTitleLabel = new JLabel(PluginServices.getText(this,
161
                                "server_version")
162
                                + ":");
163
                versionTitleLabel.setBounds(new java.awt.Rectangle(39, 443, 125, 21));
164
                this.add(versionTitleLabel, null);
165
                versionLabel = new JLabel("-");
166
                versionLabel.setBounds(new java.awt.Rectangle(170 - 15, 443, 160, 21));
167
                this.add(versionLabel, null);
168

    
169
                servicesTable.addMouseListener(this);
170
                servicesTable.setBounds(new java.awt.Rectangle(15, 45, 400, 181));
171
                servicesTable.setDragEnabled(false);
172
                servicesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
173
                servicesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
174

    
175
                fireServerUrlUnknown();
176
        }
177

    
178
        /**
179
         * Gets the service type of the selected service (ImageService or
180
         * FeatureService)
181
         * 
182
         * @return service type of the service selected by the user
183
         */
184
        public String getSelectedServiceType() {
185
                return selectedServiceType;
186
        }
187

    
188
        /**
189
         * This method initializes northPanel
190
         * 
191
         * @return javax.swing.JPanel
192
         */
193
        private JPanel getNorthPanel() {
194
                if (northPanel == null) {
195
                        serverLabel = new JLabel();
196
                        serverLabel.setText("URL:");
197
                        serverLabel.setBounds(new java.awt.Rectangle(10, 20, 71, 20));
198

    
199
                        versionTitleLabel = new JLabel(PluginServices.getText(this,
200
                                        "version")
201
                                        + ":");
202
                        versionTitleLabel.setBounds(new java.awt.Rectangle(10, 40, 71, 20));
203
                        versionLabel = new JLabel("-");
204
                        versionLabel.setBounds(new java.awt.Rectangle(60, 40, 200, 20));
205

    
206
                        cacheOverride = new JCheckBox(PluginServices.getText(this,
207
                                        "update_catalog"), false);
208
                        cacheOverride.setBounds(new java.awt.Rectangle(7, 60 - 9, 200, 20));
209

    
210
                        northPanel = new JPanel();
211
                        northPanel.setLayout(null);
212
                        northPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
213
                                        null, PluginServices.getText(this, "server"),
214
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
215
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
216
                                        null));
217
                        northPanel.setBounds(new java.awt.Rectangle(17, 10, 477, 85)); // hasta
218
                        // y
219
                        // =
220
                        // 96
221
                        // northPanel.add(serverLabel, null);
222

    
223
                        northPanel.add(getServerComboBox(), null);
224
                        northPanel.add(getConnectButton(), null);
225

    
226
                        // northPanel.add(versionTitleLabel);
227
                        // northPanel.add(versionLabel);
228
                        northPanel.add(cacheOverride);
229
                }
230

    
231
                return northPanel;
232
        }
233

    
234
        /**
235
         * Initializes the JComboBox that contains the URLs of the favorite servers,
236
         * read from a XML file located on the user's local directory.
237
         * 
238
         * @return javax.swing.JComboBox
239
         */
240
        public JComboBox getServerComboBox() {
241
                if (serverComboBox == null) {
242
                        serverComboBox = new JComboBox();
243
                        serverComboBox.setEditable(true);
244

    
245
                        serverComboBox.getEditor().getEditorComponent()
246
                                        .addKeyListener(this);
247
                        serverComboBox.addItemListener(this);
248

    
249
                        loadFavoriteServers();
250
                        addServersFromVector();
251

    
252
                        serverComboBox.setBounds(new java.awt.Rectangle(11, 26, 380 + 74,
253
                                        20));
254
                }
255

    
256
                return serverComboBox;
257
        }
258

    
259
        /**
260
         * Inicializes the connectButton
261
         * 
262
         * @return javax.swing.JButton
263
         */
264
        public JButton getConnectButton() {
265
                if (connectButton == null) {
266
                        connectButton = new JButton();
267
                        connectButton.setText(PluginServices.getText(this, "conectar"));
268
                        connectButton.setBounds(new java.awt.Rectangle(383 - 17, 33 + 17,
269
                                        100, 20));
270

    
271
                        connectButton.addActionListener(this);
272
                }
273

    
274
                return connectButton;
275
        }
276

    
277
        /**
278
         * Inicializes the servicesTableScrollPane
279
         * 
280
         * @return javax.swing.JScrollPane
281
         */
282
        private JScrollPane getServicesTableScrollPane() {
283
                if (servicesTableScrollPane == null) {
284
                        servicesTableScrollPane = new JScrollPane();
285
                        servicesTableScrollPane
286
                                        .setBorder(javax.swing.BorderFactory
287
                                                        .createTitledBorder(
288
                                                                        null,
289
                                                                        PluginServices.getText(this,
290
                                                                                        "available_services"),
291
                                                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
292
                                                                        javax.swing.border.TitledBorder.DEFAULT_POSITION,
293
                                                                        null, null));
294

    
295
                        servicesTableScrollPane.setBounds(new java.awt.Rectangle(17, 152,
296
                                        477, 261 + 12)); // hasta y = 413
297
                }
298

    
299
                return servicesTableScrollPane;
300
        }
301

    
302
        /**
303
         * Inicializes the nextButton, which will enable and open a new panel
304
         * depending on the user's selection
305
         * 
306
         * @return javax.swing.JButton
307
         */
308
        public JButton getNextButton() {
309
                if (nextButton == null) {
310
                        nextButton = new JButton(); // 
311
                        nextButton.setText(PluginServices.getText(this, "siguiente"));
312
                        nextButton.setBounds(new java.awt.Rectangle(395, 444, 100, 20));
313
                        nextButton.addActionListener(this);
314
                }
315

    
316
                return nextButton;
317
        }
318

    
319
        public JButton getPreviousButton() {
320
                if (previousButton == null) {
321
                        previousButton = new JButton(); // 
322
                        previousButton.setText(PluginServices.getText(this, "anterior"));
323
                        previousButton.setBounds(new java.awt.Rectangle(395 - 103, 444,
324
                                        100, 20));
325
                        previousButton.setEnabled(false);
326
                        previousButton.addActionListener(this);
327
                }
328

    
329
                return previousButton;
330
        }
331

    
332
        public void setServerComboText(String s) {
333
                this.serverComboBox.addItem(s);
334
                this.serverComboBox.setSelectedItem(s);
335
        }
336

    
337
        /**
338
         * Writes the list of ArcIMS servers contained in the serverComboBox on a
339
         * XML local file. It should remove server URLs from unsuccessful
340
         * connections.
341
         * 
342
         * @param v
343
         *            a vector of Strings (servers' URLs)
344
         */
345
        private void writeServerVectorToXML(Vector<String> v) {
346
                String s = "";
347

    
348
                for (int i = 0; i < v.size(); i++)
349
                        s = s + "," + (v.get(i));
350

    
351
                s = s.substring(1);
352

    
353
                XMLEntity xml = PluginServices.getPluginServices(this)
354
                                .getPersistentXML();
355

    
356
                if (xml == null) {
357
                        xml = new XMLEntity();
358
                        xml.putProperty("arcims-servers", s);
359
                } else {
360
                        xml.putProperty("arcims-servers", s);
361
                }
362
        }
363

    
364
        /**
365
         * Loads a local variable (favoriteServers) with a list of servers and the
366
         * ones found in the local persistent data XML file.
367
         * 
368
         */
369
        private void loadFavoriteServers() {
370
                favoriteServers.add("http://njgin.state.nj.us");
371
                // favoriteServers.add("http://massotti.carm.es");
372
                // favoriteServers.add("http://cartoweb.paeria.es"); // Ayunt. de Lerida
373
                // favoriteServers.add("http://geodaten.stadt-bottrop.de"); // Ayunt. de
374
                // Bottrop (Alemania)
375
                // favoriteServers.add("http://comercio.ideam.gov.co");
376
                // favoriteServers.add("http://atl.cenapred.unam.mx");
377
                // favoriteServers.add("http://gdz1.leipzig.ifag.de");
378

    
379
                XMLEntity xml = PluginServices.getPluginServices(this)
380
                                .getPersistentXML();
381

    
382
                if (xml == null) {
383
                        return;
384
                }
385

    
386
                try {
387
                        String[] s = xml.getStringArrayProperty("arcims-servers");
388
                        String[] servers = s[0].split(",");
389

    
390
                        for (int i = 0; i < servers.length; i++)
391
                                addIfNotRepeated(favoriteServers, servers[i]);
392
                } catch (NotExistInXMLEntity e) {
393
                        logger.warn("Property 'arcims-servers' not found in XML file. ");
394
                }
395
        }
396

    
397
        /**
398
         * 
399
         * @param v
400
         * @param s
401
         */
402
        private void addIfNotRepeated(Vector<String> v, String s) {
403
                if (v.contains(s)) {
404
                        return;
405
                }
406
                v.add(0, s);
407
        }
408

    
409
        /**
410
         * 
411
         * @param v
412
         * @param s
413
         */
414
        private void removeServerFromVector(Vector<String> v, String s) {
415
                if (!(v.contains(s))) {
416
                        return;
417
                }
418

    
419
                v.remove(s);
420
        }
421

    
422
        /**
423
         * Utility method to load the server names from a 'favoriteServers' vector
424
         * into the server names combobox.
425
         */
426
        private void addServersFromVector() {
427
                for (int i = 0; i < favoriteServers.size(); i++)
428
                        this.serverComboBox.addItem((String) favoriteServers.get(i));
429
        }
430

    
431
        /**
432
         * Listens to mouse events on the available services list. A double click
433
         * will have the same effect as a click on the nextButton.
434
         */
435
        public void mouseClicked(MouseEvent e) {
436
                if (e.getSource() == this.servicesTable) {
437
                        if (!this.servicesTable.isEnabled()) {
438
                                return;
439
                        }
440

    
441
                        int selrow = servicesTable.getSelectedRow();
442

    
443
                        if (selrow == -1) {
444
                                return;
445
                        }
446

    
447
                        String servname = ServicesTableModel
448
                                        .getColumnValueOfRow(servicesTable, PluginServices.getText(
449
                                                        this, "name"), selrow);
450
                        userDefinedServiceNameTextField.setText(servname);
451

    
452
                        // double click event:
453
                        if (e.getClickCount() == 2) {
454
                                ActionEvent artificialEvent = new ActionEvent(nextButton,
455
                                                ActionEvent.ACTION_PERFORMED, "");
456
                                this.actionPerformed(artificialEvent);
457
                        }
458
                }
459
        }
460

    
461
        /**
462
         * 
463
         */
464
        public void mouseEntered(MouseEvent e) {
465
        }
466

    
467
        /**
468
         * 
469
         */
470
        public void mouseExited(MouseEvent e) {
471
        }
472

    
473
        /**
474
         * 
475
         */
476
        public void mousePressed(MouseEvent e) {
477
        }
478

    
479
        /**
480
         * 
481
         */
482
        public void mouseReleased(MouseEvent e) {
483
        }
484

    
485
        /**
486
         * Listens to button events: connectButton will retrieve the server's
487
         * available services list (catalog). NextButton will be enabled if a valid
488
         * service (imageservice or featureservice) has been selected by the user.
489
         */
490
        public void actionPerformed(ActionEvent e) {
491
                if (e.getSource() == connectButton) {
492
                        // ---------------------- CONNECT BUTTON - START
493
                        this.nextButton.setEnabled(false);
494

    
495
                        boolean badURL = false;
496
                        URL url = null;
497
                        ServicesTableDataSource stds = null;
498
                        url = null;
499

    
500
                        // find out complete url (using libArcIMS):
501
                        try {
502
                                url = new URL(serverComboBox.getSelectedItem().toString());
503
                                this.completeURL = ArcImsProtocolHandler.getUrlWithServlet(url);
504
                                this.parentWizard.createServerExplorer(completeURL.toString(), "4.0.1");
505

    
506
                                List<List<String>> services = this.parentWizard.getServerExplorer().listServices();
507

    
508
                                // this truly loads the service names, types and status:
509
                                stds = new ServicesTableDataSource(services);
510
                        } catch (Exception e1) {
511
                                badURL = true;
512
                        }
513

    
514
                        if (badURL) {
515
                                logger.info("Wrong url");
516
                                JOptionPane.showMessageDialog(this, PluginServices.getText(
517
                                                this, "wrong_url")
518
                                                + ": " + serverComboBox.getSelectedItem().toString(),
519
                                                PluginServices.getText(this, "wrong_url"),
520
                                                JOptionPane.ERROR_MESSAGE);
521

    
522
                                return;
523
                        } else {
524
                                // check that the list is non-empty:
525
                                if (stds.getDataVector().size() == 0) {
526
                                        JOptionPane.showMessageDialog(this, PluginServices.getText(
527
                                                        this, "server_has_no_services")
528
                                                        + ": " + url, PluginServices.getText(this,
529
                                                        "server_has_no_services"),
530
                                                        JOptionPane.ERROR_MESSAGE);
531

    
532
                                        // remove useless url:
533
                                        removeServerFromVector(favoriteServers, url.toString());
534
                                        
535

    
536
                                        return;
537
                                }
538
                        }
539

    
540
                        // enables certain controls if the url is ok and there are services:
541
                        fireServerUrlOk();
542

    
543
                        // save working url:
544
                        favoriteServers.add(0, url.toString());
545
                        writeServerVectorToXML(favoriteServers);
546

    
547
                        parentWizard.getServerExplorer().getStatus().setServerURL(completeURL.toString());
548

    
549
                        ServicesTableModel dtm = new ServicesTableModel(stds
550
                                        .getDataVector(), stds.getColNamesVector());
551
                        setModelAndWidths(servicesTable, dtm);
552

    
553
                        // Ask to be notified of selection changes.
554
                        ListSelectionModel rowSM = servicesTable.getSelectionModel();
555
                        rowSM.addListSelectionListener(this.tableSelectionListener);
556

    
557
                        servicesTableScrollPane.setViewportView(servicesTable);
558
                        servicesTable.repaint();
559
                        servicesTableScrollPane.repaint();
560
                        
561
                        // ---------------------- CONNECT BUTTON - END
562
                }
563

    
564
                // ---------------------- NEXT BUTTON - START
565
                if (e.getSource() == nextButton) {
566
                        
567
                        // get serverexplorer
568
                        ArcImsServerExplorer explorer = this.parentWizard
569
                        .getServerExplorer();
570
                        
571
                        // Get service name
572
                        String userServiceName = userDefinedServiceNameTextField.getText();
573
                        
574
                        
575
                        int ind = -1;
576

    
577
                        try {
578
                                ind = ServicesTableModel.getFirstRowWithValueInColumnName(
579
                                                servicesTable, PluginServices.getText(this,
580
                                                                ServiceInfoTags.aNAME), userServiceName);
581
                        } catch (ArcImsException e1) {
582
                                logger.error("While searching value in table ", e1);
583
                                ind = -1;
584
                        }
585

    
586
                        if (ind == -1) {
587
                                JOptionPane.showMessageDialog(this, PluginServices.getText(
588
                                                this, "service_does_not_exist")
589
                                                + ": " + userServiceName, "Error",
590
                                                JOptionPane.ERROR_MESSAGE);
591

    
592
                                return;
593
                        }
594

    
595
                        selectedServiceType = ServicesTableModel
596
                                        .getColumnValueOfRowWithIndex(servicesTable, 1, ind);
597
                        selectedServiceName = userServiceName;
598
                        explorer.getStatus().setServiceName(userServiceName);
599
                        explorer.getStatus().setServiceType(selectedServiceType);
600
                        explorer.getClient().getHandler().setService(userServiceName);
601

    
602
                        // }
603

    
604
                        // check that service type is supported:
605
                        if (!isSupportedServiceType(selectedServiceName)) {
606
                                return;
607
                        }
608

    
609
                        // Get list layers of selected service
610
                        List<String> layers = new ArrayList<String>();
611
                        try {
612
                                
613
                                layers = explorer.list();
614
                        } catch (Exception de) {
615
                                logger.error("While setting host service ", de);
616
                                showErrorMessage(de.getMessage());
617
                                return;
618
                        }
619
                        
620
                        parentWizard.fillAndMoveTabbedPaneToEnabled();
621

    
622
                        // ---------------------- NEXT BUTTON - END
623
                }
624
        }
625

    
626
        /**
627
         * 
628
         * @param lyr
629
         */
630
        public void pseudoNextFired(FLayer lyr) {
631
                // if the 'user defined' option is set, local variables get values from
632
                // the combobox
633
                // if (serviceNameSelectionModeUserRadioButton.isSelected()) {
634
                String userServiceName = userDefinedServiceNameTextField.getText();
635
                int ind = -1;
636

    
637
                try {
638
                        ind = ServicesTableModel.getFirstRowWithValueInColumnName(
639
                                        servicesTable, PluginServices.getText(this,
640
                                                        ServiceInfoTags.aNAME), userServiceName);
641
                } catch (ArcImsException e1) {
642
                        logger.error("While searching value in table ", e1);
643
                        ind = -1;
644
                }
645

    
646
                if (ind == -1) {
647
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,
648
                                        "service_does_not_exist")
649
                                        + ": " + userServiceName, "Error",
650
                                        JOptionPane.ERROR_MESSAGE);
651

    
652
                        return;
653
                }
654

    
655
                selectedServiceType = ServicesTableModel.getColumnValueOfRowWithIndex(
656
                                servicesTable, 1, ind);
657
                selectedServiceName = userServiceName;
658

    
659
                // }
660

    
661
                // check that service type is supported:
662
                if (!isSupportedServiceType(selectedServiceName)) {
663
                        return;
664
                }
665

    
666
                try {
667
                        // +++++++++++++ This line truly starts the connection: ++++++++++++
668
                        // +++++++++++++ creates driver, client and gets capabilities
669
                        // ++++++++++++
670
                        // FIXME Vicent
671
                        // parentWizard.getDataSource().setHostService(completeURL,
672
                        // selectedServiceName, selectedServiceType, lyr);
673
                } catch (Exception de) {
674
                        logger.error("While setting host service ", de);
675
                        showErrorMessage(de.getMessage());
676

    
677
                        return;
678
                }
679

    
680
                // get service information:
681
                ArcImsClient cli = null;
682

    
683
                if (selectedServiceType
684
                                .compareToIgnoreCase(ServiceInfoTags.vFEATURESERVICE) == 0) {
685
                        // FIXME
686
                        // FMapFeatureArcImsDriver fdrv = (FMapFeatureArcImsDriver)
687
                        // parentWizard
688
                        // .getDataSource().getDriver();
689
                        // cli = (ArcImsClient) fdrv.getClient();
690
                }
691

    
692
                ServiceInformation si = cli.getServiceInformation();
693

    
694
                // 
695
                if ((si.getLayers() == null) || (si.getLayers().size() == 0)) {
696
                        logger.warn("No layers found ");
697
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,
698
                                        "no_layers_found")
699
                                        + ". "
700
                                        + PluginServices.getText(this, "service")
701
                                        + ": "
702
                                        + selectedServiceName, PluginServices.getText(this,
703
                                        "no_layers_found"), JOptionPane.ERROR_MESSAGE);
704

    
705
                        return;
706
                }
707

    
708
                parentWizard.fillAndMoveTabbedPaneToEnabled();
709
        }
710

    
711
        /**
712
         * 
713
         * @param table
714
         * @param m
715
         */
716
        private void setModelAndWidths(JTable table, ServicesTableModel m) {
717
                table.setModel(m);
718

    
719
                TableColumnModel cm = table.getColumnModel();
720
                TableColumn col;
721
                int count = cm.getColumnCount();
722

    
723
                if (count == 3) {
724
                        col = cm.getColumn(0);
725
                        col.setPreferredWidth(230);
726
                        col = cm.getColumn(1);
727
                        col.setPreferredWidth(110);
728
                        col = cm.getColumn(2);
729
                        col.setPreferredWidth(110);
730
                        table.repaint();
731
                }
732
        }
733

    
734
        /**
735
         * 
736
         * @param message
737
         */
738
        private void showErrorMessage(String message) {
739
                JOptionPane.showMessageDialog(this, PluginServices.getText(this,
740
                                message), PluginServices.getText(this, "error"),
741
                                JOptionPane.ERROR_MESSAGE);
742
        }
743

    
744
        /**
745
         * 
746
         * @return
747
         */
748
        private boolean servicesTableHasSelection() {
749
                return (servicesTable.getSelectedRow() != -1);
750
        }
751

    
752
        /**
753
         * Utility method to manage the services names table.
754
         * 
755
         * @param sName
756
         *            service name
757
         * @return row that contains it
758
         */
759
        public int getRowNumberOfServiceName(String sName) {
760
                int l = servicesTable.getRowCount();
761
                String s;
762

    
763
                for (int i = 0; i < l; i++) {
764
                        s = ServicesTableModel.getColumnValueOfRow(servicesTable,
765
                                        PluginServices.getText(this, "name"), i);
766

    
767
                        if (s.compareToIgnoreCase(sName) == 0) {
768
                                return i;
769
                        }
770
                }
771

    
772
                logger.error("Service name not found: " + sName);
773

    
774
                return -1;
775
        }
776

    
777
        public JTable getServicesTable() {
778
                return servicesTable;
779
        }
780

    
781
        public void setServicesTable(JTable servicesTable) {
782
                this.servicesTable = servicesTable;
783
        }
784

    
785
        public void setSelectedServiceType(String selectedServiceType) {
786
                this.selectedServiceType = selectedServiceType;
787
        }
788

    
789
        public String getSelectedServiceName() {
790
                return selectedServiceName;
791
        }
792

    
793
        public void setSelectedServiceName(String selectedServiceName) {
794
                this.selectedServiceName = selectedServiceName;
795
        }
796

    
797
        public ArcImsFeatureWizard getParentWizard() {
798
                return parentWizard;
799
        }
800

    
801
        public void setParentWizard(ArcImsFeatureWizard parentWizard) {
802
                this.parentWizard = parentWizard;
803
        }
804

    
805
        public ServicesTableSelectionListener getTableSelectionListener() {
806
                return tableSelectionListener;
807
        }
808

    
809
        public void setTableSelectionListener(
810
                        ServicesTableSelectionListener tableSelectionListener) {
811
                this.tableSelectionListener = tableSelectionListener;
812
        }
813

    
814
        /**
815
         * This method initializes userDefinedServiceNamePanel
816
         * 
817
         * @return javax.swing.JPanel
818
         */
819
        private JPanel getUserDefinedServiceNamePanel() {
820
                if (userDefinedServiceNamePanel == null) {
821
                        userDefinedServiceNamePanel = new JPanel();
822
                        userDefinedServiceNamePanel.setBounds(new java.awt.Rectangle(17,
823
                                        97, 477, 56)); // hasta y = 151
824
                        userDefinedServiceNamePanel.setLayout(null);
825
                        userDefinedServiceNamePanel
826
                                        .setBorder(javax.swing.BorderFactory
827
                                                        .createTitledBorder(
828
                                                                        null,
829
                                                                        PluginServices
830
                                                                                        .getText(this, "service_name"),
831
                                                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
832
                                                                        javax.swing.border.TitledBorder.DEFAULT_POSITION,
833
                                                                        null, null));
834
                        userDefinedServiceNameTextField = new JTextField();
835
                        userDefinedServiceNameTextField.setBounds(11, 21, 380 + 74, 20);
836
                        userDefinedServiceNameTextField.addKeyListener(this);
837

    
838
                        serviceNameLabel = new JLabel(PluginServices.getText(this,
839
                                        "service_name"));
840
                        serviceNameLabel.setBounds(70, 21, 120, 20);
841

    
842
                        // serviceSelectionModeButtonGroup = new ButtonGroup();
843
                        // serviceSelectionModeButtonGroup.add(getServiceNameSelectionModeRadioButton());
844
                        // serviceSelectionModeButtonGroup.add(getServiceNameSelectionModeListRadioButton());
845
                        userDefinedServiceNamePanel.add(userDefinedServiceNameTextField);
846

    
847
                        // userDefinedServiceNamePanel.add(serviceNameLabel);
848
                }
849

    
850
                return userDefinedServiceNamePanel;
851
        }
852

    
853
        /**
854
         * This method initializes serviceNameSelectionModeRadioButton
855
         * 
856
         * @return javax.swing.JRadioButton
857
         */
858

    
859
        /*
860
         * public JRadioButton getServiceNameSelectionModeRadioButton() { if
861
         * (serviceNameSelectionModeUserRadioButton == null) {
862
         * serviceNameSelectionModeUserRadioButton = new JRadioButton();
863
         * serviceNameSelectionModeUserRadioButton.addActionListener(this);
864
         * serviceNameSelectionModeUserRadioButton .setBounds(new
865
         * java.awt.Rectangle(170, 20, 160, 20));
866
         * serviceNameSelectionModeUserRadioButton.setText(PluginServices
867
         * .getText(this, "defined_by_user") + ":"); } return
868
         * serviceNameSelectionModeUserRadioButton; }
869
         */
870

    
871
        /**
872
         * This method initializes serviceNameSelectionModeListRadioButton
873
         * 
874
         * @return javax.swing.JRadioButton
875
         */
876

    
877
        /*
878
         * private JRadioButton getServiceNameSelectionModeListRadioButton() { if
879
         * (serviceNameSelectionModeListRadioButton == null) {
880
         * serviceNameSelectionModeListRadioButton = new JRadioButton();
881
         * serviceNameSelectionModeListRadioButton.addActionListener(this);
882
         * serviceNameSelectionModeListRadioButton .setBounds(new
883
         * java.awt.Rectangle(10, 20, 160, 20));
884
         * serviceNameSelectionModeListRadioButton.setText(PluginServices
885
         * .getText(this, "selected_from_list"));
886
         * 
887
         * } return serviceNameSelectionModeListRadioButton; }
888
         */
889
        private void setEnableComponent(JComponent c, boolean v) {
890
                if (c == null) {
891
                        return;
892
                }
893

    
894
                c.setEnabled(v);
895

    
896
                if (c instanceof JTable) {
897
                        if (v) {
898
                                c.setForeground(Color.BLACK);
899
                        } else {
900
                                c.setForeground(Color.LIGHT_GRAY);
901
                        }
902
                }
903
        }
904

    
905
        public void keyPressed(KeyEvent e) {
906
        }
907

    
908
        public void keyTyped(KeyEvent e) {
909
        }
910

    
911
        public void keyReleased(KeyEvent e) {
912
                if (e.getSource() == userDefinedServiceNameTextField) {
913
                        boolean user = true;
914
                        int length = userDefinedServiceNameTextField.getText().length();
915
                        setServiceType("");
916
                        nextButton.setEnabled(user && (length > 0));
917
                }
918

    
919
                if (e.getSource() == serverComboBox.getEditor().getEditorComponent()) {
920
                        fireServerUrlUnknown();
921
                }
922
        }
923

    
924
        private void fireServerUrlUnknown() {
925
                // setEnableComponent(serviceNameSelectionModeListRadioButton, false);
926
                // setEnableComponent(serviceNameSelectionModeUserRadioButton, false);
927
                setEnableComponent(servicesTable, false);
928
                setEnableComponent(nextButton, false);
929
                setEnableComponent(userDefinedServiceNameTextField, false);
930

    
931
                if (versionLabel != null) {
932
                        versionLabel.setText("-");
933
                }
934
        }
935

    
936
        private void fireServerUrlOk() {
937
                
938
                setEnableComponent(servicesTable, true);
939
                setEnableComponent(nextButton, true);
940

    
941
                String str = "-";
942

    
943
                if (versionLabel != null) {
944
                        str = getServerVersion(completeURL);
945
                        versionLabel.setText(str);
946
                        parentWizard.setServerVersionInPanels(str);
947
                }
948
                
949
                setUserDecisionTrue();
950
        }
951

    
952
        /**
953
         * This method calls the liArcIMS library to quickly find out the server
954
         * full url (with '/servlet/...')
955
         * 
956
         * @param url
957
         *            server short url
958
         * @return full url
959
         */
960
        private String getServerVersion(URL url) {
961
                String[] vers;
962

    
963
                try {
964
                        vers = ArcImsProtocolHandler.getVersion(url);
965
                } catch (ArcImsException e) {
966
                        logger.error("Server version not found ", e);
967
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,
968
                                        "unable_to_findout_server_version"), PluginServices
969
                                        .getText(this, "unable_to_findout_server_version"),
970
                                        JOptionPane.ERROR_MESSAGE);
971

    
972
                        return "-";
973
                }
974

    
975
                return vers[0] + " (" + vers[1] + ")";
976
        }
977

    
978
        public String getServiceType() {
979
                return serviceType;
980
        }
981

    
982
        public void setServiceType(String serviceType) {
983
                this.serviceType = serviceType;
984
        }
985

    
986
        public void setUserServiceName(String str) {
987
                userDefinedServiceNameTextField.setText(str);
988
        }
989

    
990
        public void itemStateChanged(ItemEvent e) {
991
                this.fireServerUrlUnknown();
992
        }
993

    
994
        /**
995
         * 
996
         * Utility method to decide if a service type is supported or not
997
         * (ServiceInfoTags.IMAGESERVICE, etc)
998
         */
999
        private boolean isSupportedServiceType(String svcName) {
1000
                int ind = -1;
1001

    
1002
                try {
1003
                        ind = ServicesTableModel.getFirstRowWithValueInColumnIndex(
1004
                                        servicesTable, 0, svcName);
1005
                } catch (ArcImsException e1) {
1006
                        logger.error("While searching value in table ", e1);
1007
                        ind = -1;
1008
                }
1009

    
1010
                if (ind == -1) {
1011
                        // should never happen
1012
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,
1013
                                        "service_does_not_exist")
1014
                                        + ": " + svcName, "Error", JOptionPane.ERROR_MESSAGE);
1015

    
1016
                        return false;
1017
                }
1018

    
1019
                String svcType = ServicesTableModel.getColumnValueOfRowWithIndex(
1020
                                servicesTable, 1, ind);
1021

    
1022
                if ((svcType.compareToIgnoreCase(ServiceInfoTags.vIMAGESERVICE) != 0)
1023
                                && (svcType
1024
                                                .compareToIgnoreCase(ServiceInfoTags.vFEATURESERVICE) != 0)) {
1025
                        showNotImplementedMessage();
1026

    
1027
                        return false;
1028
                }
1029

    
1030
                return true;
1031
        }
1032

    
1033
        private void showNotImplementedMessage() {
1034
                JOptionPane.showMessageDialog(this, PluginServices.getText(this,
1035
                                "service_type_not_supported"), PluginServices.getText(this,
1036
                                "error"), JOptionPane.ERROR_MESSAGE);
1037
        }
1038

    
1039
        public void setUserDecisionTrue() {
1040
                setEnableComponent(userDefinedServiceNameTextField, true);
1041
                setEnableComponent(servicesTable, true);
1042

    
1043
                int length = userDefinedServiceNameTextField.getText().length();
1044
                setEnableComponent(nextButton, (length > 0));
1045
        }
1046
} // @jve:decl-index=0:visual-constraint="10,8"