wicket manipulating DefaultDataTable - datatable

I was creating a web application in wicket and had created a table which shows the user some information. Now I wanted to manipulate this table so if the cell contained "N" the background color would be RED and if it contained "Y" the background color would be GREEN. At the moment I was having trouble to determine what is actually inside the cell. I create my table by the following:
dataTable = new DefaultDataTable<TableModalInt, String>("table", columns,
new TableModalProvider(), 100000){
#SuppressWarnings("rawtypes")
#Override
protected Item newCellItem(String id, int index, IModel model) {
Item item = super.newCellItem(id, index, model);
if (id == "3"){
item.add(AttributeModifier.replace("align", "center"));
}
return item;
}
};
I am capable of determining the cell which I wanna now check what is being displayed to the user. Any help on how i can do this? to change the color i know I'll have to add item.add(AttributeModifier.replace("bgcolor", "red")); but don't know how to tell whats inside the cell

You should do your checks in the IColumn implementation.
https://github.com/apache/wicket/blob/24e9db6c8af85043ce36e4d25a0e8a2d8dc2f49e/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/PropertyColumn.java#L94 populates the Item with a Label. You need to add a AttributeModifier to the Label.
You can also achieve your goal with pure JavaScript and/or CSS at the client side.

Item is extending list item, so you can try .getModelObject and validate it if it is "X" or "Y"
http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/repeater/Item.html
http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/html/list/ListItem.html#getModelObject()

This example extracts the cell value when a cell is clicked in a Wicket DataView. The model for this DataView is a Map with String keys and Integer values: Map<String,Integer>.
The PropertyColumn list is created using with column headers ("ALPHA", "BETA", "GAMMA") and property expressions: "alpha", "beta", "gamma". PropertyColumn uses the expressions to retrieve the values from the map.
The DataView is created with the list of PropertyColumns and a DataProvider. DataView uses the DataProvider to populate the PropertyColumn when the table is rendered and reacts to clicks to expose the cell values.
Cells are exposed by overriding the newCellItem(String,int,IModel) method and calling the super-class method to get the cell. This example adds a behavior to react to "onclick" events. Within the event, the cell's first child component should be the Label used to display the cell value.
The innermost model of the cell Label is the PropertyModel from the PropertyColumn.
innerModel.getPropertyExpression(): Our data map key (String).
innerModel.getObject(): The data value (Integer).
innerModel.getInnermostModelOrObject(): The list item (Map<String,Integer>).
Wicket DataView: Extract Cell Value
public class MessageLogStatus
extends WebPage
{
/** Represents serialVersionUID. */
private static final long serialVersionUID = 20150701L;
private static final Logger log = LoggerFactory.getLogger(MessageLogStatus.class);
static final String A = "alpha";
static final String B = "beta";
static final String C = "gamma";
public MessageLogStatus()
{
super();
final List<String> keys = Arrays.asList(A, B, C);
final List<Map<String,Integer>> data = Arrays.asList
(
map(A, 1).put(B, 11).put(C, 21).toMap(),
map(A, 2).put(B, 12).put(C, 22).toMap(),
map(A, 3).put(B, 13).put(C, 23).toMap(),
map(A, 4).put(B, 14).put(C, 24).toMap(),
map(A, 5).put(B, 15).put(C, 25).toMap(),
map(A, 6).put(B, 16).put(C, 26).toMap(),
map(A, 7).put(B, 17).put(C, 27).toMap(),
map(A, 8).put(B, 18).put(C, 28).toMap(),
map(A, 9).put(B, 19).put(C, 29).toMap()
);
// Using a DefaultDataTable
ISortableDataProvider<Map<String,Integer>,String> dataProvider = new SortableDataProvider<Map<String,Integer>,String>()
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
public Iterator<Map<String,Integer>> iterator(long first, long count)
{
int start = Math.max(0, (int) first);
int end = Math.min(data.size(), start + (int) count);
return data.subList(start, end).iterator();
}
public long size()
{
return data.size();
}
public IModel<Map<String,Integer>> model(Map<String,Integer> object)
{
return new CompoundPropertyModel<Map<String,Integer>>(object);
}
};
List<PropertyColumn<Map<String,Integer>,String>> columns = new ArrayList<PropertyColumn<Map<String,Integer>,String>>();
for (String key : keys)
{
columns.add
(
new PropertyColumn<Map<String,Integer>, String>(Model.of(key.toUpperCase()), key)
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
#Override
public void populateItem(Item<ICellPopulator<Map<String, Integer>>> item, String componentId,
IModel<Map<String, Integer>> rowModel)
{
super.populateItem(item, componentId, rowModel);
Map<String, Integer> entity = rowModel.getObject();
String px = getPropertyExpression();
PropertyModel<Object> propModel = new PropertyModel<Object>(rowModel, px);
log.info("Add Label to Cell: PropEx="+px+", Value="+propModel.getObject()+", entity="+entity);
}
}
);
}
//
// Wicket: <table wicket:id="dataTable"></table>
//
DataTable<Map<String,Integer>,String> dataTable =
new DataTable<Map<String,Integer>,String>("dataTable", columns, dataProvider, 5)
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
#Override
protected Item<IColumn<Map<String, Integer>, String>> newCellItem(final String id, final int index,
final IModel<IColumn<Map<String, Integer>, String>> model)
{
final Item<IColumn<Map<String,Integer>, String>> cell = super.newCellItem(id, index, model);
cell.add
(
new AjaxEventBehavior("onclick")
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
#SuppressWarnings("unchecked")
#Override
protected void onEvent(AjaxRequestTarget target)
{
if ( (cell.size() > 0) && (cell.get(0) instanceof Label) )
{
Label cellLabel = (Label) cell.get(0);
PropertyModel<Integer> cellLabelModel = (PropertyModel<Integer>) cellLabel.getInnermostModel();
String property = cellLabelModel.getPropertyExpression();
Integer value = cellLabelModel.getObject();
Map<String, Integer> entity = (Map<String,Integer>) cellLabelModel.getInnermostModelOrObject();
log.info("OnClick: Index="+index+", PropEx="+property+", Value="+value+", Entity="+entity);
}
}
}
);
return cell;
}
};
dataTable.addBottomToolbar(new NavigationToolbar(dataTable));
dataTable.addTopToolbar(new HeadersToolbar<String>(dataTable, null));
add(dataTable);
}
// Make building the data structure a little more fun :)
private MapBuilder<String, Integer> map(String key, Integer value)
{
return new MapBuilder<String, Integer>().put(key, value);
}
private static class MapBuilder<K, V>
{
Map<K, V> map = new HashMap<K, V>();
MapBuilder<K, V> put(K key, V value)
{
map.put(key, value);
return this;
}
Map<K, V> toMap()
{
return map;
}
}
}
Output
OnClick: Index=0, PropEx=alpha, Value=5, Entity={gamma=25, alpha=5, beta=15}
OnClick: Index=1, PropEx=beta, Value=15, Entity={gamma=25, alpha=5, beta=15}
OnClick: Index=2, PropEx=gamma, Value=25, Entity={gamma=25, alpha=5, beta=15}

Related

Java8 Streams: Remove an field from an object of the map value

I have a hash map like this
Map<String, AttributeValueUpdate> myMap = new HashMap<>;
The class AttributeValueUpdate looks like this:
public class AttributeValueUpdate {
private AttributeValue value;
private String action;
public static class Builder {
private AttributeValue value;
private String action;
public Builder() {
}
public AttributeValueUpdate.Builder withValue(AttributeValue value) {
this.value = value;
return this;
}
public AttributeValueUpdate.Builder withAction(String action) {
this.action = action;
return this;
}
protected void populate(AttributeValueUpdate instance) {
instance.setValue(this.value);
instance.setAction(this.action);
}
public AttributeValueUpdate build() {
AttributeValueUpdate instance = new AttributeValueUpdate();
this.populate(instance);
return instance;
}
}
}
The map has two entries
AttributeValueUpdate att1 = AttributeValueUpdate.builder().withAction("Add").withValue(new AttributeValue("sam").build();
AttributeValueUpdate att2 = AttributeValueUpdate.builder().withAction("Delete").withValue(new AttributeValue("john").build();
myMap.add("entry1", attr1);
myMap.add("entry2", atte2);
I want to modify mymap by deleting the "value field" from all the AttributeValueUpdate (which is value of the map), basically map's value field will be changed by removing "value field" of the AttributeValueUpdate object. How can I achieve this using java streams?
Java Stream API is not a friend with Map as long as it's collection-based (List, Set). You need to stream over the entries of the map.
As far as I understand, you want to remove (= make null) AttributeValue value of each AttributeValueUpdate instance (map's value). Here is the way to go assuming a constructor AttributeValueUpdate(String action):
Map<String, AttributeValueUpdate> updatedMap = myMap.entrySet().stream()
.map(entry -> {
String action = entry.getValue().getAction();
AttributeValueUpdate update = new AttributeValueUpdate(action);
return new SimpleEntry<>(entry.getKey(), update);
})
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
The easiest solution is using Map#replaceAll if you don't mind to mutate the map:
myMap.replaceAll((k, v) -> {
String action = v.getAction();
return new AttributeValueUpdate(action);
});

How to refresh tableview after adding the new data in JavaFx

I am currently working on JAVA FX application to fetch the user's information from JIRA REST API. I want to have a refresh button and on clicking it,the table view must be shown with the newly added data.
Please have a look at the code.
My POJO class
public class Issues {
private String jiraKey;
private String jiraSummary;
private String jiraPriority;
private String jiraAssignee;
private String jiraIssueType;
private Hyperlink hyperLink;
private String loghours;
private String commentLogHours;
public Issues(String jiraKey, String jiraSummary, String jiraPriority, String jiraAssignee, String jiraIssueType) {
super();
this.hyperLink = new Hyperlink(jiraKey);
this.jiraSummary = jiraSummary;
this.jiraPriority = jiraPriority;
this.jiraAssignee = jiraAssignee;
this.jiraIssueType = jiraIssueType;
this.hyperLink = hyperLink;
this.loghours = loghours;
this.commentLogHours = commentLogHours;
}
public String getJiraKey() {
return jiraKey;
}
public void setJiraKey(String jiraKey) {
this.jiraKey = jiraKey;
}
//getters and setters
}
My Observable list method which populates the data to table
Class ABC{
public ObservableList<Issues> issueJsonList(){
// A processed arraylist
ObservableList<Issues> data = FXCollections.observableList(list);
return data;
}
}
Here's my tableview code.
#SuppressWarnings({ "unchecked", "rawtypes" })
public TableView<Issues> initTable() throws IOException, URISyntaxException {
TableView<Issues> table = new TableView<Issues>();
table.setEditable(true);
TableColumn<Issues, Hyperlink> jiraKey = new TableColumn<>(keyField);
jiraKey.setCellValueFactory(new PropertyValueFactory("hyperLink"));
jiraKey.setCellFactory(new HyperlinkCell());
TableColumn jiraPriority = new TableColumn(priorityField);
TableColumn jiraIssueType = new TableColumn(issueTypeField);
TableColumn jiraSummary = new TableColumn(summaryField);
TableColumn jiraAssignee = new TableColumn(assigneeField);
TableColumn workLogCol = new TableColumn(workLogField);
TableColumn timeSpent = new TableColumn(timeSpentField);
TableColumn commentTimeLog = new TableColumn(commentField);
workLogCol.getColumns().addAll(timeSpent, commentTimeLog);
jiraSummary.setCellValueFactory(new PropertyValueFactory("jiraSummary"));
jiraIssueType.setCellValueFactory(new PropertyValueFactory("jiraIssueType"));
jiraPriority.setCellValueFactory(new PropertyValueFactory("jiraPriority"));
jiraAssignee.setCellValueFactory(new PropertyValueFactory("jiraAssignee"));
timeSpent.setCellValueFactory(new PropertyValueFactory("getTimeSpent"));
timeSpent.setCellFactory(TextFieldTableCell.forTableColumn());
commentTimeLog.setCellValueFactory(new PropertyValueFactory("getComment"));
commentTimeLog.setCellFactory(TextFieldTableCell.forTableColumn());
timeSpent.setOnEditCommit(new EventHandler<CellEditEvent<JiraAuth, String>>() {
public void handle(CellEditEvent<JiraAuth, String> t) {
JiraAuth.setTimeSpent(t.getNewValue());
}
});
commentTimeLog.setCellFactory(TextFieldTableCell.forTableColumn());
commentTimeLog.setOnEditCommit(new EventHandler<CellEditEvent<JiraAuth, String>>() {
public void handle(CellEditEvent<JiraAuth, String> t) {
JiraAuth.setWorkLogComment(t.getNewValue());
int i = t.getTablePosition().getRow();
String abc = table.getColumns().get(0).getCellData(i).toString();
String finalStr = abc.substring(abc.indexOf("]") + 1);
JiraAuth.setWorkLogJiraKey(finalStr);
}
});
table.getColumns().setAll(jiraKey, jiraSummary, jiraPriority, jiraAssignee, jiraIssueType);
IssuesJsonParser issueObject = new IssuesJsonParser();
ObservableList<Issues> data = issueObject.issueJsonList();
table.setItems(data);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
return table;
}
upon calling the refresh button,
buttons[0] = new Button("Refresh");
buttons[0].setOnAction(event->{
initTable().refresh();
});
nothing happens. Please help!!

add dynamic row content to vaadin grid

In vaadin7, I used GeneratedPropertyContainer to do this, eg adding row number:
wrappedContainer = new GeneratedPropertyContainer(_container);
wrappedContainer.addGeneratedProperty("#",
new PropertyValueGenerator<Integer>() {
#Override
public Integer getValue(Item item, Object itemId, Object propertyId) {
return (int) _container.indexOfId(itemId) + 1;
}
#Override
public Class<Integer> getType() {
return java.lang.Integer.class;
}
}
);
setContainerDataSource(wrappedContainer);
In vaadin8, since GeneratedPropertyContainer is deprecated, I tried to do like this:
grid.addColumn((v)->((List)_container.getData().getItems()).indexOf(v)+1);
But the index is static, when I sort the rows ascending and descending, the row number is moving too.
What I need is the first row is row number 1 and the last row is row number N, no matter how I sort the rows.
Thanks.
Unfortunately, as far as I can tell there is no simple solution out of the box. However, you can do like the following code:
Grid<MyBean> grid = new Grid<>();
grid.setDataProvider(new RowIndexDataProviderWrapper<>(DataProvider.ofItems(new MyBean("Item 1"), new MyBean("Item 2"), new MyBean("Item 3"))));
grid.addColumn(MyBean::getRowIndex).setCaption("#");
grid.addColumn(MyBean::getName).setCaption("Name");
public interface RowIndexAware {
void setRowIndex(int rowIndex);
int getRowIndex();
}
public class MyBean implements RowIndexAware {
// implement the interface (e.g. store row index in field)
// and add your bean properties
}
public class RowIndexDataProviderWrapper<T extends RowIndexAware, F> implements DataProvider<T, F> {
private DataProvider<T, F> wrapped;
public RowIndexDataProviderWrapper(DataProvider<T, F> wrapped) {
this.wrapped = wrapped;
}
// delegate all methods to be implemented for DataProvider interface
// to wrapped DataProvider with the exception of "fetch":
#Override
public Stream<T> fetch(Query<T, F> query) {
List<T> result = wrapped.fetch(query).collect(Collectors.toCollection(ArrayList::new));
for (int i = 0; i < result.size(); i++) {
result.get(i).setRowIndex(query.getOffset() + i);
}
return result.stream();
}
}
The idea is to get the row index when rows are fetched within the DataProvider and to store them in your bean.

Recycler View with Header and Edit Text

I have a recyclerview with a header achieved by using two different element types. In my header there is an edit text which I want to use for filtering the nonheader elements of the list. Below is my current implementation, I have one concern and one problem with it.
My concern is that what I am doing in publishResults with the notifyItemRangeRemoved and notifyItemInserted is the wrong way to update the recycler view. I originally was doing notifyDatasetChanged but his would cause the header row to be refreshed too and the edit text to lose focus. What I really want is a way to refresh only the item rows and leave the header row untouched.
My current problem is that with the existing code if I scroll down too much the edit text looses focus. I want the edit text to keep focus even if I scroll to the bottom of the list.
The code used to use a ListView with setHeaderView and that worked somehow so there must be someway of achieving the goal just not sure what the trick with a recycler view is. Any help is much appreciated.
public class SideListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private final List<String> data;
public List<String> filteredData;
private HeaderActionListener headerActionListener;
public SideListAdapter(Context context, ArrayList<String> data, HeaderActionListener headerActionListener) {
this.data = data;
filteredData = new ArrayList<>(data);
this.context = context;
this.headerActionListener = headerActionListener;
}
#Override
public Filter getFilter() {
return new TestFilter();
}
static class SideListItem extends RecyclerView.ViewHolder {
LinearLayout baseLayout;
public SideListItem(View itemView) {
super(itemView);
baseLayout = (LinearLayout) itemView.findViewById(R.id.settings_defaultcolor);
}
}
class SideListHeader extends SideListHeader {
EditText sort;
public SideListHeaderLoggedIn(View itemView) {
super(itemView);
sort = (EditText) itemView.findViewById(R.id.sort);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new SideListItem(v);
} else if (viewType == SideListHeader) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
return new SideListHeader(v);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
public interface HeaderActionListener {
boolean onSortEditorAction(TextView arg0, int arg1, KeyEvent arg2);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof SideListHeader) {
final SideListHeader sideListHeader = (SideListHeader) holder;
sideListHeader.sort.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
}
});
sideListHeader.sort.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable editable) {
String result = sideListHeader.sort.getText().toString().replaceAll(" ", "");
getFilter().filter(result);
}
});
}
if (holder instanceof SideListItem) {
// Inflate normal item //
}
}
// need to override this method
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
//increasing getItemcount to 1. This will be the row of header.
#Override
public int getItemCount() {
return filteredData.size() + 1;
}
private class TestFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix.isEmpty()) {
ArrayList<String> list = new ArrayList<>(data);
results.values = list;
results.count = list.size();
} else {
final ArrayList<String> list = new ArrayList<>(data);
final ArrayList<String> nlist = new ArrayList<>();
for (int i = 0 ; i < list.size(); i++) {
String item = list.get(i);
if (item.contains(prefix)) {
nlist.add(item);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
notifyItemRangeRemoved(1, getItemCount()-1);
filteredData.clear();
filteredData.addAll((List<String>)results.values);
for(int i = 1; i < getItemCount() - 1; i++){
notifyItemInserted(i);
}
}
}
}
I'm not sure how correct this way is, but in my code I implemented it like that
private var headerList: List<HeaderItem> = listOf(HeaderItem("Title"))
private fun searchItem(items: List<Items>, query: String) {
items.filterIsInstance<MainItem>().filter { filteredItems ->
filteredItems.header.lowercase().contains(query.lowercase())
}.let { searchedItems ->
rvAdapter.submitList(headerList + searchedItems)
}
}
This way I was able to preserve header element when I did my search

javaFX Tableview Data not visible

I tried all to populate a TableView with data. The next code inserts a new row in table but the data not appear the table. I tried to find an explication for this without success.
Please help. I can't what is wrong.
In controller.java
#FXML private TableView<TempTableData> tempTable;
#FXML private TableColumn<TempTableData,String> columnTime;
#FXML private TableColumn<TempTableData,Float> columnTempOne;
#FXML private TableColumn<TempTableData,Float> columnTempTwo;
#FXML private TableColumn<TempTableData,Float> columnTempThree;
#FXML protected void initialize() {
columnTime = new TableColumn<TempTableData,String>();
columnTime.setCellValueFactory(
new PropertyValueFactory<TempTableData,String>("Time"));
columnTempOne = new TableColumn<TempTableData,Float>();
columnTempOne.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 1"));
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempTwo.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 2"));
columnTempThree = new TableColumn<TempTableData,Float>();
columnTempThree.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 3"));
tempDataList = FXCollections.observableArrayList();
tempDataList.add(new TempTableData("0",3.0f, 4f, 5f));
tempTable.setItems(tempDataList);
}
TempTableData.java
public class TempTableData {
private final SimpleStringProperty time;
private final SimpleFloatProperty dataSensorOne;
private final SimpleFloatProperty dataSensorTwo;
private final SimpleFloatProperty dataSensorThree;
public TempTableData(String time, float dataSensorOne, float dataSensorTwo, float dataSensorThree){
this.time = new SimpleStringProperty(time);
this.dataSensorOne = new SimpleFloatProperty(dataSensorOne);
this.dataSensorTwo = new SimpleFloatProperty(dataSensorTwo);
this.dataSensorThree = new SimpleFloatProperty(dataSensorThree);
}
public String getTime() {
return time.get();
}
public void setTime(String time) {
this.time.set(time);
}
public float getDataSensorOne() {
return dataSensorOne.get();
}
public void setDataSensorOne(float dataSensorOne) {
this.dataSensorOne.set(dataSensorOne);
}
public float getDataSensorTwo() {
return dataSensorTwo.get();
}
public void setDataSensorTwo(float dataSensorTwo) {
this.dataSensorTwo.set(dataSensorTwo);
}
public float getDataSensorThree() {
return dataSensorThree.get();
}
public void setDataSensorThree(float dataSensorThree) {
this.dataSensorThree.set(dataSensorThree);
}
public String toString(){
String string = String.format("[time: %s | dataSensorOne: %f |dataSensorTwo: %f |dataSensorThree: %f ]",
time.get(), dataSensorOne.get(), dataSensorTwo.get(), dataSensorThree.get());
return string;
}
}
why you creating table columns again ? ,as they already created with #FXML annotation (injection!).
remove column instance creation lines from your code and everything will be fine
// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();
make sure your cell factory values are spelt exactly as the corresponding model class values.
e.g.
private final .... SimpleStringProperty time;
and
new ...... PropertyValueFactory("time"));

Resources