Search in sources :

Example 21 with Media

use of com.servoy.j2db.persistence.Media in project servoy-client by Servoy.

the class ImageLoader method paintBackgroundImage.

private static void paintBackgroundImage(Graphics graphics, IStyleRule styleRule, IApplication application, String url, Dimension parentSize) {
    int start = url.indexOf(MediaURLStreamHandler.MEDIA_URL_DEF);
    if (start != -1) {
        String name = url.substring(start + MediaURLStreamHandler.MEDIA_URL_DEF.length());
        if (name.endsWith("')") || name.endsWith("\")"))
            name = name.substring(0, name.length() - 2);
        if (name.endsWith(")"))
            name = name.substring(0, name.length() - 1);
        Media media = application.getFlattenedSolution().getMedia(name);
        if (media != null) {
            byte[] imageData = media.getMediaData();
            if (styleRule.hasAttribute(CSSName.BACKGROUND_SIZE.toString())) {
                PropertyDeclaration declaration = ((ServoyStyleRule) styleRule).getPropertyDeclaration(CSSName.BACKGROUND_SIZE.toString());
                if (declaration.getValue() instanceof PropertyValue && ((PropertyValue) declaration.getValue()).getValues() != null && ((PropertyValue) declaration.getValue()).getValues().size() == 2) {
                    boolean autoWidth = "auto".equals(((CSSPrimitiveValue) ((PropertyValue) declaration.getValue()).getValues().get(0)).getCssText());
                    boolean autoHeight = "auto".equals(((CSSPrimitiveValue) ((PropertyValue) declaration.getValue()).getValues().get(1)).getCssText());
                    Boolean fixedWidth = null;
                    if (autoWidth && !autoHeight) {
                        fixedWidth = Boolean.FALSE;
                    } else if (autoHeight && !autoWidth) {
                        fixedWidth = Boolean.TRUE;
                    }
                    int width = getImageSize(parentSize.width, ((CSSPrimitiveValue) ((PropertyValue) declaration.getValue()).getValues().get(0)).getCssText());
                    int height = getImageSize(parentSize.height, ((CSSPrimitiveValue) ((PropertyValue) declaration.getValue()).getValues().get(1)).getCssText());
                    imageData = resize(imageData, width, height, autoWidth || autoHeight, fixedWidth);
                }
            }
            Image image = ImageLoader.getBufferedImage(imageData, -1, -1, false);
            int offsetWidth = 0;
            int offsetHeight = 0;
            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);
            if (styleRule.hasAttribute(CSSName.BACKGROUND_POSITION.toString())) {
                PropertyDeclaration declaration = ((ServoyStyleRule) styleRule).getPropertyDeclaration(CSSName.BACKGROUND_POSITION.toString());
                if (declaration.getValue() instanceof PropertyValue && ((PropertyValue) declaration.getValue()).getValues() != null && ((PropertyValue) declaration.getValue()).getValues().size() == 2) {
                    offsetWidth = getImagePosition(parentSize.width, imageWidth, ((CSSPrimitiveValue) ((PropertyValue) declaration.getValue()).getValues().get(0)).getCssText());
                    offsetHeight = getImagePosition(parentSize.height, imageHeight, ((CSSPrimitiveValue) ((PropertyValue) declaration.getValue()).getValues().get(1)).getCssText());
                }
            }
            boolean hRepeat = true;
            boolean vRepeat = true;
            if (styleRule.hasAttribute(CSSName.BACKGROUND_REPEAT.toString())) {
                String repeat = styleRule.getValue(CSSName.BACKGROUND_REPEAT.toString());
                hRepeat = false;
                vRepeat = false;
                if ("repeat".equals(repeat) || "repeat-x".equals(repeat))
                    hRepeat = true;
                if ("repeat".equals(repeat) || "repeat-y".equals(repeat))
                    vRepeat = true;
            }
            if (hRepeat) {
                offsetWidth = adjustRepeatCoordinate(offsetWidth, imageWidth);
            }
            if (vRepeat) {
                offsetHeight = adjustRepeatCoordinate(offsetHeight, imageHeight);
            }
            if (!hRepeat && !vRepeat) {
                graphics.drawImage(image, offsetWidth, offsetHeight, null);
            } else if (hRepeat && vRepeat) {
                for (int x = offsetWidth; x < parentSize.width; x += imageWidth) {
                    for (int y = offsetHeight; y < parentSize.height; y += imageHeight) {
                        graphics.drawImage(image, x, y, null);
                    }
                }
            } else if (hRepeat) {
                for (int x = offsetWidth; x < parentSize.width; x += imageWidth) {
                    graphics.drawImage(image, x, offsetHeight, null);
                }
            } else if (vRepeat) {
                for (int y = offsetHeight; y < parentSize.height; y += imageHeight) {
                    graphics.drawImage(image, offsetWidth, y, null);
                }
            }
        }
    }
}
Also used : Media(com.servoy.j2db.persistence.Media) PropertyValue(org.xhtmlrenderer.css.parser.PropertyValue) CSSPrimitiveValue(org.w3c.dom.css.CSSPrimitiveValue) PropertyDeclaration(org.xhtmlrenderer.css.sheet.PropertyDeclaration) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint)

Example 22 with Media

use of com.servoy.j2db.persistence.Media in project servoy-client by Servoy.

the class WebBaseLabel method setRolloverImageURL.

public void setRolloverImageURL(String imageUrl) {
    this.rolloverUrl = imageUrl;
    rolloverIconReference = null;
    if (rolloverUrl != null) {
        int index = imageUrl.indexOf(MediaURLStreamHandler.MEDIA_URL_DEF);
        if (index != -1) {
            String nm = rolloverUrl.substring(index + MediaURLStreamHandler.MEDIA_URL_DEF.length());
            Media m = application.getFlattenedSolution().getMedia(nm);
            if (m != null) {
                setRolloverIcon(m.getID());
            }
        }
    }
    addRolloverBehaviors();
}
Also used : Media(com.servoy.j2db.persistence.Media) Point(java.awt.Point)

Example 23 with Media

use of com.servoy.j2db.persistence.Media in project servoy-client by Servoy.

the class WebBaseLabel method getThumbnailJPGImage.

public static byte[] getThumbnailJPGImage(int width, int height, MediaResource icon, String text_url, int iconId, boolean keepAspectRatio, IApplication application) {
    Image sourceImage = null;
    byte[] sourceRawData = null;
    if (icon != null) {
        sourceRawData = icon.getRawData();
    } else if (text_url != null) {
        int index = text_url.indexOf(MediaURLStreamHandler.MEDIA_URL_DEF);
        // If it is a media URL.
        if (index != -1) {
            String name = text_url.substring(index + MediaURLStreamHandler.MEDIA_URL_DEF.length());
            try {
                Media media = application.getFlattenedSolution().getMedia(name);
                if (media != null)
                    sourceRawData = media.getMediaData();
                else if (name.startsWith(MediaURLStreamHandler.MEDIA_URL_BLOBLOADER))
                    sourceRawData = MediaURLStreamHandler.getBlobLoaderMedia(application, text_url);
            } catch (Exception ex) {
                // $NON-NLS-1$
                Debug.error("Error loading media for URL: " + text_url, ex);
            }
        } else // If it is a regular, non-media, URL.
        {
            try {
                URL url = new URL(text_url);
                ImageIcon iicon = new ImageIcon(url);
                iicon = ImageLoader.resizeImageIcon(iicon, width, height, keepAspectRatio);
                sourceImage = iicon.getImage();
            } catch (Exception ex) {
                // $NON-NLS-1$
                Debug.error("Error loading icon from URL: " + text_url, ex);
            }
        }
    } else if (iconId > 0) {
        Media media = application.getFlattenedSolution().getMedia(iconId);
        if (media != null)
            sourceRawData = media.getMediaData();
    }
    if ((sourceImage == null) && (sourceRawData != null)) {
        // don't get the directly scaled buffered image, it is not precise.
        sourceImage = ImageLoader.getBufferedImage(sourceRawData, -1, -1, true);
        ImageIcon iicon = new ImageIcon(sourceImage);
        iicon = ImageLoader.resizeImageIcon(iicon, width, height, keepAspectRatio);
        sourceImage = iicon.getImage();
    }
    byte[] jpegedRawData = null;
    if (sourceImage != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JpegEncoder encoder = new JpegEncoder(sourceImage, 100, baos);
        encoder.compress();
        jpegedRawData = baos.toByteArray();
    }
    return jpegedRawData;
}
Also used : ImageIcon(javax.swing.ImageIcon) JpegEncoder(com.servoy.j2db.util.gui.JpegEncoder) Media(com.servoy.j2db.persistence.Media) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) IOException(java.io.IOException) URL(java.net.URL)

Example 24 with Media

use of com.servoy.j2db.persistence.Media in project servoy-client by Servoy.

the class WebBaseSubmitLink method setImageURL.

public void setImageURL(String textUrl) {
    this.text_url = textUrl;
    if (textUrl == null) {
        icon = null;
        iconReference = null;
        iconUrl = null;
    } else {
        int index = textUrl.indexOf(MediaURLStreamHandler.MEDIA_URL_DEF);
        if (index == -1) {
            icon = null;
            iconReference = null;
            iconUrl = textUrl;
            addEnabledStyleAttributeModifier();
        } else {
            String nm = textUrl.substring(index + MediaURLStreamHandler.MEDIA_URL_DEF.length());
            try {
                Media m = application.getFlattenedSolution().getMedia(nm);
                if (m != null) {
                    setMediaIcon(m.getID());
                } else if (nm.startsWith(MediaURLStreamHandler.MEDIA_URL_BLOBLOADER)) {
                    // clear previous images
                    icon = null;
                    iconReference = null;
                    iconUrl = null;
                    addEnabledStyleAttributeModifier();
                }
            } catch (Exception ex) {
                // $NON-NLS-1$
                Debug.error("Error loading media for url: " + textUrl, ex);
            }
        }
    }
}
Also used : Media(com.servoy.j2db.persistence.Media) Point(java.awt.Point) IOException(java.io.IOException)

Example 25 with Media

use of com.servoy.j2db.persistence.Media in project servoy-client by Servoy.

the class WebBaseSubmitLink method onResourceRequested.

/**
 * @see wicket.IResourceListener#onResourceRequested()
 */
public void onResourceRequested() {
    // $NON-NLS-1$
    String mediaName = RequestCycle.get().getRequest().getParameter("media");
    if (mediaName != null) {
        Media m;
        try {
            m = application.getFlattenedSolution().getMedia(mediaName);
            byte[] bytes = m.getMediaData();
            new ByteArrayResource(MimeTypes.getContentType(bytes), bytes, null).onResourceRequested();
        } catch (Exception ex) {
            // $NON-NLS-1$
            Debug.error("Error serving media: " + mediaName, ex);
        }
    } else if (getRequest().getParameter(StripHTMLTagsConverter.BLOB_LOADER_PARAM) != null) {
        String url = StripHTMLTagsConverter.getBlobLoaderUrlPart(getRequest());
        try {
            byte[] bytes = MediaURLStreamHandler.getBlobLoaderMedia(application, url);
            if (bytes != null) {
                String mime = MediaURLStreamHandler.getBlobLoaderMimeType(url);
                if (mime == null)
                    mime = MimeTypes.getContentType(bytes);
                String filename = MediaURLStreamHandler.getBlobLoaderFileName(url);
                if (size != null) {
                    MediaResource tempIcon = new MediaResource(bytes, mediaOptions);
                    (tempIcon).checkResize(size);
                    bytes = tempIcon.resized;
                }
                new ByteArrayResource(mime, bytes, filename).onResourceRequested();
            }
        } catch (IOException ex) {
            // $NON-NLS-1$
            Debug.error("Error serving blobloader url: " + url, ex);
        }
    } else {
        icon.onResourceRequested();
    }
}
Also used : Media(com.servoy.j2db.persistence.Media) ByteArrayResource(com.servoy.j2db.server.headlessclient.ByteArrayResource) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Media (com.servoy.j2db.persistence.Media)29 FlattenedSolution (com.servoy.j2db.FlattenedSolution)10 IOException (java.io.IOException)8 Point (java.awt.Point)7 Solution (com.servoy.j2db.persistence.Solution)4 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 FormController (com.servoy.j2db.FormController)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ByteArrayResource (com.servoy.j2db.server.headlessclient.ByteArrayResource)2 Image (java.awt.Image)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 URLConnection (java.net.URLConnection)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)1 IForm (com.servoy.j2db.IForm)1 IFormController (com.servoy.j2db.IFormController)1 IScriptExecuter (com.servoy.j2db.IScriptExecuter)1 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)1