Statistics
| Revision:

svn-gvsig-desktop / tags / v1_11_0_Build_1306 / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / gui / panels / ServiceNamesPanel.java @ 35731

History | View | Annotate | Download (35.9 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 es.prodevelop.cit.gvsig.arcims.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.MalformedURLException;
55
import java.net.URL;
56
import java.util.Vector;
57

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

    
72
import org.apache.log4j.Logger;
73
import org.gvsig.remoteClient.arcims.ArcImsClientP;
74
import org.gvsig.remoteClient.arcims.ArcImsProtocolHandler;
75
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
76
import org.gvsig.remoteClient.arcims.utils.ServiceInfoTags;
77
import org.gvsig.remoteClient.arcims.utils.ServiceInformation;
78

    
79
import com.hardcode.gdbms.engine.data.driver.DriverException;
80
import com.iver.andami.PluginServices;
81
import com.iver.cit.gvsig.fmap.layers.FLayer;
82
import com.iver.utiles.NotExistInXMLEntity;
83
import com.iver.utiles.XMLEntity;
84

    
85
import es.prodevelop.cit.gvsig.arcims.fmap.drivers.FMapFeatureArcImsDriver;
86
import es.prodevelop.cit.gvsig.arcims.fmap.drivers.FMapRasterArcImsDriver;
87
import es.prodevelop.cit.gvsig.arcims.gui.panels.utils.ServicesTableDataSource;
88
import es.prodevelop.cit.gvsig.arcims.gui.panels.utils.ServicesTableModel;
89
import es.prodevelop.cit.gvsig.arcims.gui.panels.utils.ServicesTableSelectionListener;
90
import es.prodevelop.cit.gvsig.arcims.gui.wizards.ArcImsWizard;
91

    
92

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

    
124
    // private JRadioButton serviceNameSelectionModeUserRadioButton = null;
125
    // private JRadioButton serviceNameSelectionModeListRadioButton = null;
126
    // private ButtonGroup serviceSelectionModeButtonGroup = null;
127
    private JLabel serviceNameLabel = null;
128
    private JTextField userDefinedServiceNameTextField = null;
129
    private String serviceType = "";
130
    private URL completeURL = null;
131
    private JLabel versionTitleLabel = null;
132
    private JLabel versionLabel = null;
133
    private JCheckBox cacheOverride = null;
134

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

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

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

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

    
173
        fireServerUrlUnknown();
174
    }
175

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

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

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

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

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

    
216
            northPanel.add(getServerComboBox(), null);
217
            northPanel.add(getConnectButton(), null);
218

    
219
            // northPanel.add(versionTitleLabel);
220
            // northPanel.add(versionLabel);
221
            northPanel.add(cacheOverride);
222
        }
223

    
224
        return northPanel;
225
    }
226

    
227
    /**
228
     * Initializes the JComboBox that contains the URLs of the favorite servers,
229
     * read from a XML file located on the user's local directory.
230
     *
231
     * @return javax.swing.JComboBox
232
     */
233
    public JComboBox getServerComboBox() {
234
        if (serverComboBox == null) {
235
            serverComboBox = new JComboBox();
236
            serverComboBox.setEditable(true);
237

    
238
            serverComboBox.getEditor().getEditorComponent().addKeyListener(this);
239
            serverComboBox.addItemListener(this);
240

    
241
            loadFavoriteServers();
242
            addServersFromVector();
243

    
244
            serverComboBox.setBounds(new java.awt.Rectangle(11, 26, 380 + 74, 20));
245
        }
246

    
247
        return serverComboBox;
248
    }
249

    
250
    /**
251
     * Inicializes the connectButton
252
     *
253
     * @return javax.swing.JButton
254
     */
255
    public JButton getConnectButton() {
256
        if (connectButton == null) {
257
            connectButton = new JButton();
258
            connectButton.setText(PluginServices.getText(this, "conectar"));
259
            connectButton.setBounds(new java.awt.Rectangle(383 - 17, 33 + 17,
260
                    100, 20));
261

    
262
            connectButton.addActionListener(this);
263
        }
264

    
265
        return connectButton;
266
    }
267

    
268
    /**
269
     * Inicializes the servicesTableScrollPane
270
     *
271
     * @return javax.swing.JScrollPane
272
     */
273
    private JScrollPane getServicesTableScrollPane() {
274
        if (servicesTableScrollPane == null) {
275
            servicesTableScrollPane = new JScrollPane();
276
            servicesTableScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder(
277
                    null, PluginServices.getText(this, "available_services"),
278
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
279
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
280

    
281
            servicesTableScrollPane.setBounds(new java.awt.Rectangle(17, 152,
282
                    477, 261 + 12)); // hasta y = 413
283
        }
284

    
285
        return servicesTableScrollPane;
286
    }
287

    
288
    /**
289
     * Inicializes the nextButton, which will enable and open a new panel
290
     * depending on the user's selection
291
     *
292
     * @return javax.swing.JButton
293
     */
294
    public JButton getNextButton() {
295
        if (nextButton == null) {
296
            nextButton = new JButton(); // 
297
            nextButton.setText(PluginServices.getText(this, "siguiente"));
298
            nextButton.setBounds(new java.awt.Rectangle(395, 444, 100, 20));
299
            nextButton.addActionListener(this);
300
        }
301

    
302
        return nextButton;
303
    }
304

    
305
    public JButton getPreviousButton() {
306
        if (previousButton == null) {
307
            previousButton = new JButton(); // 
308
            previousButton.setText(PluginServices.getText(this, "anterior"));
309
            previousButton.setBounds(new java.awt.Rectangle(395 - 103, 444,
310
                    100, 20));
311
            previousButton.setEnabled(false);
312
            previousButton.addActionListener(this);
313
        }
314

    
315
        return previousButton;
316
    }
317

    
318
    public void setServerComboText(String s) {
319
        this.serverComboBox.addItem(s);
320
        this.serverComboBox.setSelectedItem(s);
321
    }
322

    
323
    /**
324
     * Writes the list of ArcIMS servers contained in the serverComboBox on a
325
     * XML local file. It should remove server URLs from unsuccessful
326
     * connections.
327
     *
328
     * @param v
329
     *            a vector of Strings (servers' URLs)
330
     */
331
    private void writeServerVectorToXML(Vector v) {
332
        String s = "";
333

    
334
        for (int i = 0; i < v.size(); i++)
335
            s = s + "," + ((String) v.get(i));
336

    
337
        s = s.substring(1);
338

    
339
        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
340

    
341
        if (xml == null) {
342
            xml = new XMLEntity();
343
            xml.putProperty("arcims-servers", s);
344
        }
345
        else {
346
            xml.putProperty("arcims-servers", s);
347
        }
348
    }
349

    
350
    /**
351
     * Loads a local variable (favoriteServers) with a list of servers and the
352
     * ones found in the local persistent data XML file.
353
     *
354
     */
355
    private void loadFavoriteServers() {
356
        favoriteServers.add("http://www.geographynetwork.com");
357
        favoriteServers.add("http://massotti.carm.es");
358
        // favoriteServers.add("http://cartoweb.paeria.es"); // Ayunt. de Lerida
359
        // favoriteServers.add("http://geodaten.stadt-bottrop.de"); // Ayunt. de Bottrop (Alemania)
360
        favoriteServers.add("http://comercio.ideam.gov.co");
361
        favoriteServers.add("http://atl.cenapred.unam.mx");
362
        // favoriteServers.add("http://gdz1.leipzig.ifag.de");
363

    
364
        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
365

    
366
        if (xml == null) {
367
            return;
368
        }
369

    
370
        try {
371
            String[] s = xml.getStringArrayProperty("arcims-servers");
372
            String[] servers = s[0].split(",");
373

    
374
            for (int i = 0; i < servers.length; i++)
375
                addIfNotRepeated(favoriteServers, servers[i]);
376
        }
377
        catch (NotExistInXMLEntity e) {
378
            logger.warn("Property 'arcims-servers' not found in XML file. ");
379
        }
380
    }
381

    
382
    private void addIfNotRepeated(Vector v, String s) {
383
        if (v.contains(s)) {
384
            return;
385
        }
386

    
387
        v.add(0, s);
388
    }
389

    
390
    private void removeServerFromVector(Vector v, String s) {
391
        if (!(v.contains(s))) {
392
            return;
393
        }
394

    
395
        v.remove(s);
396
    }
397

    
398
    /**
399
     * Utility method to load the server names from a 'favoriteServers' vector
400
     * into the server names combobox.
401
     */
402
    private void addServersFromVector() {
403
        for (int i = 0; i < favoriteServers.size(); i++)
404
            this.serverComboBox.addItem((String) favoriteServers.get(i));
405
    }
406

    
407
    /**
408
     * Listens to mouse events on the available services list. A double click
409
     * will have the same effect as a click on the nextButton.
410
     */
411
    public void mouseClicked(MouseEvent e) {
412
        if (e.getSource() == this.servicesTable) {
413
            if (!this.servicesTable.isEnabled()) {
414
                return;
415
            }
416

    
417
            int selrow = servicesTable.getSelectedRow();
418

    
419
            if (selrow == -1) {
420
                return;
421
            }
422

    
423
            String servname = ServicesTableModel.getColumnValueOfRow(servicesTable,
424
                    PluginServices.getText(this, "name"), selrow);
425
            userDefinedServiceNameTextField.setText(servname);
426

    
427
            // double click event:
428
            if (e.getClickCount() == 2) {
429
                ActionEvent artificialEvent = new ActionEvent(nextButton,
430
                        ActionEvent.ACTION_PERFORMED, "");
431
                this.actionPerformed(artificialEvent);
432
            }
433
        }
434
    }
435

    
436
    public void mouseEntered(MouseEvent e) {
437
    }
438

    
439
    public void mouseExited(MouseEvent e) {
440
    }
441

    
442
    public void mousePressed(MouseEvent e) {
443
    }
444

    
445
    public void mouseReleased(MouseEvent e) {
446
    }
447

    
448
    /**
449
     * Listens to button events: connectButton will retrieve the server's
450
     * available services list (catalog). NextButton will be enabled if a valid
451
     * service (imageservice or featureservice) has been selected by the user.
452
     */
453
    public void actionPerformed(ActionEvent e) {
454
        if (e.getSource() == connectButton) {
455
            // ---------------------- CONNECT BUTTON - START
456
            this.nextButton.setEnabled(false);
457

    
458
            boolean badURL = false;
459
            URL url = null;
460
            ServicesTableDataSource stds = null;
461
            url = null;
462

    
463
            // find out complete url (using libArcIMS):
464
            try {
465
                url = new URL(serverComboBox.getSelectedItem().toString());
466
                completeURL = ArcImsProtocolHandler.getUrlWithServlet(url);
467

    
468
                // this truly loads the service names, types and status:
469
                stds = new ServicesTableDataSource(completeURL,
470
                        cacheOverride.isSelected());
471
            }
472
            catch (MalformedURLException e1) {
473
                badURL = true;
474
            }
475
            catch (ArcImsException e1) {
476
                badURL = true;
477
            }
478

    
479
            if (badURL) {
480
                logger.info("Wrong url");
481
                JOptionPane.showMessageDialog(this,
482
                    PluginServices.getText(this, "wrong_url") + ": " +
483
                    serverComboBox.getSelectedItem().toString(),
484
                    PluginServices.getText(this, "wrong_url"),
485
                    JOptionPane.ERROR_MESSAGE);
486

    
487
                return;
488
            }
489
            else {
490
                // check that the list is non-empty:
491
                if (stds.getDataVector().size() == 0) {
492
                    JOptionPane.showMessageDialog(this,
493
                        PluginServices.getText(this, "server_has_no_services") +
494
                        ": " + url,
495
                        PluginServices.getText(this, "server_has_no_services"),
496
                        JOptionPane.ERROR_MESSAGE);
497

    
498
                    // remove useless url:
499
                    removeServerFromVector(favoriteServers, url.toString());
500
                    // writeServerVectorToXML(favoriteServers);
501

    
502
                    return;
503
                }
504
            }
505

    
506
            // enables certain controls if the url is ok and there are services:
507
            fireServerUrlOk();
508

    
509
            // save working url:
510
            favoriteServers.add(0, url.toString());
511
            writeServerVectorToXML(favoriteServers);
512

    
513
            parentWizard.setHostName(completeURL.toString());
514

    
515
            ServicesTableModel dtm = new ServicesTableModel(stds.getDataVector(),
516
                    stds.getColNamesVector());
517
            setModelAndWidths(servicesTable, dtm);
518

    
519
            // Ask to be notified of selection changes.
520
            ListSelectionModel rowSM = servicesTable.getSelectionModel();
521
            rowSM.addListSelectionListener(this.tableSelectionListener);
522

    
523
            servicesTableScrollPane.setViewportView(servicesTable);
524
            servicesTable.repaint();
525
            servicesTableScrollPane.repaint();
526

    
527
            // setEnableComponent(servicesTable, serviceNameSelectionModeListRadioButton.isSelected());
528
            // ---------------------- CONNECT BUTTON - END
529
        }
530

    
531
        // ---------------------- NEXT BUTTON - START
532
        if (e.getSource() == nextButton) {
533
            // if the 'user defined' option is set, local variables get values from
534
            // the combobox
535
            // if (serviceNameSelectionModeUserRadioButton.isSelected()) {
536
            String userServiceName = userDefinedServiceNameTextField.getText();
537
            int ind = -1;
538

    
539
            try {
540
                ind = ServicesTableModel.getFirstRowWithValueInColumnName(servicesTable,
541
                        PluginServices.getText(this, ServiceInfoTags.aNAME),
542
                        userServiceName);
543
            }
544
            catch (ArcImsException e1) {
545
                logger.error("While searching value in table ", e1);
546
                ind = -1;
547
            }
548

    
549
            if (ind == -1) {
550
                JOptionPane.showMessageDialog(this,
551
                    PluginServices.getText(this, "service_does_not_exist") +
552
                    ": " + userServiceName, "Error", JOptionPane.ERROR_MESSAGE);
553

    
554
                return;
555
            }
556

    
557
            selectedServiceType = ServicesTableModel.getColumnValueOfRowWithIndex(servicesTable,
558
                    1, ind);
559
            selectedServiceName = userServiceName;
560

    
561
            // }
562

    
563
            // check that service type is supported:
564
            if (!isSupportedServiceType(selectedServiceName)) {
565
                return;
566
            }
567

    
568
                // +++++++++++++  This line truly starts the connection:          ++++++++++++
569
                // +++++++++++++  creates driver, client and gets capabilities    ++++++++++++
570
                try {
571
                                        parentWizard.getDataSource()
572
                                                    .setHostService(completeURL, selectedServiceName,
573
                                            selectedServiceType);
574
                                } catch (DriverException de) {
575
                        logger.error("While setting host service ", de);
576
                        showErrorMessage(de.getMessage());
577
                        return;
578
                                }
579

    
580
            // get service information:
581
            ArcImsClientP cli = null;
582

    
583
            if (selectedServiceType.compareToIgnoreCase(
584
                        ServiceInfoTags.vIMAGESERVICE) == 0) {
585
                FMapRasterArcImsDriver idrv = (FMapRasterArcImsDriver) parentWizard.getDataSource()
586
                                                                                   .getDriver();
587
                cli = (ArcImsClientP) idrv.getClient();
588
            }
589

    
590
            if (selectedServiceType.compareToIgnoreCase(
591
                        ServiceInfoTags.vFEATURESERVICE) == 0) {
592
                FMapFeatureArcImsDriver fdrv = (FMapFeatureArcImsDriver) parentWizard.getDataSource()
593
                                                                                     .getDriver();
594
                cli = (ArcImsClientP) fdrv.getClient();
595
            }
596

    
597
            ServiceInformation si = cli.getServiceInformation();
598

    
599
            // 
600
            if ((si.getLayers() == null) || (si.getLayers().size() == 0)) {
601
                logger.warn("No layers found ");
602
                JOptionPane.showMessageDialog(this,
603
                    PluginServices.getText(this, "no_layers_found") + ". " +
604
                    PluginServices.getText(this, "service") + ": " +
605
                    selectedServiceName,
606
                    PluginServices.getText(this, "no_layers_found"),
607
                    JOptionPane.ERROR_MESSAGE);
608

    
609
                return;
610
            }
611

    
612
            parentWizard.setServiceName(selectedServiceName);
613
            parentWizard.setServiceType(selectedServiceType);
614
            parentWizard.fillAndMoveTabbedPaneToEnabled();
615

    
616
            // ---------------------- NEXT BUTTON - END
617
        }
618
    }
619

    
620
    public void pseudoNextFired(FLayer lyr) {
621
        // if the 'user defined' option is set, local variables get values from
622
        // the combobox
623
        // if (serviceNameSelectionModeUserRadioButton.isSelected()) {
624
        String userServiceName = userDefinedServiceNameTextField.getText();
625
        int ind = -1;
626

    
627
        try {
628
            ind = ServicesTableModel.getFirstRowWithValueInColumnName(servicesTable,
629
                    PluginServices.getText(this, ServiceInfoTags.aNAME),
630
                    userServiceName);
631
        }
632
        catch (ArcImsException e1) {
633
            logger.error("While searching value in table ", e1);
634
            ind = -1;
635
        }
636

    
637
        if (ind == -1) {
638
            JOptionPane.showMessageDialog(this,
639
                PluginServices.getText(this, "service_does_not_exist") + ": " +
640
                userServiceName, "Error", JOptionPane.ERROR_MESSAGE);
641

    
642
            return;
643
        }
644

    
645
        selectedServiceType = ServicesTableModel.getColumnValueOfRowWithIndex(servicesTable,
646
                1, ind);
647
        selectedServiceName = userServiceName;
648

    
649
        // }
650

    
651
        // check that service type is supported:
652
        if (!isSupportedServiceType(selectedServiceName)) {
653
            return;
654
        }
655

    
656
        try {
657
//             +++++++++++++  This line truly starts the connection:          ++++++++++++
658
//             +++++++++++++  creates driver, client and gets capabilities    ++++++++++++
659
            parentWizard.getDataSource()
660
                        .setHostService(completeURL, selectedServiceName,
661
                selectedServiceType, lyr);
662
        }
663
        catch (DriverException de) {
664
            logger.error("While setting host service ", de);
665
            showErrorMessage(de.getMessage());
666

    
667
            return;
668
        }
669

    
670
        // get service information:
671
        ArcImsClientP cli = null;
672

    
673
        if (selectedServiceType.compareToIgnoreCase(
674
                    ServiceInfoTags.vIMAGESERVICE) == 0) {
675
            FMapRasterArcImsDriver idrv = (FMapRasterArcImsDriver) parentWizard.getDataSource()
676
                                                                               .getDriver();
677
            cli = (ArcImsClientP) idrv.getClient();
678
        }
679

    
680
        if (selectedServiceType.compareToIgnoreCase(
681
                    ServiceInfoTags.vFEATURESERVICE) == 0) {
682
            FMapFeatureArcImsDriver fdrv = (FMapFeatureArcImsDriver) parentWizard.getDataSource()
683
                                                                                 .getDriver();
684
            cli = (ArcImsClientP) fdrv.getClient();
685
        }
686

    
687
        ServiceInformation si = cli.getServiceInformation();
688

    
689
        // 
690
        if ((si.getLayers() == null) || (si.getLayers().size() == 0)) {
691
            logger.warn("No layers found ");
692
            JOptionPane.showMessageDialog(this,
693
                PluginServices.getText(this, "no_layers_found") + ". " +
694
                PluginServices.getText(this, "service") + ": " +
695
                selectedServiceName,
696
                PluginServices.getText(this, "no_layers_found"),
697
                JOptionPane.ERROR_MESSAGE);
698

    
699
            return;
700
        }
701

    
702
        parentWizard.setServiceName(selectedServiceName);
703
        parentWizard.setServiceType(selectedServiceType);
704
        parentWizard.fillAndMoveTabbedPaneToEnabled();
705
    }
706

    
707
    private void setModelAndWidths(JTable table, ServicesTableModel m) {
708
        table.setModel(m);
709

    
710
        TableColumnModel cm = table.getColumnModel();
711
        TableColumn col;
712
        int count = cm.getColumnCount();
713

    
714
        if (count == 3) {
715
            col = cm.getColumn(0);
716
            col.setPreferredWidth(230);
717
            col = cm.getColumn(1);
718
            col.setPreferredWidth(110);
719
            col = cm.getColumn(2);
720
            col.setPreferredWidth(110);
721
            table.repaint();
722
        }
723
    }
724

    
725
    private void showErrorMessage(String message) {
726
        JOptionPane.showMessageDialog(this,
727
            PluginServices.getText(this, message),
728
            PluginServices.getText(this, "error"), JOptionPane.ERROR_MESSAGE);
729
    }
730

    
731
    private boolean servicesTableHasSelection() {
732
        return (servicesTable.getSelectedRow() != -1);
733
    }
734

    
735
    /**
736
     * Utility method to manage the services names table.
737
     *
738
     * @param sName service name
739
     * @return row that contains it
740
     */
741
    public int getRowNumberOfServiceName(String sName) {
742
        int l = servicesTable.getRowCount();
743
        String s;
744

    
745
        for (int i = 0; i < l; i++) {
746
            s = ServicesTableModel.getColumnValueOfRow(servicesTable,
747
                    PluginServices.getText(this, "name"), i);
748

    
749
            if (s.compareToIgnoreCase(sName) == 0) {
750
                return i;
751
            }
752
        }
753

    
754
        logger.error("Service name not found: " + sName);
755

    
756
        return -1;
757
    }
758

    
759
    public JTable getServicesTable() {
760
        return servicesTable;
761
    }
762

    
763
    public void setServicesTable(JTable servicesTable) {
764
        this.servicesTable = servicesTable;
765
    }
766

    
767
    public void setSelectedServiceType(String selectedServiceType) {
768
        this.selectedServiceType = selectedServiceType;
769
    }
770

    
771
    public String getSelectedServiceName() {
772
        return selectedServiceName;
773
    }
774

    
775
    public void setSelectedServiceName(String selectedServiceName) {
776
        this.selectedServiceName = selectedServiceName;
777
    }
778

    
779
    public ArcImsWizard getParentWizard() {
780
        return parentWizard;
781
    }
782

    
783
    public void setParentWizard(ArcImsWizard parentWizard) {
784
        this.parentWizard = parentWizard;
785
    }
786

    
787
    public ServicesTableSelectionListener getTableSelectionListener() {
788
        return tableSelectionListener;
789
    }
790

    
791
    public void setTableSelectionListener(
792
        ServicesTableSelectionListener tableSelectionListener) {
793
        this.tableSelectionListener = tableSelectionListener;
794
    }
795

    
796
    /**
797
     * This method initializes userDefinedServiceNamePanel
798
     *
799
     * @return javax.swing.JPanel
800
     */
801
    private JPanel getUserDefinedServiceNamePanel() {
802
        if (userDefinedServiceNamePanel == null) {
803
            userDefinedServiceNamePanel = new JPanel();
804
            userDefinedServiceNamePanel.setBounds(new java.awt.Rectangle(17,
805
                    97, 477, 56)); // hasta y = 151
806
            userDefinedServiceNamePanel.setLayout(null);
807
            userDefinedServiceNamePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
808
                    null, PluginServices.getText(this, "service_name"),
809
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
810
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
811
            userDefinedServiceNameTextField = new JTextField();
812
            userDefinedServiceNameTextField.setBounds(11, 21, 380 + 74, 20);
813
            userDefinedServiceNameTextField.addKeyListener(this);
814

    
815
            serviceNameLabel = new JLabel(PluginServices.getText(this,
816
                        "service_name"));
817
            serviceNameLabel.setBounds(70, 21, 120, 20);
818

    
819
            // serviceSelectionModeButtonGroup = new ButtonGroup();
820
            // serviceSelectionModeButtonGroup.add(getServiceNameSelectionModeRadioButton());
821
            // serviceSelectionModeButtonGroup.add(getServiceNameSelectionModeListRadioButton());
822
            userDefinedServiceNamePanel.add(userDefinedServiceNameTextField);
823

    
824
            // userDefinedServiceNamePanel.add(serviceNameLabel);
825
        }
826

    
827
        return userDefinedServiceNamePanel;
828
    }
829

    
830
    /**
831
     * This method initializes serviceNameSelectionModeRadioButton
832
     *
833
     * @return javax.swing.JRadioButton
834
     */
835

    
836
    /*
837
    public JRadioButton getServiceNameSelectionModeRadioButton() {
838
            if (serviceNameSelectionModeUserRadioButton == null) {
839
                    serviceNameSelectionModeUserRadioButton = new JRadioButton();
840
                    serviceNameSelectionModeUserRadioButton.addActionListener(this);
841
                    serviceNameSelectionModeUserRadioButton
842
                                    .setBounds(new java.awt.Rectangle(170, 20, 160, 20));
843
                    serviceNameSelectionModeUserRadioButton.setText(PluginServices
844
                                    .getText(this, "defined_by_user") + ":");
845
            }
846
            return serviceNameSelectionModeUserRadioButton;
847
    }
848
    */
849

    
850
    /**
851
     * This method initializes serviceNameSelectionModeListRadioButton
852
     *
853
     * @return javax.swing.JRadioButton
854
     */
855

    
856
    /*
857
    private JRadioButton getServiceNameSelectionModeListRadioButton() {
858
            if (serviceNameSelectionModeListRadioButton == null) {
859
                    serviceNameSelectionModeListRadioButton = new JRadioButton();
860
                    serviceNameSelectionModeListRadioButton.addActionListener(this);
861
                    serviceNameSelectionModeListRadioButton
862
                                    .setBounds(new java.awt.Rectangle(10, 20, 160, 20));
863
                    serviceNameSelectionModeListRadioButton.setText(PluginServices
864
                                    .getText(this, "selected_from_list"));
865
    
866
            }
867
            return serviceNameSelectionModeListRadioButton;
868
    }
869
    */
870
    private void setEnableComponent(JComponent c, boolean v) {
871
        if (c == null) {
872
            return;
873
        }
874

    
875
        c.setEnabled(v);
876

    
877
        if (c instanceof JTable) {
878
            if (v) {
879
                c.setForeground(Color.BLACK);
880
            }
881
            else {
882
                c.setForeground(Color.LIGHT_GRAY);
883
            }
884
        }
885
    }
886

    
887
    public void keyPressed(KeyEvent e) {
888
    }
889

    
890
    public void keyTyped(KeyEvent e) {
891
    }
892

    
893
    public void keyReleased(KeyEvent e) {
894
        if (e.getSource() == userDefinedServiceNameTextField) {
895
            boolean user = true;
896
            int length = userDefinedServiceNameTextField.getText().length();
897
            setServiceType("");
898
            nextButton.setEnabled(user && (length > 0));
899
        }
900

    
901
        if (e.getSource() == serverComboBox.getEditor().getEditorComponent()) {
902
            fireServerUrlUnknown();
903
        }
904
    }
905

    
906
    private void fireServerUrlUnknown() {
907
        // setEnableComponent(serviceNameSelectionModeListRadioButton, false);
908
        // setEnableComponent(serviceNameSelectionModeUserRadioButton, false);
909
        setEnableComponent(servicesTable, false);
910
        setEnableComponent(nextButton, false);
911
        setEnableComponent(userDefinedServiceNameTextField, false);
912

    
913
        if (versionLabel != null) {
914
            versionLabel.setText("-");
915
        }
916
    }
917

    
918
    private void fireServerUrlOk() {
919
        // setEnableComponent(serviceNameSelectionModeListRadioButton, true);
920
        // setEnableComponent(serviceNameSelectionModeUserRadioButton, true);
921
        setEnableComponent(servicesTable, true);
922
        setEnableComponent(nextButton, true);
923

    
924
        String str = "-";
925

    
926
        if (versionLabel != null) {
927
            str = getServerVersion(completeURL);
928
            versionLabel.setText(str);
929
            parentWizard.setServerVersionInPanels(str);
930
        }
931

    
932
        // serviceNameSelectionModeListRadioButton.setSelected(true);
933
        // ActionEvent ae = new ActionEvent(serviceNameSelectionModeListRadioButton, ActionEvent.ACTION_PERFORMED, "");
934
        // actionPerformed(ae);
935
        setUserDecisionTrue();
936
    }
937

    
938
    /**
939
     * This method calls the liArcIMS library to quickly find out
940
     * the server full url (with '/servlet/...')
941
     *
942
     * @param url server short url
943
     * @return full url
944
     */
945
    private String getServerVersion(URL url) {
946
        String[] vers;
947

    
948
        try {
949
            vers = ArcImsProtocolHandler.getVersion(url);
950
        }
951
        catch (ArcImsException e) {
952
            logger.error("Server version not found ", e);
953
            JOptionPane.showMessageDialog(this,
954
                PluginServices.getText(this, "unable_to_findout_server_version"),
955
                PluginServices.getText(this, "unable_to_findout_server_version"),
956
                JOptionPane.ERROR_MESSAGE);
957

    
958
            return "-";
959
        }
960

    
961
        return vers[0] + " (" + vers[1] + ")";
962
    }
963

    
964
    public String getServiceType() {
965
        return serviceType;
966
    }
967

    
968
    public void setServiceType(String serviceType) {
969
        this.serviceType = serviceType;
970
    }
971

    
972
    public void setUserServiceName(String str) {
973
        userDefinedServiceNameTextField.setText(str);
974
    }
975

    
976
    public void itemStateChanged(ItemEvent e) {
977
        this.fireServerUrlUnknown();
978
    }
979

    
980
    /**
981
     *
982
     * Utility method to decide if a service type is supported
983
     * or not (ServiceInfoTags.IMAGESERVICE, etc)
984
     */
985
    private boolean isSupportedServiceType(String svcName) {
986
        int ind = -1;
987

    
988
        try {
989
            ind = ServicesTableModel.getFirstRowWithValueInColumnIndex(servicesTable,
990
                    0, svcName);
991
        }
992
        catch (ArcImsException e1) {
993
            logger.error("While searching value in table ", e1);
994
            ind = -1;
995
        }
996

    
997
        if (ind == -1) {
998
            // should never happen
999
            JOptionPane.showMessageDialog(this,
1000
                PluginServices.getText(this, "service_does_not_exist") + ": " +
1001
                svcName, "Error", JOptionPane.ERROR_MESSAGE);
1002

    
1003
            return false;
1004
        }
1005

    
1006
        String svcType = ServicesTableModel.getColumnValueOfRowWithIndex(servicesTable,
1007
                1, ind);
1008

    
1009
        if ((svcType.compareToIgnoreCase(ServiceInfoTags.vIMAGESERVICE) != 0) &&
1010
                (svcType.compareToIgnoreCase(ServiceInfoTags.vFEATURESERVICE) != 0)) {
1011
            showNotImplementedMessage();
1012

    
1013
            return false;
1014
        }
1015

    
1016
        return true;
1017
    }
1018

    
1019
    private void showNotImplementedMessage() {
1020
        JOptionPane.showMessageDialog(this,
1021
            PluginServices.getText(this, "service_type_not_supported"),
1022
            PluginServices.getText(this, "error"), JOptionPane.ERROR_MESSAGE);
1023
    }
1024

    
1025
    public void setUserDecisionTrue() {
1026
        setEnableComponent(userDefinedServiceNameTextField, true);
1027
        setEnableComponent(servicesTable, true);
1028

    
1029
        int length = userDefinedServiceNameTextField.getText().length();
1030
        setEnableComponent(nextButton, (length > 0));
1031
    }
1032
} // @jve:decl-index=0:visual-constraint="10,8"