Revision 40719

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.labeling.app/org.gvsig.labeling.app.mainplugin/src/main/java/org/gvsig/labeling/gui/layerproperties/GeneralLabeling.java
207 207
			try {
208 208
				targetLayer = (FLyrVect) layer;//.cloneLayer();
209 209
				
210
				FeatureStore fsto = (FeatureStore) targetLayer.getFeatureStore().clone();
211
				
212
				auxLayer = (FLyrVect) MapContextLocator.getMapContextManager().createLayer(
213
						layer.getName(), fsto);
214
				// auxLayer = (FLyrVect) targetLayer.cloneLayer();
210
				auxLayer = (FLyrVect) targetLayer.cloneLayer();
215 211
				auxLayer.setParentLayer(layer.getParentLayer());
216 212
				auxLayer.setLegend((IVectorLegend)targetLayer.getLegend());
217 213
				auxLayer.setProjection(targetLayer.getProjection());
218 214
				
219
				/*
220
						(FLyrVect) MapContextLocator.getMapContextManager().createLayer(
221
						layer.getName(), ((FLyrVect) layer).getFeatureStore());
222
				auxLayer.setParentLayer(layer.getParentLayer());
223
				auxLayer.setLegend((IVectorLegend)targetLayer.getLegend());
224
				auxLayer.setProjection(targetLayer.getProjection());
225
				*/
226

  
227 215
				if (str instanceof GeneralLabelingStrategy) {
228 216
					GeneralLabelingStrategy gls = (GeneralLabelingStrategy) str; 
229 217
					gStr = (GeneralLabelingStrategy) gls.clone();
......
424 412

  
425 413
	}
426 414
}
427

  
428
/*
429
class _GeneralLabeling extends JPanel implements ILabelingStrategyPanel, ActionListener {
430
	private static final long serialVersionUID = 8864709758980903351L;
431
	private static Hashtable<String, Class<? extends ILabelingMethod>> methods
432
		= new Hashtable<String, Class<? extends ILabelingMethod>>();
433
	private static int newClassSuffix = 0;
434
	private JButton btnVisualization;
435
	private JButton btnRenameClass;
436
	private JButton btnSQLQuery;
437
	private JButton btnDelClass;
438
	private JButton btnAddClass;
439
	private JButton btnPlacement;
440
	private JComboBoxLabelingMethod cmbMethod;
441
	private GridBagLayoutPanel classesPanel;
442
	private JComboBox cmbClasses;
443
	private IPlacementConstraints placementConstraints;
444
	private IZoomConstraints zoomConstraints;
445
	private long minScaleView = -1, maxScaleView = -1;
446
	private boolean noEvent;
447
	private JCheckBox chkLabel;
448
	private JCheckBox chkTextOnly;
449
	private FLyrVect layer;
450
	private String[] fieldNames;
451
	private JDnDList lstClassPriorities;
452
	private JCheckBox chkDefinePriorities;
453
	private JScrollPane scrlPan;
454
	private LabelClassRenderingProperties labelClassRenderingProperties;
455
	private JCheckBox chkAllowLabelOverlapping;
456

  
457
	public _GeneralLabeling() {
458
		initialize();
459
	}
460

  
461
	private void initialize() {
462
		setLayout(new BorderLayout());
463
		GridBagLayoutPanel left = new GridBagLayoutPanel();
464
		JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
465
		aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
466
		aux.add(getCmbMethod());
467
		aux.setPreferredSize(new Dimension(605, 40));
468
		left.addComponent(aux);
469

  
470

  
471
		classesPanel = new GridBagLayoutPanel();
472
		aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
473
		aux.add(new JLabel(PluginServices.getText(this, "class")+":"));
474
		aux.add(getCmbClasses());
475
		aux.add(getChkLabelFeatures());
476
		aux.setPreferredSize(new Dimension(602, 40));
477
		classesPanel.addComponent(aux);
478

  
479
		aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
480
		aux.add(getBtnAddClass());
481
		aux.add(getBtnDelClass());
482
		aux.add(getBtnRenameClass());
483
		aux.add(getBtnSQLQuery());
484
		classesPanel.addComponent(aux);
485
		classesPanel.setBorder(BorderFactory.createTitledBorder(
486
				null, PluginServices.getText(this, "classes")));
487
		left.addComponent(classesPanel);
488

  
489

  
490
		left.addComponent(labelClassRenderingProperties = new LabelClassRenderingProperties());
491
		aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
492
		aux.setBorder(BorderFactory.createTitledBorder(
493
				null, PluginServices.getText(this, "options")));
494
		aux.add(getBtnVisualization());
495

  
496
		aux.add(getBtnPlacement());
497
		aux.add(getChkAllowLabelOverlapping());
498
		aux.setPreferredSize(new Dimension(612, 60));
499

  
500
		left.addComponent(aux);
501

  
502
		add(left, BorderLayout.CENTER);
503

  
504
		JPanel right = new JPanel(new BorderLayout(15, 15));
505
		aux = new JPanel(new BorderLayout(15, 15));
506
		aux.add(getChkDefinePriorities(), BorderLayout.NORTH);
507
		aux.add(getScrlClassPriorities(), BorderLayout.CENTER);
508
		right.add(new JBlank(10, 10), BorderLayout.NORTH);
509
		right.add(aux, BorderLayout.CENTER);
510
		right.add(new JBlank(10, 10), BorderLayout.SOUTH);
511
		right.add(new JBlank(10, 10), BorderLayout.EAST);
512
		add(right, BorderLayout.EAST);
513
	}
514

  
515

  
516
	private JCheckBox getChkAllowLabelOverlapping() {
517
		if (chkAllowLabelOverlapping == null) {
518
			chkAllowLabelOverlapping = new JCheckBox(PluginServices.getText(this, "allow_label_overlapping"));
519

  
520
		}
521

  
522
		return chkAllowLabelOverlapping;
523
	}
524

  
525
	private Component getScrlClassPriorities() {
526
		if (scrlPan == null) {
527
			scrlPan = new JScrollPane();
528
			scrlPan.setViewportView(getLstClassPriorities());
529
			scrlPan.setPreferredSize(new Dimension(180, 300));
530
		}
531
		return scrlPan;
532
	}
533

  
534
	private JCheckBox getChkDefinePriorities() {
535
		if (chkDefinePriorities == null) {
536
			chkDefinePriorities = new JCheckBox(PluginServices.getText(this, "label_priority"));
537
			chkDefinePriorities.addActionListener(this);
538
			chkDefinePriorities.setName("CHK_DEFINE_PRIORITIES");
539
		}
540
		return chkDefinePriorities;
541
	}
542

  
543
	private JDnDList getLstClassPriorities() {
544
		if (lstClassPriorities == null) {
545
			lstClassPriorities = new JDnDList();
546
			lstClassPriorities.setName("CLASS_PRIORITY_LIST");
547
			lstClassPriorities.addMouseListener(new MouseAdapter() {
548
				@Override
549
				public void mouseReleased(MouseEvent e) {
550
					JDnDListModel m = (JDnDListModel) lstClassPriorities.getModel();
551
					for (int i = 0; i < m.getSize(); i++) {
552
						((LabelClass) m.getElementAt(i)).setPriority(i);
553
					}
554
				}
555
			});
556

  
557
		}
558
		return lstClassPriorities;
559
	}
560

  
561
	private void refreshControls() {
562
		// classes combo box
563
		refreshClasses();
564

  
565
		// fires an event from the methods combo box
566
		actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
567
	}
568

  
569
	private JButton getBtnVisualization() {
570
		if (btnVisualization == null) {
571
			btnVisualization = new JButton(
572
					PluginServices.getText(this, "visualization")+"...");
573
			btnVisualization.setName("BTNVISUALIZATION");
574
			btnVisualization.addActionListener(this);
575
		}
576
		return btnVisualization;
577
	}
578

  
579
	private JButton getBtnPlacement() {
580
		if (btnPlacement == null) {
581
			btnPlacement = new JButton(
582
					PluginServices.getText(this, "placement")+"...");
583
			btnPlacement.setName("BTNPLACEMENT");
584
			btnPlacement.addActionListener(this);
585
		}
586
		return btnPlacement;
587
	}
588

  
589
	private JComboBox getCmbMethod() {
590
		if (cmbMethod == null) {
591
			Iterator<String> it = methods.keySet().iterator();
592
			ArrayList<MethodItem> aux = new ArrayList<MethodItem>();
593
			while (it.hasNext()) {
594
				String name = it.next();
595

  
596
				Class<? extends ILabelingMethod> methodClass = (Class<? extends ILabelingMethod>) methods.get(name);
597
				ILabelingMethod method;
598
				try {
599
					method = (ILabelingMethod) methodClass.newInstance();
600
					MethodItem newItem = new MethodItem(name, method);
601
					aux.add(newItem);
602

  
603
				} catch (InstantiationException e) {
604
					NotificationManager.addError("Trying to instantiate an interface" +
605
							" or abstract class + "+methodClass.getName(), e);
606
				} catch (IllegalAccessException e) {
607
					NotificationManager.addError("IllegalAccessException: does " +
608
							methodClass.getName()	+ " class have an anonymous" +
609
							" constructor?", e);
610
				}
611

  
612
			}
613
			cmbMethod = new JComboBoxLabelingMethod(aux.toArray(new MethodItem[0]));
614
			cmbMethod.setSize(new Dimension(300, 22));
615
			cmbMethod.setName("CMBMETHOD");
616
			cmbMethod.addActionListener(this);
617
		}
618
		return cmbMethod;
619
	}
620

  
621
	private JButton getBtnSQLQuery() {
622
		if (btnSQLQuery == null) {
623
			btnSQLQuery = new JButton(PluginServices.getText(this, "SQL_query"));
624
			btnSQLQuery.setName("BTNSQLQUERY");
625
			btnSQLQuery.addActionListener(this);
626
		}
627
		return btnSQLQuery;
628
	}
629

  
630
	private JButton getBtnRenameClass() {
631
		if (btnRenameClass == null) {
632
			btnRenameClass = new JButton(PluginServices.getText(this, "remane_class"));
633
			btnRenameClass.setName("BTNRENAMECLASS");
634
			btnRenameClass.addActionListener(this);
635
		}
636
		return btnRenameClass;
637
	}
638

  
639
	private JButton getBtnDelClass() {
640
		if (btnDelClass == null) {
641
			btnDelClass = new JButton(PluginServices.getText(this, "delete_class"));
642
			btnDelClass.setName("BTNDELCLASS");
643
			btnDelClass.addActionListener(this);
644
		}
645
		return btnDelClass;
646
	}
647

  
648
	private JButton getBtnAddClass() {
649
		if (btnAddClass == null) {
650
			btnAddClass = new JButton(PluginServices.getText(this, "add_class"));
651
			btnAddClass.setName("BTNADDCLASS");
652
			btnAddClass.addActionListener(this);
653
		}
654
		return btnAddClass;
655
	}
656

  
657
	private JCheckBox getChkLabelFeatures() {
658
		if (chkLabel == null) {
659
			chkLabel = new JCheckBox();
660
			chkLabel.setText(PluginServices.getText(this, "label_features_in_this_class"));
661
			chkLabel.setName("CHKLABEL");
662
			chkLabel.addActionListener(this);
663
		}
664
		return chkLabel;
665
	}
666

  
667
	private JComboBox getCmbClasses() {
668
		if (cmbClasses == null) {
669
			cmbClasses = new JComboBox();
670
			cmbClasses.setPreferredSize(new Dimension(150, 20));
671
			cmbClasses.setName("CMBCLASSES");
672
			cmbClasses.addActionListener(this);
673
		}
674
		return cmbClasses;
675
	}
676

  
677
	public ILabelingStrategy getLabelingStrategy() {
678
		ILabelingStrategy st = ExtendedLabelingFactory.
679
						createStrategy((FLayer) layer,
680
								getMethod(),
681
								getPlacementConstraints(),
682
								getZoomConstraints());
683
		if (st instanceof GeneralLabelingStrategy) {
684
			GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) st;
685
			gStr.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
686
			gStr.setMinScaleView(minScaleView);
687
			gStr.setMaxScaleView(maxScaleView);
688
		}
689
		return st;
690
	}
691

  
692
	public void setModel(FLayer layer, ILabelingStrategy str) {
693
		if (layer instanceof FLyrVect) {
694
			this.layer = (FLyrVect) layer;
695
			FLyrVect lv = (FLyrVect) layer;
696
			try {
697
				fieldNames = lv.getRecordset().getFieldNames();
698
				labelClassRenderingProperties.setFieldNames(fieldNames);
699
				if (str instanceof GeneralLabelingStrategy) {
700
					try {
701
						GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) LabelingFactory.createStrategyFromXML(str.getXMLEntity(), layer.cloneLayer());
702
						setMethod(str.getLabelingMethod());
703
						placementConstraints = str.getPlacementConstraints();
704
						getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
705
						minScaleView = gStr.getMinScaleView();
706
						maxScaleView = gStr.getMaxScaleView();
707

  
708
					} catch (ReadDriverException e) {
709
						NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
710
					} catch (Exception e) {
711
						NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
712
					}
713
				}
714

  
715
				refreshControls();
716
				labelClassRenderingProperties.setModel(getActiveClass());
717

  
718
			} catch (ReadDriverException e) {
719
				NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
720
			}
721
		}
722
	}
723

  
724

  
725
	public static void addLabelingMethod(String name, Class<? extends ILabelingMethod> iLabelingMethodClass) {
726
		methods.put(name, iLabelingMethodClass);
727
	}
728

  
729
	private void setMethod(ILabelingMethod labelingMethod) {
730
		getCmbMethod().setSelectedItem(new MethodItem(null, labelingMethod));
731
	}
732

  
733
	private ILabelingMethod getMethod() {
734
		return ((MethodItem) getCmbMethod().getSelectedItem()).method;
735
	}
736

  
737
	private void refreshClasses() {
738
		// label classes
739
		getCmbClasses().removeAllItems();
740
		LabelClass[] lClasses = getMethod().getLabelClasses();
741
		for (int i = 0; i < lClasses.length; i++) {
742
			getCmbClasses().addItem(lClasses[i]);
743

  
744
		}
745

  
746
		labelClassRenderingProperties.setModel(
747
				(LabelClass) getCmbClasses().getSelectedItem());
748
		// expressions combo box
749
//		refreshCmbExpressions();
750

  
751
		// panel priorities
752
		refreshPnlPriorities();
753

  
754
	}
755

  
756
	private void refreshPnlPriorities() {
757
		TreeSet<LabelClass> ts = new TreeSet<LabelClass>(new LabelClassComparatorByPriority());
758

  
759
		LabelClass[] lClasses = getMethod().getLabelClasses();
760
		for (int i = 0; i < lClasses.length; i++) {
761
			ts.add(lClasses[i]);
762
		}
763

  
764
		// refresh label priority panel
765
		getChkDefinePriorities().setSelected(getMethod().definesPriorities());
766
		JDnDListModel m = new JDnDListModel();
767
		for (LabelClass labelClass : ts) {
768
			m.addElement(labelClass);
769
		}
770
		getLstClassPriorities().setModel(m);
771

  
772
	}
773

  
774
	private LabelClass getActiveClass() {
775
		return (LabelClass) getCmbClasses().getSelectedItem();
776
	}
777

  
778
	private IZoomConstraints getZoomConstraints() {
779
		return zoomConstraints;
780
	}
781

  
782
	private IPlacementConstraints getPlacementConstraints() {
783
		return placementConstraints;
784
	}
785

  
786
	public void actionPerformed(ActionEvent e) {
787
		if (noEvent) return;
788
		JComponent c = (JComponent)e.getSource();
789

  
790
		if (c.equals(btnAddClass)) {
791
			LabelClass newClass = new LabelClass();
792
			newClass.setName(PluginServices.getText(this, "labeling")+String.valueOf(++newClassSuffix));
793
			getMethod().addLabelClass(newClass);
794
			refreshClasses();
795
			getCmbClasses().setSelectedItem(newClass);
796
		} else if (c.equals(btnDelClass)) {
797
			LabelClass clazz = getActiveClass();
798
			getMethod().deleteLabelClass(clazz);
799
			refreshClasses();
800
		} else if (c.equals(btnRenameClass)) {
801
			LabelClass clazz = getActiveClass();
802
			String newName = JOptionPane.showInputDialog(
803
					PluginServices.getText(this, "enter_new_name"));
804
			if (newName != null)
805
				getMethod().renameLabelClass(clazz, newName);
806
			refreshClasses();
807
		} else if (c.equals(btnSQLQuery)) {
808
			LabelClass clazz = getActiveClass();
809
			String query = clazz.getSQLQuery();
810
			query = JOptionPane.showInputDialog(null,
811
					  "select from "+layer.getName()+" where ",
812
					  "SQL query",
813
					  JOptionPane.QUESTION_MESSAGE);
814
			if (!query.equals("")) {
815
				clazz.setSQLQuery(query);
816
			}
817

  
818
		} else  if (c.equals(chkLabel)) {
819
			LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
820
			if (lc == null)
821
				lc = getMethod().getDefaultLabelClass();
822
			lc.setVisible(chkLabel.isSelected());
823
		} else if (c.equals(chkTextOnly)) {
824

  
825
		} else if (c.equals(cmbClasses)) {
826
			// refresh expressions
827
//			LabelClass lc = (LabelClass) cmbClasses.getSelectedItem();
828
//			if (lc != null && lc.getLabelExpression() != null && !getExpressions().contains(lc.getLabelExpression())) {
829
//				getExpressions().add(0, lc.getLabelExpression());
830
//			}
831
//			refreshCmbExpressions();
832
		} else if (c.equals(btnPlacement)) {
833

  
834
			try {
835
				IPlacementProperties pp = PlacementProperties.createPlacementProperties(
836
						getPlacementConstraints(),
837
						((FLyrVect) layer).getShapeType());
838
				PluginServices.getMDIManager().addWindow(pp);
839
				placementConstraints = pp.getPlacementConstraints();
840
			} catch (ClassCastException ccEx) {
841
				NotificationManager.addError("Placement constraints not prepared for:"
842
						+layer.getClass().getName(),
843
						ccEx);
844
			} catch (ReadDriverException dEx) {
845
				NotificationManager.addWarning("Should be unreachable code", dEx);
846
				NotificationManager.addError(PluginServices.getText(this, "usupported_layer_type"), dEx);
847
			}
848

  
849
		} else if (c.equals(btnVisualization)) {
850
			LabelScaleRange lsr = new LabelScaleRange(minScaleView, maxScaleView);
851
			PluginServices.getMDIManager().addWindow(lsr);
852
			minScaleView = lsr.getMinScale();
853
			maxScaleView = lsr.getMaxScale();
854
		} else if (c.equals(cmbMethod)) {
855
			// disable components in class panel
856
			// multiple class or not enables or disables the class panel
857
			setComponentEnabled(classesPanel, getMethod().allowsMultipleClass());
858
			refreshClasses();
859

  
860
		} else if (c.equals(chkDefinePriorities)) {
861
			getMethod().setDefinesPriorities(chkDefinePriorities.isSelected());
862
			refreshPnlPriorities();
863
		}
864
	}
865

  
866
	private void setComponentEnabled(Component c, boolean b) {
867
		if (c instanceof JComponent) {
868
			JComponent c1 = (JComponent) c;
869
			for (int i = 0; i < c1.getComponentCount(); i++) {
870
				setComponentEnabled(c1.getComponent(i), b);
871
			}
872
		}
873
		c.setEnabled(b);
874
	}
875

  
876
	public String getLabelingStrategyName() {
877
		return PluginServices.getText(this, "user_defined_labels");
878
	}
879

  
880
	public Class getLabelingStrategyClass() {
881
		return GeneralLabelingStrategy.class;
882
	}
883

  
884
	private class JComboBoxLabelingMethod extends JComboBox {
885
		private static final long serialVersionUID = 5935267402200698145L;
886

  
887
		public JComboBoxLabelingMethod(MethodItem[] items) {
888
			super(items);
889
		}
890

  
891
		@Override
892
		public void setSelectedItem(Object anObject) {
893
			if (anObject instanceof MethodItem) {
894
				MethodItem methodItem = (MethodItem) anObject;
895
				for (int i = 0; i < getItemCount(); i++) {
896
					MethodItem aux = (MethodItem) getItemAt(i);
897
					if (aux.equals(methodItem)) {
898
						aux.method=methodItem.method;
899
					}
900
				}
901
				super.setSelectedItem(methodItem);
902
//				((MethodItem) super.getSelectedItem()).method = methodItem.method;
903
			}
904
		}
905
	}
906
	private class MethodItem {
907
		private String name;
908
		private ILabelingMethod method;
909

  
910
		private MethodItem(String name, ILabelingMethod method) {
911
			this.name = name;
912
			this.method = method;
913
		}
914

  
915
		public String toString() {
916
			return name;
917
		}
918

  
919
		@Override
920
		public boolean equals(Object obj) {
921
			if (obj != null && obj instanceof MethodItem) {
922
				MethodItem methodItem = (MethodItem) obj;
923
				return methodItem.method.getClass().equals(this.method.getClass());
924
			}
925
			return false;
926
		}
927
	}
928

  
929
}*/

Also available in: Unified diff