From b8f61827dbecac7f8572be44deba3265f6095563 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Sun, 13 Jul 2008 01:38:02 +0000 Subject: [PATCH] * Implemented editor for blob/clob --- .../adempiere/webui/editor/WBinaryEditor.java | 126 ++++++ .../webui/editor/WebEditorFactory.java | 4 + .../adempiere/webui/panel/WAttachment.java | 165 +------- .../adempiere/webui/window/WImageDialog.java | 14 +- .../adempiere/webui/window/WMediaDialog.java | 375 ++++++++++++++++++ 5 files changed, 522 insertions(+), 162 deletions(-) create mode 100644 zkwebui/WEB-INF/src/org/adempiere/webui/editor/WBinaryEditor.java create mode 100644 zkwebui/WEB-INF/src/org/adempiere/webui/window/WMediaDialog.java diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WBinaryEditor.java b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WBinaryEditor.java new file mode 100644 index 0000000000..c5301dc820 --- /dev/null +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WBinaryEditor.java @@ -0,0 +1,126 @@ +/****************************************************************************** + * Copyright (C) 2008 Low Heng Sin 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.window.WMediaDialog; +import org.compiere.model.GridField; +import org.compiere.util.CLogger; +import org.zkoss.zk.ui.event.Event; +import org.zkoss.zk.ui.event.Events; + +/** + * @author Low Heng Sin + */ +public class WBinaryEditor extends WEditor +{ + private static final String[] LISTENER_EVENTS = {}; + + /** Logger */ + private static CLogger log = CLogger.getCLogger(WBinaryEditor.class); + + private boolean m_mandatory; + private Object m_data; + + public WBinaryEditor(GridField gridField) + { + super(new Button(), gridField); + init(); + } + + private void init() + { + label.setValue(" "); + getComponent().setLabel("-"); + getComponent().setTooltiptext(gridField.getDescription()); + getComponent().addEventListener(Events.ON_CLICK, this); + + } + + @Override + public String getDisplay() + { + return getComponent().getLabel(); + } + + @Override + public Object getValue() + { + return m_data; + } + + @Override + public boolean isMandatory() + { + return m_mandatory; + } + + + @Override + public void setMandatory(boolean mandatory) + { + m_mandatory = mandatory; + } + + @Override + public Button getComponent() { + return (Button) component; + } + + @Override + public void setValue(Object value) + { + log.config("=" + value); + m_data = value; + if (m_data == null) + getComponent().setLabel("-"); + else + { + String text = "?"; + if (m_data instanceof byte[]) + { + byte[] bb = (byte[])m_data; + text = "#" + bb.length; + } + else + { + text = m_data.getClass().getName(); + int index = text.lastIndexOf('.'); + if (index != -1) + text = text.substring(index+1); + } + getComponent().setLabel(text); + } + } + + @Override + public String[] getEvents() + { + return LISTENER_EVENTS; + } + + public void onEvent(Event event) throws Exception + { + if (Events.ON_CLICK.equals(event.getName())) + { + WMediaDialog dialog = new WMediaDialog(gridField.getHeader(), m_data); + if (!dialog.isCancel() && dialog.isChange()) + m_data = dialog.getData(); + } + } +} diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java index 997f283eb4..3692402334 100644 --- a/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java @@ -139,6 +139,10 @@ public class WebEditorFactory { editor = new WImageEditor(gridField); } + else if (displayType == DisplayType.Binary) + { + editor = new WBinaryEditor(gridField); + } else { editor = new WUnknownEditor(gridField); diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/panel/WAttachment.java b/zkwebui/WEB-INF/src/org/adempiere/webui/panel/WAttachment.java index 5df1000db7..ea1533951d 100644 --- a/zkwebui/WEB-INF/src/org/adempiere/webui/panel/WAttachment.java +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/panel/WAttachment.java @@ -1,6 +1,5 @@ /****************************************************************************** - * Product: Posterita Ajax UI * - * Copyright (C) 2007 Posterita Ltd. All Rights Reserved. * + * Copyright (C) 2008 Low Heng Sin 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 * @@ -18,7 +17,6 @@ package org.adempiere.webui.panel; import java.awt.Dimension; -import java.io.File; import java.util.logging.Level; import org.adempiere.webui.apps.AEnv; @@ -48,8 +46,12 @@ import org.zkoss.zul.Filedownload; import org.zkoss.zul.Fileupload; import org.zkoss.zul.Hbox; import org.zkoss.zul.Iframe; -import org.zkoss.zul.Image; +/** + * + * @author Low Heng Sin + * + */ public class WAttachment extends Window implements EventListener { private static final long serialVersionUID = 1L; @@ -72,14 +74,13 @@ public class WAttachment extends Window implements EventListener private Listbox cbContent = new Listbox(); private Button bDelete = new Button(); - private Button bOpen = new Button(); private Button bSave = new Button(); private Button bDeleteAll = new Button(); private Button bLoad = new Button(); private Button bCancel = new Button(); private Button bOk = new Button(); - private Panel graphPanel = new Panel(); + private Panel previewPanel = new Panel(); private Borderlayout mainPanel = new Borderlayout(); @@ -90,7 +91,6 @@ public class WAttachment extends Window implements EventListener /** * Constructor. * loads Attachment, if ID <> 0 - * @param frame frame * @param WindowNo window no * @param AD_Attachment_ID attachment * @param AD_Table_ID table @@ -103,8 +103,6 @@ public class WAttachment extends Window implements EventListener { super(); - // needs to be modal otherwise APanel does not recognize change. - log.config("ID=" + AD_Attachment_ID + ", Table=" + AD_Table_ID + ", Record=" + Record_ID); m_WindowNo = WindowNo; @@ -135,7 +133,6 @@ public class WAttachment extends Window implements EventListener { } - //text.requestFocus(); } // WAttachment /** @@ -145,9 +142,7 @@ public class WAttachment extends Window implements EventListener * - toolBar * - title * - centerPane [split] - * - graphPanel (left) - * - gifScroll - gifPanel - * - pdfViewer + * - previewPanel (left) * - text (right) * - confirmPanel * @@ -177,18 +172,11 @@ public class WAttachment extends Window implements EventListener toolBar.appendChild(bLoad); toolBar.appendChild(bDelete); toolBar.appendChild(bSave); - toolBar.appendChild(bOpen); toolBar.appendChild(cbContent); mainPanel.appendChild(northPanel); northPanel.appendChild(toolBar); - - bOpen.setEnabled(false); - bOpen.setSrc("/images/Editor24.gif"); - bOpen.setTooltiptext(Msg.getMsg(Env.getCtx(), "Open")); - bOpen.addEventListener(Events.ON_CLICK, this); - bSave.setEnabled(false); bSave.setSrc("/images/Export24.gif"); bSave.setTooltiptext(Msg.getMsg(Env.getCtx(), "AttachmentSave")); @@ -202,7 +190,7 @@ public class WAttachment extends Window implements EventListener bDelete.setTooltiptext(Msg.getMsg(Env.getCtx(), "Delete")); bDelete.addEventListener(Events.ON_CLICK, this); - graphPanel.appendChild(preview); + previewPanel.appendChild(preview); preview.setHeight("100%"); preview.setWidth("100%"); @@ -210,7 +198,7 @@ public class WAttachment extends Window implements EventListener centerPane.setAutoscroll(true); centerPane.setFlex(true); mainPanel.appendChild(centerPane); - centerPane.appendChild(graphPanel); + centerPane.appendChild(previewPanel); West westPane = new West(); westPane.setWidth("20%"); @@ -294,14 +282,12 @@ public class WAttachment extends Window implements EventListener preview.setVisible(false); bDelete.setEnabled(false); - bOpen.setEnabled(false); bSave.setEnabled(false); Dimension size = null; if (entry != null && entry.getData() != null) { - bOpen.setEnabled(true); bSave.setEnabled(true); bDelete.setEnabled(true); @@ -315,7 +301,7 @@ public class WAttachment extends Window implements EventListener } catch (Exception e) { - log.log(Level.SEVERE, "(pdf)", e); + log.log(Level.SEVERE, "attachment", e); } } } // displayData @@ -346,7 +332,6 @@ public class WAttachment extends Window implements EventListener public void onEvent(Event e) { - // log.config(e.getActionCommand()); // Save and Close if (e.getTarget() == bOk) @@ -413,13 +398,6 @@ public class WAttachment extends Window implements EventListener else if (e.getTarget() == bSave) saveAttachmentToFile(); - // Open Attachment - - else if (e.getTarget() == bOpen) - { - if (!openAttachment()) - saveAttachmentToFile(); - } } // onEvent /************************************************************************** @@ -446,8 +424,7 @@ public class WAttachment extends Window implements EventListener } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.log(Level.WARNING, e.getLocalizedMessage(), e); } String fileName = media.getName(); @@ -471,8 +448,6 @@ public class WAttachment extends Window implements EventListener if (m_attachment.addEntry(fileName, media.getByteData())) { - //MAttachmentEntry attachmentEntry = new MAttachmentEntry(media.getName(), media.getByteData()); - cbContent.appendItem(media.getName(), media.getName()); cbContent.setSelectedIndex(cbContent.getItemCount()-1); m_change = true; @@ -535,123 +510,9 @@ public class WAttachment extends Window implements EventListener } catch (Exception e) { - log.log(Level.SEVERE, "(pdf)", e); + log.log(Level.SEVERE, "attachment", e); } } } // saveAttachmentToFile - /** - * Open the temporary file with the application associated with the extension in the file name - * @return true if file was opened with third party application - */ - - private boolean openAttachment () - { - int index = cbContent.getSelectedIndex(); - byte[] data = m_attachment.getEntryData(index); - - if (data == null) - return false; - - try - { - String fileName = System.getProperty("java.io.tmpdir") + m_attachment.getEntryName(index); - File tempFile = new File(fileName); - m_attachment.getEntryFile(index, tempFile); - - if (Env.isWindows()) - { - // Runtime.getRuntime().exec ("rundll32 url.dll,FileProtocolHandler " + url); - Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + tempFile + "\""); - // p.waitFor(); - return true; - } - else if (Env.isMac()) - { - String [] cmdArray = new String [] {"open", tempFile.getAbsolutePath()}; - Process p = Runtime.getRuntime ().exec (cmdArray); - // p.waitFor(); - return true; - } - else // other OS - { - } - } - catch (Exception e) - { - log.log(Level.SEVERE, "", e); - } - return false; - } // openFile - - - /************************************************************************** - * Graphic Image Panel - */ - - class GImage extends Panel - { - private static final long serialVersionUID = 1L; - - /** The Image */ - private Image m_image = null; - - /** - * Graphic Image - */ - - public GImage() - { - super(); - } // GImage - - /** - * Set Image - * @param image image - */ - - public void setImage (Image image) - { - m_image = image; - - if (m_image == null) - return; - - //MediaTracker mt = new MediaTracker(this); - this.appendChild(m_image); - -/* try - { - mt.waitForID(0); - } - catch (Exception e) - {} - - Dimension dim = new Dimension(m_image.getWidth(this), m_image.getHeight(this)); - this.setPreferredSize(dim);*/ - } // setImage - - /** - * Paint - * @param g graphics - */ - -/* public void paint (Graphics g) - { - Insets in = getInsets(); - - if (m_image != null) - g.drawImage(m_image, in.left, in.top, this); - } // paint -*/ - /** - * Update - * @param g graphics - */ - -/* public void update (Graphics g) - { - paint(g); - } // update -*/ } // GImage } diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/window/WImageDialog.java b/zkwebui/WEB-INF/src/org/adempiere/webui/window/WImageDialog.java index 7ede84a005..b926389b3b 100644 --- a/zkwebui/WEB-INF/src/org/adempiere/webui/window/WImageDialog.java +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/window/WImageDialog.java @@ -52,7 +52,6 @@ public class WImageDialog extends Window implements EventListener { /** * Constructor - * @param owner * @param mImage */ public WImageDialog (MImage mImage) @@ -75,7 +74,7 @@ public class WImageDialog extends Window implements EventListener fileButton.setLabel(m_mImage.getName()); // imageLabel.setIcon(m_mImage.getIcon()); AEnv.showCenterScreen(this); - } // VImageDialog + } // WImageDialog /** Image Model */ private MImage m_mImage = null; @@ -131,7 +130,7 @@ public class WImageDialog extends Window implements EventListener // fileButton.addEventListener(Events.ON_CLICK, this); confirmPanel.addActionListener(Events.ON_CLICK, this); - } // jbInit + } // init public void onEvent(Event e) throws Exception { if (e.getTarget() == fileButton) @@ -163,12 +162,7 @@ public class WImageDialog extends Window implements EventListener { imageFile = Fileupload.get(); - if (imageFile != null) - { -// pdfViewer.setContent(media); - ; - } - else + if (imageFile == null) return; } catch (InterruptedException e) @@ -216,4 +210,4 @@ public class WImageDialog extends Window implements EventListener return m_mImage.getAD_Image_ID(); return 0; } // getAD_Image_ID -} // VImageDialog +} // WImageDialog diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/window/WMediaDialog.java b/zkwebui/WEB-INF/src/org/adempiere/webui/window/WMediaDialog.java new file mode 100644 index 0000000000..ac388b3ecd --- /dev/null +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/window/WMediaDialog.java @@ -0,0 +1,375 @@ +/****************************************************************************** + * Copyright (C) 2008 Low Heng Sin 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.window; + +import java.awt.Dimension; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.SQLException; +import java.util.logging.Level; + +import org.adempiere.webui.apps.AEnv; +import org.adempiere.webui.component.Button; +import org.adempiere.webui.component.Panel; +import org.adempiere.webui.component.Window; +import org.compiere.util.CLogger; +import org.compiere.util.Env; +import org.compiere.util.Msg; +import org.zkoss.util.media.AMedia; +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.Filedownload; +import org.zkoss.zul.Fileupload; +import org.zkoss.zul.Hbox; +import org.zkoss.zul.Iframe; + +/** + * + * @author Low Heng Sin + * + */ +public class WMediaDialog extends Window implements EventListener +{ + private static final long serialVersionUID = 1L; + + private static CLogger log = CLogger.getCLogger(WMediaDialog.class); + + /** data */ + private Object m_data; + + /** Change */ + private boolean m_change = false; + + private Iframe preview = new Iframe(); + + private Button bDelete = new Button(); + private Button bSave = new Button(); + private Button bLoad = new Button(); + private Button bCancel = new Button(); + private Button bOk = new Button(); + + private Panel previewPanel = new Panel(); + + private Borderlayout mainPanel = new Borderlayout(); + + private Hbox toolBar = new Hbox(); + + private Hbox confirmPanel = new Hbox(); + + private boolean m_cancel; + + /** + * Constructor. + * @param title + * @param data + */ + + public WMediaDialog(String title, Object data) + { + super(); + this.setTitle(title); + + try + { + staticInit(); + } + catch (Exception ex) + { + log.log(Level.SEVERE, "", ex); + } + + try + { + AEnv.showWindow(this); + } + catch (Exception e) + { + } + } // WAttachment + + /** + * Static setup. + *
+	 *  - northPanel
+	 *      - toolBar
+	 *      - title
+	 *  - centerPane [split]
+	 * 		- graphPanel (left)
+	 *		  	- gifScroll - gifPanel
+	 *			- pdfViewer
+	 *  	- text (right)
+	 *  - confirmPanel
+	 *  
+ * @throws Exception + */ + + void staticInit() throws Exception + { + this.setWidth("500px"); + this.setHeight("600px"); + this.setClosable(true); + this.setBorder("normal"); + this.appendChild(mainPanel); + mainPanel.setHeight("100%"); + mainPanel.setWidth("100%"); + + + North northPanel = new North(); + northPanel.setCollapsible(false); + northPanel.setSplittable(false); + + toolBar.appendChild(bLoad); + toolBar.appendChild(bDelete); + toolBar.appendChild(bSave); + + mainPanel.appendChild(northPanel); + northPanel.appendChild(toolBar); + + + bSave.setEnabled(false); + bSave.setSrc("/images/Export24.gif"); + bSave.setTooltiptext(Msg.getMsg(Env.getCtx(), "AttachmentSave")); + bSave.addEventListener(Events.ON_CLICK, this); + + bLoad.setSrc("/images/Import24.gif"); + bLoad.setTooltiptext(Msg.getMsg(Env.getCtx(), "Load")); + bLoad.addEventListener(Events.ON_CLICK, this); + + bDelete.setSrc("/images/Delete24.gif"); + bDelete.setTooltiptext(Msg.getMsg(Env.getCtx(), "Delete")); + bDelete.addEventListener(Events.ON_CLICK, this); + + previewPanel.appendChild(preview); + preview.setHeight("100%"); + preview.setWidth("100%"); + + Center centerPane = new Center(); + centerPane.setAutoscroll(true); + centerPane.setFlex(true); + mainPanel.appendChild(centerPane); + centerPane.appendChild(previewPanel); + + South southPane = new South(); + mainPanel.appendChild(southPane); + southPane.appendChild(confirmPanel); + southPane.setHeight("30px"); + + bCancel.setImage("/images/Cancel24.gif"); + bCancel.addEventListener(Events.ON_CLICK, this); + + bOk.setImage("/images/Ok24.gif"); + bOk.addEventListener(Events.ON_CLICK, this); + + confirmPanel.appendChild(bCancel); + confirmPanel.appendChild(bOk); + } + + /** + * Dispose + */ + + public void dispose () + { + preview = null; + this.detach(); + } // dispose + + /** + * Display gif or jpg in gifPanel + * @param index index + */ + + private void displayData () + { + // Reset UI + preview.setVisible(false); + + bDelete.setEnabled(false); + bSave.setEnabled(false); + + Dimension size = null; + + if (m_data != null) + { + bSave.setEnabled(true); + bDelete.setEnabled(true); + + try + { + AMedia media = createMedia(); + + preview.setContent(media); + preview.setVisible(true); + } + catch (Exception e) + { + log.log(Level.SEVERE, "Failed to preview content", e); + } + } + } // displayData + + private AMedia createMedia() throws SQLException { + AMedia media; + String contentType = null; + if (m_data instanceof byte[]) + { + contentType = "application/octet-stream"; + media = new AMedia(this.getTitle(), null, contentType, (byte[])m_data); + } + else if (m_data instanceof Blob) + { + contentType = "application/octet-stream"; + media = new AMedia(this.getTitle(), null, contentType, ((Blob)m_data).getBinaryStream()); + } + else if (m_data instanceof Clob) + { + Clob clob = (Clob)m_data; + long length = clob.length() > 100 ? 100 : clob.length(); + String data = ((Clob)m_data).getSubString(1, new Long(length).intValue()); + if (data.toUpperCase().indexOf("") >= 0) + { + contentType = "text/html"; + } + else + { + contentType = "text/plain"; + } + media = new AMedia(this.getTitle(), null, contentType, ((Clob)m_data).getCharacterStream()); + } + else + { + contentType = "text/plain"; + media = new AMedia(this.getTitle(), null, contentType, m_data.toString()); + } + return media; + } + + /** + * Action Listener + * @param e event + */ + + public void onEvent(Event e) + { + // log.config(e.getActionCommand()); + // Save and Close + + if (e.getTarget() == bOk) + { + dispose(); + } + + // Cancel + + else if (e.getTarget() == bCancel) + { + m_cancel = true; + dispose(); + } + + // clear data + + else if (e.getTarget() == bDelete) + { + m_data = null; + m_change = true; + displayData(); + } + + // Load Attachment + + else if (e.getTarget() == bLoad) + loadFile(); + + // Open Attachment + + else if (e.getTarget() == bSave) + { + save(); + } + } // onEvent + + /************************************************************************** + * Load file + */ + + private void loadFile() + { + log.info(""); + + Media media = null; + + try + { + media = Fileupload.get(); + + if (media == null) + return; + } + catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + String fileName = media.getName(); + log.config(fileName); + //update + m_change = true; + m_data = media.getByteData(); + + } // getFileName + + /** + * download + */ + + private void save() + { + if (m_data == null) + return; + + try + { + AMedia media = createMedia(); + Filedownload.save(media); + } + catch (Exception e) + { + log.log(Level.SEVERE, "Failed to export content.", e); + } + } // saveAttachmentToFile + + public boolean isCancel() { + return m_cancel; + } + + public boolean isChange() { + return m_change; + } + + public Object getData() { + return m_data; + } + +}