Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4a070146a654271c607d2ae7af3ae7ec2af93ee1 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 Intel 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:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.envvar;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.envvar.IContributedEnvironment;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;

public class ContributedEnvironment implements IContributedEnvironment{
	private EnvironmentVariableManager fMngr;
	public ContributedEnvironment(EnvironmentVariableManager mngr){
		fMngr = mngr;
	}
	private class ContributedEnvContextInfo extends DefaultEnvironmentContextInfo {
		private IEnvironmentContextInfo fBaseInfo;
		private ICoreEnvironmentVariableSupplier fSuppliers[];

		public ContributedEnvContextInfo(IEnvironmentContextInfo info) {
			super(info.getContext());
			fBaseInfo = info;
		}

		public ICoreEnvironmentVariableSupplier[] getSuppliers() {
			if(fSuppliers == null){
				ICoreEnvironmentVariableSupplier[] suppliers = fBaseInfo.getSuppliers();
				int i = 0;
				for(; i < suppliers.length; i++){
					if(suppliers[i] == EnvironmentVariableManager.fEclipseSupplier){
						break;
					}
				}
				
				if(i != suppliers.length){
					ICoreEnvironmentVariableSupplier tmp[] = new ICoreEnvironmentVariableSupplier[suppliers.length - 1];
					if(i != 0)
						System.arraycopy(suppliers, 0, tmp, 0, i);
					if(i != tmp.length)
						System.arraycopy(suppliers, i+1, tmp, i, tmp.length - i);
					suppliers = tmp;
				}
				
				fSuppliers = suppliers;
			}
			return fSuppliers;
		}

		public IEnvironmentContextInfo getNext() {
			IEnvironmentContextInfo baseNext = fBaseInfo.getNext();
			if(baseNext != null)
				return new ContributedEnvContextInfo(baseNext);
			return null;
		}
	}
	
	public IEnvironmentContextInfo getContextInfo(Object context){
		return new ContributedEnvContextInfo(fMngr.getDefaultContextInfo(context));
	}
	
	public IEnvironmentVariable[] getVariables(ICConfigurationDescription des){
		EnvVarCollector cr = EnvironmentVariableManager.getVariables(getContextInfo(des), true);
		if(cr != null){
			EnvVarDescriptor collected[] = cr.toArray(true);
			List<IEnvironmentVariable> vars = new ArrayList<IEnvironmentVariable>(collected.length);
			IEnvironmentVariable var;
			IEnvironmentContextInfo info = new DefaultEnvironmentContextInfo(des);//getContextInfo(des);
			for(int i = 0; i < collected.length; i++){
				var = collected[i];
				var = EnvironmentVariableManager.getVariable(var.getName(), info, true);
				if(var != null)
					vars.add(var);
			}
			return vars.toArray(new EnvVarDescriptor[vars.size()]);
		}
		return new EnvVarDescriptor[0];
	}
	
	public IEnvironmentVariable getVariable(String name, ICConfigurationDescription des){
		EnvVarDescriptor varDes = EnvironmentVariableManager.getVariable(name, getContextInfo(des), true);
		if(varDes != null)
			return EnvironmentVariableManager.getVariable(name, new DefaultEnvironmentContextInfo(des), true);
		return null;
	}
	
	public boolean appendEnvironment(ICConfigurationDescription des){
		return EnvironmentVariableManager.fUserSupplier.appendContributedEnvironment(des);
	}

	public void setAppendEnvironment(boolean append, ICConfigurationDescription des){
		EnvironmentVariableManager.fUserSupplier.setAppendContributedEnvironment(append, des);
	}

	public IEnvironmentVariable addVariable(String name,
			String value,
			int op,
			String delimiter,
			ICConfigurationDescription des){
		return new EnvVarDescriptor(
				EnvironmentVariableManager.fUserSupplier.createVariable(name, value, op, delimiter, des),
				null,
				-1,
				EnvironmentVariableManager.fUserSupplier);
	}
	
	public void addVariables(IEnvironmentVariable[] vars,
			ICConfigurationDescription des) {
		for (IEnvironmentVariable v : vars)
			addVariable(v, des);
	}
	
	public IEnvironmentVariable addVariable(IEnvironmentVariable var,
			ICConfigurationDescription des) {
		return addVariable(var.getName(), 
						   var.getValue(), 
						   var.getOperation(), 
						   var.getDelimiter(), 
						   des);
	}
	
	public IEnvironmentVariable removeVariable(String name, ICConfigurationDescription des){
		return EnvironmentVariableManager.fUserSupplier.deleteVariable(name, des);
	}
	
	public void restoreDefaults(ICConfigurationDescription des){
		EnvironmentVariableManager.fUserSupplier.restoreDefaults(des);
	}
	
	public boolean isUserVariable(ICConfigurationDescription des, IEnvironmentVariable var){
		if(var instanceof EnvVarDescriptor)
			return ((EnvVarDescriptor)var).getSupplier() == EnvironmentVariableManager.fUserSupplier;
		return false;
	}
	
	public String getOrigin(IEnvironmentVariable var) {
		if(var instanceof EnvVarDescriptor) {
			ICoreEnvironmentVariableSupplier sup = ((EnvVarDescriptor)var).getSupplier();
			if (sup instanceof BuildSustemEnvironmentSupplier)
				return Messages.getString("ContributedEnvironment.0"); //$NON-NLS-1$
			if (sup instanceof EclipseEnvironmentSupplier)
				return Messages.getString("ContributedEnvironment.1"); //$NON-NLS-1$
			if (sup instanceof UserDefinedEnvironmentSupplier)
				return Messages.getString("ContributedEnvironment.2"); //$NON-NLS-1$
		}
		return Messages.getString("ContributedEnvironment.3"); //$NON-NLS-1$
	}
	
	public void serialize(ICProjectDescription des){
		EnvironmentVariableManager.fUserSupplier.storeProjectEnvironment(des, false);
	}

	
}

Back to the top