]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ux/src/org/argeo/cms/ux/AbstractImageManager.java
Move JNI from Argeo Commons to Argeo SLC
[lgpl/argeo-commons.git] / org.argeo.cms.ux / src / org / argeo / cms / ux / AbstractImageManager.java
1 package org.argeo.cms.ux;
2
3 import org.argeo.api.cms.ux.Cms2DSize;
4 import org.argeo.api.cms.ux.CmsImageManager;
5
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;
11
12 protected Cms2DSize resizeTo(Cms2DSize orig, Cms2DSize constraints) {
13 if (constraints.getWidth() != 0 && constraints.getHeight() != 0) {
14 return constraints;
15 } else if (constraints.getWidth() == 0 && constraints.getHeight() == 0) {
16 return orig;
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());
23 }
24 throw new IllegalArgumentException("Cannot resize " + orig + " to " + constraints);
25 }
26
27 protected int scale(int origDimension, int otherDimension, int otherConstraint) {
28 return Math.round(origDimension * divide(otherConstraint, otherDimension));
29 }
30
31 protected float divide(int a, int b) {
32 return ((float) a) / ((float) b);
33 }
34
35 /** @return null if not available */
36 @Override
37 public String getImageTag(M node) {
38 return getImageTag(node, getImageSize(node));
39 }
40
41 protected String getImageTag(M node, Cms2DSize size) {
42 StringBuilder buf = getImageTagBuilder(node, size);
43 if (buf == null)
44 return null;
45 return buf.append("/>").toString();
46 }
47
48 /** @return null if not available */
49 @Override
50 public StringBuilder getImageTagBuilder(M node, Cms2DSize size) {
51 return getImageTagBuilder(node, Integer.toString(size.getWidth()), Integer.toString(size.getHeight()));
52 }
53
54 /** @return null if not available */
55 protected StringBuilder getImageTagBuilder(M node, String width, String height) {
56 String url = getImageUrl(node);
57 if (url == null)
58 return null;
59 return CmsUxUtils.imgBuilder(url, width, height);
60 }
61
62 }