use of com.codename1.rad.attributes.Badge in project CodeRAD by shannah.
the class Actions method addToContainer.
public void addToContainer(Container cnt, Entity entity, ComponentSelector.ComponentMapper wrapper) {
boolean requiresFlowLayoutWrapperForBadge = (cnt.getLayout() instanceof GridLayout || cnt.getLayout() instanceof BorderLayout || cnt.getLayout() instanceof BoxLayout);
for (ActionNode n : this) {
if (requiresFlowLayoutWrapperForBadge && n.getBadge() != null) {
// If there is a badge, we'll wrap it in a flowlayout
Container fl = FlowLayout.encloseCenter(n.createView(entity));
fl.getStyle().stripMarginAndPadding();
if (wrapper == null) {
cnt.addComponent(fl);
} else {
cnt.addComponent(wrapper.map(fl));
}
} else {
if (wrapper == null) {
cnt.addComponent(n.createView(entity));
} else {
cnt.addComponent(wrapper.map(n.createView(entity)));
}
}
}
}
use of com.codename1.rad.attributes.Badge in project CodeRAD by shannah.
the class DefaultActionViewFactory method update.
public static void update(MultiButton btn, Entity entity, ActionNode action) {
boolean repaint = false;
boolean revalidate = false;
Condition cond = action.getCondition();
if (cond != null) {
boolean hidden = !cond.getValue().test(entity);
if (hidden != btn.isHidden()) {
btn.setHidden(hidden);
btn.setVisible(!hidden);
revalidate = true;
}
}
EnabledCondition enabledCond = action.getEnabledCondition();
if (enabledCond != null) {
boolean enabled = enabledCond.getValue().test(entity);
if (enabled != btn.isEnabled()) {
btn.setEnabled(enabled);
repaint = true;
}
}
if (action.getLabel() != null) {
// String currTextVal = btn.getText();
String newTextVal = action.getLabelText(entity);
String currTextVal = btn.getTextLines();
if (!Objects.equals(currTextVal.trim(), newTextVal.trim())) {
btn.setTextLines(newTextVal.trim());
repaint = true;
}
}
if (!action.isTextStyle() && !"".equals(btn.getTextLines().trim())) {
btn.setTextLine1("");
btn.setTextLine2("");
btn.setTextLine3("");
btn.setTextLine4("");
repaint = true;
}
if (action.getUIID() != null) {
String currUiid = btn.getUIID();
String newUiid = action.getUIID(entity, "Button");
if (!Objects.equals(currUiid, newUiid)) {
btn.setUIID(newUiid);
repaint = true;
}
}
if (btn.isCheckBox()) {
SelectedCondition selectedCond = action.getSelectedCondition();
if (selectedCond != null) {
boolean selected = selectedCond.getValue().test(entity);
if (selected != btn.isSelected()) {
btn.setSelected(selected);
repaint = true;
ActionNode newState = selected ? action.getSelected() : action.getUnselected();
ActionNode oldState = selected ? action.getUnselected() : action.getSelected();
if (oldState != newState) {
String currText = btn.getText();
String newText = newState.getLabelText(entity);
if (!newState.isTextStyle()) {
newText = "";
}
if (!Objects.equals(newText, currText)) {
btn.setText(newText);
}
}
}
}
}
Badge badge = action.getBadge();
if (badge != null) {
btn.setBadgeText(badge.getValue(entity));
BadgeUIID badgeUiid = action.getBadgeUIID();
if (badgeUiid != null) {
btn.setBadgeUIID(badgeUiid.getValue());
}
}
if (revalidate || repaint) {
Form f = btn.getComponentForm();
if (f != null) {
if (revalidate) {
Component entityView = findEntityViewParent(btn);
if (entityView instanceof Container) {
((Container) entityView).revalidateLater();
} else {
entityView.repaint();
}
} else {
btn.repaint();
}
}
}
}
use of com.codename1.rad.attributes.Badge in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawComponent.
private void drawComponent(Graphics g, Label l, Image icon, Image stateIcon, int preserveSpaceForState) {
setFG(g, l);
int alpha = g.concatenateAlpha(l.getStyle().getFgAlpha());
int gap = l.getGap();
int stateIconSize = 0;
int stateIconYPosition = 0;
String text = l.getText();
Style style = l.getStyle();
int cmpX = l.getX();
int cmpY = l.getY();
int cmpHeight = l.getHeight();
int cmpWidth = l.getWidth();
boolean rtl = l.isRTL();
int leftPadding = style.getPaddingLeft(rtl);
int rightPadding = style.getPaddingRight(rtl);
int topPadding = style.getPaddingTop();
int bottomPadding = style.getPaddingBottom();
Font font = style.getFont();
int fontHeight = 0;
if (text == null) {
text = "";
}
if (text.length() > 0) {
fontHeight = font.getHeight();
}
int x = cmpX + leftPadding;
int y = cmpY + topPadding;
boolean opposite = false;
boolean stateButtonOnLeft = false;
if (stateIcon != null) {
// square image width == height
stateIconSize = stateIcon.getWidth();
preserveSpaceForState = stateIconSize + gap;
stateIconYPosition = cmpY + topPadding + (cmpHeight - topPadding - bottomPadding) / 2 - stateIconSize / 2;
int tX = cmpX;
if (((Button) l).isOppositeSide()) {
if (rtl) {
tX += leftPadding;
stateButtonOnLeft = true;
x = cmpX + leftPadding + preserveSpaceForState;
} else {
tX = tX + cmpWidth - leftPadding - stateIconSize;
}
// cmpWidth -= leftPadding - stateIconSize;
// preserveSpaceForState = 0;
opposite = true;
} else {
if (rtl) {
tX = tX + cmpWidth - leftPadding - stateIconSize;
} else {
x = cmpX + leftPadding + preserveSpaceForState;
tX += leftPadding;
stateButtonOnLeft = true;
}
}
g.drawImage(stateIcon, tX, stateIconYPosition);
}
// default for bottom left alignment
int align = reverseAlignForBidi(l, style.getAlignment());
int textPos = reverseAlignForBidi(l, l.getTextPosition());
// set initial x,y position according to the alignment and textPosition
if (align == Component.LEFT) {
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
if (textPos == Label.RIGHT && stateIcon == null) {
x += preserveSpaceForState;
}
break;
case Label.BOTTOM:
case Label.TOP:
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;
}
} else if (align == Component.CENTER) {
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + ((icon != null) ? icon.getWidth() + l.getGap() : 0) + l.getStringWidth(font))) / 2;
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + Math.max(((icon != null) ? icon.getWidth() + l.getGap() : 0), l.getStringWidth(font)))) / 2;
if (!opposite) {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
} else {
x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
}
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;
}
} else if (align == Component.RIGHT) {
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
x = cmpX + cmpWidth - rightPadding - (((icon != null) ? (icon.getWidth() + gap) : 0) + l.getStringWidth(font));
x = stateButtonOnLeft ? x : x - preserveSpaceForState;
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = cmpX + cmpWidth - rightPadding - (Math.max(((icon != null) ? (icon.getWidth()) : 0), l.getStringWidth(font)));
if (!opposite) {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
} else {
x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
}
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;
}
}
int textSpaceW = cmpWidth - rightPadding - leftPadding;
int textSpaceX = cmpX + leftPadding;
if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
textSpaceW = textSpaceW - icon.getWidth();
textSpaceX = (textPos == Label.RIGHT) ? textSpaceX + icon.getWidth() : textSpaceX;
}
textSpaceW = textSpaceW - preserveSpaceForState;
textSpaceX = stateButtonOnLeft ? textSpaceX + preserveSpaceForState : textSpaceX;
if (icon == null) {
// no icon only string
drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
} else {
int strWidth = l.getStringWidth(font);
int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();
int iconStringWGap;
int iconStringHGap;
switch(textPos) {
case Label.LEFT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
strWidth = drawLabelStringValign(g, l, text, x, y, iconStringHGap, iconHeight, textSpaceX, textSpaceW, fontHeight);
g.drawImage(icon, x + strWidth + gap, y);
} else {
strWidth = drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
drawLabelImageValign(g, l, icon, x + strWidth + gap, y, fontHeight, iconHeight);
}
break;
case Label.RIGHT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
g.drawImage(icon, x, y);
drawLabelStringValign(g, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceX, textSpaceW, fontHeight);
} else {
drawLabelImageValign(g, l, icon, x, y, fontHeight, iconHeight);
drawLabelString(g, l, text, x + iconWidth + gap, y, textSpaceX, textSpaceW);
}
break;
case Label.BOTTOM:
if (iconWidth > strWidth) {
// center align the smaller
iconStringWGap = (iconWidth - strWidth) / 2;
g.drawImage(icon, x, y);
drawLabelString(g, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceX, textSpaceW);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
g.drawImage(icon, x + iconStringWGap, y);
drawLabelString(g, l, text, x, y + iconHeight + gap, textSpaceX, textSpaceW);
}
break;
case Label.TOP:
if (iconWidth > strWidth) {
// center align the smaller
iconStringWGap = (iconWidth - strWidth) / 2;
drawLabelString(g, l, text, x + iconStringWGap, y, textSpaceX, textSpaceW);
g.drawImage(icon, x, y + fontHeight + gap);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
}
break;
}
}
String badgeText = l.getBadgeText();
if (badgeText != null && badgeText.length() > 0) {
Component badgeCmp = l.getBadgeStyleComponent();
int badgePaddingTop = CN.convertToPixels(1);
int badgePaddingBottom = badgePaddingTop;
int badgePaddingLeft = badgePaddingTop;
int badgePaddingRight = badgePaddingTop;
int fgColor = 0xffffff;
int bgColor = 0x666666;
int strokeColor = bgColor;
Font badgeFont = null;
Style badgeStyle;
if (badgeCmp == null) {
badgeStyle = l.getUIManager().getComponentStyle("Badge");
} else {
badgeStyle = badgeCmp.getStyle();
}
if (badgeStyle != null) {
fgColor = badgeStyle.getFgColor();
bgColor = badgeStyle.getBgColor();
if (badgeStyle.getBorder() instanceof RoundBorder) {
strokeColor = ((RoundBorder) badgeStyle.getBorder()).getStrokeColor();
} else {
strokeColor = bgColor;
}
badgeFont = badgeStyle.getFont();
badgePaddingTop = badgeStyle.getPaddingTop();
badgePaddingBottom = badgeStyle.getPaddingBottom();
badgePaddingLeft = badgeStyle.getPaddingLeftNoRTL();
badgePaddingRight = badgeStyle.getPaddingRightNoRTL();
}
if (badgeFont == null) {
if (Font.isNativeFontSchemeSupported()) {
badgeFont = Font.createTrueTypeFont(Font.NATIVE_MAIN_LIGHT).derive(fontHeight / 2, 0);
} else {
badgeFont = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
}
}
int badgeFontHeight = badgeFont.getHeight();
int badgeTextWidth = badgeFont.stringWidth(badgeText);
GeneralPath path = new GeneralPath();
Rectangle rect = new Rectangle(cmpX + cmpWidth - badgeTextWidth - badgePaddingLeft - badgePaddingRight, cmpY, badgePaddingLeft + badgePaddingRight + badgeTextWidth, badgePaddingTop + badgePaddingBottom + badgeFontHeight);
if (rect.getWidth() < rect.getHeight()) {
rect.setX(cmpX + cmpWidth - rect.getHeight());
rect.setWidth(rect.getHeight());
}
path.moveTo(rect.getX() + rect.getHeight() / 2, rect.getY());
path.lineTo(rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY());
path.arcTo(rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY() + rect.getHeight() / 2, rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY() + rect.getHeight(), true);
path.lineTo(rect.getX() + rect.getHeight() / 2, rect.getY() + rect.getHeight());
path.arcTo(rect.getX() + rect.getHeight() / 2, rect.getY() + rect.getHeight() / 2, rect.getX() + rect.getHeight() / 2, rect.getY(), true);
path.closePath();
int col = g.getColor();
boolean antialias = g.isAntiAliased();
g.setAntiAliased(true);
g.setColor(bgColor);
g.fillShape(path);
if (bgColor != strokeColor) {
g.setColor(strokeColor);
int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
g.drawShape(path, new Stroke(1, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1f));
g.setAlpha(alpha2);
}
g.setColor(fgColor);
g.setFont(badgeFont);
int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
g.drawString(badgeText, rect.getX() + rect.getWidth() / 2 - badgeTextWidth / 2, rect.getY() + badgePaddingTop);
g.setAlpha(alpha2);
g.setColor(col);
g.setAntiAliased(antialias);
}
g.setAlpha(alpha);
}
use of com.codename1.rad.attributes.Badge in project CodenameOne by codenameone.
the class PushBuilder method build.
/**
* Builds the payload for this rich push notification.
* @return The payload that can be passed as the message body to {@link Push}
*/
public String build() {
StringBuilder sb = new StringBuilder();
switch(type) {
case 0:
case 1:
sb.append(body);
break;
case 2:
sb.append(metaData);
break;
case 3:
sb.append(metaData).append(";").append(body);
break;
case 4:
sb.append(title).append(";").append(body);
break;
case 5:
sb.append(body);
break;
case 6:
case 100:
sb.append(badge);
break;
case 101:
sb.append(badge).append(" ").append(body);
break;
case 102:
sb.append(badge).append(";").append(title).append(";").append(body);
break;
}
if (isRichPush()) {
String b = sb.toString();
sb.setLength(0);
Element el = new Element("push");
el.setAttribute("type", "" + type);
el.setAttribute("body", b);
if (category != null) {
el.setAttribute("category", category);
}
if (imageUrl != null) {
Element imgEl = new Element("img");
imgEl.setAttribute("src", imageUrl);
el.addChild(imgEl);
}
return el.toString();
}
return sb.toString();
}
use of com.codename1.rad.attributes.Badge in project CodeRAD by shannah.
the class DefaultActionViewFactory method update.
public static void update(Button btn, Entity entity, ActionNode action) {
boolean repaint = false;
boolean revalidate = false;
Condition cond = action.getCondition();
if (cond != null) {
boolean hidden = !cond.getValue().test(entity);
if (hidden != btn.isHidden()) {
btn.setHidden(hidden);
btn.setVisible(!hidden);
revalidate = true;
}
}
EnabledCondition enabledCond = action.getEnabledCondition();
if (enabledCond != null) {
boolean enabled = enabledCond.getValue().test(entity);
if (enabled != btn.isEnabled()) {
btn.setEnabled(enabled);
repaint = true;
}
}
if (action.getLabel() != null) {
String currTextVal = btn.getText();
String newTextVal = action.getLabelText(entity);
if (!Objects.equals(currTextVal, newTextVal)) {
btn.setText(newTextVal);
repaint = true;
}
}
if (!action.isTextStyle() && !"".equals(btn.getText().trim())) {
btn.setText("");
repaint = true;
}
if (action.getUIID() != null) {
String currUiid = btn.getUIID();
String newUiid = action.getUIID(entity, "Button");
if (!Objects.equals(currUiid, newUiid)) {
btn.setUIID(newUiid);
repaint = true;
}
}
if (btn instanceof CheckBox) {
SelectedCondition selectedCond = action.getSelectedCondition();
if (selectedCond != null) {
boolean selected = selectedCond.getValue().test(entity);
if (selected != btn.isSelected()) {
((CheckBox) btn).setSelected(selected);
repaint = true;
ActionNode newState = selected ? action.getSelected() : action.getUnselected();
ActionNode oldState = selected ? action.getUnselected() : action.getSelected();
if (oldState != newState) {
String currText = btn.getText();
String newText = newState.getLabelText(entity);
if (!newState.isTextStyle()) {
newText = "";
}
if (!Objects.equals(newText, currText)) {
btn.setText(newText);
}
}
}
}
}
Badge badge = action.getBadge();
if (badge != null) {
btn.setBadgeText(badge.getValue(entity));
BadgeUIID badgeUiid = action.getBadgeUIID();
if (badgeUiid != null) {
btn.setBadgeUIID(badgeUiid.getValue());
}
}
if (revalidate || repaint) {
Form f = btn.getComponentForm();
if (f != null) {
if (revalidate) {
Component entityView = findEntityViewParent(btn);
if (entityView instanceof Container) {
((Container) entityView).revalidateLater();
} else {
entityView.repaint();
}
} else {
btn.repaint();
}
}
}
}
Aggregations