Skip to main content
summaryrefslogtreecommitdiffstats
blob: ddeee67056cf7da8a54a9f52cf9f71dc9fc9df1e (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. 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:
 * - Mickael Istria (Red Hat Inc.)
 *******************************************************************************/
package org.eclipse.ui.internal.genericeditor.hover;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IInformationControlCreatorExtension;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextHoverExtension;
import org.eclipse.swt.widgets.Shell;

public class CompositeInformationControlCreator
		implements IInformationControlCreator, IInformationControlCreatorExtension {

	private final LinkedHashMap<ITextHover, IInformationControlCreator> creators;

	public CompositeInformationControlCreator(List<ITextHover> hovers) {
		this.creators = new LinkedHashMap<>();
		for (ITextHover hover : hovers) {
			creators.put(hover, getInformationControlCreator(hover));
		}
	}

	public CompositeInformationControlCreator(LinkedHashMap<ITextHover, IInformationControlCreator> creators) {
		this.creators = creators;
	}

	private static IInformationControlCreator getInformationControlCreator(ITextHover hover) {
		IInformationControlCreator controlCreator = null;
		if (hover instanceof ITextHoverExtension) {
			controlCreator = ((ITextHoverExtension)hover).getHoverControlCreator();
		}
		if (controlCreator == null) {
			controlCreator = new AbstractReusableInformationControlCreator() {
				@Override
				protected IInformationControl doCreateInformationControl(Shell parent) {
					return new DefaultInformationControl(parent, true);
				}
			};
		}
		return controlCreator;
	}


	@Override
	public boolean canReuse(IInformationControl control) {
		if (control.getClass() != CompositeInformationControl.class) {
			return false;
		}
		CompositeInformationControl other = (CompositeInformationControl)control;
		if (!other.creators.equals(this.creators)) {
			return false;
		}
		Iterator<Entry<ITextHover, IInformationControlCreator>> thisIterator = this.creators.entrySet().iterator();
		Iterator<Entry<ITextHover, IInformationControl>> otherIterator = other.controls.entrySet().iterator();
		do {
			Entry<ITextHover, IInformationControlCreator> thisEntry = thisIterator.next();
			Entry<ITextHover, IInformationControl> otherEntry = otherIterator.next();
			if (!thisEntry.getKey().equals(otherEntry.getKey())) {
				return false;
			}
			if (!(thisEntry.getValue() instanceof IInformationControlCreatorExtension)) {
				return false;
			}
			if (!((IInformationControlCreatorExtension)thisEntry.getValue()).canReuse(otherEntry.getValue())) {
				return false;
			}
		} while (thisIterator.hasNext());
		return true;
	}

	@Override
	public boolean canReplace(IInformationControlCreator creator) {
		if (creator.getClass() != this.getClass()) {
			return false;
		}
		CompositeInformationControlCreator other = (CompositeInformationControlCreator)creator;
		if (other.creators.size() != this.creators.size()) {
			return false;
		}
		Iterator<Entry<ITextHover, IInformationControlCreator>> thisIterator = this.creators.entrySet().iterator();
		Iterator<Entry<ITextHover, IInformationControlCreator>> otherIterator = other.creators.entrySet().iterator();
		do {
			Entry<ITextHover, IInformationControlCreator> thisEntry = thisIterator.next();
			Entry<ITextHover, IInformationControlCreator> otherEntry = otherIterator.next();
			if (!thisEntry.getKey().equals(otherEntry.getKey())) {
				return false;
			}
			if (!(thisEntry.getValue() instanceof IInformationControlCreatorExtension)) {
				return false;
			}
			if (!((IInformationControlCreatorExtension)thisEntry.getValue()).canReplace(otherEntry.getValue())) {
				return false;
			}
		} while (thisIterator.hasNext());
		return true;
	}
	

	@Override
	public IInformationControl createInformationControl(Shell parent) {
		return new CompositeInformationControl(parent, this.creators);
	}

}

Back to the top