]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ux/src/org/argeo/cms/ux/AbstractImageManager.java
1 package org
.argeo
.cms
.ux
;
3 import org
.argeo
.api
.cms
.ux
.Cms2DSize
;
4 import org
.argeo
.api
.cms
.ux
.CmsImageManager
;
6 /** Manages only public images so far. */
7 public abstract class AbstractImageManager
<V
, M
> implements CmsImageManager
<V
, M
> {
8 public final static String NO_IMAGE
= "icons/noPic-square-640px.png";
9 public final static Cms2DSize NO_IMAGE_SIZE
= new Cms2DSize(320, 320);
10 public final static Float NO_IMAGE_RATIO
= 1f
;
12 protected Cms2DSize
resizeTo(Cms2DSize orig
, Cms2DSize constraints
) {
13 if (constraints
.getWidth() != 0 && constraints
.getHeight() != 0) {
15 } else if (constraints
.getWidth() == 0 && constraints
.getHeight() == 0) {
17 } else if (constraints
.getHeight() == 0) {// force width
18 return new Cms2DSize(constraints
.getWidth(),
19 scale(orig
.getHeight(), orig
.getWidth(), constraints
.getWidth()));
20 } else if (constraints
.getWidth() == 0) {// force height
21 return new Cms2DSize(scale(orig
.getWidth(), orig
.getHeight(), constraints
.getHeight()),
22 constraints
.getHeight());
24 throw new IllegalArgumentException("Cannot resize " + orig
+ " to " + constraints
);
27 protected int scale(int origDimension
, int otherDimension
, int otherConstraint
) {
28 return Math
.round(origDimension
* divide(otherConstraint
, otherDimension
));
31 protected float divide(int a
, int b
) {
32 return ((float) a
) / ((float) b
);
35 /** @return null if not available */
37 public String
getImageTag(M node
) {
38 return getImageTag(node
, getImageSize(node
));
41 protected String
getImageTag(M node
, Cms2DSize size
) {
42 StringBuilder buf
= getImageTagBuilder(node
, size
);
45 return buf
.append("/>").toString();
48 /** @return null if not available */
50 public StringBuilder
getImageTagBuilder(M node
, Cms2DSize size
) {
51 return getImageTagBuilder(node
, Integer
.toString(size
.getWidth()), Integer
.toString(size
.getHeight()));
54 /** @return null if not available */
55 protected StringBuilder
getImageTagBuilder(M node
, String width
, String height
) {
56 String url
= getImageUrl(node
);
59 return CmsUxUtils
.imgBuilder(url
, width
, height
);