Search in sources :

Example 1 with RcvrImageException

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

the class SEDemoUI method addSelectionChangeListeners.

/**
     * Add the selection Change event listeners.
     */
protected void addSelectionChangeListeners() {
    SWTselectionChangeHandler = new SelectionListener() {

        public void widgetSelected(SelectionEvent evt) {
            SWTList swtList = view.getScriptEditorList();
            TableItem[] selectedItems = swtList.getSelectionObject();
            for (TableItem selectedItem : selectedItems) {
                // TODO: Currently supports only single selection.
                Object data = selectedItem.getData();
                if (data instanceof Frame) {
                    selection.setSelectedState(null);
                } else {
                    // should be instanceof DefaultModelGeneratorState!!!
                    AScriptStep step = ((DefaultModelGeneratorState) data).getScriptStep();
                    if (step instanceof HearScriptStep) {
                        if (interaction.askEditFrame()) {
                            Frame f = step.getCurrentFrame();
                            performAction(DesignEditorLID.EditFrame, f);
                        }
                        TableItem previousSelectedRow = swtList.getRowItem(selection.getPreviousSelection());
                        selection.setSelectedState(previousSelectedRow);
                    } else {
                        selection.setSelectedState(selectedItem);
                    }
                }
            }
            centerSelectedRegion();
            setViewEnabledState(selection, ListenerIdentifierMap.NORMAL);
        // Let selection change handle changing the frame
        }

        public void widgetDefaultSelected(SelectionEvent evt) {
            SWTList swtList = view.getScriptEditorList();
            TableItem[] selectedItems = swtList.getSelectionObject();
            // TODO: Currently supports only single selection.
            for (TableItem selectedItem : selectedItems) {
                Object data = selectedItem.getData();
                if (data instanceof DefaultModelGeneratorState) {
                    DefaultModelGeneratorState stepState = (DefaultModelGeneratorState) data;
                    AScriptStep step = stepState.getScriptStep();
                    // In case we need this; not a context selection
                    // in truth, but we can re-use the structure.
                    contextSelection.setSelectedState(stepState);
                    if (editable) {
                        if (step instanceof DelayScriptStep) {
                            performAction(SEDemoLID.ChangeWaitProperties, contextSelection);
                        } else if (step instanceof ThinkScriptStep) {
                            performAction(SEDemoLID.ChangeThinkProperties, contextSelection);
                        } else if (isEditable(step)) {
                            performAction(SEDemoLID.Edit, contextSelection);
                        }
                    }
                }
            }
        }
    };
    view.addSWTListSelectionHandler(SWTselectionChangeHandler);
    AlertHandler selectionChangeHandler = new AlertHandler() {

        public void handleAlert(EventObject alert) {
            SEDemoSelectionState.StepStateSelectionChange event = (SEDemoSelectionState.StepStateSelectionChange) alert;
            if (event != null) {
                if (event.selected) {
                    DefaultModelGeneratorState stepState = event.changedState;
                    uiModel.setCurrentOverride(script, script.getPreviousState(stepState));
                    Frame resultFrame = null;
                    if (stepState != null) {
                        resultFrame = stepState.getScriptStep().getCurrentFrame();
                    } else {
                        resultFrame = script.getDemonstration().getResultFrame();
                    }
                    try {
                        setCurrentFrame(resultFrame);
                    } catch (GraphicsUtil.ImageException ex) {
                        throw new RcvrImageException("Changing current demonstration frame", ex);
                    }
                } else {
                    // deselect item.
                    uiModel.setCurrentOverride(script, script.getLastState());
                }
            }
        }
    };
    selection.addHandler(this, SEDemoSelectionState.StepStateSelectionChange.class, selectionChangeHandler);
}
Also used : DelayScriptStep(edu.cmu.cs.hcii.cogtool.model.DelayScriptStep) Frame(edu.cmu.cs.hcii.cogtool.model.Frame) TableItem(org.eclipse.swt.widgets.TableItem) AScriptStep(edu.cmu.cs.hcii.cogtool.model.AScriptStep) EventObject(java.util.EventObject) ThinkScriptStep(edu.cmu.cs.hcii.cogtool.model.ThinkScriptStep) SWTList(edu.cmu.cs.hcii.cogtool.view.SWTList) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) SelectionEvent(org.eclipse.swt.events.SelectionEvent) HearScriptStep(edu.cmu.cs.hcii.cogtool.model.HearScriptStep) EventObject(java.util.EventObject) AlertHandler(edu.cmu.cs.hcii.cogtool.util.AlertHandler) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) DefaultModelGeneratorState(edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 2 with RcvrImageException

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

the class ScriptViewerUI method addSelectionChangeListeners.

/**
     * Add the selection Change event listeners.
     */
protected void addSelectionChangeListeners() {
    SWTselectionChangeHandler = new SelectionListener() {

        public void widgetSelected(SelectionEvent evt) {
            SWTList swtList = view.getScriptEditorList();
            TableItem[] selectedItems = swtList.getSelectionObject();
            for (TableItem selectedItem : selectedItems) {
                // TODO: Currently supports only single selection.
                Object data = selectedItem.getData();
                if (data instanceof Frame) {
                    selection.setSelectedState(null);
                } else {
                    selection.setSelectedState(selectedItem);
                }
            }
            setViewEnabledState(selection, ListenerIdentifierMap.NORMAL);
        // Let selection change handle changing the frame
        }

        public void widgetDefaultSelected(SelectionEvent evt) {
            SWTList swtList = view.getScriptEditorList();
            TableItem[] selectedItems = swtList.getSelectionObject();
            // TODO: Currently supports only single selection.
            for (TableItem selectedItem : selectedItems) {
                Object data = selectedItem.getData();
                if (data instanceof DefaultModelGeneratorState) {
                    Script s = (Script) selectedItem.getData(SWTListGroupScript.SCRIPT_DATA_KEY);
                    DefaultModelGeneratorState stepState = (DefaultModelGeneratorState) data;
                    // In case we need this; not a context selection
                    // in truth, but we can re-use the structure.
                    contextSelection.setSelectedState(stepState);
                    performAction(ProjectLID.EditScript, s);
                }
            }
        }
    };
    view.addSWTListSelectionHandler(SWTselectionChangeHandler);
    AlertHandler selectionChangeHandler = new AlertHandler() {

        public void handleAlert(EventObject alert) {
            SEDemoSelectionState.StepStateSelectionChange event = (SEDemoSelectionState.StepStateSelectionChange) alert;
            if (event != null) {
                if (event.selected) {
                    DefaultModelGeneratorState stepState = event.changedState;
                    Frame resultFrame = null;
                    Script script = getSelectedScript(stepState);
                    AUndertaking t = script.getDemonstration().getTaskApplication().getTask();
                    if (stepState != null) {
                        resultFrame = stepState.getScriptStep().getCurrentFrame();
                    } else if (script != null) {
                        resultFrame = script.getDemonstration().getResultFrame();
                    }
                    DefaultModelGeneratorState overrideState = script.getPreviousState(stepState);
                    if (overrideState == null) {
                        TaskGroup group = (TaskGroup) task;
                        List<AUndertaking> siblingTasks = group.getUndertakings();
                        int currentTaskIndex = siblingTasks.indexOf(t);
                        CognitiveModelGenerator modelGen = script.getModelGenerator();
                        while ((overrideState == null) && (0 <= --currentTaskIndex)) {
                            AUndertaking prevSibling = siblingTasks.get(currentTaskIndex);
                            TaskApplication ta = project.getTaskApplication(prevSibling, design);
                            if (ta != null) {
                                script = ta.getScript(modelGen);
                                if (script != null) {
                                    overrideState = script.getLastState();
                                }
                            }
                        }
                    }
                    uiModel.setCurrentOverride(script, overrideState);
                    try {
                        setCurrentFrame(resultFrame);
                    } catch (GraphicsUtil.ImageException ex) {
                        throw new RcvrImageException("Changing current demonstration frame", ex);
                    }
                }
            }
        }
    };
    selection.addHandler(this, SEDemoSelectionState.StepStateSelectionChange.class, selectionChangeHandler);
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) SWTListGroupScript(edu.cmu.cs.hcii.cogtool.view.SWTListGroupScript) Frame(edu.cmu.cs.hcii.cogtool.model.Frame) TableItem(org.eclipse.swt.widgets.TableItem) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) EventObject(java.util.EventObject) SWTList(edu.cmu.cs.hcii.cogtool.view.SWTList) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EventObject(java.util.EventObject) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) AlertHandler(edu.cmu.cs.hcii.cogtool.util.AlertHandler) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) DefaultModelGeneratorState(edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 3 with RcvrImageException

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

the class Controller method open.

/**
     * Prompts for and opens a file.
     *
     * @throws RcvrIOLoadException if the open fails
     */
protected boolean open(DoublePoint loc, File[] openLocs) {
    try {
        // If the open dialog was canceled, do nothing.
        if (openLocs != null) {
            Interaction interaction = getUI().getStandardInteraction();
            // Otherwise, open all of the selected projects.
            for (int i = 0; i < openLocs.length; i++) {
                String statusMsg = null;
                if (!openLocs[i].exists()) {
                    interaction.protestFileNotFound(openLocs[i].getName());
                    continue;
                }
                Project proj = null;
                try {
                    proj = (Project) persist.isLoaded(openLocs[i]);
                    if (proj != null) {
                        if (UndoManager.isAtSavePoint(proj)) {
                            statusMsg = L10N.get("AC.OpenNotModified", "Project already open and not modified");
                        } else {
                            boolean revert = interaction.askRevertBeforeOpen(proj.getName());
                            if (revert) {
                                closeProject(proj, false);
                            } else {
                                continue;
                            }
                        }
                    }
                    proj = (Project) persist.load(openLocs[i]);
                } catch (RuntimeException e) {
                    // format
                    throw new RcvrIOLoadException(("Error loading project: " + e.getMessage()), e);
                }
                CogToolPref.setRecent(openLocs[i].getCanonicalPath());
                // Reset the project name in case the file was renamed
                String name = openLocs[i].getName();
                // Remove the .cgt, if it is there.
                int periodIndex = name.lastIndexOf('.');
                if (periodIndex > 0) {
                    name = name.substring(0, periodIndex);
                }
                proj.setName(name);
                // Create a window; the project is not yet registered
                // and is unmodified.
                ProjectController c = ProjectController.openController(proj, false, true);
                UI ui = getUI();
                if ((loc != null) && (ui != null)) {
                    c.getUI().setLocation(loc);
                }
                if (statusMsg != null) {
                    c.interaction.setStatusMessage(statusMsg);
                }
            }
            return true;
        }
        return false;
    } catch (IOException e) {
        throw new RcvrIOLoadException("Error loading project: " + e.getMessage(), e);
    } catch (GraphicsUtil.ImageException e) {
        throw new RcvrImageException("Error loading image from project: " + e.getMessage(), e);
    }
}
Also used : Interaction(edu.cmu.cs.hcii.cogtool.ui.Interaction) IOException(java.io.IOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) Project(edu.cmu.cs.hcii.cogtool.model.Project) RcvrIOLoadException(edu.cmu.cs.hcii.cogtool.util.RcvrIOLoadException) UI(edu.cmu.cs.hcii.cogtool.ui.UI) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)

Example 4 with RcvrImageException

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

the class DesignEditorController method setBackgroundImageOnFrames.

/**
	 * Sets the background image for the array of frames
	 *
	 * @param frames an array of IFrames
	 * @param imageData the new image, or null if none
	 * @param imageURL the file path of the new image, or
	 * <code>WidgetAttributes.NO_IMAGE</code>
	 */
protected void setBackgroundImageOnFrames(final Frame[] frames, final byte[] imageData, final String imageURL, CogToolLID editLID) {
    try {
        // compute the size of the new image.
        final DoubleRectangle imageSize = GraphicsUtil.getImageBounds(imageData);
        // save previous data for undo/redo
        final Map<Frame, ImageData> previousImageData = getBackgroundImageData(frames);
        // do operation on all frames
        for (Frame frame : frames) {
            frame.setBackgroundImage(imageData, imageSize);
            frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageURL);
        }
        // Add the undo edit
        IUndoableEdit edit = new AUndoableEdit(editLID) {

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

            @Override
            public void redo() {
                super.redo();
                // selected frames
                try {
                    for (Frame frame : frames) {
                        frame.setBackgroundImage(imageData, imageSize);
                        frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageURL);
                    }
                } catch (GraphicsUtil.ImageException ex) {
                    throw new RcvrImageException("Redo set background image failed", ex);
                }
            }

            @Override
            public void undo() {
                super.undo();
                try {
                    // iterate through frames
                    Iterator<Entry<Frame, ImageData>> imageEntryIterator = previousImageData.entrySet().iterator();
                    while (imageEntryIterator.hasNext()) {
                        Entry<Frame, ImageData> imageEntry = imageEntryIterator.next();
                        Frame f = imageEntry.getKey();
                        ImageData id = imageEntry.getValue();
                        f.setBackgroundImage(id.data, id.bounds);
                        f.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, id.imageURL);
                    }
                } catch (GraphicsUtil.ImageException ex) {
                    throw new RcvrImageException("Undo set background image failed", ex);
                }
            }
        };
        undoMgr.addEdit(edit);
        for (Frame frame : frames) {
            UndoManager frameMgr = UndoManager.getUndoManager(frame, project);
            frameMgr.addEdit(edit);
        }
    } catch (GraphicsUtil.ImageException e) {
        // setBackgroundImage and GraphicsUtil.getImageBounds may
        // throw ImageException, translating any other exception.
        interaction.protestInvalidImageFile();
    }
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) Entry(java.util.Map.Entry) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) 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)

Example 5 with RcvrImageException

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

the class GraphicalDevice method paintFigure.

/**
     * Override to perform drawing tasks common to all graphical sources
     */
@Override
protected void paintFigure(Graphics g) {
    Dimension size = getSize();
    // Workaround Draw2D's inability to draw 0-size images.
    int width = size.width;
    if (width == 0) {
        width = 1;
    }
    int height = size.height;
    if (height == 0) {
        height = 1;
    }
    Image scaled = null;
    try {
        // Draw foreground sheen (not selected)
        foreground.setAlpha(0, 0, displayAlpha.determineAlpha(false));
        // TODO: Can this be cached more efficiently?
        scaled = new Image(null, foreground.scaledTo(width, height));
        Point location = getLocation();
        g.drawImage(scaled, location.x, location.y);
    } catch (SWTException ex) {
        throw new RcvrImageException("Drawing device foreground image failed", ex);
    } finally {
        if (scaled != null) {
            scaled.dispose();
        }
    }
    super.paintFigure(g);
    button.paintFigure(g);
}
Also used : SWTException(org.eclipse.swt.SWTException) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) Dimension(org.eclipse.draw2d.geometry.Dimension) Point(org.eclipse.draw2d.geometry.Point) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.draw2d.geometry.Point)

Aggregations

RcvrImageException (edu.cmu.cs.hcii.cogtool.util.RcvrImageException)7 GraphicsUtil (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)6 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)3 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)3 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)2 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)2 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)2 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)2 AlertHandler (edu.cmu.cs.hcii.cogtool.util.AlertHandler)2 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)2 SWTList (edu.cmu.cs.hcii.cogtool.view.SWTList)2 IOException (java.io.IOException)2 EventObject (java.util.EventObject)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 SelectionListener (org.eclipse.swt.events.SelectionListener)2 TableItem (org.eclipse.swt.widgets.TableItem)2 CogToolLID (edu.cmu.cs.hcii.cogtool.CogToolLID)1 AScriptStep (edu.cmu.cs.hcii.cogtool.model.AScriptStep)1 CognitiveModelGenerator (edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator)1 DelayScriptStep (edu.cmu.cs.hcii.cogtool.model.DelayScriptStep)1