Difference between revisions of "Teams Winter 2011/team1/BlackBerry/Add Elements to Main Screen"

From CDOT Wiki
Jump to: navigation, search
Line 7: Line 7:
 
     private String email;
 
     private String email;
 
  </pre>
 
  </pre>
3.3. Add an array in <code>MyScreen</code> class to work with data.<br/>
+
3.3. Add three argument constructor:
3.4. The following elements are added to the <code>MyScreen()</code> constructor. Add a text field: <br/>
 
 
  <pre>
 
  <pre>
    AutoTextEditField atf = new AutoTextEditField("Student Name: ", "");
+
  public Student(String firstName, String lastName, String email) {
 +
        this.firstName = firstName;
 +
        this.lastName = lastName;
 +
        this.email = email;
 +
    }
 
  </pre>
 
  </pre>
3.5. Set the border and background for the text field:
+
3.4. Add getter method for e-mail and override the <code>toString()</code> from the Object class(it will be used to implement <code>Comparator</code> interface):
 
  <pre>
 
  <pre>
    Border roundedBorder = BorderFactory.createRoundedBorder(new XYEdges(5, 5, 5, 5), Border.STYLE_SOLID);
+
  public String getEmail() {
     Background solidBackground = BackgroundFactory.createSolidBackground(Color.LIGHTSTEELBLUE);
+
        return email;
     atf.setBorder(roundedBorder);
+
     }
    atf.setBackground(solidBackground);
+
 
     add(atf); 
+
     public String toString() {
 +
        return firstName + " " + lastName;
 +
     }
 
  </pre>
 
  </pre>
3.6. Add a search button:
+
3.5. Add class that will hold all Student objects and make necessary manipulations:
 
  <pre>
 
  <pre>
     ButtonField _bf=new ButtonField("Search");
+
  public class StudentList extends SortedReadableList implements KeywordProvider {
     add(_bf);
+
 
 +
    public StudentList(Vector students) {
 +
        super(new StudentListComparator()); 
 +
        loadFrom(students.elements()); 
 +
    }
 +
 
 +
    /**
 +
    * Adds a new element to the list.
 +
    * @param element The element to be added.
 +
    */
 +
    void addElement(Object element) {
 +
        doAdd(element);
 +
     }
 +
 
 +
    /**
 +
    * A Comparator class used for sorting our Student objects by name.
 +
    */
 +
    final static class StudentListComparator implements Comparator { 
 +
        /**
 +
        * Compares two students by comparing their names in alphabetical order.
 +
        * @see net.rim.device.api.util.Comparator#compare(Object, Object)
 +
        */
 +
        public int compare(Object o1, Object o2) {
 +
            if (o1 == null || o2 == null) {
 +
                throw new IllegalArgumentException("Cannot compare null students");
 +
            }
 +
            return o1.toString().compareTo(o2.toString());
 +
        }     
 +
     }
 +
 
 +
    public String[] getKeywords(Object element) {
 +
        if(element instanceof Student )
 +
        {         
 +
            return StringUtilities.stringToWords(element.toString());
 +
        }
 +
        return null;
 +
    } 
 +
}
 +
 
 
  </pre>
 
  </pre>
3.7. Add the array of students to the list field:
+
 
 +
3.6. The following elements are added to the <code>MyScreen()</code> constructor:<br/>
 
  <pre>
 
  <pre>
  ObjectListField olf = new ObjectListField();
+
    // A reference to the UiApplication instance for use in this class
  for (int i=0; i< ls.length; i++) {
+
    _app = app;
      olf.insert(i, ls[i].getFirstName() + " " + ls[i].getLastName());
+
    // Obtain a reference to the UiApplication's KeywordFilterField     
  }      
+
     _keywordFilterField = _app.getKeywordFilterField();
  add(olf);
 
 
  </pre>
 
  </pre>
 +
 +
3.7. Other elements are added inside the <code>ViewStudentApp</code> class. Add necessary private fields:
 +
<pre>
 +
    private KeywordFilterField _keywordFilterField; 
 +
    private StudentList _studentList;
 +
    private Vector _students;
 +
</pre>
 +
3.8. Add search field and students list:
 +
<pre>
 +
  MyScreen _mainScreen = new MyScreen(this);
 +
 +
  _students = getFromList();
 +
  _studentList = new StudentList(_students);
 +
 +
    // Add our list to a KeywordFilterField object.
 +
    _keywordFilterField = new KeywordFilterField();                 
 +
    _keywordFilterField.setSourceList(_studentList, _studentList);
 +
 +
  _mainScreen.setTitle(_keywordFilterField.getKeywordField());
 +
    _mainScreen.add(_keywordFilterField);
 +
</pre>
 +
 +
3.8. Add inner class for custom search field:
 +
<pre>
 +
  final static class CustomKeywordField extends BasicEditField { 
 +
        // Constructor
 +
        CustomKeywordField() {
 +
            // Custom style.
 +
            super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE);
 +
            setLabel("Search: ");
 +
        }
 +
 +
        /**
 +
        * Intercepts ESCAPE key.
 +
        * @see net.rim.device.api.ui.component.TextField#keyChar(char,int,int)
 +
        */
 +
        protected boolean keyChar(char ch, int status, int time) {
 +
            switch(ch) {
 +
            case Characters.ESCAPE:
 +
                // Clear keyword.
 +
                if(super.getTextLength() > 0)
 +
                {
 +
                    setText("");                     
 +
                    return true;
 +
                }
 +
            }             
 +
            return super.keyChar(ch, status, time);
 +
        }                   
 +
 +
        /**
 +
        * Overriding super to add custom painting to our class.
 +
        * @see net.rim.device.api.ui.Field#paint(Graphics)
 +
        */
 +
        protected void paint(Graphics graphics)
 +
        {         
 +
            super.paint(graphics);
 +
            // Draw caret.
 +
            getFocusRect(new XYRect());
 +
            drawFocus(graphics, true);                       
 +
        }
 +
    }
 +
</pre>
 +
3.9.

Revision as of 13:02, 21 March 2011

3. Add Elements to Main (First) Screen

3.1. Open MyScreen.java class.
3.2. Create class Student with the following fields:

    private String firstName;
    private String lastName;
    private String email;
 

3.3. Add three argument constructor:

  public Student(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }
 

3.4. Add getter method for e-mail and override the toString() from the Object class(it will be used to implement Comparator interface):

   public String getEmail() {
        return email;
    }
   
    public String toString() {
        return firstName + " " + lastName;
    }
 

3.5. Add class that will hold all Student objects and make necessary manipulations:

  public class StudentList extends SortedReadableList implements KeywordProvider {

    public StudentList(Vector students) {
        super(new StudentListComparator());   
        loadFrom(students.elements());  
    }

    /**
     * Adds a new element to the list.
     * @param element The element to be added.
     */
    void addElement(Object element) {
        doAdd(element);
    } 

    /**
     * A Comparator class used for sorting our Student objects by name.
     */
    final static class StudentListComparator implements Comparator {  
        /**
         * Compares two students by comparing their names in alphabetical order.
         * @see net.rim.device.api.util.Comparator#compare(Object, Object)
         */
        public int compare(Object o1, Object o2) {
            if (o1 == null || o2 == null) {
                throw new IllegalArgumentException("Cannot compare null students");
            }
            return o1.toString().compareTo(o2.toString());
        }       
    }

    public String[] getKeywords(Object element) {
        if(element instanceof Student )
        {           
            return StringUtilities.stringToWords(element.toString());
        } 
        return null;
    }  
}

 

3.6. The following elements are added to the MyScreen() constructor:

     // A reference to the UiApplication instance for use in this class
     _app = app;
     // Obtain a reference to the UiApplication's KeywordFilterField       
     _keywordFilterField = _app.getKeywordFilterField();
 

3.7. Other elements are added inside the ViewStudentApp class. Add necessary private fields:

    private KeywordFilterField _keywordFilterField;  
    private StudentList _studentList;
    private Vector _students;
 

3.8. Add search field and students list:

   MyScreen _mainScreen = new MyScreen(this);

   _students = getFromList();
   _studentList = new StudentList(_students);

    // Add our list to a KeywordFilterField object.
    _keywordFilterField = new KeywordFilterField();                  
    _keywordFilterField.setSourceList(_studentList, _studentList); 

   _mainScreen.setTitle(_keywordFilterField.getKeywordField()); 
    _mainScreen.add(_keywordFilterField);
 

3.8. Add inner class for custom search field:

  final static class CustomKeywordField extends BasicEditField {  
        // Constructor
        CustomKeywordField() {
            // Custom style.
            super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE);
            setLabel("Search: ");
        }

        /**
         * Intercepts ESCAPE key.
         * @see net.rim.device.api.ui.component.TextField#keyChar(char,int,int)
         */
        protected boolean keyChar(char ch, int status, int time) {
            switch(ch) {
            case Characters.ESCAPE:
                // Clear keyword.
                if(super.getTextLength() > 0)
                {
                    setText("");                       
                    return true;
                }
            }               
            return super.keyChar(ch, status, time);
        }                    

        /**
         * Overriding super to add custom painting to our class.
         * @see net.rim.device.api.ui.Field#paint(Graphics)
         */
        protected void paint(Graphics graphics)
        {           
            super.paint(graphics);
            // Draw caret.
            getFocusRect(new XYRect());
            drawFocus(graphics, true);                         
        }
    }
 

3.9.