Search in sources :

Example 96 with com.codename1.rad.models

use of com.codename1.rad.models in project CodenameOne by codenameone.

the class CSSTheme method createImageBorders.

public void createImageBorders(WebViewProvider webviewProvider) {
    if (res == null) {
        res = new EditableResourcesForCSS(resourceFile);
    }
    ArrayList<Border> borders = new ArrayList<Border>();
    ResourcesMutator resm = new ResourcesMutator(res, Display.DENSITY_VERY_HIGH, minDpi, maxDpi);
    resm.targetDensity = targetDensity;
    List<Runnable> onComplete = new ArrayList<Runnable>();
    for (String id : elements.keySet()) {
        try {
            if (!isModified(id)) {
                continue;
            }
            Element e = (Element) elements.get(id);
            Element unselected = e.getUnselected();
            Map<String, LexicalUnit> unselectedStyles = (Map<String, LexicalUnit>) unselected.getFlattenedStyle();
            Border b = unselected.createBorder(unselectedStyles);
            Border unselectedBorder = b;
            currentId = id;
            if (e.requiresImageBorder(unselectedStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id, (img) -> {
                        Insets insets = unselected.getImageBorderInsets(unselectedStyles, img.getWidth(), img.getHeight());
                        resm.targetDensity = getSourceDensity(unselectedStyles, resm.targetDensity);
                        com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) insets.top, (int) insets.right, (int) insets.bottom, (int) insets.left);
                        resm.put(id + ".border", border);
                        unselectedBorder.border = border;
                        resm.targetDensity = targetDensity;
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".border", borders.get(borders.indexOf(unselectedBorder)).border);
                    });
                }
            } else if (e.requiresBackgroundImageGeneration(unselectedStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id, (img) -> {
                        int i = 1;
                        while (res.containsResource(id + "_" + i + ".png")) {
                            i++;
                        }
                        String prefix = id + "_" + i + ".png";
                        resm.targetDensity = getSourceDensity(unselectedStyles, resm.targetDensity);
                        Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
                        unselectedBorder.image = im;
                        resm.put(id + ".bgImage", im);
                        resm.targetDensity = targetDensity;
                    // resm.put(id+".press#bgType", Style.B)
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".bgImage", unselectedBorder.image);
                    });
                }
            }
            Element selected = e.getSelected();
            Map<String, LexicalUnit> selectedStyles = (Map<String, LexicalUnit>) selected.getFlattenedStyle();
            b = selected.createBorder(selectedStyles);
            Border selectedBorder = b;
            if (e.requiresImageBorder(selectedStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id + ".sel", (img) -> {
                        Insets insets = selected.getImageBorderInsets(selectedStyles, img.getWidth(), img.getHeight());
                        resm.targetDensity = getSourceDensity(selectedStyles, resm.targetDensity);
                        com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) insets.top, (int) insets.right, (int) insets.bottom, (int) insets.left);
                        resm.put(id + ".sel#border", border);
                        selectedBorder.border = border;
                        resm.targetDensity = targetDensity;
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".sel#border", borders.get(borders.indexOf(selectedBorder)).border);
                    });
                }
            } else if (e.requiresBackgroundImageGeneration(selectedStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id + ".sel", (img) -> {
                        int i = 1;
                        while (res.containsResource(id + "_" + i + ".png")) {
                            i++;
                        }
                        String prefix = id + "_" + i + ".png";
                        resm.targetDensity = getSourceDensity(selectedStyles, resm.targetDensity);
                        Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
                        selectedBorder.image = im;
                        resm.put(id + ".sel#bgImage", im);
                        // resm.put(id+".press#bgType", Style.B)
                        resm.targetDensity = targetDensity;
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".sel#bgImage", selectedBorder.image);
                    });
                }
            }
            Element pressed = e.getPressed();
            Map<String, LexicalUnit> pressedStyles = (Map<String, LexicalUnit>) pressed.getFlattenedStyle();
            b = pressed.createBorder(pressedStyles);
            Border pressedBorder = b;
            if (e.requiresImageBorder(pressedStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id + ".press", (img) -> {
                        Insets insets = pressed.getImageBorderInsets(pressedStyles, img.getWidth(), img.getHeight());
                        resm.targetDensity = getSourceDensity(pressedStyles, resm.targetDensity);
                        com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) insets.top, (int) insets.right, (int) insets.bottom, (int) insets.left);
                        resm.put(id + ".press#border", border);
                        pressedBorder.border = border;
                        resm.targetDensity = targetDensity;
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".press#border", borders.get(borders.indexOf(pressedBorder)).border);
                    });
                }
            } else if (e.requiresBackgroundImageGeneration(pressedStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id + ".press", (img) -> {
                        int i = 1;
                        while (res.containsResource(id + "_" + i + ".png")) {
                            i++;
                        }
                        String prefix = id + "_" + i + ".png";
                        resm.targetDensity = getSourceDensity(pressedStyles, resm.targetDensity);
                        Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
                        pressedBorder.imageId = prefix;
                        resm.put(id + ".press#bgImage", im);
                        resm.targetDensity = targetDensity;
                    // resm.put(id+".press#bgType", Style.B)
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".press#bgImage", res.findId(pressedBorder.imageId, true));
                    });
                }
            }
            Element disabled = e.getDisabled();
            Map<String, LexicalUnit> disabledStyles = (Map<String, LexicalUnit>) disabled.getFlattenedStyle();
            b = disabled.createBorder(disabledStyles);
            Border disabledBorder = b;
            if (e.requiresImageBorder(disabledStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id + ".dis", (img) -> {
                        Insets disabledInsets = disabled.getImageBorderInsets(disabledStyles, img.getWidth(), img.getHeight());
                        resm.targetDensity = getSourceDensity(disabledStyles, resm.targetDensity);
                        com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) disabledInsets.top, (int) disabledInsets.right, (int) disabledInsets.bottom, (int) disabledInsets.left);
                        disabledBorder.border = border;
                        resm.put(id + ".dis#border", border);
                        resm.targetDensity = targetDensity;
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".dis#border", borders.get(borders.indexOf(disabledBorder)).border);
                    });
                }
            } else if (e.requiresBackgroundImageGeneration(disabledStyles)) {
                if (!borders.contains(b)) {
                    borders.add(b);
                    resm.addImageProcessor(id + ".dis", (img) -> {
                        int i = 1;
                        while (res.containsResource(id + "_" + i + ".png")) {
                            i++;
                        }
                        String prefix = id + "_" + i + ".png";
                        resm.targetDensity = getSourceDensity(disabledStyles, resm.targetDensity);
                        Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
                        disabledBorder.image = im;
                        resm.put(id + ".dis#bgImage", im);
                        resm.targetDensity = targetDensity;
                    // resm.put(id+".press#bgType", Style.B)
                    });
                } else {
                    onComplete.add(() -> {
                        resm.put(id + ".dis#bgImage", disabledBorder.image);
                    });
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException("An exception occurred while processing the image border for element " + id, ex);
        }
    }
    if (requiresCaptureHtml()) {
        resm.createScreenshots(webviewProvider.getWebView(), generateCaptureHtml(), this.baseURL.toExternalForm());
    }
    for (Runnable r : onComplete) {
        r.run();
    }
}
Also used : Arrays(java.util.Arrays) CharArrayReader(java.io.CharArrayReader) URL(java.net.URL) ConditionalSelector(org.w3c.css.sac.ConditionalSelector) URISyntaxException(java.net.URISyntaxException) ObjectInputStream(java.io.ObjectInputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) SelectorList(org.w3c.css.sac.SelectorList) CSSParseException(org.w3c.css.sac.CSSParseException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) DocumentHandler(org.w3c.css.sac.DocumentHandler) ParserFactory(org.w3c.css.sac.helpers.ParserFactory) EditorTTFFont(com.codename1.ui.EditorTTFFont) Vector(java.util.Vector) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) Display(com.codename1.ui.Display) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Locale(java.util.Locale) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) Condition(org.w3c.css.sac.Condition) LexicalUnit(org.w3c.css.sac.LexicalUnit) Set(java.util.Set) InputSource(org.w3c.css.sac.InputSource) Logger(java.util.logging.Logger) Style(com.codename1.ui.plaf.Style) FileNotFoundException(java.io.FileNotFoundException) ImageInputStream(javax.imageio.stream.ImageInputStream) List(java.util.List) SACMediaList(org.w3c.css.sac.SACMediaList) ParseException(org.w3c.flute.parser.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CSSBorder(com.codename1.ui.plaf.CSSBorder) Util(com.codename1.io.Util) EditableResources(com.codename1.ui.util.EditableResources) MessageDigest(java.security.MessageDigest) CSSException(org.w3c.css.sac.CSSException) ElementSelector(org.w3c.css.sac.ElementSelector) HashMap(java.util.HashMap) AttributeCondition(org.w3c.css.sac.AttributeCondition) Selector(org.w3c.css.sac.Selector) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Accessor(com.codename1.ui.plaf.Accessor) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Parser(org.w3c.css.sac.Parser) SimpleSelector(org.w3c.css.sac.SimpleSelector) Component(com.codename1.ui.Component) Node(org.w3c.dom.Node) EncodedImage(com.codename1.ui.EncodedImage) ObjectOutputStream(java.io.ObjectOutputStream) NamedNodeMap(org.w3c.dom.NamedNodeMap) LinkedList(java.util.LinkedList) Hashtable(java.util.Hashtable) OutputStream(java.io.OutputStream) EditableResourcesForCSS(com.codename1.ui.util.EditableResourcesForCSS) ImageReader(javax.imageio.ImageReader) ErrorHandler(org.w3c.css.sac.ErrorHandler) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) Image(com.codename1.ui.Image) AnimationAccessor(com.codename1.ui.animations.AnimationAccessor) Resources(com.codename1.ui.util.Resources) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JSONParser(com.codename1.io.JSONParser) RoundBorder(com.codename1.ui.plaf.RoundBorder) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StringReader(java.io.StringReader) Font(com.codename1.ui.Font) Result(com.codename1.processing.Result) Collections(java.util.Collections) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) URISyntaxException(java.net.URISyntaxException) CSSParseException(org.w3c.css.sac.CSSParseException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.w3c.flute.parser.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CSSException(org.w3c.css.sac.CSSException) IOException(java.io.IOException) EditableResourcesForCSS(com.codename1.ui.util.EditableResourcesForCSS) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) CSSBorder(com.codename1.ui.plaf.CSSBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) LexicalUnit(org.w3c.css.sac.LexicalUnit)

Example 97 with com.codename1.rad.models

use of com.codename1.rad.models in project CodenameOne by codenameone.

the class CSSTheme method isInUse.

private boolean isInUse(String imageName) {
    if (isImageReferencedInCSS(imageName)) {
        return true;
    }
    Object multi = res.getResourceObject(imageName);
    if (multi instanceof EditableResources.MultiImage) {
        EditableResources.MultiImage m = (EditableResources.MultiImage) multi;
        for (com.codename1.ui.Image i : m.getInternalImages()) {
            if (isInUse(i)) {
                return true;
            }
        }
        return false;
    }
    com.codename1.ui.Image resourceValue = res.getImage(imageName);
    return isInUse(resourceValue);
}
Also used : Image(com.codename1.ui.Image) EditableResources(com.codename1.ui.util.EditableResources)

Example 98 with com.codename1.rad.models

use of com.codename1.rad.models in project CodenameOne by codenameone.

the class EditableResources method setResource.

void setResource(final String id, final byte type, final Object value) {
    /*if(!SwingUtilities.isEventDispatchThread()) {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        setResource(id, type, value);
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            } 
            return;
        }*/
    if (overrideResource != null) {
        overrideResource.setResource(id, type, value);
        return;
    }
    boolean exists = false;
    int index = -1;
    if (value != null) {
        exists = getResourceObject(id) != null;
    } else {
        index = getIndexOfChild(getParent(type), id);
    }
    Object superValue = value;
    if (multiPending != null) {
        if (superValue instanceof com.codename1.ui.EncodedImage) {
            superValue = multiPending;
        }
        multiPending = null;
    }
    super.setResource(id, type, superValue);
    if (superValue != null) {
        index = getIndexOfChild(getParent(type), id);
        if (exists) {
            fireTreeNodeChanged(id, index);
        } else {
            fireTreeNodeAdded(id, index);
        }
    } else {
        fireTreeNodeRemoved(id, type, index);
    }
}
Also used : AnimationObject(com.codename1.ui.animations.AnimationObject) EncodedImage(com.codename1.ui.EncodedImage)

Example 99 with com.codename1.rad.models

use of com.codename1.rad.models in project CodenameOne by codenameone.

the class EditableResources method saveFont.

private void saveFont(DataOutputStream output, boolean packed, String id) throws IOException {
    EditorFont f = (EditorFont) getFont(id);
    // write the system font fallback
    output.writeByte(f.getSystemFallback().getFace() | f.getSystemFallback().getSize() | f.getSystemFallback().getStyle());
    // do we have an emedded truetype font? Do we support embedded fonts?
    boolean trueTypeIncluded = f.getTruetypeFont() != null;
    output.writeBoolean(trueTypeIncluded);
    if (trueTypeIncluded) {
        output.writeInt(f.getTruetypeFont().length);
        output.write(f.getTruetypeFont());
    }
    boolean lookupIncluded = f.getLookupFont() != null;
    output.writeBoolean(lookupIncluded);
    if (lookupIncluded) {
        output.writeUTF(f.getLookupFont());
    }
    boolean bitmapIncluded = f.isIncludesBitmap();
    output.writeBoolean(bitmapIncluded);
    if (bitmapIncluded) {
        com.codename1.ui.Font bitmapFont = f.getBitmapFont();
        com.codename1.ui.Image cache = CodenameOneAccessor.getImage(bitmapFont);
        int[] imageArray = cache.getRGB();
        for (int iter = 0; iter < imageArray.length; iter++) {
            imageArray[iter] = (imageArray[iter] >> 8) & 0xff0000;
        }
        saveImage(output, com.codename1.ui.Image.createImage(imageArray, cache.getWidth(), cache.getHeight()), null, BufferedImage.TYPE_INT_RGB);
        String charset = bitmapFont.getCharset();
        int charCount = charset.length();
        output.writeShort(charCount);
        int[] cutOffsets = CodenameOneAccessor.getOffsets(bitmapFont);
        int[] charWidth = CodenameOneAccessor.getWidths(bitmapFont);
        for (int iter = 0; iter < charCount; iter++) {
            output.writeShort(cutOffsets[iter]);
        }
        for (int iter = 0; iter < charCount; iter++) {
            output.writeByte(charWidth[iter]);
        }
        output.writeUTF(charset);
        output.writeByte(f.getRenderingHint());
    }
}
Also used : Image(com.codename1.ui.Image) EditorFont(com.codename1.ui.EditorFont)

Example 100 with com.codename1.rad.models

use of com.codename1.rad.models in project CodenameOne by codenameone.

the class EditableResources method readMultiImage.

@Override
Image readMultiImage(DataInputStream input, boolean skipAll) throws IOException {
    com.codename1.ui.EncodedImage resultImage = null;
    int dpi = com.codename1.ui.Display.getInstance().getDeviceDensity();
    int dpiCount = input.readInt();
    int bestFitOffset = 0;
    int bestFitDPI = 0;
    int[] lengths = new int[dpiCount];
    dpisLoaded = new int[dpiCount];
    widthForDPI = new int[dpiCount];
    heightForDPI = new int[dpiCount];
    for (int iter = 0; iter < dpiCount; iter++) {
        int currentDPI = input.readInt();
        dpisLoaded[iter] = currentDPI;
        lengths[iter] = input.readInt();
        if (bestFitDPI != dpi && dpi >= currentDPI && currentDPI >= bestFitDPI) {
            bestFitDPI = currentDPI;
            bestFitOffset = iter;
        }
    }
    multiPending = new MultiImage();
    multiPending.setDpi(dpisLoaded);
    multiPending.setInternalImages(new EncodedImage[dpisLoaded.length]);
    for (int iter = 0; iter < lengths.length; iter++) {
        int size = lengths[iter];
        if (!skipAll && bestFitOffset == iter) {
            byte[] multiImageData = new byte[size];
            input.readFully(multiImageData, 0, size);
            resultImage = com.codename1.ui.EncodedImage.create(multiImageData);
            widthForDPI[iter] = resultImage.getWidth();
            heightForDPI[iter] = resultImage.getHeight();
            multiPending.getInternalImages()[iter] = resultImage;
        } else {
            byte[] multiImageData = new byte[size];
            input.readFully(multiImageData, 0, size);
            com.codename1.ui.EncodedImage tmp = com.codename1.ui.EncodedImage.create(multiImageData);
            widthForDPI[iter] = tmp.getWidth();
            heightForDPI[iter] = tmp.getHeight();
            multiPending.getInternalImages()[iter] = tmp;
        }
    }
    if (resultImage == null) {
        return Image.createImage(5, 5);
    }
    return resultImage;
}
Also used : EncodedImage(com.codename1.ui.EncodedImage)

Aggregations

IOException (java.io.IOException)34 EncodedImage (com.codename1.ui.EncodedImage)28 ArrayList (java.util.ArrayList)27 Point (java.awt.Point)25 File (java.io.File)24 AnimationObject (com.codename1.ui.animations.AnimationObject)22 BufferedImage (java.awt.image.BufferedImage)22 Hashtable (java.util.Hashtable)19 Component (com.codename1.ui.Component)18 Form (com.codename1.ui.Form)18 Image (com.codename1.ui.Image)16 EditableResources (com.codename1.ui.util.EditableResources)16 FileInputStream (java.io.FileInputStream)16 Timeline (com.codename1.ui.animations.Timeline)15 BorderLayout (com.codename1.ui.layouts.BorderLayout)14 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)11 FileOutputStream (java.io.FileOutputStream)10 AttributedString (java.text.AttributedString)10 EditorFont (com.codename1.ui.EditorFont)9