Search in sources :

Example 6 with ListItem

use of edu.cmu.cs.hcii.cogtool.model.ListItem 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;
}
Also used : SimpleWidgetGroup(edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup) MenuHeader(edu.cmu.cs.hcii.cogtool.model.MenuHeader) ArrayList(java.util.ArrayList) AParentWidget(edu.cmu.cs.hcii.cogtool.model.AParentWidget) DoubleSize(edu.cmu.cs.hcii.cogtool.model.DoubleSize) LinkedHashMap(java.util.LinkedHashMap) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) ListItem(edu.cmu.cs.hcii.cogtool.model.ListItem) ChildWidget(edu.cmu.cs.hcii.cogtool.model.ChildWidget) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Aggregations

ListItem (edu.cmu.cs.hcii.cogtool.model.ListItem)6 SimpleWidgetGroup (edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup)6 MenuHeader (edu.cmu.cs.hcii.cogtool.model.MenuHeader)5 AParentWidget (edu.cmu.cs.hcii.cogtool.model.AParentWidget)4 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)4 ChildWidget (edu.cmu.cs.hcii.cogtool.model.ChildWidget)3 ContextMenu (edu.cmu.cs.hcii.cogtool.model.ContextMenu)3 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)3 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)3 PullDownHeader (edu.cmu.cs.hcii.cogtool.model.PullDownHeader)3 TraversableWidget (edu.cmu.cs.hcii.cogtool.model.TraversableWidget)3 AMenuWidget (edu.cmu.cs.hcii.cogtool.model.AMenuWidget)2 CheckBox (edu.cmu.cs.hcii.cogtool.model.CheckBox)2 DoubleSize (edu.cmu.cs.hcii.cogtool.model.DoubleSize)2 GridButtonGroup (edu.cmu.cs.hcii.cogtool.model.GridButtonGroup)2 MenuItem (edu.cmu.cs.hcii.cogtool.model.MenuItem)2 PullDownItem (edu.cmu.cs.hcii.cogtool.model.PullDownItem)2 RadioButton (edu.cmu.cs.hcii.cogtool.model.RadioButton)2 Widget (edu.cmu.cs.hcii.cogtool.model.Widget)2 WidgetType (edu.cmu.cs.hcii.cogtool.model.WidgetType)2