Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Developer
stevephe
Posts: 2
Registered: 11-05-2009

svgformsdemo question...need help with adding text fields to text panel

Hello!

 

I'm wondering if anyone can help me figure out how to add multiple text fields to the TextPanel in the svgformsdemo app that came as a sample with the Blackberry JDE 4.7.0 package using the default simulator. I've been looking all over for help with this but have had no luck.

 

With the demo, there is a single text field. I'd simply like to add a few more that do the exact same thing. If someone can help me with what needs to be done to afford this flexibility I'd really appreciate it.

 

I've made alterations in many of the files and feel like I'm close to getting the correct functionality but am not quite there yet. I'll post my TextPanel.java file for starters. Let me know if more is needed to make this easier:

 

 

/*
 * SlidersEventHandler.java
 *
 * Copyright © 1998-2008 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

package com.rim.samples.device.svgformsdemo;

import org.w3c.dom.svg.*;
import org.w3c.dom.events.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

/**
 * Panel containing a Text box
 */
class TextPanel extends SVGPanel implements org.w3c.dom.events.EventListener
{    
    // Textbox panel elements
    private SVGElement _textbox1;
    private SVGElement _textbox_cursor1;
    private SVGElement _textbox_text1;
    
    private SVGElement _textbox2;
    private SVGElement _textbox_cursor2;
    private SVGElement _textbox_text2;
    
    private SVGElement _textbox3;
    private SVGElement _textbox_cursor3;
    private SVGElement _textbox_text3;    
    
    private SVGElement _dialogbox;
    private SVGElement _dialogboxbutton;
    private SVGElement _dialogbox_text1;
    private SVGElement _dialogbox_text2;
    private SVGElement _dialogbox_text3;
    
    private SVGElement _button;    
    
    // Contains the textfield helper class
    private SvgTextFieldHelper _svgTextFieldHelper1;
    private SvgTextFieldHelper _svgTextFieldHelper2;    
    private SvgTextFieldHelper _svgTextFieldHelper3;    
    
    // Contains the textbox contents
    private String _textboxText1;    
    private String _textboxText2;  
    private String _textboxText3;  
    
    // Contains the textbox field cursor element 
    private SVGElement _textboxCursor1;
    private SVGElement _textboxCursor2;
    private SVGElement _textboxCursor3;
    
    // Indicates if textbox is active or not
    //private boolean _textFieldActive = false;   
    

    


    private boolean _textFieldActive1 = false; 
    private boolean _textFieldActive2 = false; 
    private boolean _textFieldActive3 = false;     
    public boolean _textBoxModeNavigation1 = false;
    public boolean _textBoxModeNavigation2 = false; 
    public boolean _textBoxModeNavigation3 = false;
    private SVGElement _firstTextBoxItem, _secondTextBoxItem, _thirdTextBoxItem;
    private SVGElement _firstTextBoxText, _secondTextBoxText, _thirdTextBoxText;
    private SVGElement _currentTextBoxItem;
    private SVGElement _currentTextBoxItemText;


    
    /**
     * Constructs a new TextPanel
     * @param svgFormsScreen The applications's main screen
     */
    TextPanel(SVGFormsScreen svgFormsScreen) 
    {
        super(svgFormsScreen);
        this.initializeTextbox();
        this.activateTextBox();
        super.setFirstElement(_textbox1);
        super.setLastElement(_button);
        
        
        this.setTextBoxElement1(_textbox_cursor1);
        this.setTextBoxElement2(_textbox_cursor2);
        this.setTextBoxElement3(_textbox_cursor3);
        _currentTextBoxItem = this.getTextBoxElement1();
       
        
        
        // Textfield helper class is associated with the text panel
        _svgTextFieldHelper1 = new SvgTextFieldHelper(_svgFormsScreen, this);  
        
        _svgTextFieldHelper2 = new SvgTextFieldHelper(_svgFormsScreen, this); 
        _svgTextFieldHelper3 = new SvgTextFieldHelper(_svgFormsScreen, this);
    }    
    
    /**
     * Initializes the textbox panel elements
     */
    private void initializeTextbox()
    {
        _textbox1 = (SVGElement)_svgFormsScreen.getElementById("textbox1");
        _textbox_cursor1 = (SVGElement)_svgFormsScreen.getElementById("textboxcursor1");
        _textbox_text1 = (SVGElement)_svgFormsScreen.getElementById("textboxtext1");
        
        _textbox2 = (SVGElement)_svgFormsScreen.getElementById("textbox2");
        _textbox_cursor2 = (SVGElement)_svgFormsScreen.getElementById("textboxcursor2");
        _textbox_text2 = (SVGElement)_svgFormsScreen.getElementById("textboxtext2");
        
        _textbox3 = (SVGElement)_svgFormsScreen.getElementById("textbox3");
        _textbox_cursor3 = (SVGElement)_svgFormsScreen.getElementById("textboxcursor3");
        _textbox_text3 = (SVGElement)_svgFormsScreen.getElementById("textboxtext3");  
        
        _dialogbox = (SVGElement)_svgFormsScreen.getElementById("dialogbox");
        _dialogboxbutton = (SVGElement)_svgFormsScreen.getElementById("dialogboxbutton");       
        _dialogbox_text1 = (SVGElement)_svgFormsScreen.getElementById("dialogboxtext1");   
        _dialogbox_text2 = (SVGElement)_svgFormsScreen.getElementById("dialogboxtext2"); 
        _dialogbox_text3 = (SVGElement)_svgFormsScreen.getElementById("dialogboxtext3");    
        
        _button = (SVGElement)_svgFormsScreen.getElementById("button2");     
    }
       
    /**
     * Activates the textbox on a click
     */
    private void activateTextBox()
    {
        activateSVGElement(_textbox1, this);
        activateSVGElement(_textbox2, this);
        activateSVGElement(_textbox3, this);
        activateSVGElement(_button, this);
    }    
    
    
    
    private void activateTextBox1()
    {
        _textFieldActive1 = true;
        activateSVGElement(_textbox_cursor1, this);
    }
    
    private void activateTextBox2()
    {
        _textFieldActive2 = true;
        activateSVGElement(_textbox_cursor2, this);
    }
    
    private void activateTextBox3()
    {
        _textFieldActive3 = true;
        activateSVGElement(_textbox_cursor3, this);
    }        
    
    private void deactivateTextBox1()
    {
        _textFieldActive1 = false;
        deActivateSVGElement(_textbox_cursor1, this);
    }

    private void deactivateTextBox2()
    {
        _textFieldActive2 = false;
        deActivateSVGElement(_textbox_cursor2, this);
    }
    
    private void deactivateTextBox3()
    {
        _textFieldActive3 = false;
        deActivateSVGElement(_textbox_cursor3, this);
    }        
    
    private void setTextBoxElement1(SVGElement svgElement)
    {
        _firstTextBoxText = svgElement;
    }

    private void setTextBoxElement2(SVGElement svgElement)
    {
        _secondTextBoxText = svgElement;
    }
    
    private void setTextBoxElement3(SVGElement svgElement)
    {
        _thirdTextBoxText = svgElement;
    }
    
    SVGElement getTextBoxElement1()
    { 
        return _firstTextBoxText;
    }
    
    SVGElement getTextBoxElement2()
    { 
        return _secondTextBoxText;
    } 
       
    SVGElement getTextBoxElement3()
    {
        return _thirdTextBoxText;
    }
    
    
    
        
    /**
     * Checks if the text box is active
     * @return True if the text box is active, otherwise false
     */
    //boolean isTextBoxActive()
    //{
    //    return _textFieldActive1;
    //}    
    
    boolean isTextBoxActive1()
    {
        return this._textFieldActive1;
    }    
    
    boolean isTextBoxActive2()
    {
        return this._textFieldActive2;
    }
    
    boolean isTextBoxActive3()
    {
        return this._textFieldActive3;
    }
    
    
    void textBoxModeNavigationOn1()
    {
        _textBoxModeNavigation1 = true;
    }
    
    void textBoxModeNavigationOn2()
    {
        _textBoxModeNavigation2 = true;
    }
    
    void textBoxModeNavigationOn3()
    {
        _textBoxModeNavigation3 = true;
    }    
    
    void textBoxModeNavigationOff1()
    {
        _textBoxModeNavigation1 = false;
    }
    
    void textBoxModeNavigationOff2()
    {
        _textBoxModeNavigation2 = false;
    }
    
    void textBoxModeNavigationOff3()
    {
        _textBoxModeNavigation3 = false;
    } 
    
    
    boolean inTextBoxModeNavigation1()
    {
        return _textBoxModeNavigation1;
    }
 
    boolean inTextBoxModeNavigation2()
    {
        return _textBoxModeNavigation2;
    }
    
    boolean inTextBoxModeNavigation3()
    {
        return _textBoxModeNavigation3;
    }    
        
    
/*    
    boolean isTextBoxElement1()
    {
        Node currentNode = _currentFocusInElement.getParentNode();
        if(currentNode.getParentNode() == (Node)_textBox1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    boolean isTextBoxElement2()
    {
        Node currentNode = _currentFocusInElement.getParentNode();
        if(currentNode.getParentNode() == (Node)_textBox2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }    
    
    boolean isTextBoxElement3()
    {
        Node currentNode = _currentFocusInElement.getParentNode();
        if(currentNode.getParentNode() == (Node)_textBox3)
        {
            return true;
        }
        else
        {
            return false;
        }
    }    
  */     
       
        
    /**
     * Adds the svgTextField that remains in the background
     */
    //void addTextField()
    //{
    //    _svgTextFieldHelper.addTextField();
    //    _textFieldActive = true;            
    //}    
    
    
    
    void addTextField1()
    {
        _svgTextFieldHelper1.addTextField();
        _textFieldActive1 = true;            
    }    
   
    void addTextField2()
    {
        _svgTextFieldHelper2.addTextField();
        _textFieldActive2 = true;            
    }  
    
    void addTextField3()
    {
        _svgTextFieldHelper3.addTextField();
        _textFieldActive3 = true;            
    }           
            
    /**
     * Removes the text field
     */
    //void removeTextField()
    //{
    //    _svgTextFieldHelper.removeTextField();
    //    _svgFormsScreen.setFocus(this._currentClickedElement);
    //    _textFieldActive = false;        
    //}    
    
      
    void removeTextField1()
    {
        _svgTextFieldHelper1.removeTextField();
        _svgFormsScreen.setFocus(this._currentClickedElement);
        _textFieldActive1 = false;        
    }   
 
     void removeTextField2()
    {
        _svgTextFieldHelper2.removeTextField();
        _svgFormsScreen.setFocus(this._currentClickedElement);
        _textFieldActive2 = false;        
    }   
    
    void removeTextField3()
    {
        _svgTextFieldHelper3.removeTextField();
        _svgFormsScreen.setFocus(this._currentClickedElement);
        _textFieldActive3 = false;        
    }   
               
     /**
     * Sets the text of the textbox element
     * @param text The text to display in the textbox
     */
    void setText(String text)
    {
        _textboxText1 = text;   
        _textboxText2 = text;
        _textboxText3 = text;          
        _animator.invokeLater(new Runnable()
        {
            public void run()
            {
                
                

                
                
                if(_textbox_cursor1.getTrait("display").equals("none"))
                {
                    _textbox_cursor1.setTrait("display", "inline");
                    
                    _textbox_text1.setTrait("#text", _textboxText1);
                
                    try
                    {
                        // Obtain the font object corresponding to the text element
                        FontFamily family = FontFamily.forName("BBMillbank");
                        Font font = family.getFont(Font.PLAIN, 15, Ui.UNITS_px);
                    
                        // Set the X and Y co-ordinates of the cursor rect
                        _textbox_cursor1.setFloatTrait("x", (Display.getWidth() / 2 - 83 + font.getAdvance(_textboxText1)));
                        _textbox_cursor1.setFloatTrait("y", 144.0f);              
                    }
                    catch(ClassNotFoundException cnfe)
                    {
                        System.out.println(cnfe.toString());
                    }   
                } 
                
                

                
                else if(_textbox_cursor2.getTrait("display").equals("none"))
                {
                    _textbox_cursor2.setTrait("display", "inline");
                    
                    _textbox_text2.setTrait("#text", _textboxText2);
                
                    try
                    {
                        
                        FontFamily family = FontFamily.forName("BBMillbank");
                        Font font = family.getFont(Font.PLAIN, 15, Ui.UNITS_px);
                    
                        
                        _textbox_cursor2.setFloatTrait("x", (Display.getWidth() / 2 - 83 + font.getAdvance(_textboxText2)));
                        _textbox_cursor2.setFloatTrait("y", 144.0f);              
                    }
                    catch(ClassNotFoundException cnfe)
                    {
                        System.out.println(cnfe.toString());
                    }   
                }  
                
                

                
                else if(_textbox_cursor3.getTrait("display").equals("none"))
                {
                    _textbox_cursor3.setTrait("display", "inline");
                    
                    _textbox_text3.setTrait("#text", _textboxText3);
                
                    try
                    {
                        
                        FontFamily family = FontFamily.forName("BBMillbank");
                        Font font = family.getFont(Font.PLAIN, 15, Ui.UNITS_px);
                    
                        
                        _textbox_cursor3.setFloatTrait("x", (Display.getWidth() / 2 - 83 + font.getAdvance(_textboxText3)));
                        _textbox_cursor3.setFloatTrait("y", 144.0f);              
                    }
                    catch(ClassNotFoundException cnfe)
                    {
                        System.out.println(cnfe.toString());
                    }   
                }          
             
               
            }
               
               
          
            
            
        });      
    }
    


       
    
    /**
    * Handles the DOMActivate event
    * @param evt The event to handle
    */
    public void handleEvent(Event evt)
    {
        // Handle the DOMFocusIn event
        if (evt.getType().equals("DOMFocusIn"))
        {
            // Store the currently focused element
            _currentFocusInElement = (SVGElement)evt.getCurrentTarget();
            setCurrentFocusElement(_currentFocusInElement);
        }
        // Handle the click event       
        else if (evt.getType().equals("click"))
        {
            _currentClickedElement = (SVGElement)evt.getCurrentTarget();
            System.out.println("current clicked element  = " + _currentClickedElement.getId());
            
     
            
          if(_currentClickedElement == _textbox1 || _currentClickedElement == _textbox2 || _currentClickedElement == _textbox3) 
          {  
            
            
            
            if ((_currentClickedElement.getId()).equals("textbox1"))
            {
                if (_textFieldActive1 == false)
                {
                    addTextField1();
                }
            }
            else if((_currentClickedElement.getId()).equals("textbox2"))
            {
                if (_textFieldActive2 == false)
                {
                    addTextField2();
                }
            }
            else if((_currentClickedElement.getId()).equals("textbox3"))
            {
                if (_textFieldActive3 == false)
                {
                    addTextField3();
                }
            }
            
 
 
 
            
          } 
          else if(_currentClickedElement == _button)
          {
                activateSVGElement(_dialogboxbutton, this);
                _svgFormsScreen.dialogboxMode(this, true);
                _animator.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        String mess1 = "License # = ";
                        String mess2 = "First Name = ";
                        String mess3 = "Last Name = ";
                        _dialogbox.setTrait("display", "inline");
                        String itemName1 = "";
                        String itemName2 = "";
                        String itemName3 = "";
                        
                        itemName1 = _textbox_text1.getTrait("#text");
                        itemName2 = _textbox_text2.getTrait("#text");
                        itemName3 = _textbox_text3.getTrait("#text");
                        
                        mess1 = mess1 + itemName1;
                        mess2 = mess2 + itemName2;
                        mess3 = mess3 + itemName3;
                        
                        //item = item + itemName;
                        //quantity = quantity + _combobox_currentItem_text.getTrait("#text") + "  ";
                        _dialogbox_text1.setTrait("#text", mess1);
                        _dialogbox_text2.setTrait("#text", mess2);
                        _dialogbox_text3.setTrait("#text", mess3);
                    }
                });
          }
          else if(_currentClickedElement == _dialogboxbutton)
          {
                deActivateSVGElement(_dialogboxbutton, this);
                _svgFormsScreen.dialogboxMode(this, false);
                _animator.invokeLater(new Runnable()
                {
                    public void run()
                    {
                    _dialogbox.setTrait("display", "none");
                    }
                });
          } 
          
          
          
          
          
            
        }
    }
}

 

 

Please use plain text.