<commit_before>/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.item.crosstab.internal.ui.editors.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.internal.ui.util.UIUtil;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.item.crosstab.core.ICrosstabConstants;
import org.eclipse.birt.report.item.crosstab.core.de.AggregationCellHandle;
import org.eclipse.birt.report.item.crosstab.core.de.ComputedMeasureViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabCellHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabReportItemHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.DimensionViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.LevelViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.MeasureViewHandle;
import org.eclipse.birt.report.item.crosstab.core.util.CrosstabUtil;
import org.eclipse.birt.report.item.crosstab.internal.ui.AggregationCellProviderWrapper;
import org.eclipse.birt.report.item.crosstab.internal.ui.dialogs.AggregationDialog;
import org.eclipse.birt.report.item.crosstab.internal.ui.dialogs.AggregationDialog.GrandTotalInfo;
import org.eclipse.birt.report.item.crosstab.internal.ui.dialogs.AggregationDialog.SubTotalInfo;
import org.eclipse.birt.report.item.crosstab.internal.ui.editors.model.CrosstabAdaptUtil;
import org.eclipse.birt.report.item.crosstab.internal.ui.util.CrosstabUIHelper;
import org.eclipse.birt.report.item.crosstab.ui.extension.IAggregationCellViewProvider;
import org.eclipse.birt.report.item.crosstab.ui.extension.SwitchCellInfo;
import org.eclipse.birt.report.item.crosstab.ui.i18n.Messages;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.olap.LevelHandle;
import org.eclipse.birt.report.model.api.olap.MeasureHandle;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;

/**
 * Add the sub total to the level handle.
 */
public class AddSubTotalAction extends AbstractCrosstabAction
{

	boolean needUpdateView = false;
	LevelViewHandle levelHandle = null;
	private static final String NAME = Messages.getString( "AddSubTotalAction.TransName" );//$NON-NLS-1$
	private static final String ID = "add_subtotal";//$NON-NLS-1$
	private static final String TEXT = Messages.getString( "AddSubTotalAction.DisplayName" );//$NON-NLS-1$

	/**
	 * The name of the label into the sub total cell.
	 */
	// private static final String DISPALY_NAME = "TOTAL";
	/**
	 * Constructor
	 * 
	 * @param handle
	 */
	public AddSubTotalAction( DesignElementHandle handle )
	{
		super( handle );
		setId( ID );
		setText( TEXT );
		ExtendedItemHandle extendedHandle = CrosstabAdaptUtil.getExtendedItemHandle( handle );
		setHandle( extendedHandle );
		levelHandle = CrosstabAdaptUtil.getLevelViewHandle( extendedHandle );

		Image image = CrosstabUIHelper.getImage( CrosstabUIHelper.LEVEL_AGGREGATION );
		setImageDescriptor( ImageDescriptor.createFromImage( image ) );
	}

	private AggregationCellProviderWrapper providerWrapper;

	private void initializeProviders( )
	{
		providerWrapper = new AggregationCellProviderWrapper( (ExtendedItemHandle) levelHandle.getCrosstab( )
				.getModelHandle( ) );
	}

	public boolean isEnabled( )
	{
		return !DEUtil.isReferenceElement( levelHandle.getCrosstabHandle( ) );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run( )
	{
		transStar( NAME );
		try
		{

			AggregationDialog dialog = new AggregationDialog( UIUtil.getDefaultShell( ),
					levelHandle.getCrosstab( ) );
			dialog.setAxis( levelHandle.getAxisType( ) );
			List<AggregationDialog.SubTotalInfo> rowSubTotals = getSubTotalInfo( ICrosstabConstants.ROW_AXIS_TYPE );
			List<AggregationDialog.GrandTotalInfo> rowGrandTotals = getGrandTotalInfo( ICrosstabConstants.ROW_AXIS_TYPE );
			List<AggregationDialog.SubTotalInfo> colSubTotals = getSubTotalInfo( ICrosstabConstants.COLUMN_AXIS_TYPE );
			List<AggregationDialog.GrandTotalInfo> colGrandTotals = getGrandTotalInfo( ICrosstabConstants.COLUMN_AXIS_TYPE );
			dialog.setInput( copySubTotal( rowSubTotals ),
					copyGrandTotal( rowGrandTotals ),
					copySubTotal( colSubTotals ),
					copyGrandTotal( colGrandTotals ) );
			if ( dialog.open( ) == Window.OK )
			{
				initializeProviders( );

				Object[] result = (Object[]) dialog.getResult( );
				processSubTotal( rowSubTotals,
						(List<AggregationDialog.SubTotalInfo>) result[0] );
				processGrandTotal( rowGrandTotals,
						(List<AggregationDialog.GrandTotalInfo>) result[1],
						ICrosstabConstants.ROW_AXIS_TYPE );

				processSubTotal( colSubTotals,
						(List<AggregationDialog.SubTotalInfo>) result[2] );
				processGrandTotal( colGrandTotals,
						(List<AggregationDialog.GrandTotalInfo>) result[3],
						ICrosstabConstants.COLUMN_AXIS_TYPE );

				providerWrapper.switchViews( );
				if ( needUpdateView )
				{
					// providerWrapper.updateAllAggregationCells(
					// AggregationCellViewAdapter.SWITCH_VIEW_TYPE );
				}
			}
		}
		catch ( SemanticException e )
		{
			rollBack( );
			ExceptionHandler.handle( e );
			return;
		}
		transEnd( );
	}

	private List<AggregationDialog.SubTotalInfo> copySubTotal(
			List<AggregationDialog.SubTotalInfo> list )
	{
		List<AggregationDialog.SubTotalInfo> retValue = new ArrayList<AggregationDialog.SubTotalInfo>( );
		for ( int i = 0; i < list.size( ); i++ )
		{<commit_msg>- Summary: Show as chart may work incorrectly.<commit_after>/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.item.crosstab.internal.ui.editors.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.internal.ui.util.UIUtil;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.item.crosstab.core.ICrosstabConstants;
import org.eclipse.birt.report.item.crosstab.core.de.AggregationCellHandle;
import org.eclipse.birt.report.item.crosstab.core.de.ComputedMeasureViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabCellHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabReportItemHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.DimensionViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.LevelViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.MeasureViewHandle;
import org.eclipse.birt.report.item.crosstab.core.util.CrosstabUtil;
import org.eclipse.birt.report.item.crosstab.internal.ui.AggregationCellProviderWrapper;
import org.eclipse.birt.report.item.crosstab.internal.ui.dialogs.AggregationDialog;
import org.eclipse.birt.report.item.crosstab.internal.ui.dialogs.AggregationDialog.GrandTotalInfo;
import org.eclipse.birt.report.item.crosstab.internal.ui.dialogs.AggregationDialog.SubTotalInfo;
import org.eclipse.birt.report.item.crosstab.internal.ui.editors.model.CrosstabAdaptUtil;
import org.eclipse.birt.report.item.crosstab.internal.ui.util.CrosstabUIHelper;
import org.eclipse.birt.report.item.crosstab.ui.extension.AggregationCellViewAdapter;
import org.eclipse.birt.report.item.crosstab.ui.extension.IAggregationCellViewProvider;
import org.eclipse.birt.report.item.crosstab.ui.extension.SwitchCellInfo;
import org.eclipse.birt.report.item.crosstab.ui.i18n.Messages;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.olap.LevelHandle;
import org.eclipse.birt.report.model.api.olap.MeasureHandle;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;

/**
 * Add the sub total to the level handle.
 */
public class AddSubTotalAction extends AbstractCrosstabAction
{

	boolean needUpdateView = false;
	LevelViewHandle levelHandle = null;
	private static final String NAME = Messages.getString( "AddSubTotalAction.TransName" );//$NON-NLS-1$
	private static final String ID = "add_subtotal";//$NON-NLS-1$
	private static final String TEXT = Messages.getString( "AddSubTotalAction.DisplayName" );//$NON-NLS-1$

	/**
	 * The name of the label into the sub total cell.
	 */
	// private static final String DISPALY_NAME = "TOTAL";
	/**
	 * Constructor
	 * 
	 * @param handle
	 */
	public AddSubTotalAction( DesignElementHandle handle )
	{
		super( handle );
		setId( ID );
		setText( TEXT );
		ExtendedItemHandle extendedHandle = CrosstabAdaptUtil.getExtendedItemHandle( handle );
		setHandle( extendedHandle );
		levelHandle = CrosstabAdaptUtil.getLevelViewHandle( extendedHandle );

		Image image = CrosstabUIHelper.getImage( CrosstabUIHelper.LEVEL_AGGREGATION );
		setImageDescriptor( ImageDescriptor.createFromImage( image ) );
	}

	private AggregationCellProviderWrapper providerWrapper;

	private void initializeProviders( )
	{
		providerWrapper = new AggregationCellProviderWrapper( (ExtendedItemHandle) levelHandle.getCrosstab( )
				.getModelHandle( ) );
	}

	public boolean isEnabled( )
	{
		return !DEUtil.isReferenceElement( levelHandle.getCrosstabHandle( ) );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run( )
	{
		transStar( NAME );
		try
		{

			AggregationDialog dialog = new AggregationDialog( UIUtil.getDefaultShell( ),
					levelHandle.getCrosstab( ) );
			dialog.setAxis( levelHandle.getAxisType( ) );
			List<AggregationDialog.SubTotalInfo> rowSubTotals = getSubTotalInfo( ICrosstabConstants.ROW_AXIS_TYPE );
			List<AggregationDialog.GrandTotalInfo> rowGrandTotals = getGrandTotalInfo( ICrosstabConstants.ROW_AXIS_TYPE );
			List<AggregationDialog.SubTotalInfo> colSubTotals = getSubTotalInfo( ICrosstabConstants.COLUMN_AXIS_TYPE );
			List<AggregationDialog.GrandTotalInfo> colGrandTotals = getGrandTotalInfo( ICrosstabConstants.COLUMN_AXIS_TYPE );
			dialog.setInput( copySubTotal( rowSubTotals ),
					copyGrandTotal( rowGrandTotals ),
					copySubTotal( colSubTotals ),
					copyGrandTotal( colGrandTotals ) );
			if ( dialog.open( ) == Window.OK )
			{
				initializeProviders( );

				Object[] result = (Object[]) dialog.getResult( );
				processSubTotal( rowSubTotals,
						(List<AggregationDialog.SubTotalInfo>) result[0] );
				processGrandTotal( rowGrandTotals,
						(List<AggregationDialog.GrandTotalInfo>) result[1],
						ICrosstabConstants.ROW_AXIS_TYPE );

				processSubTotal( colSubTotals,
						(List<AggregationDialog.SubTotalInfo>) result[2] );
				processGrandTotal( colGrandTotals,
						(List<AggregationDialog.GrandTotalInfo>) result[3],
						ICrosstabConstants.COLUMN_AXIS_TYPE );

				providerWrapper.switchViews( );
				if ( needUpdateView )
				{
					 providerWrapper.updateAllAggregationCells( AggregationCellViewAdapter.SWITCH_VIEW_TYPE );
				}
			}
		}
		catch ( SemanticException e )
		{
			rollBack( );
			ExceptionHandler.handle( e );
			return;
		}
		transEnd( );
	}

	private List<AggregationDialog.SubTotalInfo> copySubTotal(
			List<AggregationDialog.SubTotalInfo> list )
	{
		List<AggregationDialog.SubTotalInfo> retValue = new ArrayList<AggregationDialog.SubTotalInfo>( );
		for ( int i = 0; i < list.size( ); i++ )
		{
{'fraction_non_alphanumeric': 0.13039038567276726, 'fraction_numerical': 0.0028277433037467597, 'mean_word_length': 5.138862102217936, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5402352', 'n_tokens_mistral': 4438, 'n_tokens_neox': 3925, 'n_words': 629}