001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.texteditor.rulers;
011:
012: import java.util.Collections;
013: import java.util.LinkedHashSet;
014: import java.util.Set;
015:
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.ILog;
019: import org.eclipse.core.runtime.InvalidRegistryObjectException;
020:
021: import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
022:
023: /**
024: * Describes the placement specification of a contribution to the
025: * <code>org.eclipse.ui.texteditor.rulerColumns</code> extension point.
026: *
027: * @since 3.3
028: */
029: public final class RulerColumnPlacement {
030: /** The extension schema name of the id attribute. */
031: private static final String ID = "id"; //$NON-NLS-1$
032: /** The extension schema name of the optional gravity attribute. */
033: private static final String GRAVITY = "gravity"; //$NON-NLS-1$
034: /** The extension schema name of the before element. */
035: private static final String BEFORE = "before"; //$NON-NLS-1$
036: /** The extension schema name of the after element. */
037: private static final String AFTER = "after"; //$NON-NLS-1$
038:
039: /** The placement gravity. */
040: private final float fGravity;
041: /** The placement constraints (element type: {@link RulerColumnPlacementConstraint}). */
042: private final Set fConstraints;
043:
044: public RulerColumnPlacement() {
045: fGravity = 1f;
046: fConstraints = Collections.EMPTY_SET;
047: }
048:
049: public RulerColumnPlacement(IConfigurationElement element)
050: throws InvalidRegistryObjectException {
051: Assert.isLegal(element != null);
052: ILog log = TextEditorPlugin.getDefault().getLog();
053: ExtensionPointHelper helper = new ExtensionPointHelper(element,
054: log);
055:
056: fGravity = helper.getDefaultAttribute(GRAVITY, 1f);
057: if (fGravity < 0 || fGravity > 1)
058: helper
059: .fail(RulerColumnMessages.RulerColumnPlacement_illegal_gravity_msg);
060: fConstraints = readIds(log, element.getChildren());
061: }
062:
063: private Set readIds(ILog log, IConfigurationElement[] children) {
064: Set constraints = new LinkedHashSet(
065: (int) (children.length / 0.75) + 1, 0.75f);
066: for (int i = 0; i < children.length; i++) {
067: IConfigurationElement child = children[i];
068: String name = child.getName();
069: ExtensionPointHelper childHelper = new ExtensionPointHelper(
070: child, log);
071: boolean before;
072: if (AFTER.equals(name))
073: before = false;
074: else if (BEFORE.equals(name))
075: before = true;
076: else {
077: childHelper
078: .fail(RulerColumnMessages.RulerColumnPlacement_illegal_child_msg);
079: continue;
080: }
081: constraints.add(new RulerColumnPlacementConstraint(
082: childHelper.getNonNullAttribute(ID), before));
083: }
084: return Collections.unmodifiableSet(constraints);
085: }
086:
087: /**
088: * The gravity of the placement specification, a float in the range <code>[0, 1]</code>.
089: *
090: * @return the gravity of the placement specification
091: */
092: public float getGravity() {
093: return fGravity;
094: }
095:
096: /**
097: * Returns the placement constraints in the order that they appear in the extension declaration.
098: *
099: * @return the unmodifiable set of placement constraints in the order that they appear in the
100: * extension declaration
101: */
102: public Set getConstraints() {
103: return fConstraints;
104: }
105: }
|