use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit 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.IUndoableEdit in project cogtool by cogtool.
the class DictEntryGenerator method insertEntries.
public void insertEntries(final DictionaryEditorController dec, final boolean computeAll, final ITermSimilarity oldAlg, final ITermSimilarity alg, Project project, IUndoableEditSequence editSequence) {
final ISimilarityDictionary dict = (ISimilarityDictionary) design.getAttribute(WidgetAttributes.DICTIONARY_ATTR);
if ((newDictEntries.size() > 0) || (updatedDictEntries.size() > 0)) {
dict.insertEntries(newDictEntries, computeAll);
dict.insertEntries(updatedDictEntries, computeAll);
IUndoableEdit edit = new AUndoableEdit(ProjectLID.GenerateDictionary) {
@Override
public String getPresentationName() {
return GENERATE_DICTIONARY;
}
@Override
public void redo() {
super.redo();
dict.insertEntries(newDictEntries, computeAll);
dict.insertEntries(updatedDictEntries, computeAll);
dec.setCurrentAlgorithm(alg);
}
@Override
public void undo() {
super.undo();
dict.removeEntries(newDictEntries);
dict.insertEntries(oldDictEntries, computeAll);
dec.setCurrentAlgorithm(oldAlg);
}
};
editSequence.addEdit(edit);
UndoManager.getUndoManager(dict, project).addEdit(edit);
} else {
dec.interaction.setStatusMessage("No entries to compute.");
}
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class DictionaryEditorController method createSetStringAction.
protected IListenerAction createSetStringAction(final int column) {
return new IListenerAction() {
public Class<?> getParameterClass() {
return DictionaryEditorUI.SetStringParms.class;
}
public boolean performAction(Object actionParms) {
DictionaryEditorUI.SetStringParms p = (DictionaryEditorUI.SetStringParms) actionParms;
if (p != null) {
final int rowIndex = p.rowIndex;
final String newString = p.string;
IUndoableEdit edit = null;
DictEntry entry = dictionary.getEntry(rowIndex);
if (entry == null) {
interaction.reportProblem(ERROR_TITLE, NO_ENTRY);
return true;
}
CogToolLID lid;
final String goal;
final String search;
final String oldGoal;
final String oldSearch;
if (column == 0) {
goal = newString;
oldGoal = entry.goalWord;
if (goal.equals(oldGoal)) {
interaction.setStatusMessage(STRING_UNCHANGED);
return true;
}
search = entry.searchWord;
oldSearch = search;
lid = DictionaryEditorLID.SetGoalString;
} else {
goal = entry.goalWord;
oldGoal = goal;
search = newString;
oldSearch = entry.searchWord;
if (search.equals(oldSearch)) {
interaction.setStatusMessage(STRING_UNCHANGED);
return true;
}
lid = DictionaryEditorLID.SetSearchString;
}
if (checkNewEntry(goal, search, entry.algorithm)) {
interaction.reportProblem(ERROR_TITLE, ENTRY_EXISTS);
return false;
}
dictionary.updateEntry(rowIndex, goal, search);
CompoundUndoableEdit editSeq = new CompoundUndoableEdit(MODIFY_TERM, lid);
edit = new AUndoableEdit(lid) {
@Override
public String getPresentationName() {
return MODIFY_TERM;
}
@Override
public void redo() {
super.redo();
dictionary.updateEntry(rowIndex, goal, search);
}
@Override
public void undo() {
super.undo();
dictionary.updateEntry(rowIndex, oldGoal, oldSearch);
}
};
editSeq.addEdit(edit);
if (entry.algorithm != ITermSimilarity.MANUAL) {
double similarity = computeSimilarity(goal, search, entry.algorithm);
setSimilarity(rowIndex, similarity, entry.algorithm, editSeq);
}
editSeq.end();
undoMgr.addEdit(editSeq);
}
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class FrameEditorController method updateFrameName.
// renameEltGroup
/**
* Update the frame's name.
* The name must be unique.
*
* @param frame
* @param newName
* @return
*/
private boolean updateFrameName(final Frame frame, String tryName) {
final String oldName = frame.getName();
boolean notDone = true;
do {
if (tryName.length() == 0) {
tryName = interaction.protestNameCannotBeEmpty("Frame");
// If canceled, indicate so; otherwise, test again
if (tryName == null) {
return false;
}
} else {
Frame frameForName = model.getDesign().getFrame(tryName);
// then no change is necessary!
if (frameForName == frame) {
notDone = false;
} else if (frameForName != null) {
// A non-null widget for the tryName indicates a collision
tryName = interaction.protestNameCollision("Frame");
// 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;
frame.setName(newName);
IUndoableEdit edit = new AUndoableEdit(DesignEditorLID.RenameFrame) {
@Override
public String getPresentationName() {
return CHG_FRAME_NAME;
}
@Override
public void redo() {
super.redo();
Frame testFrame = design.getFrame(newName);
frame.setName(newName);
if (testFrame != null) {
makeFrameNameUnique(frame);
}
}
@Override
public void undo() {
super.undo();
Frame testFrame = design.getFrame(oldName);
frame.setName(oldName);
if (testFrame != null) {
makeFrameNameUnique(frame);
}
}
};
undoMgr.addEdit(edit);
}
}
} while (notDone);
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class FrameEditorController method createGroupElementsAction.
private IListenerAction createGroupElementsAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return FrameEditorSelectionState.class;
}
public boolean performAction(Object prms) {
FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
int selectedEltCount = selection.getElementSelectionCount();
if (selectedEltCount > 1) {
final FrameElementGroup newGroup = new FrameElementGroup();
assignUniqueEltGroupName(newGroup);
Iterator<FrameElement> selectedElts = selection.getSelectedElementsIterator();
while (selectedElts.hasNext()) {
FrameElement rootElt = selectedElts.next().getRootElement();
// that the element is a member of the group
if (!newGroup.contains(rootElt)) {
newGroup.add(rootElt);
}
}
model.addEltGroup(newGroup);
IUndoableEdit edit = new AUndoableEdit(FrameEditorLID.Group) {
@Override
public String getPresentationName() {
return GROUP_ELEMENTS;
}
@Override
public void redo() {
super.redo();
Iterator<FrameElement> groupElts = newGroup.iterator();
// back to the frame
while (groupElts.hasNext()) {
groupElts.next().addToEltGroup(newGroup);
}
model.addEltGroup(newGroup);
}
@Override
public void undo() {
super.undo();
model.removeEltGroup(newGroup);
Iterator<FrameElement> groupElts = newGroup.iterator();
// association from the element.
while (groupElts.hasNext()) {
groupElts.next().removeFromEltGroup(newGroup);
}
}
};
undoMgr.addEdit(edit);
return true;
}
if (selectedEltCount == 1) {
interaction.protestTooFewWidgets();
} else {
interaction.protestNoSelection();
}
return false;
}
};
}
Aggregations