use of com.codename1.tools.translator.bytecodes.Field in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawTextArea.
/**
* {@inheritDoc}
*/
public void drawTextArea(Graphics g, TextArea ta) {
setFG(g, ta);
int line = ta.getLines();
int oX = g.getClipX();
int oY = g.getClipY();
int oWidth = g.getClipWidth();
int oHeight = g.getClipHeight();
Font f = ta.getStyle().getFont();
int fontHeight = f.getHeight();
int align = reverseAlignForBidi(ta);
int leftPadding = ta.getStyle().getPaddingLeft(ta.isRTL());
int rightPadding = ta.getStyle().getPaddingRight(ta.isRTL());
int topPadding = ta.getStyle().getPaddingTop();
boolean shouldBreak = false;
for (int i = 0; i < line; i++) {
int x = ta.getX() + leftPadding;
int y = ta.getY() + topPadding + (ta.getRowsGap() + fontHeight) * i;
if (Rectangle.intersects(x, y, ta.getWidth(), fontHeight, oX, oY, oWidth, oHeight)) {
String rowText = (String) ta.getTextAt(i);
// display ******** if it is a password field
String displayText = "";
if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
int rlen = rowText.length();
for (int j = 0; j < rlen; j++) {
displayText += passwordChar;
}
} else {
displayText = rowText;
}
switch(align) {
case Component.RIGHT:
x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
break;
case Component.CENTER:
x += (ta.getWidth() - leftPadding - rightPadding - f.stringWidth(displayText)) / 2;
break;
}
int nextY = ta.getY() + topPadding + (ta.getRowsGap() + fontHeight) * (i + 2);
// add "..." at the last row
if (ta.isEndsWith3Points() && ta.getGrowLimit() == (i + 1) && ta.getGrowLimit() != line) {
if (displayText.length() > 3) {
displayText = displayText.substring(0, displayText.length() - 3);
}
g.drawString(displayText + "...", x, y, ta.getStyle().getTextDecoration());
return;
} else {
g.drawString(displayText, x, y, ta.getStyle().getTextDecoration());
}
shouldBreak = true;
} else {
if (shouldBreak) {
break;
}
}
}
}
use of com.codename1.tools.translator.bytecodes.Field in project CodenameOne by codenameone.
the class DefaultLookAndFeel method getTextAreaSize.
/**
* {@inheritDoc}
*/
public Dimension getTextAreaSize(TextArea ta, boolean pref) {
int prefW = 0;
int prefH = 0;
Style style = ta.getStyle();
Font f = style.getFont();
// if this is a text field the preferred size should be the text width
if (ta.getRows() == 1) {
prefW = f.stringWidth(ta.getText());
} else {
prefW = f.charWidth(TextArea.getWidestChar()) * ta.getColumns();
}
int rows;
if (pref) {
rows = ta.getActualRows();
} else {
rows = ta.getLines();
}
prefH = (f.getHeight() + ta.getRowsGap()) * rows;
if (!ta.isActAsLabel()) {
int columns = ta.getColumns();
String str = "";
for (int iter = 0; iter < columns; iter++) {
str += TextArea.getWidestChar();
}
if (columns > 0) {
prefW = Math.max(prefW, f.stringWidth(str));
}
}
prefH = Math.max(prefH, rows * f.getHeight());
prefW += style.getPaddingRightNoRTL() + style.getPaddingLeftNoRTL();
prefH += style.getPaddingTop() + style.getPaddingBottom();
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);
}
use of com.codename1.tools.translator.bytecodes.Field in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawTextField.
/**
* {@inheritDoc}
*/
public void drawTextField(Graphics g, TextArea ta) {
setFG(g, ta);
// display ******** if it is a password field
String displayText = getTextFieldString(ta);
Style style = ta.getStyle();
int x = 0;
// ta.getCursorX();
int cursorCharPosition = ta.getCursorPosition();
Font f = style.getFont();
int cursorX = 0;
int xPos = 0;
int align = reverseAlignForBidi(ta);
int displayX = 0;
String inputMode = ta.getInputMode();
int inputModeWidth = f.stringWidth(inputMode);
// QWERTY devices don't quite have an input mode hide it also when we have a VK
if (ta.isQwertyInput() || Display.getInstance().isVirtualKeyboardShowing()) {
inputMode = "";
inputModeWidth = 0;
}
if (ta.isSingleLineTextArea()) {
// there is currently no support for CENTER aligned text fields
if (align == Component.LEFT) {
if (cursorCharPosition > 0) {
cursorCharPosition = Math.min(displayText.length(), cursorCharPosition);
xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
cursorX = ta.getX() + style.getPaddingLeft(ta.isRTL()) + xPos;
// no point in showing the input mode when there is only one input mode...
if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length == 1) {
inputModeWidth = 0;
}
if (ta.isEnableInputScroll()) {
if (ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL())) {
if (x + xPos >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2) {
x = ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2 - xPos - 1;
}
}
}
}
displayX = ta.getX() + x + style.getPaddingLeft(ta.isRTL());
} else {
x = 0;
cursorX = getTextFieldCursorX(ta);
int baseX = ta.getX() + style.getPaddingLeftNoRTL() + inputModeWidth;
int endX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL();
if (cursorX < baseX) {
x = baseX - cursorX;
} else {
if (cursorX > endX) {
x = endX - cursorX;
}
}
displayX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL() - f.stringWidth(displayText) + x;
}
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();
int clipx = ta.getX() + style.getPaddingLeft(ta.isRTL());
int clipw = ta.getWidth() - style.getPaddingLeft(ta.isRTL()) - style.getPaddingRight(ta.isRTL());
// g.pushClip();
g.clipRect(clipx, cy, clipw, ch);
switch(ta.getVerticalAlignment()) {
case Component.BOTTOM:
g.drawString(displayText, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), style.getTextDecoration());
break;
case Component.CENTER:
g.drawString(displayText, displayX, ta.getY() + ta.getHeight() / 2 - f.getHeight() / 2, style.getTextDecoration());
break;
default:
g.drawString(displayText, displayX, ta.getY() + style.getPaddingTop(), style.getTextDecoration());
break;
}
g.setClip(cx, cy, cw, ch);
// g.popClip();
} else {
drawTextArea(g, ta);
}
// no point in showing the input mode when there is only one input mode...
if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length > 1) {
if (ta.handlesInput() && ta.getWidth() / 2 > inputModeWidth) {
int drawXPos = ta.getX() + style.getPaddingLeft(ta.isRTL());
if ((!ta.isRTL() && style.getAlignment() == Component.LEFT) || (ta.isRTL() && style.getAlignment() == Component.RIGHT)) {
drawXPos = drawXPos + ta.getWidth() - inputModeWidth - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL();
}
g.setColor(style.getFgColor());
int inputIndicatorY = ta.getY() + ta.getScrollY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight();
g.fillRect(drawXPos, inputIndicatorY, inputModeWidth, f.getHeight(), (byte) 140);
g.setColor(style.getBgColor());
g.drawString(inputMode, drawXPos, inputIndicatorY);
}
}
}
use of com.codename1.tools.translator.bytecodes.Field 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.tools.translator.bytecodes.Field in project CodenameOne by codenameone.
the class ResetableTextWatcher method edit.
/**
* Entry point for using this class
* @param impl The current running activity
* @param component Any subclass of com.codename1.ui.TextArea
* @param inputType One of the TextArea's input-type constants
*/
public static void edit(final AndroidImplementation impl, final Component component, final int inputType) {
if (impl.getActivity() == null) {
throw new IllegalArgumentException("activity is null");
}
if (component == null) {
throw new IllegalArgumentException("component is null");
}
if (!(component instanceof TextArea)) {
throw new IllegalArgumentException("component must be instance of TextArea");
}
final TextArea textArea = (TextArea) component;
final String initialText = textArea.getText();
textArea.putClientProperty("InPlaceEditView.initialText", initialText);
// The very first time we try to edit a string, let's determine if the
// system default is to do async editing. If the system default
// is not yet set, we set it here, and it will be used as the default from now on
// We do this because the nativeInstance.isAsyncEditMode() value changes
// to reflect the currently edited field so it isn't a good way to keep a
// system default.
String defaultAsyncEditingSetting = Display.getInstance().getProperty("android.VKBAlwaysOpen", null);
if (defaultAsyncEditingSetting == null) {
defaultAsyncEditingSetting = impl.isAsyncEditMode() ? "true" : "false";
Display.getInstance().setProperty("android.VKBAlwaysOpen", defaultAsyncEditingSetting);
}
boolean asyncEdit = "true".equals(defaultAsyncEditingSetting) ? true : false;
// Check if the form has any setting for asyncEditing that should override
// the application defaults.
final Form parentForm = component.getComponentForm();
if (parentForm == null) {
com.codename1.io.Log.p("Attempt to edit text area that is not on a form. This is not supported");
return;
}
if (parentForm.getClientProperty("asyncEditing") != null) {
Object async = parentForm.getClientProperty("asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean) async).booleanValue();
// Log.p("Form overriding asyncEdit due to asyncEditing client property: "+asyncEdit);
}
}
if (parentForm.getClientProperty("android.asyncEditing") != null) {
Object async = parentForm.getClientProperty("android.asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean) async).booleanValue();
// Log.p("Form overriding asyncEdit due to ios.asyncEditing client property: "+asyncEdit);
}
}
if (parentForm.isFormBottomPaddingEditingMode()) {
asyncEdit = true;
}
// then this will override all other settings.
if (component.getClientProperty("asyncEditing") != null) {
Object async = component.getClientProperty("asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean) async).booleanValue();
// Log.p("Overriding asyncEdit due to field asyncEditing client property: "+asyncEdit);
}
}
if (component.getClientProperty("android.asyncEditing") != null) {
Object async = component.getClientProperty("android.asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean) async).booleanValue();
// Log.p("Overriding asyncEdit due to field ios.asyncEditing client property: "+asyncEdit);
}
}
// if true, then in async mode we are currently editing and are switching to another field
final boolean isEditedFieldSwitch;
// If we are already editing, we need to finish that up before we proceed to edit the next field.
synchronized (editingLock) {
if (mIsEditing) {
if (impl.isAsyncEditMode()) {
// Using isEditedFieldSwitch was causing issues with cursors not showing up.
// https://github.com/codenameone/CodenameOne/issues/2353
// https://stackoverflow.com/questions/49004370/focus-behaviour-in-textarea-in-cn1
// Disabling this feature by default now, but can be re-enabled by setting
// Display.getInstance().setProperty("android.reuseTextEditorOnSwitch", "true");
// This editedFieldSwitch feature was added a while back to improve experience on older
// Android devices where the field switching was going too slow.
// https://github.com/codenameone/CodenameOne/issues/2012
// This issue was resolved in this commit (https://github.com/jaanushansen/CodenameOne/commit/f3e53a80704149e4d7cde276d01c1368bcdcfe2c)
// which was submitted as part of a pull request. This fix has been the source of several
// regressions, mostly related to properties not being propagated properly when a text field is changed
// However, this issue (with the cursor not showing up), doesn't appear to have a simple solution
// so, I'm disabling this feature for now.
isEditedFieldSwitch = "true".equals(Display.getInstance().getProperty("android.reuseTextEditorOnSwitch", "false"));
final String[] out = new String[1];
TextArea prevTextArea = null;
if (sInstance != null && sInstance.mLastEditText != null) {
prevTextArea = sInstance.mLastEditText.getTextArea();
}
if (prevTextArea != null) {
final TextArea fPrevTextArea = prevTextArea;
final String retVal = sInstance.mLastEditText.getText().toString();
Display.getInstance().callSerially(new Runnable() {
public void run() {
Display.getInstance().onEditingComplete(fPrevTextArea, retVal);
}
});
}
InPlaceEditView.setEditedTextField(textArea);
nextTextArea = null;
} else {
isEditedFieldSwitch = false;
final InPlaceEditView instance = sInstance;
if (instance != null && instance.mEditText != null && instance.mEditText.mTextArea == textArea) {
instance.showTextEditorAgain();
return;
}
if (!isClosing && sInstance != null && sInstance.mEditText != null) {
isClosing = true;
impl.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
instance.endEditing(REASON_UNDEFINED, true, 0);
}
});
}
afterClose = new Runnable() {
@Override
public void run() {
impl.callHideTextEditor();
Display.getInstance().editString(component, textArea.getMaxSize(), inputType, textArea.getText());
}
};
return;
}
} else {
isEditedFieldSwitch = false;
}
mIsEditing = true;
isClosing = false;
afterClose = null;
}
impl.setAsyncEditMode(asyncEdit);
// textArea.setPreferredSize(prefSize);
if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
((TextField) textArea).setEditable(false);
}
final boolean scrollableParent = isScrollableParent(textArea);
// We wrap the text area so that we can safely pass data across to the
// android UI thread.
final TextAreaData textAreaData = new TextAreaData(textArea);
impl.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isEditedFieldSwitch) {
releaseEdit();
if (sInstance == null) {
sInstance = new InPlaceEditView(impl);
impl.relativeLayout.addView(sInstance);
}
// Let's try something new here
// We'll ALWAYS try resize edit mode (since it just works better)
// But we'll detect whether the field is still covered by the keyboard
// and switch to pan mode if necessary.
}
if (scrollableParent || parentForm.isFormBottomPaddingEditingMode()) {
setEditMode(true);
} else {
trySetEditMode(true);
}
sInstance.startEditing(impl.getActivity(), textAreaData, initialText, inputType, isEditedFieldSwitch);
}
});
final String[] out = new String[1];
// In order to reuse the code the runs after edit completion, we will wrap it in a runnable
// For sync edit mode, we will just run onComplete.run() at the end of this method. For
// Async mode we add the Runnable to the textarea as a client property, then run it
// when editing eventually completes.
Runnable onComplete = new Runnable() {
public void run() {
if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
((TextField) textArea).setEditable(true);
}
textArea.setPreferredSize(null);
if (sInstance != null && sInstance.mLastEditText != null && sInstance.mLastEditText.mTextArea == textArea) {
String retVal = sInstance.mLastEditText.getText().toString();
if (!impl.isAsyncEditMode()) {
sInstance.mLastEditText = null;
impl.getActivity().runOnUiThread(new Runnable() {
public void run() {
releaseEdit();
}
});
}
out[0] = retVal;
} else {
out[0] = initialText;
}
Display.getInstance().onEditingComplete(component, out[0]);
if (impl.isAsyncEditMode()) {
impl.callHideTextEditor();
} else {
// lock.
if (sInstance != null) {
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (sInstance != null) {
com.codename1.io.Util.sleep(5);
}
}
});
}
}
// Release the editing flag
synchronized (editingLock) {
mIsEditing = false;
}
// as a runnable ... this should take priority over the "nextTextArea" setting
if (afterClose != null) {
Display.getInstance().callSerially(afterClose);
} else if (nextTextArea != null) {
final TextArea next = nextTextArea;
nextTextArea = null;
next.requestFocus();
Display.getInstance().callSerially(new Runnable() {
public void run() {
Display.getInstance().editString(next, next.getMaxSize(), next.getConstraint(), next.getText());
}
});
}
}
};
textArea.requestFocus();
textArea.repaint();
if (impl.isAsyncEditMode()) {
component.putClientProperty("android.onAsyncEditingComplete", onComplete);
return;
}
// Make this call synchronous
// We set this flag so that waitForEditCompletion can block on it.
// The flag will be released inside the endEditing method which will
// allow the method to proceed.
waitingForSynchronousEditingCompletion = true;
waitForEditCompletion();
onComplete.run();
}
Aggregations