Java/GWT/MouseListener

Материал из Java эксперт
Перейти к: навигация, поиск

implements SourcesMouseEvents

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SourcesMouseEvents;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.rpc.IsSerializable;
public class GWTClient implements EntryPoint{
  public void onModuleLoad() {
    final UserWidget userWidget = new UserWidget();
    userWidget.addMouseListener(new MouseListenerAdapter() {            
       public void onMouseEnter(Widget sender)
       {
           userWidget.addStyleName("userWidget-focus");
       }
       public void onMouseLeave(Widget sender)
       {
           userWidget.removeStyleName("userWidget-focus");               
       }
    });
    RootPanel.get().add(userWidget);
  }
}
class UserWidget extends VerticalPanel implements SourcesMouseEvents {
    TextBox firstName = new TextBox();
    TextBox lastName = new TextBox();
    TextBox address = new TextBox();
    TextBox city = new TextBox();
    TextBox postalCode = new TextBox();
    ListBox country = new ListBox();
    ListBox stateProvince = new ListBox();
    TextArea comments = new TextArea();
    CheckBox sendMeSpam = new CheckBox();
    RadioButton htmlSpam = new RadioButton("radioGroup", "HTML");
    RadioButton textSpam = new RadioButton("radioGroup", "Plain Text");
    Grid table = new Grid(11, 2);
    Button submit = new Button("Submit");
    User user = new User();
    MouseListenerCollection listeners;
    public UserWidget() {
        super();
        this.setStyleName("userWidget");
        postalCode.addKeyboardListener(new KeyboardListenerAdapter() {
                public void onKeyPress(Widget sender, char keyCode,
                    int modifiers) {
                    if (!Character.isDigit(keyCode)) {
                        ((TextBox) sender).cancelKey();
                    }
                }
            });
        
        country.addItem("United States", "US");
        
        stateProvince.addItem("Alabama", "AL");
        stateProvince.addItem("Alaska", "AK");
        stateProvince.addItem("American Samoa", "AS");
        
        htmlSpam.setChecked(true);
        VerticalPanel spamChoicePanel = new VerticalPanel();
        spamChoicePanel.add(htmlSpam);
        spamChoicePanel.add(textSpam);
        
        submit.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    user.firstName = firstName.getText();
                    user.lastName = lastName.getText();
                    user.address = address.getText();
                    user.city = city.getText();
                    user.postalCode = postalCode.getText();
                    user.country = country.getValue(country.getSelectedIndex());
                    user.stateProvince = stateProvince.getValue(stateProvince.getSelectedIndex());
                    user.ruments = comments.getText();
                    user.sendMeSpam = sendMeSpam.isChecked();
                    if (htmlSpam.isChecked()) {
                        user.htmlSpam = true;
                    }
                    Window.alert("User is currently set as - \n" +
                        user.toString());
                }
            });
        table.setWidget(0, 0, new Label("First Name"));
        table.setWidget(0, 1, firstName);
        table.setWidget(1, 0, new Label("Last Name"));
        table.setWidget(1, 1, lastName);
        table.setWidget(2, 0, new Label("Address"));
        table.setWidget(2, 1, address);
        table.setWidget(3, 0, new Label("City"));
        table.setWidget(3, 1, city);
        table.setWidget(4, 0, new Label("State/Province"));
        table.setWidget(4, 1, stateProvince);
        table.setWidget(5, 0, new Label("PostalCode/Zip"));
        table.setWidget(5, 1, postalCode);
        table.setWidget(6, 0, new Label("Country"));
        table.setWidget(6, 1, country);
        table.setWidget(7, 0, new Label("Comments"));
        table.setWidget(7, 1, comments);
        table.setWidget(8, 0, new Label("Sign Up For Our Email List!"));
        table.setWidget(8, 1, sendMeSpam);
        table.setWidget(9, 0, new Label("Email Format:"));
        table.setWidget(9, 1, spamChoicePanel);
        table.setWidget(10, 0, submit);
        table.setWidget(10, 1, null);
        Label label = new Label("Enter User Information");
        this.add(label);
        this.add(table);
        sinkEvents(Event.MOUSEEVENTS);
    }
    public void onBrowserEvent(Event event) {
        int eventType = DOM.eventGetType(event);
        switch (eventType) {
        case Event.ONMOUSEOVER: 
            if (listeners != null) {
                listeners.fireMouseEnter(this);
            }
            break;
        case Event.ONMOUSEOUT:
            if (listeners != null) {
                listeners.fireMouseLeave(this);
            }
            break;        
        }
    }
    public void addMouseListener(MouseListener listener) {
        if (listeners == null) {
            listeners = new MouseListenerCollection();
        }
        listeners.add(listener);
    }
    public void removeMouseListener(MouseListener listener) {
        if (listeners != null) {
            listeners.remove(listener);
        }
    }
}
class User implements IsSerializable {
    String firstName;
    String lastName;
    String address;
    String city;
    String stateProvince;
    String postalCode;
    String country;
    String comments;
    boolean sendMeSpam;
    boolean htmlSpam;    
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(" User:");
        sb.append(" [firstName] " + firstName + "\n");
        sb.append(" [lastName] " + lastName + "\n");
        sb.append(" [address] " + address + "\n");
        sb.append(" [city] " + city + "\n");
        sb.append(" [stateProvince] " + stateProvince + "\n");
        sb.append(" [postalCode] " + postalCode + "\n");
        sb.append(" [country] " + country + "\n");
        sb.append(" [comments] " + comments + "\n");
        sb.append(" [sendMeSpam] " + sendMeSpam + "\n");
        sb.append(" [htmlSpam] " + htmlSpam + "\n");
        return sb.toString();
    }
}





Work with mouse Listener

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class GWTClient implements EntryPoint{
     /**
    * This is the entry point method.
    */
   public void onModuleLoad() {
        // Create Label widget
      final Label roller = new Label("Default value");
        // Associate HTML element to GWT widget
      RootPanel.get().add(roller);
        // Add mouse lister to label widget
      roller.addMouseListener(new MouseListener() {
         public void onMouseEnter(Widget sender) {
            roller.setText("Entered element...");
         }
         public void onMouseLeave(Widget sender) {
            roller.setText("Leaving element...");
         }
         // Do nothing
         public void onMouseDown(Widget sender, int x, int y) {}
         // Do nothing
         public void onMouseMove(Widget sender, int x, int y) {}
         // Do nothing
         public void onMouseUp(Widget sender, int x, int y) {}
      });
   }
}