Enhance image field editor.
Added reset button to image editor dialog, handle user cancellation of dialog correctly.
This commit is contained in:
parent
fb02b1955a
commit
3a82aaec93
|
@ -37,10 +37,10 @@ import org.compiere.model.MLookupFactory;
|
|||
import org.compiere.util.DisplayType;
|
||||
import org.compiere.util.Env;
|
||||
import org.compiere.util.NamePair;
|
||||
import org.zkoss.zk.ui.HtmlBasedComponent;
|
||||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zk.ui.event.Events;
|
||||
import org.zkoss.zul.Image;
|
||||
import org.zkoss.zul.Listbox;
|
||||
import org.zkoss.zul.Listcell;
|
||||
import org.zkoss.zul.Listitem;
|
||||
|
@ -138,7 +138,7 @@ public class GridTabListItemRenderer implements ListitemRenderer, ListitemRender
|
|||
editor.addValueChangeListener(dataBinder);
|
||||
}
|
||||
cell.appendChild(editor.getComponent());
|
||||
if (editor.getComponent() instanceof Checkbox) {
|
||||
if (editor.getComponent() instanceof Checkbox || editor.getComponent() instanceof Image) {
|
||||
cell.setStyle("text-align:center");
|
||||
}
|
||||
else if (DisplayType.isNumeric(gridField.getDisplayType())) {
|
||||
|
@ -155,22 +155,7 @@ public class GridTabListItemRenderer implements ListitemRenderer, ListitemRender
|
|||
}
|
||||
|
||||
//streach component to fill grid cell
|
||||
if (editor.getComponent() instanceof HtmlBasedComponent) {
|
||||
//can't stretch bandbox & datebox
|
||||
if (!(editor.getComponent() instanceof Bandbox) &&
|
||||
!(editor.getComponent() instanceof Datebox)) {
|
||||
String width = "100%";
|
||||
if (editor.getComponent() instanceof Button) {
|
||||
Button btn = (Button) editor.getComponent();
|
||||
String zclass = btn.getZclass();
|
||||
if (!zclass.contains("form-button ")) {
|
||||
btn.setZclass("form-button " + zclass);
|
||||
}
|
||||
} else {
|
||||
((HtmlBasedComponent)editor.getComponent()).setWidth(width);
|
||||
}
|
||||
}
|
||||
}
|
||||
editor.fillHorizontal();
|
||||
}
|
||||
|
||||
return cell;
|
||||
|
|
|
@ -30,9 +30,11 @@ import org.adempiere.webui.event.ValueChangeEvent;
|
|||
import org.adempiere.webui.event.ValueChangeListener;
|
||||
import org.compiere.model.GridField;
|
||||
import org.compiere.model.GridTab;
|
||||
import org.compiere.util.DisplayType;
|
||||
import org.zkoss.zk.ui.Component;
|
||||
import org.zkoss.zk.ui.HtmlBasedComponent;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zul.Image;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -438,9 +440,17 @@ public abstract class WEditor implements EventListener, PropertyChangeListener
|
|||
if (getComponent() instanceof Button) {
|
||||
Button btn = (Button) getComponent();
|
||||
String zclass = btn.getZclass();
|
||||
if (!zclass.contains("form-button ")) {
|
||||
if (gridField.getDisplayType() == DisplayType.Image) {
|
||||
if (!zclass.contains("image-button-field ")) {
|
||||
btn.setZclass("image-button-field " + zclass);
|
||||
}
|
||||
} else if (!zclass.contains("form-button ")) {
|
||||
btn.setZclass("form-button " + zclass);
|
||||
}
|
||||
} else if (getComponent() instanceof Image) {
|
||||
Image image = (Image) getComponent();
|
||||
image.setWidth("48px");
|
||||
image.setHeight("48px");
|
||||
} else {
|
||||
((HtmlBasedComponent)getComponent()).setWidth(width);
|
||||
}
|
||||
|
|
|
@ -14,15 +14,18 @@
|
|||
package org.adempiere.webui.editor;
|
||||
|
||||
|
||||
import org.adempiere.webui.component.Button;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.adempiere.webui.event.ValueChangeEvent;
|
||||
import org.adempiere.webui.window.WImageDialog;
|
||||
import org.compiere.model.GridField;
|
||||
import org.compiere.model.MImage;
|
||||
import org.compiere.util.CLogger;
|
||||
import org.compiere.util.Env;
|
||||
import org.zkoss.image.AImage;
|
||||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.Events;
|
||||
import org.zkoss.zul.Image;
|
||||
|
||||
/**
|
||||
* This class is based on org.compiere.grid.ed.VImage written by Jorg Janke.
|
||||
|
@ -35,7 +38,8 @@ public class WImageEditor extends WEditor
|
|||
{
|
||||
private static final String[] LISTENER_EVENTS = {Events.ON_CLICK};
|
||||
|
||||
private static final CLogger logger;
|
||||
@SuppressWarnings("unused")
|
||||
private static final CLogger logger;
|
||||
|
||||
static
|
||||
{
|
||||
|
@ -48,24 +52,29 @@ public class WImageEditor extends WEditor
|
|||
private String m_columnName = "AD_Image_ID";
|
||||
|
||||
private boolean m_mandatory;
|
||||
|
||||
private Object readWrite;
|
||||
|
||||
private boolean readwrite;
|
||||
|
||||
/** Logger */
|
||||
private static CLogger log = CLogger.getCLogger(WImageEditor.class);
|
||||
|
||||
public WImageEditor(GridField gridField)
|
||||
{
|
||||
super(new Button(), gridField);
|
||||
super(new Image(), gridField);
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Button getComponent() {
|
||||
return (Button) component;
|
||||
public Image getComponent() {
|
||||
return (Image) component;
|
||||
}
|
||||
|
||||
private void init()
|
||||
{
|
||||
getComponent().setLabel("-");
|
||||
AImage img = null;
|
||||
getComponent().setContent(img);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -97,12 +106,16 @@ public class WImageEditor extends WEditor
|
|||
|
||||
@Override
|
||||
public boolean isReadWrite() {
|
||||
return getComponent().isEnabled();
|
||||
return readwrite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadWrite(boolean readWrite) {
|
||||
getComponent().setEnabled(readWrite);
|
||||
this.readwrite = readWrite;
|
||||
if (readWrite)
|
||||
getComponent().setStyle("cursor: pointer; border: 1px solid;");
|
||||
else
|
||||
getComponent().setStyle("cursor: default; border: none;");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -114,7 +127,8 @@ public class WImageEditor extends WEditor
|
|||
if (newValue == 0)
|
||||
{
|
||||
m_mImage = null;
|
||||
getComponent().setLabel("-");
|
||||
AImage img = null;
|
||||
getComponent().setContent(img);
|
||||
return;
|
||||
}
|
||||
// Get/Create Image
|
||||
|
@ -122,9 +136,15 @@ public class WImageEditor extends WEditor
|
|||
m_mImage = MImage.get (Env.getCtx(), newValue);
|
||||
//
|
||||
log.fine(m_mImage.toString());
|
||||
getComponent().setLabel(m_mImage.getName());
|
||||
if (m_mImage.getDescription() != null)
|
||||
getComponent().setTooltiptext(m_mImage.getDescription());
|
||||
AImage img = null;
|
||||
byte[] data = m_mImage.getData();
|
||||
if (data != null && data.length > 0) {
|
||||
try {
|
||||
img = new AImage(null, data);
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -135,20 +155,22 @@ public class WImageEditor extends WEditor
|
|||
|
||||
public void onEvent(Event event) throws Exception
|
||||
{
|
||||
if (Events.ON_CLICK.equals(event.getName()))
|
||||
if (Events.ON_CLICK.equals(event.getName()) && readwrite)
|
||||
{
|
||||
WImageDialog vid = new WImageDialog(m_mImage);
|
||||
int AD_Image_ID = vid.getAD_Image_ID();
|
||||
Object oldValue = getValue();
|
||||
Integer newValue = null;
|
||||
if (AD_Image_ID != 0)
|
||||
newValue = new Integer (AD_Image_ID);
|
||||
//
|
||||
m_mImage = null; // force reload
|
||||
setValue(newValue); // set explicitly
|
||||
//
|
||||
ValueChangeEvent vce = new ValueChangeEvent(this, gridField.getColumnName(), oldValue, newValue);
|
||||
fireValueChange(vce);
|
||||
if (!vid.isCancel()) {
|
||||
int AD_Image_ID = vid.getAD_Image_ID();
|
||||
Object oldValue = getValue();
|
||||
Integer newValue = null;
|
||||
if (AD_Image_ID != 0)
|
||||
newValue = new Integer (AD_Image_ID);
|
||||
//
|
||||
m_mImage = null; // force reload
|
||||
setValue(newValue); // set explicitly
|
||||
//
|
||||
ValueChangeEvent vce = new ValueChangeEvent(this, gridField.getColumnName(), oldValue, newValue);
|
||||
fireValueChange(vce);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,7 +98,8 @@ public class WImageDialog extends Window implements EventListener
|
|||
private Label fileLabel = new Label();
|
||||
private Button fileButton = new Button();
|
||||
private Image image = new Image();
|
||||
private ConfirmPanel confirmPanel = new ConfirmPanel(true);
|
||||
private ConfirmPanel confirmPanel = new ConfirmPanel(true,false,true,false,false,false);
|
||||
private boolean cancel = false;
|
||||
|
||||
/**
|
||||
* Static Init
|
||||
|
@ -147,18 +148,31 @@ public class WImageDialog extends Window implements EventListener
|
|||
if (e.getTarget() == fileButton)
|
||||
cmd_file();
|
||||
|
||||
else if (e.getTarget().getId().equals("Ok"))
|
||||
else if (e.getTarget().getId().equals(ConfirmPanel.A_OK))
|
||||
{
|
||||
if (m_mImage.save())
|
||||
detach();
|
||||
else
|
||||
FDialog.error(-1, "Failed to save image");
|
||||
}
|
||||
else if (e.getTarget().getId().equals("Cancel"))
|
||||
else if (e.getTarget().getId().equals(ConfirmPanel.A_CANCEL))
|
||||
{
|
||||
m_mImage = null; // reset
|
||||
cancel = true;
|
||||
detach();
|
||||
}
|
||||
else if (e.getTarget().getId().equals(ConfirmPanel.A_RESET))
|
||||
{
|
||||
m_mImage = null;
|
||||
AImage img = null;
|
||||
image.setContent(img);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if dialog cancel by user
|
||||
*/
|
||||
public boolean isCancel() {
|
||||
return cancel;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue