use of com.codename1.rad.models.Property in project CodenameOne by codenameone.
the class Resources method createImage.
Image createImage(DataInputStream input) throws IOException {
if (majorVersion == 0 && minorVersion == 0) {
byte[] data = new byte[input.readInt()];
input.readFully(data, 0, data.length);
return EncodedImage.create(data);
} else {
int type = input.readByte() & 0xff;
switch(type) {
// PNG file
case 0xf1:
// JPEG File
case 0xf2:
byte[] data = new byte[input.readInt()];
input.readFully(data, 0, data.length);
if (minorVersion > 3) {
int width = input.readInt();
int height = input.readInt();
boolean opaque = input.readBoolean();
return EncodedImage.create(data, width, height, opaque);
}
return EncodedImage.create(data);
// Indexed image
case 0xF3:
return createPackedImage8();
// SVG
case 0xF5:
{
int svgSize = input.readInt();
if (Image.isSVGSupported()) {
byte[] s = new byte[svgSize];
input.readFully(s);
String baseURL = input.readUTF();
boolean animated = input.readBoolean();
loadSVGRatios(input);
byte[] fallback = new byte[input.readInt()];
if (fallback.length > 0) {
input.readFully(fallback, 0, fallback.length);
}
return Image.createSVG(baseURL, animated, s);
} else {
svgSize -= input.skip(svgSize);
while (svgSize > 0) {
svgSize -= input.skip(svgSize);
}
// read the base url, the animated property and screen ratios to skip them as well...
input.readUTF();
input.readBoolean();
input.readFloat();
input.readFloat();
byte[] fallback = new byte[input.readInt()];
input.readFully(fallback, 0, fallback.length);
return EncodedImage.create(fallback);
}
}
// SVG with multi-image
case 0xf7:
{
int svgSize = input.readInt();
if (Image.isSVGSupported()) {
byte[] s = new byte[svgSize];
input.readFully(s);
String baseURL = input.readUTF();
boolean animated = input.readBoolean();
Image img = readMultiImage(input, true);
Image svg = createSVG(animated, s);
if (svg.getSVGDocument() == null) {
return img;
}
return svg;
} else {
svgSize -= input.skip(svgSize);
while (svgSize > 0) {
svgSize -= input.skip(svgSize);
}
String baseURL = input.readUTF();
// read the animated property to skip it as well...
input.readBoolean();
return readMultiImage(input);
}
}
// mutli image
case 0xF6:
return readMultiImage(input);
case 0xEF:
Timeline tl = readTimeline(input);
return tl;
// Fail this is the wrong data type
default:
throw new IOException("Illegal type while creating image: " + Integer.toHexString(type));
}
}
}
use of com.codename1.rad.models.Property in project CodenameOne by codenameone.
the class LazyValueC method createComponent.
private Component createComponent(DataInputStream in, Container parent, Container root, Resources res, Hashtable componentListeners, EmbeddedContainer embedded) throws Exception {
String name = in.readUTF();
int property = in.readInt();
// special case for the base form
if (property == PROPERTY_BASE_FORM) {
String baseFormName = name;
initBaseForm(baseFormName);
if (!ignorBaseForm) {
Form base = (Form) createContainer(res, baseFormName);
Container destination = (Container) findByName("destination", base);
// try finding an appropriate empty container if no "fixed" destination is defined
if (destination == null) {
destination = findEmptyContainer(base.getContentPane());
if (destination == null) {
System.out.println("Couldn't find appropriate 'destination' container in base form: " + baseFormName);
return null;
}
}
root = base;
Component cmp = createComponent(in, destination, root, res, componentListeners, embedded);
if (destination.getLayout() instanceof BorderLayout) {
destination.addComponent(BorderLayout.CENTER, cmp);
} else {
destination.addComponent(cmp);
}
return root;
} else {
name = in.readUTF();
property = in.readInt();
}
}
Component cmp = createComponentType(name);
if (componentListeners != null) {
Object listeners = componentListeners.get(name);
if (listeners != null) {
if (listeners instanceof Vector) {
Vector v = (Vector) listeners;
for (int iter = 0; iter < v.size(); iter++) {
bindListenerToComponent(cmp, v.elementAt(iter));
}
} else {
bindListenerToComponent(cmp, listeners);
}
}
}
Component actualLead = cmp;
if (actualLead instanceof Container) {
Container cnt = (Container) actualLead;
actualLead = cnt.getLeadComponent();
if (actualLead == null) {
actualLead = cmp;
}
}
if (actualLead instanceof Button) {
ActionListener l = getFormListenerInstance(root, embedded);
if (l != null) {
((Button) actualLead).addActionListener(l);
}
} else {
if (actualLead instanceof TextArea) {
ActionListener l = getFormListenerInstance(root, embedded);
if (l != null) {
((TextArea) actualLead).addActionListener(l);
}
} else {
if (actualLead instanceof List) {
ActionListener l = getFormListenerInstance(root, embedded);
if (l != null) {
((List) actualLead).addActionListener(l);
}
} else {
if (actualLead instanceof ContainerList) {
ActionListener l = getFormListenerInstance(root, embedded);
if (l != null) {
((ContainerList) actualLead).addActionListener(l);
}
} else {
if (actualLead instanceof com.codename1.ui.Calendar) {
ActionListener l = getFormListenerInstance(root, embedded);
if (l != null) {
((com.codename1.ui.Calendar) actualLead).addActionListener(l);
}
}
}
}
}
}
cmp.putClientProperty(TYPE_KEY, name);
if (root == null) {
root = (Container) cmp;
}
while (property != -1) {
modifyingProperty(cmp, property);
switch(property) {
case PROPERTY_CUSTOM:
String customPropertyName = in.readUTF();
modifyingCustomProperty(cmp, customPropertyName);
boolean isNull = in.readBoolean();
if (isNull) {
cmp.setPropertyValue(customPropertyName, null);
break;
}
boolean cl = cmp instanceof ContainerList;
String[] propertyNames = cmp.getPropertyNames();
for (int iter = 0; iter < propertyNames.length; iter++) {
if (propertyNames[iter].equals(customPropertyName)) {
Class type = cmp.getPropertyTypes()[iter];
String[] typeNames = cmp.getPropertyTypeNames();
String typeName = null;
if (typeNames != null && typeNames.length > iter) {
typeName = typeNames[iter];
}
Object value = readCustomPropertyValue(in, type, typeName, res, propertyNames[iter]);
if (cl && customPropertyName.equals("ListItems") && setListModel((ContainerList) cmp)) {
break;
}
cmp.setPropertyValue(customPropertyName, value);
break;
}
}
break;
case PROPERTY_EMBED:
root.putClientProperty(EMBEDDED_FORM_FLAG, "");
((EmbeddedContainer) cmp).setEmbed(in.readUTF());
Container embed = createContainer(res, ((EmbeddedContainer) cmp).getEmbed(), (EmbeddedContainer) cmp);
if (embed != null) {
if (embed instanceof Form) {
embed = formToContainer((Form) embed);
}
((EmbeddedContainer) cmp).addComponent(BorderLayout.CENTER, embed);
// this isn't exactly the "right thing" but its the best we can do to make all
// use cases work
beforeShowContainer(embed);
postShowContainer(embed);
}
break;
case PROPERTY_TOGGLE_BUTTON:
((Button) cmp).setToggle(in.readBoolean());
break;
case PROPERTY_RADIO_GROUP:
((RadioButton) cmp).setGroup(in.readUTF());
break;
case PROPERTY_SELECTED:
boolean isSelected = in.readBoolean();
if (cmp instanceof RadioButton) {
((RadioButton) cmp).setSelected(isSelected);
} else {
((CheckBox) cmp).setSelected(isSelected);
}
break;
case PROPERTY_SCROLLABLE_X:
((Container) cmp).setScrollableX(in.readBoolean());
break;
case PROPERTY_SCROLLABLE_Y:
((Container) cmp).setScrollableY(in.readBoolean());
break;
case PROPERTY_TENSILE_DRAG_ENABLED:
cmp.setTensileDragEnabled(in.readBoolean());
break;
case PROPERTY_TACTILE_TOUCH:
cmp.setTactileTouch(in.readBoolean());
break;
case PROPERTY_SNAP_TO_GRID:
cmp.setSnapToGrid(in.readBoolean());
break;
case PROPERTY_FLATTEN:
cmp.setFlatten(in.readBoolean());
break;
case PROPERTY_TEXT:
if (cmp instanceof Label) {
((Label) cmp).setText(in.readUTF());
} else {
((TextArea) cmp).setText(in.readUTF());
}
break;
case PROPERTY_TEXT_MAX_LENGTH:
((TextArea) cmp).setMaxSize(in.readInt());
break;
case PROPERTY_TEXT_CONSTRAINT:
((TextArea) cmp).setConstraint(in.readInt());
if (cmp instanceof TextField) {
int cons = ((TextArea) cmp).getConstraint();
if ((cons & TextArea.NUMERIC) == TextArea.NUMERIC) {
((TextField) cmp).setInputModeOrder(new String[] { "123" });
}
}
break;
case PROPERTY_ALIGNMENT:
if (cmp instanceof Label) {
((Label) cmp).setAlignment(in.readInt());
} else {
((TextArea) cmp).setAlignment(in.readInt());
}
break;
case PROPERTY_TEXT_AREA_GROW:
((TextArea) cmp).setGrowByContent(in.readBoolean());
break;
case PROPERTY_LAYOUT:
Layout layout = null;
switch(in.readShort()) {
case LAYOUT_BORDER_LEGACY:
layout = new BorderLayout();
break;
case LAYOUT_BORDER_ANOTHER_LEGACY:
{
BorderLayout b = new BorderLayout();
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.NORTH, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.EAST, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.WEST, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.SOUTH, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.CENTER, in.readUTF());
}
layout = b;
break;
}
case LAYOUT_BORDER:
{
BorderLayout b = new BorderLayout();
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.NORTH, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.EAST, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.WEST, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.SOUTH, in.readUTF());
}
if (in.readBoolean()) {
b.defineLandscapeSwap(BorderLayout.CENTER, in.readUTF());
}
b.setAbsoluteCenter(in.readBoolean());
layout = b;
break;
}
case LAYOUT_BOX_X:
layout = new BoxLayout(BoxLayout.X_AXIS);
break;
case LAYOUT_BOX_Y:
layout = new BoxLayout(BoxLayout.Y_AXIS);
break;
case LAYOUT_FLOW_LEGACY:
layout = new FlowLayout();
break;
case LAYOUT_FLOW:
FlowLayout f = new FlowLayout();
f.setFillRows(in.readBoolean());
f.setAlign(in.readInt());
f.setValign(in.readInt());
layout = f;
break;
case LAYOUT_LAYERED:
layout = new LayeredLayout();
break;
case LAYOUT_GRID:
layout = new GridLayout(in.readInt(), in.readInt());
break;
case LAYOUT_TABLE:
layout = new TableLayout(in.readInt(), in.readInt());
break;
}
((Container) cmp).setLayout(layout);
break;
case PROPERTY_TAB_PLACEMENT:
((Tabs) cmp).setTabPlacement(in.readInt());
break;
case PROPERTY_TAB_TEXT_POSITION:
((Tabs) cmp).setTabTextPosition(in.readInt());
break;
case PROPERTY_PREFERRED_WIDTH:
cmp.setPreferredW(in.readInt());
break;
case PROPERTY_PREFERRED_HEIGHT:
cmp.setPreferredH(in.readInt());
break;
case PROPERTY_UIID:
cmp.setUIID(in.readUTF());
break;
case PROPERTY_DIALOG_UIID:
((Dialog) cmp).setDialogUIID(in.readUTF());
break;
case PROPERTY_DISPOSE_WHEN_POINTER_OUT:
((Dialog) cmp).setDisposeWhenPointerOutOfBounds(in.readBoolean());
break;
case PROPERTY_CLOUD_BOUND_PROPERTY:
cmp.setCloudBoundProperty(in.readUTF());
break;
case PROPERTY_CLOUD_DESTINATION_PROPERTY:
cmp.setCloudDestinationProperty(in.readUTF());
break;
case PROPERTY_DIALOG_POSITION:
String pos = in.readUTF();
if (pos.length() > 0) {
((Dialog) cmp).setDialogPosition(pos);
}
break;
case PROPERTY_FOCUSABLE:
cmp.setFocusable(in.readBoolean());
break;
case PROPERTY_ENABLED:
cmp.setEnabled(in.readBoolean());
break;
case PROPERTY_SCROLL_VISIBLE:
cmp.setScrollVisible(in.readBoolean());
break;
case PROPERTY_ICON:
((Label) cmp).setIcon(res.getImage(in.readUTF()));
break;
case PROPERTY_ROLLOVER_ICON:
((Button) cmp).setRolloverIcon(res.getImage(in.readUTF()));
break;
case PROPERTY_PRESSED_ICON:
((Button) cmp).setPressedIcon(res.getImage(in.readUTF()));
break;
case PROPERTY_DISABLED_ICON:
((Button) cmp).setDisabledIcon(res.getImage(in.readUTF()));
break;
case PROPERTY_GAP:
((Label) cmp).setGap(in.readInt());
break;
case PROPERTY_VERTICAL_ALIGNMENT:
if (cmp instanceof TextArea) {
((TextArea) cmp).setVerticalAlignment(in.readInt());
} else {
((Label) cmp).setVerticalAlignment(in.readInt());
}
break;
case PROPERTY_TEXT_POSITION:
((Label) cmp).setTextPosition(in.readInt());
break;
case PROPERTY_CLIENT_PROPERTIES:
int count = in.readInt();
StringBuilder sb = new StringBuilder();
for (int iter = 0; iter < count; iter++) {
String k = in.readUTF();
String v = in.readUTF();
cmp.putClientProperty(k, v);
sb.append(k);
if (iter < count - 1) {
sb.append(",");
}
}
cmp.putClientProperty("cn1$Properties", sb.toString());
break;
case PROPERTY_NAME:
String componentName = in.readUTF();
cmp.setName(componentName);
root.putClientProperty("%" + componentName + "%", cmp);
break;
case PROPERTY_LAYOUT_CONSTRAINT:
if (parent.getLayout() instanceof BorderLayout) {
cmp.putClientProperty("layoutConstraint", in.readUTF());
} else {
TableLayout tl = (TableLayout) parent.getLayout();
TableLayout.Constraint con = tl.createConstraint(in.readInt(), in.readInt());
con.setHeightPercentage(in.readInt());
con.setWidthPercentage(in.readInt());
con.setHorizontalAlign(in.readInt());
con.setHorizontalSpan(in.readInt());
con.setVerticalAlign(in.readInt());
con.setVerticalSpan(in.readInt());
cmp.putClientProperty("layoutConstraint", con);
}
break;
case PROPERTY_TITLE:
((Form) cmp).setTitle(in.readUTF());
break;
case PROPERTY_COMPONENTS:
int componentCount = in.readInt();
if (cmp instanceof Tabs) {
for (int iter = 0; iter < componentCount; iter++) {
String tab = in.readUTF();
Component child = createComponent(in, (Container) cmp, root, res, componentListeners, embedded);
((Tabs) cmp).addTab(tab, child);
}
} else {
for (int iter = 0; iter < componentCount; iter++) {
Component child = createComponent(in, (Container) cmp, root, res, componentListeners, embedded);
Object con = child.getClientProperty("layoutConstraint");
if (con != null) {
((Container) cmp).addComponent(con, child);
} else {
((Container) cmp).addComponent(child);
}
}
}
break;
case PROPERTY_COLUMNS:
((TextArea) cmp).setColumns(in.readInt());
break;
case PROPERTY_ROWS:
((TextArea) cmp).setRows(in.readInt());
break;
case PROPERTY_HINT:
if (cmp instanceof List) {
((List) cmp).setHint(in.readUTF());
} else {
((TextArea) cmp).setHint(in.readUTF());
}
break;
case PROPERTY_HINT_ICON:
if (cmp instanceof List) {
((List) cmp).setHintIcon(res.getImage(in.readUTF()));
} else {
((TextArea) cmp).setHintIcon(res.getImage(in.readUTF()));
}
break;
case PROPERTY_ITEM_GAP:
((List) cmp).setItemGap(in.readInt());
break;
case PROPERTY_LIST_FIXED:
((List) cmp).setFixedSelection(in.readInt());
break;
case PROPERTY_LIST_ORIENTATION:
((List) cmp).setOrientation(in.readInt());
break;
case PROPERTY_LIST_ITEMS_LEGACY:
String[] items = new String[in.readInt()];
for (int iter = 0; iter < items.length; iter++) {
items[iter] = in.readUTF();
}
if (!setListModel(((List) cmp))) {
((List) cmp).setModel(new DefaultListModel((Object[]) items));
}
break;
case PROPERTY_LIST_ITEMS:
Object[] elements = readObjectArrayForListModel(in, res);
if (!setListModel(((List) cmp))) {
((List) cmp).setModel(new DefaultListModel(elements));
}
break;
case PROPERTY_LIST_RENDERER:
if (cmp instanceof ContainerList) {
((ContainerList) cmp).setRenderer(readRendererer(res, in));
} else {
((List) cmp).setRenderer(readRendererer(res, in));
}
break;
case PROPERTY_NEXT_FORM:
String nextForm = in.readUTF();
setNextForm(cmp, nextForm, res, root);
break;
case PROPERTY_COMMANDS:
readCommands(in, cmp, res, false);
break;
case PROPERTY_COMMANDS_LEGACY:
readCommands(in, cmp, res, true);
break;
case PROPERTY_CYCLIC_FOCUS:
((Form) cmp).setCyclicFocus(in.readBoolean());
break;
case PROPERTY_RTL:
cmp.setRTL(in.readBoolean());
break;
case PROPERTY_SLIDER_THUMB:
((Slider) cmp).setThumbImage(res.getImage(in.readUTF()));
break;
case PROPERTY_INFINITE:
((Slider) cmp).setInfinite(in.readBoolean());
break;
case PROPERTY_PROGRESS:
((Slider) cmp).setProgress(in.readInt());
break;
case PROPERTY_VERTICAL:
((Slider) cmp).setVertical(in.readBoolean());
break;
case PROPERTY_EDITABLE:
if (cmp instanceof TextArea) {
((TextArea) cmp).setEditable(in.readBoolean());
} else {
((Slider) cmp).setEditable(in.readBoolean());
}
break;
case PROPERTY_INCREMENTS:
((Slider) cmp).setIncrements(in.readInt());
break;
case PROPERTY_RENDER_PERCENTAGE_ON_TOP:
((Slider) cmp).setRenderPercentageOnTop(in.readBoolean());
break;
case PROPERTY_MAX_VALUE:
((Slider) cmp).setMaxValue(in.readInt());
break;
case PROPERTY_MIN_VALUE:
((Slider) cmp).setMinValue(in.readInt());
break;
}
property = in.readInt();
}
postCreateComponent(cmp);
return cmp;
}
use of com.codename1.rad.models.Property in project CodenameOne by codenameone.
the class CSSBorder method backgroundImage.
/**
* Adds one or more background images from a CSS background-image property.
* @param cssDirective The value of the background-image property.
* @return Self for chaining.
*/
public CSSBorder backgroundImage(String cssDirective) {
String[] parts = Util.split(cssDirective, ",");
List<Image> imgs = new ArrayList<Image>();
for (String part : parts) {
part = part.trim();
if (part.indexOf("url(") == 0) {
part = part.substring(4, part.length() - 1);
}
if (part.charAt(0) == '"' || part.charAt(0) == '"') {
part = part.substring(1, part.length() - 1);
}
if (part.indexOf("/") != -1) {
part = part.substring(part.lastIndexOf("/") + 1);
}
Image im = res.getImage(part);
if (im == null) {
try {
im = EncodedImage.create("/" + part);
im.setImageName(part);
} catch (IOException ex) {
Log.e(ex);
throw new IllegalArgumentException("Failed to parse image: " + part);
}
}
imgs.add(im);
}
return backgroundImage(imgs.toArray(new Image[imgs.size()]));
}
use of com.codename1.rad.models.Property in project CodenameOne by codenameone.
the class HTMLComponent method handleImageMapArea.
/**
* Handles an area definition for an image map
*
* @param areaTag The AREA tag
*/
private void handleImageMapArea(HTMLElement areaTag) {
if (curImageMap != null) {
String shape = areaTag.getAttributeById(HTMLElement.ATTR_SHAPE);
boolean supportedShape = false;
if (shape != null) {
String hrefStr = areaTag.getAttributeById(HTMLElement.ATTR_HREF);
if (shape.equalsIgnoreCase("default")) {
supportedShape = true;
curImageMap.setDefaultLink(hrefStr);
} else if ((shape.equalsIgnoreCase("rect")) || (shape.equalsIgnoreCase("circle"))) {
supportedShape = true;
String coordsStr = areaTag.getAttributeById(HTMLElement.ATTR_COORDS);
if ((coordsStr != null) && (hrefStr != null)) {
String curValStr = "";
int[] coords = new int[4];
int curCoord = 0;
boolean error = true;
try {
for (int c = 0; c < coordsStr.length(); c++) {
char ch = coordsStr.charAt(c);
if (ch != ',') {
curValStr += ch;
} else {
coords[curCoord] = Integer.parseInt(curValStr);
curCoord++;
curValStr = "";
}
}
if (curValStr.length() > 0) {
coords[curCoord] = Integer.parseInt(curValStr);
curCoord++;
}
if (shape.equalsIgnoreCase("rect")) {
if (curCoord == 4) {
curImageMap.addRectArea(new Rectangle(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]), hrefStr);
error = false;
}
} else if (curCoord == 3) {
// circle
// coords[2] is the radius, and 0,1 are the center x,y
curImageMap.addRectArea(new Rectangle(coords[0] - coords[2], coords[1] - coords[2], coords[2] * 2 + 1, coords[2] * 2 + 1), hrefStr);
// Note: The 'circle' SHAPE in AREA tag is implemented as a rectangle to avoid complication of circle pixel collision
error = false;
}
} catch (Exception e) {
// Can be number format exception or index out of bounds
// do nothing - error will stay true
}
if (error) {
notifyImageMapError("AREA tag 'coords' property value is invalid (should be exactly 3 comma seperated numbers for circle, 4 for rectangle): " + coordsStr, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_COORDS, coordsStr);
}
}
}
}
if (!supportedShape) {
notifyImageMapError("Unsupported or missing AREA tag 'shape' property: " + shape, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_SHAPE, shape);
}
} else {
notifyImageMapError("AREA tag is defined without a parent MAP tag - ignoring", HTMLCallback.ERROR_INVALID_TAG_HIERARCHY, -1, null);
}
}
use of com.codename1.rad.models.Property in project CodenameOne by codenameone.
the class ResetableTextWatcher method startEditing.
/**
* Start editing the given text-area
* This method is executed on the UI thread, so UI manipulation is safe here.
* @param activity Current running activity
* @param textArea The TextAreaData instance that wraps the CN1 TextArea that our internal EditText needs to overlap. We use
* a TextAreaData so that the text area properties can be accessed off the EDT safely.
* @param codenameOneInputType One of the input type constants in com.codename1.ui.TextArea
* @param initialText The text that appears in the Codename One text are before the call to startEditing
* @param isEditedFieldSwitch if true, then special case for async edit mode - the native editing is already active, no need to show
* native field, just change the connected field
*/
private synchronized void startEditing(Activity activity, TextAreaData textArea, String initialText, int codenameOneInputType, final boolean isEditedFieldSwitch) {
int txty = lastTextAreaY = textArea.getAbsoluteY() + textArea.getScrollY();
int txtx = lastTextAreaX = textArea.getAbsoluteX() + textArea.getScrollX();
lastTextAreaWidth = textArea.getWidth();
lastTextAreaHeight = textArea.getHeight();
int paddingTop = 0;
int paddingLeft = textArea.paddingLeft;
int paddingRight = textArea.paddingRight;
int paddingBottom = textArea.paddingBottom;
// An ugly hack to smooth over an apparent race condition where
// the lightweight textarea is not repainted after the native text field
// becomes visible - resulting in the hint still appearing while typing.
// https://github.com/codenameone/CodenameOne/issues/2629
// We just blindly repaint the textfield every 50ms for half a second
// to make sure it gets a repaint properly.
final TextArea fTextArea = textArea.textArea;
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
com.codename1.io.Util.sleep(50);
com.codename1.ui.CN.callSerially(new Runnable() {
public void run() {
fTextArea.repaint();
}
});
}
}
}).start();
if (textArea.isTextField) {
switch(textArea.getVerticalAlignment()) {
case Component.BOTTOM:
paddingTop = textArea.getHeight() - textArea.paddingBottom - textArea.fontHeight;
break;
case Component.CENTER:
paddingTop = textArea.getHeight() / 2 - textArea.fontHeight / 2;
break;
default:
paddingTop = textArea.paddingTop;
break;
}
} else {
paddingTop = textArea.paddingTop;
}
int id = activity.getResources().getIdentifier("cn1Style", "attr", activity.getApplicationInfo().packageName);
if (!isEditedFieldSwitch) {
mEditText = new EditView(activity, textArea.textArea, this, id);
defaultInputType = mEditText.getInputType();
defaultIMEOptions = mEditText.getImeOptions();
} else {
mEditText.switchToTextArea(textArea.textArea);
}
if (textArea.getClientProperty("blockCopyPaste") != null || Display.getInstance().getProperty("blockCopyPaste", "false").equals("true")) {
// The code below is taken from this stackoverflow answer: http://stackoverflow.com/a/22756538/756809
if (android.os.Build.VERSION.SDK_INT < 11) {
mEditText.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.clear();
}
});
} else {
mEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
}
} else if (isEditedFieldSwitch) {
// reset copy-paste protection
if (android.os.Build.VERSION.SDK_INT < 11) {
mEditText.setOnCreateContextMenuListener(null);
} else {
mEditText.setCustomSelectionActionModeCallback(null);
}
}
if (!isEditedFieldSwitch) {
mEditText.addTextChangedListener(mEditText.mTextWatcher);
}
mEditText.setBackgroundDrawable(null);
mEditText.setFocusableInTouchMode(true);
mEditLayoutParams = new FrameLayout.LayoutParams(0, 0);
// Set the appropriate gravity so that the left and top margins will be
// taken into account
mEditLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mEditLayoutParams.setMargins(txtx, txty, 0, 0);
mEditLayoutParams.width = textArea.getWidth();
mEditLayoutParams.height = textArea.getHeight();
mEditText.setLayoutParams(mEditLayoutParams);
if (textArea.isRTL()) {
mEditText.setGravity(Gravity.RIGHT | Gravity.TOP);
} else {
mEditText.setGravity(Gravity.LEFT | Gravity.TOP);
}
mEditText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
Component nextDown = textArea.nextDown;
boolean imeOptionTaken = true;
int ime = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
if (textArea.isSingleLineTextArea() || textArea.getDoneListener() != null) {
if (textArea.getClientProperty("searchField") != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_SEARCH);
} else {
if (textArea.getClientProperty("sendButton") != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_SEND);
} else {
if (textArea.getClientProperty("goButton") != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_GO);
} else {
if (textArea.getDoneListener() != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
} else if (nextDown != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_NEXT);
} else {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
imeOptionTaken = false;
}
}
}
}
}
mEditText.setSingleLine(textArea.isSingleLineTextArea());
mEditText.setAdapter((ArrayAdapter<String>) null);
mEditText.setText(initialText);
if (!textArea.isSingleLineTextArea() && textArea.textArea.isGrowByContent() && textArea.textArea.getGrowLimit() > -1) {
defaultMaxLines = mEditText.getMaxLines();
mEditText.setMaxLines(textArea.textArea.getGrowLimit());
}
if (textArea.nativeHintBool && textArea.getHint() != null) {
mEditText.setHint(textArea.getHint());
}
if (!isEditedFieldSwitch) {
addView(mEditText, mEditLayoutParams);
}
invalidate();
setVisibility(VISIBLE);
bringToFront();
mEditText.requestFocus();
Object nativeFont = textArea.nativeFont;
if (nativeFont == null) {
nativeFont = impl.getDefaultFont();
}
Paint p = (Paint) ((AndroidImplementation.NativeFont) nativeFont).font;
mEditText.setTypeface(p.getTypeface());
mEditText.setTextScaleX(p.getTextScaleX());
mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, p.getTextSize());
int fgColor = textArea.fgColor;
mEditText.setTextColor(Color.rgb(fgColor >> 16, (fgColor & 0x00ff00) >> 8, (fgColor & 0x0000ff)));
boolean password = false;
if ((codenameOneInputType & TextArea.PASSWORD) == TextArea.PASSWORD) {
codenameOneInputType = codenameOneInputType ^ TextArea.PASSWORD;
password = true;
}
if (textArea.isSingleLineTextArea()) {
mEditText.setInputType(getAndroidInputType(codenameOneInputType));
// if not ime was explicity requested and this is a single line textfield of type ANY add the emoji keyboard.
if (!imeOptionTaken && codenameOneInputType == TextArea.ANY) {
mEditText.setInputType(getAndroidInputType(codenameOneInputType) | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
}
if (Display.getInstance().getProperty("andAddComma", "false").equals("true") && (codenameOneInputType & TextArea.DECIMAL) == TextArea.DECIMAL) {
defaultKeyListener = mEditText.getKeyListener();
mEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
}
} else {
if (textArea.getDoneListener() != null) {
mEditText.setHorizontallyScrolling(false);
mEditText.setMaxLines(Integer.MAX_VALUE);
mEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
mEditText.setMaxWidth(textArea.getWidth());
mEditText.setMaxHeight(textArea.getHeight());
mEditText.setHorizontalScrollBarEnabled(false);
mEditText.getLayoutParams().width = textArea.getWidth();
mEditText.getLayoutParams().height = textArea.getHeight();
} else {
mEditText.setInputType(getAndroidInputType(codenameOneInputType, true));
}
}
if (password) {
int type = mInputTypeMap.get(codenameOneInputType, InputType.TYPE_CLASS_TEXT);
if ((type & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) == InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) {
type = type ^ InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
}
// turn off suggestions for passwords
mEditText.setInputType(type | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mEditText.setTransformationMethod(new MyPasswordTransformationMethod());
}
int maxLength = textArea.maxSize;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
mEditText.setFilters(FilterArray);
mEditText.setSelection(mEditText.getText().length());
showVirtualKeyboard(true);
if (Boolean.FALSE.equals(textArea.getClientProperty("android.cursorVisible"))) {
// This provides an imperfect workaround for this issue:
// https://github.com/codenameone/CodenameOne/issues/2317
// Blinking cursor causes text to disappear on some versions of android
// Can't seem to find how to detect whether device is affected, so
// just providing a client property to disable the blinking cursor
// on a particular text field.
mEditText.setCursorVisible(false);
}
/*
// Leaving this hack here for posterity. It seems that this manually
// blinking cursor causes the paste menu to disappear
// https://github.com/codenameone/CodenameOne/issues/2147
// Removing the hack below, fixes this issue. And in the test device
// I'm using the blinking of text doesn't seem to occur, so perhaps
// it was fixed via other means. Test device:
// Name: Samsung Galaxy S3 (T-Mobile)
// OS: 4.3
// Manufacturer: Samsung
// Model: 4.3
// Chipset: armeabi-v7a 1512MHz
// Memory: 16000000000
// Heap: 256000000
// Display: 720 x 1280
//
// UPDATE Feb. 13, 2018:
// This issue seems to be still present in some devices, but it isn't clear even
// how to detect it.
// Issue reported and reproduced here https://github.com/codenameone/CodenameOne/issues/2317
// Issue has been observed in a Virtual Box installation with 5.1.1, but
// cannot be reproduced in a Nexus 5 running 5.1.1.
//
if (Build.VERSION.SDK_INT < 21) {
// HACK!!! On Android 4.4, it seems that the natural blinking cursor
// causes text to disappear when it blinks. Manually blinking the
// cursor seems to work around this issue, so that's what we do here.
// This issue is described here: http://stackoverflow.com/questions/41305052/textfields-content-disappears-during-typing?noredirect=1#comment69977316_41305052
mEditText.setCursorVisible(false);
final boolean[] cursorVisible = new boolean[]{false};
if (cursorTimer != null) {
cursorTimer.cancel();
}
cursorTimer = new Timer();
cursorTimerTask = new TimerTask() {
public void run() {
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
public void run() {
EditView v = mEditText;
if (v != null) {
cursorVisible[0] = !cursorVisible[0];
v.setCursorVisible(cursorVisible[0]);
}
}
});
}
};
cursorTimer.schedule(cursorTimerTask, 100, 500);
}
*/
}
Aggregations