How to hide a WebMarkUpContainer checking an AjaxCheckBox in Wicket - java

OK basically I have a WebMarkUpContainer which contains a DateTextField component and I want to make it visible only when I check an AjaxCheckBox.
In general my code is:
private static final class Results extends BootstrapForm<ResultsModel>
{
final AjaxCheckBox isExamsSuccess = new AjaxCheckBox("isExamsSuccess") {
private static final long serialVersionUID = 1L;
#Override
protected void onUpdate(AjaxRequestTarget target) {
updateModel();
toggleStep(target);
}
};
final WebMarkupContainer wmc = new WebMarkupContainer("wmc");
final DateTextField startDate = new DateTextField("startDate",
new DateTextFieldConfig()
.autoClose(true).withFormat("dd/MM/yyyy")
.withLanguage("el").withEndDate(new DateTime()));
public Results(String id, CompoundPropertyModel<ResultsModel> propertyModel)
{
super(id, propertyModel);
add(isExamsSuccess);
wmc.add(startDate);
add(wmc);
protected void toggleStep(AjaxRequestTarget target) {
if(isExamsSuccess.getModelObject() == true){
isExamsSuccess.setModelObject(true);
wmc.setVisible(true);
target.add(wmc);
}
else {
wmc.setVisible(false);
target.add(wmc);
}
}
}
I would really appreciate some help

Your code looks good! You just need to set the initial visibility of wmc to depend on isExamsSuccess:
wmc = new WebMarkupContainer("wmc") {
#Override public void onConfigure() {
super.onConfigure();
setVisible(isExamsSuccess.getModelObject());
}
}
wmc.setOutputMarkupPlaceholderTag(true);
In addition you need to call setOutputMarkupPlaceholderTag(true) because Wicket needs to be able to find the HTML element to turn the visibility from off to on.

Related

Wicket, how to get data line index number

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();
...
}
}

Container Visibility control on Change behavior of text field in Wicket 1.4

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

Wicket AjaxSelfUpdatingTimerBehavior refreshes the full page when opened with 2 browser tags

I have a label that checks for new notifications every 5 seconds and updates its text to represent the current notification count by using AjaxSelfUpdatingTimeBehaviour.
All is good until I open another browser tab with the same page, then on both tabs instead of updating the label, AjaxSelfUpdatingTimeBehaviour starts refreshing the page every 5 seconds.
private void initializeNotificationPanel() {
EventNotificationService notificationService = EventNotificationService
.getInstanceOfNotificationService();
List<Event> list = notificationService
.getSubscribedEvents(MyFoodSession.get().getUser());
final User currentUser = MyFoodSession.get().getUser();
final Label countLabel;
final WebMarkupContainer wmc = new WebMarkupContainer("markupContainer");
wmc.setOutputMarkupId(true);
final ListView<Event> listView = initListView(list);
wmc.add(listView);
listView.setVisible(false);
listView.setOutputMarkupId(true);
final AjaxLink<String> notificationLink = initializeAjaxLink(wmc,
listView);
if (notificationService.hasNotifications(currentUser)) {
countLabel = new Label("countlbl", list.size());
} else {
countLabel = new Label("countlbl", "0");
}
countLabel.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)) {
private static final long serialVersionUID = 1L;
EventNotificationService notificationService = EventNotificationService
.getInstanceOfNotificationService();
#Override
protected final void onPostProcessTarget(AjaxRequestTarget target) {
if (notificationService.hasNotifications(currentUser)) {
countLabel.setDefaultModelObject(""
+ notificationService.getSubscribedEvents(
MyFoodSession.get().getUser()).size());
} else {
countLabel.setDefaultModelObject("0");
}
}
});
wmc.add(notificationLink);
notificationLink.add(countLabel);
add(wmc);
}
I can't think of a reason why AjaxSelfUpdatingTimerBehavior should expose this behavior. You should create a quickstart and attache it to a new Jira issue.

GWT DialogBox that returns values back to the "opener"

I want to create a popup (implemented as a DialogBox or other similar component) which i should be able to reuse in multiple pages or forms. I want the DialogBox to be able to return a value to the "opener".
I am thinking i.e. on a DialogBox that shows a table (obtained via RPC). That DialogBox can be used in several different pages. When the user selects a row, an object is "passed back to the page" (for example, calling a method on it), so it can write it to a form field, or do whatever with it. The called doesn't know anything about the logic inside de DialogBox, only knows how to deal with the returning type.
A good example of what i'm intending to do could be a DatePicker that returns a java.util.Date.
Have you done something similiar?
I appreciate your help.
Thanks!
David
It's really easy. You should first create an interface that will be implemented by all the pages opening you DialogBox :
public interface DialogBoxOpener {
void dialogBoxValidated (Date selectedDate);
void dialogBoxCancelled ();
}
Then, create your DialogBox, and take a DialogBoxOpener as parameter to your showDialogBox method :
public class MyDialogBox extends DialogBox {
private DialogBoxOpener opener = null;
private final Button cancelButton = new Button("Cancel");
private final Button validButton = new Button("Ok");
private final DateBox myDateBox = new DateBox();
public MyDialogBox () {
cancelButton.addClickHandler(new ClickHandler () {
#Override
public void onClick(final ClickEvent event) {
hide();
if (opener!=null)
opener.dialogBoxCancelled();
}
});
validButton.addClickHandler(new ClickHandler () {
#Override
public void onClick(final ClickEvent event) {
hide();
if (opener!=null)
opener.dialogBoxValidated(myDateBox.getValue());
}
});
// TODO : create your DialogBox
}
public void showDialogBox (final DialogBoxOpener opener) {
this.opener = opener;
// Show the DialogBox
center ();
}
}
And now, you can show your DialogBox from your page :
public class MyPage implements DialogBoxOpener {
private MyDialogBox myDialogBox = getMyDialogBox();
private void openDialogBox () {
myDialogBox.showDialogBox(this);
}
public void dialogBoxValidated (Date selectedDate) {
// TODO : Do something with the date
}
public void dialogBoxCancelled () {
// TODO : Do something
}
}

java Swing -- JPanel and PropertyChangeListener

My use case is that a List<String> is passed to a Jpanel and for each String in the List, the JPanel renders a UI component. This UI component consists of 3 buttons and my current code for my given use case is as follows. -- The code for the 'UI component' follows --
public class MacroEditorEntity implements ActionListener {
private String macro;
private JButton upButton;
private JButton downButton;
private JButton MacroDetailsButton;
public MacroEditorEntity(String macro) {
this.macro = macro;
upButton = new JButton("Up");
downButton = new JButton("Down");
MacroDetailsButton = new JButton(macro);
upButton.addActionListener(this);
downButton.addActionListener(this);
MacroDetailsButton.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent evt) {
if(evt.getSource().equals(MacroDetailsButton))
{
System.out.println(macro);
}
}
public JButton GetUpButton()
{
return upButton;
}
public JButton GetDownButton()
{
return downButton;
}
public JButton getMacroDetailsButton()
{
return MacroDetailsButton;
}
}
The code for my Panel is as follows --
public class MacroEditor extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = 1L;
private List<String> stringlist;
public MacroEditor(List<String> list) {
this.stringlist = list;
setupComponents();
validate();
setVisible(true);
}
public void setupComponents()
{
Box allButtons = Box.createVerticalBox();
for(String string : stringlist)
{
MacroEditorEntity entry = new MacroEditorEntity(string);
Box entryBox = Box.createHorizontalBox();
entryBox.add(entry.GetUpButton());
entryBox.add(Box.createHorizontalStrut(15));
entryBox.add(entry.getMacroDetailsButton());
entryBox.add(Box.createHorizontalStrut(15));
entryBox.add(entry.GetDownButton());
allButtons.add(entryBox);
}
add(allButtons);
}
#Override
public void propertyChange(PropertyChangeEvent arg0) {
revalidate();
repaint();
}
}
The code works fine for all Strings in the passed List. I want my Panel to pick up any change that may happen to the List like additions or deletions and add/remove relevant corresponding UI components accordingly. I think this can be done by using PropertyChangeListener but have not been able to account for that in my code.
Any ideas or suggestions on how i can make my Panel render/rerender stuff as soon as there are changes to the List would be of help.
What you need here is an observable collection. This should do it: http://commons.apache.org/dormant/events/apidocs/org/apache/commons/events/observable/ObservableCollection.html
Edit:
Here's the code snippet you requested:
public class ObservableListExample implements StandardPostModificationListener,
StandardPreModificationListener {
public static void main(String[] args) {
new ObservableListExample();
}
public ObservableListExample() {
ObservableList list = ObservableList.decorate(new ArrayList<>(),
new StandardModificationHandler());
list.getHandler().addPostModificationListener(this);
list.getHandler().addPreModificationListener(this);
//....
}
#Override
public void modificationOccurring(StandardPreModificationEvent event) {
// before modification
Collection changeCollection = event.getChangeCollection();
if (event.isTypeAdd()) {
// changeCollection contains added elements
} else if (event.isTypeReduce()) {
// changeCollection contains removed elements
}
}
#Override
public void modificationOccurred(StandardPostModificationEvent event) {
// after modification
Collection changeCollection = event.getChangeCollection();
if (event.isTypeAdd()) {
// changeCollection contains added elements
} else if (event.isTypeReduce()) {
// changeCollection contains removed elements
}
}
}
By the way: Another concept that helps to bind buisness objects to your GUI and react to modifications (bidirectionally) is Data Binding. Have a look at this, a Data Binding Library commonly used with Swing.

Categories

Resources