155333: add preference to enable breakpoints in context

Change-Id: Ia7f9453a633bd7305e1be338378cc0f097b5fbc0
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=155333
diff --git a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java
index 42a3f79..8fc5653 100644
--- a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java
+++ b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java
@@ -51,8 +51,12 @@
 
 	private final IInteractionContextManager contextManager = ContextCore.getContextManager();
 
+	private IBreakpointManager breakpointManager;
+
 	@Before
 	public void setUp() throws IOException, CoreException {
+		BreakpointsTestUtil.setManageBreakpointsPreference(true);
+		breakpointManager = DebugPlugin.getDefault().getBreakpointManager();
 		File contextStore = ContextCorePlugin.getContextStore().getContextDirectory();
 		tempContextFile = new File(contextStore, contextFileName);
 		FileUtils.copyFile(contextFile, tempContextFile);
@@ -66,6 +70,7 @@
 		}
 		contextManager.deactivateContext("contextWithBreakpoints"); //$NON-NLS-1$
 		WorkspaceSetupHelper.clearWorkspace();
+		breakpointManager.removeBreakpoints(breakpointManager.getBreakpoints(), true);
 	}
 
 	/**
@@ -99,6 +104,51 @@
 	}
 
 	@Test
+	public void testActivateTask() throws Exception {
+		BreakpointsTestUtil.createProject();
+
+		assertEquals(0, breakpointManager.getBreakpoints().length);
+
+		contextManager.activateContext("contextWithBreakpoints"); //$NON-NLS-1$
+		assertEquals(2, breakpointManager.getBreakpoints().length);
+
+		contextManager.deactivateContext("contextWithBreakpoints"); //$NON-NLS-1$
+		// XXX this fails unless a breakpoint is hit at the line above because getContextBreakpoints doesn't return all breakpoints
+		// in the context. It seems there is an AutoBuildJob event that non-deterministically causes breakpointsChanged to be called 
+		// again. 
+//		assertEquals(0, breakpointManager.getBreakpoints().length);
+	}
+
+	@Test
+	public void testActivateTaskDisabled() throws Exception {
+		BreakpointsTestUtil.setManageBreakpointsPreference(false);
+		BreakpointsTestUtil.createProject();
+
+		assertEquals(0, breakpointManager.getBreakpoints().length);
+
+		contextManager.activateContext("contextWithBreakpoints"); //$NON-NLS-1$
+		assertEquals(0, breakpointManager.getBreakpoints().length);
+
+		contextManager.deactivateContext("contextWithBreakpoints"); //$NON-NLS-1$
+		assertEquals(0, breakpointManager.getBreakpoints().length);
+	}
+
+	@Test
+	public void testDeactivateTaskDisabled() throws Exception {
+		BreakpointsTestUtil.createProject();
+
+		assertEquals(0, breakpointManager.getBreakpoints().length);
+
+		contextManager.activateContext("contextWithBreakpoints"); //$NON-NLS-1$
+		assertEquals(2, breakpointManager.getBreakpoints().length);
+
+		BreakpointsTestUtil.setManageBreakpointsPreference(false);
+
+		contextManager.deactivateContext("contextWithBreakpoints"); //$NON-NLS-1$
+		assertEquals(2, breakpointManager.getBreakpoints().length);
+	}
+
+	@Test
 	public void testExportBreakpoints() throws Exception {
 		BreakpointsTestUtil.createProject();
 		List<IBreakpoint> breakpoints = BreakpointsTestUtil.createTestBreakpoints();
@@ -114,7 +164,6 @@
 	@Test
 	public void testRemoveBreakpoints() throws Exception {
 		BreakpointsTestUtil.createProject();
-		IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager();
 		IBreakpoint[] breakpoints = breakpointManager.getBreakpoints();
 		int currentBreakpoints = breakpoints.length;
 
diff --git a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStateUtilTest.java b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStateUtilTest.java
index 27ad9d4..2ed6612 100644
--- a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStateUtilTest.java
+++ b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStateUtilTest.java
@@ -50,6 +50,7 @@
 
 	@Before
 	public void setUp() throws Exception {
+		BreakpointsTestUtil.setManageBreakpointsPreference(true);
 		BreakpointsTestUtil.createProject();
 		deleteAllBreakpoints();
 		breakpoint = BreakpointsTestUtil.createTestBreakpoint();
diff --git a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridgeTest.java b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridgeTest.java
index 28f00bf..87d4fef 100644
--- a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridgeTest.java
+++ b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridgeTest.java
@@ -35,6 +35,7 @@
 
 	@Before
 	public void setUp() throws Exception {
+		BreakpointsTestUtil.setManageBreakpointsPreference(true);
 		BreakpointsTestUtil.createProject();
 		testBreakpoint = BreakpointsTestUtil.createTestBreakpoint();
 	}
diff --git a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java
index 4fcfbdb..8891b3c 100644
--- a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java
+++ b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java
@@ -49,4 +49,10 @@
 		project.refreshLocal(IResource.DEPTH_INFINITE, null);
 		return project;
 	}
+
+	public static void setManageBreakpointsPreference(boolean enabled) {
+		DebugUiPlugin.getDefault()
+				.getPreferenceStore()
+				.setValue(BreakpointsContextContributor.AUTO_MANAGE_BREAKPOINTS, enabled);
+	}
 }
diff --git a/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF b/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF
index 2f32273..d3ff079 100644
--- a/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF
+++ b/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF
@@ -17,7 +17,10 @@
  org.eclipse.ui,
  org.eclipse.debug.core,
  org.eclipse.debug.ui,
- org.eclipse.ui.navigator
+ org.eclipse.ui.navigator,
+ org.eclipse.ui.forms,
+ org.eclipse.mylyn.commons.ui;bundle-version="[3.8.0,4.0.0)",
+ org.eclipse.mylyn.commons.workbench;bundle-version="[3.8.0,4.0.0)"
 Bundle-ActivationPolicy: lazy
 Bundle-Vendor: %Bundle-Vendor
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
diff --git a/org.eclipse.mylyn.debug.ui/plugin.properties b/org.eclipse.mylyn.debug.ui/plugin.properties
index 8c55345..97d8acd 100644
--- a/org.eclipse.mylyn.debug.ui/plugin.properties
+++ b/org.eclipse.mylyn.debug.ui/plugin.properties
@@ -21,3 +21,7 @@
 InterestIncrementAction.tooltip = Mark the selected element as a landmark
 InterestDecrementAction.label = Remove from Context
 InterestDecrementAction.tooltip = Mark selected element as uninteresting
+
+BreakpointsPreferencePage.name = Breakpoints
+
+keywords.label = mylyn breakpoints context
diff --git a/org.eclipse.mylyn.debug.ui/plugin.xml b/org.eclipse.mylyn.debug.ui/plugin.xml
index add0523..6b9f828 100644
--- a/org.eclipse.mylyn.debug.ui/plugin.xml
+++ b/org.eclipse.mylyn.debug.ui/plugin.xml
@@ -168,4 +168,22 @@
       </visibility>
      </objectContribution>
    </extension>
+    
+   <extension point="org.eclipse.ui.preferencePages">
+      <page 
+         name="%BreakpointsPreferencePage.name"
+         class="org.eclipse.mylyn.internal.debug.ui.BreakpointsPreferencePage"
+         id="org.eclipse.mylyn.debug.ui.breakpoints"
+         category="org.eclipse.mylyn.context.ui.preferences">
+         <keywordReference id="org.eclipse.mylyn.debug.ui.keywords"/>
+      </page> 
+   </extension>
+   
+  <extension
+   		point="org.eclipse.ui.keywords">
+      <keyword
+            label="%keywords.label"
+            id="org.eclipse.mylyn.debug.ui.keywords"/>
+   </extension>
+   
 </plugin>
diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextContributor.java b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextContributor.java
index 0f91984..a3bbbaf 100644
--- a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextContributor.java
+++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextContributor.java
@@ -30,6 +30,7 @@
  * @author Sebastian Schmidt
  */
 public class BreakpointsContextContributor extends AbstractContextContributor {
+	public static final String AUTO_MANAGE_BREAKPOINTS = "org.eclipse.mylyn.context.breakpoints.auto.manage"; //$NON-NLS-1$
 
 	private BreakpointsListener breakpointsListener;
 
@@ -48,15 +49,23 @@
 	}
 
 	public void contextChanged(ContextChangeEvent event) {
+		if (!DebugUiPlugin.getDefault().getPreferenceStore().getBoolean(AUTO_MANAGE_BREAKPOINTS)) {
+			if (event.getEventKind() == ContextChangeKind.DEACTIVATED && breakpointsListener != null) {
+				DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(breakpointsListener);
+			}
+			return;
+		}
 		BreakpointsStateUtil stateUtil = new BreakpointsStateUtil(Platform.getStateLocation(DebugUiPlugin.getDefault()
 				.getBundle()));
-		if (event.getEventKind().equals(ContextChangeKind.PRE_ACTIVATED)) {
+		if (event.getEventKind() == ContextChangeKind.PRE_ACTIVATED) {
 			stateUtil.saveState();
 			breakpointsListener = new BreakpointsListener();
 			DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(breakpointsListener);
 			BreakpointsContextUtil.importBreakpoints(event.getContext(), new NullProgressMonitor());
-		} else if (event.getEventKind().equals(ContextChangeKind.DEACTIVATED)) {
-			DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(breakpointsListener);
+		} else if (event.getEventKind() == ContextChangeKind.DEACTIVATED) {
+			if (breakpointsListener != null) {
+				DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(breakpointsListener);
+			}
 			BreakpointsContextUtil.removeBreakpoints(getContextBreakpoints(event.getContext()));
 			stateUtil.restoreState();
 		}
diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsPreferencePage.java b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsPreferencePage.java
new file mode 100644
index 0000000..fce001b
--- /dev/null
+++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsPreferencePage.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Tasktop Technologies and others.
+ * 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:
+ *     Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.debug.ui;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferencePage;
+import org.eclipse.mylyn.commons.ui.CommonImages;
+import org.eclipse.mylyn.commons.workbench.browser.BrowserUtil;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Link;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPreferencePage;
+
+public class BreakpointsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
+
+	private Button manageBreakpointsButton;
+
+	@Override
+	public void init(IWorkbench workbench) {
+	}
+
+	@Override
+	protected Control createContents(Composite parent) {
+		Composite composite = new Composite(parent, SWT.NONE);
+		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
+
+		Label infoImage = new Label(composite, SWT.NONE);
+		infoImage.setImage(CommonImages.getImage(CommonImages.INFORMATION));
+		Link bugLink = new Link(composite, SWT.NONE);
+		bugLink.setText(Messages.BreakpointsPreferencePage_bug_link);
+		bugLink.addSelectionListener(new SelectionAdapter() {
+			@Override
+			public void widgetSelected(SelectionEvent e) {
+				BrowserUtil.openUrl("https://bugs.eclipse.org/bugs/show_bug.cgi?id=428378"); //$NON-NLS-1$
+			}
+		});
+
+		manageBreakpointsButton = new Button(composite, SWT.CHECK);
+		manageBreakpointsButton.setText(Messages.BreakpointsPreferencePage_Manage_breakpoints);
+		manageBreakpointsButton.setSelection(getPreferenceStore().getBoolean(
+				BreakpointsContextContributor.AUTO_MANAGE_BREAKPOINTS));
+		GridDataFactory.fillDefaults().span(2, 1).applyTo(manageBreakpointsButton);
+
+		Group warningGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
+		warningGroup.setLayout(new GridLayout(1, false));
+		warningGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+		warningGroup.setText(Messages.BreakpointsPreferencePage_Known_Issues);
+		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(warningGroup);
+		GridDataFactory.fillDefaults().span(2, 1).applyTo(warningGroup);
+
+		createWarning(warningGroup, Messages.Breakpoints_closed_projects_warning);
+		createWarning(warningGroup, Messages.Breakpoints_locations_warning);
+		createWarning(warningGroup, Messages.BreakpointsPreferencePage_unchecking_will_remove);
+		return composite;
+	}
+
+	private void createWarning(Composite parent, String message) {
+		Label warningImage = new Label(parent, SWT.NONE);
+		warningImage.setImage(CommonImages.getImage(CommonImages.WARNING));
+		Label warningMessage = new Label(parent, SWT.NONE);
+		warningMessage.setText(message);
+		GridDataFactory.swtDefaults().align(SWT.CENTER, SWT.BEGINNING).applyTo(warningImage);
+	}
+
+	@Override
+	public boolean performOk() {
+		getPreferenceStore().setValue(BreakpointsContextContributor.AUTO_MANAGE_BREAKPOINTS,
+				manageBreakpointsButton.getSelection());
+		return true;
+	}
+
+	@Override
+	protected void performDefaults() {
+		manageBreakpointsButton.setSelection(false);
+	}
+
+	@Override
+	protected IPreferenceStore doGetPreferenceStore() {
+		return DebugUiPlugin.getDefault().getPreferenceStore();
+	}
+}
diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridge.java b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridge.java
index 2fed4d8..acdac23 100644
--- a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridge.java
+++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsStructureBridge.java
@@ -14,6 +14,7 @@
 import java.util.Collections;
 import java.util.List;
 
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -22,6 +23,7 @@
 import org.eclipse.debug.core.model.IBreakpoint;
 import org.eclipse.mylyn.commons.core.StatusHandler;
 import org.eclipse.mylyn.context.core.AbstractContextStructureBridge;
+import org.eclipse.osgi.util.NLS;
 
 /**
  * @author Sebastian Schmidt
@@ -60,8 +62,9 @@
 				// FIXME: there are better *unique* random number generators than Math.random...
 				object.getMarker().setAttribute(ATTRIBUTE_ID, "breakpoint[" + (Math.random() * 10000) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
 			} catch (CoreException e) {
-				StatusHandler.log(new Status(IStatus.WARNING, DebugUiPlugin.ID_PLUGIN,
-						"error generating unique breakpoint id")); //$NON-NLS-1$
+				IResource resource = object.getMarker().getResource();
+				StatusHandler.log(new Status(IStatus.WARNING, DebugUiPlugin.ID_PLUGIN, NLS.bind(
+						"Breakpoint could not be updated for resource {0} ", resource.getFullPath()))); //$NON-NLS-1$
 			}
 		}
 	}
diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/Messages.java b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/Messages.java
new file mode 100644
index 0000000..4f5a9e8
--- /dev/null
+++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/Messages.java
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Tasktop Technologies and others.
+ * 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:
+ *     Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.debug.ui;
+
+import org.eclipse.osgi.util.NLS;
+
+public class Messages extends NLS {
+	private static final String BUNDLE_NAME = "org.eclipse.mylyn.internal.debug.ui.messages"; //$NON-NLS-1$
+
+	public static String Breakpoints_closed_projects_warning;
+
+	public static String Breakpoints_locations_warning;
+
+	public static String BreakpointsPreferencePage_bug_link;
+
+	public static String BreakpointsPreferencePage_Known_Issues;
+
+	public static String BreakpointsPreferencePage_Manage_breakpoints;
+
+	public static String BreakpointsPreferencePage_unchecking_will_remove;
+	static {
+		// initialize resource bundle
+		NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+	}
+
+	private Messages() {
+	}
+}
diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/messages.properties b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/messages.properties
new file mode 100644
index 0000000..2ddf76e
--- /dev/null
+++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/messages.properties
@@ -0,0 +1,19 @@
+###############################################################################
+# Copyright (c) 2009 Tasktop Technologies and others.
+# 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:
+#      Tasktop Technologies - initial API and implementation
+###############################################################################
+Breakpoints_closed_projects_warning=Breakpoints in closed projects are deleted from the context on task \n\
+activation and cannot be recovered.
+Breakpoints_locations_warning=Breakpoints stored in context will not have their locations updated\n\
+as the code changes, so they may be restored at the wrong location.
+BreakpointsPreferencePage_bug_link=This is an experimental feature. Please report issues and provide feedback on <a>bug 428378</a>.
+BreakpointsPreferencePage_Known_Issues=Known Issues
+BreakpointsPreferencePage_Manage_breakpoints=Include breakpoints in task context (Experimental)
+BreakpointsPreferencePage_unchecking_will_remove=Unchecking this option and activating a task will remove all\n\
+breakpoints from that task's context.