View Javadoc

1   package org.kuali.student.common.ui.client.widgets.table.scroll;
2   
3   import com.google.gwt.event.dom.client.BlurEvent;
4   import com.google.gwt.event.dom.client.BlurHandler;
5   import com.google.gwt.event.dom.client.ChangeEvent;
6   import com.google.gwt.event.dom.client.ChangeHandler;
7   import com.google.gwt.event.dom.client.ClickEvent;
8   import com.google.gwt.event.dom.client.ClickHandler;
9   import com.google.gwt.event.dom.client.HasChangeHandlers;
10  import com.google.gwt.event.dom.client.HasClickHandlers;
11  import com.google.gwt.event.shared.HandlerRegistration;
12  import com.google.gwt.user.client.ui.HasText;
13  import com.google.gwt.user.client.ui.Label;
14  import com.google.gwt.user.client.ui.SimplePanel;
15  import com.google.gwt.user.client.ui.TextBox;
16  
17  public class EditableLabel extends SimplePanel implements HasClickHandlers,  HasText, HasChangeHandlers {
18  	Label label = new Label();
19  	TextBox textBox = new TextBox();
20  
21  	public EditableLabel() {
22  		super.setWidget(label);
23  		textBox.addChangeHandler(new ChangeHandler() {
24  			@Override
25  			public void onChange(ChangeEvent event) {
26  				label.setText(textBox.getText());
27  			}
28  		});
29  		textBox.addClickHandler(new ClickHandler(){
30  			@Override
31  			public void onClick(ClickEvent event) {
32  				event.stopPropagation();
33  			}
34  			
35  		});
36  		textBox.addBlurHandler(new BlurHandler() {
37  			@Override
38  			public void onBlur(BlurEvent event) {
39  				label.setText(textBox.getText());
40  				EditableLabel.this.setWidget(label);
41  					
42  			}
43  		});
44  		label.addClickHandler(new ClickHandler() {
45  			@Override
46  			public void onClick(ClickEvent event) {
47  				textBox.setText(label.getText());
48  					int width = label.getOffsetWidth();
49  					int height = label.getOffsetHeight()+3;
50  					EditableLabel.this.setWidget(textBox);
51  					textBox.setWidth(""+width+"px");
52  					textBox.setHeight(""+height+"px");
53  					textBox.selectAll();
54  					textBox.setFocus(true);
55  			}
56  		});
57  	}
58  
59  	@Override
60  	public String getText() {
61  		return label.getText();
62  
63  	}
64  
65  	@Override
66  	public void setText(String text) {
67  		label.setText(text);
68  	}
69  
70  	@Override
71  	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
72  		return textBox.addChangeHandler(handler);
73  	}
74  
75  	@Override
76  	public HandlerRegistration addClickHandler(ClickHandler handler) {
77  		return label.addClickHandler(handler);
78  	}
79  }