<commit_before>/*******************************************************************************
 * Copyright (c) 2017 Red Hat, Inc and others.
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.reddeer.workbench.impl.editor;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.hamcrest.Matcher;
import org.eclipse.reddeer.common.exception.RedDeerException;
import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException;
import org.eclipse.reddeer.common.logging.Logger;
import org.eclipse.reddeer.common.matcher.RegexMatcher;
import org.eclipse.reddeer.common.wait.AbstractWait;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.core.exception.CoreLayerException;
import org.eclipse.reddeer.core.handler.MenuItemHandler;
import org.eclipse.reddeer.core.lookup.ShellLookup;
import org.eclipse.reddeer.core.matcher.WithTextMatcher;
import org.eclipse.reddeer.core.reference.ReferencedComposite;
import org.eclipse.reddeer.core.util.InstanceValidator;
import org.eclipse.reddeer.jface.text.contentassist.ContentAssistant;
import org.eclipse.reddeer.swt.api.MenuItem;
import org.eclipse.reddeer.swt.impl.ctab.DefaultCTabItem;
import org.eclipse.reddeer.swt.impl.menu.ShellMenuItem;
import org.eclipse.reddeer.swt.keyboard.KeyboardFactory;
import org.eclipse.reddeer.workbench.api.Editor;
import org.eclipse.reddeer.workbench.api.EditorFile;
import org.eclipse.reddeer.workbench.condition.ContentAssistantShellIsOpened;
import org.eclipse.reddeer.workbench.condition.EditorIsDirty;
import org.eclipse.reddeer.workbench.core.lookup.EditorPartLookup;
import org.eclipse.reddeer.workbench.core.lookup.WorkbenchShellLookup;
import org.eclipse.reddeer.workbench.exception.WorkbenchLayerException;
import org.eclipse.reddeer.workbench.handler.EditorHandler;
import org.eclipse.reddeer.workbench.handler.WorkbenchPartHandler;
import org.eclipse.reddeer.workbench.impl.shell.WorkbenchShell;
import org.eclipse.reddeer.workbench.matcher.EditorPartTitleMatcher;
import org.eclipse.reddeer.workbench.part.AbstractWorkbenchPart;

/**
 * Abstract class for all Editor implementations.
 * 
 * @author rawagner
 */
public abstract class AbstractEditor extends AbstractWorkbenchPart implements Editor {

	protected static final Logger log = Logger.getLogger(AbstractEditor.class);

	protected IEditorPart editorPart;

	/**
	 * Initialize editor.
	 */
	public AbstractEditor() {
		this(EditorPartLookup.getInstance().getEditor());
	}

	/**
	 * Initialize editor with given title and activate it.
	 * 
	 * @param title
	 *            Title of editor to initialize and activate
	 */
	public AbstractEditor(String title) {
		this(new WithTextMatcher(title));
	}

	/**
	 * Initialize editor with given title matcher.
	 * 
	 * @param title
	 *            Title matcher of editor to initialize and activate
	 */
	@SuppressWarnings("unchecked")
	public AbstractEditor(Matcher<String> title) {
		this(new EditorPartTitleMatcher(title));
	}

	/**
	 * Initialize editor that matches given matchers
	 * 
	 * @param matchers
	 *            Matchers the editor should match
	 */
	@SuppressWarnings("unchecked")
	public AbstractEditor(Matcher<IEditorPart>... matchers) {
		this(EditorPartLookup.getInstance().getEditor(matchers));
	}

	protected AbstractEditor(IEditorPart part) {
		// find also dirty editors
		super(new DefaultCTabItem(new WorkbenchShellReferenced(), new WithTextMatcher(new RegexMatcher("\\*?" + part.getTitle()))));
		InstanceValidator.checkNotNull(part, "part");
		this.editorPart = part;
		activate();
	}

	@Override
	public boolean isDirty() {
		return EditorHandler.getInstance().isDirty(editorPart);
	}

	@Override
	public void save() {
		activate();
		log.info("Save editor");
		ShellMenuItem saveItem = new ShellMenuItem(new WorkbenchShell(), "File", "Save");
		if (saveItem.isEnabled()) {
			saveItem.select();
		} else {
			throw new WorkbenchLayerException("Menu item 'Save' is not enabled");
		}
		new WaitWhile(new EditorIsDirty(this));
		activate();
	}

	@Override
	public void close() {
		close(false);
	}

	@Override
	public void close(final boolean save) {
		activate();
		if (save) {
			log.info("Close editor and save it");
		} else {
			log.info("Close editor and do not save it");
		}
		EditorHandler.getInstance().close(save, editorPart);
	}

	@Override
	public void activate() {
		cTabItem.activate();
		EditorHandler.getInstance().activate(editorPart);
		WorkbenchPartHandler.getInstance().focusChildControl(editorPart);
	}
	
	@Override
	public boolean isActive() {
		return editorPart.equals(EditorPartLookup.getInstance().getActiveEditor());
	}

	@Override
	public ContentAssistant openContentAssistant() {
		MenuItem assistMenu = null;
		try {
			assistMenu = new ShellMenuItem("Edit", "Content Assist");
		} catch (RedDeerException e) {
			log.info("Content assist menu not found, open via keyboard shortcut");
			return openContentAssistantViaKeyboard();
		}
		boolean hasAssistMenuChildren = MenuItemHandler.getInstance()
				.getMenuFromMenuItem(assistMenu.getSWTWidget()) != null;

		if (hasAssistMenuChildren) {
			return openContentAssistant(ContentAssistantEnum.DEFAULT);
		}
		return openContentAssistant("Edit", "Content Assist");
	}
<commit_msg>Do not activate cTabItem when activating an editor #1844<commit_after>/*******************************************************************************
 * Copyright (c) 2017 Red Hat, Inc and others.
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.reddeer.workbench.impl.editor;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.reddeer.common.exception.RedDeerException;
import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException;
import org.eclipse.reddeer.common.logging.Logger;
import org.eclipse.reddeer.common.matcher.RegexMatcher;
import org.eclipse.reddeer.common.wait.AbstractWait;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.core.handler.MenuItemHandler;
import org.eclipse.reddeer.core.lookup.ShellLookup;
import org.eclipse.reddeer.core.matcher.WithTextMatcher;
import org.eclipse.reddeer.core.reference.ReferencedComposite;
import org.eclipse.reddeer.core.util.InstanceValidator;
import org.eclipse.reddeer.jface.text.contentassist.ContentAssistant;
import org.eclipse.reddeer.swt.api.MenuItem;
import org.eclipse.reddeer.swt.impl.ctab.DefaultCTabItem;
import org.eclipse.reddeer.swt.impl.menu.ShellMenuItem;
import org.eclipse.reddeer.swt.keyboard.KeyboardFactory;
import org.eclipse.reddeer.workbench.api.Editor;
import org.eclipse.reddeer.workbench.api.EditorFile;
import org.eclipse.reddeer.workbench.condition.ContentAssistantShellIsOpened;
import org.eclipse.reddeer.workbench.condition.EditorIsDirty;
import org.eclipse.reddeer.workbench.core.lookup.EditorPartLookup;
import org.eclipse.reddeer.workbench.core.lookup.WorkbenchShellLookup;
import org.eclipse.reddeer.workbench.exception.WorkbenchLayerException;
import org.eclipse.reddeer.workbench.handler.EditorHandler;
import org.eclipse.reddeer.workbench.impl.shell.WorkbenchShell;
import org.eclipse.reddeer.workbench.matcher.EditorPartTitleMatcher;
import org.eclipse.reddeer.workbench.part.AbstractWorkbenchPart;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.hamcrest.Matcher;

/**
 * Abstract class for all Editor implementations.
 * 
 * @author rawagner
 */
public abstract class AbstractEditor extends AbstractWorkbenchPart implements Editor {

	protected static final Logger log = Logger.getLogger(AbstractEditor.class);

	protected IEditorPart editorPart;

	/**
	 * Initialize editor.
	 */
	public AbstractEditor() {
		this(EditorPartLookup.getInstance().getEditor());
	}

	/**
	 * Initialize editor with given title and activate it.
	 * 
	 * @param title
	 *            Title of editor to initialize and activate
	 */
	public AbstractEditor(String title) {
		this(new WithTextMatcher(title));
	}

	/**
	 * Initialize editor with given title matcher.
	 * 
	 * @param title
	 *            Title matcher of editor to initialize and activate
	 */
	@SuppressWarnings("unchecked")
	public AbstractEditor(Matcher<String> title) {
		this(new EditorPartTitleMatcher(title));
	}

	/**
	 * Initialize editor that matches given matchers
	 * 
	 * @param matchers
	 *            Matchers the editor should match
	 */
	@SuppressWarnings("unchecked")
	public AbstractEditor(Matcher<IEditorPart>... matchers) {
		this(EditorPartLookup.getInstance().getEditor(matchers));
	}

	protected AbstractEditor(IEditorPart part) {
		// find also dirty editors
		super(new DefaultCTabItem(new WorkbenchShellReferenced(), new WithTextMatcher(new RegexMatcher("\\*?" + part.getTitle()))));
		InstanceValidator.checkNotNull(part, "part");
		this.editorPart = part;
		activate();
	}

	@Override
	public boolean isDirty() {
		return EditorHandler.getInstance().isDirty(editorPart);
	}

	@Override
	public void save() {
		activate();
		log.info("Save editor");
		ShellMenuItem saveItem = new ShellMenuItem(new WorkbenchShell(), "File", "Save");
		if (saveItem.isEnabled()) {
			saveItem.select();
		} else {
			throw new WorkbenchLayerException("Menu item 'Save' is not enabled");
		}
		new WaitWhile(new EditorIsDirty(this));
		activate();
	}

	@Override
	public void close() {
		close(false);
	}

	@Override
	public void close(final boolean save) {
		activate();
		if (save) {
			log.info("Close editor and save it");
		} else {
			log.info("Close editor and do not save it");
		}
		EditorHandler.getInstance().close(save, editorPart);
	}

	@Override
	public void activate() {
		EditorHandler.getInstance().activate(editorPart);
	}
	
	@Override
	public boolean isActive() {
		return editorPart.equals(EditorPartLookup.getInstance().getActiveEditor());
	}

	@Override
	public ContentAssistant openContentAssistant() {
		MenuItem assistMenu = null;
		try {
			assistMenu = new ShellMenuItem("Edit", "Content Assist");
		} catch (RedDeerException e) {
			log.info("Content assist menu not found, open via keyboard shortcut");
			return openContentAssistantViaKeyboard();
		}
		boolean hasAssistMenuChildren = MenuItemHandler.getInstance()
				.getMenuFromMenuItem(assistMenu.getSWTWidget()) != null;

		if (hasAssistMenuChildren) {
			return openContentAssistant(ContentAssistantEnum.DEFAULT);
		}
		return openContentAssistant("Edit", "Content Assist");
	}

{'fraction_non_alphanumeric': 0.1326150020721094, 'fraction_numerical': 0.0016576875259013675, 'mean_word_length': 5.245341614906832, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1571644', 'n_tokens_mistral': 3698, 'n_tokens_neox': 3482, 'n_words': 734}