Guest User

Untitled

a guest
Jan 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. public class TaskEditorWidget extends Composite implements Editor<TaskProxy>, HasSelectionHandlers<FormPanel.FormLabel>{
  2.  
  3.     private static TaskEditorWidgetUiBinder uiBinder = GWT
  4.             .create(TaskEditorWidgetUiBinder.class);
  5.  
  6.     interface TaskEditorWidgetUiBinder extends UiBinder<Widget, TaskEditorWidget> {}
  7.    
  8.     @UiField protected FormPanel form;
  9.     @UiField protected TextField titleEditor;
  10.     @UiField protected TextArea descriptionEditor;
  11.    
  12.     @UiField protected PropertiesMultiSelect propertiesEditor;
  13.    
  14.     @UiField(provided=true) protected ValueListBox<AccountProxy> assignedToEditor;
  15.  
  16.     public TaskEditorWidget() {
  17.         assignedToEditor = new ValueListBox<AccountProxy>(AccountProxy.renderer , AccountProxy.keyProvider );
  18.         initWidget(uiBinder.createAndBindUi(this));
  19.     }
  20.    
  21.     public void setAccounts(List<AccountProxy> accounts){
  22.         assignedToEditor.setAcceptableValues(new ArrayList<AccountProxy>(accounts) );
  23.     }
  24.  
  25.     @Override
  26.     public HandlerRegistration addSelectionHandler(
  27.             SelectionHandler<FormLabel> handler) {
  28.         return form.addSelectionHandler(handler);
  29.     }
  30.    
  31.     public void addProperties(List<String> properties) {
  32.         for(String prop : properties){
  33.             propertiesEditor.addProperty(prop, "");
  34.         }
  35.     }
  36.  
  37.     public List<String> getPropertyNames() {
  38.         return propertiesEditor.getPropertyNames();
  39.     }
  40.    
  41.  
  42. }
  43.  
  44.  
  45. // *********************************************
  46.  
  47. public class PropertiesMultiSelect extends Composite implements IsResettable{
  48.  
  49.     private static PropertiesMultiSelectUiBinder uiBinder = GWT.create(PropertiesMultiSelectUiBinder.class);
  50.  
  51.     interface PropertiesMultiSelectUiBinder extends UiBinder<Widget, PropertiesMultiSelect> {}
  52.  
  53.     @UiField protected TextField addText;
  54.     @UiField protected Anchor addAnchor;
  55.    
  56.     @UiField protected ElementPanel tbody;
  57.    
  58.     public PropertiesMultiSelect() {
  59.         initWidget(uiBinder.createAndBindUi(this));
  60.         setStyleName("property-multiselect");
  61.     }
  62.  
  63.     public void setValues(Map<String, String> values){
  64.         // TODO reset
  65.         for(String key : values.keySet()){
  66.             addProperty(key, values.get(key));
  67.         }
  68.     }
  69.  
  70.     public Map<String, String> getValues(){
  71.         HashMap<String, String> result = new HashMap<String, String>();
  72.         for(Widget w : tbody){
  73.             PropertyRow row = (PropertyRow) w;
  74.             result.put(row.getProperty(), row.getValue());
  75.         }
  76.         return result;
  77.     }
  78.    
  79.     public List<String> getPropertyNames(){
  80.         return new ArrayList<String>(getValues().keySet());
  81.     }
  82.    
  83.     public void addProperty(String property, String value){
  84.         PropertyRow row = new PropertyRow(property);
  85.         row.setValue(value);
  86.         tbody.add(row);
  87.         addText.setValue("");
  88.     }
  89.    
  90.     @UiHandler("addAnchor")
  91.     protected void addClick(ClickEvent e){
  92.         addProperty();
  93.     }
  94.    
  95.     @UiHandler("addText")
  96.     protected void addEnterKey(KeyPressEvent e){
  97.         if(e.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER){
  98.             addProperty();
  99.         }
  100.     }
  101.    
  102.     private void addProperty(){
  103.         String prop = addText.getValue().trim();
  104.         if(!prop.isEmpty() && !getValues().containsKey(prop)){
  105.             addProperty(prop, "");
  106.         }
  107.     }
  108.  
  109.     private static class PropertyRow extends Composite{
  110.        
  111.         private String property;
  112.         private TextField field;
  113.        
  114.         public PropertyRow(String propName) {
  115.             this.property = propName;
  116.            
  117.             ElementPanel row = new ElementPanel("tr");
  118.             initWidget(row);
  119.            
  120.             ElementPanel td1 = new ElementPanel("td");
  121.             td1.getElement().setInnerText(propName);
  122.             td1.setStyleName("labelColumn");
  123.             row.add(td1);
  124.            
  125.             ElementPanel td2 = new ElementPanel("td");
  126.             this.field = new TextField();
  127.             td2.add(this.field);
  128.             row.add(td2);
  129.            
  130.             ElementPanel td3 = new ElementPanel("td");
  131.             td3.setStyleName("actionColumn delete");
  132.             Anchor a = new Anchor();
  133.             a.setStyleName("");
  134.             a.addClickHandler(new ClickHandler() {
  135.                
  136.                 @Override
  137.                 public void onClick(ClickEvent event) {
  138.                     PropertyRow.this.removeFromParent();
  139.                 }
  140.             });
  141.             td3.add(a);
  142.             row.add(td3);
  143.            
  144.         }
  145.        
  146.         public String getProperty() {
  147.             return property;
  148.         }
  149.        
  150.         public String getValue(){
  151.             return field.getValue();
  152.         }
  153.        
  154.         public void setValue(String value){
  155.             field.setValue(value);
  156.         }
  157.     }
  158.  
  159.     @Override
  160.     public void reset() {
  161.         addText.setValue("");
  162.         tbody.clear();
  163.     }
  164. }
Add Comment
Please, Sign In to add comment