/*******************************************************************************
 * Copyright (c) 2006-2010, G. Weirich 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
 *
 * Contributors:
 *    G. Weirich - initial implementation
 *    
 *******************************************************************************/
package ch.elexis.core.ui.medication.views;

import javax.inject.Inject;
import javax.inject.Named;

import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.core.ui.util.CoreUiUtil;
import ch.elexis.core.ui.util.ListDisplaySelectionProvider;
import ch.elexis.core.ui.util.SWTHelper;

/**
 * Eine platzsparende View zur Anzeige der Dauermedikation
 * 
 * @author gerry
 * 
 */
public class DauerMediView extends ViewPart {
	public final static String ID = "ch.elexis.dauermedikationview"; //$NON-NLS-1$
	private IAction toClipBoardAction;
	FixMediDisplay dmd;
	
	public DauerMediView(){
		
	}
	
	@Override
	public void createPartControl(Composite parent){
		parent.setLayout(new GridLayout());
		dmd = new FixMediDisplay(parent, getViewSite());
		dmd.setLayoutData(SWTHelper.getFillGridData(1, true, 1, true));
		ListDisplaySelectionProvider selDisplay = new ListDisplaySelectionProvider(dmd);
		getSite().registerContextMenu(FixMediDisplay.ID, dmd.getMenuManager(), selDisplay);
		getSite().setSelectionProvider(selDisplay);
		
		makeActions();
		getViewSite().getActionBars().getToolBarManager().add(toClipBoardAction);
	}
	
	public void dispose(){
		dmd.dispose();
	}
	
	@Override
	public void setFocus(){
		// TODO Auto-generated method stub
		
	}
	
	private void makeActions(){
		toClipBoardAction = new Action(Messages.DauerMediView_copy) { //$NON-NLS-1$
				{
					setToolTipText(Messages.DauerMediView_copyToClipboard); //$NON-NLS-1$
					setImageDescriptor(Images.IMG_CLIPBOARD.getImageDescriptor());
				}
				
				@Override
				public void run(){
					dmd.toClipBoard(true);
				}
				
			};
	}
	
	@Optional
	@Inject
	public void setFixLayout(MPart part, @Named(Preferences.USR_FIX_LAYOUT) boolean currentState){
		CoreUiUtil.updateFixLayout(part, currentState);
	}
}

@@ -977,24 +977,80 @@
 references;%0A
+import ch.elexis.core.ui.events.RefreshingPartListener;%0A
 import ch.el
@@ -1218,16 +1218,61 @@
 THelper;
+%0Aimport ch.elexis.core.ui.views.IRefreshable;
 %0A%0A/**%0A *
@@ -1401,16 +1401,40 @@
 iewPart 
+implements IRefreshable 
 %7B%0A%09publi
@@ -1566,16 +1566,102 @@
  dmd;%0A%09%0A
+%09private RefreshingPartListener udpateOnVisible = new RefreshingPartListener(this);%0A%09%0A
 %09public 
@@ -2217,35 +2217,156 @@
 );%0A%09
-%7D%0A%09%0A%09public void dispose()%7B
+%09%0A%09%09getSite().getPage().addPartListener(udpateOnVisible);%0A%09%7D%0A%09%0A%09public void dispose()%7B%0A%09%09getSite().getPage().removePartListener(udpateOnVisible);%0A%09%09
 %0A%09%09d
@@ -2426,45 +2426,23 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A%09%09
+dmd.setFocus();
 %0A%09%7D%0A
@@ -2966,10 +2966,113 @@
 te);%0A%09%7D%0A
+%09%0A%09@Override%0A%09public void refresh()%7B%0A%09%09if (dmd != null && !dmd.isDisposed()) %7B%0A%09%09%09dmd.reload();%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

add refreshing part listener to DauerMediView