svn-gvsig-desktop / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / layers / Annotation_LayerFactory.java @ 28605
History | View | Annotate | Download (3.75 KB)
1 |
package com.iver.cit.gvsig.fmap.layers; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import org.cresques.cts.IProjection; |
7 |
|
8 |
import com.hardcode.driverManager.Driver; |
9 |
import com.hardcode.driverManager.DriverLoadException; |
10 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
11 |
import com.iver.andami.PluginServices; |
12 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
13 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
14 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
15 |
import com.iver.cit.gvsig.fmap.core.FShape; |
16 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
17 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
18 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
19 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
20 |
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend; |
21 |
|
22 |
public class Annotation_LayerFactory { |
23 |
public static FieldDescription[] getDefaultFieldDescriptions() { |
24 |
String[] fieldNames = { |
25 |
Annotation_Mapping.TEXT, |
26 |
Annotation_Mapping.TYPEFONT, |
27 |
Annotation_Mapping.STYLEFONT, |
28 |
Annotation_Mapping.COLOR, |
29 |
Annotation_Mapping.HEIGHT, |
30 |
Annotation_Mapping.ROTATE}; |
31 |
int[] fieldLength = { |
32 |
150, // text |
33 |
25, // typefont |
34 |
-1, // font style |
35 |
10, // color |
36 |
-1, // height |
37 |
-1 // rotate |
38 |
}; |
39 |
|
40 |
FieldDescription[] fieldsDescriptions = new FieldDescription[fieldNames.length]; |
41 |
FieldDescription desc; |
42 |
|
43 |
for (int i=0; i<fieldNames.length; i++) { |
44 |
desc = new FieldDescription();
|
45 |
desc.setFieldName(fieldNames[i]); |
46 |
if (fieldLength[i]!=-1) { |
47 |
desc.setFieldLength(fieldLength[i]); |
48 |
} |
49 |
desc.setFieldType(Annotation_Mapping.getType(fieldNames[i])); |
50 |
fieldsDescriptions[i] = desc; |
51 |
} |
52 |
return fieldsDescriptions;
|
53 |
} |
54 |
|
55 |
public static void createEmptyLayer(File file, IProjection proj) throws DriverLoadException, InitializeWriterException, StartWriterVisitorException, StopWriterVisitorException, LoadLayerException { |
56 |
if (!file.exists())
|
57 |
{ |
58 |
SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
59 |
lyrDef.setFieldsDesc(getDefaultFieldDescriptions()); |
60 |
lyrDef.setFile(file); |
61 |
lyrDef.setName(file.getName()); |
62 |
lyrDef.setShapeType(FShape.POINT); |
63 |
ShpWriter writer= (ShpWriter)LayerFactory.getWM().getWriter("Shape Writer");
|
64 |
writer.setFile(file); |
65 |
writer.initialize(lyrDef); |
66 |
writer.preProcess(); |
67 |
writer.postProcess(); |
68 |
int pos = file.getPath().toLowerCase().lastIndexOf(".shp"); |
69 |
File gvaFile = null; |
70 |
if (pos!=-1) { |
71 |
gvaFile = new File(file.getPath().substring(0, pos)+".gva"); |
72 |
} |
73 |
else {
|
74 |
gvaFile = new File(file.getPath()+".gva"); |
75 |
} |
76 |
try {
|
77 |
gvaFile.createNewFile(); |
78 |
} catch (IOException e) { |
79 |
// TODO Auto-generated catch block
|
80 |
e.printStackTrace(); |
81 |
} |
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
public static Annotation_Layer createLayer(String layerName, File file, IProjection proj, int units) { |
87 |
if (file == null) { |
88 |
return null; |
89 |
} |
90 |
|
91 |
FLayer lyr = null;
|
92 |
String driverName = "gvSIG shp driver"; |
93 |
Driver driver = null; |
94 |
|
95 |
try {
|
96 |
driver = LayerFactory.getDM().getDriver(driverName); |
97 |
} catch (DriverLoadException e) {
|
98 |
PluginServices.getLogger().error(e.getMessage(),e); |
99 |
return null; |
100 |
} |
101 |
|
102 |
lyr = LayerFactory.createLayer(layerName, (VectorialFileDriver) driver, |
103 |
file, proj); |
104 |
|
105 |
Annotation_Layer al = new Annotation_Layer();
|
106 |
LayerListener[] layerListeners = lyr.getLayerListeners();
|
107 |
|
108 |
for (int i = 0; i < layerListeners.length; i++) { |
109 |
al.addLayerListener(layerListeners[i]); |
110 |
} |
111 |
al.setSource(((FLyrVect) lyr).getSource()); |
112 |
al.setProjection(lyr.getProjection()); |
113 |
al.setName(layerName); |
114 |
|
115 |
try {
|
116 |
Annotation_Mapping.addAnnotationMapping(al); |
117 |
} catch (Exception e) { |
118 |
PluginServices.getLogger().error(e.getMessage(),e); |
119 |
return null; |
120 |
} |
121 |
((Annotation_Legend)al.getLegend()).setUnits(units); |
122 |
return al;
|
123 |
} |
124 |
} |