/*******************************************************************************
 * Copyright (c) 2013 OPCoach.
 * 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:
 *     OPCoach - initial API and implementation
 *******************************************************************************/
package org.eclipse.e4.internal.tools.bundles.spy;

import javax.inject.Inject;

import org.eclipse.e4.tools.bundles.spy.BundleSpyPart;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

/**
 * The column Label and content Provider used to display information in context
 * data TreeViewer. Two instances for label provider are created : one for key,
 * one for values
 * 
 * @see ContextDataPart
 */
public class BundleDataProvider extends ColumnLabelProvider
{

	public static final int COL_NAME = 0;
	public static final int COL_VERSION = 1;
	public static final int COL_STATE = 2;

	private static final Color COLOR_IF_FOUND = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);


	@Inject
	private ImageRegistry imgReg;

	// Only one bundle filter, injected for all columns.
	@Inject
	private BundleDataFilter bundleFilter;

	// The column number this provider manages.
	private int column;

	@Inject
	public BundleDataProvider()
	{
		super();
		initializeImageRegistry();
	}

	@Override
	public String getText(Object element)
	{
		// Received element is a bundle...Text depends on column.
		Bundle b = (Bundle) element;
		String result = getText(b, column);
		return (result == null) ? super.getText(element) : result;

	}

	public static String getText(Bundle b, int col)
	{
		switch (col)
		{
		case COL_NAME:
			return b.getSymbolicName();
		case COL_VERSION:
			return b.getVersion().toString();
		case COL_STATE: return ""; // No text for state (see tooltip)
		
		}
		return null;
	}

	@Override
	public Color getForeground(Object element)
	{
		// Return magenta color if the value could not be yet computed (for
		// context functions)
		String s = getText(element);

		// Return blue color if the string matches the search
		return ((bundleFilter != null) && (bundleFilter.matchText(s))) ? COLOR_IF_FOUND : null;
	}

	@Override
	public Image getImage(Object element)
	{
		Bundle b = (Bundle) element;
		if (column == COL_STATE)
		{
			if (b.getState() == Bundle.ACTIVE)
				return imgReg.get(BundleSpyPart.ICON_STATE_ACTIVE);
			else if (b.getState() == Bundle.RESOLVED)
				return imgReg.get(BundleSpyPart.ICON_STATE_RESOLVED);

		}
		return null;

	}

	@Override
	public String getToolTipText(Object element)
	{
		Bundle b = (Bundle) element;
		
		if (b.getState() == Bundle.ACTIVE)
			return "This bundle is Active";
		else if (b.getState() == Bundle.RESOLVED)
			return "This bundle is Resolved";
		
		return "This bundle is in state : " + b.getState();

	}

	@Override
	public Image getToolTipImage(Object object)
	{
		return getImage(object);
	}

	@Override
	public int getToolTipStyle(Object object)
	{
		return SWT.SHADOW_OUT;
	}

	private void initializeImageRegistry()
	{
		if (imgReg != null)
			return;

		Bundle b = FrameworkUtil.getBundle(this.getClass());
		imgReg = new ImageRegistry();

	}

	public void setColumn(int col)
	{
		column = col;

	}

}

@@ -1460,17 +1460,16 @@
 BLUE);%0A%0A
-%0A
 %09@Inject
@@ -2198,17 +2198,20 @@
 L_STATE:
- 
+%0A%09%09%09
 return %22
@@ -2248,18 +2248,16 @@
 ooltip)%0A
-%09%09
 %0A%09%09%7D%0A%09%09r
@@ -3038,15 +3038,17 @@
 nt;%0A
-%09%09
 %0A%09%09
-if
+switch
  (b.
@@ -3049,35 +3049,44 @@
 ch (b.getState()
- ==
+)%0A%09%09%7B%0A%09%09case
  Bundle.ACTIVE)%0A
@@ -3079,25 +3079,25 @@
 undle.ACTIVE
-)
+:
 %0A%09%09%09return %22
@@ -3126,32 +3126,75 @@
 ;%0A%09%09
-else if (b.getState() ==
+case Bundle.INSTALLED:%0A%09%09%09return %22This bundle is Installed%22;%0A%09%09case
  Bun
@@ -3201,25 +3201,25 @@
 dle.RESOLVED
-)
+:
 %0A%09%09%09return %22
@@ -3246,16 +3246,208 @@
 ved%22;%0A%09%09
+case Bundle.STARTING:%0A%09%09%09return %22This bundle is Starting%22;%0A%09%09case Bundle.STOPPING:%0A%09%09%09return %22This bundle is Stopping%22;%0A%09%09case Bundle.UNINSTALLED:%0A%09%09%09return %22This bundle is Uninstalled%22;%0A%0A%09%09%7D%0A
 %0A%09%09retur

Add tooltip for other states