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);
}
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);
}
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);
}
}
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();
}
}
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);
}
Aggregations