use of com.codename1.charts.compat.Paint.Style in project CodenameOne by codenameone.
the class ImageDownloadService method createImageToFileSystem.
/**
* Constructs an image request that will automatically populate the given list
* when the response arrives, it will cache the file locally as a file
* in the file storage.
* This assumes the GenericListCellRenderer style of
* list which relies on a map based model approach.
*
* @param url the image URL
* @param targetList the list that should be updated when the data arrives
* @param targetOffset the offset within the list to insert the image
* @param targetKey the key for the map in the target offset
* @param destFile local file to store the data into the given path
*/
private static void createImageToFileSystem(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String destFile, final Dimension toScale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
if (Display.getInstance().isEdt()) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
createImageToFileSystem(url, targetList, targetModel, targetOffset, targetKey, destFile, toScale, priority, placeholderImage, maintainAspectRatio);
}
});
return;
}
// image not found on cache go and download from the url
ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
i.targetModel = targetModel;
i.maintainAspectRatio = maintainAspectRatio;
Image im = cacheImage(null, false, destFile, toScale, placeholderImage, maintainAspectRatio);
if (im != null) {
i.setEntryInListModel(targetOffset, im);
targetList.repaint();
return;
}
i.cacheImages = true;
i.destinationFile = destFile;
i.toScale = toScale;
i.placeholder = placeholderImage;
i.setPriority(priority);
i.setFailSilently(true);
NetworkManager.getInstance().addToQueue(i);
}
use of com.codename1.charts.compat.Paint.Style in project CodenameOne by codenameone.
the class AndroidGraphics method drawLabelComponent.
public void drawLabelComponent(int cmpX, int cmpY, int cmpHeight, int cmpWidth, Style style, String text, Bitmap icon, Bitmap stateIcon, int preserveSpaceForState, int gap, boolean rtl, boolean isOppositeSide, int textPosition, int stringWidth, boolean isTickerRunning, int tickerShiftText, boolean endsWith3Points, int valign) {
int clipX = getClipX();
int clipY = getClipY();
int clipW = getClipWidth();
int clipH = getClipHeight();
Font cn1Font = style.getFont();
Object nativeFont = cn1Font.getNativeFont();
impl.setNativeFont(this, nativeFont);
setColor(style.getFgColor());
canvas.save();
applyTransform();
int iconWidth = 0;
int iconHeight = 0;
if (icon != null) {
iconWidth = icon.getWidth();
iconHeight = icon.getHeight();
}
int textDecoration = style.getTextDecoration();
int stateIconSize = 0;
int stateIconYPosition = 0;
int leftPadding = style.getPaddingLeft(rtl);
int rightPadding = style.getPaddingRight(rtl);
int topPadding = style.getPaddingTop();
int bottomPadding = style.getPaddingBottom();
int fontHeight = 0;
if (text == null) {
text = "";
}
if (text.length() > 0) {
fontHeight = cn1Font.getHeight();
}
if (stateIcon != null) {
stateIconSize = stateIcon.getWidth();
stateIconYPosition = cmpY + topPadding + (cmpHeight - topPadding - bottomPadding) / 2 - stateIconSize / 2;
int tX = cmpX;
if (isOppositeSide) {
if (rtl) {
tX += leftPadding;
} else {
tX = tX + cmpWidth - leftPadding - stateIconSize;
}
cmpWidth -= leftPadding - stateIconSize;
} else {
preserveSpaceForState = stateIconSize + gap;
if (rtl) {
tX = tX + cmpWidth - leftPadding - stateIconSize;
} else {
tX += leftPadding;
}
}
drawImage(stateIcon, tX, stateIconYPosition);
}
// default for bottom left alignment
int x = cmpX + leftPadding + preserveSpaceForState;
int y = cmpY + topPadding;
int align = reverseAlignForBidi(rtl, style.getAlignment());
int textPos = reverseAlignForBidi(rtl, textPosition);
// set initial x,y position according to the alignment and textPosition
switch(align) {
case Component.LEFT:
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? iconHeight : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? iconHeight + gap : 0) + fontHeight)) / 2;
break;
}
break;
case Component.CENTER:
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + ((icon != null) ? iconWidth + gap : 0) + stringWidth)) / 2;
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? iconHeight : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + Math.max(((icon != null) ? iconWidth + gap : 0), stringWidth))) / 2;
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? iconHeight + gap : 0) + fontHeight)) / 2;
break;
}
break;
case Component.RIGHT:
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
x = cmpX + cmpWidth - rightPadding - (((icon != null) ? (iconWidth + gap) : 0) + stringWidth);
if (rtl) {
x = Math.max(x - preserveSpaceForState, cmpX + leftPadding);
} else {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
}
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? iconHeight : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = cmpX + cmpWidth - rightPadding - (Math.max(((icon != null) ? (iconWidth) : 0), stringWidth));
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? iconHeight + gap : 0) + fontHeight)) / 2;
break;
}
break;
default:
break;
}
int textSpaceW = cmpWidth - rightPadding - leftPadding;
if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
textSpaceW = textSpaceW - iconWidth;
}
if (stateIcon != null) {
textSpaceW = textSpaceW - stateIconSize;
} else {
textSpaceW = textSpaceW - preserveSpaceForState;
}
if (icon == null) {
// no icon only string
drawLabelString(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, stringWidth, fontHeight);
} else {
int strWidth = stringWidth;
int iconStringWGap;
int iconStringHGap;
switch(textPos) {
case Label.LEFT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
strWidth = drawLabelStringValign(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, strWidth, iconStringHGap, iconHeight, fontHeight, valign);
drawImage(icon, x + strWidth + gap, y);
} else {
iconStringHGap = (fontHeight - iconHeight) / 2;
strWidth = drawLabelString(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, strWidth, fontHeight);
drawImage(icon, x + strWidth + gap, y + iconStringHGap);
}
break;
case Label.RIGHT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
drawImage(icon, x, y);
drawLabelStringValign(nativeFont, text, x + iconWidth + gap, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, iconStringHGap, iconHeight, fontHeight, valign);
} else {
iconStringHGap = (fontHeight - iconHeight) / 2;
drawImage(icon, x, y + iconStringHGap);
drawLabelString(nativeFont, text, x + iconWidth + gap, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
}
break;
case Label.BOTTOM:
// center align the smaller
if (iconWidth > strWidth) {
iconStringWGap = (iconWidth - strWidth) / 2;
drawImage(icon, x, y);
drawLabelString(nativeFont, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
drawImage(icon, x + iconStringWGap, y);
drawLabelString(nativeFont, text, x, y + iconHeight + gap, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
}
break;
case Label.TOP:
// center align the smaller
if (iconWidth > strWidth) {
iconStringWGap = (iconWidth - strWidth) / 2;
drawLabelString(nativeFont, text, x + iconStringWGap, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
drawImage(icon, x, y + fontHeight + gap);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
drawLabelString(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
}
break;
}
}
unapplyTransform();
canvas.restore();
setClip(clipX, clipY, clipW, clipH);
}
use of com.codename1.charts.compat.Paint.Style in project CodenameOne by codenameone.
the class ImageViewer method paint.
/**
* {@inheritDoc}
*/
@Override
public void paint(Graphics g) {
if (panPositionX < 0) {
Style s = getStyle();
int width = getWidth() - s.getHorizontalPadding();
float ratio = ((float) width) * (panPositionX * -1);
g.drawImage(image, ((int) ratio) + getX() + imageX, getY() + imageY, imageDrawWidth, imageDrawHeight);
if (cycleLeft || swipeableImages.getSelectedIndex() > getImageLeftPos()) {
Image left = getImageLeft();
if (swipePlaceholder != null) {
left.asyncLock(swipePlaceholder);
} else {
left.lock();
}
ratio = ratio - width;
imageAspectCalc(left);
g.drawImage(left, ((int) ratio) + getX() + prefX, getY() + prefY, prefW, prefH);
}
return;
}
if (panPositionX > 1) {
Style s = getStyle();
int width = getWidth() - s.getHorizontalPadding();
float ratio = ((float) width) * (1 - panPositionX);
g.drawImage(image, ((int) ratio) + getX() + imageX, getY() + imageY, imageDrawWidth, imageDrawHeight);
if (cycleRight || swipeableImages.getSelectedIndex() < getImageRightPos()) {
Image right = getImageRight();
if (swipePlaceholder != null) {
right.asyncLock(swipePlaceholder);
} else {
right.lock();
}
ratio = ratio + width;
imageAspectCalc(right);
g.drawImage(right, ((int) ratio) + getX() + prefX, getY() + prefY, prefW, prefH);
}
return;
}
// can happen in the GUI builder
if (image != null) {
g.drawImage(image, getX() + imageX, getY() + imageY, imageDrawWidth, imageDrawHeight);
}
}
use of com.codename1.charts.compat.Paint.Style in project CodenameOne by codenameone.
the class InfiniteProgress method paint.
/**
* {@inheritDoc}
*/
public void paint(Graphics g) {
if (this.getComponentForm() != null && Display.getInstance().getCurrent() != this.getComponentForm()) {
return;
}
super.paint(g);
if (animation == null) {
return;
}
int v = angle % 360;
Style s = getStyle();
/*if(g.isAffineSupported()) {
g.rotate(((float)v) / 57.2957795f, getAbsoluteX() + s.getPadding(LEFT) + getWidth() / 2, getAbsoluteY() + s.getPadding(TOP) + getHeight() / 2);
g.drawImage(getAnimation(), getX() + s.getPadding(LEFT), getY() + s.getPadding(TOP));
g.resetAffine();
} else {*/
Image rotated;
if (animation instanceof FontImage) {
rotated = animation.rotate(v);
} else {
Integer angle = new Integer(v);
rotated = cache.get(angle);
if (rotated == null) {
rotated = animation.rotate(v);
cache.put(v, rotated);
}
}
g.drawImage(rotated, getX() + s.getPaddingLeftNoRTL(), getY() + s.getPaddingTop());
// }
}
use of com.codename1.charts.compat.Paint.Style in project CodenameOne by codenameone.
the class InteractionDialog method showPopupDialog.
/**
* A popup dialog is shown with the context of a component and its selection, it is disposed seamlessly if the back button is pressed
* or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
* dialog has the PopupDialog style by default.
*
* @param rect the screen rectangle to which the popup should point
*/
public void showPopupDialog(Rectangle rect) {
disposed = false;
if (getUIID().equals("Dialog")) {
setUIID("PopupDialog");
if (getTitleComponent().getUIID().equals("DialogTitle")) {
getTitleComponent().setUIID("PopupDialogTitle");
}
getContentPane().setUIID("PopupContentPane");
}
Component contentPane = getContentPane();
Label title = getTitleComponent();
UIManager manager = getUIManager();
String dialogTitle = title.getText();
// preferred size logic of the dialog won't work with large title borders
if ((dialogTitle != null || dialogTitle.length() == 0) && manager.isThemeConstant("hideEmptyTitleBool", false)) {
boolean b = getTitle().length() > 0;
titleArea.setVisible(b);
getTitleComponent().setVisible(b);
if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
getTitleComponent().setPreferredSize(new Dimension(0, 0));
getTitleComponent().getStyle().setBorder(null);
titleArea.setPreferredSize(new Dimension(0, 0));
if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
int titlePT = getTitleComponent().getStyle().getPaddingTop();
byte[] pu = getContentPane().getStyle().getPaddingUnit();
if (pu == null) {
pu = new byte[4];
}
pu[0] = Style.UNIT_TYPE_PIXELS;
getContentPane().getStyle().setPaddingUnit(pu);
int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
}
}
}
// allows a text area to recalculate its preferred size if embedded within a dialog
revalidate();
Style contentPaneStyle = getStyle();
boolean restoreArrow = false;
if (manager.isThemeConstant(getUIID() + "ArrowBool", false)) {
Image t = manager.getThemeImageConstant(getUIID() + "ArrowTopImage");
Image b = manager.getThemeImageConstant(getUIID() + "ArrowBottomImage");
Image l = manager.getThemeImageConstant(getUIID() + "ArrowLeftImage");
Image r = manager.getThemeImageConstant(getUIID() + "ArrowRightImage");
Border border = contentPaneStyle.getBorder();
if (border != null) {
border.setImageBorderSpecialTile(t, b, l, r, rect);
restoreArrow = true;
}
}
calcPreferredSize();
int prefHeight = getPreferredH();
int prefWidth = getPreferredW();
if (contentPaneStyle.getBorder() != null) {
prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
}
Form f = Display.getInstance().getCurrent();
int availableHeight = getLayeredPane(f).getParent().getHeight();
int availableWidth = getLayeredPane(f).getParent().getWidth();
int width = Math.min(availableWidth, prefWidth);
int x = 0;
int y = 0;
boolean showPortrait = Display.getInstance().isPortrait();
// if we don't have enough space then disregard device orientation
if (showPortrait) {
if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
showPortrait = false;
}
} else {
if (availableHeight / 2 > availableWidth - rect.getWidth()) {
showPortrait = true;
}
}
if (showPortrait) {
if (width < availableWidth) {
int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
// if the ideal position is less than 0 just use 0
if (idealX > 0) {
// if the idealX is too far to the right just align to the right
if (idealX + width > availableWidth) {
x = availableWidth - width;
} else {
x = idealX;
}
}
}
if (rect.getY() < availableHeight / 2) {
// popup downwards
y = rect.getY();
int height = Math.min(prefHeight, availableHeight - y);
show(y, Math.max(0, availableHeight - height - y), x, Math.max(0, availableWidth - width - x));
} else {
// popup upwards
int height = Math.min(prefHeight, rect.getY() - getLayeredPane(f).getAbsoluteY());
y = rect.getY() - height - getLayeredPane(f).getAbsoluteY();
show(y, Math.max(0, getLayeredPane(f).getComponentForm().getHeight() - rect.getY()), x, Math.max(0, availableWidth - width - x));
}
} else {
int height = Math.min(prefHeight, availableHeight);
if (height < availableHeight) {
int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
// if the ideal position is less than 0 just use 0
if (idealY > 0) {
// if the idealY is too far up just align to the top
if (idealY + height > availableHeight) {
y = availableHeight - height;
} else {
y = idealY;
}
}
}
if (prefWidth > rect.getX()) {
// popup right
x = rect.getX() + rect.getSize().getWidth();
if (x + prefWidth > availableWidth) {
x = availableWidth - prefWidth;
}
width = Math.min(prefWidth, availableWidth - x);
show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
} else {
// popup left
width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
x = rect.getX() - width;
show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
}
}
/*if(restoreArrow) {
contentPaneStyle.getBorder().clearImageBorderSpecialTile();
}*/
}
Aggregations