<commit_before>/*******************************************************************************
 * Copyright (c) 2009, <NAME> and Elexis
 * 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
 *
 * Sponsoring:
 * 	 mediX Notfallpaxis, diepraxen Stauffacher AG, Zürich
 * 
 * Contributors:
 *    <NAME> - initial implementation
 *    
 *******************************************************************************/

package ch.elexis.agenda.ui;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.ui.util.PersistentObjectDropTarget;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.actions.Activator;
import ch.elexis.agenda.data.Termin;
import ch.elexis.agenda.preferences.PreferenceConstants;
import ch.elexis.data.PersistentObject;
import ch.elexis.data.Query;
import ch.elexis.dialogs.TerminDialog;
import ch.rgw.tools.StringTool;
import ch.rgw.tools.TimeTool;

public class ProportionalSheet extends Composite implements IAgendaLayout {
	static final int LEFT_OFFSET_DEFAULT = 20;
	static final int PADDING_DEFAULT = 5;
	
	private int left_offset, padding;
	private AgendaParallel view;
	private MenuManager contextMenuManager;
	private List<TerminLabel> tlabels;
	private double ppm;
	private int sheetHeight;
	private String[] resources;
	private int textWidth;
	private double sheetWidth;
	private double widthPerColumn;
	
	private TimeTool setTerminTo(int x, int y){
		String resource = ""; //$NON-NLS-1$
		for (int i = 0; i < resources.length; i++) {
			double lower = left_offset + i * (widthPerColumn + padding);
			double upper = lower + widthPerColumn;
			if (isBetween(x, lower, upper)) {
				resource = resources[i];
				break;
			}
		}
		int minute = (int) Math.round(y / ppm);
		TimeTool tt = new TimeTool(Activator.getDefault().getActDate());
		int hour = minute / 60;
		minute = minute - (60 * hour);
		int raster = 15;
		minute = ((minute + (raster >> 1)) / raster) * raster;
		tt.set(TimeTool.AM_PM, TimeTool.AM);
		tt.set(TimeTool.HOUR, hour);
		tt.set(TimeTool.MINUTE, minute);
		if (resource.length() > 0) {
			Activator.getDefault().setActResource(resource);
		}
		return tt;
	}
	
	public ProportionalSheet(Composite parent, AgendaParallel v){
		super(parent, SWT.NO_BACKGROUND);
		view = v;
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e){
				layout();
				recalc();
			}
		});
		addPaintListener(new TimePainter());
		addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseDoubleClick(MouseEvent e){
				String startOfDayTimeInMinutes =
					CoreHub.globalCfg.get(PreferenceConstants.AG_DAY_PRESENTATION_STARTS_AT, "0000");
				int sodtHours = Integer.parseInt(startOfDayTimeInMinutes.substring(0, 2));
				int sodtMinutes = Integer.parseInt(startOfDayTimeInMinutes.substring(2));

				TimeTool tt = setTerminTo(e.x, e.y);
				tt.addHours(sodtHours);
				tt.addMinutes(sodtMinutes);
				TerminDialog dlg = new TerminDialog(null);
				dlg.create();
				dlg.setTime(tt);
				if (dlg.open() == Dialog.OK) {
					
					refresh();
				}
			}
			
		});
		
		// setBackground(Desk.getColor(Desk.COL_GREEN));
		left_offset = LEFT_OFFSET_DEFAULT;
		padding = PADDING_DEFAULT;
		new PersistentObjectDropTarget(this, new PersistentObjectDropTarget.IReceiver() {
			
			public boolean accept(PersistentObject o){
				return true;
			}
			
			public void dropped(PersistentObject o, DropTargetEvent e){
				Point pt = Display.getCurrent().map(null, ProportionalSheet.this, e.x, e.y);
				TimeTool tt = setTerminTo(pt.x, pt.y);
				TerminLabel tl = (TerminLabel) e.widget;
				Termin t = tl.getTermin();
				if (t != null) {
					t.setStartTime(tt);
					t.setBereich(Activator.getDefault().getActResource());
					tl.refresh();
					refresh();
				}
			}
		});
	}
	
	private boolean isBetween(int x, double lower, double upper){
		int y = (int) Math.round(lower);
		int z = (int) Math.round(upper);
		if ((x >= y) && (x <= z)) {
			return true;
		}
		return false;
	}
	
	public MenuManager getContextMenuManager(){
		return contextMenuManager;
	}
	
	public void clear(){<commit_msg>[3887] Drag&Drop for agenda parallel view<commit_after>/*******************************************************************************
 * Copyright (c) 2009, <NAME> and Elexis
 * 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
 *
 * Sponsoring:
 * 	 mediX Notfallpaxis, diepraxen Stauffacher AG, Zürich
 * 
 * Contributors:
 *    <NAME> - initial implementation
 *    
 *******************************************************************************/

package ch.elexis.agenda.ui;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;

import ch.elexis.actions.Activator;
import ch.elexis.agenda.data.Termin;
import ch.elexis.agenda.preferences.PreferenceConstants;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.ui.util.PersistentObjectDropTarget;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.data.PersistentObject;
import ch.elexis.data.Query;
import ch.elexis.dialogs.TerminDialog;
import ch.rgw.tools.StringTool;
import ch.rgw.tools.TimeTool;

public class ProportionalSheet extends Composite implements IAgendaLayout {
	static final int LEFT_OFFSET_DEFAULT = 20;
	static final int PADDING_DEFAULT = 5;
	
	private int left_offset, padding;
	private AgendaParallel view;
	private MenuManager contextMenuManager;
	private List<TerminLabel> tlabels;
	private double ppm;
	private int sheetHeight;
	private String[] resources;
	private int textWidth;
	private double sheetWidth;
	private double widthPerColumn;
	
	private TimeTool setTerminTo(int x, int y){
		String resource = ""; //$NON-NLS-1$
		for (int i = 0; i < resources.length; i++) {
			double lower = left_offset + i * (widthPerColumn + padding);
			double upper = lower + widthPerColumn;
			if (isBetween(x, lower, upper)) {
				resource = resources[i];
				break;
			}
		}
		String startOfDayTimeInMinutes =
			CoreHub.globalCfg.get(PreferenceConstants.AG_DAY_PRESENTATION_STARTS_AT, "0000");
		int dayStartHour = Integer.parseInt(startOfDayTimeInMinutes.substring(0, 2));
		
		int minute = (int) Math.round(y / ppm);
		TimeTool tt = new TimeTool(Activator.getDefault().getActDate());
		int hour = minute / 60;
		minute = minute - (60 * hour);
		int raster = 15;
		minute = ((minute + (raster >> 1)) / raster) * raster;
		tt.set(TimeTool.AM_PM, TimeTool.AM);
		tt.set(TimeTool.HOUR, (dayStartHour + hour));
		tt.set(TimeTool.MINUTE, minute);
		if (resource.length() > 0) {
			Activator.getDefault().setActResource(resource);
		}
		return tt;
	}
	
	public ProportionalSheet(Composite parent, AgendaParallel v){
		super(parent, SWT.NO_BACKGROUND);
		view = v;
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e){
				layout();
				recalc();
			}
		});
		addPaintListener(new TimePainter());
		addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseDoubleClick(MouseEvent e){
				String startOfDayTimeInMinutes =
					CoreHub.globalCfg.get(PreferenceConstants.AG_DAY_PRESENTATION_STARTS_AT, "0000");
				int sodtHours = Integer.parseInt(startOfDayTimeInMinutes.substring(0, 2));
				int sodtMinutes = Integer.parseInt(startOfDayTimeInMinutes.substring(2));

				TimeTool tt = setTerminTo(e.x, e.y);
				tt.addHours(sodtHours);
				tt.addMinutes(sodtMinutes);
				TerminDialog dlg = new TerminDialog(null);
				dlg.create();
				dlg.setTime(tt);
				if (dlg.open() == Dialog.OK) {
					
					refresh();
				}
			}
			
		});
		
		// setBackground(Desk.getColor(Desk.COL_GREEN));
		left_offset = LEFT_OFFSET_DEFAULT;
		padding = PADDING_DEFAULT;
		new PersistentObjectDropTarget(this, new PersistentObjectDropTarget.IReceiver() {
			
			public boolean accept(PersistentObject o){
				return true;
			}
			
			public void dropped(PersistentObject o, DropTargetEvent e){
				Point pt = Display.getCurrent().map(null, ProportionalSheet.this, e.x, e.y);
				TimeTool tt = setTerminTo(pt.x, pt.y);
				if (o instanceof Termin) {
					Termin t = (Termin) o;
					t.setStartTime(tt);
					t.setBereich(Activator.getDefault().getActResource());
					refresh();
				}
			}
		});
	}
	
	private boolean isBetween(int x, double lower, double upper){
		int y = (int) Math.round(lower);
		int z = (int) Math.round(upper);
		if ((x >= y) && (x <= z)) {
			return true;
		}
		return false;
	}
	
	public MenuManager getContextMenuManager(){
		return contextMenuManager;
	}
	
	public void clear(){
{'fraction_non_alphanumeric': 0.1444885318881254, 'fraction_numerical': 0.006387302816219878, 'mean_word_length': 4.351631279129984, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '3871341', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3346, 'n_words': 736}