/******************************************************************************
* Copyright (C) 2008 Low Heng Sin *
* Copyright (C) 2008 Idalica Corporation *
* 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.webui.desktop;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.adempiere.util.Callback;
import org.adempiere.webui.AdempiereWebUI;
import org.adempiere.webui.ClientInfo;
import org.adempiere.webui.LayoutUtils;
import org.adempiere.webui.adwindow.ADTabpanel;
import org.adempiere.webui.adwindow.ADWindow;
import org.adempiere.webui.component.Window;
import org.adempiere.webui.event.DialogEvents;
import org.adempiere.webui.exception.ApplicationException;
import org.adempiere.webui.part.AbstractUIPart;
import org.adempiere.webui.session.SessionManager;
import org.compiere.model.MMenu;
import org.compiere.model.MPreference;
import org.compiere.model.MQuery;
import org.compiere.model.Query;
import org.compiere.model.SystemIDs;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Window.Mode;
/**
* Abstract base class for {@link IDesktop} implementation
* @author hengsin
*/
public abstract class AbstractDesktop extends AbstractUIPart implements IDesktop {
private transient ClientInfo clientInfo;
private String predefinedContextVariables;
private boolean menuIsSOTrx;
private boolean isCloseTabWithShortcut = true;
@SuppressWarnings("unused")
private static final CLogger logger = CLogger.getCLogger(AbstractDesktop.class);
/**
* Default constructor
*/
public AbstractDesktop() {
}
/**
* Event listener for menu item selection.
* Identifies the action associated with the selected menu item and acts accordingly.
* Event from favourite panel, global search and application menu tree will be routed here.
*
* @param menuId Identifier for the selected menu item
*
* @throws ApplicationException If the selected menu action has yet
* to be implemented
*/
@Override
public void onMenuSelected(int menuId)
{
MMenu menu = MMenu.get(menuId);
try
{
setPredefinedContextVariables(menu.getPredefinedContextVariables());
setMenuIsSOTrx(menu.isSOTrx());
if(menu.getAction().equals(MMenu.ACTION_Window))
{
openWindow(menu.getAD_Window_ID(), null);
}
else if(menu.getAction().equals(MMenu.ACTION_Process) ||
menu.getAction().equals(MMenu.ACTION_Report))
{
openProcessDialog(menu.getAD_Process_ID(), menu.isSOTrx());
}
else if(menu.getAction().equals(MMenu.ACTION_Form))
{
openForm(menu.getAD_Form_ID());
}
else if(menu.getAction().equals(MMenu.ACTION_Info))
{
openInfo(menu.getAD_InfoWindow_ID());
}
else if(menu.getAction().equals(MMenu.ACTION_WorkFlow))
{
openWorkflow(menu.getAD_Workflow_ID());
}
else if(menu.getAction().equals(MMenu.ACTION_Task))
{
openTask(menu.getAD_Task_ID());
}
else
{
throw new ApplicationException("Menu Action not yet implemented: " + menu.getAction());
}
}
finally
{
setPredefinedContextVariables(null);
}
updateRecentMenuItem(menuId);
}
/**
* Open AD window in new record mode.
* Call by global search, application menu tree and favourite panel.
* @param menuId
*/
@Override
public void onNewRecord(int menuId) {
MMenu menu = new MMenu(Env.getCtx(), menuId, null);
setPredefinedContextVariables(menu.getPredefinedContextVariables());
MQuery query = new MQuery("");
query.addRestriction("1=2");
query.setRecordCount(0);
SessionManager.getAppDesktop().openWindow(menu.getAD_Window_ID(), query, new Callback() {
@Override
public void onCallback(ADWindow result) {
if(result == null)
return;
result.getADWindowContent().onNew();
ADTabpanel adtabpanel = (ADTabpanel) result.getADWindowContent().getADTab().getSelectedTabpanel();
adtabpanel.focusToFirstEditor(false);
}
});
updateRecentMenuItem(menuId);
}
/**
* Perform asynchronous update of recent menu items preference for user
* @param menuId
*/
protected void updateRecentMenuItem(int menuId) {
Runnable runnable = () -> {
int AD_User_ID = Env.getAD_User_ID(Env.getCtx());
int AD_Role_ID = Env.getAD_Role_ID(Env.getCtx());
int AD_Org_ID = 0;
String attribute = AD_Role_ID+"|RecentMenuItems";
Query query = new Query(Env.getCtx(), MPreference.Table_Name, "PreferenceFor=? AND Attribute=? AND AD_Org_ID=? AND AD_User_ID=? AND AD_Window_ID=?", null);
MPreference preference = query.setClient_ID().setParameters("W", attribute, AD_Org_ID, AD_User_ID, SystemIDs.WINDOW_MENU).first();
if (preference == null) {
preference = new MPreference(Env.getCtx(), 0, null);
preference.setAD_Org_ID(AD_Org_ID);
preference.setPreferenceFor("W");
preference.setAttribute(attribute);
preference.setAD_User_ID(AD_User_ID);
preference.setValue(Integer.toString(menuId));
preference.setAD_Window_ID(SystemIDs.WINDOW_MENU);
preference.saveEx();
} else {
String recentItemValue = preference.getValue();
List itemList = new ArrayList();
String[] recentItemValues = recentItemValue.split("[,]");
String menuIdValue = Integer.toString(menuId);
itemList.add(menuIdValue);
for (int i = 0; itemList.size() < 7 && i < recentItemValues.length; i++) {
if (!recentItemValues[i].equals(menuIdValue))
itemList.add(recentItemValues[i]);
}
recentItemValue = itemList.stream().collect(Collectors.joining(","));
preference.setValue(recentItemValue);
preference.saveEx();
}
};
Executions.schedule(getComponent().getDesktop(), e -> {
runnable.run();
}, new Event("onUpdateRecentMenuItem"));
}
/**
* @return {@link ClientInfo}
*/
public ClientInfo getClientInfo() {
return clientInfo;
}
/**
* @param clientInfo
*/
public void setClientInfo(ClientInfo clientInfo) {
this.clientInfo = clientInfo;
}
/**
* @param win
*/
public int registerWindow(Object win) {
List