Skip to content

Instantly share code, notes, and snippets.

@topriddy
Created February 7, 2012 10:32
Show Gist options
  • Save topriddy/1759026 to your computer and use it in GitHub Desktop.
Save topriddy/1759026 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:wicket>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>AlertPanel</title>
</head>
<body>
<wicket:panel>
<div wicket:id="wrapper" class="alert">
<a class="close" href="#" wicket:id="closeLink">&times;</a>
<p><span wicket:id="alertMessage"></span></p>
</div>
</wicket:panel>
</body>
</html>
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
/**
*
* @author topriddy
*/
public final class AlertPanel extends Panel {
public enum AlertType {
WARNING(""), ERROR("alert-error"), SUCCESS("alert-success"), INFO("alert-info");
private final String css;
AlertType(String css) {
this.css = css;
}
};
private String alertMessage;
AlertType alertType;
WebMarkupContainer wrapper;
public AlertPanel(String id, String alertMessage, AlertType alertType, final boolean closeable) {
super(id);
wrapper = new WebMarkupContainer("wrapper");
setOutputMarkupId(true);
setOutputMarkupPlaceholderTag(true);
add(wrapper);
this.alertMessage = alertMessage;
Label alertLabel = new Label("alertMessage", new PropertyModel<String>(this, "alertMessage"));
alertLabel.setEscapeModelStrings(false);
alertLabel.setOutputMarkupId(true);
alertLabel.setOutputMarkupPlaceholderTag(true);
AjaxFallbackLink closeLink = new AjaxFallbackLink("closeLink") {
@Override
public void onClick(AjaxRequestTarget target) {
if (target != null) {
AlertPanel.this.setVisible(closeable);
target.add(AlertPanel.this);
}
}
};
closeLink.setVisible(closeable);
setAlertType(alertType);
wrapper.add(alertLabel);
wrapper.add(closeLink);
}
public AlertType getAlertType() {
return alertType;
}
public void setAlertType(AlertType alertType) {
this.alertType = alertType;
wrapper.add(AttributeModifier.replace("class", "alert " + alertType.css));
}
public String getAlertMessage() {
return alertMessage;
}
public void setAlertMessage(String alertMessage) {
this.alertMessage = alertMessage;
}
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<wicket:panel>
<div wicket:id="feedbackul" class="feedbackPanel">
<div wicket:id="messages" class="errorlevel">
<div wicket:id="message"></div>
</div>
</div>
</wicket:panel>
</body>
</html>
import org.apache.wicket.Component;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
/**
*
* @author topriddy
*/
public class MyFeedbackPanel extends FeedbackPanel {
public MyFeedbackPanel(String id) {
super(id);
}
@Override
protected Component newMessageDisplayComponent(String id, FeedbackMessage message) {
AlertPanel alertPanel = new AlertPanel("message", message.getMessage().toString(), AlertPanel.AlertType.ERROR, false);
alertPanel.setVisible(true);
switch (message.getLevel()) {
case FeedbackMessage.INFO:
alertPanel.setAlertType(AlertPanel.AlertType.INFO);
break;
case FeedbackMessage.WARNING:
alertPanel.setAlertType(AlertPanel.AlertType.WARNING);
break;
case FeedbackMessage.SUCCESS:
alertPanel.setAlertType(AlertPanel.AlertType.SUCCESS);
break;
}
return alertPanel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment