Revision 44088 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 | ||
---|---|---|
40 | 40 |
import java.io.Serializable; |
41 | 41 |
import java.text.DateFormat; |
42 | 42 |
import java.text.MessageFormat; |
43 |
import java.text.SimpleDateFormat; |
|
43 | 44 |
import java.util.ArrayList; |
44 | 45 |
import java.util.Collections; |
45 | 46 |
import java.util.Date; |
... | ... | |
55 | 56 |
import java.util.zip.ZipFile; |
56 | 57 |
import java.util.zip.ZipOutputStream; |
57 | 58 |
import javax.imageio.ImageIO; |
59 |
import org.apache.commons.io.FileUtils; |
|
60 |
import org.apache.commons.io.FilenameUtils; |
|
58 | 61 |
import org.apache.commons.io.IOUtils; |
59 | 62 |
|
60 | 63 |
import org.cresques.cts.IProjection; |
... | ... | |
186 | 189 |
return preferences; |
187 | 190 |
} |
188 | 191 |
|
192 |
@Override |
|
189 | 193 |
public void propertyChange(PropertyChangeEvent evt) { |
190 | 194 |
change.firePropertyChange(evt); |
191 | 195 |
} |
... | ... | |
206 | 210 |
* |
207 | 211 |
* @return |
208 | 212 |
*/ |
213 |
@Override |
|
209 | 214 |
public String getCreationDate() { |
210 | 215 |
return creationDate; |
211 | 216 |
} |
... | ... | |
215 | 220 |
change.firePropertyChange("setCreationDate", null, null); |
216 | 221 |
} |
217 | 222 |
|
223 |
@Override |
|
218 | 224 |
public Document createDocument(String type) { |
219 | 225 |
logger.info("createDocument('{}')", type); |
220 | 226 |
return ProjectManager.getInstance().createDocument(type); |
... | ... | |
225 | 231 |
* |
226 | 232 |
* @return |
227 | 233 |
*/ |
234 |
@Override |
|
228 | 235 |
public String getName() { |
229 | 236 |
return name; |
230 | 237 |
} |
... | ... | |
234 | 241 |
* |
235 | 242 |
* @param string |
236 | 243 |
*/ |
244 |
@Override |
|
237 | 245 |
public void setName(String name) { |
238 | 246 |
this.name = name; |
239 | 247 |
change.firePropertyChange("setName", null, null); |
... | ... | |
244 | 252 |
* |
245 | 253 |
* @return comments |
246 | 254 |
*/ |
255 |
@Override |
|
247 | 256 |
public String getComments() { |
248 | 257 |
return comments; |
249 | 258 |
} |
... | ... | |
253 | 262 |
* |
254 | 263 |
* @param comments as string |
255 | 264 |
*/ |
265 |
@Override |
|
256 | 266 |
public void setComments(String string) { |
257 | 267 |
comments = string; |
258 | 268 |
change.firePropertyChange("setComments", null, null); |
... | ... | |
263 | 273 |
* |
264 | 274 |
* @return modification date as string |
265 | 275 |
*/ |
276 |
@Override |
|
266 | 277 |
public String getModificationDate() { |
267 | 278 |
return modificationDate; |
268 | 279 |
} |
... | ... | |
277 | 288 |
* |
278 | 289 |
* @return author as string |
279 | 290 |
*/ |
291 |
@Override |
|
280 | 292 |
public String getOwner() { |
281 | 293 |
return owner; |
282 | 294 |
} |
... | ... | |
286 | 298 |
* |
287 | 299 |
* @param author name as string |
288 | 300 |
*/ |
301 |
@Override |
|
289 | 302 |
public void setOwner(String owner) { |
290 | 303 |
this.owner = owner; |
291 | 304 |
change.firePropertyChange("setOwner", null, null); |
... | ... | |
296 | 309 |
* |
297 | 310 |
* @return |
298 | 311 |
*/ |
312 |
@Override |
|
299 | 313 |
public Color getSelectionColor() { |
300 | 314 |
if (selectionColor == null) { |
301 | 315 |
selectionColor = getPreferences().getDefaultSelectionColor(); |
... | ... | |
308 | 322 |
* |
309 | 323 |
* @param selection color as string |
310 | 324 |
*/ |
325 |
@Override |
|
311 | 326 |
public void setSelectionColor(String selectionColor) { |
312 | 327 |
this.setSelectionColor(StringUtilities.string2Color(selectionColor)); |
313 | 328 |
} |
... | ... | |
317 | 332 |
* |
318 | 333 |
* @param selection color as Color |
319 | 334 |
*/ |
335 |
@Override |
|
320 | 336 |
public void setSelectionColor(Color selectionColor) { |
321 | 337 |
this.selectionColor = selectionColor; |
322 | 338 |
MapContext.setSelectionColor(selectionColor); |
323 | 339 |
change.firePropertyChange("selectionColor", null, selectionColor); |
324 | 340 |
} |
325 | 341 |
|
342 |
@Override |
|
326 | 343 |
public IProjection getProjection() { |
327 | 344 |
if (projection == null) { |
328 | 345 |
projection = getPreferences().getDefaultProjection(); |
... | ... | |
330 | 347 |
return projection; |
331 | 348 |
} |
332 | 349 |
|
350 |
@Override |
|
333 | 351 |
public void setProjection(IProjection projection) { |
334 | 352 |
this.projection = projection; |
335 | 353 |
} |
... | ... | |
341 | 359 |
* |
342 | 360 |
* @param modified as boolean |
343 | 361 |
*/ |
362 |
@Override |
|
344 | 363 |
public void setModified(boolean modified) { |
345 | 364 |
this.modified = modified; |
346 | 365 |
if (modified == false) { |
... | ... | |
351 | 370 |
} |
352 | 371 |
} |
353 | 372 |
|
373 |
@Override |
|
354 | 374 |
public boolean hasChanged() { |
355 | 375 |
// we return true if the project is not empty (until we have a better |
356 | 376 |
// method...) |
... | ... | |
365 | 385 |
* |
366 | 386 |
* @return documents as List of IProjectDocument |
367 | 387 |
*/ |
388 |
@Override |
|
368 | 389 |
public List<Document> getDocuments() { |
369 | 390 |
return Collections.unmodifiableList(documents); |
370 | 391 |
} |
... | ... | |
376 | 397 |
* |
377 | 398 |
* @return List of IProjectDocument |
378 | 399 |
*/ |
400 |
@Override |
|
379 | 401 |
public List<Document> getDocuments(String type) { |
380 | 402 |
List<Document> docs = new ArrayList<Document>(); |
381 | 403 |
if (type != null) { |
... | ... | |
393 | 415 |
* |
394 | 416 |
* @param document as Document |
395 | 417 |
*/ |
418 |
@Override |
|
396 | 419 |
public void add(Document document) { |
397 | 420 |
this.addDocument(document); |
398 | 421 |
} |
399 | 422 |
|
423 |
@Override |
|
400 | 424 |
public void addDocument(Document document) { |
401 | 425 |
logger.info("add('{}')", document.toString()); |
402 | 426 |
|
... | ... | |
414 | 438 |
change.firePropertyChange("addDocument", null, document); |
415 | 439 |
} |
416 | 440 |
|
441 |
@Override |
|
417 | 442 |
public void remove(Document doc) { |
418 | 443 |
this.removeDocument(doc); |
419 | 444 |
} |
420 | 445 |
|
446 |
@Override |
|
421 | 447 |
public void removeDocument(Document doc) { |
422 | 448 |
logger.info("remove('{}')", doc.toString()); |
423 | 449 |
if (notifyObservers(ProjectNotification.BEFORE_REMOVE_DOCUMENT, doc).isProcessCanceled()) { |
... | ... | |
430 | 456 |
notifyObservers(ProjectNotification.AFTER_REMOVE_DOCUMENT, doc); |
431 | 457 |
} |
432 | 458 |
|
459 |
@Override |
|
433 | 460 |
public Iterator<Document> iterator() { |
434 | 461 |
return documents.iterator(); |
435 | 462 |
} |
436 | 463 |
|
464 |
@Override |
|
437 | 465 |
public boolean isEmpty() { |
438 | 466 |
return documents.isEmpty(); |
439 | 467 |
} |
... | ... | |
448 | 476 |
* @throws RuntimeException Si la capa que se pasa como par�metro no se |
449 | 477 |
* encuentra en ninguna vista |
450 | 478 |
*/ |
479 |
@Override |
|
451 | 480 |
public String getViewName(FLayer layer) { |
452 | 481 |
List<Document> views = getDocuments(ViewManager.TYPENAME); |
453 | 482 |
for (int v = 0; v < views.size(); v++) { |
... | ... | |
476 | 505 |
return false; |
477 | 506 |
} |
478 | 507 |
|
508 |
@Override |
|
479 | 509 |
public void addExtent(ProjectExtent arg1) { |
480 | 510 |
extents.add(arg1); |
481 | 511 |
change.firePropertyChange("addExtent", null, null); |
482 | 512 |
} |
483 | 513 |
|
514 |
@Override |
|
484 | 515 |
public ProjectExtent removeExtent(int arg0) { |
485 | 516 |
change.firePropertyChange("delExtent", null, null); |
486 | 517 |
return extents.remove(arg0); |
487 | 518 |
} |
488 | 519 |
|
520 |
@Override |
|
489 | 521 |
public ProjectExtent[] getExtents() { |
490 | 522 |
return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]); |
491 | 523 |
} |
... | ... | |
500 | 532 |
* |
501 | 533 |
* @return Documento |
502 | 534 |
*/ |
535 |
@Override |
|
503 | 536 |
public Document getDocument(String name, String type) { |
504 | 537 |
if (type == null) { |
505 | 538 |
for (int i = 0; i < documents.size(); i++) { |
... | ... | |
520 | 553 |
return null; |
521 | 554 |
} |
522 | 555 |
|
556 |
@Override |
|
523 | 557 |
public Document getDocument(String name) { |
524 | 558 |
return this.getDocument(name, null); |
525 | 559 |
} |
... | ... | |
556 | 590 |
} |
557 | 591 |
} |
558 | 592 |
|
593 |
@Override |
|
559 | 594 |
public void saveState(File out) throws PersistenceException { |
560 | 595 |
FileOutputStream fout; |
561 | 596 |
if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_FILE, out).isProcessCanceled()) { |
... | ... | |
565 | 600 |
fout = new FileOutputStream(out); |
566 | 601 |
saveState(fout, new File(out.getParent())); |
567 | 602 |
} catch (FileNotFoundException e) { |
603 |
createProjectCopy(true); |
|
568 | 604 |
throw new PersistenceException(e); |
569 | 605 |
} |
570 | 606 |
if( !isValidZIP(out) ) { |
571 | 607 |
throw new PersistenceException(new ZipException()); |
572 | 608 |
} |
573 | 609 |
this.fname = out; |
610 |
//sacar copia al bak |
|
611 |
createProjectCopy(false); |
|
574 | 612 |
notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out); |
575 | 613 |
} |
576 | 614 |
|
... | ... | |
580 | 618 |
zipfile = new ZipFile(file); |
581 | 619 |
return true; |
582 | 620 |
} catch (IOException e) { |
621 |
//sacar copia. |
|
583 | 622 |
return false; |
584 | 623 |
} finally { |
585 | 624 |
try { |
... | ... | |
625 | 664 |
throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'"); |
626 | 665 |
} |
627 | 666 |
this.fname = file; |
667 |
if (file.exists()) { |
|
668 |
createProjectCopy(false); |
|
669 |
} |
|
628 | 670 |
} catch (Exception ex) { |
629 |
throw new RuntimeException("Can't write project in '"+file.getAbsolutePath()+".",ex); |
|
671 |
createProjectCopy(true); |
|
672 |
throw new RuntimeException("Can't write project in '" + file.getAbsolutePath() + ".", ex); |
|
630 | 673 |
} finally { |
631 | 674 |
IOUtils.closeQuietly(zout); |
632 |
IOUtils.closeQuietly(fout); |
|
675 |
IOUtils.closeQuietly(fout); |
|
676 |
|
|
633 | 677 |
} |
634 | 678 |
} |
635 | 679 |
|
... | ... | |
651 | 695 |
} |
652 | 696 |
manager.saveState(state, out, true); |
653 | 697 |
if (state.getContext().getErrors() != null) { |
698 |
createProjectCopy(true); |
|
654 | 699 |
throw state.getContext().getErrors(); |
655 | 700 |
} |
656 | 701 |
this.fname = null; |
657 | 702 |
notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder); |
658 | 703 |
} |
704 |
|
|
705 |
private File getNameFileForProject(File actualProjectFile, boolean error) { |
|
706 |
// if there is a error, creates a file with a unique timecode |
|
707 |
// not error: return path with .gvsproj.bak extension that could exists |
|
708 |
|
|
709 |
if (actualProjectFile==null) { |
|
710 |
LOG.info("Doesn't have previous project to create a bak"); |
|
711 |
return null; |
|
712 |
} |
|
713 |
String noExt = FilenameUtils.removeExtension(actualProjectFile.getAbsolutePath()); |
|
714 |
String timeCode = new SimpleDateFormat("_MMddHHmmss").format(new Date()); |
|
715 |
String ext = ".gvsproj.bak"; |
|
659 | 716 |
|
717 |
int code = 0; |
|
718 |
if (noExt.length() > 230) { |
|
719 |
noExt = noExt.substring(0, 229); |
|
720 |
} |
|
721 |
|
|
722 |
File bakProjectFile; |
|
723 |
if (error) { |
|
724 |
bakProjectFile = new File(noExt + timeCode + ext); |
|
725 |
while (bakProjectFile.exists()) { |
|
726 |
code = code + 1; |
|
727 |
bakProjectFile = new File(noExt + timeCode + "_" + code + ext); |
|
728 |
} |
|
729 |
} else { |
|
730 |
bakProjectFile = new File(noExt + ext); |
|
731 |
} |
|
732 |
return bakProjectFile; |
|
733 |
} |
|
734 |
public File createProjectCopy(boolean error) { |
|
735 |
File actualProjectFile = ApplicationLocator.getProjectManager().getCurrentProject().getFile(); |
|
736 |
File bakProjectFile = getNameFileForProject(actualProjectFile, error); |
|
737 |
if (bakProjectFile==null){ |
|
738 |
return null; |
|
739 |
} |
|
740 |
if (!error){ |
|
741 |
try { |
|
742 |
if (bakProjectFile.exists()){ |
|
743 |
FileUtils.deleteQuietly(bakProjectFile); |
|
744 |
} |
|
745 |
} catch (Exception ex) { |
|
746 |
LOG.error("Not possible to delete file", ex); |
|
747 |
} |
|
748 |
try { |
|
749 |
FileUtils.copyFile(actualProjectFile, bakProjectFile); |
|
750 |
} catch (IOException ex) { |
|
751 |
LOG.error("Can't create bak copy from project", ex); |
|
752 |
} |
|
753 |
} else { |
|
754 |
File bakProjectFile2 = getNameFileForProject(actualProjectFile, false); |
|
755 |
try { |
|
756 |
FileUtils.moveFile(bakProjectFile2, bakProjectFile); |
|
757 |
} catch (IOException ex) { |
|
758 |
LOG.error("Can't rename file", ex); |
|
759 |
} |
|
760 |
} |
|
761 |
|
|
762 |
return bakProjectFile; |
|
763 |
} |
|
764 |
|
|
660 | 765 |
@Deprecated |
766 |
@Override |
|
661 | 767 |
public void loadState(InputStream in) { |
662 | 768 |
loadState(in, null); |
663 | 769 |
} |
... | ... | |
744 | 850 |
} catch (FileNotFoundException e) { |
745 | 851 |
LOG.info("Can't load project to stream", e); |
746 | 852 |
} |
747 |
this.fname = in; |
|
853 |
if ("bak".equals(FilenameUtils.getExtension(in.getAbsolutePath()))) { |
|
854 |
this.fname = null; |
|
855 |
} else { |
|
856 |
this.fname = in; |
|
857 |
} |
|
748 | 858 |
notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in); |
749 | 859 |
} |
750 | 860 |
|
... | ... | |
819 | 929 |
notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state); |
820 | 930 |
} |
821 | 931 |
|
932 |
@Override |
|
822 | 933 |
public void saveToState(PersistentState state) throws PersistenceException { |
823 | 934 |
state.set("version", VERSION); |
824 | 935 |
state.set("comments", getComments()); |
... | ... | |
864 | 975 |
|
865 | 976 |
} |
866 | 977 |
|
978 |
@Override |
|
867 | 979 |
public Object getProperty(Object key) { |
868 | 980 |
return this.propertiesHelper.getProperty(key); |
869 | 981 |
} |
870 | 982 |
|
983 |
@Override |
|
871 | 984 |
public void setProperty(Object key, Object obj) { |
872 | 985 |
this.propertiesHelper.setProperty(key, obj); |
873 | 986 |
} |
874 | 987 |
|
988 |
@Override |
|
875 | 989 |
public Map getExtendedProperties() { |
876 | 990 |
return this.propertiesHelper.getExtendedProperties(); |
877 | 991 |
} |
... | ... | |
1133 | 1247 |
throw new UnsupportedOperationException("This method is not supported"); |
1134 | 1248 |
} |
1135 | 1249 |
|
1250 |
@Override |
|
1136 | 1251 |
public boolean canImportDocuments(String data, String doctype) { |
1137 | 1252 |
// TODO Auto-generated method stub |
1138 | 1253 |
return false; |
1139 | 1254 |
} |
1140 | 1255 |
|
1256 |
@Override |
|
1141 | 1257 |
public String exportDocumentsAsText(List<Document> documents) { |
1142 | 1258 |
// TODO Auto-generated method stub |
1143 | 1259 |
return null; |
1144 | 1260 |
} |
1145 | 1261 |
|
1262 |
@Override |
|
1146 | 1263 |
public void importDocuments(String data, String doctype) { |
1147 | 1264 |
// TODO Auto-generated method stub |
1148 | 1265 |
|
1149 | 1266 |
} |
1150 | 1267 |
|
1268 |
@Override |
|
1151 | 1269 |
public Document getActiveDocument() { |
1152 | 1270 |
return this.getActiveDocument((Class<? extends Document>)null); |
1153 | 1271 |
} |
1154 | 1272 |
|
1273 |
@Override |
|
1155 | 1274 |
public Document getActiveDocument(String documentTypeName) { |
1156 | 1275 |
ApplicationManager application = ApplicationLocator.getManager(); |
1157 | 1276 |
|
... | ... | |
1187 | 1306 |
return null; |
1188 | 1307 |
} |
1189 | 1308 |
|
1309 |
@Override |
|
1190 | 1310 |
public Document getActiveDocument(Class<? extends Document> documentClass) { |
1191 | 1311 |
ApplicationManager application = ApplicationLocator.getManager(); |
1192 | 1312 |
|
... | ... | |
1222 | 1342 |
return null; |
1223 | 1343 |
} |
1224 | 1344 |
|
1345 |
@Override |
|
1225 | 1346 |
public void addObserver(Observer o) { |
1226 | 1347 |
observableHelper.addObserver(o); |
1227 | 1348 |
} |
1228 | 1349 |
|
1350 |
@Override |
|
1229 | 1351 |
public void deleteObserver(Observer o) { |
1230 | 1352 |
observableHelper.deleteObserver(o); |
1231 | 1353 |
} |
1232 | 1354 |
|
1355 |
@Override |
|
1233 | 1356 |
public void deleteObservers() { |
1234 | 1357 |
observableHelper.deleteObservers(); |
1235 | 1358 |
} |
Also available in: Unified diff