use of com.codename1.ui.Label in project CodenameOne by codenameone.
the class XYChart method drawXLabels.
/**
* The graphical representation of the labels on the X axis.
*
* @param xLabels the X labels values
* @param xTextLabelLocations the X text label locations
* @param canvas the canvas to paint to
* @param paint the paint to be used for drawing
* @param left the left value of the labels area
* @param top the top value of the labels area
* @param bottom the bottom value of the labels area
* @param xPixelsPerUnit the amount of pixels per one unit in the chart labels
* @param minX the minimum value on the X axis in the chart
* @param maxX the maximum value on the X axis in the chart
*/
protected void drawXLabels(List<Double> xLabels, Double[] xTextLabelLocations, Canvas canvas, Paint paint, int left, int top, int bottom, double xPixelsPerUnit, double minX, double maxX) {
int length = xLabels.size();
boolean showLabels = mRenderer.isShowLabels();
boolean showGridY = mRenderer.isShowGridY();
boolean showTickMarks = mRenderer.isShowTickMarks();
for (int i = 0; i < length; i++) {
double label = xLabels.get(i);
float xLabel = (float) (left + xPixelsPerUnit * (label - minX));
if (showLabels) {
paint.setColor(mRenderer.getXLabelsColor());
if (showTickMarks) {
canvas.drawLine(xLabel, bottom, xLabel, bottom + mRenderer.getLabelsTextSize() / 3, paint);
}
drawText(canvas, getLabel(mRenderer.getXLabelFormat(), label), xLabel, bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint, mRenderer.getXLabelsAngle());
}
if (showGridY) {
paint.setColor(mRenderer.getGridColor(0));
canvas.drawLine(xLabel, bottom, xLabel, top, paint);
}
}
drawXTextLabels(xTextLabelLocations, canvas, paint, showLabels, left, top, bottom, xPixelsPerUnit, minX, maxX);
}
use of com.codename1.ui.Label in project CodenameOne by codenameone.
the class ToastBar method updateStatus.
/**
* Updates the ToastBar UI component with the settings of the current status.
*/
private void updateStatus() {
final ToastBarComponent c = getToastBarComponent();
if (c != null) {
try {
if (updatingStatus) {
pendingUpdateStatus = true;
return;
}
updatingStatus = true;
if (c.currentlyShowing != null && !statuses.contains(c.currentlyShowing)) {
c.currentlyShowing = null;
}
if (c.currentlyShowing == null || statuses.isEmpty()) {
if (!statuses.isEmpty()) {
c.currentlyShowing = statuses.get(statuses.size() - 1);
} else {
setVisible(false);
return;
}
}
Status s = c.currentlyShowing;
Label l = new Label(s.getMessage() != null ? s.getMessage() : "");
c.leadButton.getListeners().clear();
c.leadButton.addActionListener(s.getListener());
c.leadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
if (c.currentlyShowing != null && !c.currentlyShowing.showProgressIndicator) {
c.currentlyShowing.clear();
}
ToastBar.this.setVisible(false);
}
});
c.progressLabel.setVisible(s.isShowProgressIndicator());
if (c.progressLabel.isVisible()) {
if (!c.contains(c.progressLabel)) {
c.addComponent(BorderLayout.EAST, c.progressLabel);
}
Image anim = c.progressLabel.getAnimation();
if (anim != null && anim.getWidth() > 0) {
c.progressLabel.setWidth(anim.getWidth());
}
if (anim != null && anim.getHeight() > 0) {
c.progressLabel.setHeight(anim.getHeight());
}
} else {
if (c.contains(c.progressLabel)) {
c.removeComponent(c.progressLabel);
}
}
c.progressBar.setVisible(s.getProgress() >= -1);
if (s.getProgress() >= -1) {
if (!c.contains(c.progressBar)) {
c.addComponent(BorderLayout.SOUTH, c.progressBar);
}
if (s.getProgress() < 0) {
c.progressBar.setInfinite(true);
} else {
c.progressBar.setInfinite(false);
c.progressBar.setProgress(s.getProgress());
}
} else {
c.removeComponent(c.progressBar);
}
c.icon.setVisible(s.getIcon() != null);
if (s.getIcon() != null && c.icon.getIcon() != s.getIcon()) {
c.icon.setIcon(s.getIcon());
}
if (s.getIcon() == null && c.contains(c.icon)) {
c.removeComponent(c.icon);
} else if (s.getIcon() != null && !c.contains(c.icon)) {
c.addComponent(BorderLayout.WEST, c.icon);
}
String oldText = c.label.getText();
if (!oldText.equals(l.getText())) {
if (s.getUiid() != null) {
c.setUIID(s.getUiid());
} else if (defaultUIID != null) {
c.setUIID(defaultUIID);
}
if (c.isVisible()) {
TextArea newLabel = new TextArea();
// newLabel.setColumns(l.getText().length()+1);
// newLabel.setRows(l.getText().length()+1);
newLabel.setFocusable(false);
newLabel.setEditable(false);
// newLabel.getAllStyles().setFgColor(0xffffff);
if (s.getMessageUIID() != null) {
newLabel.setUIID(s.getMessageUIID());
} else if (defaultMessageUIID != null) {
newLabel.setUIID(defaultMessageUIID);
} else {
newLabel.setUIID(c.label.getUIID());
}
if (s.getUiid() != null) {
c.setUIID(s.getUiid());
} else if (defaultUIID != null) {
c.setUIID(defaultUIID);
}
newLabel.setWidth(c.label.getWidth());
newLabel.setText(l.getText());
Dimension oldTextAreaSize = UIManager.getInstance().getLookAndFeel().getTextAreaSize(c.label, true);
Dimension newTexAreaSize = UIManager.getInstance().getLookAndFeel().getTextAreaSize(newLabel, true);
// https://stackoverflow.com/questions/46172993/codename-one-toastbar-nullpointerexception
if (c.label.getParent() != null) {
c.label.getParent().replaceAndWait(c.label, newLabel, CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, true, 300));
c.label = newLabel;
if (oldTextAreaSize.getHeight() != newTexAreaSize.getHeight()) {
c.label.setPreferredH(newTexAreaSize.getHeight());
c.getParent().animateHierarchyAndWait(300);
}
}
} else {
if (s.getMessageUIID() != null) {
c.label.setUIID(s.getMessageUIID());
} else if (defaultMessageUIID != null) {
c.label.setUIID(defaultMessageUIID);
}
if (s.getUiid() != null) {
c.setUIID(s.getUiid());
} else if (defaultUIID != null) {
c.setUIID(defaultUIID);
}
c.label.setText(l.getText());
// c.label.setColumns(l.getText().length()+1);
// c.label.setRows(l.getText().length()+1);
c.label.setPreferredW(c.getWidth());
c.revalidate();
}
} else {
c.revalidate();
}
} finally {
updatingStatus = false;
if (pendingUpdateStatus) {
pendingUpdateStatus = false;
Display.getInstance().callSerially(new Runnable() {
public void run() {
updateStatus();
}
});
}
}
}
}
use of com.codename1.ui.Label in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawLabelString.
/**
* Implements the drawString for the text component and adjust the valign
* assuming the icon is in one of the sides
*/
private int drawLabelString(Graphics g, Label l, String text, int x, int y, int textSpaceW) {
Style style = l.getStyle();
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();
// g.pushClip();
g.clipRect(x, cy, textSpaceW, ch);
if (l.isTickerRunning()) {
Font font = style.getFont();
if (l.getShiftText() > 0) {
if (l.getShiftText() > textSpaceW) {
l.setShiftText(x - l.getX() - l.getStringWidth(font));
}
} else if (l.getShiftText() + l.getStringWidth(font) < 0) {
l.setShiftText(textSpaceW);
}
}
int drawnW = drawLabelText(g, l, text, x, y, textSpaceW);
g.setClip(cx, cy, cw, ch);
return drawnW;
}
use of com.codename1.ui.Label in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawPullToRefresh.
/**
* {@inheritDoc}
*/
public void drawPullToRefresh(Graphics g, final Component cmp, boolean taskExecuted) {
final Form parentForm = cmp.getComponentForm();
final int scrollY = cmp.getScrollY();
Component cmpToDraw;
if (taskExecuted) {
cmpToDraw = updating;
} else {
if (-scrollY > getPullToRefreshHeight()) {
cmpToDraw = releaseToRefresh;
} else {
cmpToDraw = pullDown;
}
}
if (pull.getComponentAt(0) != updating && cmpToDraw != pull.getComponentAt(0)) {
parentForm.registerAnimated(new Animation() {
int counter = 0;
Image i;
{
i = UIManager.getInstance().getThemeImageConstant("pullToRefreshImage");
if (i == null) {
i = getDefaultRefreshIcon();
}
}
public boolean animate() {
counter++;
if (pull.getComponentAt(0) == releaseToRefresh) {
((Label) releaseToRefresh).setIcon(i.rotate(180 - (180 / 6) * counter));
} else {
((Label) pullDown).setIcon(i.rotate(180 * counter / 6));
}
if (counter == 6) {
((Label) releaseToRefresh).setIcon(i);
((Label) pullDown).setIcon(i.rotate(180));
parentForm.deregisterAnimated(this);
}
cmp.repaint(cmp.getAbsoluteX(), cmp.getAbsoluteY() - getPullToRefreshHeight(), cmp.getWidth(), getPullToRefreshHeight());
return false;
}
public void paint(Graphics g) {
}
});
}
if (pull.getComponentAt(0) != cmpToDraw && cmpToDraw instanceof Label && (pull.getComponentAt(0) instanceof Label)) {
((Label) cmpToDraw).setIcon(((Label) pull.getComponentAt(0)).getIcon());
}
Component current = pull.getComponentAt(0);
if (current != cmpToDraw) {
pull.replace(current, cmpToDraw, null);
}
pull.setWidth(cmp.getWidth());
pull.setX(cmp.getAbsoluteX());
pull.setY(cmp.getY() - scrollY - getPullToRefreshHeight());
pull.layoutContainer();
pull.paintComponent(g);
}
use of com.codename1.ui.Label in project CodenameOne by codenameone.
the class DefaultLookAndFeel method getPreferredSize.
/**
* {@inheritDoc}
*/
private Dimension getPreferredSize(Label l, Image[] icons, Image stateImage) {
int prefW = 0;
int prefH = 0;
Style style = l.getStyle();
int gap = l.getGap();
int ilen = icons.length;
for (int i = 0; i < ilen; i++) {
Image icon = icons[i];
if (icon != null) {
prefW = Math.max(prefW, icon.getWidth());
prefH = Math.max(prefH, icon.getHeight());
}
}
String text = l.getText();
Font font = style.getFont();
if (font == null) {
System.out.println("Missing font for " + l);
font = Font.getDefaultFont();
}
if (text != null && text.length() > 0) {
// add the text size
switch(l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
prefW += font.stringWidth(text);
prefH = Math.max(prefH, font.getHeight());
break;
case Label.BOTTOM:
case Label.TOP:
prefW = Math.max(prefW, font.stringWidth(text));
prefH += font.getHeight();
break;
}
}
// add the state image(relevant for CheckBox\RadioButton)
if (stateImage != null) {
prefW += (stateImage.getWidth() + gap);
prefH = Math.max(prefH, stateImage.getHeight());
}
if (icons[0] != null && text != null && text.length() > 0) {
switch(l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
prefW += gap;
break;
case Label.BOTTOM:
case Label.TOP:
prefH += gap;
break;
}
}
if (l.isShowEvenIfBlank()) {
prefH += style.getVerticalPadding();
prefW += style.getHorizontalPadding();
} else {
if (prefH != 0) {
prefH += style.getVerticalPadding();
}
if (prefW != 0) {
prefW += style.getHorizontalPadding();
}
}
if (style.getBorder() != null) {
prefW = Math.max(style.getBorder().getMinimumWidth(), prefW);
prefH = Math.max(style.getBorder().getMinimumHeight(), prefH);
}
if (isBackgroundImageDetermineSize() && style.getBgImage() != null) {
prefW = Math.max(style.getBgImage().getWidth(), prefW);
prefH = Math.max(style.getBgImage().getHeight(), prefH);
}
return new Dimension(prefW, prefH);
}
Aggregations