Search in sources :

Example 21 with EditableResources

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

the class FontTask method addToResources.

@Override
public void addToResources(EditableResources e) {
    if (logicalName == null) {
        logicalName = "Arial" + getIdentity() + size;
        createBitmap = false;
    } else {
        if (logicalName.indexOf('-') < 0) {
            logicalName += getIdentity() + size;
        }
    }
    Object aa;
    if (antiAliasing) {
        aa = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
    } else {
        aa = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    }
    EditorFont f = new EditorFont(Font.createSystemFont(systemFace, systemStyle, systemSize), null, logicalName, createBitmap, aa, charset);
    e.setFont(getName(), f);
}
Also used : EditorFont(com.codename1.ui.EditorFont)

Example 22 with EditableResources

use of com.codename1.ui.util.EditableResources 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 23 with EditableResources

use of com.codename1.ui.util.EditableResources 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)

Example 24 with EditableResources

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

the class ResourceEditorView method importResourceStream.

public void importResourceStream(InputStream is) throws IOException {
    EditableResources r = new EditableResources();
    r.openFile(is);
    checkDuplicateResourcesLoop(r, loadedResources.getThemeResourceNames(), r.getThemeResourceNames(), "Rename Theme", "Theme ");
    // load all the themes so rename will work properly on images and won't conflict
    for (String t : r.getThemeResourceNames()) {
        r.getTheme(t);
    }
    checkDuplicateResourcesLoop(r, loadedResources.getImageResourceNames(), r.getImageResourceNames(), "Rename Image", "Image ");
    checkDuplicateResourcesLoop(r, loadedResources.getL10NResourceNames(), r.getL10NResourceNames(), "Rename Localization", "Localization ");
    checkDuplicateResourcesLoop(r, loadedResources.getDataResourceNames(), r.getDataResourceNames(), "Rename Data", "Data ");
    checkDuplicateResourcesLoop(r, loadedResources.getUIResourceNames(), r.getUIResourceNames(), "Rename GUI", "GUI ");
    checkDuplicateResourcesLoop(r, loadedResources.getFontResourceNames(), r.getFontResourceNames(), "Rename Font", "Font ");
    for (String s : r.getImageResourceNames()) {
        if (r.isMultiImage(s)) {
            loadedResources.setMultiImage(s, (EditableResources.MultiImage) r.getResourceObject(s));
        } else {
            loadedResources.setImage(s, r.getImage(s));
        }
    }
    for (String s : r.getL10NResourceNames()) {
        loadedResources.setL10N(s, (Hashtable) r.getResourceObject(s));
    }
    for (String s : r.getDataResourceNames()) {
        loadedResources.setData(s, (byte[]) r.getResourceObject(s));
    }
    for (String s : r.getUIResourceNames()) {
        loadedResources.setUi(s, (byte[]) r.getResourceObject(s));
    }
    for (String s : r.getFontResourceNames()) {
        loadedResources.setFont(s, r.getFont(s));
    }
    for (String s : r.getThemeResourceNames()) {
        loadedResources.setTheme(s, r.getTheme(s));
    }
}
Also used : EditableResources(com.codename1.ui.util.EditableResources)

Example 25 with EditableResources

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

the class TimelineEditor method selectFile.

/**
 * Selects a gif file using a file chooser and converts it to a timeline
 */
public static void selectFile(ResourceEditorView view, EditableResources res, String timelineName) {
    File[] files = ResourceEditorView.showOpenFileChooser("Images", ".gif");
    if (files != null) {
        File sel = files[0];
        if (timelineName == null) {
            timelineName = sel.getName();
        }
        Preferences.userNodeForPackage(view.getClass()).put("lastDir", sel.getParentFile().getAbsolutePath());
        ImageReader iReader = ImageIO.getImageReadersBySuffix("gif").next();
        try {
            iReader.setInput(ImageIO.createImageInputStream(new FileInputStream(sel)));
            int frames = iReader.getNumImages(true);
            AnimationObject[] anims = new AnimationObject[frames];
            int currentTime = 0;
            for (int frameIter = 0; frameIter < frames; frameIter++) {
                BufferedImage currentImage = iReader.read(frameIter);
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                ImageIO.write(currentImage, "png", bo);
                bo.close();
                // create a PNG image in the resource file
                String label = sel.getName() + " frame:" + frameIter;
                EncodedImage i = EncodedImage.create(bo.toByteArray());
                res.setImage(label, i);
                int duration = Math.max(40, AnimationImpl.getFrameTime(iReader, frameIter));
                Point pos = AnimationImpl.getPixelOffsets(iReader, frameIter);
                anims[frameIter] = AnimationObject.createAnimationImage(i, pos.x, pos.y);
                anims[frameIter].setStartTime(currentTime);
                anims[frameIter].setEndTime(100000000);
                String disposeMethod = getDisposalMethod(iReader, frameIter);
                if (disposeMethod != null) {
                    if ("restoreToBackgroundColor".equals(disposeMethod)) {
                        if (frameIter + 1 < frames) {
                            int t = Math.max(40, AnimationImpl.getFrameTime(iReader, frameIter + 1));
                            anims[frameIter].setEndTime(currentTime + t);
                        } else {
                            anims[frameIter].setEndTime(currentTime + duration);
                        }
                    // for(int iter = frameIter ; iter >= 0 ; iter--) {
                    // anims[iter].setEndTime(currentTime);
                    // }
                    }
                // "none" |
                // "doNotDispose" | "restoreToBackgroundColor" |
                // "restoreToPrevious"
                }
                currentTime += duration;
            }
            Timeline t = Timeline.createTimeline(currentTime, anims, new com.codename1.ui.geom.Dimension(iReader.getWidth(0), iReader.getHeight(0)));
            res.setImage(timelineName, t);
            view.setSelectedResource(timelineName);
        } catch (IOException err) {
            err.printStackTrace();
            JOptionPane.showMessageDialog(JFrame.getFrames()[0], "Error reading file " + err, "IO Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Point(java.awt.Point) IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) FileInputStream(java.io.FileInputStream) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Timeline(com.codename1.ui.animations.Timeline) AnimationObject(com.codename1.ui.animations.AnimationObject) ImageReader(javax.imageio.ImageReader) File(java.io.File)

Aggregations

EditableResources (com.codename1.ui.util.EditableResources)12 IOException (java.io.IOException)12 Hashtable (java.util.Hashtable)11 ArrayList (java.util.ArrayList)10 EncodedImage (com.codename1.ui.EncodedImage)8 File (java.io.File)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)7 AnimationObject (com.codename1.ui.animations.AnimationObject)6 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)6 FileOutputStream (java.io.FileOutputStream)6 Point (java.awt.Point)5 BufferedImage (java.awt.image.BufferedImage)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Container (com.codename1.ui.Container)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 TableLayout (com.codename1.ui.table.TableLayout)4 EmbeddedContainer (com.codename1.ui.util.EmbeddedContainer)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4