Ignore:
Timestamp:
09/05/18 12:56:49 (6 years ago)
Author:
bln4
Message:

org.expeditee.actions.Misc ->

Added new action MigrateImages.


Three categories of @i's: relative, absolute internal and absolute internal.
Relative is ideal as it makes it easier to move Framesets around. Relative @i's reference images in Expeditee.home's images directory.
Absolute internal can be adjusted to Relative without any problems. The images are stored in Expeditee.home's image directory but are addressed absolutely.
Absolute external are images that are stored somewhere on the computer that is not Expeditee.home's image directory.


This action, when run, will transition absolute external and absolute internal to relative.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Misc.java

    r1102 r1108  
    2828import java.net.URL;
    2929import java.net.URLClassLoader;
     30import java.nio.file.Path;
     31import java.nio.file.Paths;
    3032import java.util.ArrayList;
    3133import java.util.Collection;
     34import java.util.HashMap;
    3235import java.util.LinkedList;
    3336import java.util.List;
     37import java.util.Map;
    3438import java.util.Map.Entry;
    3539import java.util.jar.Attributes;
     
    5761import org.expeditee.items.ItemUtils;
    5862import org.expeditee.items.Line;
     63import org.expeditee.items.Picture;
    5964import org.expeditee.items.Text;
    6065import org.expeditee.items.XRayable;
     
    686691        public static void MessageLn2(String message, String message2) {
    687692                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                });
    688767        }
    689768
Note: See TracChangeset for help on using the changeset viewer.