Search in sources :

Example 66 with IWidget

use of edu.cmu.cs.hcii.cogtool.model.IWidget 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);
}
Also used : ReadOnlyList(edu.cmu.cs.hcii.cogtool.util.ReadOnlyList) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) ArrayList(java.util.ArrayList) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 67 with IWidget

use of edu.cmu.cs.hcii.cogtool.model.IWidget in project cogtool by cogtool.

the class FrameEditorController method deleteElements.

/**
     * Delete currently selected widgets.
     */
protected boolean deleteElements(FrameEditorSelectionState selection) {
    String editLabel = (selection.getElementSelectionCount() > 1) ? DELETE_WIDGETS : DELETE_WIDGET;
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(editLabel, FrameEditorLID.Delete);
    FrameElement[] selectedElts = selection.getSelectedIFrameElements();
    // Check to see if any items are selected
    if (selectedElts.length > 0) {
        if (interaction.confirmDeleteElements(selectedElts)) {
            Set<IWidget> frameWidgets = model.getWidgets();
            Set<FrameElementGroup> frameAssocs = model.getEltGroups();
            for (FrameElement selectedElt : selectedElts) {
                if (selectedElt instanceof FrameElementGroup) {
                    // component of another group
                    if (frameAssocs.contains(selectedElt)) {
                        deleteFrameEltGroup((FrameElementGroup) selectedElt, null, editSequence);
                    }
                } else if (selectedElt instanceof IWidget) {
                    // of another widget (e.g., menu item part of a menu)
                    if (frameWidgets.contains(selectedElt)) {
                        deleteWidget((IWidget) selectedElt, true, null, editSequence);
                    }
                }
            }
            editSequence.end();
            undoMgr.addEdit(editSequence);
            return true;
        }
    } else {
        interaction.protestNoSelection();
    }
    return false;
}
Also used : FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 68 with IWidget

use of edu.cmu.cs.hcii.cogtool.model.IWidget 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)

Example 69 with IWidget

use of edu.cmu.cs.hcii.cogtool.model.IWidget in project cogtool by cogtool.

the class FrameEditorController method captureImageAction.

// createChangeAuxTextPropertyAction
/**
     * Create a ListenerAction which will handle capturing a background image.
     * @return
     */
private IListenerAction captureImageAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return FrameEditorSelectionState.class;
        }

        public boolean performAction(Object prms) {
            CompoundUndoableEdit editSequence = new CompoundUndoableEdit(CAPTURE_BKG_IMG, FrameEditorLID.CaptureImageProperty);
            // Get the selection.
            FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
            Iterator<IWidget> selected = selection.getSelectedWidgetsIterator();
            // Iterate over every selected widget
            while (selected.hasNext()) {
                final IWidget w = selected.next();
                DoubleRectangle bounds = w.getEltBounds();
                // Get the image from the background, and crop to shape
                final byte[] bg = GraphicsUtil.cropImage(model.getBackgroundImage(), bounds.x, bounds.y, bounds.width, bounds.height);
                // Get the old image, could be null.
                final byte[] old = w.getImage();
                final String previousImagePath = (String) w.getAttribute(WidgetAttributes.IMAGE_PATH_ATTR);
                w.setImage(bg);
                w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, WidgetAttributes.NO_IMAGE);
                editSequence.addEdit(new AUndoableEdit(FrameEditorLID.CaptureImageProperty) {

                    @Override
                    public String getPresentationName() {
                        return CAPTURE_BKG_IMG;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        w.setImage(bg);
                        w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, WidgetAttributes.NO_IMAGE);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        w.setImage(old);
                        w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, previousImagePath);
                    }
                });
            }
            editSequence.end();
            // Only add this edit if it is significant
            if (editSequence.isSignificant()) {
                undoMgr.addEdit(editSequence);
            }
            return true;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FrameEditorSelectionState(edu.cmu.cs.hcii.cogtool.ui.FrameEditorSelectionState) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 70 with IWidget

use of edu.cmu.cs.hcii.cogtool.model.IWidget in project cogtool by cogtool.

the class FrameEditorController method moveElements.

private boolean moveElements(FrameEditorSelectionState selection, double moveByX, double moveByY, boolean moveAsGroup) {
    String editLabel;
    if (selection.getWidgetSelectionCount() == 1) {
        editLabel = MOVE_WIDGET;
    } else {
        editLabel = MOVE_WIDGETS;
    }
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(editLabel, FrameEditorLID.MoveWidgets);
    FrameElement[] selected = selection.getSelectedIFrameElements();
    // Avoid moving a group more than once
    Set<SimpleWidgetGroup> movedGroups = new HashSet<SimpleWidgetGroup>();
    Set<IWidget> movedWidgets = new HashSet<IWidget>();
    // Move all selected widgets by the specified amount
    for (FrameElement eltToMove : selected) {
        if (!isMemberOfSelectedGroup(eltToMove, selection)) {
            moveElement(FrameEditorLID.MoveWidgets, editLabel, eltToMove, moveByX, moveByY, moveAsGroup, movedGroups, movedWidgets, editSequence);
        }
    }
    editSequence.end();
    // Only add this edit if it is significant
    if (editSequence.isSignificant()) {
        undoMgr.addEdit(editSequence);
    }
    return true;
}
Also used : SimpleWidgetGroup(edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) HashSet(java.util.HashSet)

Aggregations

IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)93 SimpleWidgetGroup (edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup)29 FrameElement (edu.cmu.cs.hcii.cogtool.model.FrameElement)20 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)19 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)15 FrameElementGroup (edu.cmu.cs.hcii.cogtool.model.FrameElementGroup)15 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)12 AParentWidget (edu.cmu.cs.hcii.cogtool.model.AParentWidget)11 Point (org.eclipse.draw2d.geometry.Point)11 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)10 ChildWidget (edu.cmu.cs.hcii.cogtool.model.ChildWidget)9 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)9 GridButtonGroup (edu.cmu.cs.hcii.cogtool.model.GridButtonGroup)9 HashSet (java.util.HashSet)8 GridButton (edu.cmu.cs.hcii.cogtool.model.GridButton)7 InputDevice (edu.cmu.cs.hcii.cogtool.model.InputDevice)7 WidgetType (edu.cmu.cs.hcii.cogtool.model.WidgetType)7 FrameEditorUI (edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI)7 Iterator (java.util.Iterator)7 EventObject (java.util.EventObject)6