use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class ResourceEditorView method duplicateItemActionPerformed.
// GEN-LAST:event_deleteUnusedImagesActionPerformed
private void duplicateItemActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_duplicateItemActionPerformed
if (selectedResource != null && loadedResources.containsResource(selectedResource)) {
Box rename = new Box(BoxLayout.X_AXIS);
rename.add(new JLabel("New Name: "));
JTextField field = new JTextField(selectedResource, 20);
rename.add(Box.createHorizontalStrut(3));
rename.add(field);
int result = JOptionPane.showConfirmDialog(mainPanel, rename, "Duplicate", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String val = field.getText();
if (loadedResources.containsResource(val)) {
JOptionPane.showMessageDialog(mainPanel, "An Element By This Name Already Exists", "Rename", JOptionPane.ERROR_MESSAGE);
duplicateItemActionPerformed(evt);
return;
}
try {
// this effectively creates a new instance of the object
ByteArrayOutputStream bo = new ByteArrayOutputStream();
boolean m = loadedResources.isModified();
loadedResources.save(bo);
if (m) {
loadedResources.setModified();
}
bo.close();
EditableResources r = new EditableResources();
r.openFile(new ByteArrayInputStream(bo.toByteArray()));
loadedResources.addResourceObjectDuplicate(selectedResource, val, r.getResourceObject(selectedResource));
setSelectedResource(val);
} catch (IOException err) {
err.printStackTrace();
}
}
} else {
JOptionPane.showMessageDialog(mainPanel, "An Element Must Be Selected", "Rename", JOptionPane.ERROR_MESSAGE);
}
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class BorderEditor method updateBorder.
private void updateBorder(boolean updateEnabled) {
if (updateEnabled) {
okButton.setEnabled(true);
for (JComponent c : comboSelectionEnabled[0]) {
c.setEnabled(false);
}
if (borderType.getSelectedIndex() > 1) {
List<JComponent> colorElements = colorComponents;
if (borderType.getSelectedIndex() == 6 && !imageMode.isSelected()) {
colorElements = new ArrayList<JComponent>();
} else {
if (borderType.getSelectedIndex() < 6 && !themeColors.isSelected()) {
colorElements = new ArrayList<JComponent>();
} else {
if (borderType.getSelectedIndex() > 6) {
colorElements = new ArrayList<JComponent>();
}
}
}
for (JComponent c : comboSelectionEnabled[borderType.getSelectedIndex()]) {
// if colors arrive from the theme then don't enable any color related element
c.setEnabled(!colorElements.contains(c));
}
}
}
if (borderType.getSelectedIndex() == borderType.getItemCount() - 2) {
// we need to use a special case because a theme with no images will have a different offset for the border
currentBorder = RoundBorder.create().color(getColor(backgroundColor)).opacity(((Number) opacity.getValue()).intValue()).rectangle(isRectangle.isSelected()).shadowBlur(((Number) shadowBlur.getValue()).floatValue()).shadowOpacity(((Number) shadowOpacity.getValue()).intValue()).shadowSpread(((Number) shadowSpread.getValue()).intValue()).shadowX(((Number) shadowX.getValue()).floatValue()).shadowY(((Number) shadowY.getValue()).floatValue()).stroke(((Number) strokeThickness.getValue()).floatValue(), strokeMillimeter.isSelected()).strokeColor(getColor(strokeColor)).strokeOpacity(((Number) strokeOpacity.getValue()).intValue());
} else {
if (borderType.getSelectedIndex() == borderType.getItemCount() - 1) {
// we need to use a special case because a theme with no images will have a different offset for the border
currentBorder = RoundRectBorder.create().shadowBlur(((Number) rrShadowBlur.getValue()).floatValue()).shadowOpacity(((Number) rrShadowOpacity.getValue()).intValue()).shadowSpread(((Number) rrShadowSpread.getValue()).floatValue()).shadowX(((Number) rrShadowX.getValue()).floatValue()).shadowY(((Number) rrShadowY.getValue()).floatValue()).stroke(((Number) rrStrokeThickness.getValue()).floatValue(), rrStrokeMillimeter.isSelected()).strokeColor(getColor(rrStrokeColor)).strokeOpacity(((Number) rrStrokeOpacity.getValue()).intValue()).bezierCorners(rrBezier.isSelected()).cornerRadius(((Number) rrRadius.getValue()).floatValue()).bottomOnlyMode(rrMode.getSelectedIndex() == 1).topOnlyMode(rrMode.getSelectedIndex() == 2);
} else {
switch(borderType.getSelectedIndex()) {
case 0:
// null border
currentBorder = null;
break;
case 1:
// empty border
currentBorder = Border.getEmpty();
break;
case 2:
// bevel border
if (themeColors.isSelected()) {
if (raisedBorder.isSelected()) {
currentBorder = Border.createBevelRaised();
} else {
currentBorder = Border.createBevelLowered();
}
} else {
if (raisedBorder.isSelected()) {
currentBorder = Border.createBevelRaised(getColor(highlightColor), getColor(secondaryHighlightColor), getColor(shadowColor), getColor(secondaryShadowColor));
} else {
currentBorder = Border.createBevelLowered(getColor(highlightColor), getColor(secondaryHighlightColor), getColor(shadowColor), getColor(secondaryShadowColor));
}
}
break;
case 3:
// etched border
if (themeColors.isSelected()) {
if (raisedBorder.isSelected()) {
currentBorder = Border.createEtchedRaised();
} else {
currentBorder = Border.createEtchedLowered();
}
} else {
if (raisedBorder.isSelected()) {
currentBorder = Border.createEtchedRaised(getColor(highlightColor), getColor(shadowColor));
} else {
currentBorder = Border.createEtchedLowered(getColor(highlightColor), getColor(shadowColor));
}
}
break;
case 7:
{
// this is a theme with no images
if (borderType.getItemCount() < 8) {
break;
}
// image border
Image c = getButtonImageBorderIcon(this.center);
if (imageMode.isSelected()) {
currentBorder = Border.createImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(topLeft), c);
} else {
currentBorder = Border.createImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(bottom), getButtonImageBorderIconNotNull(left), getButtonImageBorderIconNotNull(right), getButtonImageBorderIconNotNull(topLeft), getButtonImageBorderIconNotNull(topRight), getButtonImageBorderIconNotNull(bottomLeft), getButtonImageBorderIconNotNull(bottomRight), c);
}
break;
}
case 8:
{
Image c = getButtonImageBorderIcon(this.center);
currentBorder = Border.createHorizonalImageBorder(getButtonImageBorderIconNotNull(left), getButtonImageBorderIconNotNull(right), c);
break;
}
case 9:
{
Image c = getButtonImageBorderIcon(this.center);
currentBorder = Border.createVerticalImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(bottom), c);
break;
}
case 4:
// line border
if (thicknessMillimeters.isSelected()) {
if (themeColors.isSelected()) {
currentBorder = Border.createLineBorder(((Number) thickness.getValue()).floatValue());
} else {
currentBorder = Border.createLineBorder(((Number) thickness.getValue()).floatValue(), getColor(lineColor));
}
} else {
if (themeColors.isSelected()) {
currentBorder = Border.createLineBorder(((Number) thickness.getValue()).intValue());
} else {
currentBorder = Border.createLineBorder(((Number) thickness.getValue()).intValue(), getColor(lineColor));
}
}
break;
case 5:
// underline border
if (thicknessMillimeters.isSelected()) {
if (themeColors.isSelected()) {
currentBorder = Border.createUndelineBorder(((Number) thickness.getValue()).floatValue());
} else {
currentBorder = Border.createUnderlineBorder(((Number) thickness.getValue()).floatValue(), getColor(lineColor));
}
} else {
if (themeColors.isSelected()) {
currentBorder = Border.createUndelineBorder(((Number) thickness.getValue()).intValue());
} else {
currentBorder = Border.createUnderlineBorder(((Number) thickness.getValue()).intValue(), getColor(lineColor));
}
}
break;
case 6:
// rounded border
if (themeColors.isSelected()) {
currentBorder = Border.createRoundBorder(((Number) arcWidth.getValue()).intValue(), ((Number) arcHeight.getValue()).intValue());
} else {
currentBorder = Border.createRoundBorder(((Number) arcWidth.getValue()).intValue(), ((Number) arcHeight.getValue()).intValue(), getColor(lineColor));
}
break;
}
}
}
final CodenameOneComponentWrapper w = (CodenameOneComponentWrapper) imageBorderPreview;
final Border finalBorder = currentBorder;
final Button b = (Button) w.getCodenameOneComponent();
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
b.clearClientProperties();
b.setPreferredSize(new com.codename1.ui.geom.Dimension(200, 100));
b.getAllStyles().setPadding(20, 20, 20, 20);
b.getAllStyles().setBorder(finalBorder);
b.getParent().revalidate();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
w.revalidate();
}
});
}
});
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class Component method pointerDragged.
/**
* If this Component is focused, the pointer dragged event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerDragged(final int x, final int y) {
Form p = getComponentForm();
if (p == null) {
return;
}
if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
pointerDraggedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerDrag, x, y));
}
if (dragAndDropInitialized) {
// keep call to pointerDragged to move the parent scroll if needed
if (dragCallbacks < 2) {
dragCallbacks++;
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (dragActivated) {
pointerDragged(x, y);
}
dragCallbacks--;
}
});
}
if (!dragActivated) {
dragActivated = true;
setVisible(false);
p.setDraggedComponent(this);
oldx = x;
oldy = y;
draggedx = getAbsoluteX();
draggedy = getAbsoluteY();
}
Component dropTo = findDropTarget(this, x, y);
if (dropTo != null && dragOverListener != null) {
ActionEvent ev = new ActionEvent(this, dropTo, x, y);
dragOverListener.fireActionEvent(ev);
if (ev.isConsumed()) {
return;
}
}
if (dropTargetComponent != dropTo) {
if (dropTargetComponent != null) {
dropTargetComponent.dragExit(this);
}
dropTargetComponent = dropTo;
if (dropTargetComponent != null) {
dropTargetComponent.dragEnter(this);
}
}
// we repaint twice to create an intersection of the old and new position
p.repaint(draggedx, draggedy, getWidth(), getHeight());
draggedx = draggedx + (x - oldx);
draggedy = draggedy + (y - oldy);
oldx = x;
oldy = y;
p.repaint(draggedx, draggedy, getWidth(), getHeight());
Container scrollParent = getParent();
while (scrollParent != null && !scrollParent.isScrollable()) {
scrollParent = scrollParent.getParent();
}
if (scrollParent != null) {
Style s = getStyle();
int w = getWidth() - s.getHorizontalPadding();
int h = getHeight() - s.getVerticalPadding();
Rectangle view;
int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
// if the dragging component is out of bounds move the scrollable parent
if (!view.contains(draggedx - scrollParent.getAbsoluteX(), draggedy - scrollParent.getAbsoluteY(), getWidth(), getHeight())) {
if ((scrollParent.isScrollableY() && scrollParent.getScrollY() >= 0 && scrollParent.getScrollY() + (draggedy + getHeight()) < scrollParent.getScrollDimension().getHeight()) || (scrollParent.isScrollableX() && scrollParent.getScrollX() >= 0 && scrollParent.getScrollX() + (draggedx + getWidth()) < scrollParent.getScrollDimension().getWidth())) {
int yposition = draggedy - scrollParent.getAbsoluteY() - 40;
if (yposition < 0) {
yposition = 0;
}
int xposition = draggedx - scrollParent.getAbsoluteX() - 40;
if (xposition < 0) {
xposition = 0;
}
int height = getHeight() + 80;
if (scrollParent.getScrollY() + draggedy + height >= scrollParent.getScrollDimension().getHeight()) {
yposition = draggedy - scrollParent.getAbsoluteY();
height = scrollParent.getScrollDimension().getHeight() - yposition;
}
int width = getWidth() + 80;
if (scrollParent.getScrollX() + draggedx + width >= scrollParent.getScrollDimension().getWidth()) {
xposition = draggedx - scrollParent.getAbsoluteX();
width = scrollParent.getScrollDimension().getWidth() - xposition;
}
scrollParent.scrollRectToVisible(xposition, yposition, width, height, scrollParent);
}
}
}
return;
}
if (dragActivated && p.getDraggedComponent() == null) {
dragActivated = false;
}
if (!dragActivated) {
boolean draggedOnX = Math.abs(p.initialPressX - x) > Math.abs(p.initialPressY - y);
shouldGrabScrollEvents = (isScrollableX() && draggedOnX) || isScrollableY() && !draggedOnX;
}
if (isScrollable() && isSmoothScrolling() && shouldGrabScrollEvents) {
if (!dragActivated) {
dragActivated = true;
lastScrollY = y;
lastScrollX = x;
p.setDraggedComponent(this);
p.registerAnimatedInternal(this);
Component fc = p.getFocused();
if (fc != null && fc != this) {
fc.dragInitiated();
}
}
// and pulling it in the reverse direction of the drag
if (isScrollableY()) {
int tl;
if (getTensileLength() > -1 && refreshTask == null) {
tl = getTensileLength();
} else {
tl = getHeight() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollY() + (lastScrollY - y);
if (isAlwaysTensile() && getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() <= getHeight()) {
if (scroll >= -tl && scroll < getHeight() + tl) {
setScrollY(scroll);
}
} else {
if (scroll >= -tl && scroll < getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() - getHeight() + tl) {
setScrollY(scroll);
}
}
updateTensileHighlightIntensity(lastScrollY, y, false);
}
if (isScrollableX()) {
int tl;
if (getTensileLength() > -1) {
tl = getTensileLength();
} else {
tl = getWidth() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollX() + (lastScrollX - x);
if (scroll >= -tl && scroll < getScrollDimension().getWidth() - getWidth() + tl) {
setScrollX(scroll);
}
}
lastScrollY = y;
lastScrollX = x;
} else {
// try to find a scrollable element until you reach the Form
Component parent = getParent();
if (!(parent instanceof Form)) {
parent.pointerDragged(x, y);
}
}
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class List method calculateElementSize.
/**
* Calculates the size of an element based on a forumla or on rendering prototype
*/
private Dimension calculateElementSize(boolean selected, boolean addMargin) {
if (renderingPrototype != null) {
Component unselected = renderer.getListCellRendererComponent(this, renderingPrototype, 0, selected);
if (addMargin) {
return unselected.getPreferredSizeWithMargin();
} else {
return unselected.getPreferredSize();
}
}
int width = 0;
int height = 0;
int elements = Math.min(listSizeCalculationSampleCount, model.getSize());
int marginY = 0;
int marginX = 0;
for (int iter = 0; iter < elements; iter++) {
Component cmp = renderer.getListCellRendererComponent(this, model.getItemAt(iter), iter, selected);
if (cmp instanceof Container) {
cmp.setShouldCalcPreferredSize(true);
}
Dimension d = cmp.getPreferredSize();
width = Math.max(width, d.getWidth());
height = Math.max(height, d.getHeight());
if (iter == 0) {
Style s = cmp.getStyle();
marginY = s.getVerticalMargins();
marginX = s.getHorizontalMargins();
}
}
return new Dimension(width + marginX, height + marginY);
}
use of com.codename1.xml.Element in project CodenameOne by codenameone.
the class HTMLComponent method processTag.
/**
* Processes the given tag. This is the main processing method that calls all others and uses itself in a recursive manner.
*
* @param element The element to process
* @param align The current alignment
*/
private void processTag(HTMLElement element, int align) {
if ((cancelled) && (!cancelledCaught)) {
return;
}
int curAlign = align;
HTMLFont oldFont = font;
int oldFontColor = textColor;
for (int i = 0; i < element.getNumChildren(); i++) {
if ((cancelled) && (!cancelledCaught)) {
break;
}
HTMLElement child = (HTMLElement) element.getChildAt(i);
// Process Tag Open
switch(child.getTagId()) {
case HTMLElement.TAG_TEXT:
// String text=child.getAttributeById(HTMLElement.ATTR_TITLE);
String text = child.getText();
if ((curComboBox != null) && (optionTag)) {
// Text is inside an OPTION tag, i.e. belongs to a ComboBox
OptionItem oi = new OptionItem(text, optionValue);
curComboBox.addItem(oi);
if (optionSelected) {
curComboBox.setSelectedItem(oi);
if (curForm != null) {
curForm.setDefaultValue(curComboBox, oi);
}
}
} else if (curTextArea != null) {
// Text is inside of a TEXTAREA tag
curTextArea.setText(text);
if (curForm != null) {
curForm.setDefaultValue(curTextArea, text);
}
} else if (element.getTagId() == HTMLElement.TAG_LEGEND) {
// Note: this is element, i.e. the child's parent (child is TAG_TEXT, and if parent is TAG_LEGEND then we process this block)
if (fieldsets.size() > 0) {
Container fset = (Container) fieldsets.lastElement();
fset.getStyle().setBorder(Border.createLineBorder(1, text));
fset.getStyle().setPadding(Component.TOP, fset.getStyle().getFont().getHeight() + 1);
}
} else if ((curTable != null) && (element.getTagId() == HTMLElement.TAG_CAPTION)) {
// Note: this is element, i.e. the child's parent (child is TAG_TEXT, and if parent is TAG_LEGEND then we process this block)
curTable.captionTextTag = child;
} else {
// long startTextTime=System.currentTimeMillis(); //debug code for performance measurement
Vector comps = null;
if (preTagCount != 0) {
comps = showPreTagText(text, curAlign);
} else {
if (FIXED_WIDTH) {
comps = showTextFixedWidth(text, curAlign);
} else {
comps = showText(text, curAlign);
}
}
if (loadCSS) {
child.setAssociatedComponents(comps);
}
// textTime+=(System.currentTimeMillis()-startTextTime); //debug code for performance measurement
}
break;
case HTMLElement.TAG_A:
link = child.getAttributeById(HTMLElement.ATTR_HREF);
if ((link != null) && (docInfo == null) && (!DocumentInfo.isAbsoluteURL(link))) {
if (htmlCallback != null) {
htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, child.getTagName(), child.getAttributeName(new Integer(HTMLElement.ATTR_HREF)), link, "Disabling relative link (" + link + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
}
link = null;
}
if ((link != null) && (htmlCallback != null)) {
int linkProps = htmlCallback.getLinkProperties(this, convertURL(link));
if ((linkProps & HTMLCallback.LINK_FORBIDDEN) != 0) {
link = null;
} else if ((linkProps & HTMLCallback.LINK_VISTED) != 0) {
linkVisited = true;
}
}
anchor = child.getAttributeById(HTMLElement.ATTR_NAME);
if (link != null) {
String aKey = child.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
if ((aKey != null) && (aKey.length() == 1)) {
accesskey = aKey.charAt(0);
}
}
break;
case HTMLElement.TAG_H1:
case HTMLElement.TAG_H2:
case HTMLElement.TAG_H3:
case HTMLElement.TAG_H4:
case HTMLElement.TAG_H5:
case HTMLElement.TAG_H6:
font = (HTMLFont) fonts.get(child.getTagName());
if (font == null) {
font = oldFont;
}
// No break here intentionally
case HTMLElement.TAG_P:
curAlign = getHorizAlign(child.getAttributeById(HTMLElement.ATTR_ALIGN), align, true);
adjustAlignment(align, curAlign);
newLineIfNotEmpty(curAlign);
newLineIfLastWasNotEmpty(curAlign);
pushContainer(child);
break;
case HTMLElement.TAG_DIV:
case // CENTER is practically DIV align=CENTER
HTMLElement.TAG_CENTER:
curAlign = child.getTagId() == HTMLElement.TAG_DIV ? getHorizAlign(child.getAttributeById(HTMLElement.ATTR_ALIGN), align, true) : Component.CENTER;
adjustAlignment(align, curAlign);
newLineIfNotEmpty(curAlign);
pushContainer(child);
break;
case HTMLElement.TAG_FIELDSET:
newLineIfNotEmpty(curAlign);
Container newCont = new Container();
newCont.setUIID("HTMLFieldset");
if (fieldsets.size() == 0) {
// First fieldset shouldn't have margin
newCont.getStyle().setMargin(Component.LEFT, 0);
}
newCont.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
curContainer.addComponent(newCont);
fieldsets.addElement(newCont);
curContainer = newCont;
if (loadCSS) {
child.setAssociatedComponents(newCont);
}
break;
case HTMLElement.TAG_BR:
if (loadCSS) {
child.setAssociatedComponents(curLine);
}
newLine(curAlign);
break;
case HTMLElement.TAG_DL:
newLineIfNotEmpty(curAlign);
newLine(curAlign);
pushContainer(child);
break;
case HTMLElement.TAG_DT:
newLineIfNotEmpty(curAlign);
pushContainer(child);
break;
case HTMLElement.TAG_UL:
case HTMLElement.TAG_DIR:
case HTMLElement.TAG_MENU:
newLineIfNotEmpty(curAlign);
ulLevel++;
listIndent += INDENT_UL;
if ((ulLevel == 1) && (olIndex == Integer.MIN_VALUE)) {
// newline only if it's the first list
newLine(curAlign);
} else {
// extra indentation for level 2 and beyond
listIndent += INDENT_UL;
}
pushContainer(child);
break;
case HTMLElement.TAG_OL:
newLineIfNotEmpty(curAlign);
if (olIndex != Integer.MIN_VALUE) {
String indexStr = ORDERED_LIST_TYPE_IDENTIFIERS[listType] + "" + olIndex;
// new Integer(olIndex));
olUpperLevelIndex.addElement(indexStr);
}
// olIndex=1;
olIndex = getInt(child.getAttributeById(HTMLElement.ATTR_START), 1);
listType = getOrderedListType(child);
if ((olUpperLevelIndex.size() == 0) && (ulLevel == 0)) {
// newline only if it's the first list
newLine(curAlign);
} else {
// add indent only for second level - first one already gets it from the numbers alignment to a 4-digit number
listIndent += INDENT_OL;
}
pushContainer(child);
break;
case HTMLElement.TAG_LI:
Container listItemCont = new Container(new BorderLayout());
listItemCont.getStyle().setMargin(Component.LEFT, leftIndent + listIndent);
curContainer.addComponent(listItemCont);
containers.addElement(curContainer);
HTMLListItem bullet = null;
if (((HTMLElement) child.getParent()).getTagId() == HTMLElement.TAG_OL) {
olIndex = getInt(child.getAttributeById(HTMLElement.ATTR_VALUE), olIndex);
int itemListType = getOrderedListType(child, listType);
HTMLListIndex listIndex = new HTMLListIndex(olIndex, itemListType);
listIndex.getUnselectedStyle().setFgColor(textColor);
listIndex.getSelectedStyle().setFgColor(textColor);
listIndex.getUnselectedStyle().setFont(font.getFont());
bullet = listIndex;
// following aligns short and long numbers (assuming a 4-digit number as the maximum, as other browsers do)
bullet.getUnselectedStyle().setAlignment(Component.RIGHT);
bullet.setPreferredW(font.getFont().stringWidth("8888. "));
} else {
bullet = new HTMLBullet(getUnorderedListType(child, ulLevel), font.getFont().getHeight(), textColor, this);
}
Container bulletCont = new Container(new BorderLayout());
bulletCont.addComponent(BorderLayout.NORTH, bullet);
listItemCont.addComponent(BorderLayout.WEST, bulletCont);
Container listItemText = new Container(new BoxLayout(BoxLayout.Y_AXIS));
listItemCont.addComponent(BorderLayout.CENTER, listItemText);
curContainer = listItemText;
if (loadCSS) {
child.setAssociatedComponents(listItemText);
}
break;
case HTMLElement.TAG_BLOCKQUOTE:
newLineIfNotEmpty(curAlign);
updateMargin(INDENT_BLOCKQUOTE);
newLine(curAlign);
pushContainer(child);
break;
case HTMLElement.TAG_DD:
newLineIfNotEmpty(curAlign);
updateMargin(INDENT_DD);
pushContainer(child);
break;
case HTMLElement.TAG_HR:
newLineIfNotEmpty(curAlign);
Label hr = new Label();
hr.setUIID("HTMLHR");
// hr.getStyle().setBorder(Border.createBevelRaised());
int hrWidth = calcSize(width, child.getAttributeById(HTMLElement.ATTR_WIDTH), width, false);
int hrHeight = getInt(child.getAttributeById(HTMLElement.ATTR_SIZE), HR_THICKNESS);
hr.setPreferredW(hrWidth);
hr.setPreferredH(hrHeight);
curLine.addComponent(hr);
newLine(curAlign);
if (loadCSS) {
child.setAssociatedComponents(hr);
}
break;
case HTMLElement.TAG_STYLE:
break;
case HTMLElement.TAG_IMG:
handleImage(child, curAlign, null);
break;
case HTMLElement.TAG_PRE:
preTagCount++;
pushContainer(child);
case HTMLElement.TAG_EM:
case HTMLElement.TAG_STRONG:
case HTMLElement.TAG_DFN:
case HTMLElement.TAG_CODE:
case HTMLElement.TAG_SAMP:
case HTMLElement.TAG_KBD:
case HTMLElement.TAG_VAR:
case HTMLElement.TAG_CITE:
case HTMLElement.TAG_TT:
font = (HTMLFont) fonts.get(child.getTagName());
if (font == null) {
font = oldFont;
}
break;
case HTMLElement.TAG_B:
case HTMLElement.TAG_I:
case HTMLElement.TAG_BIG:
case HTMLElement.TAG_SMALL:
font = getCounterpartFont(child.getTagId(), font);
break;
case HTMLElement.TAG_FORM:
curForm = new HTMLForm(this, child.getAttributeById(HTMLElement.ATTR_ACTION), child.getAttributeById(HTMLElement.ATTR_METHOD), child.getAttributeById(HTMLElement.ATTR_ENCTYPE));
pushContainer(child);
break;
case HTMLElement.TAG_BUTTON:
handleInput(child, curAlign);
break;
case HTMLElement.TAG_INPUT:
handleInput(child, curAlign);
break;
case HTMLElement.TAG_SELECT:
String multi = child.getAttributeById(HTMLElement.ATTR_MULTIPLE);
// If a select tag has size specified, it will be shown as an open list, and not as Codename One combo, even if there's no multiple seection allowed
if ((multi != null) || (child.getAttributeById(HTMLElement.ATTR_SIZE) != null)) {
curComboBox = new MultiComboBox(multi != null);
Container comboCont = new Container(new BorderLayout());
curComboBox.setItemGap(0);
comboCont.setUIID("ComboBox");
curComboBox.setUIID("List");
comboCont.addComponent(BorderLayout.CENTER, curComboBox);
} else {
curComboBox = new HTMLComboBox();
}
String name = child.getAttributeById(HTMLElement.ATTR_NAME);
if (curForm != null) {
curForm.addInput(name, curComboBox, null);
}
// Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
child.setAssociatedComponents(curComboBox);
if (eventsListener != null) {
eventsListener.registerComponent(curComboBox, child);
}
if ((!PROCESS_HTML_MP1_ONLY) && (child.getAttributeById(HTMLElement.ATTR_DISABLED) != null)) {
curComboBox.setEnabled(false);
}
break;
case HTMLElement.TAG_OPTGROUP:
if (curComboBox != null) {
String label = child.getAttributeById(HTMLElement.ATTR_LABEL);
if (label != null) {
curComboBox.addItem(label);
}
}
break;
case HTMLElement.TAG_OPTION:
optionTag = true;
optionValue = child.getAttributeById(HTMLElement.ATTR_VALUE);
if ((curComboBox != null) && (child.getAttributeById(HTMLElement.ATTR_SELECTED) != null)) {
optionSelected = true;
}
break;
case HTMLElement.TAG_TEXTAREA:
curTextArea = new TextArea(getInt(child.getAttributeById(HTMLElement.ATTR_ROWS), DEFAULT_TEXTAREA_ROWS), getInt(child.getAttributeById(HTMLElement.ATTR_COLS), DEFAULT_TEXTAREA_COLS));
if (!PROCESS_HTML_MP1_ONLY) {
if (child.getAttributeById(HTMLElement.ATTR_DISABLED) != null) {
curTextArea.setEnabled(false);
}
if (child.getAttributeById(HTMLElement.ATTR_READONLY) != null) {
curTextArea.setEditable(false);
}
}
addCmp(curTextArea, curAlign);
if (eventsListener != null) {
eventsListener.registerComponent(curTextArea, child);
}
// Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
child.setAssociatedComponents(curTextArea);
String aKey = element.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
if ((aKey != null) && (aKey.length() == 1)) {
// accessKeys.put(new Integer(aKey.charAt(0)), curTextArea);
addAccessKey(aKey.charAt(0), curTextArea, false);
}
break;
case HTMLElement.TAG_Q:
addQuote(child, curAlign, true);
quoteTagCount++;
break;
case HTMLElement.TAG_TABLE:
newLineIfNotEmpty(curAlign);
if (curTable != null) {
tables.addElement(curTable);
HTMLTableModel newTable = new HTMLTableModel();
curTable = newTable;
} else {
curTable = new HTMLTableModel();
}
// In fixed width mode we arbitrarily divide the size by a factor knowing that probably there are several cells (If we don't do it, labels inside the cell will be built up to the full width size, leaving no space for others)
width = width / 2;
break;
case HTMLElement.TAG_TR:
break;
case HTMLElement.TAG_TH:
case HTMLElement.TAG_TD:
if (curTable != null) {
handleTableCell(child, curAlign);
}
break;
case HTMLElement.TAG_LABEL:
labelForID = child.getAttributeById(HTMLElement.ATTR_FOR);
aKey = child.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
if ((aKey != null) && (aKey.length() == 1)) {
accesskey = aKey.charAt(0);
}
break;
// HTML 4 tags
case HTMLElement.TAG_FONT:
// TODO - This will not work for nested font tags, need to either define color as a local parameter or create a vector stack
textColor = HTMLElement.getColor(child.getAttributeById(HTMLElement.ATTR_COLOR), textColor);
String family = child.getAttributeById(HTMLElement.ATTR_FACE);
int size = getInt(child.getAttributeById(HTMLElement.ATTR_SIZE));
if ((family != null) || (size != 0)) {
HTMLFont f = getClosestHTMLFont(family, size, 0, 0);
if (f != null) {
font = f;
}
}
break;
case HTMLElement.TAG_U:
case // INS (Inserted text) is rendered exactly like U (underline) in most browsers
HTMLElement.TAG_INS:
if (underlineCount == 0) {
textDecoration |= Style.TEXT_DECORATION_UNDERLINE;
}
underlineCount++;
break;
case HTMLElement.TAG_S:
case HTMLElement.TAG_STRIKE:
case // DEL (Deleted text) is rendered exactly like S (strikethru) in most browsers
HTMLElement.TAG_DEL:
if (strikethruCount == 0) {
textDecoration |= Style.TEXT_DECORATION_STRIKETHRU;
}
strikethruCount++;
break;
case HTMLElement.TAG_MAP:
String mapName = child.getAttributeById(HTMLElement.ATTR_NAME);
curImageMap = new ImageMapData(mapName);
break;
case HTMLElement.TAG_AREA:
handleImageMapArea(child);
break;
case HTMLElement.TAG_SUP:
superscript++;
break;
case HTMLElement.TAG_SUB:
superscript--;
break;
case HTMLElement.TAG_TBODY:
if (curTable != null) {
curTable.startSegment(HTMLTableModel.SEGMENT_TBODY);
}
break;
case HTMLElement.TAG_THEAD:
if (curTable != null) {
curTable.startSegment(HTMLTableModel.SEGMENT_THEAD);
}
break;
case HTMLElement.TAG_TFOOT:
if (curTable != null) {
curTable.startSegment(HTMLTableModel.SEGMENT_TFOOT);
}
break;
}
if (child.getNumChildren() > 0) {
processTag(child, curAlign);
}
// Process close tag
switch(child.getTagId()) {
case HTMLElement.TAG_H1:
case HTMLElement.TAG_H2:
case HTMLElement.TAG_H3:
case HTMLElement.TAG_H4:
case HTMLElement.TAG_H5:
case HTMLElement.TAG_H6:
font = oldFont;
case HTMLElement.TAG_P:
// Restore previous alignment
curAlign = align;
newLineIfNotEmpty(curAlign);
popContainer();
newLine(curAlign);
break;
case HTMLElement.TAG_DIV:
case HTMLElement.TAG_CENTER:
// Restore previous alignment
curAlign = align;
newLineIfNotEmpty(curAlign);
popContainer();
break;
case HTMLElement.TAG_FIELDSET:
newLineIfNotEmpty(curAlign);
Container fieldsetContainer = (Container) fieldsets.lastElement();
curContainer = fieldsetContainer.getParent();
fieldsets.removeElement(fieldsetContainer);
break;
case HTMLElement.TAG_BLOCKQUOTE:
newLineIfNotEmpty(curAlign);
newLine(curAlign);
updateMargin(-INDENT_BLOCKQUOTE);
popContainer();
break;
case HTMLElement.TAG_DT:
popContainer();
break;
case HTMLElement.TAG_DD:
newLineIfNotEmpty(curAlign);
updateMargin(-INDENT_DD);
popContainer();
break;
case HTMLElement.TAG_DL:
newLine(curAlign);
popContainer();
break;
case HTMLElement.TAG_A:
link = null;
linkVisited = false;
mainLink = null;
anchor = null;
accesskey = '\0';
break;
case HTMLElement.TAG_UL:
case HTMLElement.TAG_DIR:
case HTMLElement.TAG_MENU:
ulLevel--;
if ((ulLevel == 0) && (olIndex == Integer.MIN_VALUE)) {
newLine(curAlign);
} else {
// level 2 and beyond got extra indentation, so we remove it here
listIndent -= INDENT_UL;
}
listIndent -= INDENT_UL;
popContainer();
break;
case HTMLElement.TAG_OL:
if (olUpperLevelIndex.size() != 0) {
String indexStr = (String) olUpperLevelIndex.lastElement();
olUpperLevelIndex.removeElementAt(olUpperLevelIndex.size() - 1);
listType = getOrderedListType(indexStr.charAt(0), HTMLListIndex.LIST_NUMERIC);
olIndex = getInt(indexStr.substring(1));
// First level of ordered list doesn't get indentation, so we substract only if it's nested
listIndent -= INDENT_OL;
} else {
olIndex = Integer.MIN_VALUE;
}
if ((olIndex == Integer.MIN_VALUE) && (ulLevel == 0)) {
// new line only if it is the last nested list
newLine(curAlign);
}
popContainer();
break;
case HTMLElement.TAG_LI:
if (olIndex != Integer.MIN_VALUE) {
olIndex++;
}
newLineIfNotEmpty(curAlign);
// We can't use popContainer, since with LI the container is pushed even when CSS is ignored, to provide the spacing between the list item bullet/number and the text (in a nested way if needed)
Container prevContainer = (Container) containers.lastElement();
curContainer = prevContainer;
containers.removeElement(curContainer);
// curContainer=listItemParentContainer;
break;
case HTMLElement.TAG_PRE:
preTagCount--;
popContainer();
case HTMLElement.TAG_FONT:
textColor = oldFontColor;
case HTMLElement.TAG_EM:
case HTMLElement.TAG_STRONG:
case HTMLElement.TAG_DFN:
case HTMLElement.TAG_CODE:
case HTMLElement.TAG_SAMP:
case HTMLElement.TAG_KBD:
case HTMLElement.TAG_VAR:
case HTMLElement.TAG_CITE:
case HTMLElement.TAG_B:
case HTMLElement.TAG_I:
case HTMLElement.TAG_BIG:
case HTMLElement.TAG_SMALL:
case HTMLElement.TAG_TT:
font = oldFont;
break;
case HTMLElement.TAG_FORM:
if ((curForm != null) && (!curForm.hasSubmitButton) && (curForm.getNumFields() > 0)) {
// This is a fix for forms with no submit buttons which can be resulted due to the fact XHTML-MP doesn't support the BUTTON tag and also input type button with javascript
Button submitButton = new Button(curForm.createSubmitCommand(null, null));
addCmp(submitButton, curAlign);
}
curForm = null;
popContainer();
break;
case HTMLElement.TAG_TEXTAREA:
String name = child.getAttributeById(HTMLElement.ATTR_NAME);
if (curForm != null) {
// This was moved to the end tag to enable auto complete support (i.e. if there's an autocomplete it overrides the default value)
curForm.addInput(name, curTextArea, null);
}
curTextArea = null;
break;
case HTMLElement.TAG_SELECT:
if (curComboBox instanceof MultiComboBox) {
Container comboCont = curComboBox.getParent();
int minSize = Math.min(MIN_MULTI_COMBOBOX_ITEMS, curComboBox.size());
int maxSize = Math.min(curComboBox.size(), MAX_MULTI_COMBOBOX_ITEMS);
int size = Math.min(maxSize, Math.max(getInt(child.getAttributeById(HTMLElement.ATTR_SIZE)), minSize));
Component renderCmp = curComboBox.getRenderer().getListCellRendererComponent(curComboBox, "X", 0, false);
comboCont.setPreferredH((renderCmp.getPreferredH() + renderCmp.getStyle().getMargin(Component.TOP) + renderCmp.getStyle().getMargin(Component.BOTTOM) + curComboBox.getItemGap()) * size + curComboBox.getStyle().getPadding(Component.TOP) + curComboBox.getStyle().getPadding(Component.BOTTOM));
addCmp(comboCont, curAlign);
} else {
addCmp(curComboBox, curAlign);
}
curComboBox = null;
break;
case HTMLElement.TAG_OPTION:
optionTag = false;
optionSelected = false;
optionValue = null;
break;
case HTMLElement.TAG_Q:
quoteTagCount--;
addQuote(child, curAlign, false);
break;
case HTMLElement.TAG_TABLE:
newLineIfNotEmpty(curAlign);
// For a case that TR was not closed properly
curTable.commitRowIfNotEmpty();
if (curTable.getRowCount() != 0) {
// Don't add an empty table (Creates an exception in TableLayout and useless)
/*if (TABLES_LOCK_SIZE) {
for(int r=0;r<curTable.getRowCount();r++) {
for(int c=0;c<curTable.getColumnCount();c++) {
Component cmp=(Component)curTable.getValueAt(r, c);
if (cmp!=null) { // Can be null for cells that are "spanned over"
cmp.setPreferredSize(cmp.getPreferredSize());
}
}
}
}*/
HTMLTable table = new HTMLTable(curTable);
table.getStyle().setBgTransparency(0);
if (loadCSS) {
child.setAssociatedComponents(table);
}
int borderSize = getInt(child.getAttributeById(HTMLElement.ATTR_BORDER));
int[] borderPad = new int[4];
if (borderSize > 0) {
int frame = PROCESS_HTML_MP1_ONLY ? -1 : HTMLUtils.getStringVal(child.getAttributeById(HTMLElement.ATTR_FRAME), HTMLElement.ALLOWED_TABLE_FRAME_STRINGS);
Border border = Border.createLineBorder(borderSize);
if (frame == -1) {
for (int s = 0; s < borderPad.length; s++) {
borderPad[s] = borderSize;
}
} else {
Border[] borders = new Border[4];
for (int j = 0; j < HTMLElement.ALLOWED_TABLE_FRAME_VALS[frame].length; j++) {
int side = HTMLElement.ALLOWED_TABLE_FRAME_VALS[frame][j];
borders[side] = border;
borderPad[side] = borderSize;
}
border = Border.createCompoundBorder(borders[Component.TOP], borders[Component.BOTTOM], borders[Component.LEFT], borders[Component.RIGHT]);
}
table.getUnselectedStyle().setBorder(border);
table.getSelectedStyle().setBorder(border);
table.getUnselectedStyle().setPadding(borderPad[Component.TOP], borderPad[Component.BOTTOM], borderPad[Component.LEFT], borderPad[Component.RIGHT]);
table.getSelectedStyle().setPadding(borderPad[Component.TOP], borderPad[Component.BOTTOM], borderPad[Component.LEFT], borderPad[Component.RIGHT]);
} else {
table.getUnselectedStyle().setBorder(null);
table.getSelectedStyle().setBorder(null);
table.setDrawBorder(false);
}
if (!PROCESS_HTML_MP1_ONLY) {
int rules = HTMLUtils.getStringVal(child.getAttributeById(HTMLElement.ATTR_RULES), HTMLElement.ALLOWED_TABLE_RULES_STRINGS, Table.INNER_BORDERS_ALL);
table.setInnerBorderMode(rules);
int spacing = getInt(child.getAttributeById(HTMLElement.ATTR_CELLSPACING), -1);
if (spacing != -1) {
table.setBorderSpacing(spacing, spacing);
}
int padding = getInt(child.getAttributeById(HTMLElement.ATTR_CELLPADDING), -1);
if (padding != -1) {
for (int r = 0; r < curTable.getRowCount(); r++) {
for (int c = 0; c < curTable.getColumnCount(); c++) {
Component cmp = (Component) curTable.getValueAt(r, c);
if (cmp != null) {
// Can be null for cells that are "spanned over"
cmp.getUnselectedStyle().setPadding(padding, padding, padding, padding);
cmp.getSelectedStyle().setPadding(padding, padding, padding, padding);
}
}
}
}
}
if (curTable.captionTextTag != null) {
Container captionedTable = new Container(new BoxLayout(BoxLayout.Y_AXIS));
TextArea caption = new TextArea(curTable.captionTextTag.getText());
curTable.captionTextTag.setAssociatedComponents(caption);
caption.setUIID("HTMLTableCaption");
caption.setEditable(false);
caption.setFocusable(false);
caption.getStyle().setBorder(null);
caption.getStyle().setAlignment(Component.CENTER);
captionedTable.addComponent(caption);
captionedTable.addComponent(table);
addCmp(captionedTable, curAlign);
} else {
addCmp(table, curAlign);
}
newLineIfNotEmpty(curAlign);
}
if (tables.size() == 0) {
curTable = null;
} else {
curTable = (HTMLTableModel) tables.lastElement();
tables.removeElement(curTable);
}
// In fixed width mode we arbitrarily divide the size by a factor knowing that probably there are several cells - here we restore the size back
width = width * 2;
if (width > displayWidth) {
width = displayWidth;
}
break;
case HTMLElement.TAG_TR:
if (curTable != null) {
curTable.commitRow();
}
break;
case HTMLElement.TAG_TH:
case HTMLElement.TAG_TD:
if (curTable != null) {
newLineIfNotEmpty(curAlign);
curContainer = (Container) tableCells.lastElement();
tableCells.removeElement(curContainer);
}
break;
case HTMLElement.TAG_LABEL:
labelForID = null;
accesskey = '\0';
break;
// HTML 4 tags
case HTMLElement.TAG_U:
case HTMLElement.TAG_INS:
underlineCount--;
if (underlineCount == 0) {
textDecoration -= Style.TEXT_DECORATION_UNDERLINE;
}
break;
case HTMLElement.TAG_S:
case HTMLElement.TAG_STRIKE:
case HTMLElement.TAG_DEL:
strikethruCount--;
if (strikethruCount == 0) {
textDecoration -= Style.TEXT_DECORATION_STRIKETHRU;
}
break;
case HTMLElement.TAG_MAP:
if (curImageMap != null) {
if (imageMapData == null) {
imageMapData = new Hashtable();
}
imageMapData.put(curImageMap.name, curImageMap);
if ((imageMapComponents != null) && (imageMapComponents.containsKey(curImageMap.name))) {
HTMLImageMap imageMap = (HTMLImageMap) imageMapComponents.get(curImageMap.name);
imageMap.mapData = curImageMap;
}
curImageMap = null;
}
break;
case HTMLElement.TAG_SUP:
superscript--;
break;
case HTMLElement.TAG_SUB:
superscript++;
break;
case HTMLElement.TAG_TBODY:
case HTMLElement.TAG_THEAD:
case HTMLElement.TAG_TFOOT:
if (curTable != null) {
curTable.endSegment();
}
break;
}
}
}
Aggregations