Revision 11613 branches/v10/extensions/extAnnotations/src/com/iver/cit/gvsig/project/documents/gui/Annotation_Create.java

View differences:

Annotation_Create.java
1 1
package com.iver.cit.gvsig.project.documents.gui;
2 2

  
3
import java.awt.Component;
4
import java.io.File;
5
import java.io.IOException;
6

  
7
import javax.swing.JFileChooser;
8
import javax.swing.JOptionPane;
9

  
10
import jwizardcomponent.FinishAction;
11
import jwizardcomponent.JWizardComponents;
12

  
3 13
import com.hardcode.driverManager.Driver;
4

  
5 14
import com.hardcode.gdbms.engine.data.driver.DriverException;
6

  
7 15
import com.iver.andami.PluginServices;
8

  
9 16
import com.iver.cit.gvsig.fmap.MapContext;
10 17
import com.iver.cit.gvsig.fmap.core.FShape;
11 18
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
......
20 27
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
21 28
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
22 29
import com.iver.cit.gvsig.gui.panels.annotation.ConfigureLabel;
23

  
24 30
import com.iver.utiles.SimpleFileFilter;
25 31

  
26
import jwizardcomponent.FinishAction;
27
import jwizardcomponent.JWizardComponents;
28

  
29
import java.awt.Component;
30

  
31
import java.io.File;
32
import java.io.IOException;
33

  
34
import javax.swing.JFileChooser;
35
import javax.swing.JOptionPane;
36

  
37

  
38 32
/**
39 33
 * Dialog to create a new annotation layer.
40
 *
34
 * 
41 35
 * @author Vicente Caballero Navarro
42 36
 */
43 37
public class Annotation_Create extends FinishAction {
44
    private JWizardComponents myWizardComponents;
45
    private MapContext map;
46
    private Annotation_Layer layerAnnotation;
38
	private JWizardComponents myWizardComponents;
47 39

  
48
    public Annotation_Create(JWizardComponents wizardComponents,
49
        MapContext map, Annotation_Layer layerAnnotation) {
50
        super(wizardComponents);
51
        this.map = map;
52
        this.layerAnnotation = layerAnnotation;
53
        myWizardComponents = wizardComponents;
54
    }
40
	private MapContext map;
55 41

  
56
    public void performAction() {
57
        myWizardComponents.getFinishButton().setEnabled(false);
42
	private Annotation_Layer layerAnnotation;
58 43

  
59
        Annotation_FieldSelect panel1 = (Annotation_FieldSelect) myWizardComponents.getWizardPanel(0);
60
        Annotation_ConfigureLabel panel2 = (Annotation_ConfigureLabel) myWizardComponents.getWizardPanel(1);
44
	public Annotation_Create(JWizardComponents wizardComponents,
45
			MapContext map, Annotation_Layer layerAnnotation) {
46
		super(wizardComponents);
47
		this.map = map;
48
		this.layerAnnotation = layerAnnotation;
49
		myWizardComponents = wizardComponents;
50
	}
61 51

  
62
        SelectableDataSource source;
63
        Annotation_Mapping mapping = new Annotation_Mapping();
52
	public void performAction() {
53
		myWizardComponents.getFinishButton().setEnabled(false);
64 54

  
65
        try {
66
            source = this.layerAnnotation.getRecordset();
55
		Annotation_FieldSelect panel1 = (Annotation_FieldSelect) myWizardComponents
56
				.getWizardPanel(0);
57
		Annotation_ConfigureLabel panel2 = (Annotation_ConfigureLabel) myWizardComponents
58
				.getWizardPanel(1);
67 59

  
68
            mapping.setColumnText(source.getFieldIndexByName(panel1.getField()));
60
		SelectableDataSource source;
61
		Annotation_Mapping mapping = new Annotation_Mapping();
69 62

  
70
            if (!panel2.getAngleFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
71
                mapping.setColumnRotate(source.getFieldIndexByName(
72
                        panel2.getAngleFieldName()));
73
            }
63
		try {
64
			source = this.layerAnnotation.getRecordset();
74 65

  
75
            if (!panel2.getColorFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
76
                mapping.setColumnColor(source.getFieldIndexByName(
77
                        panel2.getColorFieldName()));
78
            }
66
			mapping
67
					.setColumnText(source
68
							.getFieldIndexByName(panel1.getField()));
79 69

  
80
            if (!panel2.getSizeFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
81
                mapping.setColumnHeight(source.getFieldIndexByName(
82
                        panel2.getSizeFieldName()));
83
            }
70
			if (!panel2.getAngleFieldName().equals(
71
					ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
72
				mapping.setColumnRotate(source.getFieldIndexByName(panel2
73
						.getAngleFieldName()));
74
			}
84 75

  
85
            this.layerAnnotation.setInPixels(panel2.sizeUnitsInPixels());
76
			if (!panel2.getColorFieldName().equals(
77
					ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
78
				mapping.setColumnColor(source.getFieldIndexByName(panel2
79
						.getColorFieldName()));
80
			}
86 81

  
87
            if (!panel2.getFontFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
88
                mapping.setColumnTypeFont(source.getFieldIndexByName(
89
                        panel2.getFontFieldName()));
90
            }
91
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
92
            e.printStackTrace();
82
			if (!panel2.getSizeFieldName().equals(
83
					ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
84
				mapping.setColumnHeight(source.getFieldIndexByName(panel2
85
						.getSizeFieldName()));
86
			}
93 87

  
94
            return;
95
        } catch (DriverException e) {
96
            e.printStackTrace();
97
        }
88
			this.layerAnnotation.setInPixels(panel2.sizeUnitsInPixels());
98 89

  
99
        //		this.layerAnnotation.setName(panel1.getNewLayerName());
100
        this.layerAnnotation.setMapping(mapping);
90
			if (!panel2.getFontFieldName().equals(
91
					ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
92
				mapping.setColumnTypeFont(source.getFieldIndexByName(panel2
93
						.getFontFieldName()));
94
			}
95
		} catch (com.iver.cit.gvsig.fmap.DriverException e) {
96
			e.printStackTrace();
101 97

  
102
        try {
103
            saveToShp(map, this.layerAnnotation);
104
        } catch (EditionException e) {
105
            e.printStackTrace();
106
        } catch (DriverIOException e) {
107
            e.printStackTrace();
108
        }
98
			return;
99
		} catch (DriverException e) {
100
			e.printStackTrace();
101
		}
109 102

  
110
        this.myWizardComponents.getCancelAction().performAction();
111
    }
103
		// this.layerAnnotation.setName(panel1.getNewLayerName());
104
		this.layerAnnotation.setMapping(mapping);
112 105

  
113
    public void saveToShp(MapContext mapContext, Annotation_Layer layer)
114
        throws EditionException, DriverIOException {
115
        try {
116
            JFileChooser jfc = new JFileChooser();
117
            SimpleFileFilter filterShp = new SimpleFileFilter("shp",
118
                    PluginServices.getText(this, "shp_files"));
119
            jfc.setFileFilter(filterShp);
106
		try {
107
			saveToShp(map, this.layerAnnotation, panel1.getDuplicate());
108
		} catch (EditionException e) {
109
			e.printStackTrace();
110
		} catch (DriverIOException e) {
111
			e.printStackTrace();
112
		}
120 113

  
121
            if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
122
                File newFile = jfc.getSelectedFile();
123
                String path = newFile.getAbsolutePath();
114
		this.myWizardComponents.getCancelAction().performAction();
115
	}
124 116

  
125
                if (newFile.exists()) {
126
                    int resp = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
127
                            PluginServices.getText(this,
128
                                "fichero_ya_existe_seguro_desea_guardarlo"),
129
                            PluginServices.getText(this, "guardar"),
130
                            JOptionPane.YES_NO_OPTION);
117
	public void saveToShp(MapContext mapContext, Annotation_Layer layer,
118
			String duplicate) throws EditionException, DriverIOException {
119
		try {
120
			JFileChooser jfc = new JFileChooser();
121
			SimpleFileFilter filterShp = new SimpleFileFilter("shp",
122
					PluginServices.getText(this, "shp_files"));
123
			jfc.setFileFilter(filterShp);
131 124

  
132
                    if (resp != JOptionPane.YES_OPTION) {
133
                        return;
134
                    }
135
                }
125
			if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
126
				File newFile = jfc.getSelectedFile();
127
				String path = newFile.getAbsolutePath();
136 128

  
137
                if (!(path.toLowerCase().endsWith(".shp"))) {
138
                    path = path + ".shp";
139
                }
129
				if (newFile.exists()) {
130
					int resp = JOptionPane
131
							.showConfirmDialog(
132
									(Component) PluginServices.getMainFrame(),
133
									PluginServices
134
											.getText(this,
135
													"fichero_ya_existe_seguro_desea_guardarlo"),
136
									PluginServices.getText(this, "guardar"),
137
									JOptionPane.YES_NO_OPTION);
140 138

  
141
                newFile = new File(path);
139
					if (resp != JOptionPane.YES_OPTION) {
140
						return;
141
					}
142
				}
142 143

  
143
                SelectableDataSource sds = layer.getRecordset();
144
                FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
144
				if (!(path.toLowerCase().endsWith(".shp"))) {
145
					path = path + ".shp";
146
				}
145 147

  
146
                ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter("Shape Writer");
147
                Driver driver = null;
148
				newFile = new File(path);
148 149

  
149
                SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
150
                lyrDefPoint.setFieldsDesc(fieldsDescrip);
150
				SelectableDataSource sds = layer.getRecordset();
151
				FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
151 152

  
152
                File filePoints = new File(path);
153
                lyrDefPoint.setFile(filePoints);
154
                lyrDefPoint.setName(filePoints.getName());
155
                lyrDefPoint.setShapeType(FShape.POINT);
156
                writer.setFile(filePoints);
157
                writer.initialize(lyrDefPoint);
158
                driver = getOpenAnnotationDriver(filePoints);
159
                writeFeatures(mapContext, layer, writer, driver);
160
            }
161
        } catch (DriverException e) {
162
            e.printStackTrace();
163
            throw new EditionException(e);
164
        } catch (IOException e) {
165
            e.printStackTrace();
166
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
167
            e.printStackTrace();
168
        }
169
    }
153
				ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter(
154
						"Shape Writer");
155
				Driver driver = null;
170 156

  
171
    private Driver getOpenAnnotationDriver(File filePoints)
172
        throws IOException {
173
        IndexedShpDriver drv = new IndexedShpDriver();
157
				SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
158
				lyrDefPoint.setFieldsDesc(fieldsDescrip);
174 159

  
175
        if (!filePoints.exists()) {
176
            filePoints.createNewFile();
160
				File filePoints = new File(path);
161
				lyrDefPoint.setFile(filePoints);
162
				lyrDefPoint.setName(filePoints.getName());
163
				lyrDefPoint.setShapeType(FShape.POINT);
164
				writer.setFile(filePoints);
165
				writer.initialize(lyrDefPoint);
166
				driver = getOpenAnnotationDriver(filePoints);
167
				writeFeatures(mapContext, layer, writer, driver, duplicate);
168
			}
169
		} catch (DriverException e) {
170
			e.printStackTrace();
171
			throw new EditionException(e);
172
		} catch (IOException e) {
173
			e.printStackTrace();
174
		} catch (com.iver.cit.gvsig.fmap.DriverException e) {
175
			e.printStackTrace();
176
		}
177
	}
177 178

  
178
            File newFileSHX = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
179
                        ".shx"));
180
            newFileSHX.createNewFile();
179
	private Driver getOpenAnnotationDriver(File filePoints) throws IOException {
180
		IndexedShpDriver drv = new IndexedShpDriver();
181 181

  
182
            File newFileDBF = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
183
                        ".dbf"));
184
            newFileDBF.createNewFile();
185
        }
182
		if (!filePoints.exists()) {
183
			filePoints.createNewFile();
186 184

  
187
        File newFileGVA = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
188
                    ".gva"));
185
			File newFileSHX = new File(filePoints.getAbsolutePath().replaceAll(
186
					"[.]shp", ".shx"));
187
			newFileSHX.createNewFile();
189 188

  
190
        if (!newFileGVA.exists()) {
191
            newFileGVA.createNewFile();
192
        }
189
			File newFileDBF = new File(filePoints.getAbsolutePath().replaceAll(
190
					"[.]shp", ".dbf"));
191
			newFileDBF.createNewFile();
192
		}
193 193

  
194
        drv.open(filePoints);
194
		File newFileGVA = new File(filePoints.getAbsolutePath().replaceAll(
195
				"[.]shp", ".gva"));
195 196

  
196
        return drv;
197
    }
197
		if (!newFileGVA.exists()) {
198
			newFileGVA.createNewFile();
199
		}
198 200

  
199
    private void writeFeatures(MapContext mapContext, Annotation_Layer layer,
200
        IWriter writer, Driver reader)
201
        throws DriverIOException, com.iver.cit.gvsig.fmap.DriverException {
202
        PluginServices.cancelableBackgroundExecution(new Annotation_TaskCreate(
203
                mapContext, layer, writer, reader));
204
    }
201
		drv.open(filePoints);
202

  
203
		return drv;
204
	}
205

  
206
	private void writeFeatures(MapContext mapContext, Annotation_Layer layer,
207
			IWriter writer, Driver reader, String duplicate)
208
			throws DriverIOException, com.iver.cit.gvsig.fmap.DriverException {
209
		PluginServices.cancelableBackgroundExecution(new Annotation_TaskCreate(
210
				mapContext, layer, writer, reader, duplicate));
211
	}
205 212
}

Also available in: Unified diff