Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / ui / ServerConnectDialogPanel.java @ 2658

History | View | Annotate | Download (6.48 KB)

1
/*
2
 * Created on 04-may-2005
3
 */
4
package es.gva.cit.catalogClient.ui;
5

    
6
import java.awt.Dimension;
7
import java.awt.FlowLayout;
8
import java.awt.event.ActionEvent;
9
import java.awt.event.ActionListener;
10
import java.io.BufferedReader;
11
import java.io.File;
12
import java.io.FileNotFoundException;
13
import java.io.FileReader;
14
import java.io.IOException;
15
import java.util.Iterator;
16
import java.util.TreeMap;
17

    
18
import javax.swing.BoxLayout;
19
import javax.swing.JButton;
20
import javax.swing.JPanel;
21

    
22
import org.w3c.dom.Node;
23

    
24
import es.gva.cit.catalogClient.CatalogClient;
25

    
26
/**
27
 * @author jpiera
28
 */
29
public class ServerConnectDialogPanel extends JPanel
30
implements ActionListener {
31
        //Panels
32
        JPanel ppalPanel = null;
33
        ServerConnectPanel controlsPanel = null;
34
        JPanel buttonsPanel = null;
35
        //Buttons
36
        JButton conectar = null; 
37
        JButton buscar = null;
38
        //Others
39
        CatalogClient cliente = null;
40
        private String serversFile = "servers.txt";
41
        private static TreeMap serverList = new TreeMap();
42
        private String currentServer = "";
43
        
44
        public ServerConnectDialogPanel(){
45
                
46
                ppalPanel = new JPanel();
47
                ppalPanel.setLayout(new BoxLayout(ppalPanel, BoxLayout.Y_AXIS));        
48
                                
49
                ppalPanel.add(getControlsPanel(),null);
50
                ppalPanel.add(getButtonPanel(),null);                                
51

    
52
                add(ppalPanel);
53
                
54
                setDefaultButtonListeners();
55
                                
56
                //Loads the servers
57
                loadServerList(serversFile);
58
                
59
        }
60
        
61
        public JPanel getControlsPanel() {
62
                if (controlsPanel == null) {
63
                        controlsPanel = new ServerConnectPanel();
64
                        controlsPanel.setSize(625, 230);
65
                        controlsPanel.getZ3950Button().addActionListener(this);
66
                        controlsPanel.getSrwButton().addActionListener(this);
67
                        controlsPanel.getCswButton().addActionListener(this);
68
                        controlsPanel.getServidoresCombo().addActionListener(this);
69
                }
70
                return controlsPanel;
71
        }
72
        
73
        public JPanel getButtonPanel() {
74
                if (buttonsPanel == null) {
75
                        buttonsPanel = new JPanel(new FlowLayout());
76
                        buttonsPanel.add(getConnectButton());
77
                        buttonsPanel.add(getSearchButton());                        
78
                }
79
                
80
                return buttonsPanel;
81
        }
82
        
83
        public JButton getConnectButton() {
84
                if (conectar == null) {
85
                        conectar = new JButton("Conectar");
86
                        conectar.setSize(new Dimension(30,20));
87
                        conectar.setActionCommand("Connect");
88
                }
89
                return conectar;
90
        }
91
        public JButton getSearchButton() {
92
                if (buscar == null) {
93
                        buscar = new JButton("Buscar");
94
                        buscar.setSize(new Dimension(30,20));
95
                        buscar.setActionCommand("Search");
96
                        buscar.setEnabled(false);
97
                }
98
                return buscar;
99
        }
100
        
101
        public static void addServer(String name) {
102
                if (ServerConnectDialogPanel.serverList == null)
103
                        ServerConnectDialogPanel.serverList = new TreeMap();
104
                if (!ServerConnectDialogPanel.serverList.containsKey(name)) {
105
                        ServerConnectDialogPanel.serverList.put(name, name);
106
                }
107
        }
108
        
109
        public void setDefaultButtonListeners() {
110
                getConnectButton().addActionListener(this);
111
                getSearchButton().addActionListener(this);
112
        }
113
        
114
        public void loadServerList(String sfile){
115
                File file = null;
116
                try {
117
                        file = new File (sfile);
118
                        // Cargo el fichero si existe
119
                        if (file.exists()) {
120
                                BufferedReader fr = new BufferedReader(
121
                                                new FileReader(file));
122
                                String s;
123
                                while ((s = fr.readLine())!= null) {
124
                                        ServerConnectDialogPanel.addServer(s);
125
                                }                        
126
                        } else {
127
                                System.out.println("No se encuentra el fichero '"+ file.getPath()+ "'");
128
                        }
129
                        // Si hay servers en el TreeMap los cargo en el Combo
130
                        Iterator iter = ServerConnectDialogPanel.serverList.keySet().iterator();
131
                        while (iter.hasNext()) {
132
                                controlsPanel.getServerCombo().addItem(
133
                                        (String) iter.next());        
134
                        }
135

    
136
                } catch (FileNotFoundException e) {
137
                        // TODO Auto-generated catch block
138
                        System.out.println("No se encuentra el fichero '"+file.getPath()+"'");
139
                        e.printStackTrace();
140
                } catch (IOException e) {
141
                        // TODO Auto-generated catch block
142
                        e.printStackTrace();
143
                }                
144
        }
145
        /* (non-Javadoc)
146
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
147
         */
148
        public void actionPerformed(ActionEvent e) {
149
                //Connect
150
                if (e.getActionCommand() == "Connect"){
151
                        doConectar();
152
                }
153
                //BUSCAR
154
                if (e.getActionCommand() == "Search"){
155
                        this.setEnabled(false);
156
                        SearchDialog frame = new SearchDialog(cliente);
157
                }
158
                
159
                if ((e.getActionCommand() == "Z39.50") ||
160
                                (e.getActionCommand() == "SRU/SRW") ||
161
                                (e.getActionCommand() == "CS-W") ||
162
                                (e.getActionCommand() == "comboBoxChanged"))
163
                        buscar.setEnabled(false);        
164
                
165
                if (e.getActionCommand() == "Z39.50")
166
                        controlsPanel.getDbText().setEnabled(true);
167
                
168
                if ((e.getActionCommand() == "SRU/SRW") ||
169
                                (e.getActionCommand() == "CS-W"))
170
                        controlsPanel.getDbText().setEnabled(false);
171
                
172
        }
173
        
174
        private void doConectar() {
175
                buscar.setEnabled(false);        
176
                
177
                //Create a new atalogClient
178
                cliente = new CatalogClient(controlsPanel.getServer(),
179
                                controlsPanel.getProtocol(),
180
                                controlsPanel.getDatabase());
181
                
182
                String msg = "";
183
                //try to connect
184
                if (!cliente.serverReady())
185
                        msg = "No se encuentra el servidor";
186
                else if (!cliente.getLnkICatalogServerDriver().isProtocolSupported(cliente.getUrl()))
187
                        msg = "El servidor No soporta el protocolo especificado";
188
                else {
189
                        //getCapabilities
190
                        Node[] nodesCapabilities =
191
                                cliente.getLnkICatalogServerDriver().getCapabilities(cliente.getUrl());
192
                        if (nodesCapabilities == null)
193
                                msg = "Error al hacer un GetCapabilities."+
194
                                "Esto puede ser debido a dos razones: "+
195
                                "O bien ha habido un error al intentar "+
196
                                "hacer la operaci?n, o bien el servidor "+
197
                                "no soporta el protocolo especificado";
198
                        else {
199
                                //Configure the client
200
                                if (!cliente.getLnkICatalogServerDriver().setParameters(nodesCapabilities))
201
                                        if (!(cliente.getLnkICatalogServerDriver().getServerAnswerReady().equals("")))
202
                                                msg = cliente.getLnkICatalogServerDriver().getServerAnswerReady();
203
                                        else
204
                                                msg = "Error al procesar la respuesta. "+
205
                                                "Se ha encontrado el servidor, pero posiblemente"+
206
                                                " no soporta el protocolo especificado";
207
                                else{
208
                                
209
                                        //Show the answer
210
                                        msg = cliente.getLnkICatalogServerDriver().getServerAnswerReady();
211
                                        
212
                                        buscar.setEnabled(true);
213
                                        currentServer = controlsPanel.getServer();
214
                                }                                        
215
                        }
216
                }
217
                controlsPanel.setRespuesta(msg);
218
        }
219
        
220
        /**
221
         * @return Returns the serversFile.
222
         */
223
        public String getServersFile() {
224
                return serversFile;
225
        }
226
        /**
227
         * @param serversFile The serversFile to set.
228
         */
229
        public void setServersFile(String serversFile) {
230
                this.serversFile = serversFile;
231
        }
232
        /**
233
         * @return Returns the currentServer.
234
         */
235
        public String getCurrentServer() {
236
                return currentServer;
237
        }
238
        /**
239
         * @return Returns the cliente.
240
         */
241
        public CatalogClient getCliente() {
242
                return cliente;
243
        }
244
}