Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97d8a578bf83fd289fa3a5f85614a14a48c07a14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.sourcelookup;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.IStatusHandler;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.sourcelookup.CommonSourceNotFoundEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

/**
 * Status handler to prompt for duplicate source element resolution.
 *
 * @since 3.0
 */
public class ResolveDuplicatesHandler implements IStatusHandler {
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.IStatusHandler#handleStatus(org.eclipse.core.runtime.IStatus, java.lang.Object)
	 */
	@Override
	public Object handleStatus(IStatus status, Object source) throws CoreException {
		Object[] args = (Object[])source;
		List<?> sources = (List<?>) args[1];
		return resolveSourceElement(sources);
	}

	public Object resolveSourceElement(List<?> sources) {
		Object file = null;
		sources = removeSourceNotFoundEditors(sources);
		if(sources.size() == 1) {
			return sources.get(0);
		} else if(sources.size() == 0) {
			return null;
		}
		ElementListSelectionDialog dialog = new ElementListSelectionDialog(DebugUIPlugin.getShell(), new SourceElementLabelProvider());
		dialog.setMultipleSelection(false);
		dialog.setTitle(SourceLookupUIMessages.ResolveDuplicatesHandler_0);
		dialog.setMessage(SourceLookupUIMessages.ResolveDuplicatesHandler_1);
		dialog.setElements(sources.toArray());
		dialog.open();
		if(dialog.getReturnCode() == Window.OK) {
			file = dialog.getFirstResult();
		}
		return file;
	}

	/**
	 * Remove extra source not found editors, if any.
	 * If multiple source not found editors and no "real" source inputs,
	 * return the first source not found editor.
	 * @param sources the list to be filtered
	 * @return the filtered list, may be empty
	 */
	private List<Object> removeSourceNotFoundEditors(List<?> sources) {
		List<Object> filteredList = new ArrayList<>();
		for (Object obj : sources) {
			if (!(obj instanceof CommonSourceNotFoundEditor)) {
				filteredList.add(obj);
			}
		}
		if (filteredList.isEmpty() && sources.get(0) != null) {
			filteredList.add(sources.get(0));
		}
		return filteredList;
	}
}

Back to the top