Changeset 1108 for trunk/src/org/expeditee/actions
- Timestamp:
- 09/05/18 12:56:49 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/actions/Misc.java
r1102 r1108 28 28 import java.net.URL; 29 29 import java.net.URLClassLoader; 30 import java.nio.file.Path; 31 import java.nio.file.Paths; 30 32 import java.util.ArrayList; 31 33 import java.util.Collection; 34 import java.util.HashMap; 32 35 import java.util.LinkedList; 33 36 import java.util.List; 37 import java.util.Map; 34 38 import java.util.Map.Entry; 35 39 import java.util.jar.Attributes; … … 57 61 import org.expeditee.items.ItemUtils; 58 62 import org.expeditee.items.Line; 63 import org.expeditee.items.Picture; 59 64 import org.expeditee.items.Text; 60 65 import org.expeditee.items.XRayable; … … 686 691 public static void MessageLn2(String message, String message2) { 687 692 MessageBay.displayMessage(message + " " + message2); 693 } 694 695 public static void MigrateImages() { MigrateImages(DisplayController.getCurrentFrame()); } 696 697 public static void MigrateImages(Frame frame) { 698 //Collect the images on frame 699 final Collection<Item> items = frame.getItems(); 700 final Collection<Item> imagesTextItems = new LinkedList<Item>(); 701 items.forEach(i -> { if(i.getText().startsWith("@i")) imagesTextItems.add(i); }); 702 final Map<Item, Collection<Path>> images = new HashMap<Item, Collection<Path>>(); 703 imagesTextItems.forEach(it -> { 704 final Collection<? extends XRayable> enclosures = it.getEnclosures(); 705 final Collection<Path> paths = new LinkedList<Path>(); 706 enclosures.forEach(enc -> { if(enc instanceof Picture) paths.add(Paths.get(enc.getName())); }); 707 images.put(it, paths); 708 }); 709 710 //Separate into categories: absolute external, absolute internal. Discard relative. 711 final Map<Item, Collection<Path>> imagesAbsolute = new HashMap<Item, Collection<Path>>(); 712 images.keySet().forEach(key -> images.get(key).forEach(path -> { 713 if(path.isAbsolute()) 714 if(imagesAbsolute.containsKey(key)) imagesAbsolute.get(key).add(path); 715 else { 716 final Collection<Path> paths = new LinkedList<Path>(); 717 paths.add(path); 718 imagesAbsolute.put(key, paths); 719 } 720 })); 721 final Path imagesPath = Paths.get(FrameIO.IMAGES_PATH); 722 final Map<Item, Collection<Path>> imagesAbsoluteInternal = new HashMap<Item, Collection<Path>>(); 723 imagesAbsolute.keySet().forEach(key -> imagesAbsolute.get(key).forEach(path -> { 724 if(path.startsWith(imagesPath)) 725 if(imagesAbsoluteInternal.containsKey(key)) imagesAbsoluteInternal.get(key).add(path); 726 else { 727 final Collection<Path> paths = new LinkedList<Path>(); 728 paths.add(path); 729 imagesAbsoluteInternal.put(key, paths); 730 } 731 })); 732 final Map<Item, Collection<Path>> imagesAbsoluteExternal = new HashMap<Item, Collection<Path>>(); 733 imagesAbsolute.keySet().forEach(key -> imagesAbsolute.get(key).forEach(path -> { 734 if(!path.startsWith(imagesPath)) 735 if(imagesAbsoluteExternal.containsKey(key)) imagesAbsoluteExternal.get(key).add(path); 736 else { 737 final Collection<Path> paths = new LinkedList<Path>(); 738 paths.add(path); 739 imagesAbsoluteExternal.put(key, paths); 740 } 741 })); 742 743 //Bryce: I am not sure why each Item is programmed to be able to have a collection of XRayables rather 744 //than a single one. Up until this point I have programmed defensively to retain this possibility. At 745 //this point the code will begin to simply use the first XRayable to determine the content of the @i. 746 747 //Transform absolute internal images into relative. 748 imagesAbsoluteInternal.keySet().forEach(key -> { 749 final Path imagePath = imagesAbsoluteInternal.get(key).iterator().next(); 750 final Path relative = imagesPath.relativize(imagePath); 751 key.setText(key.getText().replace(imagePath.toString(), relative.toString())); 752 MessageBay.displayMessage("Migrated image: " + imagePath + ". It now uses the relative path: " + relative); 753 }); 754 755 //Transform absolute external images into relative 756 imagesAbsoluteExternal.keySet().forEach(key -> { 757 final Path imagePath = imagesAbsoluteExternal.get(key).iterator().next(); 758 try { 759 FrameIO.copyFile(imagePath.toString(), imagesPath.resolve(imagePath.getFileName()).toString()); 760 final Path relative = imagesPath.relativize(imagePath); 761 key.setText(key.getText().replace(imagePath.toString(), relative.toString())); 762 MessageBay.displayMessage("Migrated image: " + imagePath + ". It now uses the relative path: " + relative); 763 } catch (IOException e) { 764 MessageBay.displayMessage("Unable to Migrate file: " + imagePath); 765 } 766 }); 688 767 } 689 768
Note:
See TracChangeset
for help on using the changeset viewer.