Make required indicator appearing at right of property name.

This commit is contained in:
robin shine 2013-09-22 09:31:49 +08:00
parent a4c7cc6cd5
commit b2a394715f
13 changed files with 80 additions and 125 deletions

View File

@ -10,6 +10,10 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import com.pmease.commons.editable.annotation.Editable;
import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.util.GeneralException;
@ -125,4 +129,14 @@ public class EditableUtils {
}
}
public static boolean isPropertyRequired(Method propertyGetter) {
if (propertyGetter.getReturnType().isPrimitive() && propertyGetter.getReturnType() != boolean.class
|| propertyGetter.getAnnotation(NotNull.class) != null
|| propertyGetter.getAnnotation(NotEmpty.class) != null) {
return true;
} else {
return false;
}
}
}

View File

@ -6,9 +6,6 @@ import java.lang.reflect.Method;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.util.BeanUtils;
@ -81,13 +78,7 @@ public abstract class PropertyEditContext extends AbstractEditContext {
}
public boolean isPropertyRequired() {
if (getPropertyGetter().getReturnType().isPrimitive()
|| getPropertyGetter().getAnnotation(NotNull.class) != null
|| getPropertyGetter().getAnnotation(NotEmpty.class) != null) {
return true;
} else {
return false;
}
return EditableUtils.isPropertyRequired(getPropertyGetter());
}
}

View File

@ -109,6 +109,7 @@ table td, table th {
}
.required {
font-size: 18px;
color: #B94A48;
}

View File

@ -6,6 +6,7 @@
<thead>
<th wicket:id="headers">
<span wicket:id="header"></span>
<span wicket:id="required" class="required"></span>
</th>
</thead>
<tbody>

View File

@ -110,6 +110,14 @@ public class TableListPropertyEditor extends Panel {
@Override
protected void populateItem(ListItem<Method> item) {
item.add(new Label("header", EditableUtils.getName(item.getModelObject())));
String required;
if (EditableUtils.isPropertyRequired(item.getModelObject()))
required = "*";
else
required = "&nbsp;";
item.add(new Label("required", required).setEscapeModelStrings(false));
}
});

View File

@ -3,7 +3,9 @@ package com.pmease.commons.wicket.editable.nuemric;
import java.io.Serializable;
import java.util.Map;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
@ -30,7 +32,18 @@ public class NumericPropertyEditContext extends PropertyEditContext {
@Override
public Object renderForEdit(Object renderParam) {
return new NumericPropertyEditor((String) renderParam, this);
return new TextField<String>((String) renderParam, getInputModel()) {
@Override
protected void onComponentTag(ComponentTag tag) {
tag.setName("input");
tag.put("type", "text");
tag.put("class", "form-control");
super.onComponentTag(tag);
}
};
}
@Override

View File

@ -1,16 +0,0 @@
<wicket:panel>
<div wicket:id="content"></div>
<wicket:fragment wicket:id="required">
<div class="input-group">
<input wicket:id="input" type="text" class="form-control">
<span class="input-group-addon required" title="This field is required">
<span class="glyphicon glyphicon-asterisk"></span>
</span>
</div>
</wicket:fragment>
<wicket:fragment wicket:id="notRequired">
<input wicket:id="input" type="text" class="form-control">
</wicket:fragment>
</wicket:panel>

View File

@ -1,31 +0,0 @@
package com.pmease.commons.wicket.editable.nuemric;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
@SuppressWarnings("serial")
public class NumericPropertyEditor extends Panel {
private final NumericPropertyEditContext editContext;
public NumericPropertyEditor(String id, NumericPropertyEditContext editContext) {
super(id);
this.editContext = editContext;
}
@Override
protected void onInitialize() {
super.onInitialize();
Fragment fragment;
if (editContext.isPropertyRequired()) {
fragment = new Fragment("content", "required", this);
} else {
fragment = new Fragment("content", "notRequired", this);
}
fragment.add(new TextField<String>("input", editContext.getInputModel()));
add(fragment);
}
}

View File

@ -12,6 +12,7 @@
<tr wicket:id="properties">
<td class="name control-label">
<span wicket:id="name"></span>
<span wicket:id="required" class="required"></span>
</td>
<td class="value">
<div wicket:id="value"></div>

View File

@ -65,6 +65,15 @@ public class ReflectionBeanEditor extends Panel {
protected void populateItem(ListItem<PropertyEditContext> item) {
final PropertyEditContext propertyContext = item.getModelObject();
item.add(new Label("name", EditableUtils.getName(propertyContext.getPropertyGetter())));
String required;
if (propertyContext.isPropertyRequired())
required = "*";
else
required = "&nbsp;";
item.add(new Label("required", required).setEscapeModelStrings(false));
item.add((Component)propertyContext.renderForEdit("value"));
String description = EditableUtils.getDescription(propertyContext.getPropertyGetter());

View File

@ -3,7 +3,10 @@ package com.pmease.commons.wicket.editable.string;
import java.io.Serializable;
import java.util.Map;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import com.pmease.commons.editable.EditContext;
import com.pmease.commons.editable.PropertyEditContext;
@ -17,7 +20,34 @@ public class StringPropertyEditContext extends PropertyEditContext {
@Override
public Object renderForEdit(Object renderParam) {
return new StringPropertyEditor((String) renderParam, this);
return new TextField<String>((String) renderParam, new IModel<String>() {
@Override
public void detach() {
}
@Override
public String getObject() {
return (String) getPropertyValue();
}
@Override
public void setObject(String object) {
setPropertyValue(object);
}
}) {
@Override
protected void onComponentTag(ComponentTag tag) {
tag.setName("input");
tag.put("type", "text");
tag.put("class", "form-control");
super.onComponentTag(tag);
}
};
}
@Override

View File

@ -1,16 +0,0 @@
<wicket:panel>
<div wicket:id="content"></div>
<wicket:fragment wicket:id="required">
<div class="input-group">
<input wicket:id="input" type="text" class="form-control">
<span class="input-group-addon required" title="This field is required">
<span class="glyphicon glyphicon-asterisk"></span>
</span>
</div>
</wicket:fragment>
<wicket:fragment wicket:id="notRequired">
<input wicket:id="input" type="text" class="form-control">
</wicket:fragment>
</wicket:panel>

View File

@ -1,50 +0,0 @@
package com.pmease.commons.wicket.editable.string;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
@SuppressWarnings("serial")
public class StringPropertyEditor extends Panel {
private final StringPropertyEditContext editContext;
public StringPropertyEditor(String id, StringPropertyEditContext editContext) {
super(id);
this.editContext = editContext;
}
@Override
protected void onInitialize() {
super.onInitialize();
Fragment fragment;
if (editContext.isPropertyRequired()) {
fragment = new Fragment("content", "required", this);
} else {
fragment = new Fragment("content", "notRequired", this);
}
fragment.add(new TextField<String>("input", new IModel<String>() {
@Override
public void detach() {
}
@Override
public String getObject() {
return (String) editContext.getPropertyValue();
}
@Override
public void setObject(String object) {
editContext.setPropertyValue(object);
}
}));
add(fragment);
}
}