Search in sources :

Example 31 with Theme

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

the class EditableResources method save.

/**
 * Allows us to store a modified resource file
 */
public void save(OutputStream out) throws IOException {
    if (overrideFile != null) {
        overrideResource.save(new FileOutputStream(overrideFile));
    }
    // disable override for the duration of the save so stuff from the override doesn't
    // get into the main resource file
    File overrideFileBackup = overrideFile;
    EditableResources overrideResourceBackup = overrideResource;
    overrideResource = null;
    overrideFile = null;
    try {
        DataOutputStream output = new DataOutputStream(out);
        String[] resourceNames = getResourceNames();
        keyOffset = 0;
        if (currentPassword != null) {
            output.writeShort(resourceNames.length + 2);
            output.writeByte(MAGIC_PASSWORD);
            output.writeUTF("" + ((char) encode('l')) + ((char) encode('w')));
            output.writeByte(encode(MAGIC_HEADER & 0xff));
        } else {
            output.writeShort(resourceNames.length + 1);
            // write the header of the resource file
            output.writeByte(MAGIC_HEADER);
        }
        output.writeUTF("");
        // the size of the header
        output.writeShort(6);
        output.writeShort(MAJOR_VERSION);
        output.writeShort(MINOR_VERSION);
        // currently resource file meta-data isn't supported
        output.writeShort(0);
        for (int iter = 0; iter < resourceNames.length; iter++) {
            // write the magic number
            byte magic = getResourceType(resourceNames[iter]);
            switch(magic) {
                case MAGIC_TIMELINE:
                case MAGIC_ANIMATION_LEGACY:
                case MAGIC_IMAGE_LEGACY:
                case MAGIC_INDEXED_IMAGE_LEGACY:
                    magic = MAGIC_IMAGE;
                    break;
                case MAGIC_THEME_LEGACY:
                    magic = MAGIC_THEME;
                    break;
                case MAGIC_FONT_LEGACY:
                    magic = MAGIC_FONT;
                    break;
            }
            if (currentPassword != null) {
                output.writeByte(encode(magic & 0xff));
                char[] chars = resourceNames[iter].toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    chars[i] = (char) encode(chars[i] & 0xffff);
                }
                output.writeUTF(new String(chars));
            } else {
                output.writeByte(magic);
                output.writeUTF(resourceNames[iter]);
            }
            switch(magic) {
                case MAGIC_IMAGE:
                    Object o = getResourceObject(resourceNames[iter]);
                    if (!(o instanceof MultiImage)) {
                        o = null;
                    }
                    saveImage(output, getImage(resourceNames[iter]), (MultiImage) o, BufferedImage.TYPE_INT_ARGB);
                    continue;
                case MAGIC_THEME:
                    saveTheme(output, getTheme(resourceNames[iter]), magic == MAGIC_THEME_LEGACY);
                    continue;
                case MAGIC_FONT:
                    saveFont(output, false, resourceNames[iter]);
                    continue;
                case MAGIC_DATA:
                    {
                        InputStream i = getData(resourceNames[iter]);
                        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                        int val = i.read();
                        while (val != -1) {
                            outArray.write(val);
                            val = i.read();
                        }
                        byte[] data = outArray.toByteArray();
                        output.writeInt(data.length);
                        output.write(data);
                        continue;
                    }
                case MAGIC_UI:
                    {
                        InputStream i = getUi(resourceNames[iter]);
                        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                        int val = i.read();
                        while (val != -1) {
                            outArray.write(val);
                            val = i.read();
                        }
                        byte[] data = outArray.toByteArray();
                        output.writeInt(data.length);
                        output.write(data);
                        continue;
                    }
                case MAGIC_L10N:
                    // we are getting the theme which allows us to acces the l10n data
                    saveL10N(output, getTheme(resourceNames[iter]));
                    continue;
                default:
                    throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff));
            }
        }
        modified = false;
        updateModified();
        undoQueue.clear();
        redoQueue.clear();
    } finally {
        overrideFile = overrideFileBackup;
        overrideResource = overrideResourceBackup;
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) AnimationObject(com.codename1.ui.animations.AnimationObject) File(java.io.File)

Example 32 with Theme

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

the class EditableResources method saveTheme.

private void saveTheme(DataOutputStream output, Hashtable theme, boolean newVersion) throws IOException {
    theme.remove("name");
    output.writeShort(theme.size());
    for (Object currentKey : theme.keySet()) {
        String key = (String) currentKey;
        output.writeUTF(key);
        if (key.startsWith("@")) {
            if (key.endsWith("Image")) {
                output.writeUTF(findId(theme.get(key), true));
            } else {
                output.writeUTF((String) theme.get(key));
            }
            continue;
        }
        // if this is a simple numeric value
        if (key.endsWith("Color")) {
            output.writeInt(Integer.decode("0x" + theme.get(key)));
            continue;
        }
        if (key.endsWith("align") || key.endsWith("textDecoration")) {
            output.writeShort(((Number) theme.get(key)).shortValue());
            continue;
        }
        // if this is a short numeric value
        if (key.endsWith("transparency")) {
            output.writeByte(Integer.parseInt((String) theme.get(key)));
            continue;
        }
        if (key.endsWith("opacity")) {
            output.writeInt(Integer.parseInt((String) theme.get(key)));
            continue;
        }
        // if this is a padding or margin then we will have the 4 values as bytes
        if (key.endsWith("padding") || key.endsWith("margin")) {
            String[] arr = ((String) theme.get(key)).split(",");
            output.writeFloat(Float.parseFloat(arr[0]));
            output.writeFloat(Float.parseFloat(arr[1]));
            output.writeFloat(Float.parseFloat(arr[2]));
            output.writeFloat(Float.parseFloat(arr[3]));
            continue;
        }
        // padding and or margin type
        if (key.endsWith("Unit")) {
            for (byte b : (byte[]) theme.get(key)) {
                output.writeByte(b);
            }
            continue;
        }
        if (key.endsWith("border")) {
            Border border = (Border) theme.get(key);
            writeBorder(output, border, newVersion);
            continue;
        }
        // if this is a font
        if (key.endsWith("font")) {
            com.codename1.ui.Font f = (com.codename1.ui.Font) theme.get(key);
            // is this a new font?
            boolean newFont = f instanceof EditorFont;
            output.writeBoolean(newFont);
            if (newFont) {
                String fontId = findId(f);
                output.writeUTF(fontId);
            } else {
                output.writeByte(f.getFace());
                output.writeByte(f.getStyle());
                output.writeByte(f.getSize());
                if (f instanceof EditorTTFFont && (((EditorTTFFont) f).getFontFile() != null || ((EditorTTFFont) f).getNativeFontName() != null)) {
                    output.writeBoolean(true);
                    EditorTTFFont ed = (EditorTTFFont) f;
                    if (ed.getNativeFontName() != null) {
                        output.writeUTF(ed.getNativeFontName());
                        output.writeUTF(ed.getNativeFontName());
                    } else {
                        output.writeUTF(ed.getFontFile().getName());
                        output.writeUTF(((java.awt.Font) ed.getNativeFont()).getPSName());
                    }
                    output.writeInt(ed.getSizeSetting());
                    output.writeFloat(ed.getActualSize());
                } else {
                    output.writeBoolean(false);
                }
            }
            continue;
        }
        // if this is a background image
        if (key.endsWith("bgImage")) {
            String imageId = findId(theme.get(key), true);
            if (imageId == null) {
                imageId = "";
            }
            output.writeUTF(imageId);
            continue;
        }
        if (key.endsWith("scaledImage")) {
            output.writeBoolean(theme.get(key).equals("true"));
            continue;
        }
        if (key.endsWith("derive")) {
            output.writeUTF((String) theme.get(key));
            continue;
        }
        // if this is a background gradient
        if (key.endsWith("bgGradient")) {
            Object[] gradient = (Object[]) theme.get(key);
            output.writeInt(((Integer) gradient[0]).intValue());
            output.writeInt(((Integer) gradient[1]).intValue());
            output.writeFloat(((Float) gradient[2]).floatValue());
            output.writeFloat(((Float) gradient[3]).floatValue());
            output.writeFloat(((Float) gradient[4]).floatValue());
            continue;
        }
        if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
            output.writeByte(((Number) theme.get(key)).intValue());
            continue;
        }
        // thow an exception no idea what this is
        throw new IOException("Error while trying to read theme property: " + key);
    }
}
Also used : IOException(java.io.IOException) LegacyFont(com.codename1.ui.util.xml.LegacyFont) EditorTTFFont(com.codename1.ui.EditorTTFFont) EditorFont(com.codename1.ui.EditorFont) EditorTTFFont(com.codename1.ui.EditorTTFFont) AnimationObject(com.codename1.ui.animations.AnimationObject) EditorFont(com.codename1.ui.EditorFont) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 33 with Theme

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

the class EditableResources method setThemeProperties.

/**
 * Place a theme property into the resource editor
 */
public void setThemeProperties(final String themeName, final String[] keys, final Object[] values) {
    final Object[] oldValues = new Object[keys.length];
    Hashtable th = getTheme(themeName);
    if (th == null) {
        return;
    }
    for (int iter = 0; iter < keys.length; iter++) {
        oldValues[iter] = th.get(keys[iter]);
    }
    pushUndoable(new UndoableEdit() {

        @Override
        protected String performAction() {
            for (int iter = 0; iter < keys.length; iter++) {
                if (values[iter] == null) {
                    getTheme(themeName).remove(keys[iter]);
                } else {
                    getTheme(themeName).put(keys[iter], values[iter]);
                }
            }
            return themeName;
        }

        @Override
        protected String performUndo() {
            for (int iter = 0; iter < keys.length; iter++) {
                if (oldValues[iter] == null) {
                    getTheme(themeName).remove(keys[iter]);
                } else {
                    getTheme(themeName).put(keys[iter], oldValues[iter]);
                }
            }
            return themeName;
        }
    });
}
Also used : Hashtable(java.util.Hashtable) AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 34 with Theme

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

the class EditableResources method setThemeProperty.

/**
 * Place a theme property into the resource editor
 */
public void setThemeProperty(final String themeName, final String key, final Object value) {
    final Object oldValue = getTheme(themeName).get(key);
    pushUndoable(new UndoableEdit() {

        @Override
        protected String performAction() {
            if (value == null) {
                getTheme(themeName).remove(key);
            } else {
                getTheme(themeName).put(key, value);
            }
            return themeName;
        }

        @Override
        protected String performUndo() {
            if (oldValue == null) {
                getTheme(themeName).remove(key);
            } else {
                getTheme(themeName).put(key, oldValue);
            }
            return themeName;
        }
    });
}
Also used : AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 35 with Theme

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

the class ImageRGBEditor method findImageUseImpl.

private static void findImageUseImpl(com.codename1.ui.Image resourceValue, Vector users, EditableResources res, JLabel borderPreview) {
    for (String themeName : res.getThemeResourceNames()) {
        Hashtable theme = res.getTheme(themeName);
        for (Object key : theme.keySet()) {
            Object value = theme.get(key);
            if (value instanceof com.codename1.ui.Image) {
                if (value.equals(resourceValue)) {
                    addToUsers((String) key, users);
                }
            }
            if (value instanceof Border) {
                Border b = (Border) value;
                // BORDER_TYPE_IMAGE
                if (Accessor.getType(b) == Accessor.TYPE_IMAGE || Accessor.getType(b) == Accessor.TYPE_IMAGE_HORIZONTAL || Accessor.getType(b) == Accessor.TYPE_IMAGE_VERTICAL) {
                    com.codename1.ui.Image[] images = Accessor.getImages(b);
                    for (int i = 0; i < images.length; i++) {
                        if (images[i] == resourceValue) {
                            addToUsers((String) key, users);
                            if (borderPreview != null && borderPreview.getIcon() == null) {
                                int borderWidth = Math.max(100, b.getMinimumWidth());
                                int borderHeight = Math.max(100, b.getMinimumHeight());
                                com.codename1.ui.Image img = com.codename1.ui.Image.createImage(borderWidth, borderHeight);
                                com.codename1.ui.Label l = new com.codename1.ui.Label("Preview");
                                l.getStyle().setBorder(b);
                                l.setSize(new com.codename1.ui.geom.Dimension(borderWidth, borderHeight));
                                l.paintComponent(img.getGraphics());
                                CodenameOneImageIcon icon = new CodenameOneImageIcon(img, borderWidth, borderHeight);
                                borderPreview.setIcon(icon);
                            }
                        }
                    }
                }
            }
        }
    }
    // check if a timeline is making use of said image and replace it
    for (String image : res.getImageResourceNames()) {
        com.codename1.ui.Image current = res.getImage(image);
        if (current instanceof com.codename1.ui.animations.Timeline) {
            com.codename1.ui.animations.Timeline time = (com.codename1.ui.animations.Timeline) current;
            for (int iter = 0; iter < time.getAnimationCount(); iter++) {
                com.codename1.ui.animations.AnimationObject o = time.getAnimation(iter);
                if (AnimationAccessor.getImage(o) == resourceValue) {
                    addToUsers(image, users);
                }
            }
        }
    }
    // check if a UI resource is making use of the image
    UIBuilderOverride builder = new UIBuilderOverride();
    for (String uiResource : res.getUIResourceNames()) {
        com.codename1.ui.Container c = builder.createContainer(res, uiResource);
        if (ResourceEditorView.findImageInContainer(c, resourceValue)) {
            addToUsers(uiResource, users);
        }
    }
}
Also used : UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) Hashtable(java.util.Hashtable) JLabel(javax.swing.JLabel) EncodedImage(com.codename1.ui.EncodedImage) Border(com.codename1.ui.plaf.Border)

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