use of edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup in project cogtool by cogtool.
the class FrameEditorController method reorderWidget.
private boolean reorderWidget(final IWidget widget, final SimpleWidgetGroup newGroup, final int newIndex) {
final SimpleWidgetGroup prevGroup = widget.getParentGroup();
final int prevIndex = prevGroup.indexOf(widget);
int index = newIndex;
if (prevGroup == newGroup) {
if (prevIndex < newIndex) {
index--;
}
if (index == prevIndex) {
return true;
}
}
DoublePoint prevGroupStartPos = prevGroup.get(0).getShape().getOrigin();
final double prevGroupStartX = prevGroupStartPos.x;
final double prevGroupStartY = prevGroupStartPos.y;
DoubleSize prevSize = widget.getShape().getSize();
final double prevWidth = prevSize.width;
final double prevHeight = prevSize.height;
DoubleRectangle newBds = newGroup.get(0).getEltBounds();
double heightFactor = (widget instanceof ListItem) ? getHeightFactor(widget, newGroup) : 1.0;
final double newWidth = newBds.width;
final double newHeight = newBds.height * heightFactor;
final double newGroupStartX = newBds.x;
final double newGroupStartY = newBds.y;
Object rendered = newGroup.getAttribute(WidgetAttributes.IS_RENDERED_ATTR);
final boolean groupRendered = ((Boolean) rendered).booleanValue();
final boolean widgetRendered = widget.isRendered();
reorderGroupWidget(widget, newWidth, newHeight, index, prevGroup, newGroup, prevGroupStartX, prevGroupStartY, newGroupStartX, newGroupStartY);
if (widgetRendered != groupRendered) {
widget.setRendered(groupRendered);
}
DemoStateManager.ObsoletingEdit edit = new DemoStateManager.ObsoletingEdit(FrameEditorLID.Reorder, demoStateMgr) {
@Override
public String getPresentationName() {
return REORDER_WIDGET;
}
@Override
public void redo() {
super.redo();
int index = newIndex;
if (prevGroup == newGroup) {
if (prevIndex < newIndex) {
index--;
}
}
reorderGroupWidget(widget, newWidth, newHeight, index, prevGroup, newGroup, prevGroupStartX, prevGroupStartY, newGroupStartX, newGroupStartY);
if (widgetRendered != groupRendered) {
widget.setRendered(groupRendered);
}
noteEditCheckRegenerate(prevGroup, newGroup, this);
}
@Override
public void undo() {
super.undo();
reorderGroupWidget(widget, prevWidth, prevHeight, prevIndex, newGroup, prevGroup, newGroupStartX, newGroupStartY, prevGroupStartX, prevGroupStartY);
if (widgetRendered != groupRendered) {
widget.setRendered(widgetRendered);
}
noteEditCheckRegenerate(newGroup, prevGroup, this);
}
};
noteEditCheckRegenerate(prevGroup, newGroup, edit);
undoMgr.addEdit(edit);
return true;
}
use of edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup in project cogtool by cogtool.
the class FrameEditorController method spaceElementsEqually.
/**
* Spaces the selected frame elements equally along a certain axis
* @param vertical true for vertical axis; false for horizontal
*/
private boolean spaceElementsEqually(FrameEditorSelectionState selection, boolean vertical) {
// Order the widgets according to location
// (either from the left or from the top)
Comparator<FrameElement> c = vertical ? elementVerticalComparator : elementHorizontalComparator;
Set<FrameElement> elements = getSelectedElements(selection, c);
if (elements.size() <= 2) {
interaction.protestTooFewElements();
return false;
}
// Calculate the spacing between widgets
double sum = 0;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
// Go through each element that is selected
// Determine the size, min & max of the region.
// this can then be used to do spacing.
Iterator<FrameElement> eltIter = elements.iterator();
while (eltIter.hasNext()) {
DoubleRectangle bounds = eltIter.next().getEltBounds();
double size = vertical ? bounds.height : bounds.width;
double position = vertical ? bounds.y : bounds.x;
sum += size;
min = Math.min(min, position);
max = Math.max(max, size + position);
}
// Get the spacing to use between each item.
double spacing = ((max - min) - sum) / (elements.size() - 1);
String undoRedoLabel = vertical ? SPACE_VERTICALLY : SPACE_HORIZONTALLY;
CogToolLID lid = vertical ? FrameEditorLID.SpaceVertically : FrameEditorLID.SpaceHorizontally;
CompoundUndoableEdit editSequence = new CompoundUndoableEdit(undoRedoLabel, lid);
// Avoid moving a group more than once
Set<SimpleWidgetGroup> movedGroups = new HashSet<SimpleWidgetGroup>();
Set<IWidget> movedWidgets = new HashSet<IWidget>();
// Adjust the spacings to the correct values and go through
// each element performing the appropriate move.
eltIter = elements.iterator();
while (eltIter.hasNext()) {
FrameElement elt = eltIter.next();
DoubleRectangle bounds = elt.getEltBounds();
// Determine the amount to move each element
double deltaX = vertical ? 0.0 : (min - bounds.x);
double deltaY = vertical ? (min - bounds.y) : 0.0;
// Set the new location, adding the undoable edit
moveElement(lid, undoRedoLabel, elt, deltaX, deltaY, true, movedGroups, movedWidgets, editSequence);
// Advance the pointer to the next location
min += spacing + (vertical ? bounds.height : bounds.width);
}
editSequence.end();
// Only add this edit if it is significant
if (editSequence.isSignificant()) {
undoMgr.addEdit(editSequence);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup in project cogtool by cogtool.
the class FrameEditorController method insertDuplicateWidget.
/**
* If group or parent is non-null, duplicate the widget within the given
* group. If they are both null, the widget was dragged to empty space, so
* give it a new group.
*/
private boolean insertDuplicateWidget(IWidget widget, SimpleWidgetGroup group, int index, AParentWidget parent, double moveByX, double moveByY) {
Map<IWidget, IWidget> widgetCopies = new LinkedHashMap<IWidget, IWidget>();
double startPosX = 0.0;
double startPosY = 0.0;
DoubleSize newSize;
if (parent != null) {
newSize = getNewWidgetSize(parent);
newSize.height *= getHeightFactor(widget, parent.getChildren());
} else if ((group == null) || (group.size() == 0)) {
newSize = widget.getShape().getSize();
} else {
DoublePoint startPos = group.get(0).getShape().getOrigin();
startPosX = startPos.x;
startPosY = startPos.y;
newSize = group.get(0).getShape().getSize();
if (widget instanceof ListItem) {
newSize.height *= getHeightFactor(widget, group);
}
}
widgetSituator.reset(widgetCopies, null);
IWidget newWidget = null;
if (parent != null) {
if (widget instanceof ChildWidget) {
newWidget = ((ChildWidget) widget).duplicate(parent, lookupFrameDuplicator, widgetSituator, index);
newWidget.setWidgetSize(newSize.width, newSize.height);
if (newWidget instanceof AParentWidget) {
resizeChildren(newWidget);
DesignEditorCmd.repositionChildren((AParentWidget) newWidget);
}
DesignEditorCmd.repositionChildren(parent);
}
} else if (group != null) {
if (widget instanceof MenuHeader) {
newWidget = ((MenuHeader) widget).duplicate(group, lookupFrameDuplicator, widgetSituator, index);
} else if (widget instanceof ListItem) {
newWidget = ((ListItem) widget).duplicate(group, lookupFrameDuplicator, index);
}
newWidget.setWidgetSize(newSize.width, newSize.height);
resizeChildren(newWidget);
widgetSituator.placeInContext(widget, newWidget);
DesignEditorCmd.repositionChildren(group, startPosX, startPosY);
} else {
// Duplicating into space
if ((widget instanceof MenuHeader) || (widget instanceof ListItem)) {
SimpleWidgetGroup newGroup = null;
if (widget instanceof MenuHeader) {
newGroup = new SimpleWidgetGroup(SimpleWidgetGroup.HORIZONTAL);
newWidget = ((MenuHeader) widget).duplicate(newGroup, lookupFrameDuplicator, widgetSituator);
} else {
// (widget instanceof ListItem)
newGroup = new SimpleWidgetGroup(SimpleWidgetGroup.VERTICAL);
newWidget = ((ListItem) widget).duplicate(newGroup, lookupFrameDuplicator);
}
group = newGroup;
widgetSituator.placeInContext(widget, newWidget);
newWidget.moveElement(moveByX, moveByY);
group.setAttribute(WidgetAttributes.IS_RENDERED_ATTR, Boolean.valueOf(widget.isRendered()));
}
}
widgetSituator.completeWork();
Collection<IWidget> duplicateWidgets = widgetCopies.values();
Iterator<IWidget> copies = duplicateWidgets.iterator();
while (copies.hasNext()) {
IWidget widgetCopy = copies.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);
}
SimpleWidgetGroup newGroup = (group != null) ? group : newWidget.getParentGroup();
Object rendered = newGroup.getAttribute(WidgetAttributes.IS_RENDERED_ATTR);
boolean groupRendered = ((Boolean) rendered).booleanValue();
boolean widgetRendered = widget.isRendered();
if (groupRendered != widgetRendered) {
newWidget.setRendered(groupRendered);
}
insertDuplicateEdit(newWidget, new ReadOnlyList<IWidget>(new ArrayList<IWidget>(duplicateWidgets)), group, index, parent, startPosX, startPosY, undoMgr);
return true;
}
use of edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup in project cogtool by cogtool.
the class FrameEditorController method reorderChildWidget.
private boolean reorderChildWidget(final ChildWidget widget, final SimpleWidgetGroup newGroup, final int newIndex, final AParentWidget newParent) {
final SimpleWidgetGroup prevGroup = widget.getParentGroup();
final int prevIndex = prevGroup.indexOf(widget);
int index = newIndex;
if (prevGroup == newGroup) {
if (prevIndex < newIndex) {
index--;
}
if (index == prevIndex) {
return true;
}
}
final AParentWidget prevParent = widget.getParent();
DoubleSize prevSize = widget.getShape().getSize();
final double prevWidth = prevSize.width;
final double prevHeight = prevSize.height;
DoubleSize newSize = getNewWidgetSize(newParent);
final double newWidth = newSize.width;
final double newHeight = newSize.height * getHeightFactor(widget, newGroup);
final boolean widgetRendered = widget.isRendered();
reorderChildWidget(widget, newWidth, newHeight, index, prevParent, newParent);
Object rendered = widget.getParentGroup().getAttribute(WidgetAttributes.IS_RENDERED_ATTR);
final boolean groupRendered = ((Boolean) rendered).booleanValue();
if (widgetRendered != groupRendered) {
widget.setRendered(groupRendered);
}
DemoStateManager.ObsoletingEdit edit = new DemoStateManager.ObsoletingEdit(FrameEditorLID.Reorder, demoStateMgr) {
private ChildWidget child = widget;
@Override
public String getPresentationName() {
return REORDER_WIDGET;
}
@Override
public void redo() {
super.redo();
int index = newIndex;
if (prevGroup == newGroup) {
if (prevIndex < newIndex) {
index--;
}
}
reorderChildWidget(child, newWidth, newHeight, index, prevParent, newParent);
if (widgetRendered != groupRendered) {
widget.setRendered(groupRendered);
}
noteEditCheckRegenerate(prevParent.getChildren(), newParent.getChildren(), this);
}
@Override
public void undo() {
super.undo();
reorderChildWidget(child, prevWidth, prevHeight, prevIndex, newParent, prevParent);
if (widgetRendered != groupRendered) {
widget.setRendered(widgetRendered);
}
noteEditCheckRegenerate(newParent.getChildren(), prevParent.getChildren(), this);
}
};
noteEditCheckRegenerate(prevParent.getChildren(), newParent.getChildren(), edit);
undoMgr.addEdit(edit);
if (CogToolPref.REGENERATE_AUTOMATICALLY.getBoolean()) {
DemoScriptCmd.regenerateDesignScripts(project, design, interaction);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup in project cogtool by cogtool.
the class FrameEditorController method resizeElements.
/**
* Resize the selected set of elements based on where the mouse was released
* and the fixed point in the resize.
*
* While it supports multiple selection, its behavior is correct if called
* with more then one selected widget.
*/
private boolean resizeElements(double oldResizeX, double oldResizeY, double newResizeX, double newResizeY, double ratioX, double ratioY, FrameEditorSelectionState selection) {
Iterator<FrameElement> selected = selection.getSelectedElementsIterator();
CompoundUndoableEdit editSequence = new CompoundUndoableEdit((selection.getWidgetSelectionCount() != 1) ? RESIZE_WIDGETS : RESIZE_WIDGET, FrameEditorLID.ResizeWidgets);
Set<SimpleWidgetGroup> resizedGroups = new HashSet<SimpleWidgetGroup>();
// Loop through selected widgets
while (selected.hasNext()) {
FrameElement elt = selected.next();
resizeElement(elt, oldResizeX, oldResizeY, newResizeX, newResizeY, ratioX, ratioY, resizedGroups, false, editSequence);
}
editSequence.end();
// Only add this edit if it is significant
if (editSequence.isSignificant()) {
undoMgr.addEdit(editSequence);
}
return true;
}
Aggregations