Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b5fd41f24fd6895a21594a54e395ae521db48ca4 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*******************************************************************************
 * Copyright (c) 2007, 2010 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
 *    James Blackburn (Broadcom Corp.)
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.ui.properties;

import java.util.Set;
import java.util.TreeSet;

import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICMultiItemsHolder;
import org.eclipse.cdt.core.settings.model.ICResourceDescription;
import org.eclipse.cdt.managedbuilder.buildproperties.IBuildPropertyValue;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IMultiConfiguration;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;


/**
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class ArtifactTab extends AbstractCBuildPropertyTab {

	public static final String PROPERTY = ManagedBuildManager.BUILD_ARTEFACT_TYPE_PROPERTY_ID;

	private Label l4;
	private Combo t2, t3, t4;
	private Combo c1;
	private int savedPos = -1; // current project type
	private IConfiguration fCfg; 
	private IBuildPropertyValue[] values;
	private ITool tTool;
	private boolean canModify = true;
	
	private enum FIELD {NAME, EXT, PREF}
	
	private Set<String> set2 = new TreeSet<String>();
	private Set<String> set3 = new TreeSet<String>();
	private Set<String> set4 = new TreeSet<String>();

	@Override
	public void createControls(Composite parent) {
		super.createControls(parent);
		usercomp.setLayout(new GridLayout(2, false));

		Label l1 = new Label(usercomp, SWT.NONE);
		l1.setLayoutData(new GridData(GridData.BEGINNING));
		l1.setText(Messages.getString("ArtifactTab.0")); //$NON-NLS-1$
		c1 = new Combo(usercomp, SWT.READ_ONLY | SWT.DROP_DOWN | SWT.BORDER);
		c1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		c1.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			typeChanged();
		}});
		c1.setOrientation(SWT.LEFT_TO_RIGHT);
		
		Label l2 = new Label(usercomp, SWT.NONE);
		l2.setLayoutData(new GridData(GridData.BEGINNING));
		l2.setText(Messages.getString("ArtifactTab.1")); //$NON-NLS-1$
		t2 = setCombo(FIELD.NAME, set2);
		t2.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (canModify)
					fCfg.setArtifactName(t2.getText());
			}} );
		
		Label l3 = new Label(usercomp, SWT.NONE);
		l3.setLayoutData(new GridData(GridData.BEGINNING));
		l3.setText(Messages.getString("ArtifactTab.2")); //$NON-NLS-1$
		t3 = setCombo(FIELD.EXT, set3);
		t3.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (canModify)
					fCfg.setArtifactExtension(t3.getText());
			}} );
		
		l4 = new Label(usercomp, SWT.NONE);
		l4.setLayoutData(new GridData(GridData.BEGINNING));
		l4.setText(Messages.getString("ArtifactTab.3")); //$NON-NLS-1$
		t4 = setCombo(FIELD.PREF, set4);
		t4.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (canModify) {
					if(tTool != null)
						tTool.setOutputPrefixForPrimaryOutput(t4.getText());
					else if (fCfg instanceof IMultiConfiguration)
						((IMultiConfiguration)fCfg).setOutputPrefixForPrimaryOutput(t4.getText());
				}
			}} );

		updateData(getResDesc());
	}

	private void typeChanged() {
		int n = c1.getSelectionIndex();
		if (n != savedPos) {
			setProjectType(n);
			savedPos = n;
			updateData(getResDesc());
		}
	}

	private void setProjectType(int n) {
		try {
			String s = values[n].getId();
			fCfg.setBuildArtefactType(s);
		} catch (BuildException ex) {
			ManagedBuilderUIPlugin.log(ex);
		}
	}
	
	@Override
	public void updateData(ICResourceDescription cfgd) {
		if (cfgd == null) return;
		fCfg = getCfg();
		if (page.isMultiCfg()) {
			values = ((IMultiConfiguration)fCfg).getSupportedValues(PROPERTY);
		} else {
			values = fCfg.getBuildProperties().getSupportedValues(PROPERTY);
		}
		c1.removeAll();
		c1.setData(values);
		for (int i=0; i<values.length; i++) {
			c1.add(values[i].getName());
		}
		c1.setText(EMPTY_STR);
		IBuildPropertyValue pv = fCfg.getBuildArtefactType();
		if (pv != null) {
			String s = pv.getId();
			for (int i=0; i<values.length; i++) {
				if (s.equals(values[i].getId())) {
					c1.select(i);
					savedPos = i;
					break;
				}
			}
		}
		
		updateCombo(t2);
		updateCombo(t3);
		updateCombo(t4);
		
		String s = fCfg.getArtifactName();
		if (! page.isMultiCfg() && (s == null || s.trim().length() == 0)) {
			s = getResDesc().getConfiguration().getProjectDescription().getName();
			getCfg().setArtifactName(s);
		}
		
		canModify = false;
		
		t2.setText(s);
		t3.setText(fCfg.getArtifactExtension());
		
		if (page.isMultiCfg()) {
			if (l4 != null) 
				l4.setVisible(true);
			if (t4 != null) {
				t4.setVisible(true);
				t4.setText(((IMultiConfiguration)fCfg).getToolOutputPrefix());
			}
		} else {
			tTool = fCfg.calculateTargetTool();
			if(tTool != null){
				if (l4 != null) 
					l4.setVisible(true);
				if (t4 != null) {
					t4.setVisible(true);
					t4.setText(tTool.getOutputPrefix());
				}
			} else {
				if (l4 != null) 
					l4.setVisible(false);
				if (t4 != null) 
					t4.setVisible(false);
			}
		}
		canModify = true;
	}
	
	@Override
	protected void performApply(ICResourceDescription src, ICResourceDescription dst) {
		IConfiguration cfg1 = getCfg(src.getConfiguration());
		IConfiguration cfg2 = getCfg(dst.getConfiguration());
		cfg2.setArtifactName(cfg1.getArtifactName());
		cfg2.setArtifactExtension(cfg1.getArtifactExtension());
		
		ITool t1 = cfg1.calculateTargetTool();
		ITool t2 = cfg2.calculateTargetTool();
		if (t1 != null && t2 != null) 
			t2.setOutputPrefixForPrimaryOutput(t1.getOutputPrefix());
			
		try {
			IBuildPropertyValue bv = cfg1.getBuildArtefactType();
			if (bv != null)
				cfg2.setBuildArtefactType(bv.getId());
		} catch (BuildException e) {
			ManagedBuilderUIPlugin.log(e);
		}
	}
	
	@Override
	protected void performDefaults() {
		fCfg.setArtifactName(fCfg.getManagedProject().getDefaultArtifactName());
		fCfg.setArtifactExtension(null);
		// workaround for bad extension setting (always "exe"):
		// set wrong project type temporary 
		// and then set right one back
		if (c1.getItemCount() > 1) {
			int right = c1.getSelectionIndex();
			int wrong = (right == 0) ? 1 : 0;
			setProjectType(wrong);
			setProjectType(right);
		}
		if (tTool != null)
			tTool.setOutputPrefixForPrimaryOutput(null);
		else if (fCfg instanceof IMultiConfiguration)
			((IMultiConfiguration)fCfg).setOutputPrefixForPrimaryOutput(null);
		updateData(getResDesc());
	}

	@Override
	public boolean canBeVisible() {
		if (page.isForProject()) {
			if (page.isMultiCfg()) {
				ICMultiItemsHolder mih = (ICMultiItemsHolder)getCfg();
				IConfiguration[] cfs = (IConfiguration[])mih.getItems();
				for (int i=0; i<cfs.length; i++) {
					if (cfs[i].getBuilder().isManagedBuildOn())
						return true;
				}
				return false;
			} else
				return getCfg().getBuilder().isManagedBuildOn();
		}
		else
			return false;
	}
	@Override
	protected void updateButtons() {} // Do nothing. No buttons to update.
	
	private Combo setCombo(FIELD field, Set<String> set) {
		Combo combo = new Combo(usercomp, SWT.BORDER);
		combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		combo.setData(ENUM, field);
		combo.setData(SSET, set);
		updateCombo(combo);
		return combo;
	}
	
	@SuppressWarnings("unchecked")
	private void updateCombo(Combo combo) {
		FIELD field = (FIELD)combo.getData(ENUM);
		Set<String> set   = (Set<String>)combo.getData(SSET);
		if (field == null || set == null)
			return;
		
		canModify = false;
		String oldStr = combo.getText();
		combo.removeAll();
		for (ICConfigurationDescription cf : page.getCfgsEditable()) {
			IConfiguration c = getCfg(cf);
			String s = null;
			switch (field) {
			case NAME:
				s = c.getArtifactName(); 
				break;
			case EXT:
				s = c.getArtifactExtension();
				break;
			case PREF:
				ITool t = c.calculateTargetTool();
				if(t != null)
					s = t.getOutputPrefix();
			}
			if (s != null && s.trim().length() > 0)
				set.add(s.trim());
		}
		if (set.size() > 0) 
			combo.setItems(set.toArray(new String[set.size()]));
		combo.setText(oldStr);
		canModify = true;
	}
}

Back to the top