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
}

Also available in: Unified diff