Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29628e9f35d3fd10cb62f8a2aa33c73f800d1dd7 (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
/*******************************************************************************
 * Copyright (c) 2006, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.texteditor.rulers;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.InvalidRegistryObjectException;

/**
 * Describes the placement specification of a contribution to the
 * <code>org.eclipse.ui.texteditor.rulerColumns</code> extension point.
 *
 * @since 3.3
 */
public final class RulerColumnPlacement {
	/** The extension schema name of the id attribute. */
	private static final String ID= "id"; //$NON-NLS-1$
	/** The extension schema name of the optional gravity attribute. */
	private static final String GRAVITY= "gravity"; //$NON-NLS-1$
	/** The extension schema name of the before element. */
	private static final String BEFORE= "before"; //$NON-NLS-1$
	/** The extension schema name of the after element. */
	private static final String AFTER= "after"; //$NON-NLS-1$

	/** The placement gravity. */
	private final float fGravity;
	/** The placement constraints. */
	private final Set<RulerColumnPlacementConstraint> fConstraints;

	public RulerColumnPlacement() {
		fGravity= 1f;
		fConstraints= Collections.emptySet();
	}

	public RulerColumnPlacement(IConfigurationElement element) throws InvalidRegistryObjectException, CoreException {
		Assert.isLegal(element != null);
		ExtensionPointHelper helper= new ExtensionPointHelper(element);

		fGravity= helper.getDefaultAttribute(GRAVITY, 1f);
		if (fGravity < 0 || fGravity > 1)
			helper.fail(RulerColumnMessages.RulerColumnPlacement_illegal_gravity_msg);
		fConstraints= readIds(element.getChildren());
	}

	private Set<RulerColumnPlacementConstraint> readIds(IConfigurationElement[] children) throws CoreException {
		Set<RulerColumnPlacementConstraint> constraints= new LinkedHashSet<>((int) (children.length / 0.75) + 1, 0.75f);
		for (IConfigurationElement child : children) {
			String name= child.getName();
			ExtensionPointHelper childHelper= new ExtensionPointHelper(child);
			boolean before;
			if (AFTER.equals(name))
				before= false;
			else if (BEFORE.equals(name))
				before= true;
			else {
				childHelper.fail(RulerColumnMessages.RulerColumnPlacement_illegal_child_msg);
				continue;
			}
			constraints.add(new RulerColumnPlacementConstraint(childHelper.getNonNullAttribute(ID), before));
		}
		return Collections.unmodifiableSet(constraints);
	}

	/**
	 * The gravity of the placement specification, a float in the range <code>[0, 1]</code>.
	 *
	 * @return the gravity of the placement specification
	 */
	public float getGravity() {
		return fGravity;
	}

	/**
	 * Returns the placement constraints in the order that they appear in the extension declaration.
	 *
	 * @return the unmodifiable set of placement constraints in the order that they appear in the
	 *         extension declaration
	 */
	public Set<RulerColumnPlacementConstraint> getConstraints() {
		return fConstraints;
	}
}

Back to the top