Package totalcross.ui

Class MultiEdit

  • All Implemented Interfaces:
    TimerListener, Scrollable, TextControl

    public class MultiEdit
    extends Container
    implements Scrollable, TextControl, TimerListener
    MultiEdit is an Edit with support for multiple lines. A static scrollbar is added, but disabled/enabled as needed.

    Here is an example showing an Edit control being used:

     import totalcross.ui.*;
    
     public class MyProgram extends MainWindow
     {
        MultiEdit mEdit;
    
        public void initUI()
        {
           // the constructor method is called with the mask, the number of lines
           // and the vertical interval in pixel between two lines
           mEdit = new MultiEdit("",3,1);
           add(mEdit,LEFT,TOP);
           // add/setRect must precede setText
           mEdit.setText("What text you want"); // eventually
        }
     }
     
    If the MultiEdit is not editable, the user can scroll the edit a page at a time just by clicking in the middle upper or middle lower.

    IN MATERIAL UI, IS VERY IMPORTANT THAT THE CAPTION IS SET BEFORE ADDING THE CONTROL.

    • Field Detail

      • hasFocus

        protected boolean hasFocus
      • editable

        protected boolean editable
      • hasCursorWhenNotEditable

        public static boolean hasCursorWhenNotEditable
        Set to false if you don't want the cursor to blink when the edit is not editable
      • improvedGeographicalFocus

        protected boolean improvedGeographicalFocus
        Set to true if you want the control to decide whether to gain/lose focus automatically, without having to press ACTION.
      • lastInsertPos

        public int lastInsertPos
        The last insert position before this control looses focus.
      • captionPress

        public CaptionPress captionPress
        Handler for the CaptionPress
      • chars

        protected java.lang.StringBuffer chars
      • mask

        public java.lang.String mask
        The mask used to infer the preferred width. Unlike the Edit class, the MultiEdit does not support real masking.
      • rowCount

        public int rowCount
        Used to set the number of rows of this MultiEdit; used as parameter to compute the preferred height. You must call setRect after changing this to resize the control in height.
      • autoSelect

        public boolean autoSelect
        If set to true, the text will be auto-selected when the focus enters. True by default on penless devices.
      • drawDots

        public boolean drawDots
        If true, a dotted line appears under each row of text (on by default)
      • gap

        public int gap
        The gap between the rows.
      • justify

        public boolean justify
        Set to true to justify the text when the MultiEdit is NOT editable. Note that this makes the text drawing a bit slower.
      • caption

        public java.lang.String caption
        The caption to draw when this Edit is empty. Important: the caption is NOT splitted.
        See Also:
        captionColor
      • captionColor

        public int captionColor
        The caption's color.
      • captionIcon

        public Image captionIcon
        An optional caption's icon
      • flick

        protected Flick flick
        The Flick object listens and performs flick animations on PenUp events when appropriate.
      • selectLast

        public boolean selectLast
        Keep selection of last character
      • persistentSelection

        public boolean persistentSelection
        Keep the selection persistent; otherwise, it is reset if you change the letter
    • Constructor Detail

      • MultiEdit

        public MultiEdit()
        Constructs a MultiEdit with 1 pixel as space between lines and with no lines. You must set the bounds using FILL or FIT.
      • MultiEdit

        public MultiEdit​(int rowCount,
                         int spaceBetweenLines)
        Constructor for a text Edit with a vertical scroll Bar, gap is 1 by default and control's bounds must be specified with a setRect. Space between lines may be 0.
      • MultiEdit

        public MultiEdit​(java.lang.String mask,
                         int rowCount,
                         int spaceBetweenLines)
        Constructor for a text Edit with a vertical scroll Bar, gap is 1 by default and control's bounds must be given with a setRect. Space between lines may be 0. The mask is used to compute the PREFERRED width of the control. Note that the mask does not masks the input. If mask is "", the FILL width is choosen.
    • Method Detail

      • flickStarted

        public boolean flickStarted()
        Description copied from interface: Scrollable
        Called when the flick animation is started.
        Specified by:
        flickStarted in interface Scrollable
      • flickEnded

        public void flickEnded​(boolean atPenDown)
        Description copied from interface: Scrollable
        Called when the flick animation is ended.
        Specified by:
        flickEnded in interface Scrollable
        Parameters:
        atPenDown - Flag indicating if the animation ended due to a pendown event.
      • canScrollContent

        public boolean canScrollContent​(int direction,
                                        java.lang.Object target)
        Description copied from interface: Scrollable
        Checks if there is room to flick this container in the given direction.
        Specified by:
        canScrollContent in interface Scrollable
        Parameters:
        direction - The direction we want to flick given by one of the following constants:
        target - The target of the series of PEN events that triggered this flick attempt.
        Returns:
        true if the container can flick in the indicated direction.
      • scrollContent

        public boolean scrollContent​(int xDelta,
                                     int yDelta,
                                     boolean fromFlick)
        Description copied from interface: Scrollable
        Performs a relative move.
        Specified by:
        scrollContent in interface Scrollable
        Parameters:
        xDelta - The relative amount of pixels to move in the X axis.
        yDelta - The relative amount of pixels to move in the Y axis.
      • getScrollPosition

        public int getScrollPosition​(int direction)
        Description copied from interface: Scrollable
        Returns the current position given the direction. Used on page scrolls.
        Specified by:
        getScrollPosition in interface Scrollable
      • mapKeys

        public void mapKeys​(java.lang.String from,
                            java.lang.String to)
        Maps the keys in the from char array into the keys in the to char array. For example enable a 'numeric pad' on devices that has the 1 in the u character, you can use this:
         ed.mapKeys("uiojklnm!.","1234567890");
         
        To make sure that lowercase characters are also handled, you should also change the capitalise mode:
         ed.capitalise = Edit.ALL_LOWER;
         
        If you want to disable a set of keys, use the setValidChars method. Note that mapKeys have precendence over setValidChars.
        Parameters:
        from - The source keys. Must have the same length of to. Set to null to disable mapping.
        to - The destination keys. Must have the same length of from
        Since:
        TotalCross 1.01
        See Also:
        setValidChars(String)
      • getPreferredHeight

        public int getPreferredHeight()
        Description copied from class: Control
        Returns the preferred height of this control.
        Overrides:
        getPreferredHeight in class Control
      • getPreferredWidth

        public int getPreferredWidth()
        Description copied from class: Control
        Returns the preferred width of this control.
        Overrides:
        getPreferredWidth in class Control
      • setMaxLength

        public void setMaxLength​(int length)
        Sets the desired maximum length for text entered in the Edit.
        Since:
        SuperWaba 2.0 beta 4
      • getMaxLength

        public int getMaxLength()
      • setKeyboard

        public void setKeyboard​(byte kbd)
        Used to change the default keyboard to be used with this Edit control. Use the constants Edit.KBD_NONE and Edit.KBD_KEYBOARD.
      • setValidChars

        public void setValidChars​(java.lang.String validCharsString)
        sets the valid chars that can be entered in this edit. if null is passed, any char can be entered. (case insensitive).
      • getText

        public java.lang.String getText()
        Returns the text displayed in the edit control.
        Specified by:
        getText in interface TextControl
      • getTextBuffer

        public java.lang.StringBuffer getTextBuffer()
        Returns the text's buffer. Do NOT change the buffer contents, since changing it will not affect the char widths array, thus, leading to a wrong display.
        Since:
        TotalCross 1.0
      • setText

        public void setText​(java.lang.String s)
        Sets the text displayed in the edit control.
        Specified by:
        setText in interface TextControl
      • setText

        public void setText​(java.lang.String s,
                            boolean postPressed)
        Sets the text displayed in the edit control.
      • setEditable

        public void setEditable​(boolean on)
        Sets if the control accepts input from the user. Note: If set to uneditable, keyboard is disabled.
      • isEditable

        public boolean isEditable()
        Gets if the control accepts input from the user
      • getNumberOfTextLines

        public int getNumberOfTextLines()
        Gets total number of lines in the text
      • setScrollbarsAlwaysVisible

        public void setScrollbarsAlwaysVisible​(boolean asNeeded)
        Set to true to hide the vertical scrollbar when it isn't needed (instead of disabling it). This must be done right after the constructor.
        Since:
        TotalCross 1.0
      • popupKCC

        public void popupKCC()
        user method to popup the keyboard/calendar/calculator for this edit.
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Description copied from class: Control
        Called after a setRect.
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • onEvent

        public void onEvent​(Event event)
        Called by the system to pass events to the edit control.
        Overrides:
        onEvent in class Control
        Parameters:
        event - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • cutText

        public void cutText​(int sel1,
                            int sel2)
        Cuts the text on the given range.
      • draw

        protected void draw​(Graphics g)
      • onWindowPaintFinished

        protected void onWindowPaintFinished()
        Description copied from class: Control
        Called after the window has finished a paint. Only called to the focused control and the parent's window.
        Overrides:
        onWindowPaintFinished in class Control
      • onPaint

        public void onPaint​(Graphics g)
        Description copied from class: Container
        Draws the border (if any). If you override this method, be sure to call super.onPaint(g);, or the border will not be drawn.
        Overrides:
        onPaint in class Container
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • pushPosState

        protected void pushPosState()
      • popPosState

        protected void popPosState()
      • onColorsChanged

        protected void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class Container
      • onFontChanged

        protected void onFontChanged()
        Description copied from class: Control
        Called after a setFont
        Overrides:
        onFontChanged in class Control
      • clear

        public void clear()
        Clears the text of this control.
        Overrides:
        clear in class Container
      • getFocusableControls

        public void getFocusableControls​(Vector v)
        Description copied from class: Container
        Get a list of child controls of this container which are focus candidates
        Overrides:
        getFocusableControls in class Container
        Parameters:
        v - A vector into which to add the focus candidates.
      • scrollToLine

        public void scrollToLine​(int line)
        Scrolls the text to the given line.
      • handleGeographicalFocusChangeKeys

        public Control handleGeographicalFocusChangeKeys​(KeyEvent ke)
        Description copied from class: Control
        Used by the main event loop to give the currently focused control an opportunity to act directly on the KeyEvent.
        Overrides:
        handleGeographicalFocusChangeKeys in class Control
        Parameters:
        ke - The KeyEvent to be processed
        Returns:
        The control that should get focus as a result of this KeyEvent. Null if this control did not handle the KeyEvent.
        See Also:
        Settings.geographicalFocus
      • scrollToBottom

        public void scrollToBottom()
        Scrolls the text to bottom.
      • scrollToTop

        public void scrollToTop()
        Scrolls the tex to the top.
      • getLength

        public int getLength()
        Returns the length of the text.
        Since:
        TotalCross 1.01
      • requestFocus

        public void requestFocus()
        Description copied from class: Control
        Sets the focus to this control. Note that in penless devices its also needed to set isHighlighting = false.
        Overrides:
        requestFocus in class Control
      • getCopy

        public MultiEdit getCopy()
        Returns a copy of this Edit with almost all features. Used by Keyboard and SIPBox classes.
        Since:
        TotalCross 1.27
      • getFlick

        public Flick getFlick()
        Description copied from interface: Scrollable
        Returns the current flick object.
        Specified by:
        getFlick in interface Scrollable
      • wasScrolled

        public boolean wasScrolled()
        Description copied from interface: Scrollable
        Returns true if the control was scrolled since last pen down
        Specified by:
        wasScrolled in interface Scrollable
      • willOpenKeyboard

        protected boolean willOpenKeyboard()
        Description copied from class: Control
        Returns true if this is a MultiEdit or an Edit that has a standard keyboard.
        Overrides:
        willOpenKeyboard in class Control
      • setCursorPos

        public void setCursorPos​(int pos)
        Sets the cursor position, ranging from 0 to the text' length. You can use this with lastInsertPos to recover cursor position.
      • setCursorPos

        public void setCursorPos​(int start,
                                 int end)
        Sets the selected text of this Edit (if start != end). Can be used to set the cursor position, if start equals end. Start must be less or equal to end, and both must be >= 0. It can also be used to clear the selectedText, calling setCursorPos(-1,0). Note: if you're setting the cursor position before the edit is drawn for the first time, the edit will not be scrolled if the end position goes beyond the limits. Important! No bounds checking is made. Be sure to not call this method with invalid positions! Example:
         ed.setText("1234567890123456");
         ed.setCursorPos(3,14);
         ed.requestFocus();
         
      • getCursorPos

        public int[] getCursorPos()
        Returns an array with the cursor positions. You can use it with getText to find the selected text String. E.g.:
         int []cursorPos = ed.getCursorPos();
         int start = cursorPos[0];
         int end = cursorPos[1];
         String text = ed.getText();
         if (start != -1) // is the text selected?
         {
            String selectedText = text.substring(start,end);
            ...