Refactor the implementation for "Red color whole label of empty mandatory fields"

(transplanted from 07073ae97fb3462921d1ce8734830efabd22cece)
This commit is contained in:
Heng Sin Low 2011-08-20 08:11:15 +08:00
parent ba2bbb7107
commit c252c4475c
4 changed files with 32 additions and 28 deletions

View File

@ -28,8 +28,8 @@ import org.zkoss.zk.ui.Component;
*/ */
public class Label extends org.zkoss.zul.Label public class Label extends org.zkoss.zul.Label
{ {
/** /**
* *
*/ */
private static final long serialVersionUID = -6818124304324329510L; private static final long serialVersionUID = -6818124304324329510L;
@ -105,16 +105,5 @@ public class Label extends org.zkoss.zul.Label
public Component rightAlign() { public Component rightAlign() {
return LayoutUtils.makeRightAlign(this); return LayoutUtils.makeRightAlign(this);
} }
private boolean m_zoomable = false;
public boolean isZoomable() {
return m_zoomable;
}
public void setZoomable(boolean zoomable) {
m_zoomable = zoomable;
}
} }

View File

@ -218,5 +218,14 @@ public class WAccountEditor extends WEditor implements ContextMenuListener
} }
} }
@Override
public boolean isNullOrEmpty() {
Object value = getValue();
if (value != null && ((Integer) value).intValue() == 0)
value = null;
return value == null;
}
} }

View File

@ -47,7 +47,7 @@ import org.zkoss.zul.Image;
*/ */
public abstract class WEditor implements EventListener, PropertyChangeListener public abstract class WEditor implements EventListener, PropertyChangeListener
{ {
private static final String[] lISTENER_EVENTS = {}; private static final String[] lISTENER_EVENTS = {};
public static final int MAX_DISPLAY_LENGTH = 35; public static final int MAX_DISPLAY_LENGTH = 35;
@ -490,21 +490,28 @@ public abstract class WEditor implements EventListener, PropertyChangeListener
hasFocus = b; hasFocus = b;
} }
public void setMandatoryLabels() { public void updateLabelStyle() {
Object value = getValue();
if (this instanceof WAccountEditor && value != null && ((Integer) value).intValue() == 0) // special case
value = null;
if (getLabel() != null) { if (getLabel() != null) {
markMandatory(mandatory && !readOnly && getGridField().isEditable(true) && (value == null || value.toString().trim().length() == 0)); boolean mandatoryStyle = mandatory && !readOnly && getGridField().isEditable(true) && isNullOrEmpty();
getLabel().setStyle( (isZoomable() ? STYLE_ZOOMABLE_LABEL : "") + (mandatoryStyle ? STYLE_EMPTY_MANDATORY_LABEL : STYLE_NORMAL_LABEL));
}
}
public boolean isNullOrEmpty() {
Object value = getValue();
return value == null || value.toString().trim().length() == 0;
}
public boolean isZoomable() {
WEditorPopupMenu menu = getPopupMenu();
if (menu != null && menu.isZoomEnabled() && this instanceof IZoomableEditor) {
return true;
} else {
return false;
} }
} }
private static final String STYLE_ZOOMABLE_LABEL = "cursor: pointer; text-decoration: underline;"; private static final String STYLE_ZOOMABLE_LABEL = "cursor: pointer; text-decoration: underline;";
private static final String STYLE_NORMAL_LABEL = "color:black;"; private static final String STYLE_NORMAL_LABEL = "color: black;";
private static final String STYLE_EMPTY_MANDATORY_LABEL = "color: red;"; private static final String STYLE_EMPTY_MANDATORY_LABEL = "color: red;";
private void markMandatory(boolean mandatory) {
getLabel().setStyle( (getLabel().isZoomable() ? STYLE_ZOOMABLE_LABEL : "") + (mandatory ? STYLE_EMPTY_MANDATORY_LABEL : STYLE_NORMAL_LABEL));
}
} }

View File

@ -479,7 +479,6 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
Label label = editor.getLabel(); Label label = editor.getLabel();
if (popupMenu.isZoomEnabled() && editor instanceof IZoomableEditor) if (popupMenu.isZoomEnabled() && editor instanceof IZoomableEditor)
{ {
label.setZoomable(true);
label.addEventListener(Events.ON_CLICK, new ZoomListener((IZoomableEditor) editor)); label.addEventListener(Events.ON_CLICK, new ZoomListener((IZoomableEditor) editor));
} }
@ -542,7 +541,7 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
for (WEditor comp : editors) for (WEditor comp : editors)
{ {
comp.setMandatoryLabels(); comp.updateLabelStyle();
} }
// Selective // Selective