<commit_before>package ch.elexis.core.ui.views;

import java.util.Optional;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.events.ElexisEvent;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.data.service.StoreToStringServiceHolder;
import ch.elexis.core.model.ICodeElement;
import ch.elexis.core.model.Identifiable;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.ui.UiDesk;
import ch.elexis.core.ui.actions.CodeSelectorHandler;
import ch.elexis.core.ui.actions.ICodeSelectorTarget;
import ch.elexis.core.ui.actions.Messages;
import ch.elexis.core.ui.events.ElexisUiEventListenerImpl;
import ch.elexis.core.ui.icons.ImageSize;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.data.VerrechenbarFavorites;
import ch.elexis.data.VerrechenbarFavorites.Favorite;

public class FavoritenComposite extends Composite {
	
	private TableViewer tv;
	private Transfer[] types = new Transfer[] {
		TextTransfer.getInstance()
	};
	
	private ElexisUiEventListenerImpl reloadListener =
		new ElexisUiEventListenerImpl(Favorite.class, ElexisEvent.EVENT_RELOAD) {
			@Override
			public void runInUi(ElexisEvent ev){
				update();
			}
		};
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public FavoritenComposite(Composite parent, int style){
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		CLabel lblHeader = new CLabel(this, SWT.NONE);
		
		lblHeader.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		lblHeader.setText("Favoriten");
		lblHeader.setImage(Images.IMG_STAR.getImage(ImageSize._75x66_TitleDialogIconSize));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		TableColumnLayout tcl_composite = new TableColumnLayout();
		composite.setLayout(tcl_composite);
		
		tv = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		final Table table = tv.getTable();
		table.setHeaderVisible(true);
		table.addListener(SWT.MeasureItem, new Listener() {
			
			@Override
			public void handleEvent(Event event){
				int clientWidth = table.getClientArea().width;
				event.height = (event.gc.getFontMetrics().getHeight() * 2) + 2;
				event.width = clientWidth * 2;
			}
		});
		
		table.addListener(SWT.PaintItem, new Listener() {
			@Override
			public void handleEvent(Event event){
				TableItem item = (TableItem) event.item;
				
				Favorite fav = (Favorite) item.getData();
				Optional<Identifiable> cfs = fav.getObject();
				
				String simpleName = "?";
				String label = "?";
				if (cfs.isPresent()) {
					simpleName = cfs.get().getClass().getSimpleName();
					label = cfs.get().getLabel();
				}
				
				/* center column 1 vertically */
				int yOffset = 0;
				
				Font defaultFont = event.gc.getFont();
				FontDescriptor boldDescriptor =
					FontDescriptor.createFrom(defaultFont).setStyle(SWT.BOLD);
				Font boldFont = boldDescriptor.createFont(event.display);
				
				switch (event.index) {
				case 0:
					event.gc.setFont(defaultFont);
					event.gc.drawText(fav.getMacroString(), event.x + 3, event.y + yOffset, true);
					break;
				case 1:
					event.gc.setFont(boldFont);
					event.gc.drawText(simpleName, event.x + 3, event.y + yOffset, true);
					event.gc.setFont(defaultFont);
					event.gc.drawText(label, event.x + 3,
						event.y + event.gc.getFontMetrics().getHeight(), true);
					break;
				default:
					break;
				}
				
			}
		});
		tv.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event){
				ICodeSelectorTarget target =
					CodeSelectorHandler.getInstance().getCodeSelectorTarget();
				if (target != null) {
					StructuredSelection ss = (StructuredSelection) tv.getSelection();
					if (!ss.isEmpty()) {
						Favorite fav = (Favorite) ss.getFirstElement();
						Optional<Identifiable> po = fav.getObject();
						if (po.isPresent()) {<commit_msg>[22467] FavoritenComposite: Argument cannot be null<commit_after>package ch.elexis.core.ui.views;

import java.util.Optional;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.events.ElexisEvent;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.data.service.StoreToStringServiceHolder;
import ch.elexis.core.model.ICodeElement;
import ch.elexis.core.model.Identifiable;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.ui.UiDesk;
import ch.elexis.core.ui.actions.CodeSelectorHandler;
import ch.elexis.core.ui.actions.ICodeSelectorTarget;
import ch.elexis.core.ui.actions.Messages;
import ch.elexis.core.ui.events.ElexisUiEventListenerImpl;
import ch.elexis.core.ui.icons.ImageSize;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.data.VerrechenbarFavorites;
import ch.elexis.data.VerrechenbarFavorites.Favorite;

public class FavoritenComposite extends Composite {
	
	private TableViewer tv;
	private Transfer[] types = new Transfer[] {
		TextTransfer.getInstance()
	};
	
	private ElexisUiEventListenerImpl reloadListener =
		new ElexisUiEventListenerImpl(Favorite.class, ElexisEvent.EVENT_RELOAD) {
			@Override
			public void runInUi(ElexisEvent ev){
				update();
			}
		};
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public FavoritenComposite(Composite parent, int style){
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		CLabel lblHeader = new CLabel(this, SWT.NONE);
		
		lblHeader.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		lblHeader.setText("Favoriten");
		lblHeader.setImage(Images.IMG_STAR.getImage(ImageSize._75x66_TitleDialogIconSize));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		TableColumnLayout tcl_composite = new TableColumnLayout();
		composite.setLayout(tcl_composite);
		
		tv = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		final Table table = tv.getTable();
		table.setHeaderVisible(true);
		table.addListener(SWT.MeasureItem, new Listener() {
			
			@Override
			public void handleEvent(Event event){
				int clientWidth = table.getClientArea().width;
				event.height = (event.gc.getFontMetrics().getHeight() * 2) + 2;
				event.width = clientWidth * 2;
			}
		});
		
		table.addListener(SWT.PaintItem, new Listener() {
			@Override
			public void handleEvent(Event event){
				TableItem item = (TableItem) event.item;
				
				Favorite fav = (Favorite) item.getData();
				Optional<Identifiable> cfs = fav.getObject();
				
				String simpleName = "?";
				String label = "?";
				if (cfs.isPresent()) {
					simpleName = cfs.get().getClass().getSimpleName();
					label = cfs.get().getLabel();
				}
				
				/* center column 1 vertically */
				int yOffset = 0;
				
				Font defaultFont = event.gc.getFont();
				FontDescriptor boldDescriptor =
					FontDescriptor.createFrom(defaultFont).setStyle(SWT.BOLD);
				Font boldFont = boldDescriptor.createFont(event.display);
				
				switch (event.index) {
				case 0:
					event.gc.setFont(defaultFont);
					event.gc.drawText(fav.getMacroString() != null ? fav.getMacroString() : "",
						event.x + 3, event.y + yOffset, true);
					break;
				case 1:
					event.gc.setFont(boldFont);
					event.gc.drawText(simpleName, event.x + 3, event.y + yOffset, true);
					event.gc.setFont(defaultFont);
					event.gc.drawText(label, event.x + 3,
						event.y + event.gc.getFontMetrics().getHeight(), true);
					break;
				default:
					break;
				}
				
			}
		});
		tv.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event){
				ICodeSelectorTarget target =
					CodeSelectorHandler.getInstance().getCodeSelectorTarget();
				if (target != null) {
					StructuredSelection ss = (StructuredSelection) tv.getSelection();
					if (!ss.isEmpty()) {
						Favorite fav = (Favorite) ss.getFirstElement();
						Optional<Identifiable> po = fav.getObject();
						if (po.isPresent()) {
{'fraction_non_alphanumeric': 0.12101346562721474, 'fraction_numerical': 0.003809355067328136, 'mean_word_length': 4.445730824891462, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '261498', 'n_tokens_mistral': 4214, 'n_tokens_neox': 3702, 'n_words': 636}