use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class HTMLComponent method addQuote.
/**
* Adds a quote according to the current quote count
*
* @param quoteElement The quote element (TAG_Q)
* @param curAlign The current horizontal alignment
*/
private void addQuote(HTMLElement quoteElement, int curAlign, boolean isStartTag) {
String quote = null;
// 0 is the start tag of primary tag and 1 its closing tag. 2 is the start of secondary tag and 3 the closing tag (Used for CSS_QUOTES)
int quoteNum = isStartTag ? 0 : 1;
if (quoteTagCount == 0) {
quote = "\"";
} else {
quote = "'";
quoteNum += 2;
}
if ((FIXED_WIDTH) && (width - x < font.stringWidth(quote))) {
newLine(curAlign);
}
Label quoteLabel = addString(quote, curAlign);
quoteLabel.putClientProperty(CLIENT_PROPERTY_QUOTE, new Integer(quoteNum));
if (loadCSS) {
quoteElement.addAssociatedComponent(quoteLabel);
}
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class HTMLComponent method pushContainer.
private void pushContainer(HTMLElement element) {
if (loadCSS) {
Container cont = new Container(new BoxLayout(BoxLayout.Y_AXIS));
cont.setScrollableX(false);
// cont.getStyle().setBgColor(Element.COLOR_VALS[contCount]);
// contCount=(contCount+1)%Element.COLOR_VALS.length; // debug for CSS
// cont.getStyle().setBgTransparency(128);
cont.getStyle().setBgTransparency(0);
element.setAssociatedComponents(cont);
curContainer.addComponent(cont);
containers.addElement(curContainer);
curContainer = cont;
}
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class HTMLComponent method setInputFormat.
/**
* Sets input format validation for a TextArea or TextField
* This is called from the CSSEngine, and since it is done after the TextArea has been added it is a bit more complicated
*
* @param inputField The TextArea to place the input format validation on
* @param inputFormat The string representing the input format
* @return The same TextArea or a new instance representing the element
*/
TextArea setInputFormat(final TextArea inputField, String inputFormat) {
TextArea returnInputField = inputField;
if (SUPPORT_INPUT_FORMAT) {
HTMLForm form = (HTMLForm) textfieldsToForms.get(inputField);
if (form != null) {
HTMLInputFormat format = HTMLInputFormat.getInputFormat(inputFormat);
if (format != null) {
form.setInputFormat(inputField, format);
final TextArea newInputField = format.applyConstraints(inputField);
returnInputField = newInputField;
// Replace operation must be done on the EDT if the form is visible
if (Display.getInstance().getCurrent() != inputField.getComponentForm()) {
// ((inputField.getComponentForm()==null) ||
// Applying the constraints may return a new instance that has to be replaced in the form
inputField.getParent().replace(inputField, newInputField, null);
} else {
Display.getInstance().callSerially(new Runnable() {
public void run() {
// Applying the constraints may return a new instance that has to be replaced in the form
inputField.getParent().replace(inputField, newInputField, null);
}
});
}
if (firstFocusable == inputField) {
firstFocusable = newInputField;
}
}
}
}
return returnInputField;
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class HTMLComponent method handleImage.
/**
* Handles the IMG tag. This includes calculating its size (if available), applying any links/accesskeys and adding it to the download queue
*
* @param imgElement the IMG element
* @param align th current alignment
* @param cmd The submit command of a form, used only for INPUT type="image"
*/
private void handleImage(HTMLElement imgElement, int align, Command cmd) {
String imageUrl = imgElement.getAttributeById(HTMLElement.ATTR_SRC);
Label imgLabel = null;
if (imageUrl != null) {
String alignStr = imgElement.getAttributeById(HTMLElement.ATTR_ALIGN);
// Image width and height
int iWidth = calcSize(getWidth(), imgElement.getAttributeById(HTMLElement.ATTR_WIDTH), 0, false);
int iHeight = calcSize(getHeight(), imgElement.getAttributeById(HTMLElement.ATTR_HEIGHT), 0, false);
// Whitespace on the image sides (i.e. Margins)
int hspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_HSPACE));
int vspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_VSPACE));
int totalWidth = iWidth + hspace * 2;
if ((FIXED_WIDTH) && (x + totalWidth >= width)) {
newLine(align);
}
// Alternative image text, shown until image is loaded.
String altText = imgElement.getAttributeById(HTMLElement.ATTR_ALT);
String imageMap = imgElement.getAttributeById(HTMLElement.ATTR_USEMAP);
if (link != null) {
// This image is inside an A tag with HREF attribute
imgLabel = new HTMLLink(altText, link, this, mainLink, false);
if (mainLink == null) {
mainLink = (HTMLLink) imgLabel;
}
if (accesskey != '\0') {
// accessKeys.put(new Integer(accesskey), imgLabel);
addAccessKey(accesskey, imgLabel, false);
}
if (!PROCESS_HTML_MP1_ONLY) {
((HTMLLink) imgLabel).isMap = (imgElement.getAttributeById(HTMLElement.ATTR_ISMAP) != null);
}
} else if (cmd != null) {
// Special case of an image submit button
imgLabel = new Button(cmd);
if ((altText != null) && (!altText.equals(""))) {
imgLabel.setText(altText);
}
if (firstFocusable == null) {
firstFocusable = imgLabel;
}
} else if (imageMap != null) {
// Image Map
imgLabel = new HTMLImageMap(this);
if (imageMapComponents == null) {
imageMapComponents = new Hashtable();
}
if (imageMap.startsWith("#")) {
// Image map are denoted by # and then the map name (But we also tolerate if map is specified without #)
imageMap = imageMap.substring(1);
}
imageMapComponents.put(imageMap, imgLabel);
if ((imageMapData != null) && (imageMapData.containsKey(imageMap))) {
ImageMapData data = (ImageMapData) imageMapData.get(imageMap);
((HTMLImageMap) imgLabel).mapData = data;
}
} else {
imgLabel = new Label(altText);
}
if ((iWidth != 0) || (iHeight != 0)) {
// reserve space while loading image if either width or height are specified, otherwise we don't know how much to reserve
iWidth += imgLabel.getStyle().getPadding(Component.LEFT) + imgLabel.getStyle().getPadding(Component.RIGHT);
iHeight += imgLabel.getStyle().getPadding(Component.TOP) + imgLabel.getStyle().getPadding(Component.BOTTOM);
imgLabel.setPreferredSize(new Dimension(iWidth, iHeight));
} else {
// If no space is reserved, make a minimal text, otherwise Codename One won't calculate the size right after the image loads
if ((imgLabel.getText() == null) || (imgLabel.getText().equals(""))) {
imgLabel.setText(" ");
}
}
// It is important that the padding of the image component itself will be all 0
// This is because when the image is loaded, its preferred size is checked to see if its width/height were preset by the width/height attribute
imgLabel.getSelectedStyle().setPadding(0, 0, 0, 0);
imgLabel.getUnselectedStyle().setPadding(0, 0, 0, 0);
imgLabel.getSelectedStyle().setFont(font.getFont());
imgLabel.getUnselectedStyle().setFont(font.getFont());
int borderSize = getInt(imgElement.getAttributeById(HTMLElement.ATTR_BORDER));
if (borderSize != 0) {
imgLabel.putClientProperty(CLIENT_PROPERTY_IMG_BORDER, new Integer(borderSize));
} else {
borderSize = 1;
}
imgLabel.getUnselectedStyle().setBorder(Border.createLineBorder(borderSize));
imgLabel.getSelectedStyle().setBorder(Border.createLineBorder(borderSize));
imgLabel.getUnselectedStyle().setBgTransparency(0);
imgLabel.getSelectedStyle().setBgTransparency(0);
Container imgCont = new Container(new BorderLayout());
imgCont.addComponent(BorderLayout.CENTER, imgLabel);
imgCont.getSelectedStyle().setMargin(vspace, vspace, hspace, hspace);
imgCont.getUnselectedStyle().setMargin(vspace, vspace, hspace, hspace);
curLine.addComponent(imgCont);
x += totalWidth;
// Alignment
imgLabel.setAlignment(getHorizAlign(alignStr, align, false));
imgLabel.setVerticalAlignment(getVertAlign(alignStr, Component.CENTER));
if (showImages) {
if (docInfo != null) {
imageUrl = docInfo.convertURL(imageUrl);
threadQueue.add(imgLabel, imageUrl);
} else {
if (DocumentInfo.isAbsoluteURL(imageUrl)) {
threadQueue.add(imgLabel, imageUrl);
} else {
if (htmlCallback != null) {
htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, imgElement.getTagName(), imgElement.getAttributeName(new Integer(HTMLElement.ATTR_SRC)), imageUrl, "Ignoring Image file referred in an IMG tag (" + imageUrl + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
}
}
}
}
if (loadCSS) {
imgElement.setAssociatedComponents(imgCont);
}
}
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class List method paint.
/**
* {@inheritDoc}
*/
public void paint(Graphics g) {
getUIManager().getLookAndFeel().drawList(g, this);
Style style = getStyle();
int width = getWidth() - style.getHorizontalPadding() - getSideGap();
if (isScrollableX()) {
width = Math.max(width, getScrollDimension().getWidth() - style.getHorizontalPadding() - getSideGap());
}
int numOfcomponents = model.getSize();
if (numOfcomponents == 0) {
paintHint(g);
return;
}
int xTranslate = getX();
int yTranslate = getY();
g.translate(xTranslate, yTranslate);
Rectangle pos = new Rectangle();
Dimension rendererSize = getElementSize(false, true);
if (fixedSelection > FIXED_NONE_BOUNDRY) {
if (animationPosition != 0 || isDragActivated()) {
if (orientation != HORIZONTAL) {
yTranslate += (animationPosition + fixedDraggedAnimationPosition);
g.translate(0, animationPosition + fixedDraggedAnimationPosition);
} else {
xTranslate += (animationPosition + fixedDraggedAnimationPosition);
g.translate(animationPosition + fixedDraggedAnimationPosition, 0);
}
}
}
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipWidth = g.getClipWidth();
int clipHeight = g.getClipHeight();
// this flag is for preformance improvements
// if we figured out that the list items are not visible anymore
// we should break from the List loop
boolean shouldBreak = false;
// improve performance for browsing the end of a very large list
int startingPoint = 0;
if (fixedSelection < FIXED_NONE_BOUNDRY) {
int startX = clipX + getAbsoluteX();
if (isRTL()) {
// In RTL the start of the list is not in the left side of the viewport, but rather the right side
startX += getWidth();
}
startingPoint = Math.max(0, pointerSelect(startX, clipY + getAbsoluteY()) - 1);
}
int startOffset = 0;
int endOffset = numOfcomponents;
if (mutableRendererBackgrounds) {
for (int i = startingPoint; i < numOfcomponents; i++) {
// skip on the selected
if (i == getCurrentSelected() && animationPosition == 0 && fixedDraggedAnimationPosition == 0) {
if (!shouldBreak) {
startOffset = i;
}
endOffset = i;
shouldBreak = true;
continue;
}
calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
// if the renderer is in the clipping region
if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
if (!shouldBreak) {
startOffset = i;
}
endOffset = i;
Dimension size = pos.getSize();
Component selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(i), i, i == getCurrentSelected());
renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
shouldBreak = true;
} else {
// this is relevant only if the List is not fixed.
if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
break;
}
}
}
} else {
T valueAt0 = getModel().getItemAt(0);
Component selectionCmp;
int selectedIndex = getSelectedIndex();
if (selectedIndex > -1 && selectedIndex < numOfcomponents) {
// this is essential otherwise we constantly ticker based on the value of the first entry
selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(selectedIndex), 0, true);
} else {
selectionCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, true);
}
Component unselectedCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, false);
for (int i = startingPoint; i < numOfcomponents; i++) {
// skip on the selected
if (i == getCurrentSelected() && animationPosition == 0) {
if (!shouldBreak) {
startOffset = i;
}
endOffset = i;
shouldBreak = true;
continue;
}
calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
// if the renderer is in the clipping region
if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
if (!shouldBreak) {
startOffset = i;
}
endOffset = i;
if (i == getCurrentSelected()) {
Dimension size = pos.getSize();
renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
} else {
Dimension size = pos.getSize();
renderComponentBackground(g, unselectedCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
}
shouldBreak = true;
} else {
// this is relevant only if the List is not fixed.
if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
break;
}
}
}
}
boolean shouldRendererSelectedEntry = (renderer.getListFocusComponent(this) == null && (fixedSelection < FIXED_NONE_BOUNDRY)) || animationPosition == 0 && model.getSize() > 0;
Rectangle selectedPos = new Rectangle();
calculateComponentPosition(getCurrentSelected(), width, selectedPos, rendererSize, getElementSize(true, true), true);
Dimension size = selectedPos.getSize();
int curSel = getCurrentSelected();
if (shouldRendererSelectedEntry && curSel > -1 && curSel < model.getSize()) {
Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
renderComponentBackground(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
}
if (paintFocusBehindList) {
paintFocus(g, width, pos, rendererSize);
}
for (int i = startOffset; i <= endOffset; i++) {
// skip on the selected
if (i == getCurrentSelected() && animationPosition == 0) {
continue;
}
calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
T value = model.getItemAt(i);
Component cmp = renderer.getListCellRendererComponent(this, value, i, false);
cmp.setCellRenderer(true);
Dimension sizeC = pos.getSize();
renderComponent(g, cmp, pos.getX(), pos.getY(), sizeC.getWidth(), sizeC.getHeight());
}
}
// if the animation has finished draw the selected element
if (shouldRendererSelectedEntry) {
Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
renderComponent(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
}
if (!paintFocusBehindList) {
paintFocus(g, width, pos, rendererSize);
}
g.translate(-xTranslate, -yTranslate);
if (spinnerOverlay != null) {
if (spinnerOverlay.getBorder() != null) {
spinnerOverlay.getBorder().paintBorderBackground(g, this);
spinnerOverlay.getBorder().paint(g, this);
} else {
spinnerOverlay.getBgPainter().paint(g, getBounds());
}
}
}
Aggregations