<pe:masterDetail id="masterDetail" selectLevelListener="#{selectLevelListener.handleNavigation}"
showAllBreadcrumbItems="true">
<f:facet name="header">
<p:messages showDetail="false" showSummary="true"/>
</f:facet>
<pe:masterDetailLevel level="1">
<f:facet name="label">
<h:outputFormat value="Overview {0}">
<f:param value="Persons"/>
</h:outputFormat>
</f:facet>
<p:dataTable id="persons" value="#{complexMasterDetailController.persons}" var="pers">
<p:column headerText="Name">
<p:commandLink value="#{pers.name}">
<pe:selectDetailLevel contextValue="#{pers}"/>
</p:commandLink>
</p:column>
<p:column headerText="Tax Class">
<h:outputText value="#{pers.taxClass}"/>
</p:column>
<p:column headerText="Birth Date">
<h:outputText value="#{pers.birthDate}">
<f:convertDateTime pattern="dd.MM.yyyy"/>
</h:outputText>
</p:column>
</p:dataTable>
</pe:masterDetailLevel>
<pe:masterDetailLevel level="2" contextVar="person" levelLabel="Person Detail">
<h:panelGrid id="personDetail" columns="2" columnClasses="formColumn1,formColumn2">
<h:outputText value="Name (string)"/>
<p:inputText value="#{person.name}" required="true" label="Name"/>
<h:outputText value="Tax Class (number)"/>
<p:inputText value="#{person.taxClass}" required="true" label="Tax Class">
<f:validateLength maximum="1"/>
</p:inputText>
<h:outputText value="Birth Date (date dd.MM.yyyy)"/>
<p:inputText value="#{person.birthDate}" required="true" label="Birth Date">
<f:convertDateTime pattern="dd.MM.yyyy"/>
</p:inputText>
<h:outputText value="Language skills"/>
<h:panelGrid columns="2" style="vertical-align: middle;">
<p:dataList value="#{person.languageSkills}" var="skill" itemType="circle">
<h:outputText value="#{skill}"/>
</p:dataList>
<p:commandLink immediate="true" style="padding:5px 0 0 8px; vertical-align:middle;">
<h:graphicImage library="images" name="flashlight_plus.png" style="border:none;"/>
<pe:selectDetailLevel contextValue="#{person}" preserveInputs="@all"/>
<pe:tooltip value="Add language skills"/>
</p:commandLink>
</h:panelGrid>
</h:panelGrid>
<p:commandButton value="Save with success" process="masterDetail"
action="#{complexMasterDetailController.saveSuccess(person)}"
style="margin-top:10px;" icon="pi pi-save">
<pe:selectDetailLevel level="1"/>
</p:commandButton>
<p:commandButton value="Save with failure" process="masterDetail"
action="#{complexMasterDetailController.saveFailure(person)}"
style="margin-top:10px;" icon="pi pi-save">
<pe:selectDetailLevel level="1"/>
</p:commandButton>
<p:commandButton type="button" value="Delete" onclick="PF('deletedDlg').show()" style="margin-top:10px;"/>
<p:dialog header="Delete confirmation" widgetVar="deletedDlg" resizable="false">
<h:panelGroup layout="block" style="padding:5px;">
<h:outputText value="Are you sure you want to delete this person?"/>
</h:panelGroup>
<p:commandButton id="deleteBtn" value="Delete"
action="#{complexMasterDetailController.delete(person)}" process="@this"
style="margin-top:10px;">
<pe:selectDetailLevel level="1"/>
</p:commandButton>
<p:commandButton value="Cancel" type="button" onclick="PF('deletedDlg').hide();"
style="margin-top:10px;"/>
</p:dialog>
</pe:masterDetailLevel>
<pe:masterDetailLevel level="3" contextVar="person" levelLabel="Language skills">
<h:panelGrid columns="2" columnClasses="formColumn1,formColumn2">
<h:outputText value="Choose language skill"/>
<p:selectOneMenu id="languageSkills" value="#{complexMasterDetailController.languageSkillToAdd}"
style="width:200px;">
<f:selectItems value="#{complexMasterDetailController.availableLanguageSkills}"/>
<p:ajax event="valueChange" process="masterDetail"
listener="#{complexMasterDetailController.addLanguageSkill(person)}"/>
<pe:selectDetailLevel step="-1"/>
</p:selectOneMenu>
</h:panelGrid>
</pe:masterDetailLevel>
</pe:masterDetail>
@Named
@ViewScoped
public class ComplexMasterDetailController implements Serializable {
private static final long serialVersionUID = 20111120L;
private List<Person> persons;
private List<SelectItem> availableLanguageSkills = null;
private final List<String> selectedLanguageSkills = new ArrayList<String>();
private String languageSkillToAdd;
public ComplexMasterDetailController() {
if (persons == null) {
persons = new ArrayList<Person>();
final Calendar calendar = Calendar.getInstance();
calendar.set(1972, 5, 25);
persons.add(new Person("1", "Max Mustermann", 1, calendar.getTime()));
calendar.set(1981, 12, 10);
persons.add(new Person("2", "Sara Schmidt", 2, calendar.getTime()));
calendar.set(1968, 2, 13);
persons.add(new Person("3", "Jasper Morgan", 3, calendar.getTime()));
}
}
public List<Person> getPersons() {
return persons;
}
public List<SelectItem> getAvailableLanguageSkills() {
if (availableLanguageSkills == null) {
availableLanguageSkills = new ArrayList<SelectItem>();
availableLanguageSkills.add(new SelectItem("English", "English"));
availableLanguageSkills.add(new SelectItem("German", "German"));
availableLanguageSkills.add(new SelectItem("Russian", "Russian"));
availableLanguageSkills.add(new SelectItem("Turkish", "Turkish"));
availableLanguageSkills.add(new SelectItem("Dutch", "Dutch"));
availableLanguageSkills.add(new SelectItem("French", "French"));
availableLanguageSkills.add(new SelectItem("Italian", "Italian"));
}
return availableLanguageSkills;
}
public List<String> getSelectedLanguageSkills() {
return selectedLanguageSkills;
}
public void setLanguageSkillToAdd(final String languageSkillToAdd) {
this.languageSkillToAdd = languageSkillToAdd;
}
public String getLanguageSkillToAdd() {
return languageSkillToAdd;
}
public String saveSuccess(final Person person) {
final FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
"Person " + person.getName() + " has been saved", null);
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}
public String saveFailure(final Person person) {
final FacesContext fc = FacesContext.getCurrentInstance();
final ELContext elContext = fc.getELContext();
SelectLevelListener selectLevelListener;
try {
selectLevelListener = (SelectLevelListener) elContext.getELResolver().getValue(elContext, null,
"selectLevelListener");
selectLevelListener.setErrorOccured(true);
}
catch (final RuntimeException e) {
throw new FacesException(e.getMessage(), e);
}
final FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Person " + person.getName() + " could not be saved", null);
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}
public String delete(final Person person) {
for (final Person pers : persons) {
if (pers.getId().equals(person.getId())) {
persons.remove(pers);
break;
}
}
return null;
}
public void addLanguageSkill(final Person person) {
if (languageSkillToAdd != null) {
person.addLanguageSkill(languageSkillToAdd);
}
languageSkillToAdd = null;
}
}
@Named
@RequestScoped
public class SelectLevelListener {
private boolean errorOccured = false;
public int handleNavigation(final SelectLevelEvent selectLevelEvent) {
if (errorOccured) {
return 2;
}
else {
return selectLevelEvent.getNewLevel();
}
}
public void setErrorOccured(final boolean errorOccured) {
this.errorOccured = errorOccured;
}
}
public class Person implements Serializable {
private static final long serialVersionUID = 20111128L;
private String id;
private String name;
private int taxClass;
private Date birthDate;
private Set<String> languageSkills = new HashSet<String>();
public Person(String id, String name, int taxClass, Date birthDate) {
this.id = id;
this.name = name;
this.taxClass = taxClass;
this.birthDate = birthDate;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTaxClass() {
return taxClass;
}
public void setTaxClass(int taxClass) {
this.taxClass = taxClass;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String[] getLanguageSkills() {
return languageSkills.toArray(new String[languageSkills.size()]);
}
public void addLanguageSkill(String languageSkill) {
languageSkills.add(languageSkill);
}
}