KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > Contribution


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.registry;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.InvalidRegistryObjectException;
15
16 // This object is used to keep track on a contributor basis of the extension and extension points being contributed.
17
// It is mainly used on removal so we can quickly find objects to remove.
18
// Each contribution is made in the context of a namespace.
19
public class Contribution implements KeyedElement {
20     static final int[] EMPTY_CHILDREN = new int[] {0, 0};
21
22     //The registry that owns this object
23
protected ExtensionRegistry registry;
24
25     // The actual contributor of the contribution
26
final protected String JavaDoc contributorId;
27
28     // Value is derived from the contributorId and cached for performance
29
private String JavaDoc defaultNamespace = null;
30
31     // indicates if this contribution needs to be saved in the registry cache
32
protected boolean persist;
33
34     // This array stores the identifiers of both the extension points and the extensions.
35
// The array has always a minimum size of 2.
36
// The first element of the array is the number of extension points and the second the number of extensions.
37
// [numberOfExtensionPoints, numberOfExtensions, extensionPoint#1, extensionPoint#2, extensionPoint..., ext#1, ext#2, ext#3, ... ].
38
// The size of the array is 2 + (numberOfExtensionPoints + numberOfExtensions).
39
private int[] children = EMPTY_CHILDREN;
40     static final public byte EXTENSION_POINT = 0;
41     static final public byte EXTENSION = 1;
42
43     protected Contribution(String JavaDoc contributorId, ExtensionRegistry registry, boolean persist) {
44         this.contributorId = contributorId;
45         this.registry = registry;
46         this.persist = persist;
47     }
48
49     void mergeContribution(Contribution addContribution) {
50         Assert.isTrue(contributorId.equals(addContribution.contributorId));
51         Assert.isTrue(registry == addContribution.registry);
52
53         // persist?
54
// Old New Result
55
// F F F
56
// F T T => needs to be adjusted
57
// T F T
58
// T T T
59
if (shouldPersist() != addContribution.shouldPersist())
60             persist = true;
61
62         int[] existing = getRawChildren();
63         int[] addition = addContribution.getRawChildren();
64
65         int extensionPoints = existing[EXTENSION_POINT] + addition[EXTENSION_POINT];
66         int extensions = existing[EXTENSION] + addition[EXTENSION];
67         int[] allChildren = new int[2 + extensionPoints + extensions];
68
69         allChildren[EXTENSION_POINT] = extensionPoints;
70         System.arraycopy(existing, 2, allChildren, 2, existing[EXTENSION_POINT]);
71         System.arraycopy(addition, 2, allChildren, 2 + existing[EXTENSION_POINT], addition[EXTENSION_POINT]);
72         allChildren[EXTENSION] = extensions;
73         System.arraycopy(existing, 2 + existing[EXTENSION_POINT], allChildren, 2 + extensionPoints, existing[EXTENSION]);
74         System.arraycopy(addition, 2 + addition[EXTENSION_POINT], allChildren, 2 + extensionPoints + existing[EXTENSION], addition[EXTENSION]);
75
76         children = allChildren;
77     }
78
79     void setRawChildren(int[] children) {
80         this.children = children;
81     }
82
83     protected String JavaDoc getContributorId() {
84         return contributorId;
85     }
86
87     protected int[] getRawChildren() {
88         return children;
89     }
90
91     protected int[] getExtensions() {
92         int[] results = new int[children[EXTENSION]];
93         System.arraycopy(children, 2 + children[EXTENSION_POINT], results, 0, children[EXTENSION]);
94         return results;
95     }
96
97     protected int[] getExtensionPoints() {
98         int[] results = new int[children[EXTENSION_POINT]];
99         System.arraycopy(children, 2, results, 0, children[EXTENSION_POINT]);
100         return results;
101     }
102
103     public String JavaDoc getDefaultNamespace() {
104         if (defaultNamespace == null)
105             defaultNamespace = registry.getObjectManager().getContributor(contributorId).getName();
106         return defaultNamespace;
107     }
108
109     public String JavaDoc toString() {
110         return "Contribution: " + contributorId + " in namespace" + getDefaultNamespace(); //$NON-NLS-1$ //$NON-NLS-2$
111
}
112
113     //Implements the KeyedElement interface
114
public int getKeyHashCode() {
115         return getKey().hashCode();
116     }
117
118     public Object JavaDoc getKey() {
119         return contributorId;
120     }
121
122     public boolean compare(KeyedElement other) {
123         return contributorId.equals(((Contribution) other).contributorId);
124     }
125
126     public boolean shouldPersist() {
127         return persist;
128     }
129
130     public void unlinkChild(int id) {
131         // find index of the child being unlinked:
132
int index = -1;
133         for (int i = 2; i < children.length; i++) {
134             if (children[i] == id) {
135                 index = i;
136                 break;
137             }
138         }
139         if (index == -1)
140             throw new InvalidRegistryObjectException();
141
142         // copy all array except one element at index
143
int[] result = new int[children.length - 1];
144         System.arraycopy(children, 0, result, 0, index);
145         System.arraycopy(children, index + 1, result, index, children.length - index - 1);
146
147         // fix sizes
148
if (index < children[EXTENSION_POINT] + 2)
149             result[EXTENSION_POINT]--;
150         else
151             result[EXTENSION]--;
152
153         children = result;
154     }
155
156     /**
157      * Contribution is empty if it has no children.
158      */

159     public boolean isEmpty() {
160         return (children[EXTENSION_POINT] == 0 || children[EXTENSION] == 0);
161     }
162
163     /**
164      * Find if this contribution has a children with ID = id.
165      * @param id possible ID of the child
166      * @return true: contribution has this child
167      */

168     public boolean hasChild(int id) {
169         for (int i = 2; i < children.length; i++) {
170             if (children[i] == id)
171                 return true;
172         }
173         return false;
174     }
175 }
176
Popular Tags