/*******************************************************************************
 * Copyright (c) 2016 Ericsson
 *
 * 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
 *******************************************************************************/

package org.eclipse.cdt.example.framespy;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;

public class FrameSpyView extends ViewPart {

	private static final String TOGGLE_STATE_PREF_KEY = "toggle.state";
	private MenuManager fMenuManager;
	private Label fToggledStateLbl;
	private Job fPollingJob;

	public FrameSpyView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		fMenuManager = new MenuManager();
		Menu menu = fMenuManager.createContextMenu(composite);
		composite.setMenu(menu);
		getViewSite().registerContextMenu(fMenuManager, null);
		
		fToggledStateLbl = new Label(composite, SWT.NONE);
		// Display the new state to the user
		boolean toggledState = getToggledState();
		fToggledStateLbl.setText(Boolean.toString(toggledState));
		// Create the polling job if the spy is enabled
		if (toggledState) {
			startPollingJob();
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void dispose() {
		super.dispose();
		fMenuManager.dispose();
	}

	public boolean getToggledState() {
		IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
		String togglePrefValue = preferences.get(TOGGLE_STATE_PREF_KEY, Boolean.FALSE.toString());
		return Boolean.parseBoolean(togglePrefValue);
	}

	public void setToggledState(boolean newState) {
		boolean oldState = getToggledState();
		if (oldState != newState) {
			// Display the new state to the user
			fToggledStateLbl.setText(Boolean.toString(newState));

			// Save the toggle state in a preference so that it's remembered
			// next time the view is opened
			IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
			preferences.put(TOGGLE_STATE_PREF_KEY, Boolean.toString(newState));
						
			// Create the polling job if the spy is enabled
			if (newState) {
				startPollingJob();
			} else {
				cancelPollingJob();
			}
		}

	}

	private void startPollingJob() {
		fPollingJob = new Job("Frame Spy Polling Job") {
			int counter = 0;
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// Ignored
				}

				if (monitor.isCanceled()) {
					Display.getDefault().syncExec(new Runnable() {
						@Override
						public void run() {
							setToggledState(false); // TODO this set state to false when it could have already been put back to true
						}
					});
					// Stop here to cancel the repeating job
					return Status.OK_STATUS;
				}

				doWork();
				
				schedule();

				return Status.OK_STATUS;
			}
			
			private void doWork() {
				Display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {
						fToggledStateLbl.setText(Integer.toString(counter));
					}
				});
				counter++;
			}
		};
		fPollingJob.schedule();
	}

	private void cancelPollingJob() {
		if (fPollingJob != null) {
			fPollingJob.cancel();
		}
	}
}

@@ -850,16 +850,98 @@
 wt.SWT;%0A
+import org.eclipse.swt.custom.StyledText;%0Aimport org.eclipse.swt.layout.GridData;%0A
 import o
@@ -1060,46 +1060,8 @@
 ay;%0A
-import org.eclipse.swt.widgets.Label;%0A
 impo
@@ -1294,30 +1294,27 @@
 ate 
-Label fToggledStateLbl
+StyledText fLogText
 ;%0A%09p
@@ -1367,50 +1367,8 @@
 ) %7B%0A
-%09%09// TODO Auto-generated constructor stub%0A
 %09%7D%0A%0A
@@ -1527,16 +1527,184 @@
 out());%0A
+%0A%09%09fLogText = new StyledText(composite, SWT.BORDER %7C SWT.V_SCROLL %7C SWT.H_SCROLL %7C SWT.MULTI);%0A%09%09fLogText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));%0A%0A
 %09%09fMenuM
@@ -1780,31 +1780,29 @@
 enu(
-composite);%0A%09%09composite
+fLogText);%0A%09%09fLogText
 .set
@@ -1877,61 +1877,8 @@
 %0A%09%09%0A
-%09%09fToggledStateLbl = new Label(composite, SWT.NONE);%0A
 %09%09//
@@ -1959,31 +1959,23 @@
 e();%0A%09%09f
-ToggledStateLbl
+LogText
 .setText
@@ -2153,43 +2153,28 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A
+fLogText.setFocus();
 %0A%09%7D%0A
@@ -2689,39 +2689,31 @@
 he user%0A%09%09%09f
-ToggledStateLbl
+LogText
 .setText(Boo
@@ -3585,12 +3585,18 @@
  // 
-TODO
+small bug:
  thi
@@ -3969,23 +3969,15 @@
 %09%09%09f
-ToggledStateLbl
+LogText
 .set

Improve log buffer in view