Search in sources :

Example 16 with Frame

use of edu.cmu.cs.hcii.cogtool.model.Frame 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 17 with Frame

use of edu.cmu.cs.hcii.cogtool.model.Frame in project cogtool by cogtool.

the class DesignEditorUI method createFrameSelectionHandler.

protected AlertHandler createFrameSelectionHandler() {
    return new AlertHandler() {

        public void handleAlert(EventObject alert) {
            FrameSelectionChange evt = (FrameSelectionChange) alert;
            ActionPropertySet actionProps = view.getActionPropertySet();
            if (evt != null) {
                if (evt.changedFrameFigure != null) {
                    evt.changedFrameFigure.setSelected(evt.selected);
                    // Handle property sheet selection
                    Frame[] selectedFrames = selection.getSelectedFrames();
                    int selectedFrameCount = selectedFrames.length;
                    if (selectedFrameCount > 0) {
                        actionProps.setComposite(ActionPropertySet.FRAME);
                        if (selectedFrameCount == 1) {
                            actionProps.setFrameName(selectedFrames[0]);
                        }
                    } else {
                        actionProps.setComposite(ActionSet.USE_NONE);
                    }
                    actionProps.enableFrameName(selectedFrameCount == 1);
                } else {
                    actionProps.setComposite(ActionSet.USE_NONE);
                    Iterator<DesignEditorFrame> frameFigures = selection.getSelectedFrameFigures();
                    while (frameFigures.hasNext()) {
                        DesignEditorFrame frameFigure = frameFigures.next();
                        frameFigure.setSelected(evt.selected);
                    }
                }
                // Repaint the frame contents
                delayedRepainting.requestRepaint(REPAINT_ALL);
            }
        }
    };
}
Also used : ActionPropertySet(edu.cmu.cs.hcii.cogtool.view.ActionPropertySet) FrameSelectionChange(edu.cmu.cs.hcii.cogtool.ui.DesignEditorSelectionState.FrameSelectionChange) Frame(edu.cmu.cs.hcii.cogtool.model.Frame) DesignEditorFrame(edu.cmu.cs.hcii.cogtool.uimodel.DesignEditorFrame) DesignEditorFrame(edu.cmu.cs.hcii.cogtool.uimodel.DesignEditorFrame) AlertHandler(edu.cmu.cs.hcii.cogtool.util.AlertHandler) EventObject(java.util.EventObject) Point(org.eclipse.draw2d.geometry.Point)

Example 18 with Frame

use of edu.cmu.cs.hcii.cogtool.model.Frame in project cogtool by cogtool.

the class DesignEditorUI method confirmRenameFrame.

protected boolean confirmRenameFrame() {
    boolean success = true;
    if ((editor != null) && editor.inUse()) {
        String newName = editor.getText();
        DesignEditorFrame frameFigure = (DesignEditorFrame) editor.getData();
        Frame frameToRename = frameFigure.getFrame();
        cleanupFrameEditor(frameFigure);
        success = performAction(DesignEditorLID.RenameFrame, new DesignEditorUI.FrameRenameParameters(frameToRename, newName), false);
    }
    return success;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) DesignEditorFrame(edu.cmu.cs.hcii.cogtool.uimodel.DesignEditorFrame) DesignEditorFrame(edu.cmu.cs.hcii.cogtool.uimodel.DesignEditorFrame)

Example 19 with Frame

use of edu.cmu.cs.hcii.cogtool.model.Frame in project cogtool by cogtool.

the class DesignEditorController method createPasteAction.

protected IListenerAction createPasteAction() {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            try {
                Collection<Object> objects = CogToolClipboard.fetchCogToolObjects();
                if ((objects != null) && (objects.size() > 0)) {
                    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(L10N.get("UNDO.Paste", "Paste"), DesignEditorLID.Paste);
                    Set<DeviceType> devTypes = design.getDeviceTypes();
                    int numPasted = 0;
                    Iterator<Object> objIt = objects.iterator();
                    while (objIt.hasNext()) {
                        Object o = objIt.next();
                        if (o instanceof Frame) {
                            Frame frame = (Frame) o;
                            makeFrameNameUnique(frame);
                            // Find an unoccupied starting position
                            // by cascading.
                            DoublePoint origin = frame.getFrameOrigin();
                            DesignUtil.findDistinctOrigin(design, origin, 16.0, 16.0);
                            // Union devices
                            Iterator<InputDevice> frameDevices = frame.getInputDevices().iterator();
                            while (frameDevices.hasNext()) {
                                InputDevice inputDevice = frameDevices.next();
                                DeviceType devType = inputDevice.getDeviceType();
                                if (!devTypes.contains(devType)) {
                                    DesignCmd.addDevice(design, devType);
                                }
                            }
                            Iterator<DeviceType> designDevTypes = devTypes.iterator();
                            while (designDevTypes.hasNext()) {
                                DeviceType devType = designDevTypes.next();
                                if (frame.getInputDevice(devType) == null) {
                                    frame.addInputDevice(devType);
                                }
                            }
                            addFrame(frame, editSequence);
                            numPasted++;
                        } else if (o instanceof Transition) {
                            Transition t = (Transition) o;
                            DeviceType device = t.getAction().getDefaultDeviceType();
                            if (!devTypes.contains(device)) {
                                DesignCmd.addDevice(design, device);
                            }
                            IUndoableEdit edit = DesignEditorCmd.addTransition(demoStateMgr, t);
                            editSequence.addEdit(edit);
                            numPasted++;
                        }
                    }
                    editSequence.end();
                    undoMgr.addEdit(editSequence);
                    interaction.setStatusMessage(numPasted + " " + pasteComplete);
                } else {
                    interaction.setStatusMessage(nothingPasted);
                }
            } catch (IOException e) {
                throw new RcvrClipboardException(e);
            } catch (ParserConfigurationException e) {
                throw new RcvrClipboardException(e);
            } catch (SAXException e) {
                throw new RcvrClipboardException(e);
            } catch (ClipboardUtil.ClipboardException e) {
                throw new RcvrClipboardException(e);
            }
            return true;
        }
    };
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) InputDevice(edu.cmu.cs.hcii.cogtool.model.InputDevice) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) IOException(java.io.IOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SAXException(org.xml.sax.SAXException) DeviceType(edu.cmu.cs.hcii.cogtool.model.DeviceType) ClipboardUtil(edu.cmu.cs.hcii.cogtool.util.ClipboardUtil) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) Transition(edu.cmu.cs.hcii.cogtool.model.Transition) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 20 with Frame

use of edu.cmu.cs.hcii.cogtool.model.Frame in project cogtool by cogtool.

the class DesignEditorController method createCutFrameAction.

protected IListenerAction createCutFrameAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return FrameSelectionState.class;
        }

        public boolean performAction(Object prms) {
            FrameSelectionState seln = (FrameSelectionState) prms;
            Frame[] frames = seln.getSelectedFrames();
            if ((frames != null) && (frames.length > 0)) {
                copyFrames(frames);
                deleteFrames(frames);
                return true;
            }
            interaction.protestNoSelection();
            return false;
        }
    };
}
Also used : FrameSelectionState(edu.cmu.cs.hcii.cogtool.ui.FrameSelectionState) Frame(edu.cmu.cs.hcii.cogtool.model.Frame) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction)

Aggregations

Frame (edu.cmu.cs.hcii.cogtool.model.Frame)68 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)12 DesignEditorFrame (edu.cmu.cs.hcii.cogtool.uimodel.DesignEditorFrame)10 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)10 EventObject (java.util.EventObject)9 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)8 FrameSelectionState (edu.cmu.cs.hcii.cogtool.ui.FrameSelectionState)8 AlertHandler (edu.cmu.cs.hcii.cogtool.util.AlertHandler)8 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)8 Transition (edu.cmu.cs.hcii.cogtool.model.Transition)7 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)7 IOException (java.io.IOException)6 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)5 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)5 GraphicsUtil (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)5 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)4 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)4 Design (edu.cmu.cs.hcii.cogtool.model.Design)4 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)4 TransitionSource (edu.cmu.cs.hcii.cogtool.model.TransitionSource)4