Revision 2963 trunk/extensions/extGeoreferencing/src/com/iver/cit/gvsig/gui/Panels/SelectPointsPanel.java

View differences:

SelectPointsPanel.java
16 16
import com.iver.andami.PluginServices;
17 17
import com.iver.cit.gvsig.fmap.layers.FLayer;
18 18
import com.iver.cit.gvsig.fmap.layers.FLyrPoints;
19
import com.iver.cit.gvsig.fmap.layers.FLyrPoints.GeoPoint;
19 20
import com.iver.cit.gvsig.gui.View;
20 21

  
21
public class SelectPointsPanel extends JPanel {
22
public class SelectPointsPanel extends JPanel{
22 23

  
23 24
	private JPanel pGeneral = null;
24 25
	private JPanel pPointSelection = null;
......
47 48
	private JPanel pSelectFromView = null;
48 49
	private JButton bSelectFromView = null;
49 50
	private FLyrPoints lyrPoints = null;
51
	private boolean isNew = false;
52
	private JButton bClear = null;
53
	private JButton bDelPoint = null;
54
	private int lastIndexSelection = -1;
50 55
	
51 56
	/**
52 57
	 * This is the default constructor
......
113 118
			flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
114 119
			pPointSelection = new JPanel();
115 120
			pPointSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12), java.awt.Color.black));
121
			pPointSelection.setName("");
116 122
			pPointSelection.setLayout(flowLayout);
117
			pPointSelection.setPreferredSize(new java.awt.Dimension(380,50));
123
			pPointSelection.setPreferredSize(new java.awt.Dimension(382,50));
118 124
			pPointSelection.add(getBPrev(), null);
119 125
			pPointSelection.add(lPoint, null);
120 126
			pPointSelection.add(getCPoint(), null);
......
138 144
			gridBagConstraints2.gridy = 0;
139 145
			pControlPoint = new JPanel();
140 146
			pControlPoint.setLayout(new GridBagLayout());
141
			pControlPoint.setPreferredSize(new java.awt.Dimension(380,160));
147
			pControlPoint.setPreferredSize(new java.awt.Dimension(382,160));
142 148
			pControlPoint.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
143 149
			gridBagConstraints11.gridy = 0;
144 150
			gridBagConstraints11.gridx = 0;
......
149 155
	}
150 156
	
151 157
	/**
158
	 * Busca el punto en la capa FLyrPoint que corresponde con la posicion pos
159
	 * y carga la informaci?n de este en el panel de informaci?n.
160
	 * @param pos
161
	 */
162
	private void loadInfoPoint(int pos){
163
		GeoPoint pto = lyrPoints.getPoint(pos);
164
		if(pto.pixelPoint != null){
165
        	getTX().setText(""+pto.pixelPoint.getX());
166
    		getTY().setText(""+pto.pixelPoint.getY());
167
    	}
168
    	if(pto.mapPoint != null){
169
    		getLatitud().setText(""+pto.mapPoint.getX());
170
    		getLongitud().setText(""+pto.mapPoint.getY());
171
    	}
172
	}
173
	
174
	/**
175
	 * Comprueba si los valores de un punto en los campos de texto est?n
176
	 * vacios y lanza una excepci?n si hay alguno sin informaci?n.
177
	 * @throws NoSuchFieldException
178
	 */
179
	private void checkInfoPointText()throws NoSuchFieldException{
180
		if(	getTX().getText().equals("") ||
181
				getTY().getText().equals("") ||
182
				getLatitud().getText().equals("") ||
183
				getLongitud().getText().equals("")){
184
				isNew = true;
185
				getCPoint().setSelectedIndex(lastIndexSelection);
186
				throw new NoSuchFieldException();
187
		}
188
	}
189
	
190
	/**
191
	 * Comprueba la posici?n del combo para ver si tiene que
192
	 * habilitar o deshabilitar las flechas de delante y detr?s.
193
	 */
194
	private void checkArrows(){
195
		if(getCPoint().getSelectedIndex() == -1){
196
			bPrev.setEnabled(false);
197
			bNext.setEnabled(false);
198
			return;
199
		}
200
		if(getCPoint().getSelectedIndex() == 0)
201
			bPrev.setEnabled(false);
202
		else
203
			bPrev.setEnabled(true);
204
		
205
		if(getCPoint().getSelectedIndex() == (getCPoint().getItemCount() - 1) )
206
			bNext.setEnabled(false);
207
		else
208
			bNext.setEnabled(true);
209
	}
210
	
211
	/**
212
	 * Actualiza el punto de la capa que corresponde con el que 
213
	 * hay seleccionado en el combo. Los valores que se usan para la
214
	 * actualizaci?n son los del info del panel. Si hubiese alg?n valor
215
	 * en blanco la actualizaci?n no se produce.
216
	 */
217
	private void updatePoint()throws NoSuchFieldException{
218
		if(getCPoint().getSelectedIndex() != -1){
219
			Point2D pixel = null, map = null;
220
			checkInfoPointText();
221
			pixel = new Point2D.Double();
222
			pixel.setLocation(	Double.valueOf(getTX().getText()).doubleValue(), 
223
								Double.valueOf(getTY().getText()).doubleValue());
224
			map = new Point2D.Double();
225
			map.setLocation(	Double.valueOf(getLatitud().getText()).doubleValue(), 
226
								Double.valueOf(getLongitud().getText()).doubleValue());
227
			lyrPoints.updatePoint(pixel, map, getCPoint().getSelectedIndex());
228
		}
229
	}
230
	
231
	/**
232
	 * Esta funci?n resetea los controles del panel de info de un punto.
233
	 */
234
	private void resetControls(){
235
		getTX().setText("");
236
		getTY().setText("");
237
		getLatitud().setText("");
238
		getLongitud().setText("");
239
	}
240
	
241
	/**
242
	 * Cuando pasamos de un punto a otro o se va a crear uno nuevo
243
	 * <UL>
244
	 * <LI>Cargamos la capa FLyrPoint si no est? ya cargada.</LI>
245
	 * <LI>Actualizamos el punto seleccionado en ese momento y lanzamos una excepci?n si hay alg?n campo vacio.</LI>
246
	 * <LI>Insertamos el nuevo punto seleccionado en los campos de informaci?n.</LI>
247
	 * <LI>Checkeamos las flechas para ver si tenemos que activarlas o desactivarlas.</LI>
248
	 * </UL>
249
	 * @param changeSelection	Cambia la posici?n seleccionada del combo si es true
250
	 * @param pos	Posici?n a la que cambia el combo si el parametro chageSelection es true
251
	 */
252
	private void changePoint(boolean changeSelection, int pos){
253
		loadLyrPoint();
254
    	try{
255
        	updatePoint();
256
        	if(changeSelection){
257
        		isNew = true;
258
        		lastIndexSelection = pos;
259
        		getCPoint().setSelectedIndex(pos);
260
        	}
261
        	loadInfoPoint(getCPoint().getSelectedIndex());
262
        	checkArrows();
263
    	}catch(NoSuchFieldException exc){
264
    		JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
265
					PluginServices.getText(this, "coordenadas_vacias"));
266
    	}
267
	}
268
	
269
	/**
270
	 * Limpia la lista de puntos y borra el dialogo.
271
	 */
272
	private void clearList(){
273
		loadLyrPoint();
274
		lyrPoints.clear();
275
		resetControls();
276
		isNew = true;
277
		getCPoint().removeAllItems();
278
		checkArrows();
279
	}
280
	
281
	/**
152 282
	 * This method initializes bBefore	
153 283
	 * 	
154 284
	 * @return javax.swing.JButton	
......
156 286
	private JButton getBPrev() {
157 287
		if (bPrev == null) {
158 288
			bPrev = new JButton();
159
			bPrev.setText("<<");
289
			bPrev.setText("<");
290
			bPrev.setEnabled(false);
160 291
			bPrev.addActionListener(new java.awt.event.ActionListener() { 
161 292
				public void actionPerformed(java.awt.event.ActionEvent e) {
162
			        
293
			        if(getCPoint().getSelectedIndex() != 0)		        	 
294
			        	changePoint(true, getCPoint().getSelectedIndex() - 1);
163 295
				}
164 296
			});
165 297
		}
......
167 299
	}
168 300

  
169 301
	/**
302
	 * This method initializes bNext	
303
	 * 	
304
	 * @return javax.swing.JButton	
305
	 */
306
	private JButton getBNext() {
307
		if (bNext == null) {
308
			bNext = new JButton();
309
			bNext.setText(">");
310
			bNext.setEnabled(false);
311
			bNext.addActionListener(new java.awt.event.ActionListener() { 
312
				public void actionPerformed(java.awt.event.ActionEvent e) {
313
					if(getCPoint().getSelectedIndex() != (getCPoint().getItemCount() - 1))
314
			        	changePoint(true, getCPoint().getSelectedIndex() + 1);
315
				}
316
			});
317
		}
318
		return bNext;
319
	}
320
	
321
	/**
170 322
	 * Este m?todo inicializa el combo que contiene el n?mero de puntos.	
171 323
	 * 	
172 324
	 * @return javax.swing.JComboBox	
......
174 326
	public JComboBox getCPoint() {
175 327
		if (cPoint == null) {
176 328
			cPoint = new JComboBox();
329
			cPoint.addActionListener(new java.awt.event.ActionListener() { 
330
				public void actionPerformed(java.awt.event.ActionEvent e) {
331
					 if(getCPoint().getSelectedIndex() != -1){
332
						 if(!isNew){
333
							 changePoint(false, 0); 
334
						 }
335
						 isNew = false;
336
					 }
337
				}
338
			});
177 339
		}
178 340
		return cPoint;
179 341
	}
180

  
342
	
181 343
	/**
182
	 * This method initializes bNext	
344
	 * Este m?todo inicializa el bot?n que crea un nuevo punto de georreferenciaci?n.	
183 345
	 * 	
184 346
	 * @return javax.swing.JButton	
185 347
	 */
186
	private JButton getBNext() {
187
		if (bNext == null) {
188
			bNext = new JButton();
189
			bNext.setText(">>");
190
			bNext.addActionListener(new java.awt.event.ActionListener() { 
348
	private JButton getBNew() {
349
		if (bNew == null) {
350
			bNew = new JButton();
351
			bNew.setText("Nuevo");
352
			bNew.setPreferredSize(new java.awt.Dimension(84,25));
353
	        bNew.addActionListener(new java.awt.event.ActionListener() { 
191 354
				public void actionPerformed(java.awt.event.ActionEvent e) {
192
			        
355
					loadLyrPoint();
356
					try{
357
						updatePoint();
358
						lyrPoints.addPoint(null, null);
359
						isNew = true;
360
						getCPoint().addItem("" + lyrPoints.getCountPoints());
361
						isNew = true;
362
						lastIndexSelection = lyrPoints.getCountPoints() - 1;
363
						getCPoint().setSelectedIndex(lyrPoints.getCountPoints() - 1);
364
						resetControls();
365
						checkArrows();
366
					}catch(NoSuchFieldException exc){
367
						JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
368
								PluginServices.getText(this, "coordenadas_vacias"));
369
					}
193 370
				}
194 371
			});
195 372
		}
196
		return bNext;
373
		return bNew;
197 374
	}
375
	
376
	/**
377
	 * Este m?todo inicializa el bot?n del clear que har? que se eliminen todos los
378
	 * puntos seleccionados.	
379
	 * 	
380
	 * @return javax.swing.JButton	
381
	 */
382
	private JButton getBClear() {
383
		if (bClear == null) {
384
			bClear = new JButton();
385
			bClear.setText(PluginServices.getText(this, "borrar_todos"));
386
			bClear.setPreferredSize(new java.awt.Dimension(125,25));
387
			bClear.addActionListener(new java.awt.event.ActionListener() { 
388
				public void actionPerformed(java.awt.event.ActionEvent e) {
389
					clearList();
390
				}
391
			});
392
		}
393
		return bClear;
394
	}
198 395

  
199 396
	/**
397
	 * Este m?todo inicializa el bot?n del eliminar punto que har? que se elimine
398
	 * el punto seleccionado.	
399
	 * 	
400
	 * @return javax.swing.JButton	
401
	 */
402
	private JButton getBDelPoint() {
403
		if (bDelPoint == null) {
404
			bDelPoint = new JButton();
405
			bDelPoint.setText(PluginServices.getText(this, "eliminar_punto"));
406
			bDelPoint.addActionListener(new java.awt.event.ActionListener() { 
407
				public void actionPerformed(java.awt.event.ActionEvent e) {
408
					if(lyrPoints.length() == 1) //Si solo hay un punto reseteamos
409
						clearList();
410
					else{
411
						loadLyrPoint();
412
						int posSelect = getCPoint().getSelectedIndex();	
413
						lyrPoints.remove(posSelect);
414
						isNew = true;
415
						getCPoint().removeAllItems();
416
						for(int i=0;i<lyrPoints.length();i++){
417
							isNew = true;
418
							getCPoint().addItem(String.valueOf(i+1));
419
						}
420
						isNew = true;
421
						if(posSelect == 0){
422
							lastIndexSelection = posSelect;
423
							getCPoint().setSelectedIndex(posSelect);
424
							loadInfoPoint(posSelect);
425
						}else{
426
							lastIndexSelection = posSelect -1 ;
427
							getCPoint().setSelectedIndex(posSelect - 1);
428
							loadInfoPoint(posSelect - 1);
429
						}
430
						checkArrows();
431
					}
432
				}
433
			});
434
		}
435
		return bDelPoint;
436
	}
437
	
438
	/**
200 439
	 * Obtiene una capa de puntos. Si no existe crea una.
201 440
	 * @return
202 441
	 */
203
	private FLyrPoints getLyrPoint(){
442
	private FLyrPoints loadLyrPoint(){
204 443
		//Buscamos una capa FlyrPoint en la vista y si no la hay la creamos
205 444
		View theView = (View) PluginServices.getMDIManager().getActiveView();
206 445
		
......
223 462
		return lyrPoints;
224 463
	}
225 464
	
226
	/**
227
	 * Este m?todo inicializa el bot?n que crea un nuevo punto de georreferenciaci?n.	
228
	 * 	
229
	 * @return javax.swing.JButton	
230
	 */
231
	private JButton getBNew() {
232
		if (bNew == null) {
233
			bNew = new JButton();
234
			bNew.setText(PluginServices.getText(this, "nuevo"));
235
	        bNew.addActionListener(new java.awt.event.ActionListener() { 
236
				public void actionPerformed(java.awt.event.ActionEvent e) {
237
					getLyrPoint();
238
					//Si hay alg?n punto en la lista se guarda en la capa
239
					if(getCPoint().getSelectedIndex() != -1){
240
						Point2D pixel = null, map = null;
241
						if(	getTX().getText().equals("") ||
242
							getTY().getText().equals("") ||
243
							getLatitud().getText().equals("") ||
244
							getLongitud().getText().equals("")){
245
							JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
246
									PluginServices.getText(this, "coordenadas_vacias"));
247
							return;
248
						}
249
						/*pixel = new Point2D.Double(	Double.valueOf(getTX().getText()), 
250
													Double.valueOf(getTY().getText()));
251
						map = new Point2D.Double(	Double.valueOf(getLatitud().getText()), 
252
													Double.valueOf(getLongitud().getText()));*/
253
						//lyrPoints.updatePoint()
254
					}
255
					getLyrPoint();
256
					getTX().setText("");
257
					getTY().setText("");
258
					getLatitud().setText("");
259
					getLongitud().setText("");
260
					getCPoint().addItem("" + (lyrPoints.getCountPoints() + 1));
261
				}
262
			});
263
		}
264
		return bNew;
265
	}
266 465

  
466

  
267 467
	/**
268 468
	 * Este m?todo inicializa el bot?n seleccionar desde la vista.	
269 469
	 * 	
......
272 472
	private JButton getBSelectFromView() {
273 473
		if (bSelectFromView == null) {
274 474
			bSelectFromView = new JButton();
275
			bSelectFromView.setText(PluginServices.getText(this, "seleccionar"));
475
			bSelectFromView.setText(PluginServices.getText(this, PluginServices.getText(this, "seleccionar")));
476
			bSelectFromView.setPreferredSize(new java.awt.Dimension(125,25));
276 477
			bSelectFromView.addActionListener(new java.awt.event.ActionListener() { 
277 478
				public void actionPerformed(java.awt.event.ActionEvent e) {
278 479
					View theView = (View) PluginServices.getMDIManager().getActiveView();
279
					getLyrPoint();
480
					loadLyrPoint();
280 481
					lyrPoints.setLastTool(theView.getMapControl().getTool());
281 482
					theView.getMapControl().setTool("pointLyrSelection");
282 483
				}
......
402 603
		}
403 604
		return pLongitud;
404 605
	}
405

  
606
	
406 607
	/**
608
	 * Checkea un textField y actualiza un punto con el valor
609
	 * actual del textField
610
	 *
611
	 */
612
	private void checkTextField(JTextField tf, boolean xValue, boolean pixelValue, int code){
613
		System.out.println(code);
614
		
615
		//Si el caracter introducido es erroneo avisamos y volvemos
616
		if((code < 48 || code > 57) && code != 46){
617
			tf.setText("");
618
			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
619
					PluginServices.getText(this, "caracteres_erroneos"));
620
			return;
621
		}
622
		
623
		//Si el caracter era valido salvamos el valor en la capa
624
		if(!tf.getText().equals("")){
625
			loadLyrPoint();
626
			Point2D p = null;
627
			if(pixelValue)
628
				p = ((GeoPoint)lyrPoints.getPoint(getCPoint().getSelectedIndex())).pixelPoint;
629
			else
630
				p = ((GeoPoint)lyrPoints.getPoint(getCPoint().getSelectedIndex())).mapPoint;
631
			if(xValue){
632
				if(p != null)
633
					p.setLocation(Double.valueOf(tf.getText()).doubleValue(), p.getY());
634
				else{
635
					p = new Point2D.Double();
636
					p.setLocation(Double.valueOf(tf.getText()).doubleValue(), 0.0);
637
				}
638
			}else{
639
				if(p != null)
640
					p.setLocation(p.getX(), Double.valueOf(tY.getText()).doubleValue());
641
				else{
642
					p = new Point2D.Double();
643
					p.setLocation(0.0, Double.valueOf(tY.getText()).doubleValue());
644
				}
645
			}
646
		}
647
	}
648
	
649
	/**
407 650
	 * This method initializes tX	
408 651
	 * 	
409 652
	 * @return javax.swing.JTextField	
......
412 655
		if (tX == null) {
413 656
			tX = new JTextField();
414 657
			tX.setPreferredSize(new java.awt.Dimension(90,19));
658
			tX.addKeyListener(new java.awt.event.KeyListener() { 
659
				public void keyPressed(java.awt.event.KeyEvent e) {}
660
				public void keyReleased(java.awt.event.KeyEvent e) {
661
					checkTextField(tX, true, true, e.getKeyCode());
662
				}
663
				public void keyTyped(java.awt.event.KeyEvent e) {}
664
			});
415 665
		}
416 666
		return tX;
417 667
	}
......
425 675
		if (tY == null) {
426 676
			tY = new JTextField();
427 677
			tY.setPreferredSize(new java.awt.Dimension(90,19));
678
			tY.addKeyListener(new java.awt.event.KeyListener() { 
679
				public void keyPressed(java.awt.event.KeyEvent e) {}
680
				public void keyReleased(java.awt.event.KeyEvent e){
681
					checkTextField(tY, false, true, e.getKeyCode());
682
				}
683
				public void keyTyped(java.awt.event.KeyEvent e) {}
684
			});
428 685
		}
429 686
		return tY;
430 687
	}
......
438 695
		if (tLatitud == null) {
439 696
			tLatitud = new JTextField();
440 697
			tLatitud.setPreferredSize(new java.awt.Dimension(111,19));
698
			tLatitud.addKeyListener(new java.awt.event.KeyListener() { 
699
				public void keyPressed(java.awt.event.KeyEvent e) {}
700
				public void keyReleased(java.awt.event.KeyEvent e) {
701
					checkTextField(tLatitud, true, false, e.getKeyCode());	
702
				}
703
				public void keyTyped(java.awt.event.KeyEvent e) {}
704
			});
705
			
441 706
		}
442 707
		return tLatitud;
443 708
	}
......
451 716
		if (tLongitud == null) {
452 717
			tLongitud = new JTextField();
453 718
			tLongitud.setPreferredSize(new java.awt.Dimension(111,19));
719
			tLongitud.addKeyListener(new java.awt.event.KeyListener() { 
720
				public void keyPressed(java.awt.event.KeyEvent e) {}
721
				public void keyReleased(java.awt.event.KeyEvent e) {
722
					checkTextField(tLongitud, false, false, e.getKeyCode());	
723
				}
724
				public void keyTyped(java.awt.event.KeyEvent e) {}
725
			});
454 726
		}
455 727
		return tLongitud;
456 728
	}
......
475 747
	 */    
476 748
	private JPanel getPSelectFromView() {
477 749
		if (pSelectFromView == null) {
750
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
751
			gridBagConstraints9.gridx = 0;
752
			gridBagConstraints9.insets = new java.awt.Insets(2,2,2,2);
753
			gridBagConstraints9.gridy = 2;
754
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
755
			gridBagConstraints8.gridx = 0;
756
			gridBagConstraints8.insets = new java.awt.Insets(2,2,2,2);
757
			gridBagConstraints8.gridy = 1;
758
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
759
			gridBagConstraints7.insets = new java.awt.Insets(2,2,2,2);
760
			gridBagConstraints7.gridy = 0;
761
			gridBagConstraints7.gridx = 0;
478 762
			pSelectFromView = new JPanel();
479
			pSelectFromView.setPreferredSize(new java.awt.Dimension(150,35));
480
			pSelectFromView.add(getBSelectFromView(), null);
763
			pSelectFromView.setLayout(new GridBagLayout());
764
			pSelectFromView.setPreferredSize(new java.awt.Dimension(150,150));
765
			pSelectFromView.add(getBSelectFromView(), gridBagConstraints7);
766
			pSelectFromView.add(getBClear(), gridBagConstraints8);
767
			pSelectFromView.add(getBDelPoint(), gridBagConstraints9);
481 768
		}
482 769
		return pSelectFromView;
483 770
	}

Also available in: Unified diff