1 |
37027
|
stefania.m
|
package eu.dnetlib.client.widgets;
|
2 |
|
|
|
3 |
|
|
import com.github.gwtbootstrap.client.ui.base.IconAnchor;
|
4 |
|
|
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
|
5 |
|
|
import com.github.gwtbootstrap.client.ui.constants.IconType;
|
6 |
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
7 |
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
8 |
|
|
import com.google.gwt.user.client.ui.FlowPanel;
|
9 |
|
|
import com.google.gwt.user.client.ui.IsWidget;
|
10 |
|
|
import com.google.gwt.user.client.ui.Label;
|
11 |
|
|
import com.google.gwt.user.client.ui.Widget;
|
12 |
|
|
import eu.dnetlib.goldoa.domain.*;
|
13 |
|
|
|
14 |
|
|
/**
|
15 |
|
|
* Created by stefania on 5/7/15.
|
16 |
|
|
*/
|
17 |
|
|
public class CoFunderInfoWidget implements IsWidget {
|
18 |
|
|
|
19 |
37073
|
stefania.m
|
private boolean isValid = true;
|
20 |
|
|
|
21 |
37027
|
stefania.m
|
private FlowPanel coFunderInfoPanel = new FlowPanel();
|
22 |
|
|
|
23 |
|
|
private AutoCompleteWidget coFunderAutoComplete = new AutoCompleteWidget("funder", "Search or insert funder");
|
24 |
|
|
private TextBox percentageTextBox = new TextBox();
|
25 |
|
|
private IconAnchor deleteIcon = new IconAnchor();
|
26 |
|
|
private Label percentageLabel = new Label();
|
27 |
|
|
|
28 |
37073
|
stefania.m
|
private Label errorLabel = new Label();
|
29 |
37027
|
stefania.m
|
|
30 |
|
|
private Vocabulary funderVocabulary = null;
|
31 |
|
|
|
32 |
|
|
private DeleteCoFunderListener deleteCoFunderListener;
|
33 |
|
|
|
34 |
|
|
public CoFunderInfoWidget() {
|
35 |
|
|
|
36 |
|
|
coFunderInfoPanel.addStyleName("coFunderInfoPanel");
|
37 |
|
|
|
38 |
|
|
AutoCompleteWidget.AutoCompleteListener organizationAutoCompleteListener = new AutoCompleteWidget.AutoCompleteListener() {
|
39 |
|
|
@Override
|
40 |
|
|
public void valueSelected(Vocabulary vocabulary) {
|
41 |
|
|
funderVocabulary = vocabulary;
|
42 |
|
|
}
|
43 |
|
|
};
|
44 |
|
|
coFunderAutoComplete.setAutoCompleteListener(organizationAutoCompleteListener);
|
45 |
|
|
coFunderAutoComplete.addStyleName("inlineBlock");
|
46 |
|
|
|
47 |
|
|
percentageTextBox.setAlternateSize(AlternateSize.MINI);
|
48 |
|
|
percentageTextBox.addStyleName("inlineBlock");
|
49 |
|
|
|
50 |
|
|
percentageLabel.setText(" %");
|
51 |
|
|
percentageLabel.addStyleName("inlineBlock");
|
52 |
|
|
|
53 |
|
|
deleteIcon.setIcon(IconType.REMOVE);
|
54 |
|
|
deleteIcon.addStyleName("inlineBlock");
|
55 |
|
|
deleteIcon.addStyleName("deleteIcon");
|
56 |
|
|
deleteIcon.addClickHandler(new ClickHandler() {
|
57 |
|
|
@Override
|
58 |
|
|
public void onClick(ClickEvent clickEvent) {
|
59 |
|
|
|
60 |
|
|
if(deleteCoFunderListener !=null)
|
61 |
|
|
deleteCoFunderListener.deleteCoFunder();
|
62 |
|
|
}
|
63 |
|
|
});
|
64 |
|
|
|
65 |
37073
|
stefania.m
|
errorLabel.addStyleName("fontItalic");
|
66 |
|
|
errorLabel.addStyleName("colorRed");
|
67 |
|
|
|
68 |
37027
|
stefania.m
|
coFunderInfoPanel.add(coFunderAutoComplete);
|
69 |
|
|
coFunderInfoPanel.add(percentageTextBox);
|
70 |
|
|
coFunderInfoPanel.add(percentageLabel);
|
71 |
|
|
coFunderInfoPanel.add(deleteIcon);
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
@Override
|
75 |
|
|
public Widget asWidget() {
|
76 |
|
|
return coFunderInfoPanel;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
public interface DeleteCoFunderListener {
|
80 |
|
|
public void deleteCoFunder();
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
public void setDeleteCoFunderListener(DeleteCoFunderListener deleteCoFunderListener) {
|
84 |
|
|
this.deleteCoFunderListener = deleteCoFunderListener;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
37073
|
stefania.m
|
public RequestCoFunder getCoFunder() {
|
88 |
37027
|
stefania.m
|
|
89 |
37073
|
stefania.m
|
isValid = true;
|
90 |
|
|
coFunderInfoPanel.remove(errorLabel);
|
91 |
37027
|
stefania.m
|
|
92 |
37073
|
stefania.m
|
if(coFunderAutoComplete.getValue().trim().equals("") && percentageTextBox.getValue().trim().equals(""))
|
93 |
|
|
return null;
|
94 |
37027
|
stefania.m
|
|
95 |
37073
|
stefania.m
|
if(!isComplete()) {
|
96 |
|
|
isValid = false;
|
97 |
|
|
errorLabel.setText("Both fields are required");
|
98 |
|
|
coFunderInfoPanel.add(errorLabel);
|
99 |
|
|
return new RequestCoFunder();
|
100 |
|
|
}
|
101 |
37027
|
stefania.m
|
|
102 |
|
|
if(isComplete()) {
|
103 |
|
|
|
104 |
|
|
RequestCoFunder requestCoFunder = new RequestCoFunder();
|
105 |
|
|
|
106 |
37073
|
stefania.m
|
if(funderVocabulary!=null && funderVocabulary.getName().equals(coFunderAutoComplete.getValue())) {
|
107 |
|
|
Funder funder = new Funder();
|
108 |
|
|
funder.setName(funderVocabulary.getName());
|
109 |
|
|
funder.setId(funderVocabulary.getId());
|
110 |
|
|
requestCoFunder.setFunder(funder);
|
111 |
|
|
} else {
|
112 |
|
|
Funder funder = new Funder();
|
113 |
|
|
funder.setName(coFunderAutoComplete.getValue());
|
114 |
|
|
}
|
115 |
|
|
|
116 |
37027
|
stefania.m
|
String percentage = percentageTextBox.getValue().trim();
|
117 |
|
|
if(!isPercentage(percentage)) {
|
118 |
|
|
|
119 |
37073
|
stefania.m
|
isValid = false;
|
120 |
|
|
errorLabel.setText("Not valid percentage");
|
121 |
|
|
coFunderInfoPanel.add(errorLabel);
|
122 |
37027
|
stefania.m
|
|
123 |
|
|
} else {
|
124 |
|
|
requestCoFunder.setPercentage(Float.parseFloat(percentageTextBox.getValue().trim()));
|
125 |
|
|
|
126 |
|
|
}
|
127 |
|
|
|
128 |
37073
|
stefania.m
|
return requestCoFunder;
|
129 |
37027
|
stefania.m
|
}
|
130 |
|
|
|
131 |
|
|
return null;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
public void clear() {
|
135 |
|
|
|
136 |
37073
|
stefania.m
|
funderVocabulary = null;
|
137 |
37027
|
stefania.m
|
coFunderAutoComplete.setValue("");
|
138 |
|
|
percentageTextBox.setValue("");
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
public void loadCoFunder(RequestCoFunder coFunder) {
|
142 |
|
|
|
143 |
|
|
if(coFunder.getFunder()!=null) {
|
144 |
|
|
funderVocabulary = new Vocabulary();
|
145 |
|
|
funderVocabulary.setId(coFunder.getFunder().getId());
|
146 |
|
|
funderVocabulary.setName(coFunder.getFunder().getName());
|
147 |
|
|
coFunderAutoComplete.setValue(coFunder.getFunder().getName());
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
percentageTextBox.setValue(coFunder.getPercentage() + "");
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
private boolean isComplete() {
|
154 |
|
|
|
155 |
37073
|
stefania.m
|
if(!coFunderAutoComplete.getValue().trim().equals("") && !percentageTextBox.getValue().trim().equals(""))
|
156 |
|
|
return true;
|
157 |
|
|
|
158 |
37027
|
stefania.m
|
return false;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
private boolean isPercentage(String percentage) {
|
162 |
|
|
|
163 |
|
|
try {
|
164 |
|
|
|
165 |
|
|
Float percentageValue = Float.parseFloat(percentage);
|
166 |
|
|
if(percentageValue>=0 && percentageValue<=100)
|
167 |
|
|
return true;
|
168 |
|
|
|
169 |
|
|
} catch(NumberFormatException nfe) {
|
170 |
|
|
return false;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
return false;
|
174 |
|
|
}
|
175 |
37073
|
stefania.m
|
|
176 |
|
|
public boolean isValid() {
|
177 |
|
|
return isValid;
|
178 |
|
|
}
|
179 |
37027
|
stefania.m
|
}
|