Search in sources :

Example 16 with Resources

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

the class CodenameOneActivity method onPrepareOptionsMenu.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.clear();
    try {
        Form currentForm = Display.getInstance().getCurrent();
        if (currentForm == null) {
            return false;
        }
        int numCommands = currentForm.getCommandCount();
        // If there are no commands, there's nothing to put in the menu
        if (numCommands == 0) {
            return false;
        }
        // Build menu items from commands
        for (int n = 0; n < numCommands; n++) {
            Command command = currentForm.getCommand(n);
            if (command != null) {
                String txt = currentForm.getUIManager().localize(command.getCommandName(), command.getCommandName());
                MenuItem item = menu.add(Menu.NONE, n, Menu.NONE, txt);
                Image icon = command.getIcon();
                if (icon != null) {
                    Bitmap b = (Bitmap) icon.getImage();
                    // Using BitmapDrawable with resources, to use device density (from 1.6 and above).
                    BitmapDrawable d = new BitmapDrawable(getResources(), b);
                    item.setIcon(d);
                }
                if (!command.isEnabled()) {
                    item.setEnabled(false);
                }
                if (android.os.Build.VERSION.SDK_INT >= 11 && command.getClientProperty("android:showAsAction") != null) {
                    String androidShowAsAction = command.getClientProperty("android:showAsAction").toString();
                    // "ifRoom" | "never" | "withText" | "always" | "collapseActionView"
                    if (androidShowAsAction.equalsIgnoreCase("ifRoom")) {
                        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                    } else if (androidShowAsAction.equalsIgnoreCase("never")) {
                        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
                    } else if (androidShowAsAction.equalsIgnoreCase("withText")) {
                        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
                    } else if (androidShowAsAction.equalsIgnoreCase("always")) {
                        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
                    } else if (android.os.Build.VERSION.SDK_INT >= 14 && androidShowAsAction.equalsIgnoreCase("collapseActionView")) {
                        // MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                        item.setShowAsAction(8);
                    }
                }
            }
        }
    } catch (Throwable t) {
    }
    return nativeMenu;
}
Also used : Bitmap(android.graphics.Bitmap) Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) MenuItem(android.view.MenuItem) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Image(com.codename1.ui.Image)

Example 17 with Resources

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

the class BlackBerryImplementation method installNativeTheme.

/**
 * Installs the native theme, this is only applicable if hasNativeTheme() returned true. Notice that this method
 * might replace the DefaultLookAndFeel instance and the default transitions.
 */
public void installNativeTheme() {
    if (nativeThemeAvailable) {
        try {
            InputStream is = getResourceAsStream(getClass(), "/blackberry_theme.res");
            Resources r = Resources.open(is);
            UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
            is.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Example 18 with Resources

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

the class IOSImplementation method installNativeTheme.

/**
 * Installs the native theme, this is only applicable if hasNativeTheme() returned true. Notice that this method
 * might replace the DefaultLookAndFeel instance and the default transitions.
 */
public void installNativeTheme() {
    try {
        Resources r;
        if (iosMode.equals("modern")) {
            r = Resources.open("/iOS7Theme.res");
            Hashtable tp = r.getTheme(r.getThemeResourceNames()[0]);
            if (!nativeInstance.isIOS7()) {
                tp.put("TitleArea.padding", "0,0,0,0");
            }
            UIManager.getInstance().setThemeProps(tp);
            return;
        }
        if (iosMode.equals("auto")) {
            if (nativeInstance.isIOS7()) {
                r = Resources.open("/iOS7Theme.res");
            } else {
                r = Resources.open("/iPhoneTheme.res");
            }
            UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
            return;
        }
        r = Resources.open("/iPhoneTheme.res");
        UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : Hashtable(java.util.Hashtable) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Example 19 with Resources

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

the class UIManager method parseStyle.

/**
 * Creates a style by providing style strings in a specific format. This method allows for the use of inline styles
 * to override the styles in {@link com.codename1.ui.Component}
 * @param theme Theme used to retrieve images referenced in the style strings.
 * @param id The style ID (UIID) to use to cache the style inside the theme.
 * @param prefix Prefix to use for styles.  Corresponds to the {@literal prefix} argument in {@link #getComponentStyleImpl(java.lang.String, boolean, java.lang.String)
 * @param baseStyle The style class from which this new style should derive.
 * @param selected True if this is for a selected style.
 * @param styleString Array of style strings to be parsed.  The format is {@literal key1:value1; key2:value2; etc...}.  While this looks similar to CSS, it is important to note that it is NOT
 * CSS.  The keys and values correspond to the properties of {@link com.codename1.ui.plaf.Style} and their associated values.
 * @return A style object representing the styles that were provided in the styleString.
 *
 * <h3>Example Usage</h3>
 *
 * {@code
 * Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
 *     "fgColor:ff0000; font:18mm; border: 1px solid ff0000; bgType:none; padding: 3mm; margin: 1mm");
 *
 * // Create a 9-piece image border on the fly:
 * Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
 *      "border:splicedImage /notes.png 0.3 0.4 0.3 0.4");
 *      // This splices the image found at /notes.png into 9 pieces.  Splice insets are specified by the 4 floating point values
 *      // at the end of the border directive:  [top] [right] [bottom] [left].
 * }
 */
Style parseStyle(Resources theme, String id, String prefix, String baseStyle, boolean selected, String... styleString) {
    String cacheKey = selected ? id + ".sel" : id + "." + prefix;
    String originalId = id;
    if (id == null || id.length() == 0) {
        // if no id return the default style
        id = "";
    } else {
        id = id + ".";
    }
    if (Arrays.toString(styleString).equals(parseCache().get(cacheKey)) && ((selected && selectedStyles.containsKey(id)) || (!selected && this.styles.containsKey(id)))) {
        return getComponentStyleImpl(originalId, selected, prefix);
    }
    parseCache().put(cacheKey, Arrays.toString(styleString));
    Style base = baseStyle != null ? getComponentStyleImpl(baseStyle, selected, prefix) : null;
    Map<String, String> styles = new HashMap<String, String>();
    for (String str : styleString) {
        StyleParser.parseString(styles, str);
    }
    StyleInfo styleInfo = new StyleInfo(styles);
    if (prefix != null && prefix.length() > 0) {
        id += prefix;
    }
    if (themeProps == null) {
        resetThemeProps(null);
    }
    if (baseStyle != null) {
        themeProps.put(id + "derive", baseStyle);
    } else {
        themeProps.remove(id + "derive");
    }
    String val = null;
    Integer bgColor = styleInfo.getBgColor();
    if (bgColor != null) {
        themeProps.put(id + Style.BG_COLOR, Integer.toHexString(bgColor));
    } else {
        themeProps.remove(id + Style.BG_COLOR);
    }
    Integer fgColor = styleInfo.getFgColor();
    if (fgColor != null) {
        themeProps.put(id + Style.FG_COLOR, Integer.toHexString(fgColor));
    } else {
        themeProps.remove(id + Style.FG_COLOR);
    }
    BorderInfo border = styleInfo.getBorder();
    if (border != null) {
        themeProps.put(id + Style.BORDER, border.createBorder(theme));
    } else {
        themeProps.remove(id + Style.BORDER);
    }
    Integer bgType = styleInfo.getBgType();
    if (bgType != null) {
        themeProps.put(id + Style.BACKGROUND_TYPE, bgType.byteValue());
    } else {
        themeProps.remove(id + Style.BACKGROUND_TYPE);
    }
    ImageInfo bgImage = styleInfo.getBgImage();
    if (bgImage != null) {
        themeProps.put(id + Style.BG_IMAGE, bgImage.getImage(theme));
    } else {
        themeProps.remove(id + Style.BG_IMAGE);
    }
    MarginInfo margin = styleInfo.getMargin();
    if (margin != null) {
        float[] marginArr = margin.createMargin(base);
        themeProps.put(id + Style.MARGIN, marginArr[Component.TOP] + "," + marginArr[Component.BOTTOM] + "," + marginArr[Component.LEFT] + "," + marginArr[Component.RIGHT]);
        byte[] unitArr = margin.createMarginUnit(base);
        themeProps.put(id + Style.MARGIN_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
    } else {
        themeProps.remove(id + Style.MARGIN);
        themeProps.remove(id + Style.MARGIN_UNIT);
    }
    PaddingInfo padding = styleInfo.getPadding();
    if (padding != null) {
        float[] paddingArr = padding.createPadding(base);
        themeProps.put(id + Style.PADDING, paddingArr[Component.TOP] + "," + paddingArr[Component.BOTTOM] + "," + paddingArr[Component.LEFT] + "," + paddingArr[Component.RIGHT]);
        byte[] unitArr = padding.createPaddingUnit(base);
        themeProps.put(id + Style.PADDING_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
    } else {
        themeProps.remove(id + Style.PADDING);
        themeProps.remove(id + Style.PADDING_UNIT);
    }
    Integer transparency = styleInfo.getTransparency();
    if (transparency != null) {
        themeProps.put(id + Style.TRANSPARENCY, String.valueOf(transparency.intValue()));
    } else {
        themeProps.remove(id + Style.TRANSPARENCY);
    }
    Integer opacity = styleInfo.getOpacity();
    if (opacity != null) {
        themeProps.put(id + Style.OPACITY, String.valueOf(opacity.intValue()));
    } else {
        themeProps.remove(id + Style.OPACITY);
    }
    Integer alignment = styleInfo.getAlignment();
    if (alignment != null) {
        themeProps.put(id + Style.ALIGNMENT, alignment);
    } else {
        themeProps.remove(id + Style.ALIGNMENT);
    }
    Integer textDecoration = styleInfo.getTextDecoration();
    if (textDecoration != null) {
        themeProps.put(id + Style.TEXT_DECORATION, textDecoration);
    } else {
        themeProps.remove(id + Style.TEXT_DECORATION);
    }
    FontInfo font = styleInfo.getFont();
    if (font != null) {
        themeProps.put(id + Style.FONT, font.createFont(base));
    } else {
        themeProps.remove(id + Style.FONT);
    }
    if (selected)
        selectedStyles.remove(id);
    else
        this.styles.remove(id);
    return getComponentStyleImpl(originalId, selected, prefix);
}
Also used : HashMap(java.util.HashMap) BorderInfo(com.codename1.ui.plaf.StyleParser.BorderInfo) PaddingInfo(com.codename1.ui.plaf.StyleParser.PaddingInfo) MarginInfo(com.codename1.ui.plaf.StyleParser.MarginInfo) StyleInfo(com.codename1.ui.plaf.StyleParser.StyleInfo) ImageInfo(com.codename1.ui.plaf.StyleParser.ImageInfo) FontInfo(com.codename1.ui.plaf.StyleParser.FontInfo)

Example 20 with Resources

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

the class UserInterfaceEditor method applyThemePreview.

private void applyThemePreview(String name) {
    if (name.indexOf('@') > -1) {
        FileInputStream f = null;
        try {
            String[] t = name.split("@");
            f = new FileInputStream(t[0]);
            Resources r = Resources.open(f);
            if (r.getTheme(t[1]) != null) {
                Accessor.setTheme(r.getTheme(t[1]));
            } else {
                Accessor.setTheme(r.getTheme(r.getThemeResourceNames()[0]));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                f.close();
            } catch (Exception ex) {
            }
        }
    } else {
        Accessor.setTheme(res.getTheme(name));
    }
}
Also used : EditableResources(com.codename1.ui.util.EditableResources) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

IOException (java.io.IOException)15 Resources (com.codename1.ui.util.Resources)14 FileInputStream (java.io.FileInputStream)6 Hashtable (java.util.Hashtable)6 Container (com.codename1.ui.Container)5 Form (com.codename1.ui.Form)5 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)5 InputStream (java.io.InputStream)5 EditableResources (com.codename1.ui.util.EditableResources)4 BufferedInputStream (com.codename1.io.BufferedInputStream)3 Command (com.codename1.ui.Command)3 Component (com.codename1.ui.Component)3 EncodedImage (com.codename1.ui.EncodedImage)3 Image (com.codename1.ui.Image)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 Button (com.codename1.ui.Button)2 RadioButton (com.codename1.ui.RadioButton)2 AnimationObject (com.codename1.ui.animations.AnimationObject)2 DataInputStream (java.io.DataInputStream)2