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);
}
}
}
}
}
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();
}
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;
}
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);
}
}
}
}
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();
}
}
Aggregations