Search in sources :

Example 1 with Wrapper

use of com.codename1.util.Wrapper in project CodeRAD by shannah.

the class Actions method addToContainer.

public void addToContainer(Container cnt, Entity entity, ComponentSelector.ComponentMapper wrapper) {
    boolean requiresFlowLayoutWrapperForBadge = (cnt.getLayout() instanceof GridLayout || cnt.getLayout() instanceof BorderLayout || cnt.getLayout() instanceof BoxLayout);
    for (ActionNode n : this) {
        if (requiresFlowLayoutWrapperForBadge && n.getBadge() != null) {
            // If there is a badge, we'll wrap it in a flowlayout
            Container fl = FlowLayout.encloseCenter(n.createView(entity));
            fl.getStyle().stripMarginAndPadding();
            if (wrapper == null) {
                cnt.addComponent(fl);
            } else {
                cnt.addComponent(wrapper.map(fl));
            }
        } else {
            if (wrapper == null) {
                cnt.addComponent(n.createView(entity));
            } else {
                cnt.addComponent(wrapper.map(n.createView(entity)));
            }
        }
    }
}
Also used : GridLayout(com.codename1.ui.layouts.GridLayout) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) BoxLayout(com.codename1.ui.layouts.BoxLayout) ActionNode(com.codename1.rad.nodes.ActionNode)

Example 2 with Wrapper

use of com.codename1.util.Wrapper in project CodeRAD by shannah.

the class ProfileAvatarsTitleComponent method initUI.

private void initUI() {
    setLayout(new BorderLayout());
    wrapper = new Container(new FanLayout(FanLayout.X_AXIS));
    avatarWrapperViewController.setView(wrapper);
    $(wrapper).selectAllStyles().setPadding(0).setMargin(0).setBorder(Border.createEmpty());
    int len = getEntity().size();
    for (int i = len - 1; i >= 0; i--) {
        // for (Entity child : getEntity()) {
        Entity child = getEntity().get(i);
        ProfileAvatarView v = createAvatar(child);
        wrapper.add(v);
    }
    add(CENTER, wrapper);
    StringBuilder text = new StringBuilder();
    Property nameProp = getEntity().getRowType().findProperty(Thing.name);
    if (getEntity().size() > 0) {
        if (nameProp != null) {
            text.append(getEntity().get(0).getEntity().getText(nameProp));
        }
    }
    if (getEntity().size() == 2) {
        if (nameProp != null) {
            text.append(" & ").append(getEntity().get(1).getEntity().getText(nameProp));
        }
    }
    if (getEntity().size() > 2) {
        if (text.length() == 0) {
            text.append(getEntity().size()).append(" People");
        } else {
            text.append(" and ").append(getEntity().size() - 1).append(" others");
        }
    }
    Label lbl = new Label(text.toString());
    lbl.setUIID("AccountAvatarsTitleComponentText");
    add(SOUTH, FlowLayout.encloseCenter(lbl));
}
Also used : Entity(com.codename1.rad.models.Entity) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) FanLayout(com.codename1.rad.layouts.FanLayout) Label(com.codename1.ui.Label) Property(com.codename1.rad.models.Property)

Example 3 with Wrapper

use of com.codename1.util.Wrapper in project CodenameOne by codenameone.

the class ResourceEditorApp method main.

/**
 * Main method launching the application.
 */
public static void main(String[] args) throws Exception {
    JavaSEPortWithSVGSupport.blockMonitors();
    JavaSEPortWithSVGSupport.setDesignMode(true);
    JavaSEPortWithSVGSupport.setShowEDTWarnings(false);
    JavaSEPortWithSVGSupport.setShowEDTViolationStacks(false);
    // creates a deadlock between FX, Swing and CN1. Horrible horrible deadlock...
    JavaSEPortWithSVGSupport.blockNativeBrowser = true;
    if (args.length > 0) {
        if (args[0].equalsIgnoreCase("-buildVersion")) {
            Properties p = new Properties();
            try {
                p.load(ResourceEditorApp.class.getResourceAsStream("/version.properties"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println(p.getProperty("build", "1"));
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-style")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            final String uiid = args[2];
            String themeName = args[3];
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            Hashtable themeHash = res.getTheme(themeName);
            final AddThemeEntry entry = new AddThemeEntry(false, res, null, new Hashtable(themeHash), "", themeName);
            entry.setKeyValues(uiid, "");
            entry.setPreferredSize(new Dimension(1000, 600));
            JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.add(entry, BorderLayout.CENTER);
            JPanel bottom = new JPanel();
            ButtonGroup gr = new ButtonGroup();
            JRadioButton unsel = new JRadioButton("Unselected", true);
            gr.add(unsel);
            unsel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("");
                    entry.setKeyValues(uiid, "");
                    entry.revalidate();
                }
            });
            bottom.add(unsel);
            JRadioButton sel = new JRadioButton("Selected");
            gr.add(sel);
            sel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("sel#");
                    entry.setKeyValues(uiid, "sel#");
                    entry.revalidate();
                }
            });
            bottom.add(sel);
            JRadioButton press = new JRadioButton("Pressed");
            gr.add(press);
            press.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("press#");
                    entry.setKeyValues(uiid, "press#");
                    entry.revalidate();
                }
            });
            bottom.add(press);
            JRadioButton dis = new JRadioButton("Disabled");
            gr.add(dis);
            dis.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("dis#");
                    entry.setKeyValues(uiid, "dis#");
                    entry.revalidate();
                }
            });
            bottom.add(dis);
            wrapper.add(bottom, BorderLayout.SOUTH);
            if (ModifiableJOptionPane.showConfirmDialog(null, wrapper, "Edit") == JOptionPane.OK_OPTION) {
                Hashtable tmp = new Hashtable(themeHash);
                entry.updateThemeHashtable(tmp);
                res.setTheme(themeName, tmp);
            }
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-img")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            String imageName;
            String fileName;
            if (args.length == 3) {
                imageName = args[2];
                fileName = args[2];
            } else {
                if (args.length == 4) {
                    imageName = args[3];
                    fileName = args[2];
                } else {
                    System.out.println("The img command works as: -img path_to_resourceFile.res pathToImageFile [image name]");
                    System.exit(1);
                    return;
                }
            }
            File imageFile = new File(fileName);
            if (!imageFile.exists()) {
                System.out.println("File not found: " + imageFile.getAbsolutePath());
                System.exit(1);
                return;
            }
            com.codename1.ui.Image img = ImageRGBEditor.createImageStatic(imageFile);
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            res.setImage(imageName, img);
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-mimg")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            String fileName;
            if (args.length == 4) {
                fileName = args[3];
            } else {
                System.out.println("The mimg command works as: -img path_to_resourceFile.res dpi pathToImageFile");
                System.out.println("dpi can be one of:  high, veryhigh, hd, 560, 2hd, 4k");
                System.exit(1);
                return;
            }
            String dpi = args[2];
            int dpiInt = -1;
            switch(dpi.toLowerCase()) {
                case "high":
                    dpiInt = 3;
                    break;
                case "veryhigh":
                    dpiInt = 4;
                    break;
                case "hd":
                    dpiInt = 5;
                    break;
                case "560":
                    dpiInt = 6;
                    break;
                case "2hd":
                    dpiInt = 7;
                    break;
                case "4k":
                    dpiInt = 8;
                    break;
                default:
                    System.out.println("dpi can be one of:  high, veryhigh, hd, 560, 2hd, 4k");
                    System.exit(1);
                    return;
            }
            File imageFile = new File(fileName);
            if (!imageFile.exists()) {
                System.out.println("File not found: " + imageFile.getAbsolutePath());
                System.exit(1);
                return;
            }
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            AddAndScaleMultiImage.generateImpl(new File[] { imageFile }, res, dpiInt);
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("gen")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File output = new File(args[1]);
            generateResourceFile(output, args[2], args[3]);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("mig")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File projectDir = new File(args[1]);
            EditableResources.setXMLEnabled(true);
            EditableResources res = new EditableResources();
            res.openFileWithXMLSupport(new File(args[2]));
            migrateGuiBuilder(projectDir, res, args[3]);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-regen")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File output = new File(args[1]);
            EditableResources.setXMLEnabled(true);
            EditableResources res = new EditableResources();
            res.openFileWithXMLSupport(output);
            FileOutputStream fos = new FileOutputStream(output);
            res.save(fos);
            fos.close();
            generate(res, output);
            System.exit(0);
            return;
        }
    }
    JavaSEPortWithSVGSupport.setDefaultInitTarget(new JPanel());
    Display.init(null);
    launch(ResourceEditorApp.class, args);
}
Also used : JPanel(javax.swing.JPanel) JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) Properties(java.util.Properties) Container(com.codename1.ui.Container) BorderLayout(java.awt.BorderLayout) EditableResources(com.codename1.ui.util.EditableResources) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Dimension(java.awt.Dimension) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 4 with Wrapper

use of com.codename1.util.Wrapper in project CodenameOne by codenameone.

the class TestComponent method getComponentAt_int_int_nested_focusable_container.

private void getComponentAt_int_int_nested_focusable_container() {
    log("Testing getComponentAt(x, y) with layered nesting");
    int w = Display.getInstance().getDisplayWidth();
    int h = Display.getInstance().getDisplayHeight();
    Form f = new Form("My Form", new BorderLayout());
    Container bottom = new Container() {

        @Override
        protected Dimension calcPreferredSize() {
            return new Dimension(w, h);
        }
    };
    bottom.setGrabsPointerEvents(true);
    bottom.setName("Bottom");
    Container top = new Container(new BorderLayout());
    top.setFocusable(true);
    Label content = new Label("Hello");
    content.setName("Content");
    top.add(BorderLayout.CENTER, content);
    top.setName("Top");
    Container wrapper = new Container(new LayeredLayout());
    wrapper.add(bottom).add(top);
    f.add(BorderLayout.CENTER, wrapper);
    f.show();
    TestUtils.waitForFormTitle("My Form", 2000);
    Component middleComponent = f.getComponentAt(w / 2, h / 2);
    assertEqual(top, middleComponent, "Found wrong component");
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Dimension(com.codename1.ui.geom.Dimension) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) Point(com.codename1.ui.geom.Point)

Example 5 with Wrapper

use of com.codename1.util.Wrapper in project CodenameOne by codenameone.

the class Util method getFileSizeWithoutDownload.

/**
 * Returns -2 if the server doesn't accept partial downloads (and if
 * checkPartialDownloadSupport is true), -1 if the content length is unknow,
 * a value greater than 0 if the Content-Length is known.
 *
 * @param url
 * @param checkPartialDownloadSupport if true returns -2 if the server
 * doesn't accept partial downloads.
 * @return Content-Length if known
 */
public static long getFileSizeWithoutDownload(final String url, final boolean checkPartialDownloadSupport) {
    // documentation about the headers: https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
    // code discussed here: https://stackoverflow.com/a/62130371
    final Wrapper<Long> result = new Wrapper<Long>(0l);
    ConnectionRequest cr = new GZConnectionRequest() {

        @Override
        protected void readHeaders(Object connection) throws IOException {
            String acceptRanges = getHeader(connection, "Accept-Ranges");
            if (checkPartialDownloadSupport && (acceptRanges == null || !acceptRanges.equals("bytes"))) {
                // Log.p("The partial downloads of " + url + " are not supported.", Log.WARNING);
                result.set(-2l);
            } else {
                String contentLength = getHeader(connection, "Content-Length");
                if (contentLength != null) {
                    result.set(Long.parseLong(contentLength));
                } else {
                    // Log.p("The Content-Length of " + url + " is unknown.", Log.WARNING);
                    result.set(-1l);
                }
            }
        }
    };
    cr.setUrl(url);
    cr.setHttpMethod("HEAD");
    cr.setPost(false);
    NetworkManager.getInstance().addToQueueAndWait(cr);
    return result.get();
}
Also used : Wrapper(com.codename1.util.Wrapper) GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject)

Aggregations

Container (com.codename1.ui.Container)9 BorderLayout (com.codename1.ui.layouts.BorderLayout)9 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)6 IOException (java.io.IOException)6 Entity (com.codename1.rad.models.Entity)5 Form (com.codename1.ui.Form)5 ActionNode (com.codename1.rad.nodes.ActionNode)4 Label (com.codename1.ui.Label)4 Dimension (com.codename1.ui.geom.Dimension)4 ArrayList (java.util.ArrayList)4 EntityList (com.codename1.rad.models.EntityList)3 ActionEvent (com.codename1.ui.events.ActionEvent)3 ActionListener (com.codename1.ui.events.ActionListener)3 BoxLayout (com.codename1.ui.layouts.BoxLayout)3 UIManager (com.codename1.ui.plaf.UIManager)3 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)2 ControllerEvent (com.codename1.rad.controllers.ControllerEvent)2 EntityListProvider (com.codename1.rad.models.EntityListProvider)2 Button (com.codename1.ui.Button)2 Component (com.codename1.ui.Component)2