//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// This file is a part of the 'esoco-gwt' project.
// Copyright 2018 Elmar Sonnenschein, esoco GmbH, Flensburg, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package de.esoco.gwt.client.app;

import de.esoco.data.element.DataElementList;
import de.esoco.data.process.ProcessState;

import de.esoco.ewt.build.ContainerBuilder;
import de.esoco.ewt.component.Container;
import de.esoco.ewt.dialog.MessageBox;
import de.esoco.ewt.style.AlignedPosition;
import de.esoco.ewt.style.StyleData;

import de.esoco.gwt.client.res.EsocoGwtResources;
import de.esoco.gwt.client.ui.PanelManager;


/********************************************************************
 * A standard root panel manager for applications with a main process.
 *
 * @author eso
 */
public class GwtProcessAppRootPanel<P extends GwtApplicationPanelManager<?, ?>>
	extends GwtApplicationPanelManager<Container, P>
{
	//~ Instance fields --------------------------------------------------------

	private DataElementList     rUserData;
	private ProcessPanelManager aProcessPanel;

	//~ Constructors -----------------------------------------------------------

	/***************************************
	 * Creates a new instance.
	 */
	public GwtProcessAppRootPanel()
	{
		super(null, EsocoGwtResources.INSTANCE.css().gaRootPanel());

		// only used for re-authentication, initial login is process-based
		setLoginMode(LoginMode.DIALOG);
	}

	//~ Methods ----------------------------------------------------------------

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	public void displayMessage(String sMessage, int nDisplayTime)
	{
		MessageBox.showNotification(getContainer().getView(),
									"$tiErrorMessage",
									sMessage,
									MessageBox.ICON_ERROR);
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	public void dispose()
	{
		rUserData = null;

		removeApplicationPanel();

		super.dispose();
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	public void updateUI()
	{
		if (aProcessPanel != null)
		{
			aProcessPanel.updateUI();
		}
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected void addComponents()
	{
		login(false);
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("unchecked")
	protected ContainerBuilder<Container> createContainer(
		ContainerBuilder<?> rBuilder,
		StyleData			rStyleData)
	{
		// as the root panel only displays a process and therefore has no
		// own UI just return parent builder to inline the process panel in
		// the main application view
		return (ContainerBuilder<Container>) rBuilder;
	}

	/***************************************
	 * Creates the process panel that will be used to render the application
	 * process. Can be overridden to return a different instance than the
	 * default implementation {@link ProcessPanelManager}.
	 *
	 * @param  rProcessState The current process state
	 *
	 * @return A new process panel manager instance
	 */
	protected ProcessPanelManager createProcessPanel(ProcessState rProcessState)
	{
		return new ProcessPanelManager(this,
									   rProcessState.getName(),
									   false,
									   false);
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected void displayProcess(ProcessState rProcessState)
	{
		if (rProcessState.isFinished())
		{
			processFinished(null, rProcessState);
		}
		else
		{
			aProcessPanel = createProcessPanel(rProcessState);

			aProcessPanel.buildIn(this, AlignedPosition.CENTER);
			aProcessPanel.handleCommandResult(rProcessState);
		}
	}

	/***************************************
	 * Overridden to check whether process panels are currently open.
	 *
	 * @see GwtApplicationPanelManager#getCloseWarning()
	 */
	@Override
	protected String getCloseWarning()
	{
		return aProcessPanel != null ? "$msgWindowCloseWarning" : null;
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected Container getProcessContainer()
	{
		return getContainer();
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected DataElementList getUserData()
	{
		return rUserData;
	}

	/***************************************
	 * Overridden to perform the error handling for process executions.
	 *
	 * @see GwtApplicationPanelManager#handleError(Throwable)
	 */
	@Override
	protected void handleError(Throwable eCaught)
	{
		if (aProcessPanel != null)
		{
			aProcessPanel.handleError(eCaught);
		}
		else
		{
			displayMessage("$msgServiceCallFailed", MESSAGE_DISPLAY_TIME);
		}
	}

	/***************************************
	 * @see CustomerSelfCarePanelManager#logout()
	 */
	@Override
	protected void logout()
	{
		dispose();
		checkAuthentication();
	}

	/***************************************
	 * Overridden to execute the application process for login.
	 *
	 * @see GwtApplicationPanelManager#performLogin(boolean)
	 */
	@Override
	protected void performLogin(boolean bReauthenticate)
	{
		if (bReauthenticate)
		{
			super.performLogin(true);
		}
		else
		{
			executeMainApplicationProcess();
		}
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected void processFinished(
		PanelManager<?, ?> rPanelManager,
		ProcessState	   rProcessState)
	{
		logout();
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected void processUpdated(
		PanelManager<?, ?> rPanelManager,
		ProcessState	   rProcessState)
	{
		// not needed as there is only one application process
	}

	/***************************************
	 * {@inheritDoc}
	 */
	@Override
	protected void removeApplicationPanel()
	{
		if (aProcessPanel != null)
		{
			aProcessPanel.dispose();
			aProcessPanel = null;
		}

		removeAllComponents();
	}
}

@@ -3904,20 +3904,19 @@
 %09%09%09%09%09   
-fals
+tru
 e);%0A%09%7D%0A%0A

set to inline rendering of process as default