* Added editor for DisplayType.Image

This commit is contained in:
Heng Sin Low 2008-07-11 08:59:36 +00:00
parent 6da7b54dd9
commit a431aa4603
3 changed files with 370 additions and 0 deletions

View File

@ -0,0 +1,149 @@
/******************************************************************************
* Product: Posterita Ajax UI *
* Copyright (C) 2007 Posterita Ltd. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* Posterita Ltd., 3, Draper Avenue, Quatre Bornes, Mauritius *
* or via info@posterita.org or http://www.posterita.org/ *
*****************************************************************************/
package org.adempiere.webui.editor;
import org.adempiere.webui.component.Button;
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.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
/**
* This class is based on org.compiere.grid.ed.VButton written by Jorg Janke.
* @author Jorg Janke
*
* Modifications - UI Compatibility
* @author ashley
*/
public class WImageEditor extends WEditor
{
private static final String[] LISTENER_EVENTS = {};
private static final CLogger logger;
static
{
logger = CLogger.getCLogger(WImageEditor.class);
}
/** The Image Model */
private MImage m_mImage = null;
/** Column Name */
private String m_columnName = "AD_Image_ID";
private boolean m_mandatory;
/** Logger */
private static CLogger log = CLogger.getCLogger(WImageEditor.class);
public WImageEditor(GridField gridField)
{
super(new Button(), gridField);
init();
}
@Override
public Button getComponent() {
return (Button) component;
}
private void init()
{
getComponent().setLabel("-");
getComponent().addEventListener(Events.ON_CLICK, this);
}
@Override
public String getDisplay()
{
return m_mImage.getName();
}
@Override
public Object getValue()
{
if (m_mImage == null || m_mImage.get_ID() == 0)
return null;
return new Integer(m_mImage.get_ID());
}
@Override
public boolean isMandatory()
{
return m_mandatory;
}
@Override
public void setMandatory(boolean mandatory)
{
m_mandatory = mandatory;
}
@Override
public void setValue(Object value)
{
int newValue = 0;
if (value instanceof Integer)
newValue = ((Integer)value).intValue();
if (newValue == 0)
{
m_mImage = null;
getComponent().setLabel("-");
return;
}
// Get/Create Image
if (m_mImage == null || newValue != m_mImage.get_ID())
m_mImage = MImage.get (Env.getCtx(), newValue);
//
log.fine(m_mImage.toString());
getComponent().setLabel(m_mImage.getName());
if (m_mImage.getDescription() != null)
getComponent().setTooltip(m_mImage.getDescription());
}
@Override
public String[] getEvents()
{
return LISTENER_EVENTS;
}
public void onEvent(Event event) throws Exception
{
if (Events.ON_CLICK.equals(event.getName()))
{
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);
}
}
}

View File

@ -135,6 +135,10 @@ public class WebEditorFactory
{
editor = new WAccountEditor(gridField);
}
else if (displayType == DisplayType.Image)
{
editor = new WImageEditor(gridField);
}
else
{
editor = new WUnknownEditor(gridField);

View File

@ -0,0 +1,217 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
*****************************************************************************/
package org.adempiere.webui.window;
import java.io.*;
import java.util.logging.*;
import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.component.Button;
import org.adempiere.webui.component.ConfirmPanel;
import org.adempiere.webui.component.Label;
import org.adempiere.webui.component.Panel;
import org.adempiere.webui.component.Window;
import org.compiere.model.*;
import org.compiere.util.*;
import org.zkoss.image.AImage;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zkex.zul.Borderlayout;
import org.zkoss.zkex.zul.Center;
import org.zkoss.zkex.zul.North;
import org.zkoss.zkex.zul.South;
import org.zkoss.zul.Fileupload;
import org.zkoss.zul.Image;
import org.zkoss.zul.Separator;
/**
* Image Dialog
*
* @author Jorg Janke
* @version $Id: VImageDialog.java,v 1.4 2006/07/30 00:51:28 jjanke Exp $
*/
public class WImageDialog extends Window implements EventListener
{
/**
* Constructor
* @param owner
* @param mImage
*/
public WImageDialog (MImage mImage)
{
super ();
this.setTitle(Msg.translate(Env.getCtx(), "AD_Image_ID"));
log.info("MImage=" + mImage);
m_mImage = mImage;
try
{
init();
}
catch(Exception ex)
{
log.log(Level.SEVERE, "", ex);
}
// load data
if (m_mImage == null)
m_mImage = MImage.get (Env.getCtx(), 0);
fileButton.setLabel(m_mImage.getName());
// imageLabel.setIcon(m_mImage.getIcon());
AEnv.showCenterScreen(this);
} // VImageDialog
/** Image Model */
private MImage m_mImage = null;
/** Logger */
private static CLogger log = CLogger.getCLogger(WImageDialog.class);
/** */
private Borderlayout mainLayout = new Borderlayout();
private Panel parameterPanel = new Panel();
private Label fileLabel = new Label();
private Button fileButton = new Button();
private Image image = new Image();
private ConfirmPanel confirmPanel = new ConfirmPanel(true);
/**
* Static Init
* @throws Exception
*/
void init() throws Exception
{
this.setWidth("450px");
this.setHeight("550px");
this.setSizable(true);
mainLayout.setParent(this);
mainLayout.setWidth("100%");
mainLayout.setHeight("100%");
mainLayout.setStyle("background-color: transparent");
fileLabel.setValue(Msg.getMsg(Env.getCtx(), "SelectFile"));
fileButton.setLabel("-");
North north = new North();
north.setParent(mainLayout);
north.setStyle("background-color: transparent");
north.appendChild(parameterPanel);
parameterPanel.appendChild(fileLabel);
parameterPanel.appendChild(new Separator());
parameterPanel.appendChild((fileButton));
Center center = new Center();
center.setFlex(true);
center.setParent(mainLayout);
center.appendChild(image);
center.setStyle("background-color: transparent");
South south = new South();
south.setStyle("background-color: transparent; border: none;");
south.setParent(mainLayout);
south.appendChild(confirmPanel);
//
fileButton.addEventListener(Events.ON_CLICK, this);
confirmPanel.addActionListener(Events.ON_CLICK, this);
} // jbInit
public void onEvent(Event e) throws Exception {
if (e.getTarget() == fileButton)
cmd_file();
else if (e.getTarget().getId().equals("Ok"))
{
if (m_mImage.save())
detach();
else
FDialog.error(-1, "Failed to save image");
}
else if (e.getTarget().getId().equals("Cancel"))
{
m_mImage = null; // reset
detach();
}
}
/**
* Load file & display
*/
private void cmd_file()
{
// Show File Open Dialog
Media imageFile = null;
try
{
imageFile = Fileupload.get();
if (imageFile != null)
{
// pdfViewer.setContent(media);
;
}
else
return;
}
catch (InterruptedException e)
{
log.warning(e.getLocalizedMessage());
return;
}
String fileName = imageFile.getName();
byte[] data = null;
// See if we can load & display it
try
{
InputStream is = imageFile.getStreamData();
AImage aImage = new AImage(fileName, is);
image.setContent(aImage);
is.close();
}
catch (Exception e)
{
log.log(Level.WARNING, "load image", e);
return;
}
// OK
fileButton.setLabel(imageFile.getName());
invalidate();
// Save info
m_mImage.setName(fileName);
m_mImage.setImageURL(fileName);
m_mImage.setBinaryData(data);
} // cmd_file
/**
* Get Image ID
* @return ID or 0
*/
public int getAD_Image_ID()
{
if (m_mImage != null)
return m_mImage.getAD_Image_ID();
return 0;
} // getAD_Image_ID
} // VImageDialog