61
61
import com .vaadin .flow .component .select .Select ;
62
62
import com .vaadin .flow .component .textfield .BigDecimalField ;
63
63
import com .vaadin .flow .component .textfield .TextField ;
64
+ import com .vaadin .flow .data .value .ValueChangeMode ;
64
65
import com .vaadin .flow .router .BeforeEnterEvent ;
65
66
import com .vaadin .flow .router .BeforeEnterObserver ;
66
67
import com .vaadin .flow .router .QueryParameters ;
67
68
68
69
import software .xdev .vaadin .builder .CustomizableFilterBuilder ;
70
+ import software .xdev .vaadin .comparators .ContainsComparator ;
69
71
import software .xdev .vaadin .comparators .FilterComparator ;
70
72
import software .xdev .vaadin .comparators .utl .DateHelper ;
71
73
import software .xdev .vaadin .daterange_picker .business .DateRange ;
@@ -102,11 +104,13 @@ public class FilterComponent<T> extends Composite<VerticalLayout> implements Bef
102
104
public static final String BTN_CANCEL_FILTER_FILTER_COMPONENT = "btnCancelFilterFilterComponent" ;
103
105
public static final String DATE_RANGE_PICKER_QUERY_FILTER_COMPONENT = "dateRangePickerQueryFilterComponent" ;
104
106
public static final String DELETED_INITIAL_CONDITION_STRING = "deletedInitialCondition" ;
107
+ public static final String BTN_RESET_FILTER_FILTER_COMPONENT = "btnResetFilterFilterComponent" ;
105
108
106
109
private final UI ui ;
107
110
private final Button btnAddNewFilter = new Button ("Add filter" );
108
111
private final Button btnAcceptFilter = new Button (VaadinIcon .CHECK .create ());
109
112
private final Button btnCancelFilter = new Button (VaadinIcon .CLOSE .create ());
113
+ private final Button btnResetFilter = new Button (VaadinIcon .ROTATE_RIGHT .create ());
110
114
private final Select <FilterField <T , ?>> selFields = new Select <>();
111
115
private final Select <FilterComparator > selOperations = new Select <>();
112
116
private final TextField txtSearchQuery = new TextField ();
@@ -130,6 +134,7 @@ private static DateRangePicker<DateRange> createDateRangePicker()
130
134
131
135
// Data
132
136
private final List <ChipBadgeExtension <FilterCondition <T , ?>>> chipBadges = new ArrayList <>();
137
+ private final List <ChipBadgeExtension <FilterCondition <T , ?>>> initialChipBadges = new ArrayList <>();
133
138
134
139
private int initialConditionIdCounter = 1 ;
135
140
private String editingBadgeId ;
@@ -160,12 +165,13 @@ public FilterComponent(final Grid<T> dataGrid)
160
165
private void initUI ()
161
166
{
162
167
final VerticalLayout vlRoot = this .getContent ();
163
- vlRoot .add (this .btnAddNewFilter , this .hlFilter , this .hlChipBadges );
168
+ vlRoot .add (new HorizontalLayout ( this .btnAddNewFilter , this . btnResetFilter ) , this .hlFilter , this .hlChipBadges );
164
169
165
170
// click listener
166
171
this .btnAddNewFilter .addClickListener (e -> this .onShowFilterInput ());
167
172
this .btnCancelFilter .addClickListener (e -> this .hlFilter .removeAll ());
168
173
this .btnAcceptFilter .addClickListener (e -> this .onAcceptFilter ());
174
+ this .btnResetFilter .addClickListener (e -> this .onResetFilter ());
169
175
170
176
// value change listener
171
177
this .selFields .addValueChangeListener (e -> this .onFieldChange (this .selFields .getValue ()));
@@ -182,6 +188,11 @@ private void initUI()
182
188
this .btnAcceptFilter .setEnabled (e .getValue () != null && this .selOperations .getValue () != null ));
183
189
this .selSearchQuery .addValueChangeListener (e ->
184
190
this .btnAcceptFilter .setEnabled (e .getValue () != null && this .selOperations .getValue () != null ));
191
+ this .txtSearchQuery .addValueChangeListener (e ->
192
+ this .btnAcceptFilter .setEnabled (!e .getValue ().isBlank () && this .selOperations .getValue () != null ));
193
+
194
+ this .nmbSearchQuery .setValueChangeMode (ValueChangeMode .EAGER );
195
+ this .txtSearchQuery .setValueChangeMode (ValueChangeMode .EAGER );
185
196
186
197
// renderer
187
198
this .selFields .setTextRenderer (FilterField ::getDescription );
@@ -193,9 +204,12 @@ private void initUI()
193
204
this .btnAcceptFilter .addClickShortcut (Key .ENTER );
194
205
this .btnCancelFilter .addClickShortcut (Key .ESCAPE );
195
206
207
+ this .btnResetFilter .setEnabled (false );
208
+
196
209
// ids
197
210
this .btnAcceptFilter .setId (BTN_ACCEPT_FILTER_FILTER_COMPONENT );
198
211
this .btnCancelFilter .setId (BTN_CANCEL_FILTER_FILTER_COMPONENT );
212
+ this .btnResetFilter .setId (BTN_RESET_FILTER_FILTER_COMPONENT );
199
213
this .selFields .setId (SEL_FIELDS_FILTER_COMPONENT );
200
214
this .selOperations .setId (SEL_OPERATIONS_FILTER_COMPONENT );
201
215
this .txtSearchQuery .setId (TXT_SEARCH_QUERY_FILTER_COMPONENT );
@@ -207,6 +221,30 @@ private void initUI()
207
221
this .dateRangePickerQuery .setId (DATE_RANGE_PICKER_QUERY_FILTER_COMPONENT );
208
222
}
209
223
224
+ private void onResetFilter ()
225
+ {
226
+ final List <ChipBadgeExtension <FilterCondition <T , ?>>> copyChipBadges = new ArrayList <>(this .chipBadges );
227
+ for (final ChipBadgeExtension <FilterCondition <T , ?>> chipBadge : copyChipBadges )
228
+ {
229
+ this .removeChipBadgeCondition (chipBadge );
230
+ }
231
+
232
+ // Creating the initial filter again
233
+ this .chipBadges .addAll (this .initialChipBadges );
234
+ this .initialChipBadges .forEach (this .hlChipBadges ::add );
235
+ this .updateGridFilter ();
236
+
237
+ // Remove query parameter
238
+ this .ui .getPage ().fetchCurrentURL (currentUrl ->
239
+ this .ui
240
+ .getPage ()
241
+ .getHistory ()
242
+ .replaceState (null , currentUrl .getPath ()));
243
+
244
+ // Disable reset button again
245
+ this .btnResetFilter .setEnabled (false );
246
+ }
247
+
210
248
private void onOperatorChanged ()
211
249
{
212
250
this .btnAcceptFilter .setEnabled (this .shouldTheAcceptButtonBeEnabled ());
@@ -219,10 +257,14 @@ private void onOperatorChanged()
219
257
220
258
// Change to a text field if the field is of type enum with condition 'contains'
221
259
if (isSelSearchQueryVisible
222
- && this .selOperations .getValue ().getDescription ().contains ("contains" ))
260
+ && this .selOperations .getValue ()
261
+ .getDescription ()
262
+ .equals (ContainsComparator .CONTAINS_COMPARATOR_DESCRIPTION ))
223
263
{
224
264
this .selSearchQuery .setVisible (false );
225
265
this .txtSearchQuery .setVisible (true );
266
+
267
+ this .btnAcceptFilter .setEnabled (this .shouldTheAcceptButtonBeEnabled ());
226
268
}
227
269
else if (!isSelSearchQueryVisible )
228
270
{
@@ -260,6 +302,10 @@ private void onShowFilterInput()
260
302
261
303
if (this .hlFilter .getChildren ().findAny ().isEmpty ())
262
304
{
305
+ // Needed if the previous condition was an editable initial condition
306
+ // The editable initial condition makes the cancel button invisible
307
+ this .btnCancelFilter .setVisible (true );
308
+
263
309
this .selFields .setValue (null );
264
310
this .selOperations .setItems (Collections .emptyList ());
265
311
this .selOperations .setEnabled (false );
@@ -334,6 +380,9 @@ private void onAcceptFilter()
334
380
335
381
// Removing all filter components after accepting
336
382
this .hlFilter .removeAll ();
383
+
384
+ // When something changes from the initial start, enable the reset button
385
+ this .btnResetFilter .setEnabled (true );
337
386
}
338
387
339
388
private ChipBadgeExtension <FilterCondition <T , ?>> createBadgeConditionAndApplyFilter (
@@ -402,7 +451,7 @@ private void onAcceptFilter()
402
451
}
403
452
404
453
this .chipBadges .add (badge );
405
- this .hlChipBadges .add (badge , badge );
454
+ this .hlChipBadges .add (badge );
406
455
407
456
this .updateGridFilter ();
408
457
@@ -421,11 +470,16 @@ private void deactivateDeleteButtonFromChipComponents(
421
470
{
422
471
this .removeChipBadgeCondition (badge );
423
472
424
- if (badge .getBadgeId () != null && !badge .getBadgeId ().equals (NO_BADGE_ID_STRING ))
473
+ if (!this .identifier .isBlank ()
474
+ && badge .getBadgeId () != null
475
+ && !badge .getBadgeId ().equals (NO_BADGE_ID_STRING ))
425
476
{
426
477
badge .setBadgeId (DELETED_INITIAL_CONDITION_STRING );
427
478
this .addQueryParameter (badge );
428
479
}
480
+
481
+ // Activate the reset button
482
+ this .btnResetFilter .setEnabled (true );
429
483
});
430
484
}
431
485
}
@@ -436,8 +490,10 @@ private void deactivateDeleteButtonFromChipComponents(
436
490
@ SuppressWarnings ("PMD.CognitiveComplexity" ) // Fixed in v2
437
491
private void formatLocalDateChipBadgeText (final ChipBadge <FilterCondition <T , ?>> chipBadge )
438
492
{
493
+ final FilterCondition <T , ?> filterField = chipBadge .getItem ();
494
+
439
495
if (this .dateRangePickerQuery .isVisible ()
440
- && chipBadge . getItem () .getSelectedCondition ().getDescription ().equals (IS_BETWEEN_COMPARATOR_DESCRIPTION ))
496
+ && filterField .getSelectedCondition ().getDescription ().equals (IS_BETWEEN_COMPARATOR_DESCRIPTION ))
441
497
{
442
498
chipBadge .setItemLabelGenerator ((ItemLabelGenerator <FilterCondition <T , ?>>)tFilterCondition ->
443
499
{
@@ -476,7 +532,7 @@ private void formatLocalDateChipBadgeText(final ChipBadge<FilterCondition<T, ?>>
476
532
return createChipComponentString (tFilterCondition , dateString );
477
533
});
478
534
}
479
- else if (this .dateSearchQuery .isVisible ())
535
+ else if (this .dateSearchQuery .isVisible () && filterField . getItem (). getType (). equals ( LocalDate . class ) )
480
536
{
481
537
chipBadge .setItemLabelGenerator ((ItemLabelGenerator <FilterCondition <T , ?>>)tFilterCondition ->
482
538
{
@@ -506,7 +562,7 @@ else if(this.dateSearchQuery.isVisible())
506
562
return createChipComponentString (tFilterCondition , dateString );
507
563
});
508
564
}
509
- else if (this .dateTimeSearchQuery .isVisible ())
565
+ else if (this .dateTimeSearchQuery .isVisible () && filterField . getItem (). getType (). equals ( LocalDateTime . class ) )
510
566
{
511
567
chipBadge .setItemLabelGenerator ((ItemLabelGenerator <FilterCondition <T , ?>>)tFilterCondition ->
512
568
{
@@ -657,7 +713,7 @@ else if(this.selSearchQuery.isVisible())
657
713
else
658
714
{
659
715
// For txtSearchQuery
660
- valueNotNull = true ;
716
+ valueNotNull = ! this . txtSearchQuery . getValue (). isBlank () ;
661
717
}
662
718
663
719
return valueNotNull && this .selOperations .getValue () != null ;
@@ -1116,6 +1172,8 @@ public void beforeEnter(final BeforeEnterEvent beforeEnterEvent)
1116
1172
1117
1173
this .removeInitialConditionIfBadgeIdAlreadyExists (this .queryBadgeIdList );
1118
1174
this .createConditionsFromQueryParameters ();
1175
+
1176
+ this .btnResetFilter .setEnabled (true );
1119
1177
}
1120
1178
}
1121
1179
}
@@ -1318,7 +1376,7 @@ public FilterComponent<T> withInitialFilter(
1318
1376
this .selFields .setItems (this .filterFieldList );
1319
1377
}
1320
1378
1321
- final ChipBadge <FilterCondition <T , ?>> chipBadge = this .createBadgeConditionAndApplyFilter (
1379
+ final ChipBadgeExtension <FilterCondition <T , ?>> chipBadge = this .createBadgeConditionAndApplyFilter (
1322
1380
finalFilterField ,
1323
1381
selectedCondition ,
1324
1382
searchQuery ,
@@ -1328,6 +1386,9 @@ public FilterComponent<T> withInitialFilter(
1328
1386
// Just needed if the url parameters are activated
1329
1387
chipBadge .setBadgeId (badgeId );
1330
1388
1389
+ // Needed for resetting the conditions
1390
+ this .initialChipBadges .add (chipBadge );
1391
+
1331
1392
return this ;
1332
1393
}
1333
1394
0 commit comments