Revision 9197 trunk/libraries/libJCRS/src/org/gvsig/crs/gui/panels/ESRIpanel.java

View differences:

ESRIpanel.java
39 39
 */
40 40

  
41 41
package org.gvsig.crs.gui.panels;
42
import java.awt.Color;
42 43
import java.awt.Dimension;
43 44
import java.awt.FlowLayout;
44 45
import java.awt.GridLayout;
45 46
import java.awt.event.ActionEvent;
46 47
import java.awt.event.ActionListener;
48
import java.awt.event.KeyEvent;
49
import java.awt.event.KeyListener;
47 50
import java.io.File;
51
import java.sql.ResultSet;
52
import java.sql.SQLException;
48 53

  
54
import javax.swing.BorderFactory;
49 55
import javax.swing.JButton;
50 56
import javax.swing.JFileChooser;
51 57
import javax.swing.JLabel;
58
import javax.swing.JOptionPane;
52 59
import javax.swing.JPanel;
60
import javax.swing.JRadioButton;
61
import javax.swing.JScrollPane;
62
import javax.swing.JTable;
53 63
import javax.swing.JTextField;
64
import javax.swing.ListSelectionModel;
54 65
import javax.swing.filechooser.FileFilter;
66
import javax.swing.table.DefaultTableModel;
67
import javax.swing.table.TableColumn;
55 68

  
69
import org.cresques.cts.IProjection;
70
import org.gvsig.crs.CrsException;
71
import org.gvsig.crs.CrsFactory;
72
import org.gvsig.crs.EpsgConnection;
73
import org.gvsig.crs.ICrs;
74
import org.gvsig.crs.Query;
75
import org.gvsig.crs.ogr.Iau2wkt;
76

  
56 77
import com.iver.andami.PluginServices;
78
import com.iver.cit.gvsig.gui.TableSorter;
57 79

  
58 80

  
59 81
/**
......
62 84
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
63 85
 *
64 86
 */
65
public class ESRIpanel extends JPanel implements ActionListener {
87
public class ESRIpanel extends JPanel implements ActionListener, KeyListener {
66 88
	
67 89
	/**
68 90
	 * 
69 91
	 */
70
	private static final long serialVersionUID = 1L;
71
	private JLabel open_esri;
72
	private JTextField jTextOpen;
73
	private JFileChooser openChooser;
74
	private JButton jButtonOpen;
75
	File f = new File("C:/Archivos de programa/FWTools1.0.0a7/data/Geographic Coordinate Systems");
92
	private static final long serialVersionUID = 1L;	
93
	
76 94

  
95
	private JRadioButton codeRadioButton = null;
96
	private JRadioButton nameRadioButton = null;	
97
	private JPanel groupRadioButton = null;
98
	public TableSorter sorter = null;
99
	public JTable jTable = null;
100
	private JScrollPane jScrollPane = null;
101
	private JButton searchButton = null;
102
	private JTextField searchTextField = null;
103
	public DefaultTableModel dtm = null;
104
	private int codeCRS = -1;
105
	public String key;
106
	String cadWKT;
107
	public int selectedRowTable = -1;	
108
	
109
	public EpsgConnection connect = null;
110
	
77 111
	public ESRIpanel() {
78 112
		initialize();		
79 113
	}
80 114
	
81 115
	private void initialize(){
82
		this.setPreferredSize(new Dimension(525,400));
83
		this.setLayout(new GridLayout(2,2));
84
		this.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));			
85
		this.add(getOpenESRI(), null);
86
		this.add(getJTextOpen(), null);
87
		this.add(getButtonOpen(), null);
116
		this.setLayout(new GridLayout(2,3));
117
		this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
118
		this.add(getGroupRadioButton(), null);
119
		this.add(getSearchButton(), null);
120
		this.add(getSearchTextField(), null);		
121
		this.add(getJScrollPane(), null);	
88 122
	}
89

  
90
	private JLabel getOpenESRI() {
91
		if(open_esri == null) {
92
			open_esri = new JLabel();
93
			open_esri.setPreferredSize(new Dimension(80,20));
94
			open_esri.setText(PluginServices.getText(this,"abrir") + " .prj:");
95
		}
96
		return open_esri;
123
	
124
	public void connection(){
125
		connect = new EpsgConnection();
126
		connect.setConnectionEsri();				
97 127
	}
98 128
	
99
	public JTextField getJTextOpen() {
100
		if(jTextOpen == null) {
101
			jTextOpen = new JTextField();
102
			jTextOpen.setPreferredSize(new Dimension(200,20));
103
			jTextOpen.setText("");
129
	private JRadioButton getCodeRadioButton() {
130
		if (codeRadioButton == null) {
131
			codeRadioButton = new JRadioButton();
132
			codeRadioButton.setText(PluginServices.getText(this,"por_codigo"));//setText("By Code EPSG");
133
			codeRadioButton.setSelected(true);
134
			codeRadioButton.addActionListener(this);
104 135
		}
105
		return jTextOpen;
136
		return codeRadioButton;
106 137
	}
138
		  
139
	private JRadioButton getNameRadioButton() {
140
		if (nameRadioButton == null) {
141
			nameRadioButton = new JRadioButton();
142
			nameRadioButton.setText(PluginServices.getText(this,"por_nombre"));
143
			nameRadioButton.addActionListener(this);
144
		}
145
		return nameRadioButton;
146
	}
147
		 
148
	private JPanel getGroupRadioButton() {
149
		if (groupRadioButton == null) {
150
			groupRadioButton = new JPanel();
151
			groupRadioButton.setLayout(new GridLayout(1,0));
152
			groupRadioButton.setPreferredSize(new Dimension(500,30));
153
			groupRadioButton.add(getLabel());
154
			groupRadioButton.add(getCodeRadioButton());
155
			groupRadioButton.add(getNameRadioButton());			
156
		}
157
		return groupRadioButton;
158
	}
107 159
	
108
	private JFileChooser getOpen(){
109
		if(openChooser == null){
110
			openChooser = new JFileChooser();
111
			openChooser.addChoosableFileFilter(new FiltroESRI());
112
			openChooser.setEnabled(false);
160
	private JLabel getLabel(){
161
		JLabel criterio = new JLabel();
162
		criterio.setPreferredSize(new Dimension(100, 20));
163
		criterio.setText(PluginServices.getText(this, "criterio_busqueda"));
164
		return criterio;
165
	}
166
	
167
	private JButton getSearchButton() {
168
		if (searchButton == null) {
169
			searchButton = new JButton();
170
			searchButton.setPreferredSize(new Dimension(75,20));
171
			searchButton.setText(PluginServices.getText(this,"buscar"));
172
			searchButton.setMnemonic('S');			
173
			searchButton.addActionListener(this);			
113 174
		}
114
		return openChooser;
175
		return searchButton;
176
	}	
177
	
178
	private JTextField getSearchTextField() {
179
		if (searchTextField == null) {
180
			searchTextField = new JTextField();
181
			searchTextField.setPreferredSize(new Dimension(300,20));
182
			searchTextField.addKeyListener(this);
183
		}		
184
		return searchTextField;
115 185
	}
116 186
	
117
	private JButton getButtonOpen() {
118
		if(jButtonOpen == null) {
119
			jButtonOpen = new JButton();
120
			jButtonOpen.setText("");
121
			jButtonOpen.setPreferredSize(new Dimension(20,20));
122
			jButtonOpen.addActionListener(this);
187
	public JTable getJTable() {
188
		if (jTable == null) {
189
			String[] columnNames= {PluginServices.getText(this,"codigo"),
190
					PluginServices.getText(this,"nombre"),
191
					PluginServices.getText(this,"projected"),
192
					PluginServices.getText(this,"datum")};
193
			Object[][]data = {};			
194
			dtm = new DefaultTableModel(data, columnNames)
195
			 {
196
				public boolean isCellEditable(int row, int column) {
197
					return false;
198
				}
199
				/*
200
				 * metodo necesario para cuando utilizamos tablas ordenadas
201
				 * ya que sino al ordenar por algun campo no se queda con el orden
202
				 * actual al seleccionar una fila (non-Javadoc)
203
				 * @see javax.swing.table.TableModel#getColumnClass(int)
204
				 */
205
				public Class getColumnClass(int column)
206
				{
207
					return getValueAt(0, column).getClass();
208
				}
209
				};
210
			sorter = new TableSorter(dtm);			
211

  
212
			jTable = new JTable(sorter);
213
			sorter.setTableHeader(jTable.getTableHeader());			
214
			jTable.setCellSelectionEnabled(false);
215
			jTable.setRowSelectionAllowed(true);
216
			jTable.setColumnSelectionAllowed(false);
217
			jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
218
			TableColumn column = null;
219
			for (int i = 0; i < 4; i++) {
220
			    column = jTable.getColumnModel().getColumn(i);
221
			    if (i == 0) {
222
			        column.setPreferredWidth(80); //code column is shorter			     	
223
			    }else if (i ==2) {
224
			    	column.setPreferredWidth(50);
225
			    } else {
226
			    	column.setPreferredWidth(175);
227
			    }
228
			    
229
			}			
230
	}
231
		return jTable;
232
		
233
	}
234
	
235
	public void setCodeCRS(int code) {
236
		codeCRS = code;
237
	}
238
	
239
	public int getCodeCRS() {
240
		return codeCRS;
241
	}	
242
	
243
	private JScrollPane getJScrollPane() {
244
		if (jScrollPane == null) {
245
			jScrollPane = new JScrollPane();
246
			jScrollPane.setPreferredSize(new Dimension(500,150));
247
			jScrollPane.setBorder(
248
				    BorderFactory.createCompoundBorder(
249
					BorderFactory.createCompoundBorder(
250
							BorderFactory.createTitledBorder(PluginServices.getText(this,"ESRI")),
251
							BorderFactory.createEmptyBorder(5,5,5,5)),
252
							jScrollPane.getBorder()));
253
			jScrollPane.setViewportView(getJTable());
123 254
		}
124
		return jButtonOpen;
255
		return jScrollPane;
125 256
	}
126 257
	
127
	public void actionPerformed(ActionEvent e) {
128
		// TODO Auto-generated method stub
129
		if (e.getSource() == this.getButtonOpen()){
130
			add(getOpen(), null);
131
			openChooser.setCurrentDirectory(f);
132
			int returnVal = openChooser.showOpenDialog(ESRIpanel.this);
133
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
134
	            File file = openChooser.getSelectedFile();
135
	            jTextOpen.setText(file.getAbsolutePath());
136
	        }
258
	public void keyPressed(KeyEvent e) {
259
		if (e.getSource() == this.getSearchTextField()){
260
			if (e.getKeyCode() == 10) {
261
				searchButton();
262
			}			
263
		}		
264
	}
265
	
266
	/**
267
	 * M?todo que controla la b?squeda de CRS del repositorio de ESRI.
268
	 * Tambi?n maneja los errores en caso de que los par?metros de b?squeda
269
	 * sean err?neos, o que no se encuentren resultados.
270
	 *
271
	 */
272
	private void searchButton() {		
273
		searchTextField.setBackground(Color.white);
274
		boolean not_numeric = false;
275
		
276
		if (searchTextField.getText().equals("")) {
277
			searchTextField.setBackground(new Color(255,204,204));
278
			JOptionPane.showMessageDialog(this, PluginServices.getText(this,"fill_name"), "Warning...", JOptionPane.WARNING_MESSAGE);
137 279
		}
280
		
281
		else {
282
            //Eliminar filas en cada nueva bsqueda
283
			int numRow = dtm.getRowCount();
284
			while (numRow != 0) {
285
				numRow = numRow - 1;
286
				dtm.removeRow(numRow);
287
			}
288
			
289
			if (codeRadioButton.isSelected() && (searchTextField.getText().length()!=searchTextField.getText().replaceAll("[^0-9]", "").length())){
290
				not_numeric = true;
291
			}
292
			
293
			//Dependiendo de la opcion se realizada una busqueda
294
			ResultSet result = null;
295
								  			
296
			if (codeRadioButton.isSelected() && !not_numeric) {
297
					
298
				key = searchTextField.getText();
299
				int code = Integer.parseInt(key);
300
				String sentence = "SELECT esri_code, esri_wkt, esri_proj, esri_geog, esri_datum " +							 
301
								  "FROM ESRI " +	                              
302
	                              "WHERE esri_code = " + code;
303
								
304
				result = Query.select(sentence,connect.getConnection());	
305
				
306
				
307
				Object[] data = new Object[4];
308
				try {
309
					while (result.next()){
310
						data[0]	= result.getString("esri_code");
311
						data[1] = result.getString("esri_wkt");
312
						String proj = result.getString("esri_proj");
313
						if (!proj.equals("")){
314
							data[1] = proj;
315
							data[2] = PluginServices.getText(this,"si");
316
						} 
317
						else 
318
						{
319
							data[1] = result.getString("esri_geog");
320
							data[2] = PluginServices.getText(this,"no");
321
						}
322
						
323
						data[3] = result.getString("esri_datum");
324
						dtm.addRow(data);
325
					}
326
				} catch (SQLException e1) {
327
					e1.printStackTrace();
328
				}
329
			}
330
			else if (nameRadioButton.isSelected()) {
331
				key = searchTextField.getText();
332
				String key2 = key.substring(0,1);
333
				String key3 = key.substring(1,key.length());
334
				key2 = key2.toUpperCase();
335
				
336
				String sentence = "SELECT esri_code, esri_wkt, esri_proj, esri_geog, esri_datum " +							 
337
					"FROM ESRI " +	                              
338
					"WHERE (esri_proj LIKE '%" + key + "%') OR (esri_proj LIKE '%"+ 
339
					key.toUpperCase() +"%') " +
340
					"OR (esri_proj LIKE '%" + key2+key3 +"%') OR " +
341
							"(esri_geog LIKE '%" + key + "%') OR (esri_geog LIKE '%"+ 
342
					key.toUpperCase() +"%') " +
343
					"OR (esri_geog LIKE '%" + key2+key3 +"%')";
344
								
345
				result = Query.select(sentence,connect.getConnection());	
346
								
347
				Object[] data = new Object[4];
348
				try {
349
					while (result.next()){
350
						data[0]	= result.getString("esri_code");
351
						data[1] = result.getString("esri_wkt");
352
						String proj = result.getString("esri_proj");
353
						if (!proj.equals("")){
354
							data[1] = proj;
355
							data[2] = PluginServices.getText(this,"si");
356
						} 
357
						else 
358
						{
359
							data[1] = result.getString("esri_geog");
360
							data[2] = PluginServices.getText(this,"no");
361
						}
362
						
363
						data[3] = result.getString("esri_datum");
364
						dtm.addRow(data);
365
					}
366
				} catch (SQLException e1) {
367
					e1.printStackTrace();
368
				}
369
			}
370
						
371
			int numr = dtm.getRowCount();
372
			if (not_numeric) {
373
				JOptionPane.showMessageDialog(ESRIpanel.this, 
374
						PluginServices.getText(this,"numeric_format"), 
375
						"Warning...", JOptionPane.WARNING_MESSAGE);
376
				searchTextField.setText("");
377
			}
378
			else if (numr == 0){
379
			JOptionPane.showMessageDialog(this, PluginServices.getText(this,"no_results"), "Warning...",
380
					JOptionPane.WARNING_MESSAGE);
381
			}
382
			else{
383
				this.getJTable().setRowSelectionInterval(0,0);				
384
			}
385
		}		
138 386
	}	
139
}
140 387

  
141
class FiltroESRI extends FileFilter{	
142
	
143
	 final static String prj = "prj";
388
	public void keyReleased(KeyEvent arg0) {
389
		// TODO Auto-generated method stub
144 390
		
145
		public boolean accept(File f) {
146
			if (f.isDirectory()) {
147
	            return true;
148
	        }
391
	}
149 392

  
150
	        String s = f.getName();
151
	        int i = s.lastIndexOf('.');
393
	public void keyTyped(KeyEvent arg0) {
394
		// TODO Auto-generated method stub
395
		
396
	}
152 397

  
153
	        if (i > 0 &&  i < s.length() - 1) {
154
	            String extension = s.substring(i+1).toLowerCase();
155
	            if (prj.equals(extension)){
156
	                    return true;
157
	            } else {
158
	                return false;
159
	            }
160
	        }
161

  
162
	        return false;
398
	/**
399
	 * Maneja los eventos de los botones y los radioButtons del panel
400
	 * del repositorio ESRI.
401
	 */
402
	public void actionPerformed(ActionEvent e) {
403
		// TODO Auto-generated method stub
404
		if (e.getSource() == this.getSearchButton()){
405
			searchTextField.setBackground(Color.white);
406
			if (searchTextField.getText().equals("")) {
407
				searchTextField.setBackground(new Color(255,204,204));
408
				JOptionPane.showMessageDialog(ESRIpanel.this, 
409
						PluginServices.getText(this,"fill_name"), 
410
						"Warning...", JOptionPane.WARNING_MESSAGE);
411
			}
412
			else {
413
				searchButton();
414
			}
163 415
		}
164

  
165
		public String getDescription() {
166
			 return "Archivos .prj";
416
		if (e.getSource() == this.getCodeRadioButton()) {
417
			searchTextField.setText("");
418
			codeRadioButton.setSelected(true);
419
			nameRadioButton.setSelected(false);
167 420
		}
168
}
169

  
421
		
422
		if (e.getSource() == this.getNameRadioButton()) {
423
			searchTextField.setText("");
424
			nameRadioButton.setSelected(true);
425
			codeRadioButton.setSelected(false);
426
		}		
427
	}	
428
	
429
	public ICrs getProjection() {
430
		try {
431
			String txt = getWKT();			
432
			ICrs crs = new CrsFactory().getCRS(getCodeCRS(), txt); 
433
			return crs ;
434
		} catch (CrsException e) {
435
			e.printStackTrace();
436
		}
437
		return null;
438
	}
439
	
440
	/**
441
	 * Consigue la cadena wkt del CRS seleccionado, y genera la cadena que m?s
442
	 * tarde volver? a ser tratada para la consecuci?n de una cadena wkt
443
	 * legible por la proj4.
444
	 *
445
	 */
446
	public void setWKT(){
447
		int code = getCodeCRS();
448
		String sentence = "SELECT esri_wkt " +							 
449
						  "FROM ESRI " +	                              
450
                          "WHERE esri_code = " + code;
451
		
452
		
453
		ResultSet result = Query.select(sentence,connect.getConnection());	
454
	
455
		try {
456
			result.next();			
457
			cadWKT = result.getString("esri_wkt");			
458
		} catch (SQLException e1) {
459
			e1.printStackTrace();
460
		}		
461
		cadWKT = cadWKT.substring(0, cadWKT.length()-1) + ", AUTHORITY[\"ESRI\","+ getCodeCRS()+"]]";
462
		
463
	}
464
	
465
	public String getWKT(){
466
		return cadWKT;
467
	}
468
	
469
	public void setProjection(IProjection crs) {
470
		//setCrs((ICrs) crs);
471
	}
472
	
473
	
474
	
475
	
476
}

Also available in: Unified diff