When a user clicks on the edit link in a wicket data grid component, a new window would open and they would modify whatever appears on the new form. In the datagrid, there are many rows. How can I get the index number of the row I want to edit? Please see below, the onclick event of the “edit” link.
columnList.add(new AbstractLinkColumn<MyModel>(new Model<String>("Edit")) {
#Override
protected AbstractLink newLink(String componentId, final IModel<MyModel> rowModel) {
return new AjaxLink<String>(componentId, getDisplayModel()) {
#Override
public void onClick(AjaxRequestTarget target) {
ModalWindow myModelWindow = requestForm.getmyModelWindow();
MyPanel panel = new MyPanel(myModelWindow.getContentId(), requestForm
.getModelObject(), myModelWindow, rowModel.getObject(), false, isSetReadOnly);
myModelWindow.setContent(panel);
Ricola.refresh(target, panel);
myModelWindow.show(target);
}
};
}
#Override
protected Label newLinkLabel(String componentId, IModel<MyModel> rowModel) {
return new Label(componentId, getDisplayModel());
}
}.setTooltip(new Model<String>("Click the Edit link to edit the line")));
public class MyColumn extends AbstractColumn {
#Override
public void populateItem(Item item, String componentId, IModel model) {
Item rowItem = item.findParent(Item.class);
int rowIndex = rowItem.getIndex();
...
}
}
Related
I would like to have this functionality in my program:
I will have a user input field. When the user pressed the button, it will be added to the list, and input will be shown to the user.
The problem is, I would like to deselect/remove those input if the user wants. I could not achieve this.
Here is the code I have written so far, I have removed some functionality unnecessary for the question's scope:
public class AddUserInput extends VerticalLayout{
// The user input will be added to the this list
// later, this list will be sent to the server for some verification
private List<String> emails;
private HorizontalLayout content;
private VerticalLayout rows;
// user input field
private TextField emailField = new TextField("Enter email address");
public AddUserInput() {
content = new HorizontalLayout();
rows = new VerticalLayout();
content.setMargin(true);
Button addToListButton= new Button("Add to list");
addToListButton.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(Button.ClickEvent event) {
// When the user clicks add to list button
// The raw input will be added to the emails list
// The UI component is added to 'rows' component
rows.addComponent(addNewRow(emailField.getValue()));
}
});
content.addComponents(emailField, addToListButton, rows);
addComponent(content);
}
public Component addNewRow(String email){
HorizontalLayout newRow = new HorizontalLayout();
Button deleteRowButton = new Button("-");
deleteRowButton.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(Button.ClickEvent event) {
// I can delete from the UI by using the code below
newRow.removeAllComponents();
rows.removeComponent(newRow);
// How to remove from the email list???
}
});
emails.add(emailField.getValue());
Label lastEmail = new Label(emailField.getValue());
emailField.clear();
newRow.addComponents(lastEmail,deleteRowButton);
return newRow;
}
}
Is there any component/library that does this functionality?
I only need a text field, and adding the input to the list, and removing the list item if a user wants to.
The visualization of the code above:
You could use the NativeSelect component for managing the entered Strings.
I modified your AddUserInput-Component to use a NativeSelect and a corresponding DataProvider:
public class AddUserInput extends VerticalLayout {
private HorizontalLayout content = new HorizontalLayout();;
private NativeSelect<String> select = new NativeSelect<>("The List");
private ListDataProvider<String> dataProvider = DataProvider.ofCollection(new ArrayList<>());
private Button addToListButton= new Button("Add to list");
private Button deleteFromListButton = new Button("-");
private TextField emailField = new TextField("Enter email address");
public AddUserInput() {
select.setVisibleItemCount(5);
select.setWidth("100px");
select.setDataProvider(dataProvider);
select.setEmptySelectionAllowed(false);
deleteFromListButton.setEnabled(false);
content.setMargin(true);
addToListButton.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(Button.ClickEvent event) {
addEmailToList(emailField.getValue());
}
});
deleteFromListButton.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(Button.ClickEvent clickEvent) {
select.getSelectedItem().ifPresent(selectedItem -> removeSelectedEmailFromList());
}
});
select.addValueChangeListener(new HasValue.ValueChangeListener<String>() {
#Override
public void valueChange(HasValue.ValueChangeEvent<String> valueChangeEvent) {
deleteFromListButton.setEnabled(select.getSelectedItem().isPresent());
}
});
content.addComponents(emailField, addToListButton, select, deleteFromListButton);
addComponent(content);
}
private void addEmailToList(String email){
dataProvider.getItems().add(email);
select.getDataProvider().refreshAll();
emailField.clear();
}
private void removeSelectedEmailFromList(){
select.getSelectedItem().ifPresent(selectedItem -> dataProvider.getItems().remove(selectedItem));
select.setSelectedItem(dataProvider.getItems().isEmpty() ? null : dataProvider.getItems().iterator().next());
select.getDataProvider().refreshAll();
}
}
It looks like the following:
Would that be a possible option for you?
Whenever I click a row, I want to update the table with new information. For some reason when I click it is not doing anything and errors out. Any idea why I can't do this?
public HierarchyItemTreeCellCallback(TreeView<Requirement> treeView, DataModel dataModelIn)
{
super();
treeViewParent = treeView;
dataModel = dataModelIn;
}
#Override
public TreeCell<Requirement> call(TreeView<Requirement> param)
{
TreeCell<Requirement> cell = new HierarchyItemTreeCell();
cell.addEventFilter(MouseEvent.MOUSE_PRESSED,new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
HierarchyTabController hierarchyTabController = new HierarchyTabController(dataModel);
Requirement req1 = new Requirement("ID", "21", 1, REQUIREMENT_TYPE.SOFTWARE);
hierarchyTabController.updateTableRow(req1);
}
});
return cell;
}
Here is the controller logic:
public void updateTableRow(Requirement r)
{
REQUIREMENT_TYPE reqType = REQUIREMENT_TYPE.SOFTWARE;
Requirement req1 = new Requirement("ID", "21", 1, reqType);
ObservableList<Requirement> list = FXCollections.observableArrayList(req1);
name.setCellValueFactory(new PropertyValueFactory<>("name"));
description.setCellValueFactory(new PropertyValueFactory<>("description"));
tableView.getItems().setAll(list);
tableView.setItems(list);
}
This works whenever I do not call the updateTableRow function from the MouseEvent. However, it is not working when I try to call the controller and update the values from the MouseEvent. Any idea what I am doing wrong?
I Have a table with 4 columns. ReferenceID,Question,Category and Reviewed.
RefrenceId,Question Category are normal texts while Reviewed is a drop down with yes or blank value.
RefrenceId, Question,Category gets the value from a model object called QuestionType.
For reviewed it is always fixed value Yes , Blank not from the model object.
I have created the Tabel viewer, Created columns , Have added different cell editors, also editing support for review column.
Till now data is coming for all the column. But when I select the reviewed column with any selection, value is not appearing in the selected cell
Below is my code
private void createCheckListTableUI(Composite parent) {
checkListTableViewer = new TableViewer(tableContainer, SWT.RESIZE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.SINGLE);
checkListTableViewer.setContentProvider(new ObservableSetContentProvider());
checkListTableViewer.getTable().setHeaderVisible(true);
checkListTableViewer.getTable().setLinesVisible(true);
TableColumnLayout tcl = new TableColumnLayout();
createColumnsForCheckListTable(checkListTableViewer, tcl);
CellEditor[] editors = new CellEditor[4];
editors[0] = new TextCellEditor(checkListTableViewer.getTable());
editors[1] = new TextCellEditor(checkListTableViewer.getTable());
editors[2] = new TextCellEditor(checkListTableViewer.getTable());
editors[3] = new ComboBoxCellEditor(parent, reviewed, SWT.DROP_DOWN);
checkListTableViewer.setCellEditors(editors);
checkListTableViewer.setInput(checkListModel.getAvailableQuestList());
tableContainer.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(3, 30).create());
tableContainer.setLayout(tcl);
}
Column creation Code
private void createColumnsForCheckListTable(TableViewer checkListTableViewer, TableColumnLayout tcl) {
//RefID
TableViewerColumn refIdColumn = createColumn(checkListTableViewer, Constants.CHECKLIST_REFID, 80);
tcl.setColumnData(refIdColumn.getColumn(), new ColumnWeightData(80));
refIdColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
if (element instanceof QuestionType) {
QuestionType question = (QuestionType) element;
return question.getRefID();
}
return element.toString();
}
});
...
TableViewerColumn reviewedColumn = createColumn(checkListTableViewer, Constants.CHECKLIST_REVIEWED, 80);
ReviewColumnEditorSupport revColEditing = new ReviewColumnEditorSupport(reviewedColumn.getViewer(),checkListModel.getReviewedVal());
reviewedColumn.setEditingSupport(revColEditing);
tcl.setColumnData(reviewedColumn.getColumn(), new ColumnWeightData(90));
reviewedColumn.setLabelProvider(new CellLabelProvider() {
#Override
public void update(ViewerCell cell) {
if(cell.getElement() instanceof String) {
cell.setText(((String) cell.getElement()));
}
}
});
}
Editing Support class look like below
public final class ReviewColumnEditorSupport extends EditingSupport {
private String[] reviewed = {"","yes","No"};
private ComboBoxViewerCellEditor cellEditor = null;
private WritableValue isReviewedValue;
public ReviewColumnEditorSupport(ColumnViewer viewer, WritableValue isReviewedValue) {
super(viewer);
this.isReviewedValue = isReviewedValue;
cellEditor = new ComboBoxViewerCellEditor((Composite) getViewer().getControl(), SWT.READ_ONLY);
cellEditor.setLabelProvider(new LabelProvider());
cellEditor.setContentProvider(new ArrayContentProvider());
cellEditor.setInput(reviewed);
cellEditor.addListener(new ICellEditorListener() {
#Override
public void editorValueChanged(boolean oldValidState, boolean newValidState) {
// TODO Auto-generated method stub
}
#Override
public void cancelEditor() {
// TODO Auto-generated method stub
}
#Override
public void applyEditorValue() {
CCombo combo = (CCombo) cellEditor.getControl();
isReviewedValue.setValue(combo.getText());
}
});
}
#Override
protected CellEditor getCellEditor(Object element) {
return cellEditor;
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
if (element instanceof QuestionType) {
CCombo combo = (CCombo) cellEditor.getControl();
return combo.getText();
}
return null;
}
#Override
protected void setValue(Object element, Object value) {
if (element instanceof QuestionType && value instanceof String) {
// CCombo combo = (CCombo) cellEditor.getControl();
getViewer().update(value, null);
isReviewedValue.setValue((String)value);
}
}
Can anyone help me in understanding what is going wrong.
Since data fro reviewed column is not coming from model should there be special handling for this?
A sample code snippet is also fine.
getValue() and setValue() for combo should use integer value instead of string. Return index of value in string array from getValue() method, and in setValue() set the index of value. Probably you will be getting an int value there from 'value'. Try to debug.
if its string value then find the index of value in array. and
isReviewedValue.select(index);
I've a container which output markup placeholder tag is set to true. I want to display it only if I digit in a certain text field a certain string. For example if I digit "show" in text field, container appears, if I digit "hide" it disappears. I made this code:
container.setOutputPlaceHolderTag(true);
container.setOuputMarkupId(true);
textfield.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget target) {
form.modelChanged();
if ("SHOW".equals(textfield.getModelObject())) {
container.setVisible(true);
} else {
container.setVisible(false);
}
target.addComponent(container);
}
this code works only if I write SHOW, BUT when I write another string it doesn't disappear. To make it disappear I've to refresh the whole form they are into (and I don't want it).
How can I solve this problem??
some details: all component I'm refering to are in a form, and only if I refresh the form setVisible(false) works. From now only setVisible(true) works, it seems the container stucks on visibility true.
This code works:
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
private String someValue;
private WebMarkupContainer container;
public HomePage(final PageParameters parameters) {
super(parameters);
add(container = container());
add(textfield());
}
private WebMarkupContainer container() {
WebMarkupContainer wmc = new WebMarkupContainer("container") {
#Override
protected void onConfigure() {
super.onConfigure();
setVisible("SHOW".equals(someValue));
}
};
wmc.setOutputMarkupPlaceholderTag(true);
return wmc;
}
private TextField textfield() {
TextField tf = new TextField("textfield", new PropertyModel(HomePage.this, "someValue"));
tf.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget art) {
//just model update
art.add(container);
}
});
return tf;
}
}
Use
container.setOutputPlaceHolderTag(true);
container.setOuputMarkupId(true);
textfield.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget target) {
form.modelChanged();
if ("SHOW".equals(((TextField<String>) getComponent()).getModelObject())) { //change this
container.setVisible(true);
} else {
container.setVisible(false);
}
target.addComponent(container);
}
I got this tip from Getting a Wicket text box's value in an AJAX onchange event
I have a celltable. I want to add multiple labels with some tooltip assigned to each in one column of a celltable. what i have tried so far -
TextColumn<C> detailsColumn = new TextColumn<C>() {
#Override
public String getValue(C c) {
List<String[]> cList = c.getChngd();
if (cList == null || cList.size() == 0) {
return null;
}
Label lbl;
HorizontalPanel hpanel=new HorizontalPanel();
for (Iterator<String[]> itr =List.iterator(); itr.hasNext();) {
String[] detail = itr.next();
lbl=new Label();
lbl.setText(detail[0]);
lbl.addMouseOverHandler(new MouseOverHandler() {
#Override
public void onMouseOver(MouseOverEvent event) {
Widget source = (Widget)event.getSource();
source.setTitle("tooltip");
}
});
hpanel.add(lbl);
}
return hpanel.getElement().getInnerText();
}
};
Its not working. Any solutions for the same?
I'd move the tooltip logic into the Cell, rather than into the getCell() method of the Column, which is used only to retrieve the underlying data the cell is going to render.
If you want a simple title-based cell, the following should work. It creates a text cell with the data value wrapped in a div with a title.
public class TooltipTextCell extends TextCell {
public interface Template extends SafeHtmlTemplates {
#Template("<div title=\"{1}\" />{0}</div>")
SafeHtml label(SafeHtml text, String title);
}
private static Template template;
public TooltipTextCell() {
super();
if (template == null) {
template = GWT.create(Template.class);
}
}
#Override
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
if (value != null) {
sb.append(template.label(value, value.asString()));
}
}
}
If you want to create a column in which each cell can contain multiple of such TooltipTextCell, you have to use a CompositeCell.