Revision 31277 branches/v2_0_0_prep/extensions/org.gvsig.annotation/src/main/java/org/gvsig/annotation/impl/DefaultAnnotationManager.java

View differences:

DefaultAnnotationManager.java
1 1
package org.gvsig.annotation.impl;
2 2

  
3 3
import java.awt.Rectangle;
4
import java.io.File;
4
import java.io.IOException;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.Map;
8
import java.util.Set;
5 9

  
6
import org.cresques.cts.IProjection;
7 10
import org.gvsig.andami.messages.NotificationManager;
8 11
import org.gvsig.annotation.AnnotationLocator;
9 12
import org.gvsig.annotation.AnnotationManager;
10 13
import org.gvsig.annotation.AnnotationPreferences;
11 14
import org.gvsig.fmap.dal.DALLocator;
12 15
import org.gvsig.fmap.dal.DataManager;
16
import org.gvsig.fmap.dal.DataServerExplorer;
13 17
import org.gvsig.fmap.dal.exception.DataException;
14 18
import org.gvsig.fmap.dal.exception.ReadException;
15 19
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
......
23 27
import org.gvsig.fmap.dal.feature.FeatureStore;
24 28
import org.gvsig.fmap.dal.feature.FeatureType;
25 29
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
26
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
27
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
28
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters;
29 30
import org.gvsig.fmap.geom.Geometry;
30 31
import org.gvsig.fmap.geom.GeometryLocator;
31 32
import org.gvsig.fmap.geom.GeometryManager;
32 33
import org.gvsig.fmap.geom.exception.CreateGeometryException;
33
import org.gvsig.fmap.mapcontext.MapContextLocator;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
35 34
import org.gvsig.tools.dataTypes.DataTypes;
36 35

  
37 36
public class DefaultAnnotationManager implements AnnotationManager{
38 37
	private DataManager dataManager = DALLocator.getDataManager();
39 38
	private AnnotationPreferences preferences=null;
40
	private SymbolManager symbolManager = null; 
39
	private FeatureAttributeDescriptor[] fieldsNames;
41 40
	private long rowCount;
41
	private int posText;
42
	private int posTypeFont;
43
	private int posStyleFont;
44
	private int posColor;
45
	private int posHeight;
46
	private int posRotate;
47
		
48
	/**
49
	 * Initialize set preference values
50
	 */
51
	private void InitializePreferences(){
52
		//logger initialized
53
		//Default mapped columns
54
		posText = getPreferences().getMappedNumColumnText();
55
        posTypeFont = getPreferences().getMappedColumnNumHeight();
56
        posStyleFont = getPreferences().getMappedColumnNumStyleFont();
57
        posColor = getPreferences().getMappedNumColumnColor();
58
        posHeight = getPreferences().getMappedColumnNumHeight();
59
        posRotate = getPreferences().getMappedColumnNumRotate();	
60
	}
42 61
	
43
	public FeatureStore InitializeAnnotationStore(File newFile,IProjection SRS) throws ValidateDataParametersException, DataException{
44
		 //Setting the New File Path
45
       FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) dataManager.createServerExplorerParameters(FilesystemServerExplorer.NAME);
46
	    explorerParams.setRoot(newFile.getParent());
47
	  
48
		//Create new Shape file with the right fields
49
	    FilesystemServerExplorer explorer = (FilesystemServerExplorer) dataManager.createServerExplorer(explorerParams);
50
		NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(newFile);
51
		EditableFeatureType featureType = (EditableFeatureType)newParams.getDefaultFeatureType();
52
          
53
       //!TODO Espacio Limitado para almacenar Strings ??, EStablecido de antemano cuando no se lo que va a ocupar.
54
  		featureType.add("TEXT",DataTypes.STRING,100);
55
  		featureType.add("TYPEFONT",DataTypes.STRING,100);
56
  		featureType.add("STYLEFONT",DataTypes.STRING,100);
62
	/***************************
63
	 * CREATE METHODS	*
64
	 ***************************/
65
	/**
66
	 * Create Feature annotation Type
67
	 * @param params 
68
	 */
69
	private EditableFeatureType createFeatureType(NewFeatureStoreParameters params){
70
		EditableFeatureType featureType = (EditableFeatureType)params.getDefaultFeatureType();
71
		
72
		// ANNOTATION TEXT
73
  		if (posText == -1)
74
  			featureType.add("TEXT",DataTypes.STRING, 100); //Default text size
75
  		else{
76
  			featureType.add("TEXT",DataTypes.STRING, fieldsNames[posText].getSize()); 
77
  		}
78
  		featureType.add("TYPEFONT",DataTypes.STRING,50);
79
  		featureType.add("STYLEFONT",DataTypes.STRING,15);
57 80
  		featureType.add("COLOR",DataTypes.INT);
58 81
  		featureType.add("HEIGHT",DataTypes.INT);
59 82
  		featureType.add("ROTATE",DataTypes.INT);
60 83
  		featureType.add("GEOMETRY", DataTypes.GEOMETRY).setGeometryType(Geometry.TYPES.POINT);
61
  			
62
		newParams.setDefaultFeatureType(featureType);
63
		if (SRS!=null){
64
			((SHPNewStoreParameters)newParams).setSRS(SRS);
65
		}
66
	    //TODO YA DEBERIA DE ESCRIBIR CON SOLO LA SIGUIENTE INSTRUCCI?N
67
	    explorer.add(newParams, true);
68
	    
69
	    //TODO Cambiar cuando se arregle la escritura de shapes en la 2.0
70
	    FeatureStore annotationStore = (FeatureStore) dataManager.createStore(newParams);
84
  		return featureType;
85
	}
86
	
87
	/**
88
	 * Create new annotation store.
89
	 */
90
	private FeatureStore createEmptyAnnotationStore(FeatureStore origStore, NewFeatureStoreParameters params, DataServerExplorer explorer) throws ValidateDataParametersException, DataException, IOException{
91
		FeatureSet origFeatures = origStore.getFeatureSet();
92
		FeatureType type = origFeatures.getDefaultFeatureType();
93
		fieldsNames = type.getAttributeDescriptors();
94
		EditableFeatureType featureType = createFeatureType(params);
95
	    params.setDefaultFeatureType(featureType);
96
		 //Adding new basic annotation feature type.
97
	    explorer.add(params, true);
98
	    FeatureStore annotationStore = (FeatureStore) dataManager.createStore(params);
71 99
	    annotationStore.edit(FeatureStore.MODE_APPEND);
72 100
	    annotationStore.finishEditing();
73 101
	    return annotationStore;
74 102
	}
75
	public void create(FeatureStore origStore, FeatureStore annotationStore, String duplicate) throws DataException, ValidateDataParametersException, CreateGeometryException {
76
		try {
77
			FeatureSelection selection = origStore.getFeatureSelection();
78
			rowCount = selection.getSize();
79
			if(rowCount==0){
80
				rowCount=origStore.getFeatureCount();
103
	
104
	/**
105
	 * Fill the annotation store with the original store.
106
	 * @throws DataException
107
	 * @throws CreateGeometryException 
108
	 */
109
	private void createFillAnnotationStore(FeatureStore origStore,FeatureStore annotationStore, String duplicate) throws DataException, CreateGeometryException{
110
		if (posText == -1){
111
  			//throws new DataException();
112
			return;
113
		}else{
114
			try {
115
				FeatureSelection selection = origStore.getFeatureSelection();
116
				rowCount = selection.getSize();
117
				if(rowCount==0){
118
					rowCount=origStore.getFeatureCount();
119
				}
120
			} catch (ReadException e) {
121
				NotificationManager.addError(e);
122
			}catch (DataException e) {
123
				NotificationManager.addError(e);
81 124
			}
82
		} catch (ReadException e) {
83
			NotificationManager.addError(e);
125
			annotationStore.edit(FeatureStore.MODE_APPEND);
126
			if (duplicate.equals("duplicate.none")) 
127
				createNone(origStore,annotationStore);
128
			else if (duplicate.equals("centered")) 
129
				createCentered(origStore,annotationStore);
130
      		annotationStore.finishEditing();
84 131
		}
85
   		catch (DataException e) {
86
			NotificationManager.addError(e);
87
		}
132
	}
133
		
134
	/**
135
	 * CREATE NONE: Take all distinct and equals values form the original layer and generates a
136
	 *  new tag for each one. 
137
	 * @throws DataException 
138
	 * @throws CreateGeometryException 
139
	 */
140
	private void createNone(FeatureStore origStore, FeatureStore annotationStore) throws DataException, CreateGeometryException{
141
		//Take the original attribute names and types from features
142
		FeatureSet origFeatures = origStore.getFeatureSet();
143
		DisposableIterator it = origFeatures.iterator();
144
		FeatureType type = origFeatures.getDefaultFeatureType();
145
		fieldsNames = type.getAttributeDescriptors();
146
		for (int i = 0; i < rowCount; i++) {
147
			Feature origFeature = (Feature)it.next();
148
				EditableFeature feature = annotationStore.createNewFeature();
149
				feature=fillNewFeature(feature,origFeature);	   			
150
				annotationStore.insert(feature);	
151
			}
152
	}
88 153
	
89
   		int posText = getPreferences().getMappedNumColumnText();
90
        int posTypeFont = getPreferences().getMappedColumnNumHeight();
91
        int posStyleFont = getPreferences().getMappedColumnNumStyleFont();
92
        int posColor = getPreferences().getMappedNumColumnColor();
93
        int posHeight = getPreferences().getMappedColumnNumHeight();
94
        int posRotate = getPreferences().getMappedColumnNumRotate();
95
      
96
         /**
97
          * NONE: Take all distinct and equals values form the original layer and generates a new tag for each one. 
98
          */
99
      	if (duplicate.equals("duplicate.none")) {
100
      		FeatureSet origFeatures = origStore.getFeatureSet();
101
      		DisposableIterator it = origFeatures.iterator();
102
	   		FeatureType type = origFeatures.getDefaultFeatureType();
103
	   		FeatureAttributeDescriptor[] fieldsNames = type.getAttributeDescriptors();
104
	   		annotationStore.edit(FeatureStore.MODE_APPEND);
105
	   		for (int i = 0; i < rowCount; i++) {
106
	   			EditableFeature feature = annotationStore.createNewFeature();
107
	   			Feature origFeature = (Feature)it.next();
108
	      		
109
	   			// ANNOTATION TEXT
110
	      		if (posText == -1)
111
	      			feature.setString("TEXT",getPreferences().getText());
112
	      		else
113
	      			feature.setString("TEXT", origFeature.getString(fieldsNames[posText].getName()));
114
	      		// TYPE FONT
115
	      		if (posTypeFont == -1)
116
	      			feature.setString("TYPEFONT",getPreferences().getTypeFont());
117
	      		else
118
	      			feature.setString("TYPEFONT",origFeature.getString(fieldsNames[posTypeFont].getName()));
119
                // STYLE FONT
120
	      		if (posStyleFont == -1)
121
	      			feature.setInt("STYLEFONT",getPreferences().getStyleFont());
122
	      		else
123
	      			feature.setInt("STYLEFONT",origFeature.getInt(fieldsNames[posStyleFont].getName()));
124
	      		// COLOR
125
	      		if (posColor==-1) 
126
	      			feature.setInt("COLOR", getPreferences().getColor());
127
	      		else
128
	      			feature.setInt("COLOR",origFeature.getInt(fieldsNames[posColor].getName()));
129
	      		// HEIGHT
130
	      		if (posHeight==-1)
131
	      			feature.setInt("HEIGHT", getPreferences().getHeight());
132
	      		else
133
	      			feature.setInt("HEIGHT",origFeature.getInt(fieldsNames[posHeight].getName()));
134
	      		// ROTATE
135
	      		if (posRotate == -1)
136
	      			feature.setInt("ROTATE", getPreferences().getRotate());
137
	      		else
138
	      			feature.setInt("ROTATE",origFeature.getInt(fieldsNames[posRotate].getName()));
139
	      		
140
	      		Geometry origGeometry = origFeature.getDefaultGeometry();
141
	      		Rectangle rect = origGeometry.getBounds();
142
	      		GeometryManager gf = GeometryLocator.getGeometryManager();
143
	      		Geometry geom = gf.createPoint(rect.getCenterX(),rect.getCenterY(), Geometry.SUBTYPES.GEOM2D);
144
      			      		
145
	      		//GEOMETRY INTERNAL POINT
146
               	feature.set("GEOMETRY", geom);                	
147
               	annotationStore.insert(feature);
154
	/**
155
	 * CREATE CENTERED: Take the not repeated values from the selected original layer chosen attribute. 
156
	 * Generates a layer with the distinct value selection. 
157
	 * TODO Take a look to use less memory.
158
	 * @throws DataException 
159
	 * @throws CreateGeometryException 
160
	 */
161
	@SuppressWarnings("unchecked")
162
	private void createCentered(FeatureStore origStore, FeatureStore annotationStore) throws DataException, CreateGeometryException{
163
		Map <String,EditableFeature>namesMapped=new HashMap<String,EditableFeature>();
164
		FeatureSet origFeatures = origStore.getFeatureSet();
165
		DisposableIterator it = origFeatures.iterator();
166
		FeatureType type = origFeatures.getDefaultFeatureType();
167
		fieldsNames = type.getAttributeDescriptors();
168
		for (int i = 0; i < rowCount; i++) {
169
				Feature origFeature = (Feature)it.next();
170
				String annotationText = (String) origFeature.get(fieldsNames[posText].getName()).toString();
171
			if (!namesMapped.containsKey(annotationText)) {
172
				EditableFeature feature = annotationStore.createNewFeature();
173
					feature=fillNewFeature(feature,origFeature);
174
					namesMapped.put(annotationText,feature);
175
			}else{
176
				EditableFeature oldFeature = namesMapped.get(annotationText);
177
				Geometry oldGeometry=oldFeature.getDefaultGeometry();
178
				Geometry origGeometry = origFeature.getDefaultGeometry();
179
				Rectangle oldRect = oldGeometry.getBounds();
180
		  		Rectangle origRect = origGeometry.getBounds();
181
		  		Rectangle resul = (Rectangle) origRect.createUnion(oldRect);
182
		  		GeometryManager gf = GeometryLocator.getGeometryManager();
183
		  		Geometry geom = gf.createPoint(resul.getCenterX(),resul.getCenterY(), Geometry.SUBTYPES.GEOM2D);
184
				//GEOMETRY INTERNAL POINT
185
		       	oldFeature.set("GEOMETRY", geom); 
186
		       	//remove previous duplicated features from mapped attributes list
187
		       	namesMapped.remove(annotationText);
188
		       	namesMapped.put(annotationText, oldFeature);
148 189
			}
149
	   		annotationStore.finishEditing();
150
        }
151
      	/**
152
		 * CENTERED: Take the not repeated values from the selected original layer chosen attribute. Generates a layer with the distinct value selection. 
190
		}
191
		//Inserting to the store the selected Features only...
192
		Set<?> entries = namesMapped.entrySet();
193
		Iterator<?> mapIterator = entries.iterator();
194
		while (mapIterator.hasNext()) {
195
		     Map.Entry entry = (Map.Entry) mapIterator.next();
196
		     annotationStore.insert((EditableFeature) entry.getValue());	
197
		}			
198
		/**
199
		 * Write the file annotation features launching a new Thread TASK 
153 200
		 */
154
      	else if (duplicate.equals("centered")) {
155
      			
156
        }
157
		/**
158
	    * Write the file annotation features launching a new Thread TASK 
159
	    */
160
	    //PluginServices.cancelableBackgroundExecution((IMonitorableTask) new AnnotationCreateTask(origStore, annotationStore, duplicate));
161
	    
201
	    //PluginServices.cancelableBackgroundExecution((IMonitorableTask) new AnnotationCreateTask(origStore, annotationStore, duplicate));    
162 202
	}
163 203

  
164
	public void open() {
165
			
204
	/**
205
	 * Create annotation Layer from Empty annotation Store and original Store
206
	 * @throws IOException 
207
	 */
208
	public void create(FeatureStore origStore, DataServerExplorer explorer, NewFeatureStoreParameters parameters, String duplicate) throws DataException, ValidateDataParametersException, CreateGeometryException, IOException {
209
		//this.explorer=explorer;
210
		//this.params=parameters;
211
		//this.duplicate=duplicate;
212
		//this.origStore=origStore;
213
		InitializePreferences();
214
		FeatureStore annotationStore = createEmptyAnnotationStore(origStore,parameters, explorer);
215
		createFillAnnotationStore(origStore, annotationStore, duplicate);
166 216
	}
217

  
218
	/**
219
	 * Fill a new feature with values from original file feature. 
220
	 * @param feature
221
	 * @param origFeature
222
	 * @return feature edited
223
	 * @throws CreateGeometryException
224
	 */
225
	private EditableFeature fillNewFeature(EditableFeature feature, Feature origFeature) throws CreateGeometryException{
226
		// ANNOTATION TEXT
227
  		if (posText == -1)
228
  			feature.setString("TEXT",getPreferences().getText());
229
  		else
230
  			feature.setString("TEXT", origFeature.getString(fieldsNames[posText].getName()));
231
  		
232
  		// TYPE FONT
233
  		if (posTypeFont == -1)
234
  			feature.setString("TYPEFONT",getPreferences().getTypeFont());
235
  		else
236
  			feature.setString("TYPEFONT",origFeature.getString(fieldsNames[posTypeFont].getName()));
237
        
238
  		// STYLE FONT
239
  		if (posStyleFont == -1)
240
  			feature.setInt("STYLEFONT",getPreferences().getStyleFont());
241
  		else
242
  			feature.setInt("STYLEFONT",origFeature.getInt(fieldsNames[posStyleFont].getName()));
243
  		
244
  		// COLOR
245
  		if (posColor==-1) 
246
  			feature.setInt("COLOR", getPreferences().getColor());
247
  		else
248
  			feature.setInt("COLOR",origFeature.getInt(fieldsNames[posColor].getName()));
249
  		
250
  		// HEIGHT
251
  		if (posHeight==-1)
252
  			feature.setInt("HEIGHT", getPreferences().getHeight());
253
  		else
254
  			feature.setInt("HEIGHT",origFeature.getInt(fieldsNames[posHeight].getName()));
255
  		
256
  		// ROTATE
257
  		if (posRotate == -1)
258
  			feature.setInt("ROTATE", getPreferences().getRotate());
259
  		else
260
  			feature.setInt("ROTATE",origFeature.getInt(fieldsNames[posRotate].getName()));
261
  		
262
  		Geometry origGeometry = origFeature.getDefaultGeometry();
263
  		Rectangle rect = origGeometry.getBounds();
264
  		GeometryManager gf = GeometryLocator.getGeometryManager();
265
  		Geometry geom = gf.createPoint(rect.getCenterX(),rect.getCenterY(), Geometry.SUBTYPES.GEOM2D);
266
			      		
267
  		//GEOMETRY INTERNAL POINT
268
       	feature.set("GEOMETRY", geom); 
269
       	return feature;
270
	}
167 271
	
168
	public AnnotationPreferences getPreferences() {
272
	
273
	private AnnotationPreferences getPreferences() {
169 274
		if ( preferences==null ) {
170 275
			preferences = AnnotationLocator.getAnnotationPreferences();
171 276
		}
172 277
		return preferences;
173 278
	}
174 279
	
175
	public SymbolManager getSymbolManager(){
176
		if(symbolManager==null){
177
			symbolManager = MapContextLocator.getSymbolManager();
178
		}
179
		return symbolManager;
280
	
281
	public void open() {
282
		// TODO Auto-generated method stub
180 283
	}
181 284
}

Also available in: Unified diff