From 9dc0dc5b94a4064d9b2d27ea7a50925601057769 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Thu, 8 Nov 2012 18:09:37 +0800 Subject: [PATCH] IDEMPIERE-234 Configure Toolbar by User, by Window, by Tab and configure new buttons and new toolbars. Fixed example action for custom window toolbar button is not thread safe. --- .../ui/zk/example/action/ExportAction.java | 183 +---------------- .../ui/zk/example/action/ExportWindow.java | 188 ++++++++++++++++++ .../org/adempiere/webui/action/IAction.java | 5 + 3 files changed, 204 insertions(+), 172 deletions(-) create mode 100644 org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportWindow.java diff --git a/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportAction.java b/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportAction.java index 910be69dc8..b9c2df7fe2 100644 --- a/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportAction.java +++ b/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportAction.java @@ -13,59 +13,21 @@ *****************************************************************************/ package org.adempiere.ui.zk.example.action; -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.adempiere.base.IGridTabExporter; -import org.adempiere.base.Service; -import org.adempiere.exceptions.AdempiereException; import org.adempiere.webui.action.IAction; import org.adempiere.webui.adwindow.ADWindow; -import org.adempiere.webui.adwindow.AbstractADWindowContent; -import org.adempiere.webui.adwindow.IADTabbox; -import org.adempiere.webui.adwindow.IADTabpanel; +import org.adempiere.webui.adwindow.ADWindowContent; import org.adempiere.webui.apps.AEnv; -import org.adempiere.webui.component.Checkbox; -import org.adempiere.webui.component.ConfirmPanel; -import org.adempiere.webui.component.Label; -import org.adempiere.webui.component.ListItem; -import org.adempiere.webui.component.Listbox; -import org.adempiere.webui.component.Window; import org.adempiere.webui.window.FDialog; -import org.compiere.model.GridTab; import org.compiere.model.MRole; -import org.compiere.util.Env; -import org.compiere.util.Msg; -import org.zkoss.util.media.AMedia; -import org.zkoss.zk.ui.event.Event; -import org.zkoss.zk.ui.event.EventListener; -import org.zkoss.zul.Div; -import org.zkoss.zul.Filedownload; -import org.zkoss.zul.Hbox; -import org.zkoss.zul.Vbox; /** * @author hengsin * */ -public class ExportAction implements IAction, EventListener { +public class ExportAction implements IAction { - private AbstractADWindowContent panel; - - private Map exporterMap = null; - private Map extensionMap = null; - - private Window winExportFile = null; - private ConfirmPanel confirmPanel = new ConfirmPanel(true); - private Listbox cboType = new Listbox(); - private Checkbox chkCurrentRow = new Checkbox(); /** - * + * default constructor */ public ExportAction() { } @@ -76,143 +38,20 @@ public class ExportAction implements IAction, EventListener { @Override public void execute(Object target) { ADWindow adwindow = (ADWindow) target; - panel = adwindow.getADWindowContent(); + ADWindowContent panel = adwindow.getADWindowContent(); if (!MRole.getDefault().isCanExport()) { FDialog.error(panel.getWindowNo(), "AccessTableNoView"); return; } - doExport(); + doExport(panel); } - private void doExport() { - exporterMap = new HashMap(); - extensionMap = new HashMap(); - List exporterList = Service.locator().list(IGridTabExporter.class).getServices(); - for(IGridTabExporter exporter : exporterList) - { - String extension = exporter.getFileExtension(); - if (!extensionMap.containsKey(extension)) - { - extensionMap.put(extension, exporter.getFileExtensionLabel()); - exporterMap.put(extension, exporter); - } - } - - if(winExportFile == null) - { - winExportFile = new Window(); - winExportFile.setTitle(Msg.getMsg(Env.getCtx(), "Export") + ": " + panel.getActiveGridTab().getName()); - winExportFile.setWidth("450px"); - winExportFile.setClosable(true); - winExportFile.setBorder("normal"); - winExportFile.setStyle("position:absolute"); - - cboType.setMold("select"); - - cboType.getItems().clear(); - for(Map.Entry entry : extensionMap.entrySet()) - { - cboType.appendItem(entry.getKey() + " - " + entry.getValue(), entry.getKey()); - } - - cboType.setSelectedIndex(0); - - Vbox vb = new Vbox(); - vb.setWidth("100%"); - winExportFile.appendChild(vb); - - Hbox hb = new Hbox(); - Div div = new Div(); - div.setAlign("right"); - div.appendChild(new Label("Files of Type: ")); - hb.appendChild(div); - hb.appendChild(cboType); - cboType.setWidth("100%"); - vb.appendChild(hb); - - hb = new Hbox(); - chkCurrentRow.setLabel(Msg.getMsg(Env.getCtx(), "ExportCurrentRowOnly")); - chkCurrentRow.setSelected(true); - hb.appendChild(chkCurrentRow); - vb.appendChild(hb); - - vb.appendChild(confirmPanel); - confirmPanel.addActionListener(this); - } - - winExportFile.setAttribute(Window.MODE_KEY, Window.MODE_HIGHLIGHTED); - AEnv.showWindow(winExportFile); - } - - @Override - public void onEvent(Event event) throws Exception { - if(event.getTarget().getId().equals(ConfirmPanel.A_CANCEL)) - winExportFile.onClose(); - else if(event.getTarget().getId().equals(ConfirmPanel.A_OK)) - exportFile(); - } - - private void exportFile() { - try { - ListItem li = cboType.getSelectedItem(); - if(li == null || li.getValue() == null) - { - FDialog.error(0, winExportFile, "FileInvalidExtension"); - return; - } - - String ext = li.getValue().toString(); - IGridTabExporter exporter = exporterMap.get(ext); - if (exporter == null) - { - FDialog.error(0, winExportFile, "FileInvalidExtension"); - return; - } - - boolean currentRowOnly = chkCurrentRow.isSelected(); - File file = File.createTempFile("Export", "."+ext); - IADTabbox adTab = panel.getADTab(); - int selected = adTab.getSelectedIndex(); - int tabLevel = panel.getActiveGridTab().getTabLevel(); - Set tables = new HashSet(); - List childs = new ArrayList(); - List includedList = panel.getActiveGridTab().getIncludedTabs(); - for(GridTab included : includedList) - { - String tableName = included.getTableName(); - if (tables.contains(tableName)) - continue; - tables.add(tableName); - childs.add(included); - } - for(int i = selected+1; i < adTab.getTabCount(); i++) - { - IADTabpanel adTabPanel = adTab.getADTabpanel(i); - if (adTabPanel.getGridTab().isSortTab()) - continue; - if (adTabPanel.getGridTab().getTabLevel() <= tabLevel) - break; - String tableName = adTabPanel.getGridTab().getTableName(); - if (tables.contains(tableName)) - continue; - tables.add(tableName); - childs.add(adTabPanel.getGridTab()); - } - - exporter.export(panel.getActiveGridTab(), childs, currentRowOnly, file); - - winExportFile.onClose(); - winExportFile = null; - AMedia media = null; - media = new AMedia(panel.getActiveGridTab().getName() + "." + ext, null, exporter.getContentType(), file, true); - Filedownload.save(media, panel.getActiveGridTab().getName() + "." + ext); - } catch (Exception e) { - throw new AdempiereException(e); - } finally { - if (winExportFile != null) - winExportFile.onClose(); - } - } + private void doExport(ADWindowContent panel) { + ExportWindow window = new ExportWindow(); + window.init(panel); + + AEnv.showWindow(window); + } } diff --git a/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportWindow.java b/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportWindow.java new file mode 100644 index 0000000000..9e67901d30 --- /dev/null +++ b/org.adempiere.ui.zk.example/src/org/adempiere/ui/zk/example/action/ExportWindow.java @@ -0,0 +1,188 @@ +/** + * + */ +package org.adempiere.ui.zk.example.action; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.adempiere.base.IGridTabExporter; +import org.adempiere.base.Service; +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.webui.adwindow.ADWindowContent; +import org.adempiere.webui.adwindow.IADTabbox; +import org.adempiere.webui.adwindow.IADTabpanel; +import org.adempiere.webui.component.Checkbox; +import org.adempiere.webui.component.ConfirmPanel; +import org.adempiere.webui.component.Label; +import org.adempiere.webui.component.ListItem; +import org.adempiere.webui.component.Listbox; +import org.adempiere.webui.component.Window; +import org.adempiere.webui.window.FDialog; +import org.compiere.model.GridTab; +import org.compiere.util.Env; +import org.compiere.util.Msg; +import org.zkoss.util.media.AMedia; +import org.zkoss.zk.ui.event.Event; +import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zul.Div; +import org.zkoss.zul.Filedownload; +import org.zkoss.zul.Hbox; +import org.zkoss.zul.Vbox; + +/** + * @author hengsin + * + */ +public class ExportWindow extends Window implements EventListener { + + /** + * generated serial id + */ + private static final long serialVersionUID = -4465587396361021680L; + + private Map exporterMap = null; + private Map extensionMap = null; + + private ConfirmPanel confirmPanel = new ConfirmPanel(true); + private Listbox cboType = new Listbox(); + private Checkbox chkCurrentRow = new Checkbox(); + private ADWindowContent panel; + + /** + * + */ + public ExportWindow() { + } + + public void init(ADWindowContent panel) { + this.panel = panel; + + exporterMap = new HashMap(); + extensionMap = new HashMap(); + List exporterList = Service.locator().list(IGridTabExporter.class).getServices(); + for(IGridTabExporter exporter : exporterList) + { + String extension = exporter.getFileExtension(); + if (!extensionMap.containsKey(extension)) + { + extensionMap.put(extension, exporter.getFileExtensionLabel()); + exporterMap.put(extension, exporter); + } + } + + setTitle(Msg.getMsg(Env.getCtx(), "Export") + ": " + panel.getActiveGridTab().getName()); + setWidth("450px"); + setClosable(true); + setBorder("normal"); + setStyle("position:absolute"); + + cboType.setMold("select"); + + cboType.getItems().clear(); + for(Map.Entry entry : extensionMap.entrySet()) + { + cboType.appendItem(entry.getKey() + " - " + entry.getValue(), entry.getKey()); + } + + cboType.setSelectedIndex(0); + + Vbox vb = new Vbox(); + vb.setWidth("100%"); + appendChild(vb); + + Hbox hb = new Hbox(); + Div div = new Div(); + div.setStyle("float: right"); + div.appendChild(new Label("Files of Type: ")); + hb.appendChild(div); + hb.appendChild(cboType); + cboType.setWidth("100%"); + vb.appendChild(hb); + + hb = new Hbox(); + chkCurrentRow.setLabel(Msg.getMsg(Env.getCtx(), "ExportCurrentRowOnly")); + chkCurrentRow.setSelected(true); + hb.appendChild(chkCurrentRow); + vb.appendChild(hb); + + vb.appendChild(confirmPanel); + confirmPanel.addActionListener(this); + + setAttribute(Window.MODE_KEY, Window.MODE_HIGHLIGHTED); + } + + @Override + public void onEvent(Event event) throws Exception { + if(event.getTarget().getId().equals(ConfirmPanel.A_CANCEL)) + onClose(); + else if(event.getTarget().getId().equals(ConfirmPanel.A_OK)) + exportFile(); + + } + + private void exportFile() { + try { + ListItem li = cboType.getSelectedItem(); + if(li == null || li.getValue() == null) + { + FDialog.error(0, this, "FileInvalidExtension"); + return; + } + + String ext = li.getValue().toString(); + IGridTabExporter exporter = exporterMap.get(ext); + if (exporter == null) + { + FDialog.error(0, this, "FileInvalidExtension"); + return; + } + + boolean currentRowOnly = chkCurrentRow.isSelected(); + File file = File.createTempFile("Export", "."+ext); + IADTabbox adTab = panel.getADTab(); + int selected = adTab.getSelectedIndex(); + int tabLevel = panel.getActiveGridTab().getTabLevel(); + Set tables = new HashSet(); + List childs = new ArrayList(); + List includedList = panel.getActiveGridTab().getIncludedTabs(); + for(GridTab included : includedList) + { + String tableName = included.getTableName(); + if (tables.contains(tableName)) + continue; + tables.add(tableName); + childs.add(included); + } + for(int i = selected+1; i < adTab.getTabCount(); i++) + { + IADTabpanel adTabPanel = adTab.getADTabpanel(i); + if (adTabPanel.getGridTab().isSortTab()) + continue; + if (adTabPanel.getGridTab().getTabLevel() <= tabLevel) + break; + String tableName = adTabPanel.getGridTab().getTableName(); + if (tables.contains(tableName)) + continue; + tables.add(tableName); + childs.add(adTabPanel.getGridTab()); + } + + exporter.export(panel.getActiveGridTab(), childs, currentRowOnly, file); + + onClose(); + AMedia media = null; + media = new AMedia(panel.getActiveGridTab().getName() + "." + ext, null, exporter.getContentType(), file, true); + Filedownload.save(media, panel.getActiveGridTab().getName() + "." + ext); + } catch (Exception e) { + throw new AdempiereException(e); + } finally { + onClose(); + } + } +} diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/action/IAction.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/action/IAction.java index 331ca49e9d..6389551c55 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/action/IAction.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/action/IAction.java @@ -14,9 +14,14 @@ package org.adempiere.webui.action; /** + * Custom UI action provided through OSGi service. Implementation must be thread safe. * @author hengsin * */ public interface IAction { + /** + * + * @param target + */ public void execute(Object target); }