2020 * #L%
2121 */
2222
23- import java .beans .Beans ;
2423import java .math .BigDecimal ;
24+ import java .text .NumberFormat ;
25+ import java .util .Objects ;
2526
26- import com .vaadin .flow .component .AbstractField .ComponentValueChangeEvent ;
2727import com .vaadin .flow .component .BlurNotifier .BlurEvent ;
2828import com .vaadin .flow .component .ClickEvent ;
29+ import com .vaadin .flow .component .Component ;
2930import com .vaadin .flow .component .ComponentEvent ;
3031import com .vaadin .flow .component .ComponentEventListener ;
31- import com .vaadin .flow .component .HasValue ;
32- import com .vaadin .flow .component .Key ;
33- import com .vaadin .flow .component .ShortcutRegistration ;
3432import com .vaadin .flow .component .button .Button ;
35- import com .vaadin .flow .component .button .ButtonVariant ;
36- import com .vaadin .flow .component .html .Label ;
37- import com .vaadin .flow .component .icon .VaadinIcon ;
38- import com .vaadin .flow .component .orderedlayout .HorizontalLayout ;
3933import com .vaadin .flow .component .textfield .BigDecimalField ;
4034import com .vaadin .flow .component .textfield .TextFieldVariant ;
41- import com . vaadin . flow . shared . Registration ;
35+
4236
4337/**
4438 * Offers a simple Vaadin label which can be edited as a {@link BigDecimalField}.
4539 *
4640 * @author JohannesRabauer
4741 */
48- public class EditableLabelBigDecimalField extends HorizontalLayout
49- implements HasValue < ComponentValueChangeEvent < EditableLabelBigDecimalField , BigDecimal >, BigDecimal >
42+ public class EditableLabelBigDecimalField
43+ extends AbstractEditableLabel < Object , EditableLabelBigDecimalField , BigDecimal , BigDecimalField >
5044{
5145
5246 private BigDecimal value ;
53- private boolean readOnly = false ;
54- private boolean currency = false ;
55- private String currencySign = "€" ;
56- private Button btnEdit , btnSave , btnClose ;
57- private BigDecimalField textField ;
58- private Label label ;
47+ private NumberFormat currencyFormatter ;
5948
60- /**
61- *
62- */
6349 public EditableLabelBigDecimalField ()
6450 {
65- super ();
66- this .initUI ();
67-
68- if (!Beans .isDesignTime ())
69- {
70- this .getElement ().addEventListener ("mouseover" , c ->
71- {
72- if (this .textField .isVisible ())
73- {
74- this .btnEdit .setVisible (false );
75- }
76-
77- if (this .label .isVisible ())
78- {
79- this .btnEdit .setVisible (!this .readOnly );
80- }
81- });
82-
83- this .getElement ().addEventListener ("mouseout" , c ->
84- {
85- this .btnEdit .setVisible (false );
86- });
87-
88- }
51+ super (new BigDecimalField ());
52+ }
53+
54+ public EditableLabelBigDecimalField (final NumberFormat currencyFormatter )
55+ {
56+ this ();
57+ Objects .requireNonNull (this .currencyFormatter = currencyFormatter );
8958 }
9059
9160 @ Override
9261 public void setValue (final BigDecimal value )
9362 {
9463 if (value == null )
9564 {
96- this .label . setText ( "k.A." );
65+ this .setLabelText ( this . emptyValue );
9766 }
9867 else
9968 {
100- this .value = value ;
101- if (this .currency )
69+ if (this .currencyFormatter != null )
10270 {
103- this .label . setText ( value . toEngineeringString () + " " + this . currencySign );
71+ this .setLabelText ( this . currencyFormatter . format ( value ) );
10472 }
10573 else
10674 {
107- this .label . setText (value .toEngineeringString ());
75+ this .setLabelText (value .toPlainString ());
10876 }
10977
110- this .textField .setValue (value );
78+ this .getEditor () .setValue (value );
11179 }
80+ this .value = value ;
11281 }
11382
11483 @ Override
@@ -117,43 +86,21 @@ public BigDecimal getValue()
11786 return this .value ;
11887 }
11988
120- @ Override
121- public Registration addValueChangeListener (
122- final ValueChangeListener <? super ComponentValueChangeEvent <EditableLabelBigDecimalField , BigDecimal >> listener )
89+ public void setCurrency (final NumberFormat currencyFormatter )
12390 {
124- return this .textField .addValueChangeListener (
125- (ValueChangeListener <? super ComponentValueChangeEvent <BigDecimalField , BigDecimal >>)listener );
126- }
127-
128- @ Override
129- public void setReadOnly (final boolean readOnly )
130- {
131- this .readOnly = readOnly ;
132-
133- }
134-
135- @ Override
136- public boolean isReadOnly ()
137- {
138- return this .readOnly ;
139- }
140-
141- public void hasCurrency (final boolean isCurrency , final String currencySign )
142- {
143- this .currency = isCurrency ;
144- this .currencySign = currencySign ;
91+ this .currencyFormatter = currencyFormatter ;
14592 }
14693
14794 @ Override
14895 public void setRequiredIndicatorVisible (final boolean requiredIndicatorVisible )
14996 {
150- this .textField .setRequiredIndicatorVisible (requiredIndicatorVisible );
97+ this .getEditor () .setRequiredIndicatorVisible (requiredIndicatorVisible );
15198 }
15299
153100 @ Override
154101 public boolean isRequiredIndicatorVisible ()
155102 {
156- return this .textField .isRequiredIndicatorVisible ();
103+ return this .getEditor () .isRequiredIndicatorVisible ();
157104 }
158105
159106 /**
@@ -164,141 +111,53 @@ public boolean isRequiredIndicatorVisible()
164111 */
165112 private void textField_onBlur (final BlurEvent <BigDecimalField > event )
166113 {
167- if (this .label . getText ().contentEquals (this .textField .getValue ().toPlainString ()))
114+ if (this .getLabelText ().contentEquals (this .getEditor () .getValue ().toPlainString ()))
168115 {
169- this .textField .setVisible (false );
170- this .label .setVisible (true );
171- this .btnSave .setVisible (false );
172- this .btnClose .setVisible (false );
116+ this .disableEditMode ();
173117 }
174118 }
175119
176- /**
177- * Event handler delegate method for the {@link BigDecimalField} {@link #textField}.
178- *
179- * @eventHandlerDelegate Do NOT delete, used by UI designer!
180- * @see ValueChangeListener#valueChanged(ValueChangeEvent)
181- */
182- private void textField_valueChanged (final ComponentValueChangeEvent <BigDecimalField , BigDecimal > event )
183- {
184- this .value = this .textField .getValue ();
185- }
186-
187- /**
188- * Event handler delegate method for the {@link Button} {@link #btnEdit}.
189- *
190- * @eventHandlerDelegate Do NOT delete, used by UI designer!
191- * @see ComponentEventListener#onComponentEvent(ComponentEvent)
192- */
193- private void btnEdit_onClick (final ClickEvent <Button > event )
120+ @ Override
121+ protected void btnEdit_onClick (final ClickEvent <Button > event )
194122 {
195- this .textField .setValue (this .value );
196- this .textField .setVisible (true );
197- this .textField .focus ();
123+ this .getEditor ().setValue (this .value );
124+ this .getEditor ().focus ();
198125
199- this .label .setVisible (false );
200- this .btnEdit .setVisible (false );
201- this .btnSave .setVisible (true );
202- this .btnClose .setVisible (true );
126+ this .enableEditMode ();
203127 }
204128
205- /**
206- * Event handler delegate method for the {@link Button} {@link #btnSave}.
207- *
208- * @eventHandlerDelegate Do NOT delete, used by UI designer!
209- * @see ComponentEventListener#onComponentEvent(ComponentEvent)
210- */
211- private void btnSave_onClick (final ClickEvent <Button > event )
129+ @ Override
130+ protected void btnSave_onClick (final ClickEvent <Button > event )
212131 {
213- this .label .setText (this .textField .getValue ().toPlainString ());
214- // EditableLabelsUtil.getNextParent(this, HasGlobalSave.class).save();
215- System .out .println ("Store item" );
132+ final BigDecimal oldValue = this .value ;
133+ this .setValue (this .getEditor ().getValue ());
216134
217- this .textField .setVisible (false );
218- this .label .setVisible (true );
219- this .btnSave .setVisible (false );
220- this .btnClose .setVisible (false );
135+ this .fireChangedEvent (oldValue );
136+
137+ this .disableEditMode ();
221138 }
222139
223- /**
224- * Event handler delegate method for the {@link Button} {@link #btnClose}.
225- *
226- * @eventHandlerDelegate Do NOT delete, used by UI designer!
227- * @see ComponentEventListener#onComponentEvent(ComponentEvent)
228- */
229- private void btnClose_onClick (final ClickEvent <Button > event )
140+ @ Override
141+ protected void btnClose_onClick (final ClickEvent <Button > event )
230142 {
231- this .textField .setVisible (false );
232-
233- this .label .setVisible (true );
234-
235- this .btnSave .setVisible (false );
236-
237- this .btnClose .setVisible (false );
143+ this .disableEditMode ();
238144 }
239145
240- /* WARNING: Do NOT edit!<br>The content of this method is always regenerated by the UI designer. */
241- // <generated-code name="initUI">
242- private void initUI ()
146+ @ Override
147+ protected void initUI (
148+ final Component editIcon ,
149+ final Component saveIcon ,
150+ final Component abortIcon
151+ )
243152 {
244- this .label = new Label ();
245- this .textField = new BigDecimalField ();
246- this .btnEdit = new Button ();
247- this .btnSave = new Button ();
248- this .btnClose = new Button ();
153+ super .initUI (editIcon , saveIcon , abortIcon );
249154
250- this .setDefaultVerticalComponentAlignment (Alignment .CENTER );
251- this .label .setText ("k.A." );
252- this .textField .setAutoselect (true );
253- this .textField .setVisible (false );
254- this .textField .addThemeVariants (TextFieldVariant .LUMO_SMALL );
255- this .btnEdit .setVisible (false );
256- this .btnEdit .addThemeVariants (ButtonVariant .LUMO_SMALL , ButtonVariant .LUMO_TERTIARY );
257- this .btnEdit .getStyle ().set ("margin" , "0px" );
258- this .btnEdit .getStyle ().set ("padding" , "0px" );
259- this .btnEdit .getStyle ().set ("font-size" , "12px" );
260- this .btnEdit .setIcon (VaadinIcon .PENCIL .create ());
261- this .btnSave .setVisible (false );
262- this .btnSave .addThemeVariants (ButtonVariant .LUMO_SMALL , ButtonVariant .LUMO_TERTIARY );
263- this .btnSave .getStyle ().set ("margin" , "0px" );
264- this .btnSave .getStyle ().set ("padding" , "0px" );
265- this .btnSave .getStyle ().set ("font-size" , "12px" );
266- final ShortcutRegistration btnSaveShortcut = this .btnSave .addClickShortcut (Key .ENTER );
267- btnSaveShortcut .setBrowserDefaultAllowed (true );
268- btnSaveShortcut .setEventPropagationAllowed (false );
269- this .btnSave .setIcon (VaadinIcon .DISC .create ());
270- this .btnClose .setVisible (false );
271- this .btnClose .addThemeVariants (ButtonVariant .LUMO_SMALL , ButtonVariant .LUMO_TERTIARY );
272- this .btnClose .getStyle ().set ("margin" , "0px" );
273- this .btnClose .getStyle ().set ("padding" , "0px" );
274- this .btnClose .getStyle ().set ("font-size" , "12px" );
275- final ShortcutRegistration btnCloseShortcut = this .btnClose .addClickShortcut (Key .ESCAPE );
276- btnCloseShortcut .setBrowserDefaultAllowed (true );
277- btnCloseShortcut .setEventPropagationAllowed (false );
278- this .btnClose .setIcon (VaadinIcon .CLOSE .create ());
155+ this .getEditor ().setAutoselect (true );
156+ this .getEditor ().addThemeVariants (TextFieldVariant .LUMO_SMALL );
279157
280- this .label .setSizeUndefined ();
281- this .textField .setWidthFull ();
282- this .textField .setHeight (null );
283- this .btnEdit .setWidth ("15px" );
284- this .btnEdit .setHeight ("15px" );
285- this .btnSave .setWidth ("15px" );
286- this .btnSave .setHeight ("15px" );
287- this .btnClose .setWidth ("15px" );
288- this .btnClose .setHeight ("15px" );
289- this .add (this .label , this .textField , this .btnEdit , this .btnSave , this .btnClose );
290- this .setWidthFull ();
291- this .setHeight (null );
158+ this .getEditor ().setWidthFull ();
159+ this .getEditor ().setHeight (null );
292160
293- this .textField .addBlurListener (this ::textField_onBlur );
294- this .textField .addValueChangeListener (this ::textField_valueChanged );
295- this .btnEdit .addClickListener (this ::btnEdit_onClick );
296- this .btnSave .addClickListener (this ::btnSave_onClick );
297- this .btnClose .addClickListener (this ::btnClose_onClick );
298- } // </generated-code>
299-
300- // <generated-code name="variables">
301-
302- // </generated-code>
303-
161+ this .getEditor ().addBlurListener (this ::textField_onBlur );
162+ }
304163}
0 commit comments