Search in sources :

Example 1 with UIFragment

use of com.codename1.ui.UIFragment in project CodenameOne by codenameone.

the class UIFragment method parseXML.

/**
 * Parses input stream of XML into a Template
 * @param input InputStream with XML content to parse
 * @return The corresponding template, or a RuntimeException if parsing failed.
 */
public static UIFragment parseXML(InputStream input) {
    try {
        XMLParser p = new XMLParser();
        Element el = p.parse(new InputStreamReader(input));
        return new UIFragment(el);
    } catch (Exception ex) {
        Log.e(ex);
        throw new RuntimeException(ex.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Element(com.codename1.xml.Element) XMLParser(com.codename1.xml.XMLParser) IOException(java.io.IOException)

Example 2 with UIFragment

use of com.codename1.ui.UIFragment 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 3 with UIFragment

use of com.codename1.ui.UIFragment in project CodenameOne by codenameone.

the class UIFragment method parseXML.

/**
 * Parses XML string into a Template
 * @param xml XML string describing a UI.
 * @return The corresponding template, or a RuntimeException if parsing failed.
 */
public static UIFragment parseXML(String xml) {
    try {
        XMLParser p = new XMLParser();
        p.setCaseSensitive(true);
        Element el = p.parse(new CharArrayReader(xml.toCharArray()));
        return new UIFragment(el);
    } catch (Exception ex) {
        Log.e(ex);
        throw new RuntimeException(ex.getMessage());
    }
}
Also used : CharArrayReader(com.codename1.io.CharArrayReader) Element(com.codename1.xml.Element) XMLParser(com.codename1.xml.XMLParser) IOException(java.io.IOException)

Aggregations

Element (com.codename1.xml.Element)2 XMLParser (com.codename1.xml.XMLParser)2 IOException (java.io.IOException)2 CharArrayReader (com.codename1.io.CharArrayReader)1 Button (com.codename1.ui.Button)1 ComboBox (com.codename1.ui.ComboBox)1 Form (com.codename1.ui.Form)1 TextArea (com.codename1.ui.TextArea)1 Toolbar (com.codename1.ui.Toolbar)1 UIFragment (com.codename1.ui.UIFragment)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 InputStreamReader (java.io.InputStreamReader)1