Search in sources :

Example 21 with Attribute

use of com.codename1.rad.models.Attribute in project CodenameOne by codenameone.

the class RSSService method readResponse.

/**
 * {@inheritDoc}
 */
protected void readResponse(InputStream input) throws IOException {
    results = new Vector();
    class FinishParsing extends RuntimeException {
    }
    XMLParser p = new XMLParser() {

        private String lastTag;

        private Hashtable current;

        private String url;

        protected boolean startTag(String tag) {
            if ("item".equalsIgnoreCase(tag) || "entry".equalsIgnoreCase(tag)) {
                if (startOffset > 0) {
                    return true;
                }
                current = new Hashtable();
                if (iconPlaceholder != null) {
                    current.put("icon", iconPlaceholder);
                }
            }
            lastTag = tag;
            return true;
        }

        protected void attribute(String tag, String attributeName, String value) {
            if (current != null) {
                if ("media:thumbnail".equalsIgnoreCase(tag) && "url".equalsIgnoreCase(attributeName)) {
                    current.put("thumb", value);
                } else {
                    if ("media:player".equalsIgnoreCase(tag) && "url".equalsIgnoreCase(attributeName)) {
                        current.put("player", value);
                    }
                }
            }
        }

        protected void textElement(String text) {
            if (lastTag != null && current != null) {
                // make "ATOM" seem like RSS
                if ("summary".equals(lastTag)) {
                    current.put("details", text);
                } else {
                    if ("content".equals(lastTag)) {
                        current.put("description", text);
                    } else {
                        current.put(lastTag, text);
                    }
                }
            }
        }

        protected void endTag(String tag) {
            if ("item".equalsIgnoreCase(tag) || "entry".equalsIgnoreCase(tag)) {
                if (startOffset > 0) {
                    startOffset--;
                    return;
                }
                results.addElement(current);
                current = null;
                if (limit > -1 && results.size() >= limit) {
                    throw new FinishParsing();
                }
            }
            if (tag.equals(lastTag)) {
                lastTag = null;
            }
        }
    };
    p.setParserCallback(this);
    input.mark(10);
    // Skip the bom marking UTF-8 in some streams
    while (input.read() != '<') {
    // input.mark(4);
    }
    int question = input.read();
    String cType = "UTF-8";
    if (question == '?') {
        // we are in an XML header, check if the encoding section exists
        StringBuilder cs = new StringBuilder();
        question = input.read();
        while (question != '>') {
            cs.append((char) question);
            question = input.read();
        }
        String str = cs.toString();
        int index = str.indexOf("encoding=\"") + 10;
        if (index > -1) {
            cType = str.substring(index, Math.max(str.indexOf("\"", index), str.indexOf("'", index)));
        }
    } else {
        // oops, continue as usual
        input.reset();
    }
    String resultType = getResponseContentType();
    if (resultType != null && resultType.indexOf("charset=") > -1) {
        cType = resultType.substring(resultType.indexOf("charset=") + 8);
    }
    try {
        int pos2 = cType.indexOf(';');
        if (pos2 > 0) {
            cType = cType.substring(0, pos2);
        }
        p.eventParser(new InputStreamReader(input, cType));
    } catch (FinishParsing ignor) {
        hasMore = true;
    }
    if (isCreatePlainTextDetails()) {
        int elementCount = results.size();
        for (int iter = 0; iter < elementCount; iter++) {
            Hashtable h = (Hashtable) results.elementAt(iter);
            String s = (String) h.get("description");
            if (s != null && !h.containsKey("details")) {
                XMLParser x = new XMLParser();
                Element e = x.parse(new CharArrayReader(("<xml>" + s + "</xml>").toCharArray()));
                Vector results = e.getTextDescendants(null, false);
                StringBuilder endResult = new StringBuilder();
                for (int i = 0; i < results.size(); i++) {
                    endResult.append(((Element) results.elementAt(i)).getText());
                }
                h.put("details", endResult.toString());
            }
        }
    }
    fireResponseListener(new NetworkEvent(this, results));
}
Also used : CharArrayReader(com.codename1.io.CharArrayReader) InputStreamReader(java.io.InputStreamReader) Hashtable(java.util.Hashtable) Element(com.codename1.xml.Element) NetworkEvent(com.codename1.io.NetworkEvent) XMLParser(com.codename1.xml.XMLParser) Vector(java.util.Vector)

Example 22 with Attribute

use of com.codename1.rad.models.Attribute in project CodenameOne by codenameone.

the class HTMLComponent method handleImage.

/**
 * Handles the IMG tag. This includes calculating its size (if available), applying any links/accesskeys and adding it to the download queue
 *
 * @param imgElement the IMG element
 * @param align th current alignment
 * @param cmd The submit command of a form, used only for INPUT type="image"
 */
private void handleImage(HTMLElement imgElement, int align, Command cmd) {
    String imageUrl = imgElement.getAttributeById(HTMLElement.ATTR_SRC);
    Label imgLabel = null;
    if (imageUrl != null) {
        String alignStr = imgElement.getAttributeById(HTMLElement.ATTR_ALIGN);
        // Image width and height
        int iWidth = calcSize(getWidth(), imgElement.getAttributeById(HTMLElement.ATTR_WIDTH), 0, false);
        int iHeight = calcSize(getHeight(), imgElement.getAttributeById(HTMLElement.ATTR_HEIGHT), 0, false);
        // Whitespace on the image sides (i.e. Margins)
        int hspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_HSPACE));
        int vspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_VSPACE));
        int totalWidth = iWidth + hspace * 2;
        if ((FIXED_WIDTH) && (x + totalWidth >= width)) {
            newLine(align);
        }
        // Alternative image text, shown until image is loaded.
        String altText = imgElement.getAttributeById(HTMLElement.ATTR_ALT);
        String imageMap = imgElement.getAttributeById(HTMLElement.ATTR_USEMAP);
        if (link != null) {
            // This image is inside an A tag with HREF attribute
            imgLabel = new HTMLLink(altText, link, this, mainLink, false);
            if (mainLink == null) {
                mainLink = (HTMLLink) imgLabel;
            }
            if (accesskey != '\0') {
                // accessKeys.put(new Integer(accesskey), imgLabel);
                addAccessKey(accesskey, imgLabel, false);
            }
            if (!PROCESS_HTML_MP1_ONLY) {
                ((HTMLLink) imgLabel).isMap = (imgElement.getAttributeById(HTMLElement.ATTR_ISMAP) != null);
            }
        } else if (cmd != null) {
            // Special case of an image submit button
            imgLabel = new Button(cmd);
            if ((altText != null) && (!altText.equals(""))) {
                imgLabel.setText(altText);
            }
            if (firstFocusable == null) {
                firstFocusable = imgLabel;
            }
        } else if (imageMap != null) {
            // Image Map
            imgLabel = new HTMLImageMap(this);
            if (imageMapComponents == null) {
                imageMapComponents = new Hashtable();
            }
            if (imageMap.startsWith("#")) {
                // Image map are denoted by # and then the map name (But we also tolerate if map is specified without #)
                imageMap = imageMap.substring(1);
            }
            imageMapComponents.put(imageMap, imgLabel);
            if ((imageMapData != null) && (imageMapData.containsKey(imageMap))) {
                ImageMapData data = (ImageMapData) imageMapData.get(imageMap);
                ((HTMLImageMap) imgLabel).mapData = data;
            }
        } else {
            imgLabel = new Label(altText);
        }
        if ((iWidth != 0) || (iHeight != 0)) {
            // reserve space while loading image if either width or height are specified, otherwise we don't know how much to reserve
            iWidth += imgLabel.getStyle().getPadding(Component.LEFT) + imgLabel.getStyle().getPadding(Component.RIGHT);
            iHeight += imgLabel.getStyle().getPadding(Component.TOP) + imgLabel.getStyle().getPadding(Component.BOTTOM);
            imgLabel.setPreferredSize(new Dimension(iWidth, iHeight));
        } else {
            // If no space is reserved, make a minimal text, otherwise Codename One won't calculate the size right after the image loads
            if ((imgLabel.getText() == null) || (imgLabel.getText().equals(""))) {
                imgLabel.setText(" ");
            }
        }
        // It is important that the padding of the image component itself will be all 0
        // This is because when the image is loaded, its preferred size is checked to see if its width/height were preset by the width/height attribute
        imgLabel.getSelectedStyle().setPadding(0, 0, 0, 0);
        imgLabel.getUnselectedStyle().setPadding(0, 0, 0, 0);
        imgLabel.getSelectedStyle().setFont(font.getFont());
        imgLabel.getUnselectedStyle().setFont(font.getFont());
        int borderSize = getInt(imgElement.getAttributeById(HTMLElement.ATTR_BORDER));
        if (borderSize != 0) {
            imgLabel.putClientProperty(CLIENT_PROPERTY_IMG_BORDER, new Integer(borderSize));
        } else {
            borderSize = 1;
        }
        imgLabel.getUnselectedStyle().setBorder(Border.createLineBorder(borderSize));
        imgLabel.getSelectedStyle().setBorder(Border.createLineBorder(borderSize));
        imgLabel.getUnselectedStyle().setBgTransparency(0);
        imgLabel.getSelectedStyle().setBgTransparency(0);
        Container imgCont = new Container(new BorderLayout());
        imgCont.addComponent(BorderLayout.CENTER, imgLabel);
        imgCont.getSelectedStyle().setMargin(vspace, vspace, hspace, hspace);
        imgCont.getUnselectedStyle().setMargin(vspace, vspace, hspace, hspace);
        curLine.addComponent(imgCont);
        x += totalWidth;
        // Alignment
        imgLabel.setAlignment(getHorizAlign(alignStr, align, false));
        imgLabel.setVerticalAlignment(getVertAlign(alignStr, Component.CENTER));
        if (showImages) {
            if (docInfo != null) {
                imageUrl = docInfo.convertURL(imageUrl);
                threadQueue.add(imgLabel, imageUrl);
            } else {
                if (DocumentInfo.isAbsoluteURL(imageUrl)) {
                    threadQueue.add(imgLabel, imageUrl);
                } else {
                    if (htmlCallback != null) {
                        htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, imgElement.getTagName(), imgElement.getAttributeName(new Integer(HTMLElement.ATTR_SRC)), imageUrl, "Ignoring Image file referred in an IMG tag (" + imageUrl + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
                    }
                }
            }
        }
        if (loadCSS) {
            imgElement.setAssociatedComponents(imgCont);
        }
    }
}
Also used : Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) Hashtable(java.util.Hashtable) Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension)

Example 23 with Attribute

use of com.codename1.rad.models.Attribute in project CodeRAD by shannah.

the class ActionNode method findAttribute.

@Override
public Attribute findAttribute(Class type) {
    Attribute att = super.findAttribute(type);
    if (att != null) {
        return att;
    }
    Node parent = getParent();
    if (parent instanceof ActionNode) {
        return parent.findAttribute(type);
    }
    return null;
}
Also used : ActionStyleAttribute(com.codename1.rad.attributes.ActionStyleAttribute)

Example 24 with Attribute

use of com.codename1.rad.models.Attribute in project CodeRAD by shannah.

the class FormNode method setAttributes.

public void setAttributes(Attribute... atts) {
    if (sections == null) {
        sections = new Sections();
    }
    // super.setAttributes(atts);
    for (Attribute att : atts) {
        if (att.getClass() == SectionNode.class) {
            sections.add((SectionNode) att);
            ((SectionNode) att).setParent(this);
        } else if (att.getClass() == FieldNode.class) {
            if (globalSection == null) {
                globalSection = new SectionNode();
                globalSection.setParent(this);
            }
            globalSection.setAttributes(att);
        } else if (att instanceof ListNode) {
            rootList = (ListNode) att;
            super.setAttributes(att);
        } else if (att instanceof ViewNode) {
            rootView = (ViewNode) att;
            super.setAttributes(att);
        } else {
            super.setAttributes(att);
        }
    }
}
Also used : Attribute(com.codename1.rad.models.Attribute)

Example 25 with Attribute

use of com.codename1.rad.models.Attribute in project CodeRAD by shannah.

the class Controller method extendAction.

/**
 * Extends an existing action from one of the parent controllers, and registers it as an action
 * on this controller.
 * @param category The category to register the action to.
 * @param overwriteAttributes Whether to overwrite existing attributes of the action.  If false, attributes
 *                            provided will be ignored when extending actions that already have those attributes
 *                            defined.
 * @param attributes Attributes to add to the action.
 * @return The action that was added.
 * @since 2.0
 */
public ActionNode extendAction(ActionNode.Category category, boolean overwriteAttributes, Attribute... attributes) {
    ActionNode action = getInheritedAction(category);
    if (action == null) {
        action = UI.action(attributes);
    } else {
        action = (ActionNode) action.createProxy(action.getParent());
        action.setAttributes(overwriteAttributes, attributes);
    }
    addActions(category, action);
    return action;
}
Also used : ActionNode(com.codename1.rad.nodes.ActionNode)

Aggregations

FieldNode (com.codename1.rad.nodes.FieldNode)14 PropertySelectorAttribute (com.codename1.rad.attributes.PropertySelectorAttribute)6 Attribute (com.codename1.rad.models.Attribute)6 NodeDecoratorAttribute (com.codename1.rad.attributes.NodeDecoratorAttribute)4 ViewPropertyParameterAttribute (com.codename1.rad.attributes.ViewPropertyParameterAttribute)4 Label (com.codename1.ui.Label)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 ActionNode (com.codename1.rad.nodes.ActionNode)2 Container (com.codename1.ui.Container)2 Hashtable (java.util.Hashtable)2 Switch (com.codename1.components.Switch)1 CharArrayReader (com.codename1.io.CharArrayReader)1 Log (com.codename1.io.Log)1 NetworkEvent (com.codename1.io.NetworkEvent)1 DateFormat (com.codename1.l10n.DateFormat)1 RAD (com.codename1.rad.annotations.RAD)1 RADDoc (com.codename1.rad.annotations.RADDoc)1 ActionStyleAttribute (com.codename1.rad.attributes.ActionStyleAttribute)1 PropertyImageRendererAttribute (com.codename1.rad.attributes.PropertyImageRendererAttribute)1 Entity (com.codename1.rad.models.Entity)1