Search in sources :

Example 1 with DataChangedListener

use of com.codename1.ui.events.DataChangedListener in project CodenameOne by codenameone.

the class HTMLEventsListener method deregisterAll.

/**
 * Deregisters all the listeners, happens before a new page is loaded
 */
void deregisterAll() {
    for (Enumeration e = comps.keys(); e.hasMoreElements(); ) {
        Component cmp = (Component) e.nextElement();
        cmp.removeFocusListener(this);
        if (cmp instanceof Button) {
            // catches Button, CheckBox, RadioButton
            ((Button) cmp).removeActionListener(this);
        } else if (cmp instanceof List) {
            // catches ComboBox
            ((List) cmp).removeSelectionListener((SelectionListener) listeners.get(cmp));
        } else if (cmp instanceof TextArea) {
            ((TextArea) cmp).removeActionListener(this);
            if (cmp instanceof TextField) {
                ((TextField) cmp).removeDataChangeListener((DataChangedListener) listeners.get(cmp));
            }
        }
    }
    comps = new Hashtable();
    listeners = new Hashtable();
}
Also used : Enumeration(java.util.Enumeration) RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button) TextArea(com.codename1.ui.TextArea) Hashtable(java.util.Hashtable) TextField(com.codename1.ui.TextField) List(com.codename1.ui.List) Component(com.codename1.ui.Component) SelectionListener(com.codename1.ui.events.SelectionListener)

Example 2 with DataChangedListener

use of com.codename1.ui.events.DataChangedListener in project CodenameOne by codenameone.

the class HTMLEventsListener method registerComponent.

/**
 * Registeres the specified component/element duo to listen to all available events
 *
 * @param cmp The actual component
 * @param element The element representing the component
 */
void registerComponent(final Component cmp, final HTMLElement element) {
    comps.put(cmp, element);
    cmp.addFocusListener(this);
    if (cmp instanceof Button) {
        // catches Button, CheckBox, RadioButton
        ((Button) cmp).addActionListener(this);
    } else if (cmp instanceof List) {
        // catches ComboBox
        final List list = (List) cmp;
        list.addActionListener(this);
        SelectionListener sl = new // We create a listener and not listen ourself since the listener's method does not pass the event origin, so we need to make one listener per component
        SelectionListener() {

            public void selectionChanged(int oldSelected, int newSelected) {
                if (htmlC.getHTMLCallback() != null) {
                    htmlC.getHTMLCallback().selectionChanged(oldSelected, newSelected, htmlC, list, element);
                }
            }
        };
        list.addSelectionListener(sl);
        listeners.put(cmp, sl);
    } else if (cmp instanceof TextArea) {
        ((TextArea) cmp).addActionListener(this);
        if (cmp instanceof TextField) {
            final TextField tf = (TextField) cmp;
            DataChangedListener dcl = new // We create a listener and not listen ourself since the listener's method does not pass the event origin, so we need to make one listener per component
            DataChangedListener() {

                public void dataChanged(int type, int index) {
                    element.setAttributeById(HTMLElement.ATTR_VALUE, tf.getText());
                    if (htmlC.getHTMLCallback() != null) {
                        htmlC.getHTMLCallback().dataChanged(type, index, htmlC, tf, element);
                    }
                }
            };
            tf.addDataChangedListener(dcl);
            listeners.put(cmp, dcl);
        }
    }
}
Also used : RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button) TextArea(com.codename1.ui.TextArea) TextField(com.codename1.ui.TextField) DataChangedListener(com.codename1.ui.events.DataChangedListener) List(com.codename1.ui.List) SelectionListener(com.codename1.ui.events.SelectionListener)

Example 3 with DataChangedListener

use of com.codename1.ui.events.DataChangedListener in project CodenameOne by codenameone.

the class ImageViewer method setImageList.

/**
 * By providing this optional list of images you can allows swiping between multiple images
 *
 * @param model a list of images
 */
public void setImageList(ListModel<Image> model) {
    if (model == null || model.getSize() == 0) {
        return;
    }
    if (image == null) {
        image = model.getItemAt(0);
    }
    if (swipeableImages != null) {
        swipeableImages.removeDataChangedListener(listListener);
        swipeableImages.removeSelectionListener((SelectionListener) listListener);
        model.addDataChangedListener(listListener);
        model.addSelectionListener((SelectionListener) listListener);
    } else {
        class Listener implements SelectionListener, DataChangedListener {

            public void selectionChanged(int oldSelected, int newSelected) {
                if (selectLock) {
                    return;
                }
                if (swipeableImages.getSize() > 0 && newSelected > -1 && newSelected < swipeableImages.getSize()) {
                    setImage(swipeableImages.getItemAt(newSelected));
                }
            }

            public void dataChanged(int type, int index) {
                if (swipeableImages.getSize() > 0 && swipeableImages.getSelectedIndex() > -1 && swipeableImages.getSelectedIndex() < swipeableImages.getSize()) {
                    setImage(swipeableImages.getItemAt(swipeableImages.getSelectedIndex()));
                }
            }
        }
        listListener = new Listener();
        model.addDataChangedListener(listListener);
        model.addSelectionListener((SelectionListener) listListener);
    }
    this.swipeableImages = model;
}
Also used : SelectionListener(com.codename1.ui.events.SelectionListener) DataChangedListener(com.codename1.ui.events.DataChangedListener) DataChangedListener(com.codename1.ui.events.DataChangedListener) SelectionListener(com.codename1.ui.events.SelectionListener)

Aggregations

SelectionListener (com.codename1.ui.events.SelectionListener)3 Button (com.codename1.ui.Button)2 List (com.codename1.ui.List)2 RadioButton (com.codename1.ui.RadioButton)2 TextArea (com.codename1.ui.TextArea)2 TextField (com.codename1.ui.TextField)2 DataChangedListener (com.codename1.ui.events.DataChangedListener)2 Component (com.codename1.ui.Component)1 Enumeration (java.util.Enumeration)1 Hashtable (java.util.Hashtable)1