<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.designer.ui.actions;

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

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.dnd.DNDUtil;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.actions.ApplyStyleAction;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.SharedStyleHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Updates "Apply style" menu.
 */

public class ApplyStyleMenuAction extends MenuUpdateAction
{

	public static final String ID = "apply style menu"; //$NON-NLS-1$

	/**
	 * @param part
	 */
	public ApplyStyleMenuAction( IWorkbenchPart part )
	{
		super( part );
		setId( ID );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.actions.MenuUpdateAction#getItems()
	 */
	protected List getItems( )
	{
		ArrayList actionList = new ArrayList( );

		StyleHandle currentStyle = getStyleHandle( );
		ApplyStyleAction reset = new ApplyStyleAction( null );
		reset.setSelection( getSelection( ) );
		reset.setChecked( currentStyle == null );
		actionList.add( reset );
		actionList.add( null );//Adds separator

		Iterator iterator = SessionHandleAdapter.getInstance( )
				.getReportDesignHandle( )
				.getStyles( )
				.iterator( );
		while ( iterator.hasNext( ) )
		{
			SharedStyleHandle handle = (SharedStyleHandle) iterator.next( );
			ApplyStyleAction action = new ApplyStyleAction( handle );
			action.setSelection( getSelection( ) );
			action.setChecked( currentStyle == handle );
			actionList.add( action );
		}
		return actionList;
	}

	private StyleHandle getStyleHandle( )
	{
		IStructuredSelection selection = DNDUtil.editPart2Model( getSelection( ) );
		if ( !selection.isEmpty( )
				&& selection.getFirstElement( ) instanceof DesignElementHandle )
		{
			SharedStyleHandle style = ( (DesignElementHandle) selection.getFirstElement( ) ).getStyle( );
			for ( Iterator iterator = selection.iterator( ); iterator.hasNext( ); )
			{
				Object obj = iterator.next( );
				if ( !( obj instanceof DesignElementHandle ) )
				{
					return null;
				}
				if ( ( (DesignElementHandle) obj ).getStyle( ) != style )
				{
					return null;
				}
			}
			return style;
		}<commit_msg>- Summary: Filter predefined styles when apply style.<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.designer.ui.actions;

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

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.dnd.DNDUtil;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.actions.ApplyStyleAction;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.SharedStyleHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Updates "Apply style" menu.
 */

public class ApplyStyleMenuAction extends MenuUpdateAction
{

	public static final String ID = "apply style menu"; //$NON-NLS-1$

	/**
	 * @param part
	 */
	public ApplyStyleMenuAction( IWorkbenchPart part )
	{
		super( part );
		setId( ID );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.actions.MenuUpdateAction#getItems()
	 */
	protected List getItems( )
	{
		ArrayList actionList = new ArrayList( );

		StyleHandle currentStyle = getStyleHandle( );
		ApplyStyleAction reset = new ApplyStyleAction( null );
		reset.setSelection( getSelection( ) );
		reset.setChecked( currentStyle == null );
		actionList.add( reset );
		actionList.add( null );//Adds separator

		Iterator iterator = SessionHandleAdapter.getInstance( )
				.getReportDesignHandle( )
				.getStyles( )
				.iterator( );
		while ( iterator.hasNext( ) )
		{
			SharedStyleHandle handle = (SharedStyleHandle) iterator.next( );
			if ( !handle.isPredefined( ) )
			{
				ApplyStyleAction action = new ApplyStyleAction( handle );
				action.setSelection( getSelection( ) );
				action.setChecked( currentStyle == handle );
				actionList.add( action );
			}
		}
		return actionList;
	}

	private StyleHandle getStyleHandle( )
	{
		IStructuredSelection selection = DNDUtil.editPart2Model( getSelection( ) );
		if ( !selection.isEmpty( )
				&& selection.getFirstElement( ) instanceof DesignElementHandle )
		{
			SharedStyleHandle style = ( (DesignElementHandle) selection.getFirstElement( ) ).getStyle( );
			for ( Iterator iterator = selection.iterator( ); iterator.hasNext( ); )
			{
				Object obj = iterator.next( );
				if ( !( obj instanceof DesignElementHandle ) )
				{
					return null;
				}
				if ( ( (DesignElementHandle) obj ).getStyle( ) != style )
				{
					return null;
				}
			}
			return style;
		}
{'fraction_non_alphanumeric': 0.1604796320630749, 'fraction_numerical': 0.0032851511169513796, 'mean_word_length': 4.15580016934801, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1379858', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1731, 'n_words': 389}