Revision 1219 trunk/applications/appgvSIG/src/com/iver/cit/gvsig/ProjectExtension.java

View differences:

ProjectExtension.java
40 40
 */
41 41
package com.iver.cit.gvsig;
42 42

  
43
import java.awt.Component;
44
import java.io.File;
45
import java.io.FileNotFoundException;
46
import java.io.FileReader;
47
import java.io.FileWriter;
48
import java.text.DateFormat;
49
import java.util.ArrayList;
50
import java.util.Date;
51

  
52
import javax.swing.JFileChooser;
53
import javax.swing.JOptionPane;
54

  
55
import org.exolab.castor.xml.MarshalException;
56
import org.exolab.castor.xml.Marshaller;
57
import org.exolab.castor.xml.ValidationException;
58

  
59 43
import com.iver.andami.PluginServices;
60 44
import com.iver.andami.messages.NotificationManager;
61 45
import com.iver.andami.plugins.Extension;
46

  
62 47
import com.iver.cit.gvsig.fmap.DriverException;
63 48
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
64 49
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
......
66 51
import com.iver.cit.gvsig.project.ProjectFactory;
67 52
import com.iver.cit.gvsig.project.ProjectWindow;
68 53
import com.iver.cit.gvsig.project.castor.Project;
54

  
69 55
import com.iver.utiles.GenericFileFilter;
70 56
import com.iver.utiles.XMLEntity;
71 57
import com.iver.utiles.xmlEntity.generate.XmlTag;
72 58

  
59
import org.exolab.castor.xml.MarshalException;
60
import org.exolab.castor.xml.Marshaller;
61
import org.exolab.castor.xml.ValidationException;
62

  
63
import java.awt.Component;
64

  
65
import java.io.File;
66
import java.io.FileNotFoundException;
67
import java.io.FileReader;
68
import java.io.FileWriter;
69

  
70
import java.text.DateFormat;
71

  
72
import java.util.ArrayList;
73
import java.util.Date;
74

  
75
import javax.swing.JFileChooser;
76
import javax.swing.JOptionPane;
77

  
78

  
73 79
/**
74 80
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
75 81
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
......
78 84
 * @author Fernando Gonz?lez Cort?s
79 85
 */
80 86
public class ProjectExtension implements Extension {
81
    private ArrayList tableExtensions = new ArrayList();
82
    private ProjectWindow projectFrame;
83
    private Project p;
87
	private ArrayList tableExtensions = new ArrayList();
88
	private ProjectWindow projectFrame;
89
	private Project p;
84 90
	private boolean modified = false;
85 91

  
86
    /**
87
     * @see com.iver.mdiApp.plugins.Extension#inicializar()
88
     */
89
    public void inicializar() {
90
        p = ProjectFactory.createProject();
91
        p.setName(PluginServices.getText(this, "untitled"));
92
        p.setModified(false);
93
        projectFrame = new ProjectWindow(this);
94
        projectFrame.setProject(p);
95
        showProjectWindow();
96
        PluginServices.getMainFrame().setTitle("gvSIG: " + PluginServices.getText(this,
97
									 "sin_titulo"));
98
        
99
        LayerFactory.setDriversPath(PluginServices.getPluginServices(this).getPluginDirectory().getAbsolutePath() + File.separator + "drivers");
100
    }
92
	/**
93
	 * @see com.iver.mdiApp.plugins.Extension#inicializar()
94
	 */
95
	public void inicializar() {
96
		p = ProjectFactory.createProject();
97
		p.setName(PluginServices.getText(this, "untitled"));
98
		p.setModified(false);
99
		projectFrame = new ProjectWindow(this);
100
		projectFrame.setProject(p);
101
		showProjectWindow();
102
		PluginServices.getMainFrame().setTitle("gvSIG: " +
103
			PluginServices.getText(this, "sin_titulo"));
101 104

  
102
	public void showProjectWindow(){
105
		LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
106
												  .getPluginDirectory()
107
												  .getAbsolutePath() +
108
			File.separator + "drivers");
109
	}
110

  
111
	/**
112
	 * Muestra la ventana con el gestor de proyectos.
113
	 */
114
	public void showProjectWindow() {
103 115
		PluginServices.getMDIManager().addView(projectFrame);
104 116
	}
105 117

  
106
	private void guardar(){
118
	/**
119
	 * Guarda el proyecto actual en disco.
120
	 */
121
	private void guardar() {
107 122
		if (p.getPath() == null) {
108 123
			JFileChooser jfc = new JFileChooser();
109
			jfc.addChoosableFileFilter(new GenericFileFilter("xml", PluginServices.getText(this,"tipo_fichero_proyecto")));
110
			if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
124
			jfc.addChoosableFileFilter(new GenericFileFilter("xml",
125
					PluginServices.getText(this, "tipo_fichero_proyecto")));
126

  
127
			if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
111 128
				escribirProyecto(jfc.getSelectedFile(), p);
112 129
			}
113 130
		} else {
114 131
			escribirProyecto(new File(p.getPath()), p);
115 132
		}
116 133
	}
117
	
118
	private boolean modificado(){
134

  
135
	/**
136
	 * Guarda si el proyecto ha sido modificado.
137
	 *
138
	 * @return True si se ha guardado correctamente.
139
	 */
140
	private boolean modificado() {
119 141
		if (p.isModified()) {
120 142
			int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
121 143
					PluginServices.getText(this, "guardar_cambios"),
......
129 151
				return false;
130 152
			}
131 153
		}
132
		
154

  
133 155
		return true;
134 156
	}
135 157

  
136
    /**
137
     * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
138
     */
139
    public void execute(String actionCommand) {
140
            if (actionCommand.equals("NUEVO")) {
141
            	//Si est? modificado se pregunta si se quiere guardar el anterior
142
				if (!modificado()) return;
158
	/**
159
	 * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
160
	 */
161
	public void execute(String actionCommand) {
162
		if (actionCommand.equals("NUEVO")) {
163
			//Si est? modificado se pregunta si se quiere guardar el anterior
164
			if (!modificado()) {
165
				return;
166
			}
143 167

  
168
			PluginServices.getMDIManager().closeAllViews();
169
			p = ProjectFactory.createProject();
170
			p.setName(PluginServices.getText(this, "untitled"));
171
			p.setModified(false);
172
			projectFrame.setProject(p);
173
			showProjectWindow();
174
			PluginServices.getMainFrame().setTitle("gvSIG: " +
175
				PluginServices.getText(this, "sin_titulo"));
176
		} else if (actionCommand.equals("ABRIR")) {
177
			//Si est? modificado se pregunta si se quiere guardar el anterior
178
			if (!modificado()) {
179
				return;
180
			}
181

  
182
			JFileChooser jfc = new JFileChooser();
183
			jfc.addChoosableFileFilter(new GenericFileFilter("xml",
184
					PluginServices.getText(this, "tipo_fichero_proyecto")));
185

  
186
			if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
144 187
				PluginServices.getMDIManager().closeAllViews();
145
                p = ProjectFactory.createProject();
146
                p.setName(PluginServices.getText(this, "untitled"));
147
                p.setModified(false);
148
                projectFrame.setProject(p);
149
                showProjectWindow();
150
                PluginServices.getMainFrame().setTitle("gvSIG: " + PluginServices.getText(this,
151
				 "sin_titulo"));
152 188

  
153
            } else if (actionCommand.equals("ABRIR")) {
154
				//Si est? modificado se pregunta si se quiere guardar el anterior
155
				if (!modificado()) return;
189
				Project o = leerProyecto(jfc.getSelectedFile());
156 190

  
157
				JFileChooser jfc = new JFileChooser();
158
				jfc.addChoosableFileFilter(new GenericFileFilter("xml", PluginServices.getText(this,"tipo_fichero_proyecto")));
159
				if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
160
					PluginServices.getMDIManager().closeAllViews();
161
                	Project o = leerProyecto(jfc.getSelectedFile());
162
                	if (o != null){
163
						p = o; 
164
                	}
165
                	projectFrame.setProject(p);
166
                	projectFrame.refreshControls();
167
                	showProjectWindow();
191
				if (o != null) {
192
					p = o;
168 193
				}
169
            } else if (actionCommand.equals("GUARDAR")) {
170
            	guardar();
171
            }
172
    }
173 194

  
174
    private void escribirProyecto(File file, Project p){
175
            // write it out as XML
176
            try {
177
            	FileWriter writer = new FileWriter(file.getAbsolutePath());
178
            	Marshaller m = new Marshaller(writer);
179
            	m.setEncoding("ISO-8859-1");
180
            	m.marshal(p.getXMLEntity().getXmlTag());
181
                p.setPath(file.toString());
182
                p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
183
                p.setModified(false);
184
                PluginServices.getMainFrame().setTitle("gvSIG: " + file.getName());
185
            } catch (Exception e) {
186
                NotificationManager.addError("Error guardando el proyecto", e);
187
            }
188
            projectFrame.refreshControls();
189
    }
195
				projectFrame.setProject(p);
196
				projectFrame.refreshControls();
197
				showProjectWindow();
198
			}
199
		} else if (actionCommand.equals("GUARDAR")) {
200
			guardar();
201
		}
202
	}
190 203

  
191
    /**
192
     * @see com.iver.mdiApp.FileExtension#writeFile(java.io.File)
193
     */
194
    public Project leerProyecto(File file) {
195
    	Project proj = null;
204
	/**
205
	 * Escribe el proyecto en XML.
206
	 *
207
	 * @param file Fichero.
208
	 * @param p Proyecto.
209
	 */
210
	private void escribirProyecto(File file, Project p) {
211
		// write it out as XML
196 212
		try {
197
        	 File xmlFile = new File(file.getAbsolutePath());
198
        	 FileReader reader;
199
				reader = new FileReader(xmlFile);
200
				XmlTag tag=(XmlTag)XmlTag.unmarshal(reader);
201
	        	p=Project.createFromXML(new XMLEntity(tag));
202
			} catch (FileNotFoundException e) {
203
				NotificationManager.addError("Al leer el proyecto",e);
204
			} catch (MarshalException e) {
205
				NotificationManager.addError("Al leer el proyecto",e);
206
			} catch (ValidationException e) {
207
				NotificationManager.addError("Al leer el proyecto",e);
208
			} catch (XMLException e) {
209
				NotificationManager.addError("Al leer el proyecto",e);
210
			} catch (DriverException e) {
211
				NotificationManager.addError("Al leer el proyecto",e);
212
			}catch (DriverIOException e2) {
213
				NotificationManager.addError("Al leer el proyecto",e2);
214
			}
215
        
216
        return proj;
217
    }
213
			FileWriter writer = new FileWriter(file.getAbsolutePath());
214
			Marshaller m = new Marshaller(writer);
215
			m.setEncoding("ISO-8859-1");
216
			m.marshal(p.getXMLEntity().getXmlTag());
217
			p.setPath(file.toString());
218
			p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
219
			p.setModified(false);
220
			PluginServices.getMainFrame().setTitle("gvSIG: " + file.getName());
221
		} catch (Exception e) {
222
			NotificationManager.addError("Error guardando el proyecto", e);
223
		}
218 224

  
219
    /**
220
	 * @return
225
		projectFrame.refreshControls();
226
	}
227

  
228
	/**
229
	 * Lee del XML el proyecto.
230
	 *
231
	 * @param file Fichero.
232
	 *
233
	 * @return Proyecto.
221 234
	 */
235
	public Project leerProyecto(File file) {
236
		Project proj = null;
237

  
238
		try {
239
			File xmlFile = new File(file.getAbsolutePath());
240
			FileReader reader;
241
			reader = new FileReader(xmlFile);
242

  
243
			XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
244
			p = Project.createFromXML(new XMLEntity(tag));
245
		} catch (FileNotFoundException e) {
246
			NotificationManager.addError("Al leer el proyecto", e);
247
		} catch (MarshalException e) {
248
			NotificationManager.addError("Al leer el proyecto", e);
249
		} catch (ValidationException e) {
250
			NotificationManager.addError("Al leer el proyecto", e);
251
		} catch (XMLException e) {
252
			NotificationManager.addError("Al leer el proyecto", e);
253
		} catch (DriverException e) {
254
			NotificationManager.addError("Al leer el proyecto", e);
255
		} catch (DriverIOException e2) {
256
			NotificationManager.addError("Al leer el proyecto", e2);
257
		}
258

  
259
		return proj;
260
	}
261

  
262
	/**
263
	 * Devuelve el proyecto.
264
	 *
265
	 * @return Proyecto.
266
	 */
222 267
	public Project getProject() {
223 268
		return p;
224 269
	}
......
236 281
	public boolean isVisible() {
237 282
		return true;
238 283
	}
239

  
240 284
}

Also available in: Unified diff