Search in sources :

Example 26 with Ui

use of com.codename1.ui.util.xml.Ui in project CodenameOne by codenameone.

the class CSSEngine method createBorder.

/**
 *  Returns a border for a specific side of the component
 *
 * @param styleAttributes The style attributes element containing the border directives
 * @param ui The component we want to set the border on
 * @param location One of Component.TOP/BOTTOM/LEFT/RIGHT
 * @return
 */
Border createBorder(CSSElement styleAttributes, Component ui, int location, int styles, int type) {
    int borderStyle = styleAttributes.getAttrVal(BORDER_OUTLINE_PROPERTIES[type][STYLE] + location);
    if ((borderStyle == -1) || (borderStyle == BORDER_STYLE_NONE)) {
        return null;
    }
    int borderColor = styleAttributes.getAttrVal(BORDER_OUTLINE_PROPERTIES[type][COLOR] + location);
    int borderWidth = styleAttributes.getAttrLengthVal(BORDER_OUTLINE_PROPERTIES[type][WIDTH] + location, ui, 0);
    if (borderWidth == -1) {
        // Default value
        borderWidth = CSSElement.BORDER_DEFAULT_WIDTH;
    }
    if (type == OUTLINE) {
        // all
        location = -1;
    }
    if ((styles & STYLE_SELECTED) != 0) {
        incPadding(ui.getSelectedStyle(), location, borderWidth);
    }
    if ((styles & STYLE_UNSELECTED) != 0) {
        incPadding(ui.getUnselectedStyle(), location, borderWidth);
    }
    if ((styles & STYLE_PRESSED) != 0) {
        incPadding(((HTMLLink) ui).getPressedStyle(), location, borderWidth);
    }
    Border border = null;
    if ((borderColor == -1) && (borderStyle >= BORDER_STYLE_GROOVE)) {
        borderColor = DEFAULT_3D_BORDER_COLOR;
    }
    switch(borderStyle) {
        case BORDER_STYLE_SOLID:
            if (borderColor == -1) {
                border = Border.createLineBorder(borderWidth);
            } else {
                border = Border.createLineBorder(borderWidth, borderColor);
            }
            break;
        case BORDER_STYLE_DOUBLE:
            if (borderColor == -1) {
                border = Border.createDoubleBorder(borderWidth);
            } else {
                border = Border.createDoubleBorder(borderWidth, borderColor);
            }
            break;
        case BORDER_STYLE_GROOVE:
            border = Border.createGrooveBorder(borderWidth, borderColor);
            break;
        case BORDER_STYLE_RIDGE:
            border = Border.createRidgeBorder(borderWidth, borderColor);
            break;
        case BORDER_STYLE_INSET:
            border = Border.createInsetBorder(borderWidth, borderColor);
            break;
        case BORDER_STYLE_OUTSET:
            border = Border.createOutsetBorder(borderWidth, borderColor);
            break;
        case BORDER_STYLE_DOTTED:
            if (borderColor == -1) {
                border = Border.createDottedBorder(borderWidth);
            } else {
                border = Border.createDottedBorder(borderWidth, borderColor);
            }
            break;
        case BORDER_STYLE_DASHED:
            if (borderColor == -1) {
                border = Border.createDashedBorder(borderWidth);
            } else {
                border = Border.createDashedBorder(borderWidth, borderColor);
            }
            break;
    }
    return border;
}
Also used : Border(com.codename1.ui.plaf.Border)

Example 27 with Ui

use of com.codename1.ui.util.xml.Ui in project CodenameOne by codenameone.

the class CSSEngine method setNowrapRecursive.

/**
 * Sets this element and all children to have unwrapped text.
 * In cases where text is already unwrapped no change will be made.
 * This will work only in FIXED_WIDTH mode (Checked before called)
 * Technically a lot of this logic can be found in HTMLComponent, but since we don't want to get into
 * the context of this element (i.e. what was the indentation, alignment etc.), we use this algorithm.
 *
 * @param element The element to apply text wrapping on
 */
private void setNowrapRecursive(final HTMLElement element) {
    // if (element.getId()==HTMLElement.TAG_TEXT) {
    if (element.isTextElement()) {
        // String text=element.getAttributeById(HTMLElement.ATTR_TITLE);
        String text = element.getText();
        final Vector ui = element.getUi();
        if ((text != null) && (ui != null) && (ui.size() > 1)) {
            // If it's just one word or already no-wrapped, no need to process
            String word = "";
            String newText = "";
            for (int c = 0; c < text.length(); c++) {
                char ch = text.charAt(c);
                if ((ch == ' ') || (ch == 10) || (ch == 13) || (ch == '\t') || (ch == '\n')) {
                    if (!word.equals("")) {
                        newText += word + " ";
                        word = "";
                    }
                } else {
                    word += ch;
                }
            }
            if (!word.equals("")) {
                newText += word + " ";
            }
            final Label label = (Label) ui.elementAt(0);
            setNowrapText(label, ui, newText, element);
        }
    }
    for (int i = 0; i < element.getNumChildren(); i++) {
        setNowrapRecursive((HTMLElement) element.getChildAt(i));
    }
    // If children elements' UI was changed, we need to recalc the UI of the parent
    element.recalcUi();
}
Also used : Label(com.codename1.ui.Label) Vector(java.util.Vector)

Example 28 with Ui

use of com.codename1.ui.util.xml.Ui 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;
    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);
    } 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()) {
        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.isTextField && textArea.getDoneListener() != null) {
                        mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
                    } else if (nextDown != null && nextDown instanceof TextArea && ((TextArea) nextDown).isEditable() && ((TextArea) nextDown).isEnabled()) {
                        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.,"));
        }
    }
    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);
        }
        */
}
Also used : InputFilter(android.text.InputFilter) TextArea(com.codename1.ui.TextArea) ContextMenu(android.view.ContextMenu) MenuItem(android.view.MenuItem) Paint(android.graphics.Paint) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) Paint(android.graphics.Paint) ActionMode(android.view.ActionMode) FrameLayout(android.widget.FrameLayout) ContextMenuInfo(android.view.ContextMenu.ContextMenuInfo) ContextMenu(android.view.ContextMenu) Menu(android.view.Menu) Component(com.codename1.ui.Component)

Example 29 with Ui

use of com.codename1.ui.util.xml.Ui in project CodenameOne by codenameone.

the class ResetableTextWatcher method showTextEditorAgain.

/**
 * Shows the native text field again after it has been hidden in async edit mode.
 */
private void showTextEditorAgain() {
    if (!mIsEditing || !isTextEditorHidden()) {
        return;
    }
    textEditorHidden = false;
    final TextArea ta = mEditText.mTextArea;
    // changed since the native aread was hidden.
    synchronized (this) {
        inputBuffer = new ArrayList<TextChange>();
    }
    // We are probably not on the EDT.  We need to be on the EDT to
    // safely get text from the textarea for synchronization.
    Display.getInstance().callSerially(new Runnable() {

        public void run() {
            // and the editing text area hasn't changed since we issued this call.
            if (mIsEditing && mEditText != null && mEditText.mTextArea == ta) {
                final String text = ta.getText();
                final int cursorPos = ta.getCursorPosition();
                // Now that we have our text from the CN1 text area, we need to be on the
                // Android UI thread in order to set the text of the native text editor.
                impl.getActivity().runOnUiThread(new Runnable() {

                    public void run() {
                        // and the editing text area hasn't changed since we issued this call.
                        if (mIsEditing && mEditText != null && mEditText.mTextArea == ta) {
                            // so that we don't find it in an inconsistent state.
                            synchronized (InPlaceEditView.this) {
                                // Let's record the cursor positions of the native
                                // text editor in case we need to use them after synchronizing
                                // with the CN1 textarea.
                                int start = cursorPos;
                                int end = cursorPos;
                                /*
                                    if (!inputBuffer.isEmpty()) {
                                        // If the input buffer isn't empty, then our start
                                        // and end positions will be "wonky"
                                        start = end = inputBuffer.get(0).atPos;

                                        // If the first change was a delete, then the atPos
                                        // will point to the beginning of the deleted section
                                        // so we need to adjust the end point to be *after*
                                        // the deleted section to begin.
                                        if (inputBuffer.get(0).deleteLength > 0) {
                                            end = start = end + inputBuffer.get(0).deleteLength;
                                        }
                                    }
                                    */
                                StringBuilder buf = new StringBuilder();
                                buf.append(text);
                                // Loop through any pending changes in the input buffer
                                // (I.e. key strokes that have occurred since we initiated
                                // this async callback hell!!)
                                List<TextChange> tinput = inputBuffer;
                                if (tinput != null) {
                                    for (TextChange change : tinput) {
                                        // end.
                                        if (change.textToAppend != null) {
                                            if (end >= 0 && end <= buf.length()) {
                                                buf.insert(end, change.textToAppend);
                                                end += change.textToAppend.length();
                                                start = end;
                                            } else {
                                                buf.append(change.textToAppend);
                                                end = buf.length();
                                                start = end;
                                            }
                                        } else // The change is "deleted" text.
                                        if (change.deleteLength > 0) {
                                            if (end >= change.deleteLength && end <= buf.length()) {
                                                buf.delete(end - change.deleteLength, end);
                                                end -= change.deleteLength;
                                                start = end;
                                            } else if (end > 0 && end < change.deleteLength) {
                                                buf.delete(0, end);
                                                end = 0;
                                                start = end;
                                            }
                                        }
                                    }
                                }
                                // Important:  Clear the input buffer so that the TextWatcher
                                // knows to stop filling it up.  We only need the inputBuffer
                                // to keep input between the original showTextEditorAgain() call
                                // and here.
                                inputBuffer = null;
                                mEditText.setText(buf.toString());
                                if (start < 0 || start > mEditText.getText().length()) {
                                    start = mEditText.getText().length();
                                }
                                if (end < 0 || end > mEditText.getText().length()) {
                                    end = mEditText.getText().length();
                                }
                                // Update the caret in the edit text field so we can continue.
                                mEditText.setSelection(start, end);
                            }
                        }
                    }
                });
            }
        }
    });
    reLayoutEdit(true);
    repaintTextEditor(true);
}
Also used : TextArea(com.codename1.ui.TextArea) Paint(android.graphics.Paint)

Example 30 with Ui

use of com.codename1.ui.util.xml.Ui in project CodenameOne by codenameone.

the class AddThemeEntry method updateThemePreview.

// GEN-LAST:event_trueTypeFontSizeValueStateChanged
private void updateThemePreview() {
    if (disableRefresh) {
        return;
    }
    updateThemeHashtable(themeHash);
    refreshTheme(themeHash);
    String name = codenameOnePreview.getComponentAt(0).getClass().getName();
    String selectedName = (String) componentName.getSelectedItem();
    if (!name.endsWith(selectedName)) {
        try {
            com.codename1.ui.Component c;
            if (selectedName.equals("BrowserComponent")) {
                // special case
                c = new com.codename1.ui.Label("Preview", "BrowserComponent");
            } else {
                Class cls = Class.forName("com.codename1.ui." + selectedName);
                c = (com.codename1.ui.Component) cls.newInstance();
                if (c instanceof com.codename1.ui.Label) {
                    ((com.codename1.ui.Label) c).setText("Preview");
                } else {
                    if (c instanceof com.codename1.ui.List) {
                        ((com.codename1.ui.List) c).setModel(new com.codename1.ui.list.DefaultListModel(new Object[] { "Preview 1", "Preview 2", "Preview 3" }));
                    }
                }
            }
            codenameOnePreview.removeAll();
            codenameOnePreview.addComponent(com.codename1.ui.layouts.BorderLayout.CENTER, c);
        } catch (Throwable t) {
            codenameOnePreview.removeAll();
            com.codename1.ui.Label l = new com.codename1.ui.Label("Preview");
            l.setUIID(selectedName);
            codenameOnePreview.addComponent(com.codename1.ui.layouts.BorderLayout.CENTER, l);
        }
    }
    com.codename1.ui.plaf.Style s;
    if (prefix != null && prefix.length() > 0) {
        s = com.codename1.ui.plaf.UIManager.getInstance().getComponentCustomStyle(selectedName, prefix.substring(0, prefix.length() - 1));
    } else {
        s = com.codename1.ui.plaf.UIManager.getInstance().getComponentStyle(selectedName);
    }
    codenameOnePreview.getComponentAt(0).setUnselectedStyle(s);
    codenameOnePreview.revalidate();
    previewPane.repaint();
}
Also used : Style(com.codename1.ui.plaf.Style) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List)

Aggregations

Component (com.codename1.ui.Component)8 ArrayList (java.util.ArrayList)7 Container (com.codename1.ui.Container)6 Label (com.codename1.ui.Label)6 TextArea (com.codename1.ui.TextArea)6 Vector (java.util.Vector)6 IOException (java.io.IOException)5 Hashtable (java.util.Hashtable)5 EncodedImage (com.codename1.ui.EncodedImage)4 Border (com.codename1.ui.plaf.Border)4 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)4 EditorTTFFont (com.codename1.ui.EditorTTFFont)3 Form (com.codename1.ui.Form)3 TextField (com.codename1.ui.TextField)3 AnimationObject (com.codename1.ui.animations.AnimationObject)3 EditableResources (com.codename1.ui.util.EditableResources)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3