Search in sources :

Example 31 with IUndoableEdit

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

the class ImportWebCrawlThread method doneCallback.

/**
     * For each page visited and parsed, create a corresponding Frame.
     * For each child link, create a corresponding Widget and Transition.
     */
@Override
public void doneCallback() {
    // Performed by the main UI thread
    try {
        // If an exception was thrown during the import, display error here
        if (RcvrExceptionHandler.recoverWorkThread(this, interaction)) {
            return;
        }
        if (isCanceled()) {
            return;
        }
        DemoStateManager demoStateMgr = DemoStateManager.getStateManager(project, design);
        if (isPaused()) {
            DefaultCmd.setAttribute(design, demoStateMgr, WidgetAttributes.PAUSED_WEB_CRAWL_ATTR, importWeb.getURLsToCrawl(), interaction, editSequence);
        }
        // -1 means that the design already is part of the project
        if (insertBeforeIndex != EXISTING_DESIGN) {
            ProjectCmd.addNewDesign(project, design, insertBeforeIndex, IMPORT_WEB_DESIGN, editSequence);
        }
        Collection<PageInfo> crawledURLs = importWeb.getCrawledURLs();
        Iterator<PageInfo> pagesVisited = crawledURLs.iterator();
        Set<DeviceType> deviceTypes = design.getDeviceTypes();
        // Map (Link) IWidget to URL
        Map<IWidget, String> neededTransitions = new HashMap<IWidget, String>();
        int minFrameWidth = DesignUtil.getFrameMinWidth();
        int minFrameHeight = DesignUtil.getFrameMinHeight();
        double frameScale = DesignUtil.getFrameScaleFactor();
        DesignUtil.IFrameSituator frameSituator = new DesignUtil.ByRowFrameSituator(0.0, 0.0, 16.0, 16.0, minFrameWidth, minFrameHeight, CogToolPref.FRAMES_PER_ROW.getInt(), frameScale);
        while (pagesVisited.hasNext()) {
            ImportWebURL.ImportPageInfo page = (ImportWebURL.ImportPageInfo) pagesVisited.next();
            Frame newFrame = new Frame(page.url, deviceTypes);
            knownFrames.put(page.url, newFrame);
            if (page.background != null) {
                DoubleRectangle bds = new DoubleRectangle(page.bkgImageX, page.bkgImageY, page.bkgImageWidth, page.bkgImageHeight);
                newFrame.setBackgroundImage(page.background, bds);
            }
            int linkCount = 0;
            Iterator<URLLabeledLink> links = page.links.iterator();
            while (links.hasNext()) {
                URLPositionedLink link = (URLPositionedLink) links.next();
                // of the page)
                if ((Math.round(link.width) == 0.0) || (Math.round(link.height) == 0.0)) {
                    continue;
                }
                IWidget linkWidget = new Widget(new DoubleRectangle(link.left, link.top, link.width, link.height), WidgetType.Link);
                linkWidget.setName("Widget " + Integer.toString(++linkCount));
                linkWidget.setTitle(StringUtil.trimWhitespace(link.getLabel()));
                newFrame.addWidget(linkWidget);
                if (deviceTypes.contains(DeviceType.Mouse)) {
                    String linkURL = link.getURL();
                    Frame targetFrame = knownFrames.get(linkURL);
                    if (targetFrame != null) {
                        Transition t = new Transition(linkWidget, targetFrame, buildLinkAction());
                        IUndoableEdit edit = DesignEditorCmd.addTransition(demoStateMgr, t);
                        editSequence.addEdit(edit);
                    } else {
                        // Have to handle this in the second pass
                        neededTransitions.put(linkWidget, linkURL);
                    }
                }
            }
            Frame oldFrame = design.getFrame(newFrame.getName());
            if (pruneSameURLs) {
                if (oldFrame != null) {
                    makeFrameNameUnique(newFrame);
                }
            } else {
                // If oldFrame exists, remove but keep incident transitions
                if (oldFrame != null) {
                    Set<Transition> transitions = oldFrame.getIncidentTransitions();
                    synchronized (transitions) {
                        // them without upsetting the iterator
                        for (Transition transition : new ArrayList<Transition>(transitions)) {
                            DesignEditorCmd.changeTransitionTarget(demoStateMgr, transition, newFrame, editSequence);
                        }
                        //transitions=transitions2;
                        // Can't delete the transitive closure from here...sigh
                        DesignEditorCmd.deleteFrame(project, design, demoStateMgr, oldFrame, ProjectLID.ImportWebCrawl, editSequence);
                    }
                }
            }
            frameSituator.situateNextFrame(newFrame);
            DesignEditorCmd.addFrame(project, design, demoStateMgr, newFrame, editSequence);
        }
        // Each entry is IWidget --> URL string
        for (Map.Entry<IWidget, String> checkTransition : neededTransitions.entrySet()) {
            String transitionURL = checkTransition.getValue();
            Frame targetFrame = knownFrames.get(transitionURL);
            // processing is done (i.e., added during background processing).
            if (targetFrame == null) {
                targetFrame = design.getFrame(transitionURL);
            }
            // May just not be there; can't link if it's not there!
            if (targetFrame != null) {
                IWidget linkWidget = checkTransition.getKey();
                Transition t = new Transition(linkWidget, targetFrame, buildLinkAction());
                IUndoableEdit edit = DesignEditorCmd.addTransition(demoStateMgr, t);
                editSequence.addEdit(edit);
            }
        }
        editSequence.end();
        undoMgr.addEdit(editSequence);
    } finally {
        // Recover resources.
        importWeb.dispose();
        super.doneCallback();
    }
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) HashMap(java.util.HashMap) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) Widget(edu.cmu.cs.hcii.cogtool.model.Widget) ArrayList(java.util.ArrayList) DesignUtil(edu.cmu.cs.hcii.cogtool.model.DesignUtil) URLLabeledLink(edu.cmu.cs.hcii.cogtool.model.URLLabeledLink) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) DeviceType(edu.cmu.cs.hcii.cogtool.model.DeviceType) PageInfo(edu.cmu.cs.hcii.cogtool.controller.WebCrawler.PageInfo) URLPositionedLink(edu.cmu.cs.hcii.cogtool.model.URLPositionedLink) Transition(edu.cmu.cs.hcii.cogtool.model.Transition) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) HashMap(java.util.HashMap) Map(java.util.Map) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 32 with IUndoableEdit

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

the class FrameEditorController method createSetSkinAction.

private AListenerAction createSetSkinAction(final SkinType newSkin, final CogToolLID lid) {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            final SkinType oldSkin = design.getSkin();
            design.setSkin(newSkin);
            IUndoableEdit edit = new AUndoableEdit(lid) {

                @Override
                public String getPresentationName() {
                    return CHANGE_SKIN;
                }

                @Override
                public void redo() {
                    super.redo();
                    design.setSkin(newSkin);
                }

                @Override
                public void undo() {
                    super.undo();
                    design.setSkin(oldSkin);
                }
            };
            UndoManager designUndoMgr = UndoManager.getUndoManager(design, project);
            designUndoMgr.addEdit(edit);
            undoMgr.addEdit(edit);
            return true;
        }
    };
}
Also used : UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) SkinType(edu.cmu.cs.hcii.cogtool.model.SkinType) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 33 with IUndoableEdit

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

the class ProjectController method createDuplicateTaskAppAction.

protected IListenerAction createDuplicateTaskAppAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object p) {
            if (p != null) {
                ProjectUI.MoveCopyTaskApplicationParms parms = (ProjectUI.MoveCopyTaskApplicationParms) p;
                // Must have selected tasks and design
                if ((parms.design == null) || (parms.fromTask == null) || (parms.toTask == null)) {
                    interaction.protestNoSelection();
                    return false;
                }
                final AUndertaking fromTask = parms.fromTask;
                final AUndertaking toTask = parms.toTask;
                TaskApplication taskApp = project.getTaskApplication(fromTask, parms.design);
                if (taskApp == null) {
                    interaction.protestNoTaskApplication();
                    return false;
                }
                final TaskApplication oldTaskApp = project.removeTaskApplication(parms.toTask, parms.design);
                final TaskApplication newTaskApp = taskApp.duplicate(toTask, parms.design);
                DemoStateManager demoMgr = DemoStateManager.getStateManager(project, parms.design);
                project.setTaskApplication(newTaskApp);
                demoMgr.trackEdits(newTaskApp.getDemonstration());
                IUndoableEdit edit = new AUndoableEdit(ProjectLID.DuplicateTaskApplication) {

                    protected boolean recoverMgrs = false;

                    @Override
                    public String getPresentationName() {
                        return DUPLICATE_TASKAPP;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        if (oldTaskApp != null) {
                            project.removeTaskApplication(oldTaskApp);
                        }
                        project.setTaskApplication(newTaskApp);
                        recoverMgrs = false;
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        project.removeTaskApplication(newTaskApp);
                        if (oldTaskApp != null) {
                            project.setTaskApplication(oldTaskApp);
                        }
                        recoverMgrs = true;
                    }

                    @Override
                    public void die() {
                        if (recoverMgrs) {
                            recoverManagers(newTaskApp);
                        }
                    }
                };
                undoMgr.addEdit(edit);
            }
            return true;
        }
    };
}
Also used : ProjectUI(edu.cmu.cs.hcii.cogtool.ui.ProjectUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 34 with IUndoableEdit

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

the class FrameEditorController method setBackgroundImage.

/**
     * Sets the background image for the frame
     *
     * @param imageData the new image, or null if none
     */
private void setBackgroundImage(final byte[] imageData, final String imagePath) {
    try {
        // compute the size of the new image.
        final DoubleRectangle imageSize = GraphicsUtil.getImageBounds(imageData);
        // Get existing image
        final byte[] previousImageData = model.getBackgroundImage();
        final DoubleRectangle previmageSize = model.getBackgroundBounds();
        final String oldPath = (String) model.getAttribute(WidgetAttributes.IMAGE_PATH_ATTR);
        // Perform operation
        model.setBackgroundImage(imageData, imageSize);
        model.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imagePath);
        CogToolLID lid = (imageData == null) ? FrameEditorLID.RemoveBackgroundImage : FrameEditorLID.SetBackgroundImage;
        // Add the undo edit
        IUndoableEdit edit = new AUndoableEdit(lid) {

            @Override
            public String getPresentationName() {
                return (imageData == null) ? REMOVE_BKG_IMG : SET_BKG_IMG;
            }

            @Override
            public void redo() {
                super.redo();
                try {
                    model.setBackgroundImage(imageData, imageSize);
                    model.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imagePath);
                } catch (GraphicsUtil.ImageException ex) {
                    throw new RcvrImageException("Redo set background image failed", ex);
                }
            }

            @Override
            public void undo() {
                super.undo();
                try {
                    model.setBackgroundImage(previousImageData, previmageSize);
                    model.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, oldPath);
                } catch (GraphicsUtil.ImageException ex) {
                    throw new RcvrImageException("Undo set background image failed", ex);
                }
            }
        };
        undoMgr.addEdit(edit);
    } catch (GraphicsUtil.ImageException e) {
        interaction.protestInvalidImageFile();
    }
}
Also used : CogToolLID(edu.cmu.cs.hcii.cogtool.CogToolLID) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)

Example 35 with IUndoableEdit

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

the class FrameEditorController method renameEltGroup.

private boolean renameEltGroup(final FrameElementGroup eltGroup, String tryName) {
    final String oldName = eltGroup.getName();
    boolean notDone = true;
    do {
        if (tryName.length() == 0) {
            tryName = interaction.protestNameCannotBeEmpty(DEFAULT_GROUP_PREFIX);
            // If canceled, indicate so; otherwise, test again
            if (tryName == null) {
                return false;
            }
        } else {
            FrameElementGroup groupForName = model.getEltGroup(tryName);
            // then no change is necessary!
            if (groupForName == eltGroup) {
                notDone = false;
            } else if (groupForName != null) {
                // A non-null widget for the tryName indicates a collision
                tryName = interaction.protestNameCollision(DEFAULT_GROUP_PREFIX);
                // If canceled, indicate so; otherwise, test again
                if (tryName == null) {
                    return false;
                }
            } else {
                // Not canceled, not empty, and not a collision
                notDone = false;
                final String newName = tryName;
                model.setEltGroupName(newName, eltGroup);
                IUndoableEdit edit = new AUndoableEdit(FrameEditorLID.ChangeNameProperty) {

                    @Override
                    public String getPresentationName() {
                        return CHG_WIDGET_NAME;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        model.setEltGroupName(newName, eltGroup);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        model.setEltGroupName(oldName, eltGroup);
                    }
                };
                undoMgr.addEdit(edit);
            }
        }
    } while (notDone);
    return true;
}
Also used : AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Aggregations

IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)44 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)34 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)12 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)12 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)10 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)10 ComputationUndoRedo (edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo)9 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)8 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)8 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)7 UndoManager (edu.cmu.cs.hcii.cogtool.util.UndoManager)7 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)5 FrameElementGroup (edu.cmu.cs.hcii.cogtool.model.FrameElementGroup)5 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)5 Design (edu.cmu.cs.hcii.cogtool.model.Design)4 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)4 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)4