Search in sources :

Example 1 with IAttributed

use of edu.cmu.cs.hcii.cogtool.util.IAttributed in project cogtool by cogtool.

the class ImportCogToolXML method addAttributes.

private static void addAttributes(IAttributed attributed, Node node, Map<IAttributed, String> pendingAttrSets) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        int numAttributes = attributes.getLength();
        if (numAttributes > 0) {
            if (notInitialized) {
                registerAttributes();
            }
            for (int i = 0; i < numAttributes; i++) {
                Node attributeNode = attributes.item(i);
                // Should never be null; sanity check
                if (attributeNode != null) {
                    String attribute = attributeNode.getNodeName();
                    IAttributed.AttributeDefinition<?> attrDefn = ATTRIBUTE_REGISTRY.get(attribute);
                    if (attrDefn != null) {
                        String attrName = attrDefn.attrName;
                        String attrNodeValue = attributeNode.getNodeValue();
                        if (WidgetAttributes.SELECTION_ATTR.equals(attrName)) {
                            // attrNodeValue is name of the selected widget,
                            // but the widget may not exist yet.
                            pendingAttrSets.put(attributed, attrNodeValue);
                        } else if (VALUE_REGISTRY.containsKey(attrNodeValue)) {
                            Object attrValue = VALUE_REGISTRY.get(attrNodeValue);
                            if (!NullSafe.equals(attrValue, attrDefn.defaultValue)) {
                                attributed.setAttribute(attrName, attrValue);
                            }
                        } else {
                            // Assume string value (eg, APPENDED_TEXT_ATTR)
                            attributed.setAttribute(attrName, attrNodeValue);
                        }
                    }
                }
            }
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed)

Example 2 with IAttributed

use of edu.cmu.cs.hcii.cogtool.util.IAttributed in project cogtool by cogtool.

the class ImportCogToolXML method parseFrame.

/**
     * Imports a frame
     * @param node
     */
private Frame parseFrame(Design design, Node node) throws IOException {
    NodeList children = node.getChildNodes();
    if (children != null) {
        String frameName = getAttributeValue(node, NAME_ATTR);
        if ((frameName == null) || frameName.equals("")) {
            failedObjectErrors.add("Cannot create a frame with an empty name.");
            return null;
        }
        // This adds the created frame to the design
        Frame frame = getFrame(design, frameName);
        addAttributes(frame, node);
        Frame.setFrameDevices(frame, design.getDeviceTypes());
        TransitionSource keyboardDevice = frame.getInputDevice(DeviceType.Keyboard);
        TransitionSource voiceDevice = frame.getInputDevice(DeviceType.Voice);
        // Some widgets have parents; so as not to require that
        // all widgets of a frame occur in a particular order, we must
        // resolve the parent names after all widgets have been parsed.
        // Maps the child widget to the name of its parent
        Map<ChildWidget, String> pendingParentSets = new LinkedHashMap<ChildWidget, String>();
        // Some attributes refer to widget names; must resolve these
        // after all widgets have been created.
        // Currently, the only such attribute that applies to widgets
        // is WidgetAttributes.SELECTION_ATTR
        // Maps the attributed object to the widget name that is
        // the value of the WidgetAttributes.SELECTION_ATTR attribute
        Map<IAttributed, String> pendingAttrSets = new HashMap<IAttributed, String>();
        // Some element groups may be referenced as members of other
        // groups before being defined; this map will hold them
        Map<String, FrameElementGroup> pendingGrps = new HashMap<String, FrameElementGroup>();
        // Some remote labels may not be defined before they're referenced
        // so keep track of those cases.  Maps the owner object to
        // the name of the remote label
        Map<FrameElement, String> pendingRemoteLabels = new HashMap<FrameElement, String>();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (nodeName.equalsIgnoreCase(BKG_IMAGE_PATH_ELT)) {
                String backgroundImagePath = getElementText(child);
                byte[] image = loadImage(backgroundImagePath);
                if (image != null) {
                    frameLoader.set(frame, Frame.backgroundVAR, image);
                    frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, backgroundImagePath);
                }
            } else if (nodeName.equalsIgnoreCase(BKG_IMAGE_DATA_ELT)) {
                String backgroundImageData = getElementText(child);
                String imageName = getAttributeValue(child, NAME_ATTR);
                byte[] image = null;
                if (backgroundImageData != "") {
                    image = Base64.decode(backgroundImageData);
                    if ((imageName != null) && !imageName.equals("")) {
                        cachedImages.put(imageName, image);
                    }
                } else if ((imageName != null) && !imageName.equals("")) {
                    // If imageName specified but there is no data, trust and
                    // try to find the last image data associated with that
                    // name in the cache.
                    image = cachedImages.get(imageName);
                }
                if (image != null) {
                    frameLoader.set(frame, Frame.backgroundVAR, image);
                    if ((imageName != null) && !imageName.equals("")) {
                        frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageName);
                    }
                }
            } else if (nodeName.equalsIgnoreCase(ORIGIN_ELT)) {
                double x = Double.parseDouble(getAttributeValue(child, X_ATTR));
                double y = Double.parseDouble(getAttributeValue(child, Y_ATTR));
                DoublePoint origin = new DoublePoint(x, y);
                frameLoader.set(frame, Frame.originVAR, origin);
            } else if (nodeName.equalsIgnoreCase(SPEAKER_TEXT_ELT)) {
                frameLoader.set(frame, Frame.speakerTextVAR, getElementText(child));
            } else if (nodeName.equalsIgnoreCase(LISTEN_TIME_SECS_ELT)) {
                frameLoader.set(frame, Frame.listenTimeVAR, Double.parseDouble(getElementText(child)));
            } else if (nodeName.equalsIgnoreCase(WIDGET_ELT)) {
                IWidget w = parseWidget(design, frame, pendingParentSets, pendingAttrSets, pendingRemoteLabels, child);
                if (w != null) {
                    frame.addWidget(w);
                } else {
                    w = new Widget(null, WidgetType.Noninteractive);
                    Image wImage = GraphicsUtil.getImageFromResource("edu/cmu/cs/hcii/cogtool/resources/warning.jpg");
                    //w.setImage(wImage.getBytes());
                    frame.addWidget(w);
                }
            } else if (nodeName.equalsIgnoreCase(ELTGROUP_ELT)) {
                FrameElementGroup g = parseEltGroup(design, frame, pendingGrps, child);
                if (g != null) {
                    String eltGrpName = g.getName();
                    pendingGrps.remove(eltGrpName);
                    eltGrpName = NamedObjectUtil.makeNameUnique(eltGrpName, frame.getEltGroups());
                    g.setName(eltGrpName);
                    frame.addEltGroup(g);
                }
            } else if (nodeName.equalsIgnoreCase(KEYBOARD_TRANSITIONS_ELT)) {
                if (keyboardDevice != null) {
                    parseTransitions(design, keyboardDevice, child);
                } else {
                    failedObjectErrors.add("Keyboard transitions require that Design have a Keyboard device");
                }
            } else if (nodeName.equalsIgnoreCase(VOICE_TRANSITIONS_ELT)) {
                if (voiceDevice != null) {
                    parseTransitions(design, voiceDevice, child);
                } else {
                    failedObjectErrors.add("Voice transitions require that Design have a Voice device");
                }
            }
        }
        if (frame.getName() != null) {
            // Handle any forward references for remote labels
            Iterator<Map.Entry<FrameElement, String>> labelRefs = pendingRemoteLabels.entrySet().iterator();
            while (labelRefs.hasNext()) {
                Map.Entry<FrameElement, String> labelRef = labelRefs.next();
                setRemoteLabel(frame, labelRef.getValue(), labelRef.getKey(), null);
            }
            // If any "pending" element groups still exist, then there
            // is an error -- an element group that didn't exist!
            Iterator<FrameElementGroup> missingGrps = pendingGrps.values().iterator();
            StringBuilder errorMsg = new StringBuilder();
            while (missingGrps.hasNext()) {
                FrameElementGroup missingGrp = missingGrps.next();
                errorMsg.append("Missing widget or group, named: ");
                errorMsg.append(missingGrp.getName());
                errorMsg.append(" as member of the following groups: ");
                Iterator<FrameElementGroup> inGrps = missingGrp.getEltGroups().iterator();
                String separator = "";
                while (inGrps.hasNext()) {
                    errorMsg.append(separator + inGrps.next().getName());
                    separator = ", ";
                }
                failedObjectErrors.add(errorMsg.toString());
                errorMsg.delete(0, errorMsg.length());
            }
            Iterator<Map.Entry<ChildWidget, String>> childToParentSet = pendingParentSets.entrySet().iterator();
            // relationships
            while (childToParentSet.hasNext()) {
                Map.Entry<ChildWidget, String> childToParent = childToParentSet.next();
                String parentName = childToParent.getValue();
                if (!"".equals(parentName)) {
                    ChildWidget child = childToParent.getKey();
                    AParentWidget parent = (AParentWidget) frame.getWidget(parentName);
                    parent.addItem(child);
                    child.setParent(parent);
                }
            }
            Iterator<Map.Entry<IAttributed, String>> selnAttrToSet = pendingAttrSets.entrySet().iterator();
            // that used widget names as values.
            while (selnAttrToSet.hasNext()) {
                Map.Entry<IAttributed, String> selnAttr = selnAttrToSet.next();
                String widgetName = selnAttr.getValue();
                IWidget attrValue = "".equals(widgetName) ? null : frame.getWidget(widgetName);
                // At the moment, all occurrences that use pendingAttrSets
                // are instances of PullDownHeader for
                // WidgetAttributes.SELECTION_ATTR
                selnAttr.getKey().setAttribute(WidgetAttributes.SELECTION_ATTR, attrValue);
            }
            return frame;
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed) Node(org.w3c.dom.Node) Image(org.eclipse.swt.graphics.Image) LinkedHashMap(java.util.LinkedHashMap) NodeList(org.w3c.dom.NodeList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 3 with IAttributed

use of edu.cmu.cs.hcii.cogtool.util.IAttributed in project cogtool by cogtool.

the class AScriptStep method overrideAttribute.

public void overrideAttribute(IAttributed attributed, String attrName, Object value) {
    IAttributed override;
    if (attributed == getStepFocus()) {
        override = this;
    } else {
        if (overrides == null) {
            overrides = new HashMap<IAttributed, IAttributed>();
        }
        override = overrides.get(attributed);
        if (override == null) {
            override = new GlobalAttributed();
            overrides.put(attributed, override);
        }
    }
    override.setAttribute(attrName, value);
    attributed.raiseAlert(new OverrideChange(this, attrName, true));
}
Also used : IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed) GlobalAttributed(edu.cmu.cs.hcii.cogtool.util.GlobalAttributed)

Example 4 with IAttributed

use of edu.cmu.cs.hcii.cogtool.util.IAttributed in project cogtool by cogtool.

the class AScriptStep method undoOverride.

public void undoOverride(IAttributed attributed, String attrName) {
    IAttributed override;
    if (attributed == getStepFocus()) {
        override = this;
    } else {
        override = overrides.get(attributed);
        if (override == null) {
            throw new IllegalStateException("Missing override");
        }
    }
    override.unsetAttribute(attrName);
    attributed.raiseAlert(new OverrideChange(this, attrName, false));
}
Also used : IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed)

Aggregations

IAttributed (edu.cmu.cs.hcii.cogtool.util.IAttributed)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 Node (org.w3c.dom.Node)2 GlobalAttributed (edu.cmu.cs.hcii.cogtool.util.GlobalAttributed)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Image (org.eclipse.swt.graphics.Image)1 NodeList (org.w3c.dom.NodeList)1