<commit_before>/**
 * Copyright (c) 2010 committers of YAKINDU 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:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */
package org.yakindu.sct.ui.editor.propertysheets;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Image;
import org.yakindu.base.base.NamedElement;
import org.yakindu.sct.ui.editor.DiagramActivator;

/**
 * 
 * @author <NAME>
 * 
 */
public class SheetLabelProvider extends BaseLabelProvider implements
		ILabelProvider {

	public String getText(Object element) {
		element = unwrap(element);
		if (element instanceof IGraphicalEditPart) {
			EObject semanticElement = ((IGraphicalEditPart) element)
					.resolveSemanticElement();
			
			if(semanticElement == null)
				return null;
			
			IElementType elementType = ElementTypeRegistry.getInstance()
					.getElementType(semanticElement);
			StringBuilder builder = new StringBuilder();
			builder.append(elementType.getDisplayName());
			if (semanticElement instanceof NamedElement
					&& ((NamedElement) semanticElement).getName() != null) {
				builder.append(' ');
				builder.append(((NamedElement) semanticElement).getName());
			}
			return builder.toString();
		}
		return null;
	}

	public Image getImage(Object element) {
		element = unwrap(element);
		if (element instanceof IGraphicalEditPart) {
			EObject semanticElement = ((IGraphicalEditPart) element)
					.resolveSemanticElement();
			
			if(semanticElement == null)
				return null;
			
			IElementType elementType = ElementTypeRegistry.getInstance()
					.getElementType(semanticElement);
			Image image = DiagramActivator.getDefault().getImageRegistry()
					.get(elementType.getIconURL().toString());
			if (image == null) {
				ImageDescriptor desc = ImageDescriptor
						.createFromURL(elementType.getIconURL());
				DiagramActivator
						.getDefault()
						.getImageRegistry()
						.put(elementType.getIconURL().toString(),
								desc.createImage());
				return DiagramActivator.getDefault().getImageRegistry()
						.get(elementType.getIconURL().toString());
			}
			return image;
		}
		return null;
	}

	private Object unwrap(Object element) {
		if (element instanceof IStructuredSelection) {
			return ((IStructuredSelection) element).getFirstElement();
		}
		return element;
	}

}<commit_msg>Added SheetLabelProvider for default element types (Note, Text,NoteAttachment)<commit_after>/**
 * Copyright (c) 2010 committers of YAKINDU 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:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */
package org.yakindu.sct.ui.editor.propertysheets;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.core.util.ViewType;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.l10n.SharedImages;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Image;
import org.yakindu.base.base.NamedElement;
import org.yakindu.sct.ui.editor.DiagramActivator;

/**
 * 
 * @author <NAME>
 * 
 */
public class SheetLabelProvider extends BaseLabelProvider implements
		ILabelProvider {

	public String getText(Object element) {
		element = unwrap(element);
		if (element instanceof IGraphicalEditPart) {
			EObject semanticElement = ((IGraphicalEditPart) element)
					.resolveSemanticElement();

			if (semanticElement == null) {
				View view = ((IGraphicalEditPart) element).getNotationView();
				return view.getType();
			} else {

				IElementType elementType = ElementTypeRegistry.getInstance()
						.getElementType(semanticElement);
				StringBuilder builder = new StringBuilder();
				builder.append(elementType.getDisplayName());
				if (semanticElement instanceof NamedElement
						&& ((NamedElement) semanticElement).getName() != null) {
					builder.append(' ');
					builder.append(((NamedElement) semanticElement).getName());
				}
				return builder.toString();
			}
		}
		return null;
	}

	public Image getImage(Object element) {
		element = unwrap(element);
		if (element instanceof IGraphicalEditPart) {
			EObject semanticElement = ((IGraphicalEditPart) element)
					.resolveSemanticElement();

			if (semanticElement == null) {
				// Default images
				View view = ((IGraphicalEditPart) element).getNotationView();
				String viewType = view.getType();
				if (ViewType.NOTE.equals(viewType)) {
					return SharedImages.get(SharedImages.IMG_NOTE);
				} else if (ViewType.TEXT.equals(viewType)) {
					return SharedImages.get(SharedImages.IMG_TEXT);
				}
			} else {
				// custom images
				IElementType elementType = ElementTypeRegistry.getInstance()
						.getElementType(semanticElement);
				Image image = DiagramActivator.getDefault().getImageRegistry()
						.get(elementType.getIconURL().toString());
				if (image == null) {
					ImageDescriptor desc = ImageDescriptor
							.createFromURL(elementType.getIconURL());
					DiagramActivator
							.getDefault()
							.getImageRegistry()
							.put(elementType.getIconURL().toString(),
									desc.createImage());
					return DiagramActivator.getDefault().getImageRegistry()
							.get(elementType.getIconURL().toString());
				}
				return image;

			}
		}
		return null;
	}

	private Object unwrap(Object element) {
		if (element instanceof IStructuredSelection) {
			return ((IStructuredSelection) element).getFirstElement();
		}
		return element;
	}

}
{'fraction_non_alphanumeric': 0.10985191900876398, 'fraction_numerical': 0.0027198549410698096, 'mean_word_length': 4.286741214057508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3146604', 'n_tokens_mistral': 2280, 'n_tokens_neox': 1913, 'n_words': 401}