Project

General

Profile

« Previous | Next » 

Revision 2921c66d

Added by Andreas Kohlbecker about 7 years ago

#6169 custom TimePeriod field with working parser field

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/component/TimePeriodField.java
9 9
package eu.etaxonomy.cdm.vaadin.component;
10 10

  
11 11
import com.vaadin.data.fieldgroup.BeanFieldGroup;
12
import com.vaadin.data.util.BeanItem;
13
import com.vaadin.event.FieldEvents.TextChangeEvent;
12 14
import com.vaadin.ui.Alignment;
13 15
import com.vaadin.ui.Component;
14 16
import com.vaadin.ui.CssLayout;
......
18 20
import com.vaadin.ui.TextField;
19 21

  
20 22
import eu.etaxonomy.cdm.model.common.TimePeriod;
23
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
21 24
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
22 25

  
23 26
/**
......
35 38

  
36 39
    TextField parseField = null;
37 40

  
41
    TextField freeText = null;
42

  
43
    Label toLabel = null;
44

  
38 45
    GridLayout grid = new GridLayout(3, 3);
39 46

  
40 47
    /**
......
63 70
        parseField = new TextField();
64 71
        parseField.setWidth(100, Unit.PERCENTAGE);
65 72
        parseField.setInputPrompt("This field will parse the entered time period");
73
        parseField.addTextChangeListener(e -> parseInput(e));
66 74

  
67 75
        PartialDateField startDate = new PartialDateField("Start");
68 76
        PartialDateField endDate = new PartialDateField("End");
69
        TextField freeText = new TextField("FreeText");
77
        freeText = new TextField("FreeText");
70 78
        freeText.setWidth(100, Unit.PERCENTAGE);
71 79

  
72 80
        fieldGroup.bind(startDate, "start");
73 81
        fieldGroup.bind(endDate, "end");
74 82
        fieldGroup.bind(freeText, "freeText");
75 83

  
76
        Label dashLabel = new Label("-");
84
        toLabel = new Label("\u2014"); // EM DASH : 0x2014
77 85

  
78 86
        int row = 0;
79 87
        grid.addComponent(parseField, 0, row, 2, row);
80 88
        row++;
81 89
        grid.addComponent(startDate, 0, row);
82
        grid.addComponent(dashLabel, 1, row);
83
        grid.setComponentAlignment(dashLabel, Alignment.BOTTOM_CENTER);
90
        grid.addComponent(toLabel, 1, row);
91
        grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
84 92
        grid.addComponent(endDate, 2, row);
85 93
        row++;
86 94
        grid.addComponent(freeText, 0, row, 2, row);
......
98 106

  
99 107

  
100 108

  
109
    /**
110
     * @param e
111
     * @return
112
     */
113
    private void parseInput(TextChangeEvent e) {
114
        if(!e.getText().isEmpty()){
115
            TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
116
            fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
117
        }
118
    }
119

  
101 120
    /**
102 121
     *
103 122
     */
104 123
    private void applyDefaultStyles() {
105 124
        if(parseField != null) {
106 125
            parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
126
            toLabel.addStyleName("to-label");
107 127
        }
108 128

  
109 129
    }
......
111 131
    @Override
112 132
    protected void setInternalValue(TimePeriod newValue) {
113 133
        super.setInternalValue(newValue);
114
        fieldGroup.setItemDataSource(newValue);
134
        fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
115 135
    }
116 136

  
117 137
    @Override
......
135 155
        return TimePeriod.class;
136 156
    }
137 157

  
158

  
159

  
138 160
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/JodaTimePartialConverter.java
87 87
            try {
88 88
                sb.append(value.get(DateTimeFieldType.year()));
89 89
                try {
90
                    sb.append(GLUE).append(StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0"));
90
                    String month = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0");
91
                    sb.append(GLUE).append(month);
91 92
                    try {
92
                        sb.append(GLUE).append(StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.dayOfMonth()))), 2, "0"));
93
                        String day = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.dayOfMonth()))), 2, "0");
94
                        sb.append(GLUE).append(day);
93 95
                    } catch (IllegalArgumentException e){
94 96
                        /* IGNORE */
95 97
                    }
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationWorkflowPresenter.java
8 8
*/
9 9
package eu.etaxonomy.cdm.vaadin.view.registration;
10 10

  
11
import java.io.Serializable;
11 12
import java.util.ArrayList;
12 13
import java.util.List;
13 14

  
......
38 39
 */
39 40
@SpringComponent
40 41
@ViewScope
41
public class RegistrationWorkflowPresenter extends AbstractPresenter<RegistrationWorkflowView> {
42
public class RegistrationWorkflowPresenter extends AbstractPresenter<RegistrationWorkflowView> implements Serializable{
42 43

  
44
    private static final long serialVersionUID = 1L;
43 45

  
44 46
    @Autowired
45 47
    private RegistrationService serviceMock;
src/main/webapp/VAADIN/themes/edit-valo/custom-fields.scss
10 10
          background-color: $v-app-background-color;
11 11
          padding: round($v-unit-size / 4);
12 12
      }
13
      .to-label {
14
        padding: 0 round($v-unit-size / 2);
15
      }
13 16
  }
14 17
  
15 18
  .v-textfield-helper-field { 

Also available in: Unified diff