Search in sources :

Example 51 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class EditableResources method openFileWithXMLSupport.

public void openFileWithXMLSupport(File f) throws IOException {
    if (xmlEnabled && f.getParentFile().getName().equals("src")) {
        File res = new File(f.getParentFile().getParentFile(), "res");
        if (res.exists()) {
            File xml = new File(res, f.getName().substring(0, f.getName().length() - 3) + "xml");
            if (xml.exists()) {
                loadingMode = true;
                com.codename1.ui.Font.clearBitmapCache();
                clear();
                try {
                    File resDir = new File(res, f.getName().substring(0, f.getName().length() - 4));
                    // open the XML file...
                    JAXBContext ctx = JAXBContext.newInstance(ResourceFileXML.class);
                    ResourceFileXML xmlData = (ResourceFileXML) ctx.createUnmarshaller().unmarshal(xml);
                    boolean normalize = xmlData.getMajorVersion() > 1 || xmlData.getMinorVersion() > 5;
                    majorVersion = (short) xmlData.getMajorVersion();
                    minorVersion = (short) xmlData.getMinorVersion();
                    xmlUI = xmlData.isUseXmlUI();
                    if (xmlData.getData() != null) {
                        for (Data d : xmlData.getData()) {
                            setResource(d.getName(), MAGIC_DATA, readFile(resDir, d.getName(), normalize));
                        }
                    }
                    if (xmlData.getLegacyFont() != null) {
                        for (LegacyFont d : xmlData.getLegacyFont()) {
                            String name = d.getName();
                            if (normalize) {
                                name = normalizeFileName(name);
                            }
                            DataInputStream fi = new DataInputStream(new FileInputStream(new File(resDir, name)));
                            setResource(d.getName(), MAGIC_FONT, loadFont(fi, d.getName(), false));
                            fi.close();
                        }
                    }
                    if (xmlData.getImage() != null) {
                        for (com.codename1.ui.util.xml.Image d : xmlData.getImage()) {
                            if (d.getType() == null) {
                                // standara JPG or PNG
                                String name = d.getName();
                                if (normalize) {
                                    name = normalizeFileName(name);
                                }
                                FileInputStream fi = new FileInputStream(new File(resDir, name));
                                EncodedImage e = EncodedImage.create(fi);
                                fi.close();
                                setResource(d.getName(), MAGIC_IMAGE, e);
                                continue;
                            }
                            if ("svg".equals(d.getType())) {
                                setResource(d.getName(), MAGIC_IMAGE, Image.createSVG(d.getType(), false, readFile(resDir, d.getName(), normalize)));
                                continue;
                            }
                            if ("timeline".equals(d.getType())) {
                                String name = d.getName();
                                if (normalize) {
                                    name = normalizeFileName(name);
                                }
                                DataInputStream fi = new DataInputStream(new FileInputStream(new File(resDir, name)));
                                setResource(d.getName(), MAGIC_IMAGE, readTimeline(fi));
                                fi.close();
                                continue;
                            }
                            if ("multi".equals(d.getType())) {
                                String name = d.getName();
                                if (normalize) {
                                    name = normalizeFileName(name);
                                }
                                File multiImageDir = new File(resDir, name);
                                File hd4k = new File(multiImageDir, "4k.png");
                                File hd2 = new File(multiImageDir, "2hd.png");
                                File hd560 = new File(multiImageDir, "560.png");
                                File hd = new File(multiImageDir, "hd.png");
                                File veryhigh = new File(multiImageDir, "veryhigh.png");
                                File high = new File(multiImageDir, "high.png");
                                File medium = new File(multiImageDir, "medium.png");
                                File low = new File(multiImageDir, "low.png");
                                File veryLow = new File(multiImageDir, "verylow.png");
                                Map<Integer, EncodedImage> images = new HashMap<Integer, EncodedImage>();
                                if (hd4k.exists()) {
                                    images.put(new Integer(Display.DENSITY_4K), EncodedImage.create(readFileNoNormal(hd4k)));
                                }
                                if (hd2.exists()) {
                                    images.put(new Integer(Display.DENSITY_2HD), EncodedImage.create(readFileNoNormal(hd2)));
                                }
                                if (hd560.exists()) {
                                    images.put(new Integer(Display.DENSITY_560), EncodedImage.create(readFileNoNormal(hd560)));
                                }
                                if (hd.exists()) {
                                    images.put(new Integer(Display.DENSITY_HD), EncodedImage.create(readFileNoNormal(hd)));
                                }
                                if (veryhigh.exists()) {
                                    images.put(new Integer(Display.DENSITY_VERY_HIGH), EncodedImage.create(readFileNoNormal(veryhigh)));
                                }
                                if (high.exists()) {
                                    images.put(new Integer(Display.DENSITY_HIGH), EncodedImage.create(readFileNoNormal(high)));
                                }
                                if (medium.exists()) {
                                    images.put(new Integer(Display.DENSITY_MEDIUM), EncodedImage.create(readFileNoNormal(medium)));
                                }
                                if (low.exists()) {
                                    images.put(new Integer(Display.DENSITY_LOW), EncodedImage.create(readFileNoNormal(low)));
                                }
                                if (veryLow.exists()) {
                                    images.put(new Integer(Display.DENSITY_VERY_LOW), EncodedImage.create(readFileNoNormal(veryLow)));
                                }
                                int[] dpis = new int[images.size()];
                                EncodedImage[] imageArray = new EncodedImage[images.size()];
                                int count = 0;
                                for (Map.Entry<Integer, EncodedImage> m : images.entrySet()) {
                                    dpis[count] = m.getKey().intValue();
                                    imageArray[count] = m.getValue();
                                    count++;
                                }
                                MultiImage result = new MultiImage();
                                result.setDpi(dpis);
                                result.setInternalImages(imageArray);
                                setResource(d.getName(), MAGIC_IMAGE, result);
                                continue;
                            }
                        }
                    }
                    if (xmlData.getL10n() != null) {
                        for (L10n d : xmlData.getL10n()) {
                            Hashtable<String, Hashtable<String, String>> l10n = new Hashtable<String, Hashtable<String, String>>();
                            for (Lang l : d.getLang()) {
                                Hashtable<String, String> language = new Hashtable<String, String>();
                                if (l != null && l.getEntry() != null) {
                                    for (Entry e : l.getEntry()) {
                                        language.put(e.getKey(), e.getValue());
                                    }
                                }
                                l10n.put(l.getName(), language);
                            }
                            setResource(d.getName(), MAGIC_L10N, l10n);
                        }
                    }
                    if (xmlData.getTheme() != null) {
                        for (Theme d : xmlData.getTheme()) {
                            Hashtable<String, Object> theme = new Hashtable<String, Object>();
                            theme.put("uninitialized", Boolean.TRUE);
                            if (d.getVal() != null) {
                                for (Val v : d.getVal()) {
                                    String key = v.getKey();
                                    if (key.endsWith("align") || key.endsWith("textDecoration") || key.endsWith(Style.ELEVATION)) {
                                        theme.put(key, Integer.valueOf(v.getValue()));
                                        continue;
                                    }
                                    if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT) || key.endsWith(Style.ICON_GAP_UNIT)) {
                                        theme.put(key, Byte.valueOf(v.getValue()));
                                        continue;
                                    }
                                    if (key.endsWith(Style.SURFACE)) {
                                        theme.put(key, Boolean.valueOf(v.getValue()));
                                        continue;
                                    }
                                    if (key.endsWith(Style.ICON_GAP)) {
                                        theme.put(key, Float.valueOf(v.getValue()));
                                        continue;
                                    }
                                    // padding and or margin type
                                    if (key.endsWith("Unit")) {
                                        String[] s = v.getValue().split(",");
                                        theme.put(key, new byte[] { Byte.parseByte(s[0]), Byte.parseByte(s[1]), Byte.parseByte(s[2]), Byte.parseByte(s[3]) });
                                        continue;
                                    }
                                    theme.put(key, v.getValue());
                                }
                            }
                            if (d.getBorder() != null) {
                                for (com.codename1.ui.util.xml.Border b : d.getBorder()) {
                                    if ("empty".equals(b.getType())) {
                                        theme.put(b.getKey(), Border.createEmpty());
                                        continue;
                                    }
                                    if ("round".equals(b.getType())) {
                                        RoundBorder rb = RoundBorder.create();
                                        rb = rb.opacity(b.getOpacity());
                                        rb = rb.color(b.getRoundBorderColor());
                                        rb = rb.rectangle(b.isRectangle());
                                        rb = rb.shadowBlur(b.getShadowBlur());
                                        rb = rb.shadowOpacity(b.getShadowOpacity());
                                        rb = rb.shadowSpread((int) b.getShadowSpread(), b.isShadowMM());
                                        rb = rb.shadowX(b.getShadowX());
                                        rb = rb.shadowY(b.getShadowY());
                                        rb = rb.stroke(b.getStrokeThickness(), b.isStrokeMM());
                                        rb = rb.strokeColor(b.getStrokeColor());
                                        rb = rb.strokeOpacity(b.getStrokeOpacity());
                                        theme.put(b.getKey(), rb);
                                        continue;
                                    }
                                    if ("css".equals(b.getType())) {
                                        CSSBorder cb = new CSSBorder(this, b.getCSS());
                                        theme.put(b.getKey(), cb);
                                        continue;
                                    }
                                    if ("roundRect".equals(b.getType())) {
                                        RoundRectBorder rb = RoundRectBorder.create();
                                        rb = rb.shadowBlur(b.getShadowBlur());
                                        rb = rb.shadowOpacity(b.getShadowOpacity());
                                        rb = rb.shadowSpread(b.getShadowSpread());
                                        rb = rb.shadowX(b.getShadowX());
                                        rb = rb.shadowY(b.getShadowY());
                                        rb = rb.stroke(b.getStrokeThickness(), b.isStrokeMM());
                                        rb = rb.strokeColor(b.getStrokeColor());
                                        rb = rb.strokeOpacity(b.getStrokeOpacity());
                                        rb = rb.bezierCorners(b.isBezierCorners());
                                        if (b.isTopOnlyMode()) {
                                            rb.topOnlyMode(true);
                                        } else if (b.isBottomOnlyMode()) {
                                            rb.bottomOnlyMode(true);
                                        }
                                        rb = rb.cornerRadius(b.getCornerRadius());
                                        theme.put(b.getKey(), rb);
                                        continue;
                                    }
                                    if ("line".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            if (b.isMillimeters()) {
                                                theme.put(b.getKey(), Border.createLineBorder(b.getThickness().floatValue()));
                                            } else {
                                                theme.put(b.getKey(), Border.createLineBorder(b.getThickness().intValue()));
                                            }
                                        } else {
                                            if (b.isMillimeters()) {
                                                theme.put(b.getKey(), Border.createLineBorder(b.getThickness().floatValue(), b.getColor().intValue()));
                                            } else {
                                                theme.put(b.getKey(), Border.createLineBorder(b.getThickness().intValue(), b.getColor().intValue()));
                                            }
                                        }
                                        continue;
                                    }
                                    if ("underline".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            theme.put(b.getKey(), Border.createUndelineBorder(b.getThickness().intValue()));
                                        } else {
                                            theme.put(b.getKey(), Border.createUnderlineBorder(b.getThickness().intValue(), b.getColor().intValue()));
                                        }
                                        continue;
                                    }
                                    if ("rounded".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            theme.put(b.getKey(), Border.createRoundBorder(b.getArcW().intValue(), b.getArcH().intValue()));
                                        } else {
                                            theme.put(b.getKey(), Border.createRoundBorder(b.getArcW().intValue(), b.getArcH().intValue(), b.getColor().intValue()));
                                        }
                                        continue;
                                    }
                                    if ("etchedRaised".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            theme.put(b.getKey(), Border.createEtchedRaised());
                                        } else {
                                            theme.put(b.getKey(), Border.createEtchedRaised(b.getColor().intValue(), b.getColorB().intValue()));
                                        }
                                        continue;
                                    }
                                    if ("etchedLowered".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            theme.put(b.getKey(), Border.createEtchedLowered());
                                        } else {
                                            theme.put(b.getKey(), Border.createEtchedLowered(b.getColor().intValue(), b.getColorB().intValue()));
                                        }
                                        continue;
                                    }
                                    if ("bevelLowered".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            theme.put(b.getKey(), Border.createBevelLowered());
                                        } else {
                                            theme.put(b.getKey(), Border.createBevelLowered(b.getColor().intValue(), b.getColorB().intValue(), b.getColorC().intValue(), b.getColorD().intValue()));
                                        }
                                        continue;
                                    }
                                    if ("bevelRaised".equals(b.getType())) {
                                        if (b.getColor() == null) {
                                            theme.put(b.getKey(), Border.createBevelRaised());
                                        } else {
                                            theme.put(b.getKey(), Border.createBevelRaised(b.getColor().intValue(), b.getColorB().intValue(), b.getColorC().intValue(), b.getColorD().intValue()));
                                        }
                                        continue;
                                    }
                                    if ("image".equals(b.getType())) {
                                        int imageCount = 2;
                                        if (b.getI9() != null) {
                                            imageCount = 9;
                                        } else {
                                            if (b.getI8() != null) {
                                                imageCount = 8;
                                            } else {
                                                if (b.getI3() != null) {
                                                    imageCount = 3;
                                                }
                                            }
                                        }
                                        String[] borderInstance;
                                        switch(imageCount) {
                                            case 2:
                                                borderInstance = new String[] { b.getI1(), b.getI2() };
                                                break;
                                            case 3:
                                                borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3() };
                                                break;
                                            case 8:
                                                borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3(), b.getI4(), b.getI5(), b.getI6(), b.getI7(), b.getI8() };
                                                break;
                                            default:
                                                borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3(), b.getI4(), b.getI5(), b.getI6(), b.getI7(), b.getI8(), b.getI9() };
                                                break;
                                        }
                                        theme.put(b.getKey(), borderInstance);
                                        continue;
                                    }
                                    if ("imageH".equals(b.getType())) {
                                        theme.put(b.getKey(), new String[] { "h", b.getI1(), b.getI2(), b.getI3() });
                                        continue;
                                    }
                                    if ("imageV".equals(b.getType())) {
                                        theme.put(b.getKey(), new String[] { "v", b.getI1(), b.getI2(), b.getI3() });
                                        continue;
                                    }
                                }
                            }
                            if (d.getFont() != null) {
                                for (com.codename1.ui.util.xml.Font b : d.getFont()) {
                                    if ("ttf".equals(b.getType())) {
                                        com.codename1.ui.Font system = com.codename1.ui.Font.createSystemFont(b.getFace().intValue(), b.getStyle().intValue(), b.getSize().intValue());
                                        EditorTTFFont t;
                                        if (b.getName().startsWith("native:")) {
                                            t = new EditorTTFFont(b.getName(), b.getSizeSettings().intValue(), b.getActualSize().floatValue(), system);
                                        } else {
                                            t = new EditorTTFFont(new File(f.getParentFile(), b.getName()), b.getSizeSettings().intValue(), b.getActualSize().floatValue(), system);
                                        }
                                        theme.put(b.getKey(), t);
                                        continue;
                                    }
                                    if ("system".equals(b.getType())) {
                                        com.codename1.ui.Font system = com.codename1.ui.Font.createSystemFont(b.getFace().intValue(), b.getStyle().intValue(), b.getSize().intValue());
                                        theme.put(b.getKey(), system);
                                        continue;
                                    }
                                // bitmap fonts aren't supported right now
                                }
                            }
                            if (d.getGradient() != null) {
                                for (com.codename1.ui.util.xml.Gradient b : d.getGradient()) {
                                    theme.put(b.getKey(), new Object[] { b.getColor1(), b.getColor2(), b.getPosX(), b.getPosY(), b.getRadius() });
                                }
                            }
                            setResource(d.getName(), MAGIC_THEME, theme);
                        }
                    }
                    // we load the UI last since it might depend on images or other elements in the future
                    if (xmlData.getUi() != null) {
                        if (xmlData.isUseXmlUI()) {
                            ArrayList<ComponentEntry> guiElements = new ArrayList<ComponentEntry>();
                            // place renderers first
                            final ArrayList<String> renderers = new ArrayList<String>();
                            for (Ui d : xmlData.getUi()) {
                                JAXBContext componentContext = JAXBContext.newInstance(ComponentEntry.class);
                                File uiFile = new File(resDir, normalizeFileName(d.getName()) + ".ui");
                                ComponentEntry uiXMLData = (ComponentEntry) componentContext.createUnmarshaller().unmarshal(uiFile);
                                guiElements.add(uiXMLData);
                                uiXMLData.findRendererers(renderers);
                            }
                            Collections.sort(guiElements, new Comparator<ComponentEntry>() {

                                private final ArrayList<String> entries1 = new ArrayList<String>();

                                private final ArrayList<String> entries2 = new ArrayList<String>();

                                @Override
                                public int compare(ComponentEntry o1, ComponentEntry o2) {
                                    if (renderers.contains(o1.getName())) {
                                        return -1;
                                    }
                                    if (renderers.contains(o2.getName())) {
                                        return 1;
                                    }
                                    entries1.clear();
                                    entries2.clear();
                                    o1.findEmbeddedDependencies(entries1);
                                    o2.findEmbeddedDependencies(entries2);
                                    if (entries1.size() == 0) {
                                        if (entries2.size() == 0) {
                                            return 0;
                                        }
                                        return -1;
                                    } else {
                                        if (entries2.size() == 0) {
                                            return 1;
                                        }
                                    }
                                    for (String e : entries1) {
                                        if (e.equals(o2.getName())) {
                                            return 1;
                                        }
                                    }
                                    for (String e : entries2) {
                                        if (e.equals(o1.getName())) {
                                            return -1;
                                        }
                                    }
                                    return 0;
                                }
                            });
                            for (ComponentEntry uiXMLData : guiElements) {
                                UIBuilderOverride uib = new UIBuilderOverride();
                                com.codename1.ui.Container cnt = uib.createInstance(uiXMLData, this);
                                // encountered an error loading the component fallback to loading with the binary types
                                if (cnt == null) {
                                    for (Ui ui : xmlData.getUi()) {
                                        setResource(uiXMLData.getName(), MAGIC_UI, readFile(resDir, ui.getName(), normalize));
                                    }
                                    break;
                                } else {
                                    byte[] data = UserInterfaceEditor.persistContainer(cnt, this);
                                    setResource(uiXMLData.getName(), MAGIC_UI, data);
                                }
                            }
                        } else {
                            for (Ui d : xmlData.getUi()) {
                                setResource(d.getName(), MAGIC_UI, readFile(resDir, d.getName(), normalize));
                            }
                        }
                    }
                    loadingMode = false;
                    modified = false;
                    updateModified();
                    // can occure when a resource file is opened via the constructor
                    if (undoQueue != null) {
                        undoQueue.clear();
                        redoQueue.clear();
                    }
                    return;
                } catch (JAXBException err) {
                    err.printStackTrace();
                }
            }
        }
    }
    openFile(new FileInputStream(f));
}
Also used : Val(com.codename1.ui.util.xml.Val) LegacyFont(com.codename1.ui.util.xml.LegacyFont) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EditorTTFFont(com.codename1.ui.EditorTTFFont) ComponentEntry(com.codename1.ui.util.xml.comps.ComponentEntry) ResourceFileXML(com.codename1.ui.util.xml.ResourceFileXML) JAXBException(javax.xml.bind.JAXBException) Lang(com.codename1.ui.util.xml.Lang) FileInputStream(java.io.FileInputStream) Ui(com.codename1.ui.util.xml.Ui) AnimationObject(com.codename1.ui.animations.AnimationObject) RoundBorder(com.codename1.ui.plaf.RoundBorder) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) L10n(com.codename1.ui.util.xml.L10n) JAXBContext(javax.xml.bind.JAXBContext) Entry(com.codename1.ui.util.xml.Entry) ComponentEntry(com.codename1.ui.util.xml.comps.ComponentEntry) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Hashtable(java.util.Hashtable) Data(com.codename1.ui.util.xml.Data) DataInputStream(java.io.DataInputStream) EncodedImage(com.codename1.ui.EncodedImage) CSSBorder(com.codename1.ui.plaf.CSSBorder) Theme(com.codename1.ui.util.xml.Theme)

Example 52 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class WebviewSnapshotter method fireJSSnap.

private void fireJSSnap(int x, int y, int w, int h) {
    // System.out.println("[WebviewSnapshotter::fireJSSnap("+x+","+y+","+w+","+h);
    if (!CN.isEdt()) {
        CN.callSerially(() -> {
            fireJSSnap(x, y, w, h);
        });
        return;
    }
    web.execute("window.snapper = window.snapper || {}; " + "window.snapper.handleJSSnap = function(scrollX, scrollY, x, y, w, h) {" + "  callback.onSuccess(JSON.stringify({scrollX:scrollX, scrollY:scrollY, x:x, y:y, w:w, h:h}));" + "}; window.getRectSnapshot(" + x + "," + y + "," + w + "," + h + ");", res -> {
        try {
            Result data = Result.fromContent(res.toString(), Result.JSON);
            handleJSSnap(data.getAsInteger("scrollX"), data.getAsInteger("scrollY"), data.getAsInteger("x"), data.getAsInteger("y"), data.getAsInteger("w"), data.getAsInteger("h"));
        } catch (Exception ex) {
            Log.p("Failed to parse callback in fireJSSnap");
            Log.e(ex);
        }
    });
}
Also used : Result(com.codename1.processing.Result)

Example 53 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class UIFragmentSample method testUIFragment.

private void testUIFragment() {
    Form f = new Form("Test Fragments", BoxLayout.y());
    TextArea ta = new TextArea();
    ta.setMaxSize(5000);
    String[] examples = new String[] { "<borderAbs><$button1 constraint='center'/><xng constraint='south'><$button2/><$button3/><$button4/></xng></borderAbs>", "{centerAbs:$button1, south:{xng:[$button2, $button3, $button4]}}", "{cs:$button1, south:{xng:[$button2, $button3, $button4]}}", "{ca:$button1, south:{xng:[$button2, $button3, $button4]}}", "{centerScale:$button1, south:{xng:[$button2, $button3, $button4]}}", "{ctb:$button1, south:{xng:[$button2, $button3, $button4]}}", "{centerTotalBelow:$button1, south:{xng:[$button2, $button3, $button4]}}", "{s:$button1, c:{xng:[$button2, $button3, $button4]}}", "{s:$button1, c:{x:[$button2, $button3, $button4]}}", "{s:$button1, c:{y:[$button2, $button3, $button4]}}", "{s:$button1, c:{yBottomLast:[$button2, $button3, $button4]}}", "{s:$button1, c:{ybl:[$button2, $button3, $button4]}}" };
    ComboBox cb = new ComboBox(examples);
    cb.addActionListener(e -> {
        ta.setText(examples[cb.getSelectedIndex()]);
    });
    ta.setText("<borderAbs><$button1 constraint='center'/><xng constraint='south'><$button2/><$button3/><$button4/></xng></borderAbs>");
    Button b = new Button("Compile");
    b.addActionListener(e -> {
        Form f2 = new Form("Result", new BorderLayout());
        f2.setToolbar(new Toolbar());
        f2.setTitle("Result");
        f2.setBackCommand("Back", null, evt -> {
            f.showBack();
        });
        f2.getToolbar().addCommandToLeftBar("Back", null, evt -> {
            f.showBack();
        });
        Button b1 = new Button("Button 1");
        Button b2 = new Button("Button 2");
        Button b3 = new Button("Button 3");
        Button b4 = new Button("Button 4");
        $(b1, b2, b3, b4).selectAllStyles().setBorder(RoundRectBorder.create().cornerRadius(2)).setBgColor(0x003399).setBgTransparency(0xff);
        UIFragment frag;
        if (ta.getText().charAt(0) == '<') {
            frag = UIFragment.parseXML(ta.getText());
        } else {
            System.out.println("Parsing " + ta.getText());
            frag = UIFragment.parseJSON(ta.getText());
        }
        f2.add(BorderLayout.CENTER, frag.set("button1", b1).set("button2", b2).set("button3", b3).set("button4", b4).getView());
        f2.show();
    });
    ta.setRows(5);
    f.addAll(cb, ta, b);
    f.show();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) TextArea(com.codename1.ui.TextArea) Button(com.codename1.ui.Button) ComboBox(com.codename1.ui.ComboBox) UIFragment(com.codename1.ui.UIFragment) Toolbar(com.codename1.ui.Toolbar)

Example 54 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class VideoPlayerSample method createDemo.

public Container createDemo(Form parent) {
    MultiButton helloOnline = new MultiButton("Hello (Online)");
    MultiButton helloOffline = new MultiButton("Hello (Offline)");
    helloOnline.setTextLine2("Play thru http");
    helloOffline.setTextLine2("Download & play");
    MultiButton capture = new MultiButton("Capture Video");
    capture.setTextLine2("Record video");
    MultiButton playCapturedFile = new MultiButton("Play Captured Video");
    playCapturedFile.setTextLine2("Last capture...");
    String capturedFile = getAppHomePath() + "captured-file.mp4";
    FontImage.setMaterialIcon(helloOnline, FontImage.MATERIAL_VIDEO_LIBRARY);
    FontImage.setMaterialIcon(helloOffline, FontImage.MATERIAL_VIDEO_LIBRARY);
    FontImage.setMaterialIcon(capture, FontImage.MATERIAL_VIDEOCAM);
    FontImage.setMaterialIcon(playCapturedFile, FontImage.MATERIAL_PERSONAL_VIDEO);
    Container cnt = BoxLayout.encloseY(ComponentGroup.enclose(helloOnline, helloOffline, capture, playCapturedFile));
    cnt.setScrollableY(true);
    helloOffline.addActionListener(e -> {
        if (!existsInFileSystem(getAppHomePath() + "hello-codenameone.mp4")) {
            downloadFile(parent);
        } else {
            playVideo(parent, getAppHomePath() + "hello-codenameone.mp4");
        }
    });
    // special case: the simulator doesn't support https URLs for media due to JavaFX limitations
    if (isSimulator()) {
        helloOnline.addActionListener(e -> {
            System.out.println("Handling action event");
            playVideo(parent, "http://www.codenameone.com/files/hello-codenameone.mp4");
        });
    } else {
        helloOnline.addActionListener(e -> playVideo(parent, "https://www.codenameone.com/files/hello-codenameone.mp4"));
    }
    capture.addActionListener(e -> {
        String result = Capture.captureVideo();
        if (result != null) {
            try {
                Util.copy(openFileInputStream(result), openFileOutputStream(capturedFile));
            } catch (IOException err) {
                Log.e(err);
                ToastBar.showErrorMessage("Error in copying captured file: " + err);
            }
        }
    });
    playCapturedFile.addActionListener(e -> {
        if (existsInFileSystem(capturedFile)) {
            playVideo(parent, capturedFile);
        } else {
            ToastBar.showErrorMessage("You need to capture a video first...");
        }
    });
    return cnt;
}
Also used : Container(com.codename1.ui.Container) IOException(java.io.IOException) MultiButton(com.codename1.components.MultiButton)

Example 55 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class FaceBookAccess method getUsersDetails.

/**
 * Gets users requested details ((this method uses the legacy rest api see http://developers.facebook.com/docs/reference/rest/))
 *
 * @param usersIds the users to query
 * @param fields which fields to query on the users see http://developers.facebook.com/docs/reference/rest/users.getInfo/
 * @param callback the result will call the callback with the result
 * to extrct the data preform the following:
 *  public void actionPerformed(ActionEvent evt) {
 *    Vector data = (Vector) ((NetworkEvent) evt).getMetaData();
 *    Vector users = (Vector) data.elementAt(0);
 * }
 */
public void getUsersDetails(String[] usersIds, String[] fields, final ActionListener callback) throws IOException {
    checkAuthentication();
    final FacebookRESTService con = new FacebookRESTService(token, "https://api.facebook.com/method/users.getInfo", false);
    String ids = usersIds[0];
    int ulen = usersIds.length;
    for (int i = 1; i < ulen; i++) {
        ids += "," + usersIds[i];
    }
    con.addArgumentNoEncoding("uids", ids);
    String fieldsStr = fields[0];
    int flen = fields.length;
    for (int i = 1; i < flen; i++) {
        fieldsStr += "," + fields[i];
    }
    con.addArgumentNoEncoding("fields", fieldsStr);
    con.addArgument("format", "json");
    con.addResponseListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (!con.isAlive()) {
                return;
            }
            if (callback != null) {
                callback.actionPerformed(evt);
            }
        }
    });
    if (slider != null) {
        SliderBridge.bindProgress(con, slider);
    }
    for (int i = 0; i < responseCodeListeners.size(); i++) {
        con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
    }
    current = con;
    NetworkManager.getInstance().addToQueue(con);
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Aggregations

IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)14 Form (com.codename1.ui.Form)12 Button (com.codename1.ui.Button)11 Map (java.util.Map)9 Date (java.util.Date)8 HashMap (java.util.HashMap)8 Label (com.codename1.ui.Label)7 BorderLayout (com.codename1.ui.layouts.BorderLayout)7 List (java.util.List)7 Container (com.codename1.ui.Container)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 OutputStream (java.io.OutputStream)6 JSONParser (com.codename1.io.JSONParser)5 Result (com.codename1.rad.processing.Result)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 File (java.io.File)5 Cursor (com.codename1.db.Cursor)4 ConnectionRequest (com.codename1.io.ConnectionRequest)4 Entity (com.codename1.rad.models.Entity)4