Revision 144 branches/usability_v2/org.gvsig.app.document.layout.app/org.gvsig.app.document.layout.app.mainplugin/src/main/java/org/gvsig/app/project/documents/layout/fframes/FFrameView.java

View differences:

FFrameView.java
42 42
import org.gvsig.app.project.documents.layout.FLayoutFunctions;
43 43
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
44 44
import org.gvsig.app.project.documents.layout.LayoutNotification;
45
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
45 46
import org.gvsig.app.project.documents.view.ViewDocument;
47
import org.gvsig.app.project.documents.view.toc.gui.TOC;
46 48
import org.gvsig.compat.print.PrintAttributes;
47 49
import org.gvsig.fmap.dal.exception.ReadException;
48 50
import org.gvsig.fmap.geom.Geometry;
......
94 96
    // following fields are unused - should be deleted? 
95 97
    private static final String EXTENSION_FIELD = "extension";
96 98

  
97
    private static final GeometryManager geomManager = GeometryLocator
98
        .getGeometryManager();
99 99
    private static final Logger logger = LoggerFactory
100 100
        .getLogger(FFrameView.class);
101 101
    public static final int PRESENTATION = 0;
......
121 121
	private boolean b_viewOriginatedUpdate = false;
122 122
	private boolean b_fframeOriginatedUpdate = false;
123 123
	private boolean b_validCache = false;
124
	protected ViewDocListener viewDocListener = new ViewDocListener();
125
	protected OwnMapContextListener ownMapContextListener = new OwnMapContextListener();
124
	protected ViewDocListener viewDocListener;
125
	protected OwnMapContextListener ownMapContextListener;
126

  
127
	private TOC toc;
126 128
		
127 129

  
128 130
    /**
......
130 132
     */
131 133
    public FFrameView() {
132 134
        num++;
135
        createListeners();
133 136
    }
137
    
138
    protected void createListeners() {
139
    	viewDocListener = new ViewDocListener();
140
    	ownMapContextListener = new OwnMapContextListener();
141
    }
134 142

  
135 143
    /**
136 144
     * Devuelve una descripci?n del FFrameView.
......
338 346
        Color theBackColor = viewPort.getBackColor();
339 347
    	int drawWidth = (int)fframeViewRect.width;
340 348
    	int drawHeight = (int)fframeViewRect.height;
341
/*
342
 * TODO INVERTIR LA CONDICION
349

  
350
        //If the image has to be created...
343 351
    	if (origin == null ||
344 352
    			!origin.equals(mapOrigin) ||
345 353
    			affineTransform.getScaleX() != scaleAnt ||
346 354
    			m_image == null ||
347
    			!b_validCache ||
348
    			) { 
349
    	}
350
    	*/
351
        //If the image has to be created...
352
        if (!(origin != null
353
            && origin.equals(mapOrigin)
354
            && affineTransform.getScaleX() == scaleAnt
355
            && m_image != null
356
            && b_validCache
357
            && !(fframeViewRect.getWidth() > visibleLayoutDocRect.getWidth() || fframeViewRect.getHeight() > visibleLayoutDocRect.getHeight()))) {
355
    			!b_validCache) { 
358 356

  
359 357
        	viewPort.setImageSize(new Dimension(drawWidth, drawHeight));
360 358
            viewPort.setDPI(getDrawPaperDPI());
......
677 675

  
678 676
    public Object clone() throws CloneNotSupportedException {
679 677
        FFrameView frame = (FFrameView) super.clone();
680
        frame.viewDocListener = new ViewDocListener();
681
        frame.ownMapContextListener = new OwnMapContextListener();
678
        frame.createListeners();
682 679
        frame.setSelected(this.isSelected());
683 680
        frame.setTypeScale(this.getTypeScale());
684 681
        frame.setLinked(linked);
......
718 715
    }
719 716

  
720 717
    public void refreshOriginalExtent() {
721
    	/*
722
        if (getTypeScale() == AUTOMATICO) {
723
            viewDocument.getMapContext().getViewPort()
724
                .setEnvelope(getMapContext().getViewPort().getEnvelope());
725
            viewDocument.getMapContext().getViewPort().refreshExtent();
726
        } else
727
            if (getTypeScale() == MANUAL) {
728
                Envelope oldExtent =
729
                    viewDocument.getMapContext().getViewPort().getEnvelope();
730
                Envelope newExtent =
731
                    getMapContext().getViewPort().getEnvelope();
732
                double xDif = newExtent.getCenter(0) - oldExtent.getCenter(0);
733
                double yDif = newExtent.getCenter(1) - oldExtent.getCenter(1);
734
                try {
735
                    viewDocument.getMapContext()
736
                        .getViewPort()
737
                        .setEnvelope(
738
                            geomManager.createEnvelope(oldExtent.getMinimum(0)
739
                                + xDif, oldExtent.getMinimum(1) + yDif,
740
                                oldExtent.getMaximum(0) + xDif,
741
                                oldExtent.getMaximum(1) + yDif, SUBTYPES.GEOM2D));
742
                } catch (CreateEnvelopeException e) {
743
                    e.printStackTrace();
744
                }
745
                viewDocument.getMapContext().getViewPort().refreshExtent();
746
            }
747
            */
748 718
    }
749 719

  
750 720
    public static void registerPersistent() {
......
851 821
        observers.notifyObservers(this, 
852 822
                new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));
853 823
	}
824

  
825
	public void refresh() {
826
        // FIXME: what should we do here? do we really need to invalidate cache??
827
    	b_validCache = false;		
828
	}
854 829
    
855
    protected class ViewDocListener
856
    	implements ViewPortListener, LegendListener, LayerCollectionListener {
830
    protected void resetListeners() {
831
		if (this.getMapContext()!=null) {
832
			clearOwnListeners(this.getMapContext());
833
		}
834
		if (this.getView()!=null && this.getView().getMapContext()!=null) {
835
			clearViewListeners(this.getView().getMapContext());
836
		}
837
    	setListeners();
838
    }
839
    
840
    protected void setListeners() {
841
    	if (getView()!=null) {
842
    		if (linked) {
843
    			getView().getMapContext().addLayerListener(viewDocListener);
844
    			getView().getMapContext().getLayers().addLayerCollectionListener(viewDocListener);
845
    		}
846
    		if (getTypeScale()==IFFrameUseFMap.AUTOMATICO) {
847
    			getView().getMapContext().getViewPort().addViewPortListener(viewDocListener);
848
    		}
849
    	}
850
    	if (getMapContext()!=null) {
851
    		getMapContext().addLayerListener(ownMapContextListener);
852
    		getMapContext().getLayers().addLayerCollectionListener(ownMapContextListener);
853
    		getMapContext().getViewPort().addViewPortListener(ownMapContextListener);
854
    	}
855
    }
856
    protected void clearOwnListeners(MapContext mapContext) {
857
    	mapContext.removeLayerListener(ownMapContextListener);
858
    	mapContext.getViewPort().removeViewPortListener(ownMapContextListener);
859
    	mapContext.getLayers().removeLayerCollectionListener(ownMapContextListener);
860
    }
861
    protected void clearViewListeners(MapContext mapContext) {
862
    	mapContext.removeLayerListener(viewDocListener);
863
    	mapContext.getViewPort().removeViewPortListener(viewDocListener);
864
    	mapContext.getLayers().removeLayerCollectionListener(viewDocListener);
865
    }
866
    
867
    /*
868
     * (non-Javadoc)
869
     * @see org.gvsig.tools.dispose.Disposable#dispose()
870
     */
871
	public void dispose() {
872
		try {
873
			if (this.getMapContext()!=null) {
874
				clearOwnListeners(this.getMapContext());
875
			}
876
			if (this.getView()!=null && this.getView().getMapContext()!=null) {
877
				clearViewListeners(this.getView().getMapContext());
878
			}
879
		}
880
		catch (Exception ex) {}
881
		this.viewDocument = null;
882
		this.mapContext = null;
883
	}
857 884

  
885
	/**
886
	 * This method is called just before the FFrame is going to
887
	 * be removed from the Layout
888
	 */
889
	protected void beforeRemoved() {
890
		if (mapContext!=null) {
891
			clearOwnListeners(mapContext);
892
		}
893
		if (this.getView()!=null && this.getView().getMapContext()!=null) {
894
			clearViewListeners(this.getView().getMapContext());
895
		}
896
	}
897

  
898
	/**
899
	 * This method is called just after the FFrame has been
900
	 * added to the Layout
901
	 */
902
	protected void afterAdded() {
903
		this.toc = new TOC();
904
		if (this.getMapContext()!=null) {
905
			this.toc.setMapContext(this.getMapContext());
906
		}
907
		setListeners();	
908
	}
909
	
910
	public void setToc(TOC toc) {
911
		this.toc = toc;
912
	}
913

  
914
	private class ViewDocListener
915
	implements ViewPortListener, LegendListener, LayerCollectionListener {
916

  
858 917
		public void extentChanged(ExtentEvent e) {
859 918
			if (!b_fframeOriginatedUpdate && getTypeScale()==AUTOMATICO) {
860 919
				if (getMapContext()!=null) {
......
901 960

  
902 961
		public void legendChanged(LegendChangedEvent e) {
903 962
			conditionalRedraw();
963
			if (linked && toc!=null) {
964
				toc.refresh();
965
			}
904 966
		}
905 967

  
906 968
		public void layerAdded(LayerCollectionEvent e) {
......
934 996
				throws CancelationException {
935 997
			conditionalRedraw();
936 998
		}
937
    }
938
    
939
    protected class OwnMapContextListener
940
    	implements ViewPortListener, LegendListener, LayerCollectionListener {
999
	}
941 1000

  
1001
	private class OwnMapContextListener
1002
	implements ViewPortListener, LegendListener, LayerCollectionListener {
1003

  
942 1004
		public void extentChanged(ExtentEvent e) {
943 1005
			if (!b_viewOriginatedUpdate && (getTypeScale() == AUTOMATICO)) {
944 1006
				if (getView()!=null) {
......
980 1042
				invalidateLayout();
981 1043
				// the view should also receive the event and update automatically
982 1044
				b_fframeOriginatedUpdate = false;
983
			}			
1045
			}
984 1046
		}
985
		
1047

  
986 1048
		public void legendChanged(final LegendChangedEvent e) {
987 1049
			conditionalRedraw();
1050
			if (toc!=null) {
1051
				toc.refresh();
1052
			}
988 1053
		}
989 1054

  
990 1055
		public void layerAdded(final LayerCollectionEvent e) {
991 1056
			conditionalRedraw();
1057
			if (toc!=null) {
1058
				toc.refresh();
1059
			}
992 1060
		}
993
		
1061

  
994 1062
		public void layerMoved(final LayerPositionEvent e) {
995 1063
			conditionalRedraw();
1064
			if (toc!=null) {
1065
				toc.refresh();
1066
			}
996 1067
		}
997
		
1068

  
998 1069
		public void layerRemoved(final LayerCollectionEvent e) {
999 1070
			conditionalRedraw();
1071
			if (toc!=null) {
1072
				toc.refresh();
1073
			}
1000 1074
		}
1001 1075

  
1002 1076
		public void layerAdding(LayerCollectionEvent e)
1003 1077
				throws CancelationException {
1004 1078
			// nothing neededO
1005
			
1079

  
1006 1080
		}
1007 1081

  
1008 1082
		public void layerMoving(LayerPositionEvent e)
1009 1083
				throws CancelationException {
1010 1084
			// nothing needed
1011
			
1085

  
1012 1086
		}
1013 1087

  
1014 1088
		public void layerRemoving(LayerCollectionEvent e)
1015 1089
				throws CancelationException {
1016 1090
			// nothing needed
1017
			
1091

  
1018 1092
		}
1019 1093

  
1020 1094
		public void visibilityChanged(LayerCollectionEvent e)
1021 1095
				throws CancelationException {
1022 1096
			conditionalRedraw();
1097
			toc.refresh();
1023 1098
		}
1024
    	
1025
    }
1026 1099

  
1027
	public void refresh() {
1028
        // FIXME: what should we do here? do we really need to invalidate cache??
1029
    	b_validCache = false;		
1030 1100
	}
1031
    
1032
    protected void resetListeners() {
1033
		if (this.getMapContext()!=null) {
1034
			clearOwnListeners(this.getMapContext());
1035
		}
1036
		if (this.getView()!=null && this.getView().getMapContext()!=null) {
1037
			clearViewListeners(this.getView().getMapContext());
1038
		}
1039
    	setListeners();
1040
    }
1041
    
1042
    protected void setListeners() {
1043
    	if (getView()!=null) {
1044
    		if (linked) {
1045
    			getView().getMapContext().addLayerListener(viewDocListener);
1046
    			getView().getMapContext().getLayers().addLayerCollectionListener(viewDocListener);
1047
    		}
1048
    		if (getTypeScale()==IFFrameUseFMap.AUTOMATICO) {
1049
    			getView().getMapContext().getViewPort().addViewPortListener(viewDocListener);
1050
    		}
1051
    	}
1052
    	if (getMapContext()!=null) {
1053
    		getMapContext().addLayerListener(ownMapContextListener);
1054
    		getMapContext().getLayers().addLayerCollectionListener(ownMapContextListener);
1055
    		getMapContext().getViewPort().addViewPortListener(ownMapContextListener);
1056
    	}
1057
    }
1058
    protected void clearOwnListeners(MapContext mapContext) {
1059
    	mapContext.removeLayerListener(ownMapContextListener);
1060
    	mapContext.getViewPort().removeViewPortListener(ownMapContextListener);
1061
    	mapContext.getLayers().removeLayerCollectionListener(ownMapContextListener);
1062
    }
1063
    protected void clearViewListeners(MapContext mapContext) {
1064
    	mapContext.removeLayerListener(viewDocListener);
1065
    	mapContext.getViewPort().removeViewPortListener(viewDocListener);
1066
    	mapContext.getLayers().removeLayerCollectionListener(viewDocListener);
1067
    }
1068
    
1069
    /*
1070
     * (non-Javadoc)
1071
     * @see org.gvsig.tools.dispose.Disposable#dispose()
1072
     */
1073
	public void dispose() {
1074
		try {
1075
			if (this.getMapContext()!=null) {
1076
				clearOwnListeners(this.getMapContext());
1077
			}
1078
			if (this.getView()!=null && this.getView().getMapContext()!=null) {
1079
				clearViewListeners(this.getView().getMapContext());
1080
			}
1081
		}
1082
		catch (Exception ex) {}
1083
		this.viewDocument = null;
1084
		this.mapContext = null;
1085
	}
1086
    
1087 1101
}

Also available in: Unified diff