Skip to main content
summaryrefslogtreecommitdiffstats
blob: d77e190efa9e71268db8024eae4f5df58b5ecf73 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2001, 2016 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
 *     Obeo - Contribution to the EEF project
 *******************************************************************************/
package org.eclipse.eef.properties.ui.internal.registry;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.eef.properties.ui.api.AbstractEEFTabDescriptor;
import org.eclipse.eef.properties.ui.api.IEEFSectionDescriptor;
import org.eclipse.eef.properties.ui.api.IEEFTabDescriptor;
import org.eclipse.eef.properties.ui.api.IEEFTabDescriptorFilter;
import org.eclipse.eef.properties.ui.api.IEEFTabDescriptorProvider;
import org.eclipse.eef.properties.ui.api.IEEFTabbedPropertySheetPageContributor;
import org.eclipse.eef.properties.ui.internal.EEFTabbedPropertyViewPlugin;
import org.eclipse.eef.properties.ui.internal.Messages;
import org.eclipse.eef.properties.ui.internal.extension.IItemDescriptor;
import org.eclipse.eef.properties.ui.internal.extension.IItemRegistry;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Provides information about the tabbed property extension points. Each tabbed property registry is associated with a
 * unique contributor ID.
 *
 * @author Anthony Hunter
 * @author Stephane Begaudeau
 */
public class EEFTabbedPropertyRegistry {

	/**
	 * The sole instance of the registry.
	 */
	private static EEFTabbedPropertyRegistry instance = new EEFTabbedPropertyRegistry();

	/**
	 * The contributor.
	 */
	private static IEEFTabbedPropertySheetPageContributor contributor;

	/**
	 * Returns the sole instance of the registry.
	 *
	 * @param eefContributor
	 *            The contributor
	 *
	 * @return The sole instance of the registry
	 */
	public static EEFTabbedPropertyRegistry getDefault(IEEFTabbedPropertySheetPageContributor eefContributor) {
		contributor = eefContributor;
		return instance;
	}

	/**
	 * Returns all the descriptors defined by the tab descriptor providers.
	 *
	 * @param part
	 *            The current part
	 * @param input
	 *            The current selection
	 *
	 * @return The descriptors
	 */
	private List<IEEFTabDescriptor> getAllTabDescriptors(IWorkbenchPart part, ISelection input) {
		// Get the tab descriptors from the extension point
		Map<String, IEEFTabDescriptor> eefTabDescriptors = new LinkedHashMap<String, IEEFTabDescriptor>();
		IItemRegistry<IEEFTabDescriptorProvider> eefTabDescriptorProviderRegistry = EEFTabbedPropertyViewPlugin.getPlugin()
				.getEEFTabDescriptorProviderRegistry();
		for (IItemDescriptor<IEEFTabDescriptorProvider> itemDescriptor : eefTabDescriptorProviderRegistry.getItemDescriptors()) {
			IEEFTabDescriptorProvider eefTabDescriptorProvider = itemDescriptor.getItem();
			for (IEEFTabDescriptor eefTabDescriptor : eefTabDescriptorProvider.get(part, input, contributor)) {
				String eefTabDescriptorId = eefTabDescriptor.getId();
				if (!eefTabDescriptors.containsKey(eefTabDescriptorId) && filter(eefTabDescriptor)) {
					eefTabDescriptors.put(eefTabDescriptorId, eefTabDescriptor);
				}
			}
		}
		return new ArrayList<IEEFTabDescriptor>(eefTabDescriptors.values());
	}

	/**
	 * Returns if a tab descriptor must be filtered or not.
	 *
	 * @param eefTabDescriptor
	 *            The tab descriptor
	 * @return <code>true</code> if the tab descriptor should be used, <code>false</code> otherwise
	 */
	private boolean filter(IEEFTabDescriptor eefTabDescriptor) {
		IItemRegistry<IEEFTabDescriptorFilter> eefTabDescriptorFilterRegistry = EEFTabbedPropertyViewPlugin.getPlugin()
				.getEEFTabDescriptorFilterRegistry();
		for (IItemDescriptor<IEEFTabDescriptorFilter> itemDescriptor : eefTabDescriptorFilterRegistry.getItemDescriptors()) {
			IEEFTabDescriptorFilter eefTabDescriptorFilter = itemDescriptor.getItem();
			if (!eefTabDescriptorFilter.filter(eefTabDescriptor)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Returns the descriptors.
	 *
	 * @param part
	 *            The current part
	 * @param input
	 *            The current selection
	 * @return The descriptors for the given part and selection
	 */
	public List<IEEFTabDescriptor> getTabDescriptors(IWorkbenchPart part, ISelection input) {
		if (input == null || input.isEmpty()) {
			return new ArrayList<IEEFTabDescriptor>();
		}
		List<IEEFTabDescriptor> descriptors = getAllTabDescriptors(part, input);
		List<IEEFTabDescriptor> result = filterTabDescriptors(descriptors, part, input);
		return result;
	}

	/**
	 * Filters out the tab descriptors that do not have any sections for the given input.
	 *
	 * @param eefTabDescriptors
	 *            EEF tab descriptors
	 * @param part
	 *            The current part
	 * @param input
	 *            The current selection
	 * @return List of EEF tab descriptors
	 */
	private List<IEEFTabDescriptor> filterTabDescriptors(List<IEEFTabDescriptor> eefTabDescriptors, IWorkbenchPart part, ISelection input) {
		Map<String, IEEFTabDescriptor> filteredEefTabDescriptors = new LinkedHashMap<String, IEEFTabDescriptor>();
		for (IEEFTabDescriptor eefTabDescriptor : eefTabDescriptors) {
			IEEFTabDescriptor filteredEefTabDescriptor = adaptDescriptorFor(eefTabDescriptor, part, input);
			if (!filteredEefTabDescriptor.getSectionDescriptors().isEmpty()) {
				filteredEefTabDescriptors.put(filteredEefTabDescriptor.getId(), filteredEefTabDescriptor);
			}
		}
		return new ArrayList<IEEFTabDescriptor>(filteredEefTabDescriptors.values());
	}

	/**
	 * Given a property tab descriptor remove all its section descriptors that do not apply to the given input object.
	 *
	 * @param target
	 *            EEF tab descriptor
	 * @param part
	 *            The current part
	 * @param selection
	 *            The current selection
	 * @return EEF tab descriptor filtered according to the given selection
	 */
	private IEEFTabDescriptor adaptDescriptorFor(IEEFTabDescriptor target, IWorkbenchPart part, ISelection selection) {
		List<IEEFSectionDescriptor> filteredEefSectionDescriptors = new ArrayList<IEEFSectionDescriptor>();
		// Get all the available section for this tab
		List<IEEFSectionDescriptor> eefSectionDescriptors = target.getSectionDescriptors();
		for (IEEFSectionDescriptor eefSectionDescriptor : eefSectionDescriptors) {
			// Check if section is valid for the current part and selection
			if (eefSectionDescriptor.appliesTo(part, selection)) {
				if (eefSectionDescriptor.getId() != null) {
					filteredEefSectionDescriptors.add(eefSectionDescriptor);
				} else {
					EEFTabbedPropertyViewPlugin.getPlugin().error(Messages.EEFTabbedPropertyRegistry_MissingSectionDescriptorId);
				}
			}
		}

		AbstractEEFTabDescriptor filteredEefTabDescriptor = (AbstractEEFTabDescriptor) ((AbstractEEFTabDescriptor) target).clone();
		filteredEefTabDescriptor.setSectionDescriptors(filteredEefSectionDescriptors);
		return filteredEefTabDescriptor;
	}
}

Back to the top