Search in sources :

Example 41 with Theme

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

the class ResourceEditorApp method importRes.

private static Hashtable importRes(EditableResources res, String file) {
    InputStream is = ResourceEditorApp.class.getResourceAsStream("/templates/" + file + ".res");
    Hashtable theme = new Hashtable();
    if (is != null) {
        try {
            EditableResources r = new EditableResources();
            r.openFile(is);
            is.close();
            if (r.getThemeResourceNames().length > 0) {
                theme = r.getTheme(r.getThemeResourceNames()[0]);
            }
            ResourceEditorView.checkDuplicateResourcesLoop(r, res.getImageResourceNames(), r.getImageResourceNames(), "Rename Image", "Image ", true, null);
            ResourceEditorView.checkDuplicateResourcesLoop(r, res.getL10NResourceNames(), r.getL10NResourceNames(), "Rename Localization", "Localization ", true, null);
            ResourceEditorView.checkDuplicateResourcesLoop(r, res.getDataResourceNames(), r.getDataResourceNames(), "Rename Data", "Data ", true, null);
            ResourceEditorView.checkDuplicateResourcesLoop(r, res.getUIResourceNames(), r.getUIResourceNames(), "Rename GUI", "GUI ", true, null);
            ResourceEditorView.checkDuplicateResourcesLoop(r, res.getFontResourceNames(), r.getFontResourceNames(), "Rename Font", "Font ", true, null);
            for (String s : r.getImageResourceNames()) {
                if (r.isMultiImage(s)) {
                    res.setMultiImage(s, (EditableResources.MultiImage) r.getResourceObject(s));
                } else {
                    res.setImage(s, r.getImage(s));
                }
            }
            for (String s : r.getL10NResourceNames()) {
                res.setL10N(s, (Hashtable) r.getResourceObject(s));
            }
            for (String s : r.getDataResourceNames()) {
                res.setData(s, (byte[]) r.getResourceObject(s));
            }
            for (String s : r.getUIResourceNames()) {
                res.setUi(s, (byte[]) r.getResourceObject(s));
            }
            for (String s : r.getFontResourceNames()) {
                res.setFont(s, r.getFont(s));
            }
        } catch (IOException err) {
            err.printStackTrace();
        }
    }
    return theme;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) IOException(java.io.IOException) EditableResources(com.codename1.ui.util.EditableResources)

Example 42 with Theme

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

the class JavaSEPort method loadSkinFile.

private void loadSkinFile(InputStream skin, final JFrame frm) {
    try {
        ZipInputStream z = new ZipInputStream(skin);
        ZipEntry e = z.getNextEntry();
        final Properties props = new Properties();
        BufferedImage map = null;
        BufferedImage landscapeMap = null;
        // if we load the native theme imediately the multi-image's will be loaded with the size of the old skin
        byte[] nativeThemeData = null;
        nativeThemeRes = null;
        nativeTheme = null;
        while (e != null) {
            String name = e.getName();
            if (name.equals("skin.png")) {
                portraitSkin = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("header.png")) {
                header = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("header_l.png")) {
                headerLandscape = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin.properties")) {
                props.load(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin_l.png")) {
                landscapeSkin = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin_map.png")) {
                map = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin_map_l.png")) {
                landscapeMap = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.endsWith(".res")) {
                long esize = e.getSize();
                if (esize > 0) {
                    nativeThemeData = new byte[(int) esize];
                    readFully(z, nativeThemeData);
                } else {
                    ByteArrayOutputStream b = new ByteArrayOutputStream();
                    Util.copyNoClose(z, b, 8192);
                    nativeThemeData = b.toByteArray();
                }
                e = z.getNextEntry();
                continue;
            }
            if (name.endsWith(".ttf")) {
                try {
                    java.awt.Font result = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, z);
                    GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(result);
                } catch (FontFormatException ex) {
                    ex.printStackTrace();
                }
                e = z.getNextEntry();
                continue;
            }
            e = z.getNextEntry();
        }
        z.close();
        String ppi = props.getProperty("ppi");
        if (ppi != null) {
            double ppiD = Double.valueOf(ppi);
            pixelMilliRatio = ppiD / 25.4;
        } else {
            String pix = props.getProperty("pixelRatio");
            if (pix != null && pix.length() > 0) {
                try {
                    pixelMilliRatio = Double.valueOf(pix);
                } catch (NumberFormatException err) {
                    err.printStackTrace();
                    pixelMilliRatio = null;
                }
            } else {
                pixelMilliRatio = null;
            }
        }
        portraitSkinHotspots = new HashMap<Point, Integer>();
        portraitScreenCoordinates = new Rectangle();
        landscapeSkinHotspots = new HashMap<Point, Integer>();
        landscapeScreenCoordinates = new Rectangle();
        if (props.getProperty("roundScreen", "false").equalsIgnoreCase("true")) {
            portraitScreenCoordinates.x = Integer.parseInt(props.getProperty("displayX"));
            portraitScreenCoordinates.y = Integer.parseInt(props.getProperty("displayY"));
            portraitScreenCoordinates.width = Integer.parseInt(props.getProperty("displayWidth"));
            portraitScreenCoordinates.height = Integer.parseInt(props.getProperty("displayHeight"));
            landscapeScreenCoordinates.x = portraitScreenCoordinates.y;
            landscapeScreenCoordinates.y = portraitScreenCoordinates.x;
            landscapeScreenCoordinates.width = portraitScreenCoordinates.height;
            landscapeScreenCoordinates.height = portraitScreenCoordinates.width;
            roundedSkin = true;
        } else {
            initializeCoordinates(map, props, portraitSkinHotspots, portraitScreenCoordinates);
            initializeCoordinates(landscapeMap, props, landscapeSkinHotspots, landscapeScreenCoordinates);
        }
        platformName = props.getProperty("platformName", "se");
        platformOverrides = props.getProperty("overrideNames", "").split(",");
        String ua = null;
        if (platformName.equals("and")) {
            ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
        } else if (platformName.equals("rim")) {
            ua = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9860; en-GB) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.296 Mobile Safari/534.11+";
        } else if (platformName.equals("ios")) {
            if (isTablet()) {
                ua = "Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5";
            } else {
                ua = "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5";
            }
        } else if (platformName.equals("me")) {
            ua = "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124";
        } else {
            if (platformName.equals("win")) {
                ua = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 800)";
            } else {
                ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
            }
        }
        Display.getInstance().setProperty("User-Agent", ua);
        isIOS = props.getProperty("systemFontFamily", "Arial").toLowerCase().contains("helvetica");
        setFontFaces(props.getProperty("systemFontFamily", "Arial"), props.getProperty("proportionalFontFamily", "SansSerif"), props.getProperty("monospaceFontFamily", "Monospaced"));
        int med;
        int sm;
        int la;
        if (pixelMilliRatio == null) {
            float factor = ((float) getDisplayHeightImpl()) / 480.0f;
            med = (int) (15.0f * factor);
            sm = (int) (11.0f * factor);
            la = (int) (19.0f * factor);
        } else {
            med = (int) Math.round(2.6 * pixelMilliRatio.doubleValue());
            sm = (int) Math.round(2 * pixelMilliRatio.doubleValue());
            la = (int) Math.round(3.3 * pixelMilliRatio.doubleValue());
        }
        setFontSize(Integer.parseInt(props.getProperty("mediumFontSize", "" + med)), Integer.parseInt(props.getProperty("smallFontSize", "" + sm)), Integer.parseInt(props.getProperty("largeFontSize", "" + la)));
        tablet = props.getProperty("tablet", "false").equalsIgnoreCase("true");
        rotateTouchKeysOnLandscape = props.getProperty("rotateKeys", "false").equalsIgnoreCase("true");
        touchDevice = props.getProperty("touch", "true").equalsIgnoreCase("true");
        keyboardType = Integer.parseInt(props.getProperty("keyboardType", "0"));
        softkeyCount = Integer.parseInt(props.getProperty("softbuttonCount", "1"));
        if (softkeyCount < 2) {
            // the values of the static variables to be correct!
            try {
                Field f = com.codename1.ui.MenuBar.class.getDeclaredField("leftSK");
                f.setAccessible(true);
                f.setInt(null, KeyEvent.VK_F1);
                f = com.codename1.ui.MenuBar.class.getDeclaredField("rightSK");
                f.setAccessible(true);
                f.setInt(null, KeyEvent.VK_F2);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        final byte[] nativeThemeFinalData = nativeThemeData;
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                if (nativeThemeFinalData != null) {
                    try {
                        nativeThemeRes = Resources.open(new ByteArrayInputStream(nativeThemeFinalData));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    try {
                        boolean isJ2me = props.getProperty("platformName", "").equals("me");
                        String t = props.getProperty("nativeThemeAttribute", null);
                        if (t != null) {
                            Properties cnop = new Properties();
                            File cnopFile = new File("codenameone_settings.properties");
                            if (cnopFile.exists()) {
                                cnop.load(new FileInputStream(cnopFile));
                                int themeConst = Integer.parseInt(cnop.getProperty("codename1.j2me.nativeThemeConst", "3"));
                                t = cnop.getProperty(t, null);
                                if (isJ2me && themeConst == 3 && t != null && new File(t).exists()) {
                                    nativeThemeRes = Resources.open(new FileInputStream(t));
                                }
                            }
                        }
                    } catch (IOException ioErr) {
                        ioErr.printStackTrace();
                    }
                }
            }
        });
        installMenu(frm, false);
    } catch (IOException err) {
        err.printStackTrace();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) Rectangle(java.awt.Rectangle) Properties(com.codename1.io.Properties) BufferedImage(java.awt.image.BufferedImage) Field(java.lang.reflect.Field) Point(java.awt.Point) FontFormatException(java.awt.FontFormatException) Point(java.awt.Point) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) java.awt(java.awt)

Example 43 with Theme

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

the class Dialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param c the context component which is used to position the dialog and can also be pointed at
 * @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
 */
public Command showPopupDialog(Component c) {
    Rectangle componentPos = c.getSelectedRect();
    componentPos.setX(componentPos.getX() - c.getScrollX());
    componentPos.setY(componentPos.getY() - c.getScrollY());
    return showPopupDialog(componentPos);
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 44 with Theme

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

the class Form method createStatusBar.

/**
 * Subclasses can override this method to control the creation of the status bar component.
 * Notice that this method will only be invoked if the paintsTitleBarBool theme constant is true
 * which it is on iOS by default
 * @return a Component that represents the status bar if the OS requires status bar spacing
 */
protected Component createStatusBar() {
    if (getUIManager().isThemeConstant("statusBarScrollsUpBool", true)) {
        Button bar = new Button();
        bar.setShowEvenIfBlank(true);
        bar.setUIID("StatusBar");
        bar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                Component c = findScrollableChild(getContentPane());
                if (c != null) {
                    c.scrollRectToVisible(new Rectangle(0, 0, 10, 10), c);
                }
            }
        });
        return bar;
    } else {
        Container bar = new Container();
        bar.setUIID("StatusBar");
        return bar;
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Rectangle(com.codename1.ui.geom.Rectangle)

Example 45 with Theme

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

the class AndroidImplementation 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() {
    hasNativeTheme();
    if (nativeThemeAvailable) {
        try {
            InputStream is;
            if (android.os.Build.VERSION.SDK_INT < 14 && !isTablet() || Display.getInstance().getProperty("and.hololight", "false").equals("true")) {
                is = getResourceAsStream(getClass(), "/androidTheme.res");
            } else {
                is = getResourceAsStream(getClass(), "/android_holo_light.res");
            }
            Resources r = Resources.open(is);
            Hashtable h = r.getTheme(r.getThemeResourceNames()[0]);
            h.put("@commandBehavior", "Native");
            UIManager.getInstance().setThemeProps(h);
            is.close();
            Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_NATIVE);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Aggregations

Hashtable (java.util.Hashtable)18 AnimationObject (com.codename1.ui.animations.AnimationObject)16 IOException (java.io.IOException)16 EncodedImage (com.codename1.ui.EncodedImage)12 Border (com.codename1.ui.plaf.Border)12 Image (com.codename1.ui.Image)10 RoundBorder (com.codename1.ui.plaf.RoundBorder)10 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)10 BufferedImage (java.awt.image.BufferedImage)8 FileInputStream (java.io.FileInputStream)8 Resources (com.codename1.ui.util.Resources)7 File (java.io.File)7 EditorTTFFont (com.codename1.ui.EditorTTFFont)6 EditableResources (com.codename1.ui.util.EditableResources)6 InputStream (java.io.InputStream)6 EditorFont (com.codename1.ui.EditorFont)5 DataInputStream (java.io.DataInputStream)5 Container (com.codename1.ui.Container)4 Form (com.codename1.ui.Form)4 Label (com.codename1.ui.Label)4