Revision 43913 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/project/DefaultProject.java
DefaultProject.java | ||
---|---|---|
23 | 23 |
package org.gvsig.app.project; |
24 | 24 |
|
25 | 25 |
import java.awt.Color; |
26 |
import java.awt.Image; |
|
27 |
import java.awt.geom.AffineTransform; |
|
28 |
import java.awt.image.AffineTransformOp; |
|
29 |
import java.awt.image.BufferedImage; |
|
26 | 30 |
import java.beans.PropertyChangeEvent; |
27 | 31 |
import java.beans.PropertyChangeListener; |
28 | 32 |
import java.beans.PropertyChangeSupport; |
... | ... | |
45 | 49 |
import java.util.List; |
46 | 50 |
import java.util.Map; |
47 | 51 |
import java.util.Set; |
52 |
import java.util.logging.Level; |
|
53 |
import java.util.zip.ZipEntry; |
|
48 | 54 |
import java.util.zip.ZipException; |
49 | 55 |
import java.util.zip.ZipFile; |
56 |
import java.util.zip.ZipOutputStream; |
|
57 |
import javax.imageio.ImageIO; |
|
58 |
import org.apache.commons.io.IOUtils; |
|
50 | 59 |
|
51 | 60 |
import org.cresques.cts.IProjection; |
52 | 61 |
|
... | ... | |
60 | 69 |
import org.gvsig.app.extension.ProjectExtension; |
61 | 70 |
import org.gvsig.app.extension.Version; |
62 | 71 |
import org.gvsig.app.project.ProjectManager.NewProjectEvent; |
63 |
import org.gvsig.app.project.ProjectManager.ProjectLoadedEvent; |
|
64 | 72 |
import org.gvsig.app.project.documents.AbstractDocument; |
65 | 73 |
import org.gvsig.app.project.documents.Document; |
66 | 74 |
import org.gvsig.app.project.documents.exceptions.SaveException; |
... | ... | |
82 | 90 |
import org.gvsig.tools.persistence.PersistentState; |
83 | 91 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
84 | 92 |
import org.gvsig.utils.StringUtilities; |
93 |
import org.gvsig.utils.save.SaveEvent; |
|
85 | 94 |
|
86 | 95 |
import org.slf4j.Logger; |
87 | 96 |
import org.slf4j.LoggerFactory; |
... | ... | |
181 | 190 |
change.firePropertyChange(evt); |
182 | 191 |
} |
183 | 192 |
|
193 |
@Override |
|
184 | 194 |
public synchronized void addPropertyChangeListener( |
185 | 195 |
PropertyChangeListener arg0) { |
186 | 196 |
change.addPropertyChangeListener(arg0); |
187 | 197 |
} |
188 | 198 |
|
199 |
@Override |
|
200 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
201 |
change.removePropertyChangeListener(listener); |
|
202 |
} |
|
203 |
|
|
189 | 204 |
/** |
190 | 205 |
* Return the creation date of the project |
191 | 206 |
* |
... | ... | |
577 | 592 |
} |
578 | 593 |
} |
579 | 594 |
|
595 |
@Override |
|
580 | 596 |
public File getFile() { |
581 | 597 |
return this.fname; |
582 | 598 |
} |
583 | 599 |
|
600 |
@Deprecated |
|
601 |
@Override |
|
584 | 602 |
public void saveState(OutputStream out) throws PersistenceException { |
585 | 603 |
saveState(out, null); |
586 | 604 |
} |
605 |
|
|
606 |
@Override |
|
607 |
public void saveState(File file, BufferedImage preview) { |
|
608 |
FileOutputStream fout=null; |
|
609 |
ZipOutputStream zout=null; |
|
610 |
try { |
|
611 |
fout = new FileOutputStream(file); |
|
612 |
zout = new ZipOutputStream(fout); |
|
613 |
this.saveState(zout, file.getParentFile()); |
|
587 | 614 |
|
615 |
zout.putNextEntry(new ZipEntry("preview.jpg")); |
|
616 |
try { |
|
617 |
ImageIO.write(preview, "jpg", zout); |
|
618 |
} catch (IOException ex) { |
|
619 |
LOG.warn("Can't save preview image'.", ex); |
|
620 |
} |
|
621 |
IOUtils.closeQuietly(zout); |
|
622 |
IOUtils.closeQuietly(fout); |
|
623 |
|
|
624 |
if( !isValidZIP(file) ) { |
|
625 |
throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'"); |
|
626 |
} |
|
627 |
this.fname = file; |
|
628 |
} catch (Exception ex) { |
|
629 |
throw new RuntimeException("Can't write project in '"+file.getAbsolutePath()+".",ex); |
|
630 |
} finally { |
|
631 |
IOUtils.closeQuietly(zout); |
|
632 |
IOUtils.closeQuietly(fout); |
|
633 |
} |
|
634 |
} |
|
635 |
|
|
636 |
@Override |
|
588 | 637 |
public void saveState(OutputStream out, File rootFolder) |
589 | 638 |
throws PersistenceException { |
590 |
if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM).isProcessCanceled()) { |
|
639 |
if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM, rootFolder).isProcessCanceled()) {
|
|
591 | 640 |
return; |
592 | 641 |
} |
593 | 642 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
... | ... | |
605 | 654 |
throw state.getContext().getErrors(); |
606 | 655 |
} |
607 | 656 |
this.fname = null; |
608 |
notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM); |
|
657 |
notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder);
|
|
609 | 658 |
} |
610 | 659 |
|
660 |
@Deprecated |
|
611 | 661 |
public void loadState(InputStream in) { |
612 | 662 |
loadState(in, null); |
613 | 663 |
} |
614 | 664 |
|
615 | 665 |
public void loadState(InputStream in, File rootFolder) { |
616 |
if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STREAM).isProcessCanceled()) { |
|
666 |
if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STREAM, rootFolder).isProcessCanceled()) {
|
|
617 | 667 |
return; |
618 | 668 |
} |
619 | 669 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
... | ... | |
631 | 681 |
this.loadFromState(state); |
632 | 682 |
this.unloadedObjects = getUnloadedObjects(state.getContext()); |
633 | 683 |
this.loadErrors = state.getContext().getErrors(); |
634 |
ProjectManager.getInstance().notifyProjectEvent(new ProjectLoadedEvent() { |
|
635 |
@Override |
|
636 |
public Project getProject() { |
|
637 |
return DefaultProject.this; |
|
638 |
} |
|
639 |
}); |
|
640 | 684 |
|
641 | 685 |
} catch (PersistenceException e) { |
642 | 686 |
LOG.info("Can't load project to stream", e); |
643 | 687 |
} |
644 | 688 |
this.fname = null; |
645 |
notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM); |
|
689 |
notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM, rootFolder);
|
|
646 | 690 |
|
647 | 691 |
} |
648 | 692 |
|
... | ... | |
704 | 748 |
notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in); |
705 | 749 |
} |
706 | 750 |
|
707 |
@SuppressWarnings("unchecked") |
|
751 |
//@SuppressWarnings("unchecked") |
|
752 |
@Override |
|
708 | 753 |
public void loadFromState(PersistentState state) |
709 | 754 |
throws PersistenceException { |
710 | 755 |
this.clean(); |
756 |
notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STATE, state); |
|
711 | 757 |
|
712 | 758 |
this.setComments(state.getString("comments")); |
713 | 759 |
this.setCreationDate(state.getString("creationDate")); |
... | ... | |
728 | 774 |
List<AbstractDocument> documents = (List<AbstractDocument>) state |
729 | 775 |
.get("documents"); |
730 | 776 |
for (int i = 0; i < documents.size(); i++) { |
731 |
this.add(documents.get(i)); |
|
777 |
AbstractDocument doc = documents.get(i); |
|
778 |
if( doc != null ) { |
|
779 |
this.add(doc); |
|
780 |
} |
|
732 | 781 |
} |
733 | 782 |
|
734 | 783 |
List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation"); |
... | ... | |
751 | 800 |
pe.setProject(this); |
752 | 801 |
pe.showProjectWindow(projectWindowInfo); |
753 | 802 |
} |
754 |
|
|
803 |
notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state); |
|
755 | 804 |
} |
756 | 805 |
|
757 | 806 |
public void saveToState(PersistentState state) throws PersistenceException { |
... | ... | |
1169 | 1218 |
observableHelper.deleteObservers(); |
1170 | 1219 |
} |
1171 | 1220 |
|
1172 |
// private void notifyObservers(ProjectNotifycation notifycation) { |
|
1173 |
// observableHelper.notifyObservers(this, notifycation); |
|
1174 |
// } |
|
1175 | 1221 |
private ProjectNotification notifyObservers(int type) { |
1176 |
DefaultProjectNotification notifycation |
|
1177 |
= new DefaultProjectNotification(type, null, null); |
|
1178 |
try { |
|
1179 |
observableHelper.notifyObservers(this, notifycation); |
|
1180 |
} catch (Exception ex) { |
|
1181 |
LOG.info("Can't notify observers", ex); |
|
1182 |
} |
|
1183 |
return notifycation; |
|
1222 |
return notifyObservers(new DefaultProjectNotification(type)); |
|
1184 | 1223 |
} |
1185 | 1224 |
|
1186 | 1225 |
private ProjectNotification notifyObservers(int type, Document document) { |
1187 |
DefaultProjectNotification notifycation |
|
1188 |
= new DefaultProjectNotification(type, document, null); |
|
1189 |
try { |
|
1190 |
observableHelper.notifyObservers(this, notifycation); |
|
1191 |
} catch (Exception ex) { |
|
1192 |
LOG.info("Can't notify observers", ex); |
|
1193 |
} |
|
1194 |
return notifycation; |
|
1226 |
return notifyObservers(new DefaultProjectNotification(type, document)); |
|
1195 | 1227 |
} |
1228 |
|
|
1229 |
private ProjectNotification notifyObservers(int type, PersistentState state) { |
|
1230 |
return notifyObservers(new DefaultProjectNotification(type, state)); |
|
1231 |
} |
|
1196 | 1232 |
|
1197 | 1233 |
private ProjectNotification notifyObservers(int type, File file) { |
1198 |
DefaultProjectNotification notifycation |
|
1199 |
= new DefaultProjectNotification(type, null, file); |
|
1234 |
return notifyObservers(new DefaultProjectNotification(type, file)); |
|
1235 |
} |
|
1236 |
|
|
1237 |
private ProjectNotification notifyObservers(ProjectNotification notifycation) { |
|
1200 | 1238 |
try { |
1201 | 1239 |
observableHelper.notifyObservers(this, notifycation); |
1202 | 1240 |
} catch (Exception ex) { |
... | ... | |
1204 | 1242 |
} |
1205 | 1243 |
return notifycation; |
1206 | 1244 |
} |
1207 |
|
|
1245 |
|
|
1208 | 1246 |
class DefaultProjectNotification implements ProjectNotification { |
1209 | 1247 |
|
1210 | 1248 |
private int type; |
1211 | 1249 |
private Document document; |
1212 | 1250 |
private File file; |
1213 | 1251 |
private boolean processCanceled = false; |
1252 |
private PersistentState state = null; |
|
1214 | 1253 |
|
1215 |
DefaultProjectNotification(int type, Document document, File file) {
|
|
1254 |
DefaultProjectNotification(int type) { |
|
1216 | 1255 |
this.type = type; |
1256 |
} |
|
1257 |
|
|
1258 |
DefaultProjectNotification(int type, Document document) { |
|
1259 |
this(type); |
|
1217 | 1260 |
this.document = document; |
1261 |
} |
|
1262 |
|
|
1263 |
DefaultProjectNotification(int type, File file) { |
|
1264 |
this(type); |
|
1218 | 1265 |
this.file = file; |
1219 | 1266 |
} |
1220 | 1267 |
|
1268 |
DefaultProjectNotification(int type, PersistentState state) { |
|
1269 |
this(type); |
|
1270 |
this.state = state; |
|
1271 |
} |
|
1272 |
|
|
1273 |
@Override |
|
1221 | 1274 |
public int getNotificationType() { |
1222 | 1275 |
return type; |
1223 | 1276 |
} |
1224 | 1277 |
|
1278 |
@Override |
|
1225 | 1279 |
public Document getDocument() { |
1226 | 1280 |
return document; |
1227 | 1281 |
} |
1228 | 1282 |
|
1283 |
@Override |
|
1229 | 1284 |
public void cancelProcess() { |
1230 | 1285 |
processCanceled = true; |
1231 | 1286 |
} |
1232 | 1287 |
|
1288 |
@Override |
|
1233 | 1289 |
public boolean isProcessCanceled() { |
1234 | 1290 |
return processCanceled; |
1235 | 1291 |
} |
1236 | 1292 |
|
1293 |
@Override |
|
1237 | 1294 |
public File getFile() { |
1238 | 1295 |
return file; |
1239 | 1296 |
} |
1240 | 1297 |
|
1298 |
@Override |
|
1299 |
public PersistentState getState() { |
|
1300 |
return this.state; |
|
1301 |
} |
|
1302 |
|
|
1303 |
@Override |
|
1304 |
public Project getProject() { |
|
1305 |
return DefaultProject.this; |
|
1306 |
} |
|
1241 | 1307 |
} |
1242 | 1308 |
|
1243 | 1309 |
} |
Also available in: Unified diff