Revision 26503

View differences:

tmp/trunk/cliente/WorkSpace_WPSClient/extWPSCallejero/src/es/logex/gvsig/streetsmap/ui/PanelConnectionWPS.java
1
package es.logex.gvsig.streetsmap.ui;
2

  
3
import java.awt.Dimension;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.Insets;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11
import java.io.IOException;
12
import java.io.ObjectOutputStream;
13
import java.io.ObjectStreamClass;
14
import java.io.Serializable;
15
import java.net.MalformedURLException;
16
import java.net.URL;
17
import java.util.ArrayList;
18
import java.util.List;
19

  
20
import javax.naming.event.EventContext;
21
import javax.swing.BorderFactory;
22
import javax.swing.ComboBoxModel;
23
import javax.swing.DefaultComboBoxModel;
24
import javax.swing.ImageIcon;
25
import javax.swing.JButton;
26
import javax.swing.JComboBox;
27
import javax.swing.JOptionPane;
28

  
29
import javax.swing.WindowConstants;
30
import javax.swing.border.LineBorder;
31
import javax.swing.border.TitledBorder;
32
import javax.swing.JFrame;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35
import javax.swing.JTextArea;
36
import javax.swing.SwingConstants;
37

  
38
import es.logex.gvsig.streetsmap.ui.events.ConnectionSucceedListener;
39
import es.logex.gvsig.streetsmap.ui.events.ConnectionSucceedProducer;
40
import es.logex.gvsig.streetsmap.ui.events.ConnectionSucceedEvent;
41
import es.logex.gvsig.wps.client.WPSClient;
42

  
43
/**
44
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
45
 * Builder, which is free for non-commercial use. If Jigloo is being used
46
 * commercially (ie, by a corporation, company or business for any purpose
47
 * whatever) then you should purchase a license for each developer using Jigloo.
48
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
49
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
50
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
51
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
52
 */
53
public class PanelConnectionWPS extends javax.swing.JPanel implements ConnectionSucceedProducer {
54
	/**
55
	 * 
56
	 */
57
	private static final long serialVersionUID = -7765843934771343314L;
58
	private JPanel panelConnectionCombo;
59
	private JPanel panelServerAbstract;
60
	private JButton btnConnect;
61
	private JComboBox cmbHost;
62
	private JPanel panelHeader;
63
	private JTextArea txtAbstract;
64
	private JLabel lblIconSearch;
65
	private JLabel lblBuscar;
66
	private JPanel jPanel2;
67
	private List<ConnectionSucceedListener> connectRequestsListeners = new ArrayList<ConnectionSucceedListener>();
68
	/**
69
	 * Auto-generated main method to display this JPanel inside a new JFrame.
70
	 */
71
	public static void main(String[] args) {
72
		JFrame frame = new JFrame();
73
		frame.getContentPane().add(new PanelConnectionWPS());
74
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
75
		frame.pack();
76
		frame.setVisible(true);
77
	}
78
	
79
	public PanelConnectionWPS() {
80
		super();
81
		initGUI();
82
	}
83
	
84
	private void initGUI() {
85
		try {
86
			GridBagLayout thisLayout = new GridBagLayout();
87
			setPreferredSize(new Dimension(400, 300));
88
			thisLayout.rowWeights = new double[] {0.0, 0.1, 0.1, 0.8, 0.0};
89
			thisLayout.rowHeights = new int[] {7, 7, 7, 7, 7};
90
			thisLayout.columnWeights = new double[] {0.0, 0.1, 0.0};
91
			thisLayout.columnWidths = new int[] {7, 7, 7};
92
			this.setLayout(thisLayout);
93
			{
94
				panelConnectionCombo = new JPanel();
95
				GridBagLayout panelConnectionComboLayout = new GridBagLayout();
96
				panelConnectionComboLayout.columnWidths = new int[] {7, 7};
97
				panelConnectionComboLayout.rowHeights = new int[] {7};
98
				panelConnectionComboLayout.columnWeights = new double[] {0.9, 0.1};
99
				panelConnectionComboLayout.rowWeights = new double[] {0.1};
100
				this.add(panelConnectionCombo, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
101
				panelConnectionCombo.setLayout(panelConnectionComboLayout);
102
				{
103
					btnConnect = new JButton();
104
					panelConnectionCombo.add(btnConnect, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
105
					btnConnect.setText("...");
106
					btnConnect.addActionListener(new ActionListener() {
107
						public void actionPerformed(ActionEvent evt) {
108
							btnConnectActionPerformed(evt);
109
						}
110
					});
111
				}
112
				{
113
					ComboBoxModel cmbHostModel = 
114
						new DefaultComboBoxModel(
115
								new String[] { "http://logexvirt2:9080/wps_ws_spring/StreetsMap/service.wps" });
116
					cmbHost = new JComboBox();
117
					panelConnectionCombo.add(cmbHost, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
118
					cmbHost.setModel(cmbHostModel);
119
					cmbHost.setEditable(true);
120
				}
121
			}
122
			{
123
				panelServerAbstract = new JPanel();
124
				GridBagLayout panelServerAbstractLayout = new GridBagLayout();
125
				panelServerAbstractLayout.columnWidths = new int[] {7};
126
				panelServerAbstractLayout.rowHeights = new int[] {7};
127
				panelServerAbstractLayout.columnWeights = new double[] {0.1};
128
				panelServerAbstractLayout.rowWeights = new double[] {0.1};
129
				this.add(panelServerAbstract, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
130
				panelServerAbstract.setLayout(panelServerAbstractLayout);
131
				{
132
					txtAbstract = new JTextArea();
133
					panelServerAbstract.add(txtAbstract, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
134
					txtAbstract.setBorder(BorderFactory.createTitledBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false), "Información servidor", TitledBorder.LEADING, TitledBorder.TOP));
135
					txtAbstract.setEditable(false);
136
				}
137
			}
138
			{
139
				panelHeader = new JPanel();
140
				this.add(panelHeader, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
141
				GridBagLayout jPanel1Layout = new GridBagLayout();
142
				jPanel1Layout.columnWidths = new int[] {7, 20};
143
				jPanel1Layout.rowHeights = new int[] {7};
144
				jPanel1Layout.columnWeights = new double[] {0.1, 0.1};
145
				jPanel1Layout.rowWeights = new double[] {0.1};
146
				panelHeader.setLayout(jPanel1Layout);
147
				panelHeader.setName("panelHeader");
148
				{
149
					jPanel2 = new JPanel();
150
					panelHeader.add(jPanel2, new GridBagConstraints(-1, -2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
151
					{
152
						lblBuscar = new JLabel();
153
						jPanel2.add(lblBuscar);
154
						lblBuscar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("es/logex/gvsig/streetsmap/ui/callejero50.gif")));
155
						lblBuscar.setText("Callejeros WPS - Conexión");
156
						lblBuscar.setHorizontalAlignment(SwingConstants.LEFT);
157
						lblBuscar.setBackground(new java.awt.Color(255,165,0));
158
						lblBuscar.setFont(new java.awt.Font("Dialog",1,16));
159
						lblBuscar.setPreferredSize(new java.awt.Dimension(294,43));
160
						lblBuscar.setOpaque(false);
161
					}
162
				}
163
				{
164
					lblIconSearch = new JLabel();
165
					panelHeader.add(lblIconSearch, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
166
					lblIconSearch.setIcon(new ImageIcon(getClass().getClassLoader().getResource("es/logex/gvsig/streetsmap/ui/search.gif")));
167
				}
168
			}
169
		} catch (Exception e) {
170
			e.printStackTrace();
171
		}
172
	}
173
	
174
	private void btnConnectActionPerformed(ActionEvent evt) {
175
		System.out.println("btnConnect.actionPerformed, event="+evt);
176
		try {
177
			URL url = new URL((String)cmbHost.getModel().getSelectedItem());
178
			fireConnectRequest(url);
179
		} catch (MalformedURLException e) {
180
			JOptionPane.showMessageDialog(this, "Direccion_URL_Incorrecta", "Titulo_direccion_url_incorrecta", JOptionPane.CANCEL_OPTION);
181
		}
182
	}
183

  
184
	public void addConnectRequestListener(
185
			ConnectionSucceedListener connectRequestListener) {
186
		connectRequestsListeners.add(connectRequestListener);
187
		
188
	}
189
	public void removeConnectRequestListener(
190
			ConnectionSucceedListener connectRequestListener) {
191
		connectRequestsListeners.remove(connectRequestListener);
192
		
193
	}
194
	
195
	private void fireConnectRequest(URL url)
196
	{
197
		WPSClient wpsClient = new WPSClient(url.toString());
198
		try {
199
			if(wpsClient.connect())
200
			{
201
				this.txtAbstract.setText(wpsClient.getServiceAbstract());
202

  
203
				// Avisar a los clientes de que ha habido una conexion
204
				ConnectionSucceedEvent connRequest = new ConnectionSucceedEvent(this,url,wpsClient);
205
				for(ConnectionSucceedListener connListener : connectRequestsListeners)
206
				{
207
					connListener.connectionSucceed(connRequest);
208
				}
209
				
210
				// Si la url no estaba en la lista, la añadimos
211
				DefaultComboBoxModel cmbModel = (DefaultComboBoxModel)cmbHost.getModel();
212
				boolean foundInList = false;
213
				for(int index=0; index < cmbModel.getSize(); index++)
214
				{
215
					if(cmbModel.getElementAt(index).equals(url.toString()))
216
					{
217
						foundInList = true;
218
						break;
219
					}
220
				}
221
				if(!foundInList)
222
				{
223
					cmbModel.addElement(url.toString());	
224
					// TODO recuperar la lista persistida
225
					FileOutputStream fos = null;
226
					try {
227
						// Persistimos la lista... Habra que hacerlo en un sitio adecuado
228
						// No se emplea la estrategia del jComboServer por falta de documentacion
229
						fos = new FileOutputStream("/tmp/datospersistidos.bin");
230
	
231
						ObjectOutputStream oos;
232

  
233
						oos = new ObjectOutputStream(fos);
234
						
235
						oos.writeObject(cmbModel);
236
						oos.flush();
237
						oos.close();
238
						fos.flush();
239
						fos.close();
240
					} catch (IOException e) {
241
						// TODO Auto-generated catch block
242
						e.printStackTrace();
243
					}
244

  
245
						
246
				}
247
					
248
				
249
			}
250
			else
251
			{
252
				// No se ha podido abrir la conexion, recuperar los errores si los hubiese
253
				// Y mostrarlos
254
				String errorString = "";
255
				String error = "";
256
				
257
				while( wpsClient.getErrorsStack().isEmpty()== false)
258
				{
259
					error = wpsClient.getErrorsStack().pop();
260
					errorString = errorString.concat(error).concat(System.getProperty("line.separator"));
261
				}
262

  
263
				this.txtAbstract.setText(errorString);
264
			}
265
		} catch (RuntimeException e) {
266
			e.printStackTrace();
267
		}
268
	}
269
	
270
	public void setAbstract(String abstractString)
271
	{
272
		this.txtAbstract.setText(abstractString);
273
	}
274
	
275
	
276
}

Also available in: Unified diff