use of edu.cmu.cs.hcii.cogtool.util.ReadOnlyList in project cogtool by cogtool.
the class FrameEditorController method duplicateWidgetsAction.
// duplicateFrameEltGroup
private IListenerAction duplicateWidgetsAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return FrameEditorUI.DuplicateParameters.class;
}
public boolean performAction(Object prms) {
FrameEditorUI.DuplicateParameters parameters = (FrameEditorUI.DuplicateParameters) prms;
int elementCount = parameters.selection.getElementSelectionCount();
// If selection is non zero, copy widgets
if (elementCount > 0) {
Map<IWidget, IWidget> widgetCopies = new LinkedHashMap<IWidget, IWidget>();
final Map<FrameElementGroup, FrameElementGroup> groupCopies = new LinkedHashMap<FrameElementGroup, FrameElementGroup>();
widgetSituator.reset(widgetCopies, groupCopies);
Set<FrameElement> selectedElements = getSelectedElements(parameters.selection, elementLevelComparator);
Iterator<FrameElement> elements = selectedElements.iterator();
// Iterate through the widgets and duplicate.
while (elements.hasNext()) {
FrameElement elt = elements.next();
if (elt instanceof IWidget) {
duplicateWidget((IWidget) elt, parameters.selection, parameters.moveByX, parameters.moveByY);
} else if (elt instanceof FrameElementGroup) {
duplicateFrameEltGroup((FrameElementGroup) elt, parameters.moveByX, parameters.moveByY);
}
}
widgetSituator.completeWork();
final Iterable<? extends IWidget> widgetDuplicates = new ReadOnlyList<IWidget>(new ArrayList<IWidget>(widgetCopies.values()));
Iterator<? extends IWidget> copiedWidgets = widgetDuplicates.iterator();
Set<GridButtonGroup> dupGridGroups = new HashSet<GridButtonGroup>();
while (copiedWidgets.hasNext()) {
IWidget widgetCopy = copiedWidgets.next();
// Warning: it is important that each widget be added
// to the frame *before* we make the next widget name
// unique or we can end up with non-unique names.
makeWidgetNameUnique(widgetCopy);
model.addWidget(widgetCopy);
if (widgetCopy instanceof GridButton) {
GridButtonGroup gbg = (GridButtonGroup) widgetCopy.getParentGroup();
// for each grid button group
if (!dupGridGroups.contains(gbg)) {
gbg.recalculateOffsets();
dupGridGroups.add(gbg);
}
}
}
Iterator<FrameElementGroup> copiedGroups = groupCopies.values().iterator();
while (copiedGroups.hasNext()) {
FrameElementGroup copiedGroup = copiedGroups.next();
// Ensure name is unique, then add to frame immediately
// Otherwise, it would be possible to generate presumed
// unique names that weren't.
makeEltGroupNameUnique(copiedGroup);
model.addEltGroup(copiedGroup);
}
DemoStateManager.IDesignUndoableEdit edit = new DemoStateManager.InvalidatingEdit(FrameEditorLID.Duplicate, demoStateMgr) {
@Override
public String getPresentationName() {
return DUPLICATE_WIDGETS;
}
@Override
public void redo() {
super.redo();
Iterator<? extends IWidget> addCopies = widgetDuplicates.iterator();
while (addCopies.hasNext()) {
IWidget widgetCopy = addCopies.next();
model.addWidget(widgetCopy);
}
Iterator<FrameElementGroup> copiedGroups = groupCopies.values().iterator();
while (copiedGroups.hasNext()) {
model.addEltGroup(copiedGroups.next());
}
demoStateMgr.noteWidgetsEdit(widgetDuplicates, this);
}
@Override
public void undo() {
super.undo();
Iterator<? extends IWidget> removeCopies = widgetDuplicates.iterator();
while (removeCopies.hasNext()) {
IWidget widgetCopy = removeCopies.next();
model.removeWidget(widgetCopy);
}
Iterator<FrameElementGroup> copiedGroups = groupCopies.values().iterator();
while (copiedGroups.hasNext()) {
model.removeEltGroup(copiedGroups.next());
}
demoStateMgr.noteWidgetsEdit(widgetDuplicates, this);
}
};
demoStateMgr.noteWidgetsEdit(widgetDuplicates, edit);
undoMgr.addEdit(edit);
return true;
}
// tell user to select something.
interaction.protestNoSelection();
return false;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.ReadOnlyList in project cogtool by cogtool.
the class FrameEditorController method moveWidgetGroup.
// moveWidget
private void moveWidgetGroup(ListenerIdentifier lid, final String presentationLabel, final SimpleWidgetGroup group, double widgetMoveByX, double widgetMoveByY, IUndoableEditSequence editSequence) {
// Keep track of old and new locations, ensuring not less than zero
IWidget firstChildWidget = group.get(0);
DoublePoint oldLocation = firstChildWidget.getShape().getOrigin();
final double deltaX = Math.max(widgetMoveByX, -oldLocation.x);
final double deltaY = Math.max(widgetMoveByY, -oldLocation.y);
final int numWidgets = group.size();
List<IWidget> groupWidgets = new ArrayList<IWidget>();
// Change model and create undo.
for (int i = 0; i < numWidgets; i++) {
IWidget widget = group.get(i);
groupWidgets.add(widget);
widget.moveElement(deltaX, deltaY);
}
final ReadOnlyList<? extends IWidget> roGroupWidgets = new ReadOnlyList<IWidget>(groupWidgets);
DemoStateManager.ObsoletingEdit edit = new DemoStateManager.ObsoletingEdit(lid, demoStateMgr) {
@Override
public String getPresentationName() {
return presentationLabel;
}
@Override
public void redo() {
super.redo();
for (int i = 0; i < numWidgets; i++) {
IWidget w = roGroupWidgets.get(i);
w.moveElement(deltaX, deltaY);
}
noteEditCheckRegenerate(roGroupWidgets, this);
}
@Override
public void undo() {
super.undo();
for (int i = 0; i < numWidgets; i++) {
IWidget w = roGroupWidgets.get(i);
w.moveElement(-deltaX, -deltaY);
}
noteEditCheckRegenerate(roGroupWidgets, this);
}
};
noteEditCheckRegenerate(roGroupWidgets, edit);
editSequence.addEdit(edit);
}
Aggregations