Search in sources :

Example 16 with Entry

use of com.codename1.ui.util.xml.Entry in project CodenameOne by codenameone.

the class ResourceEditorView method addNewFontWizard.

/**
 * Invoked by the "..." button in the add theme entry dialog, allows us to add
 * a font on the fly while working on a theme
 */
public void addNewFontWizard() {
    AddResourceDialog addResource = new AddResourceDialog(loadedResources, AddResourceDialog.FONT);
    if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(mainPanel, addResource, "Add Font", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
        if (addResource.checkName(loadedResources)) {
            JOptionPane.showMessageDialog(mainPanel, "A resource with that name already exists", "Add Font", JOptionPane.ERROR_MESSAGE);
            addNewFontWizard();
            return;
        }
        // show the image editing dialog...
        FontEditor font = new FontEditor(loadedResources, new EditorFont(com.codename1.ui.Font.createSystemFont(com.codename1.ui.Font.FACE_SYSTEM, com.codename1.ui.Font.STYLE_PLAIN, com.codename1.ui.Font.SIZE_MEDIUM), null, "Arial-plain-12", true, RenderingHints.VALUE_TEXT_ANTIALIAS_ON, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,;:!/\\*()[]{}|#$%^&<>?'\"+- "), addResource.getResourceName());
        font.setFactoryCreation(true);
        if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(mainPanel, font, "Add Font", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
            loadedResources.setFont(addResource.getResourceName(), font.createFont());
        }
    }
}
Also used : EditorFont(com.codename1.ui.EditorFont)

Example 17 with Entry

use of com.codename1.ui.util.xml.Entry 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 18 with Entry

use of com.codename1.ui.util.xml.Entry in project CodenameOne by codenameone.

the class List method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    getUIManager().getLookAndFeel().drawList(g, this);
    Style style = getStyle();
    int width = getWidth() - style.getHorizontalPadding() - getSideGap();
    if (isScrollableX()) {
        width = Math.max(width, getScrollDimension().getWidth() - style.getHorizontalPadding() - getSideGap());
    }
    int numOfcomponents = model.getSize();
    if (numOfcomponents == 0) {
        paintHint(g);
        return;
    }
    int xTranslate = getX();
    int yTranslate = getY();
    g.translate(xTranslate, yTranslate);
    Rectangle pos = new Rectangle();
    Dimension rendererSize = getElementSize(false, true);
    if (fixedSelection > FIXED_NONE_BOUNDRY) {
        if (animationPosition != 0 || isDragActivated()) {
            if (orientation != HORIZONTAL) {
                yTranslate += (animationPosition + fixedDraggedAnimationPosition);
                g.translate(0, animationPosition + fixedDraggedAnimationPosition);
            } else {
                xTranslate += (animationPosition + fixedDraggedAnimationPosition);
                g.translate(animationPosition + fixedDraggedAnimationPosition, 0);
            }
        }
    }
    int clipX = g.getClipX();
    int clipY = g.getClipY();
    int clipWidth = g.getClipWidth();
    int clipHeight = g.getClipHeight();
    // this flag is for preformance improvements
    // if we figured out that the list items are not visible anymore
    // we should break from the List loop
    boolean shouldBreak = false;
    // improve performance for browsing the end of a very large list
    int startingPoint = 0;
    if (fixedSelection < FIXED_NONE_BOUNDRY) {
        int startX = clipX + getAbsoluteX();
        if (isRTL()) {
            // In RTL the start of the list is not in the left side of the viewport, but rather the right side
            startX += getWidth();
        }
        startingPoint = Math.max(0, pointerSelect(startX, clipY + getAbsoluteY()) - 1);
    }
    int startOffset = 0;
    int endOffset = numOfcomponents;
    if (mutableRendererBackgrounds) {
        for (int i = startingPoint; i < numOfcomponents; i++) {
            // skip on the selected
            if (i == getCurrentSelected() && animationPosition == 0 && fixedDraggedAnimationPosition == 0) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                shouldBreak = true;
                continue;
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
            // if the renderer is in the clipping region
            if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                Dimension size = pos.getSize();
                Component selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(i), i, i == getCurrentSelected());
                renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                shouldBreak = true;
            } else {
                // this is relevant only if the List is not fixed.
                if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
    } else {
        T valueAt0 = getModel().getItemAt(0);
        Component selectionCmp;
        int selectedIndex = getSelectedIndex();
        if (selectedIndex > -1 && selectedIndex < numOfcomponents) {
            // this is essential otherwise we constantly ticker based on the value of the first entry
            selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(selectedIndex), 0, true);
        } else {
            selectionCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, true);
        }
        Component unselectedCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, false);
        for (int i = startingPoint; i < numOfcomponents; i++) {
            // skip on the selected
            if (i == getCurrentSelected() && animationPosition == 0) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                shouldBreak = true;
                continue;
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
            // if the renderer is in the clipping region
            if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                if (i == getCurrentSelected()) {
                    Dimension size = pos.getSize();
                    renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                } else {
                    Dimension size = pos.getSize();
                    renderComponentBackground(g, unselectedCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                }
                shouldBreak = true;
            } else {
                // this is relevant only if the List is not fixed.
                if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
    }
    boolean shouldRendererSelectedEntry = (renderer.getListFocusComponent(this) == null && (fixedSelection < FIXED_NONE_BOUNDRY)) || animationPosition == 0 && model.getSize() > 0;
    Rectangle selectedPos = new Rectangle();
    calculateComponentPosition(getCurrentSelected(), width, selectedPos, rendererSize, getElementSize(true, true), true);
    Dimension size = selectedPos.getSize();
    int curSel = getCurrentSelected();
    if (shouldRendererSelectedEntry && curSel > -1 && curSel < model.getSize()) {
        Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
        renderComponentBackground(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
    }
    if (paintFocusBehindList) {
        paintFocus(g, width, pos, rendererSize);
    }
    for (int i = startOffset; i <= endOffset; i++) {
        // skip on the selected
        if (i == getCurrentSelected() && animationPosition == 0) {
            continue;
        }
        calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
        if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
            T value = model.getItemAt(i);
            Component cmp = renderer.getListCellRendererComponent(this, value, i, false);
            cmp.setCellRenderer(true);
            Dimension sizeC = pos.getSize();
            renderComponent(g, cmp, pos.getX(), pos.getY(), sizeC.getWidth(), sizeC.getHeight());
        }
    }
    // if the animation has finished draw the selected element
    if (shouldRendererSelectedEntry) {
        Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
        renderComponent(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
    }
    if (!paintFocusBehindList) {
        paintFocus(g, width, pos, rendererSize);
    }
    g.translate(-xTranslate, -yTranslate);
    if (spinnerOverlay != null) {
        if (spinnerOverlay.getBorder() != null) {
            spinnerOverlay.getBorder().paintBorderBackground(g, this);
            spinnerOverlay.getBorder().paint(g, this);
        } else {
            spinnerOverlay.getBgPainter().paint(g, getBounds());
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 19 with Entry

use of com.codename1.ui.util.xml.Entry in project CodenameOne by codenameone.

the class CSSEngine method applyStyle.

/**
 * Applies the given style attributes to the HTML DOM entry
 *
 * @param element The element to apply the style to
 * @param selector The selector containing the style directives
 * @param htmlC The HTMLComponent
 */
private void applyStyle(HTMLElement element, CSSElement selector, HTMLComponent htmlC) {
    if ((element.getUi() != null) && (element.getUi().size() > 0)) {
        if (!HTMLComponent.PROCESS_HTML_MP1_ONLY) {
            String reset = selector.getAttributeById(CSSElement.CSS_COUNTER_RESET);
            if (reset != null) {
                htmlC.incCounter(reset, true);
            }
            String inc = selector.getAttributeById(CSSElement.CSS_COUNTER_INCREMENT);
            if (inc != null) {
                htmlC.incCounter(inc, false);
            }
            if ((selector.getSelectorPseudoClass() & (CSSElement.PC_BEFORE | CSSElement.PC_AFTER)) != 0) {
                handleContentProperty(element, selector, htmlC);
                return;
            }
        }
        for (int iter = 0; iter < element.getUi().size(); iter++) {
            Object o = element.getUi().elementAt(iter);
            if (o != null && o instanceof Component) {
                final Component cmp = (Component) o;
                applyStyleToUIElement(cmp, selector, element, htmlC);
            }
        }
    }
}
Also used : Component(com.codename1.ui.Component)

Example 20 with Entry

use of com.codename1.ui.util.xml.Entry in project CodenameOne by codenameone.

the class Resources method getTheme.

/**
 * Returns the theme resource from the file
 *
 * @param id name of the theme resource
 * @return cached theme instance
 */
public Hashtable getTheme(String id) {
    Hashtable h = (Hashtable) resources.get(id);
    // theme can be null in valid use cases such as the resource editor
    if (h != null && h.containsKey("uninitialized")) {
        Enumeration e = h.keys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            if (key.endsWith("font") || (key.endsWith("Image") && !key.endsWith("scaledImage"))) {
                Object value = h.get(key);
                if (value == null) {
                    throw new IllegalArgumentException("Couldn't find resource: " + key);
                }
                // it must be loaded now so we can resolve the temporary name
                if (value instanceof String) {
                    Object o;
                    // we need to trigger multi image logic  in the  resource editor
                    if (key.endsWith("Image")) {
                        o = getImage((String) value);
                    } else {
                        o = resources.get(value);
                    }
                    if (o == null) {
                        throw new IllegalArgumentException("Theme entry for " + key + " could not be found: " + value);
                    }
                    h.put(key, o);
                }
            }
            // been loaded yet when the border was created
            if (key.endsWith("order")) {
                Border b = confirmBorder(h, key);
                h.put(key, b);
            }
        }
        h.remove("uninitialized");
    }
    return h;
}
Also used : Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) AnimationObject(com.codename1.ui.animations.AnimationObject) RoundBorder(com.codename1.ui.plaf.RoundBorder) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Border(com.codename1.ui.plaf.Border)

Aggregations

IOException (java.io.IOException)7 Hashtable (java.util.Hashtable)5 ActionEvent (com.codename1.ui.events.ActionEvent)4 Container (com.codename1.ui.Container)3 Form (com.codename1.ui.Form)3 AnimationObject (com.codename1.ui.animations.AnimationObject)3 RoundBorder (com.codename1.ui.plaf.RoundBorder)3 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)3 EventDispatcher (com.codename1.ui.util.EventDispatcher)3 SpanButton (com.codename1.components.SpanButton)2 Component (com.codename1.ui.Component)2 EditorFont (com.codename1.ui.EditorFont)2 EditorTTFFont (com.codename1.ui.EditorTTFFont)2 EncodedImage (com.codename1.ui.EncodedImage)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 Border (com.codename1.ui.plaf.Border)2 LegacyFont (com.codename1.ui.util.xml.LegacyFont)2 DataInputStream (java.io.DataInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2