From a314fcc615b774d0ddc75f4ae7327293a72a4e0b Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Wed, 4 Feb 2009 20:57:51 +0000 Subject: [PATCH] FR [ 1943731 ] Window data export functionality https://sourceforge.net/tracker/?func=detail&atid=879335&aid=1943731&group_id=176962 --- .../impexp/GridTabExcelExporter.java | 146 ++++++++++++++++++ client/src/org/compiere/apps/AExport.java | 106 +++++++++++++ client/src/org/compiere/apps/APanel.java | 14 +- 3 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 base/src/org/adempiere/impexp/GridTabExcelExporter.java create mode 100644 client/src/org/compiere/apps/AExport.java diff --git a/base/src/org/adempiere/impexp/GridTabExcelExporter.java b/base/src/org/adempiere/impexp/GridTabExcelExporter.java new file mode 100644 index 0000000000..c2eb2a6887 --- /dev/null +++ b/base/src/org/adempiere/impexp/GridTabExcelExporter.java @@ -0,0 +1,146 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2008 SC ARHIPAC SERVICE SRL. 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. * + *****************************************************************************/ +package org.adempiere.impexp; + +import java.util.HashMap; +import java.util.Properties; + +import org.compiere.model.GridField; +import org.compiere.model.GridTab; +import org.compiere.model.Lookup; +import org.compiere.model.MLookup; +import org.compiere.model.MLookupFactory; +import org.compiere.util.DisplayType; +import org.compiere.util.Env; + +/** + * Excel Exporter Adapter for GridTab + * @author Teo Sarca, www.arhipac.ro + *
  • FR [ 1943731 ] Window data export functionality + */ +public class GridTabExcelExporter extends AbstractExcelExporter +{ + private GridTab m_tab = null; + + public GridTabExcelExporter(Properties ctx, GridTab tab) + { + m_tab = tab; + setFreezePane(0, 1); + } + + @Override + protected int getColumnCount() + { + return m_tab.getFieldCount(); + } + + @Override + protected int getDisplayType(int row, int col) + { + return m_tab.getField(col).getDisplayType(); + } + + @Override + protected String getHeaderName(int col) + { + return m_tab.getField(col).getHeader(); + } + + @Override + protected int getRowCount() + { + return m_tab.getRowCount(); + } + + @Override + protected Object getValueAt(int row, int col) + { + GridField f = m_tab.getField(col); + Object key = m_tab.getValue(row, f.getColumnName()); + Object value = key; + Lookup lookup = f.getLookup(); + if (lookup != null) + { + ; + } + else if (f.getDisplayType() == DisplayType.Button) + { + lookup = getButtonLookup(f); + } + // + if (lookup != null) + { + value = lookup.getDisplay(key); + } + return value; + } + + @Override + protected boolean isColumnPrinted(int col) + { + GridField f = m_tab.getField(col); + // Hide not displayed fields + if (!f.isDisplayed()) + return false; + // Hide encrypted fields + if (f.isEncrypted()) + return false; + // Hide simple button fields without a value + if (f.getDisplayType() == DisplayType.Button && f.getAD_Reference_Value_ID() == 0) + return false; + return true; + } + + @Override + protected boolean isFunctionRow() + { + return false; + } + + @Override + protected boolean isPageBreak(int row, int col) + { + return false; + } + + @Override + protected void setCurrentRow(int row) + { + ; // nothing + } + + private HashMap m_buttonLookups = new HashMap(); + + private MLookup getButtonLookup(GridField mField) + { + MLookup lookup = m_buttonLookups.get(mField.getColumnName()); + if (lookup != null) + return lookup; + // TODO: refactor with org.compiere.grid.ed.VButton.setField(GridField) + if (mField.getColumnName().endsWith("_ID") && !mField.getColumnName().equals("Record_ID")) + { + lookup = MLookupFactory.get(Env.getCtx(), mField.getWindowNo(), 0, + mField.getAD_Column_ID(), DisplayType.Search); + } + else if (mField.getAD_Reference_Value_ID() != 0) + { + // Assuming List + lookup = MLookupFactory.get(Env.getCtx(), mField.getWindowNo(), 0, + mField.getAD_Column_ID(), DisplayType.List); + } + // + m_buttonLookups.put(mField.getColumnName(), lookup); + return lookup; + } +} diff --git a/client/src/org/compiere/apps/AExport.java b/client/src/org/compiere/apps/AExport.java new file mode 100644 index 0000000000..12cabe545e --- /dev/null +++ b/client/src/org/compiere/apps/AExport.java @@ -0,0 +1,106 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2008 SC ARHIPAC SERVICE SRL. 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. * + *****************************************************************************/ +package org.compiere.apps; + +import java.awt.Cursor; +import java.io.File; +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Level; + +import javax.swing.JFileChooser; + +import org.adempiere.impexp.GridTabExcelExporter; +import org.compiere.model.GridTab; +import org.compiere.util.CLogMgt; +import org.compiere.util.CLogger; +import org.compiere.util.Env; +import org.compiere.util.ExtensionFileFilter; +import org.compiere.util.Msg; + +/** + * Export button consequences + * @author Teo Sarca, www.arhipac.ro + *
  • FR [ 1943731 ] Window data export functionality + */ +public class AExport +{ + private CLogger log = CLogger.getCLogger(getClass()); + private Properties m_ctx = null; + private int m_WindowNo = 0; + + public AExport(APanel parent) + { + m_ctx = Env.getCtx(); + m_WindowNo = parent.getWindowNo(); + // + JFileChooser chooser = new JFileChooser(); + chooser.addChoosableFileFilter(new ExtensionFileFilter("xls", Msg.getMsg(m_ctx, "FileXLS"))); + if (chooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION) + return; + + // Create File + File outFile = ExtensionFileFilter.getFile(chooser.getSelectedFile(), chooser.getFileFilter()); + try + { + outFile.createNewFile(); + } + catch (IOException e) + { + log.log(Level.SEVERE, "", e); + ADialog.error(m_WindowNo, parent, "FileCannotCreate", e.getLocalizedMessage()); + return; + } + + String ext = outFile.getPath(); + // no extension + if (ext.lastIndexOf('.') == -1) + { + ADialog.error(m_WindowNo, parent, "FileInvalidExtension"); + return; + } + ext = ext.substring(ext.lastIndexOf('.')+1).toLowerCase(); + log.config( "File=" + outFile.getPath() + "; Type=" + ext); + + parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + try + { + if (ext.equals("xls")) + { + createXLS(outFile, parent.getCurrentTab()); + } + else + { + ADialog.error(m_WindowNo, parent, "FileInvalidExtension"); + } + } + catch (Exception e) + { + ADialog.error(m_WindowNo, parent, "Error", e.getLocalizedMessage()); + if (CLogMgt.isLevelFinest()) + e.printStackTrace(); + } + finally + { + parent.setCursor(Cursor.getDefaultCursor()); + } + } + + private void createXLS(File outFile, GridTab tab) + throws Exception + { + GridTabExcelExporter exporter = new GridTabExcelExporter(m_ctx, tab); + exporter.export(outFile, null); + } +} diff --git a/client/src/org/compiere/apps/APanel.java b/client/src/org/compiere/apps/APanel.java index 3096095c91..94490aae5a 100644 --- a/client/src/org/compiere/apps/APanel.java +++ b/client/src/org/compiere/apps/APanel.java @@ -117,6 +117,7 @@ import org.compiere.util.Util; * @author Teo Sarca, SC ARHIPAC SERVICE SRL *
  • BF [ 1824621 ] History button can't be canceled *
  • BF [ 1941271 ] VTreePanel is modifying even if is save wasn't successfull + *
  • FR [ 1943731 ] Window data export functionality *
  • BF [ 1996056 ] Report error message is not displayed *
  • BF [ 1998575 ] Document Print is discarding any error * @author victor.perez@e-evolution.com @@ -279,6 +280,7 @@ public final class APanel extends CPanel private AppsAction aPrevious, aNext, aParent, aDetail, aFirst, aLast, aNew, aCopy, aDelete, aPrint, aPrintPreview, + aExport, aRefresh, aHistory, aAttachment, aChat, aMulti, aFind, aWorkflow, aZoomAcross, aRequest, aWinSize, aArchive; /** Ignore Button */ @@ -314,6 +316,10 @@ public final class APanel extends CPanel aReport = addAction("Report", mFile, KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0), false); aPrint = addAction("Print", mFile, KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0), false); aPrintPreview = addAction("PrintPreview", mFile, KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.SHIFT_MASK+Event.ALT_MASK), false); + if (MRole.getDefault().isCanExport()) + { + aExport = addAction("Export", mFile, null, false); + } mFile.addSeparator(); aEnd = addAction("End", mFile, KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.ALT_MASK), false); aLogout = addAction("Logout", mFile, KeyStroke.getKeyStroke(KeyEvent.VK_L, Event.SHIFT_MASK+Event.ALT_MASK), false); @@ -1492,6 +1498,8 @@ public final class APanel extends CPanel cmd_print(); else if (cmd.equals(aPrintPreview.getName())) cmd_print(true); + else if (cmd.equals(aExport.getName())) + cmd_export(); else if (cmd.equals(aEnd.getName())) cmd_end(false); else if (cmd.equals(aExit.getName())) @@ -2190,7 +2198,11 @@ public final class APanel extends CPanel win.setWindowSize(size); win.save(); } // cmdWinSize - + + private void cmd_export() + { + new AExport(this); + } /**************************************************************************