ejp - reusing editors.
diff --git a/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/ISelectionActivatedListener.java b/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/ISelectionActivatedListener.java
new file mode 100644
index 0000000..8c48390
--- /dev/null
+++ b/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/ISelectionActivatedListener.java
@@ -0,0 +1,18 @@
+package org.eclipse.jface.viewers;

+

+/*

+ * (c) Copyright IBM Corp. 2000, 2001.

+ * All Rights Reserved.

+ */

+

+/**

+ * A listener which is notified of double-click events on viewers.

+ */

+public interface ISelectionActivatedListener {

+/**

+ * Notifies of a double click.

+ *

+ * @param event event object describing the double-click

+ */

+public void selectionActivated(SelectionActivatedEvent event);

+}

diff --git a/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/SelectionActivatedEvent.java b/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/SelectionActivatedEvent.java
new file mode 100644
index 0000000..e112bdf
--- /dev/null
+++ b/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/SelectionActivatedEvent.java
@@ -0,0 +1,49 @@
+package org.eclipse.jface.viewers;

+

+/*

+ * (c) Copyright IBM Corp. 2000, 2001.

+ * All Rights Reserved.

+ */

+import org.eclipse.jface.util.Assert;

+import java.util.EventObject;

+

+/**

+ * Event object describing a selection activated. The source of these

+ * events is a viewer.

+ *

+ * @see ISelectionActivatedListener

+ */

+public class SelectionActivatedEvent extends EventObject {

+

+	/**

+	 * The selection.

+	 */

+	protected ISelection selection;

+/**

+ * Creates a new event for the given source and selection.

+ *

+ * @param source the viewer

+ * @param selection the selection

+ */

+public SelectionActivatedEvent(Viewer source, ISelection selection) {

+	super(source);

+	Assert.isNotNull(selection);

+	this.selection = selection;

+}

+/**

+ * Returns the selection.

+ *

+ * @return the selection

+ */

+public ISelection getSelection() {

+	return selection;

+}

+/**

+ * Returns the viewer that is the source of this event.

+ *

+ * @return the originating viewer

+ */

+public Viewer getViewer() {

+	return (Viewer) getSource();

+}

+}

diff --git a/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/StructuredViewer.java b/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/StructuredViewer.java
index cb4147b..9ab8c67 100644
--- a/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/StructuredViewer.java
+++ b/bundles/org.eclipse.ui/Eclipse JFace/org/eclipse/jface/viewers/StructuredViewer.java
@@ -67,12 +67,38 @@
 	 * @see #fireDoubleClick

 	 */

 	private ListenerList doubleClickListeners = new ListenerList(1);

+

+	/**

+	 * List of selection-activated state listeners (element type: <code>ISelectionActivatedListener</code>).

+	 * @see #fireSelectionActivated

+	 */

+	private ListenerList selectionActivatedListeners = new ListenerList(1);

+	

+	/**

+	 * If true, selection is activated on single-click

+	 * otherwise selection is activated on double-click

+	 */

+	private static boolean activateSelectionOnClick = false;

+	

 /**

  * Creates a structured element viewer. The viewer has no input, 

  * no content provider, a default label provider, no sorter, and no filters.

  */

 protected StructuredViewer() {

 }

+

+public static void setActivateSelectionOnClick(boolean singleClick) {

+	activateSelectionOnClick = singleClick;

+}

+/**

+ * Adds a listener for selection activated in this viewer.

+ * Has no effect if an identical listener is already registered.

+ *

+ * @param listener a selection activated listener

+ */

+public void addSelectionActivatedListener(ISelectionActivatedListener listener) {

+	selectionActivatedListeners.add(listener);

+}

 /**

  * Adds a listener for double-clicks in this viewer.

  * Has no effect if an identical listener is already registered.

@@ -270,6 +296,20 @@
 	}

 }

 /**

+ * Notifies any selection-activated listeners that a selection has been activated.

+ * Only listeners registered at the time this method is called are notified.

+ *

+ * @param event a double-click event

+ *

+ * @see ISelectionActivatedListener#doubleClick

+ */

+protected void fireSelectionActivated(SelectionActivatedEvent event) {

+	Object[] listeners = selectionActivatedListeners.getListeners();

+	for (int i = 0; i < listeners.length; ++i) {

+		((ISelectionActivatedListener) listeners[i]).selectionActivated(event);

+	}

+}

+/**

  * Returns the filtered array of children of the given element.

  * The resulting array must not be modified,

  * as it may come directly from the model's internal state.

@@ -411,6 +451,8 @@
 		ISelection selection = getSelection();

 		updateSelection(selection);

 		fireDoubleClick(new DoubleClickEvent(this, selection));

+		if(!activateSelectionOnClick)

+			fireSelectionActivated(new SelectionActivatedEvent(this, selection));

 	}

 }

 /**

@@ -461,7 +503,10 @@
 	Control control = getControl();

 	if (control != null && !control.isDisposed()) {

 		updateSelection(getSelection());

+		if(activateSelectionOnClick)

+			fireSelectionActivated(new SelectionActivatedEvent(this, getSelection()));

 	}

+	

 }

 /**

  * Returns whether this viewer has any filters.

diff --git a/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java b/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java
index f9186bd..0fccfeb 100644
--- a/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java
+++ b/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java
@@ -180,6 +180,11 @@
 			handleSelectionChanged(event);

 		}

 	});

+	viewer.addSelectionActivatedListener(new ISelectionActivatedListener() {

+		public void selectionActivated(SelectionActivatedEvent event) {

+			handleSelectionActivated(event);

+		}

+	});

 	viewer.addDoubleClickListener(new IDoubleClickListener() {

 		public void doubleClick(DoubleClickEvent event) {

 			handleDoubleClick(event);

@@ -476,16 +481,28 @@
 void handleDoubleClick(DoubleClickEvent event) {

 	IStructuredSelection s = (IStructuredSelection)event.getSelection();

 	Object element = s.getFirstElement();

-	if (element instanceof IFile) {

-		runOpenFileAction(s);

-	}

-	else {

+//	if (element instanceof IFile) {

+//		runOpenFileAction(s);

+//	}

+//	else {

+	if (!(element instanceof IFile)) {

 		// 1GBZIA0: ITPUI:WIN2000 - Double-clicking in navigator should expand/collapse containers

 		if (viewer.isExpandable(element)) {

 			viewer.setExpandedState(element, !viewer.getExpandedState(element));

 		}

 	}

 }

+

+/**

+ * Handles selection activated events in viewer.

+ * Opens editor if file double-clicked.

+ */

+void handleSelectionActivated(SelectionActivatedEvent event) {

+	IStructuredSelection s = (IStructuredSelection)event.getSelection();

+	Object element = s.getFirstElement();

+	if (element instanceof IFile)

+		runOpenFileAction(s);

+}

 /**

  */

 void runOpenFileAction(IStructuredSelection s) {

@@ -510,8 +527,7 @@
 	updateStatusLine(sel);

 	goIntoAction.update();

 	updateGlobalActions(sel);

-	if(!linkToEditor(sel))

-		runOpenFileAction(sel);

+	linkToEditor(sel);

 }

 /* (non-Javadoc)

  * Method declared on IViewPart.

diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IWorkbenchPreferenceConstants.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IWorkbenchPreferenceConstants.java
index fcdba8f..32a1aa7 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IWorkbenchPreferenceConstants.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IWorkbenchPreferenceConstants.java
@@ -10,6 +10,16 @@
  */

 public interface IWorkbenchPreferenceConstants {

 	

+	/**

+	 * A named preference for how a selection is activated.

+	 * If true, the selection is activated on single click

+	 * otherwise it is activated on double click.

+	 * <p>

+	 * Value is of type <code>boolean</code>.

+	 * </p>

+	 */

+	public static final String ACTIVATE_SELECTION_ON_CLICK = "ACTIVATE_SELECTION_ON_CLICK"; //$NON-NLS-1$

+ 

 	/*

 	 * A named preference for whether to show an editor when its

 	 * input file is selected in the Navigator (and vice versa).

diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java
index b48d4d0..eec9314 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java
@@ -21,6 +21,7 @@
 import org.eclipse.jface.dialogs.*;

 import org.eclipse.jface.window.Window;

 import org.eclipse.jface.operation.*;

+import org.eclipse.jface.preference.IPreferenceStore;

 import org.eclipse.swt.SWT;

 import org.eclipse.swt.custom.BusyIndicator;

 import org.eclipse.swt.widgets.Display;

@@ -294,9 +295,12 @@
  *

  */

 private IReusableEditor findReusableEditor(EditorDescriptor desc) {

-	//Must get global preference: "Reuse opened editors".

-	//if(!reuseEditors)

-	//	return null;

+	

+	IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore();

+	boolean reuseEditors = store.getBoolean(IPreferenceConstants.REUSE_EDITORS);

+	if(!reuseEditors)

+		return null;

+	

 	IEditorPart editors[] = getEditors();

 	IReusableEditor dirtyEditor = null;

 	IWorkbenchPart activePart = page.getActivePart();

@@ -326,7 +330,7 @@
 	if(dirtyEditor == null)

 		return null;

 	

-	//Must get global preference: "Open new Editor when dirty"

+	//Should we have a global preference "Open new Editor when dirty"?

 	//if(openNewWhenDirty)

 	//	return null;

 	MessageDialog dialog = new MessageDialog(

diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/IPreferenceConstants.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/IPreferenceConstants.java
index 18bb031..3fe4abc 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/IPreferenceConstants.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/IPreferenceConstants.java
@@ -29,5 +29,9 @@
 

 	//Do we show tabs up or down for editors

 	public static final String EDITOR_TAB_POSITION = "EDITOR_TAB_POSITION"; //$NON-NLS-1$

+	

+	// (boolean) If true a editor of the same type will be reused

+	// otherwise a new editor will be opened.

+	public static final String REUSE_EDITORS = "REUSE_EDITORS"; //$NON-NLS-1$

 

 }

diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/Workbench.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/Workbench.java
index 789ff23..4500c69 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/Workbench.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/Workbench.java
@@ -21,6 +21,8 @@
 import org.eclipse.jface.operation.IRunnableWithProgress;

 import org.eclipse.jface.resource.*;

 import org.eclipse.jface.preference.PreferenceManager;

+import org.eclipse.jface.viewers.StructuredViewer;

+import org.eclipse.jface.preference.IPreferenceStore;

 import org.eclipse.swt.graphics.*;

 import org.eclipse.swt.widgets.*;

 import org.eclipse.swt.*;

@@ -392,6 +394,10 @@
 	

 	openWindows();

 	openWelcomeDialog();

+

+	IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore();

+	StructuredViewer.setActivateSelectionOnClick(

+		store.getBoolean(IWorkbenchPreferenceConstants.ACTIVATE_SELECTION_ON_CLICK));

 	

 	isStarting = false;

 	return true;

diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java
index 3457742..801bc22 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java
@@ -300,6 +300,8 @@
 	store.setDefault(IPreferenceConstants.SAVE_ALL_BEFORE_BUILD, false);

 	store.setDefault(IPreferenceConstants.WELCOME_DIALOG, true);

 	store.setDefault(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR, true);

+	store.setDefault(IWorkbenchPreferenceConstants.ACTIVATE_SELECTION_ON_CLICK, false);

+	store.setDefault(IPreferenceConstants.REUSE_EDITORS, false);

 	store.setDefault(IPreferenceConstants.VIEW_TAB_POSITION, SWT.BOTTOM);

 	store.setDefault(IPreferenceConstants.EDITOR_TAB_POSITION, SWT.TOP);

 	store.setDefault(

diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/dialogs/WorkbenchPreferencePage.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/dialogs/WorkbenchPreferencePage.java
index ed8cd38..c303e67 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/dialogs/WorkbenchPreferencePage.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/dialogs/WorkbenchPreferencePage.java
@@ -10,6 +10,7 @@
 import org.eclipse.ui.internal.*;

 import org.eclipse.ui.part.*;

 import org.eclipse.jface.preference.*;

+import org.eclipse.jface.viewers.StructuredViewer;

 import org.eclipse.swt.*;

 import org.eclipse.swt.events.*;

 import org.eclipse.swt.layout.*;

@@ -22,6 +23,8 @@
 	private Button autoBuildButton;

 	private Button autoSaveAllButton;

 	private Button linkButton;

+	private Button activateSelectionOnClickButton;

+	private Button reuseEditorsButton; 

 

 	//Widgets for menu based perspective operation

 	private Button openInNewWindowButton;

@@ -112,6 +115,14 @@
 

 	linkButton = new Button(composite, SWT.CHECK);

 	linkButton.setText(WorkbenchMessages.getString("WorkbenchPreference.linkNavigator")); //$NON-NLS-1$

+	

+	activateSelectionOnClickButton = new Button(composite, SWT.CHECK);

+	activateSelectionOnClickButton.setText("Activate selection on single click");

+	//activateSelectionOnClickButton.setText(WorkbenchMessages.getString("WorkbenchPreference.activateSelectionOnClick")); //$NON-NLS-1$

+	

+	reuseEditorsButton = new Button(composite, SWT.CHECK);

+	reuseEditorsButton.setText("Reuse open editors");

+	//reuseEditorsIfSavedButton.setText(WorkbenchMessages.getString("WorkbenchPreference.reuseEditorsButton")); //$NON-NLS-1$

 

 	createSpace(composite);

 

@@ -128,6 +139,10 @@
 		store.getBoolean(IPreferenceConstants.SAVE_ALL_BEFORE_BUILD));

 	linkButton.setSelection(

 		store.getBoolean(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR));

+	activateSelectionOnClickButton.setSelection(

+		store.getBoolean(IWorkbenchPreferenceConstants.ACTIVATE_SELECTION_ON_CLICK));

+	reuseEditorsButton.setSelection(

+		store.getBoolean(IPreferenceConstants.REUSE_EDITORS));

 

 	return composite;

 }

@@ -362,7 +377,13 @@
 	linkButton.setSelection(

 		store.getDefaultBoolean(

 			IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR));

-

+	activateSelectionOnClickButton.setSelection(

+		store.getDefaultBoolean(

+			IWorkbenchPreferenceConstants.ACTIVATE_SELECTION_ON_CLICK));

+	reuseEditorsButton.setSelection(

+		store.getDefaultBoolean(

+			IPreferenceConstants.REUSE_EDITORS));

+ 

 	//Perspective preferences

 	String defaultPreference =

 		store.getDefaultString(IWorkbenchPreferenceConstants.OPEN_NEW_PERSPECTIVE);

@@ -440,6 +461,17 @@
 		IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR,

 		linkButton.getSelection());

 

+	// store the activate selection on click setting

+	store.setValue(

+		IWorkbenchPreferenceConstants.ACTIVATE_SELECTION_ON_CLICK,

+		activateSelectionOnClickButton.getSelection());

+	StructuredViewer.setActivateSelectionOnClick(activateSelectionOnClickButton.getSelection());

+

+	// store the reuse editors setting

+	store.setValue(

+		IPreferenceConstants.REUSE_EDITORS,

+		reuseEditorsButton.getSelection());

+		

 	// store the open in new window settings

 	store.setValue(

 		IWorkbenchPreferenceConstants.OPEN_NEW_PERSPECTIVE,