-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from FlowingCode/sort
feat: extend selection semantics to support ordered value along with UI to adjust
- Loading branch information
Showing
8 changed files
with
255 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,5 @@ rebel.xml | |
/webpack.generated.js | ||
/package.json | ||
/bin/ | ||
/.npmrc | ||
/frontend/generated/vaadin.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/TwinColGridListAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/*- | ||
* #%L | ||
* TwinColGrid add-on | ||
* %% | ||
* Copyright (C) 2017 - 2021 Flowing Code | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.flowingcode.vaadin.addons.twincolgrid; | ||
|
||
import com.vaadin.flow.component.HasValue; | ||
import com.vaadin.flow.component.HasValue.ValueChangeEvent; | ||
import com.vaadin.flow.shared.Registration; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.Delegate; | ||
|
||
/** | ||
* Expose {@link TwinColGrid} as a {@code HasValue} of {@code List} | ||
* | ||
* @author Javier Godoy | ||
*/ | ||
@SuppressWarnings("serial") | ||
@RequiredArgsConstructor | ||
class TwinColGridListAdapter<T> implements HasValue<ValueChangeEvent<List<T>>, List<T>> { | ||
|
||
private interface IDelegate { | ||
boolean isEmpty(); | ||
|
||
void clear(); | ||
|
||
void setReadOnly(boolean readOnly); | ||
|
||
boolean isReadOnly(); | ||
|
||
void setRequiredIndicatorVisible(boolean requiredIndicatorVisible); | ||
|
||
boolean isRequiredIndicatorVisible(); | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
private final class ValueChangeEventImpl implements ValueChangeEvent<List<T>> { | ||
|
||
boolean isFromClient; | ||
List<T> value; | ||
|
||
@Override | ||
public HasValue<?, List<T>> getHasValue() { | ||
return TwinColGridListAdapter.this; | ||
} | ||
|
||
@Override | ||
public List<T> getOldValue() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
@NonNull | ||
@Delegate(types = IDelegate.class) | ||
private final TwinColGrid<T> delegate; | ||
|
||
@Override | ||
public void setValue(List<T> value) { | ||
delegate.setValue(new LinkedHashSet<>(value)); | ||
} | ||
|
||
@Override | ||
public List<T> getValue() { | ||
return Collections.unmodifiableList(delegate.collectValue(Collectors.toList())); | ||
} | ||
|
||
@Override | ||
public Registration addValueChangeListener( | ||
ValueChangeListener<? super ValueChangeEvent<List<T>>> listener) { | ||
|
||
List<Registration> registrations = new ArrayList<>(); | ||
|
||
registrations.add(delegate.addValueChangeListener(ev -> { | ||
List<T> value = new ArrayList<>(ev.getValue()); | ||
ValueChangeEvent<List<T>> listEvent; | ||
listEvent = new ValueChangeEventImpl(ev.isFromClient(), new ArrayList<>(value)); | ||
listener.valueChanged(listEvent); | ||
})); | ||
|
||
// sorting the grid changes its value under List::equals | ||
registrations.add(delegate.getRightGrid().addSortListener(ev -> { | ||
List<T> value = getValue(); | ||
ValueChangeEvent<List<T>> listEvent; | ||
listEvent = new ValueChangeEventImpl(ev.isFromClient(), value); | ||
listener.valueChanged(listEvent); | ||
})); | ||
|
||
return () -> registrations.forEach(Registration::remove); | ||
} | ||
|
||
@Override | ||
public List<T> getEmptyValue() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.