Search in sources :

Example 1 with com.codename1.ui

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

the class Resources method loadBitmapFont.

Font loadBitmapFont(DataInputStream input, String id, com.codename1.ui.Font font) throws IOException {
    Image bitmap = createImage(input);
    int charCount = input.readShort();
    int[] cutOffsets = new int[charCount];
    int[] charWidth = new int[charCount];
    for (int iter = 0; iter < charCount; iter++) {
        cutOffsets[iter] = input.readShort();
    }
    for (int iter = 0; iter < charCount; iter++) {
        charWidth[iter] = input.readByte();
    }
    String charset = input.readUTF();
    readRenderingHint(input);
    if (font == null) {
        if (Font.isBitmapFontEnabled()) {
            Font old = Font.getBitmapFont(id);
            if (old != null) {
                // old resource file use Font.clearBitmapCache()
                return old;
            }
            return Font.createBitmapFont(id, bitmap, cutOffsets, charWidth, charset);
        }
    }
    return font;
}
Also used : Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) Font(com.codename1.ui.Font)

Example 2 with com.codename1.ui

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

the class LazyValueC method showContainerImpl.

private Container showContainerImpl(String resourceName, Command sourceCommand, Component sourceComponent, boolean forceBack) {
    if (sourceComponent != null) {
        Form currentForm = sourceComponent.getComponentForm();
        // avoid the overhead of searching if no embedding is used
        if (currentForm.getClientProperty(EMBEDDED_FORM_FLAG) != null) {
            Container destContainer = sourceComponent.getParent();
            while (!(destContainer instanceof EmbeddedContainer || destContainer instanceof Form)) {
                // race condition, container was already removed by someone else
                if (destContainer == null) {
                    return null;
                }
                destContainer = destContainer.getParent();
            }
            if (destContainer instanceof EmbeddedContainer) {
                Container cnt = createContainer(fetchResourceFile(), resourceName, (EmbeddedContainer) destContainer);
                if (cnt instanceof Form) {
                    // Form f = (Form)cnt;
                    // cnt = formToContainer(f);
                    showForm((Form) cnt, sourceCommand, sourceComponent);
                    return cnt;
                }
                Component fromCmp = destContainer.getComponentAt(0);
                // This seems to be no longer necessary now that we have the replaceAndWait version that drops events
                // block the user from the ability to press the button twice by mistake
                // fromCmp.setEnabled(false);
                boolean isBack = forceBack;
                Transition t = fromCmp.getUIManager().getLookAndFeel().getDefaultFormTransitionOut();
                if (forceBack) {
                    initBackContainer(cnt, destContainer.getComponentForm(), getFormNavigationStackForComponent(sourceComponent));
                    t = t.copy(true);
                } else {
                    if (sourceCommand != null) {
                        if (t != null && backCommands != null && backCommands.contains(sourceCommand) || Display.getInstance().getCurrent().getBackCommand() == sourceCommand) {
                            isBack = true;
                            t = t.copy(true);
                        }
                    }
                }
                // create a back command if supported
                String commandAction = cnt.getName();
                Vector formNavigationStack = getFormNavigationStackForComponent(fromCmp);
                if (formNavigationStack != null && !isBack && allowBackTo(commandAction) && !isSameBackDestination((Container) fromCmp, cnt)) {
                    // trigger listener creation if this is the only command in the form
                    getFormListenerInstance(destContainer.getComponentForm(), null);
                    formNavigationStack.addElement(getContainerState((com.codename1.ui.Container) fromCmp));
                }
                beforeShowContainer(cnt);
                destContainer.replaceAndWait(fromCmp, cnt, t, true);
                postShowContainer(cnt);
                return cnt;
            } else {
                Container cnt = createContainer(fetchResourceFile(), resourceName);
                showForm((Form) cnt, sourceCommand, sourceComponent);
                return cnt;
            }
        }
    }
    Container cnt = createContainer(fetchResourceFile(), resourceName);
    if (cnt instanceof Form) {
        showForm((Form) cnt, sourceCommand, sourceComponent);
    } else {
        Form f = new Form();
        f.setLayout(new BorderLayout());
        f.addComponent(BorderLayout.CENTER, cnt);
        f.setName("Form" + cnt.getName());
        showForm(f, sourceCommand, sourceComponent);
    }
    return cnt;
}
Also used : Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) Transition(com.codename1.ui.animations.Transition) Component(com.codename1.ui.Component) Vector(java.util.Vector)

Example 3 with com.codename1.ui

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

the class UIManager method buildTheme.

private void buildTheme(Hashtable themeProps) {
    String con = (String) themeProps.get("@includeNativeBool");
    if (con != null && con.equalsIgnoreCase("true") && Display.getInstance().hasNativeTheme()) {
        boolean a = accessible;
        accessible = true;
        Display.getInstance().installNativeTheme();
        accessible = a;
    }
    Enumeration e = themeProps.keys();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        // this is a constant not a theme entry
        if (key.startsWith("@")) {
            themeConstants.put(key.substring(1, key.length()), themeProps.get(key));
            continue;
        }
        this.themeProps.put(key, themeProps.get(key));
    }
    // necessary to clear up the style so we don't get resedue from the previous UI
    defaultStyle = new Style();
    // create's the default style
    defaultStyle = createStyle("", "", false);
    defaultSelectedStyle = new Style(defaultStyle);
    defaultSelectedStyle = createStyle("", "sel#", true);
    String overlayThemes = (String) themeProps.get("@OverlayThemes");
    if (overlayThemes != null) {
        java.util.List<String> overlayThemesArr = StringUtil.tokenize(overlayThemes, ',');
        for (String th : overlayThemesArr) {
            th = th.trim();
            if (th.length() == 0) {
                continue;
            }
            try {
                Resources res = Resources.openLayered("/" + th);
                boolean a = accessible;
                accessible = true;
                addThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
                accessible = a;
            } catch (Exception ex) {
                System.err.println("Failed to load overlay theme file specified by @overlayThemes theme constant: " + th);
                Log.e(ex);
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Example 4 with com.codename1.ui

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

the class InstantUI method createEditUI.

/**
 * Creates editing UI for the given business object
 * @param bo the business object
 * @param autoCommit true if the bindings used should be auto-committed
 * @return a UI container that can be used to edit the business object
 */
public Container createEditUI(PropertyBusinessObject bo, boolean autoCommit) {
    Container cnt;
    if (Display.getInstance().isTablet()) {
        TableLayout tl = new TableLayout(1, 2);
        tl.setGrowHorizontally(true);
        cnt = new Container(tl);
    } else {
        cnt = new Container(BoxLayout.y());
    }
    UiBinding uib = new UiBinding();
    ArrayList<UiBinding.Binding> allBindings = new ArrayList<UiBinding.Binding>();
    for (PropertyBase b : bo.getPropertyIndex()) {
        if (isExcludedProperty(b)) {
            continue;
        }
        Class cls = (Class) b.getClientProperty("cn1$cmpCls");
        if (cls != null) {
            try {
                Component cmp = (Component) cls.newInstance();
                cmp.setName(b.getName());
                cnt.add(b.getLabel()).add(cmp);
                allBindings.add(uib.bind(b, cmp));
            } catch (Exception err) {
                Log.e(err);
                throw new RuntimeException("Custom property instant UI failed for " + b.getName() + " " + err);
            }
            continue;
        }
        String[] multiLabels = (String[]) b.getClientProperty("cn1$multiChceLbl");
        if (multiLabels != null) {
            // multi choice component
            final Object[] multiValues = (Object[]) b.getClientProperty("cn1$multiChceVal");
            if (multiLabels.length < 5) {
                // toggle buttons
                ButtonGroup bg = new ButtonGroup();
                RadioButton[] rbs = new RadioButton[multiLabels.length];
                cnt.add(b.getLabel());
                Container radioBox = new Container(new GridLayout(multiLabels.length));
                for (int iter = 0; iter < multiLabels.length; iter++) {
                    rbs[iter] = RadioButton.createToggle(multiLabels[iter], bg);
                    radioBox.add(rbs[iter]);
                }
                cnt.add(radioBox);
                allBindings.add(uib.bindGroup(b, multiValues, rbs));
            } else {
                Picker stringPicker = new Picker();
                stringPicker.setStrings(multiLabels);
                Map<Object, Object> m1 = new HashMap<Object, Object>();
                Map<Object, Object> m2 = new HashMap<Object, Object>();
                for (int iter = 0; iter < multiLabels.length; iter++) {
                    m1.put(multiLabels[iter], multiValues[iter]);
                    m2.put(multiValues[iter], multiLabels[iter]);
                }
                cnt.add(b.getLabel()).add(stringPicker);
                allBindings.add(uib.bind(b, stringPicker, new UiBinding.PickerAdapter<Object>(new UiBinding.MappingConverter(m1), new UiBinding.MappingConverter(m2))));
            }
            continue;
        }
        Class t = b.getGenericType();
        if (t != null) {
            if (t == Boolean.class) {
                CheckBox cb = new CheckBox();
                uib.bind(b, cb);
                cnt.add(b.getLabel()).add(cb);
                continue;
            }
            if (t == Date.class) {
                Picker dp = new Picker();
                dp.setType(Display.PICKER_TYPE_DATE);
                uib.bind(b, dp);
                cnt.add(b.getLabel()).add(dp);
                continue;
            }
        }
        TextField tf = new TextField();
        tf.setConstraint(getTextFieldConstraint(b));
        uib.bind(b, tf);
        cnt.add(b.getLabel()).add(tf);
    }
    cnt.putClientProperty("cn1$iui-binding", uib.createGroupBinding(allBindings));
    return cnt;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Container(com.codename1.ui.Container) GridLayout(com.codename1.ui.layouts.GridLayout) Picker(com.codename1.ui.spinner.Picker) TextField(com.codename1.ui.TextField) Component(com.codename1.ui.Component) TableLayout(com.codename1.ui.table.TableLayout) RadioButton(com.codename1.ui.RadioButton) ButtonGroup(com.codename1.ui.ButtonGroup) CheckBox(com.codename1.ui.CheckBox)

Example 5 with com.codename1.ui

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

the class UserInterfaceEditor method createCustomComponentButton.

private void createCustomComponentButton(final CustomComponent c) {
    try {
        final JButton b = new JButton(c.getType());
        b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/jdesktop/swingx/resources/placeholder32.png")));
        b.setHorizontalAlignment(SwingConstants.LEFT);
        b.setBorder(null);
        userComponents.add(b);
        b.putClientProperty("CustomComponent", c);
        final Class codenameOneBaseClass = c.getCls();
        makeDraggable(b, codenameOneBaseClass, c.getType(), c);
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (lockForDragging) {
                    lockForDragging = false;
                    return;
                }
                try {
                    if (c.isUiResource()) {
                        UIBuilderOverride u = new UIBuilderOverride();
                        com.codename1.ui.Component cmp = u.createContainer(res, c.getType());
                        String t = (String) cmp.getClientProperty(TYPE_KEY);
                        if (t == null) {
                            cmp.putClientProperty(TYPE_KEY, c.getType());
                            t = c.getType();
                        }
                        addComponentToContainer(cmp, t);
                        return;
                    }
                    com.codename1.ui.Component cmp = (com.codename1.ui.Component) codenameOneBaseClass.newInstance();
                    cmp.putClientProperty("CustomComponent", c);
                    cmp.putClientProperty(TYPE_KEY, c.getType());
                    initializeComponentText(cmp);
                    addComponentToContainer(cmp, c.getType());
                } catch (Exception err) {
                    err.printStackTrace();
                    JOptionPane.showMessageDialog(UserInterfaceEditor.this, err.getClass().getName() + ": " + err, "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    /*b.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if(BaseForm.isRightClick(e)) {
                        JPopupMenu p = new JPopupMenu();
                        AbstractAction deleteAction = new AbstractAction("Delete") {
                            public void actionPerformed(ActionEvent e) {
                                componentPalette.remove(b);
                                componentPalette.revalidate();
                                customComponents.remove(c);
                                res.setUi(name, persistContainer(containerInstance));
                            }
                        };
                        p.add(deleteAction);
                        p.show(b, e.getPoint().x, e.getPoint().y);
                    }
                }
            });*/
    } catch (Exception err) {
        err.printStackTrace();
        JOptionPane.showMessageDialog(UserInterfaceEditor.this, err.getClass().getName() + ": " + err, "Error", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) ActionListener(java.awt.event.ActionListener) ImageIcon(javax.swing.ImageIcon) Component(java.awt.Component) JComponent(javax.swing.JComponent)

Aggregations

EncodedImage (com.codename1.ui.EncodedImage)26 Component (com.codename1.ui.Component)23 Point (java.awt.Point)23 IOException (java.io.IOException)23 AnimationObject (com.codename1.ui.animations.AnimationObject)22 ArrayList (java.util.ArrayList)22 BufferedImage (java.awt.image.BufferedImage)19 Hashtable (java.util.Hashtable)18 Form (com.codename1.ui.Form)15 Timeline (com.codename1.ui.animations.Timeline)15 Image (com.codename1.ui.Image)13 EditableResources (com.codename1.ui.util.EditableResources)13 File (java.io.File)13 Vector (java.util.Vector)13 TextArea (com.codename1.ui.TextArea)12 Border (com.codename1.ui.plaf.Border)12 Label (com.codename1.ui.Label)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)10 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)10 Container (com.codename1.ui.Container)9