Search in sources :

Example 1 with ImageException

use of edu.cmu.cs.hcii.cogtool.util.GraphicsUtil.ImageException in project cogtool by cogtool.

the class DesignExportToHTML method exportToHTML.

public void exportToHTML(Design d, String dir, Cancelable cancelState, ProgressCallback progressState) {
    // No need to duplicate here; we're already in the child thread
    // and the design had to have been duplicated in the main thread!
    design = d;
    destDirectory = dir;
    // Performed by the child thread
    // Create a file object for DestDir and test if it is actually
    // a directory
    parentDir = new File(destDirectory);
    if (!parentDir.isDirectory()) {
        // If there is no directory specified, we should fail here.
        throw new IllegalArgumentException("Create Web pages called " + "without a directory");
    }
    Set<Frame> frameSet = design.getFrames();
    int frameCount = frameSet.size();
    // Start at 0 leaving one extra "count" for overlib copy
    // Use "double" to force proper division when setting progress below
    double progressCount = 0.0;
    // call buildFrameList to build the lookup maps.
    buildFrameList();
    Iterator<Frame> iter = frameSet.iterator();
    ImageLoader imageLoader = new ImageLoader();
    String html = null;
    Image img = null;
    //Note: Very long while loop in terms of code
    while ((!cancelState.isCanceled()) && iter.hasNext()) {
        Frame frame = iter.next();
        try {
            //below function, "buildFrameImage", takes in a frame and returns its image
            img = buildFrameImage(frame);
            imageLoader.data = new ImageData[] { img.getImageData() };
            String imageName = getFrameFileName(frame, ".jpg");
            // create new FILE handler for the new imageSaver's use
            File imageFile = new File(parentDir, imageName);
            try {
                imageLoader.save(imageFile.getCanonicalPath(), SWT.IMAGE_JPEG);
            } catch (IOException ex) {
                // We can continue even with exceptions on individual images
                throw new ImageException("Failed saving image for HTML export", ex);
            }
        } finally {
            // dispose the image, it's not needed any more.
            img.dispose();
        }
        try {
            // write HTML to destDir
            FileWriter fileOut = null;
            BufferedWriter writer = null;
            try {
                // Use the local file name, and not the complete path.
                html = buildFrameHTML(frame);
                File htmlFile = new File(parentDir, getFrameFileName(frame, ".html"));
                fileOut = new FileWriter(htmlFile);
                writer = new BufferedWriter(fileOut);
                writer.write(html);
            } finally {
                if (writer != null) {
                    writer.close();
                } else if (fileOut != null) {
                    fileOut.close();
                }
            }
        } catch (IOException ex) {
            throw new ExportIOException("Could not save HTML for export ", ex);
        }
        // Update the progress count
        progressCount += 1.0;
        progressState.updateProgress(progressCount / frameCount, SWTStringUtil.insertEllipsis(frame.getName(), 250, StringUtil.NO_FRONT, SWTStringUtil.DEFAULT_FONT));
    //end of getting frames
    }
    //make sure user did not cancel, and then create folder "build"
    if (!cancelState.isCanceled()) {
        File buildDir = new File(parentDir, "build");
        if (!buildDir.exists()) {
            if (!buildDir.mkdir()) {
                throw new ExportIOException("Could not create build directory");
            }
        }
        try {
            // Write out the index page.
            FileWriter fileOut = null;
            BufferedWriter writer = null;
            try {
                // Use the local file name, and not the complete path.
                //This creates the main page, needs a little styling
                html = buildIndexPage();
                File htmlFile = new File(parentDir, "index.html");
                fileOut = new FileWriter(htmlFile);
                writer = new BufferedWriter(fileOut);
                writer.write(html);
            } finally {
                if (writer != null) {
                    writer.close();
                } else if (fileOut != null) {
                    fileOut.close();
                }
            }
        } catch (IOException ex) {
            throw new ExportIOException("Could not save index.html for export ", ex);
        }
        //Here we import all the resources from the standard directory
        InputStream overlibStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/overlib.js");
        if (overlibStream == null) {
            throw new ExportIOException("Could not locate overlib.js resource");
        }
        File overlibFile = new File(buildDir, "overlib.js");
        InputStream containerCoreStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/container_core.js");
        if (containerCoreStream == null) {
            throw new ExportIOException("Could not locate container_core.js resource");
        }
        File containerCoreFile = new File(buildDir, "container_core.js");
        InputStream fontsStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/fonts-min.css");
        if (fontsStream == null) {
            throw new ExportIOException("Could not locate fonts-min.css resource");
        }
        File fontsFile = new File(buildDir, "fonts-min.css");
        InputStream menuStyleStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/menu.css");
        if (menuStyleStream == null) {
            throw new ExportIOException("Could not locate menu.css resource");
        }
        File menuStyleFile = new File(buildDir, "menu.css");
        InputStream menuStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/menu.js");
        if (menuStream == null) {
            throw new ExportIOException("Could not locate menu.js resource");
        }
        File menuFile = new File(buildDir, "menu.js");
        //As you can see, lots of yahoo fancy shmancy
        InputStream eventStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/yahoo-dom-event.js");
        if (eventStream == null) {
            throw new ExportIOException("Could not locate yahoo-dom-event.js resource");
        }
        File eventFile = new File(buildDir, "yahoo-dom-event.js");
        InputStream spriteStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/sprite.png");
        if (spriteStream == null) {
            throw new ExportIOException("Could not locate sprite.png resource");
        }
        File spriteFile = new File(buildDir, "sprite.png");
        try {
            FileUtil.copyStreamToFile(overlibStream, overlibFile);
            FileUtil.copyStreamToFile(containerCoreStream, containerCoreFile);
            FileUtil.copyStreamToFile(fontsStream, fontsFile);
            FileUtil.copyStreamToFile(menuStyleStream, menuStyleFile);
            FileUtil.copyStreamToFile(menuStream, menuFile);
            FileUtil.copyStreamToFile(eventStream, eventFile);
            FileUtil.copyStreamToFile(spriteStream, spriteFile);
        } catch (IOException ex) {
            throw new ExportIOException("Failed to create file", ex);
        }
    }
    // clear the look up object
    frameLookUp.clear();
    frameLookUp = null;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) ImageException(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil.ImageException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) BufferedWriter(java.io.BufferedWriter) ImageLoader(org.eclipse.swt.graphics.ImageLoader) File(java.io.File)

Example 2 with ImageException

use of edu.cmu.cs.hcii.cogtool.util.GraphicsUtil.ImageException in project cogtool by cogtool.

the class DesignExportToHTML method getHotspotString.

/**
	 * Code for generating a generic html hotspot with bounds, transitions, and
	 * potentially a background image and border.
	 */
protected String getHotspotString(IWidget widget, String properties, String eventString) {
    properties = properties.substring(0, properties.length() - 1);
    ImageLoader imageLoader = new ImageLoader();
    Image img = buildWidgetImage(widget);
    if (img != null) {
        imageLoader.data = new ImageData[] { img.getImageData() };
        String imageName = getWidgetFileName(widget, ".jpg");
        // create new FILE handler for the new imageSaver's use
        File imageFile = new File(parentDir, imageName);
        try {
            imageLoader.save(imageFile.getCanonicalPath(), SWT.IMAGE_JPEG);
        } catch (IOException ex) {
            // We can continue even with exceptions on individual images
            throw new ImageException("Failed saving image for HTML export", ex);
        }
        properties += " background-image: url(" + imageName + ");";
        // dispose the image, it's not needed any more.
        img.dispose();
    }
    return "<div " + properties + "\"" + eventString + ">" + widget.getTitle() + "</div>\n";
}
Also used : ImageException(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil.ImageException) IOException(java.io.IOException) ImageLoader(org.eclipse.swt.graphics.ImageLoader) Image(org.eclipse.swt.graphics.Image) File(java.io.File)

Aggregations

ImageException (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil.ImageException)2 File (java.io.File)2 IOException (java.io.IOException)2 Image (org.eclipse.swt.graphics.Image)2 ImageLoader (org.eclipse.swt.graphics.ImageLoader)2 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)1 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1