Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 073b02d0ae5a2964b56e7122a2289afda181795b (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*******************************************************************************
 * Copyright (c) 2005, 2007 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.managedbuilder.ui.properties;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.cdtvariables.ICdtVariable;
import org.eclipse.cdt.core.cdtvariables.ICdtVariableManager;
import org.eclipse.cdt.core.cdtvariables.ICdtVariableStatus;
import org.eclipse.cdt.core.model.util.CDTListComparator;
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.internal.core.cdtvariables.CdtVariableManager;
import org.eclipse.cdt.internal.core.cdtvariables.EclipseVariablesVariableSupplier;
import org.eclipse.cdt.internal.core.cdtvariables.ICoreVariableContextInfo;
import org.eclipse.cdt.internal.core.cdtvariables.StorableCdtVariables;
import org.eclipse.cdt.internal.core.cdtvariables.UserDefinedVariableSupplier;
import org.eclipse.cdt.ui.newui.AbstractCPropertyTab;
import org.eclipse.cdt.ui.newui.AbstractPage;
import org.eclipse.cdt.ui.newui.PrefPage_Abstract;
import org.eclipse.cdt.ui.newui.UIMessages;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.cdt.utils.envvar.EnvVarOperationProcessor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnLayoutData;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * displays the build macros for the given context
 */
public class CPropertyVarsTab extends AbstractCPropertyTab {
	/*
	 * String constants
	 */
	private static final String PREFIX = "MacrosBlock";	//$NON-NLS-1$
	private static final String LABEL = PREFIX + ".label";	//$NON-NLS-1$

	private static final String HEADER = LABEL + ".header";  //$NON-NLS-1$
	private static final String HEADER_NAME = HEADER + ".name";  //$NON-NLS-1$
	private static final String HEADER_TYPE = HEADER + ".type";  //$NON-NLS-1$
	private static final String HEADER_VALUE = HEADER + ".value";  //$NON-NLS-1$

	private static final String TYPE = LABEL + ".type";	//$NON-NLS-1$
	private static final String TYPE_TEXT = TYPE + ".text";	//$NON-NLS-1$
	private static final String TYPE_TEXT_LIST = TYPE + ".text.list";	//$NON-NLS-1$
	private static final String TYPE_PATH_FILE = TYPE + ".path.file";	//$NON-NLS-1$
	private static final String TYPE_PATH_FILE_LIST = TYPE + ".path.file.list";	//$NON-NLS-1$
	private static final String TYPE_PATH_DIR = TYPE + ".path.dir";	//$NON-NLS-1$
	private static final String TYPE_PATH_DIR_LIST = TYPE + ".path.dir.list";	//$NON-NLS-1$
	private static final String TYPE_PATH_ANY = TYPE + ".path.any";	//$NON-NLS-1$
	private static final String TYPE_PATH_ANY_LIST = TYPE + ".path.any.list";	//$NON-NLS-1$

	private static final String DELETE_CONFIRM_TITLE = LABEL + ".delete.confirm.title";	//$NON-NLS-1$
	private static final String DELETE_CONFIRM_MESSAGE = LABEL + ".delete.confirm.message";	//$NON-NLS-1$

	private static final String DELETE_ALL_CONFIRM_TITLE = LABEL + ".delete.all.confirm.title";	//$NON-NLS-1$
	private static final String DELETE_ALL_CONFIRM_MESSAGE = LABEL + ".delete.all.confirm.message";	//$NON-NLS-1$
	
	private static final String VALUE = LABEL + ".value";	//$NON-NLS-1$
	private static final String VALUE_ECLIPSE_DYNAMIC = VALUE + ".eclipse.dynamic";	//$NON-NLS-1$
	
	private static final String VALUE_DELIMITER = " || ";	//$NON-NLS-1$

	private static final ICdtVariableManager vmgr = CCorePlugin.getDefault().getCdtVariableManager();
	private static final UserDefinedVariableSupplier fUserSup = CdtVariableManager.fUserDefinedMacroSupplier;
	private static final EnvCmp comparator = new EnvCmp(); 

	private ICConfigurationDescription cfgd = null;
	private StorableCdtVariables vars = null;
	
	//currently the "CWD" and "PWD" macros are not displayed in UI
	private static final String fHiddenMacros[] = new String[]{
			"CWD",   //$NON-NLS-1$
			"PWD"	  //$NON-NLS-1$
		};
	
	private boolean fShowSysMacros = false;
	private Set fIncorrectlyDefinedMacrosNames = new HashSet();
	private static final int CONTEXT = ICoreVariableContextInfo.CONTEXT_CONFIGURATION;

	private TableViewer tv;
	private Label fStatusLabel;
	
	private static final String[] fEditableTableColumnProps = new String[] {
		"editable name",	//$NON-NLS-1$
		"editable type",	//$NON-NLS-1$
		"editable value",	//$NON-NLS-1$
	};

	private static final String[] fTableColumnNames = new String[] {
		UIMessages.getString(HEADER_NAME),
		UIMessages.getString(HEADER_TYPE),
		UIMessages.getString(HEADER_VALUE),
	};

	private static final ColumnLayoutData[] fTableColumnLayouts = {new ColumnPixelData(100), new ColumnPixelData(100), new ColumnPixelData(250)};
	
	private class MacroContentProvider implements IStructuredContentProvider{
		public Object[] getElements(Object inputElement) { return (Object[])inputElement; }
		public void dispose() {	}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
	}
	
	private class MacroLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider , ITableFontProvider, IColorProvider{
		public Image getImage(Object element) { return null; }
		public String getText(Object element) { return getColumnText(element, 0); }
		public Font getFont(Object element) { return getFont(element, 0); }
		public Image getColumnImage(Object element, int columnIndex) { return null; }
		public Color getBackground(Object element){	return null; }
		
		public String getColumnText(Object element, int columnIndex) {
			ICdtVariable var = (ICdtVariable)element;
			switch(columnIndex){
			case 0:
				return var.getName();
			case 1:
				switch(var.getValueType()){
				case ICdtVariable.VALUE_PATH_FILE:
					return UIMessages.getString(TYPE_PATH_FILE);
				case ICdtVariable.VALUE_PATH_FILE_LIST:
					return UIMessages.getString(TYPE_PATH_FILE_LIST);
				case ICdtVariable.VALUE_PATH_DIR:
					return UIMessages.getString(TYPE_PATH_DIR);
				case ICdtVariable.VALUE_PATH_DIR_LIST:
					return UIMessages.getString(TYPE_PATH_DIR_LIST);
				case ICdtVariable.VALUE_PATH_ANY:
					return UIMessages.getString(TYPE_PATH_ANY);
				case ICdtVariable.VALUE_PATH_ANY_LIST:
					return UIMessages.getString(TYPE_PATH_ANY_LIST);
				case ICdtVariable.VALUE_TEXT:
					return UIMessages.getString(TYPE_TEXT);
				case ICdtVariable.VALUE_TEXT_LIST:
					return UIMessages.getString(TYPE_TEXT_LIST);
				default:
					return "? " + var.getValueType();   //$NON-NLS-1$
				}
			case 2:
				return getString(var);  
			}
			return EMPTY_STR;
		}
		
		private Font getValueFont(ICdtVariable var){
			Font font = null;
			if(isUserVar(var))
				font = JFaceResources.getFontRegistry().getItalic(JFaceResources.DIALOG_FONT);
			return font;
		}
		public Font getFont(Object element, int columnIndex) {
			ICdtVariable var = (ICdtVariable)element;
			switch(columnIndex){
			case 0:
			case 1:
				break;
			case 2:
				return getValueFont(var);
			}
			if(isUserVar(var))
				return JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT);
			return null;
		}
	    public Color getForeground(Object element){
			if(fIncorrectlyDefinedMacrosNames.contains(((ICdtVariable)element).getName()))
				return JFaceResources.getColorRegistry().get(JFacePreferences.ERROR_COLOR);
			return null;
	    }
	}

	/*
	 * called when the user macro selection was changed 
	 */
	private void handleSelectionChanged(SelectionChangedEvent event){
		updateButtons();
	}
	
	protected void updateButtons() {
		Object[] obs = ((IStructuredSelection)tv.getSelection()).toArray();
		boolean canEdit = false;
		boolean canDel  = false;
		if (obs != null && obs.length > 0) {
			canEdit = (obs.length == 1);
			for (int i=0; i<obs.length; i++) {
				if (obs[i] instanceof ICdtVariable && isUserVar((ICdtVariable)obs[i])) { 
					canDel = true;
					break;
				}
			}
		}
		buttonSetEnabled(1, canEdit);
		buttonSetEnabled(2, canDel);
	}
	
	/*
	 * called when a custom button was pressed
	 */
	public void buttonPressed(int index){
		switch(index){
		case 0:{
			NewVarDialog dlg = new NewVarDialog(usercomp.getShell(), null, cfgd, getVariables());
			if(dlg.open() == Dialog.OK){
				ICdtVariable macro = dlg.getDefinedMacro();
				if(canCreate(macro)) {
					if (cfgd != null) {
						if (dlg.isForAllCfgs) {
							ICConfigurationDescription[] cfgs = page.getCfgsEditable();
							for (int k=0; k<cfgs.length; k++) 
								fUserSup.createMacro(macro, CONTEXT, cfgs[k]);
						} else { 
							if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
								ICConfigurationDescription[] cfs = (ICConfigurationDescription[])((ICMultiItemsHolder)cfgd).getItems();
								for (int k=0; k<cfs.length; k++)
									fUserSup.createMacro(macro, CONTEXT, cfs[k]);
							} else
								fUserSup.createMacro(macro, CONTEXT, cfgd);
						}
					} 
					else if (vars != null)
						vars.createMacro(macro);
					updateData();
				}
			}
		}
		break;
		case 1:{
			ICdtVariable _vars[] = getSelectedUserMacros();
			if(_vars != null && _vars.length == 1){
				NewVarDialog dlg = new NewVarDialog(usercomp.getShell() ,_vars[0], cfgd, getVariables());
				if(dlg.open() == Dialog.OK){
					ICdtVariable macro = dlg.getDefinedMacro();
					if(canCreate(macro)) {
						if (cfgd != null) {
							if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
								ICConfigurationDescription[] cfs = (ICConfigurationDescription[])((ICMultiItemsHolder)cfgd).getItems();
								for (int k=0; k<cfs.length; k++)
									fUserSup.createMacro(macro, CONTEXT, cfs[k]);
							} else
								fUserSup.createMacro(macro, CONTEXT, cfgd);
						}
						else if (vars != null)
							vars.createMacro(macro);
						updateData();
					}
				}
			}
		}
		break;
		case 2:{
			ICdtVariable macros[] = getSelectedUserMacros();
			if(macros != null && macros.length > 0){
				if(MessageDialog.openQuestion(usercomp.getShell(),
						UIMessages.getString(DELETE_CONFIRM_TITLE),
						UIMessages.getString(DELETE_CONFIRM_MESSAGE))){
					for(int i = 0; i < macros.length; i++){
						if (cfgd != null) {
							if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
								ICConfigurationDescription[] cfs = (ICConfigurationDescription[])((ICMultiItemsHolder)cfgd).getItems();
								for (int k=0; k<cfs.length; k++)
									fUserSup.deleteMacro(macros[i].getName(), CONTEXT, cfs[k]);
							}
							else		
								fUserSup.deleteMacro(macros[i].getName(), CONTEXT, cfgd);
						}
						else if (vars != null)
							vars.deleteMacro(macros[i].getName());
					}
					updateData();
				}
			}
		}
		break;
		}
	}
	
	/*
	 * returnes the selected user-defined macros
	 */
	private ICdtVariable[] getSelectedUserMacros(){
		if(tv == null)	return null;
		List list = ((IStructuredSelection)tv.getSelection()).toList();
		return (ICdtVariable[])list.toArray(new ICdtVariable[list.size()]);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.ui.dialogs.ICOptionPage#performDefaults()
	 */
	protected void performDefaults() {
		if(MessageDialog.openQuestion(usercomp.getShell(),
				UIMessages.getString(DELETE_ALL_CONFIRM_TITLE),
				UIMessages.getString(DELETE_ALL_CONFIRM_MESSAGE))){
			if (cfgd != null) {
				if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
					ICConfigurationDescription[] cfs = (ICConfigurationDescription[])((ICMultiItemsHolder)cfgd).getItems();
					for (int i=0; i<cfs.length; i++)
						fUserSup.deleteAll(CONTEXT, cfs[i]);
				} else
					fUserSup.deleteAll(CONTEXT, cfgd);
			}
			else if (vars != null)
				vars.deleteAll();
			updateData();
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControls(Composite parent) {
		super.createControls(parent);
		initButtons(new String[] {ADD_STR, EDIT_STR, DEL_STR});
		usercomp.setLayout(new GridLayout(1, false));
		createTableControl();
		
		// Create a "show parent levels" button 
		final Button b = new Button(usercomp, SWT.CHECK);
		b.setFont(usercomp.getFont());
		b.setText(Messages.getString("CPropertyVarsTab.0")); //$NON-NLS-1$
		b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		b.setSelection(fShowSysMacros);
		b.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				fShowSysMacros = b.getSelection();
				updateData(getResDesc());
			}
		});
//		if (page.isForPrefs()) b.setVisible(false);
		
		fStatusLabel = new Label(usercomp, SWT.LEFT);
		fStatusLabel.setFont(usercomp.getFont());
		fStatusLabel.setText(EMPTY_STR);
		fStatusLabel.setLayoutData(new GridData(GridData.BEGINNING));
		fStatusLabel.setForeground(JFaceResources.getColorRegistry().get(JFacePreferences.ERROR_COLOR));
	}
	
	private void createTableControl(){
		TableViewer tableViewer;
		tableViewer = new TableViewer(usercomp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI
				| SWT.FULL_SELECTION);
		
		Table table = tableViewer.getTable();
		TableLayout tableLayout = new TableLayout();
		for (int i = 0; i < fTableColumnNames.length; i++) {
			tableLayout.addColumnData(fTableColumnLayouts[i]);
			TableColumn tc = new TableColumn(table, SWT.NONE, i);
			tc.setResizable(fTableColumnLayouts[i].resizable);
			tc.setText(fTableColumnNames[i]);
		}
		table.setLayout(tableLayout);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		GridData gd = new GridData(GridData.FILL_BOTH);
		tableViewer.getControl().setLayoutData(gd);
		tableViewer.setContentProvider(new MacroContentProvider());
		tableViewer.setLabelProvider(new MacroLabelProvider());
		tableViewer.setSorter(new ViewerSorter());
		
		tableViewer.setColumnProperties(fEditableTableColumnProps);
		tv = tableViewer;
		tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				handleSelectionChanged(event);
			}
		});
		tableViewer.addDoubleClickListener(new IDoubleClickListener() {
	
			public void doubleClick(DoubleClickEvent event) {
				if (!tv.getSelection().isEmpty()) {	buttonPressed(1); }
			}
		});
	
		table.addKeyListener(new KeyListener(){
			public void keyPressed(KeyEvent e){
				if(e.keyCode == SWT.DEL) buttonPressed(2);
			}

			public void keyReleased(KeyEvent e){}
		});
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
	}

	/*
	 * answers whether the macro of a given name can be sreated
	 */
	private boolean canCreate(ICdtVariable v){
		if (v == null) return false;
		String name = v.getName();
		if(name == null || (name = name.trim()).length() == 0)
			return false;
		if(fHiddenMacros != null){
			for(int i = 0; i < fHiddenMacros.length; i++){
				if(fHiddenMacros[i].equals(EnvVarOperationProcessor.normalizeName(name)))
					return false;
			}
		}
		return true; 
	}
		
	public void updateData(ICResourceDescription _cfgd) {
		if (_cfgd == null) {
			cfgd = null;
			if (vars == null)
				vars = fUserSup.getWorkspaceVariablesCopy();
		} else {
			cfgd = _cfgd.getConfiguration();
			vars = null;
		}
		updateData();
	}
	
	private void checkVariableIntegrity() {
		try{
			if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
				ICConfigurationDescription[] cfs = (ICConfigurationDescription[])((ICMultiItemsHolder)cfgd).getItems();
				for (int i=0; i<cfs.length; i++)
					vmgr.checkVariableIntegrity(cfs[i]);
			} else
				vmgr.checkVariableIntegrity(cfgd);
			updateState(null);
		} catch (CdtVariableException e){
			updateState(e);
		}
	}

	private ICdtVariable[] getVariables() {
		if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
			ICMultiItemsHolder mih = (ICMultiItemsHolder)cfgd;
			ICConfigurationDescription[] cfs = (ICConfigurationDescription[])mih.getItems();
			ICdtVariable[][] vs = new ICdtVariable[cfs.length][];
			for (int i=0; i<cfs.length; i++)
				vs[i] = vmgr.getVariables(cfs[i]);
			Object[] obs = mih.getListForDisplay(vs, comparator);
			ICdtVariable[] v = new ICdtVariable[obs.length];
			System.arraycopy(obs, 0, v, 0, obs.length);
			return v;
		} else
			return vmgr.getVariables(cfgd);
	}
	
	private void updateData() {
		if(tv == null) return;
			
		checkVariableIntegrity();
		// get variables
		ICdtVariable[] _vars = getVariables();
		if (_vars == null) return;
		if (cfgd == null) {
			if (fShowSysMacros) {
				List lst = new ArrayList(_vars.length);
				ICdtVariable[] uvars = vars.getMacros();
				for (int i=0; i<uvars.length; i++) {
					lst.add(uvars[i]);
					for (int j=0; j<_vars.length; j++) {
						if (_vars[j] != null && _vars[j].getName().equals(uvars[i].getName())) {
							_vars[j] = null;
							break;
						}
					}
				}
				// add system vars not rewritten by user's
				for (int j=0; j<_vars.length; j++) {
					if (_vars[j] != null && !vmgr.isUserVariable(_vars[j], null)) 
						lst.add(_vars[j]);
				}
				_vars = (ICdtVariable[])lst.toArray(new ICdtVariable[lst.size()]);
			} else {
				_vars = vars.getMacros();
			}
		}
		
		ArrayList list = new ArrayList(_vars.length);
		for(int i = 0; i < _vars.length; i++){
			if(_vars[i] != null && (fShowSysMacros || isUserVar(_vars[i]))) 
				list.add(_vars[i]);
		}
		Collections.sort(list, CDTListComparator.getInstance());
		tv.setInput(list.toArray(new ICdtVariable[list.size()]));
		updateButtons();
	}
	
	private void updateState(CdtVariableException e){
		fIncorrectlyDefinedMacrosNames.clear();
		if(e != null){
			fStatusLabel.setText(e.getMessage());
			fStatusLabel.setVisible(true);
			ICdtVariableStatus statuses[] = e.getVariableStatuses();
			for(int i = 0; i < statuses.length; i++){
				String name = statuses[i].getVariableName();
				if(name != null)
					fIncorrectlyDefinedMacrosNames.add(name);
			}
		}
		else
			fStatusLabel.setVisible(false);
	}
	
	/**
	 * Checks whether variable is user's
	 * @param v - variable to check
	 * @return 
	 */
	private boolean isUserVar(ICdtVariable v) {
		if (cfgd == null)
			return vars.contains(v);
		if (page.isMultiCfg() && cfgd instanceof ICMultiItemsHolder) {
			ICConfigurationDescription[] cfs = (ICConfigurationDescription[])((ICMultiItemsHolder)cfgd).getItems();
			for (int i=0; i<cfs.length; i++)
				if (vmgr.isUserVariable(v, cfs[i]))
					return true;
			return false;
		} else 
			return vmgr.isUserVariable(v, cfgd); 
	}
	
	/* check whether variable is dynamic */
	private boolean isDynamic(ICdtVariable v) {
		if (v instanceof EclipseVariablesVariableSupplier.EclipseVarMacro) {
			EclipseVariablesVariableSupplier.EclipseVarMacro evar =
				(EclipseVariablesVariableSupplier.EclipseVarMacro)v;
			if (evar.getVariable() instanceof IDynamicVariable)
				return true;
		}		
		return false;
	}
	
	private String getString(ICdtVariable v) {
		if (isDynamic(v)) 
			return UIMessages.getString(VALUE_ECLIPSE_DYNAMIC);
		String value = EMPTY_STR; 
		try {			
			if (CdtVariableResolver.isStringListVariable(v.getValueType()))
				value = vmgr.convertStringListToString(v.getStringListValue(), VALUE_DELIMITER);
			else
				value = v.getStringValue();
		} catch (CdtVariableException e1) {}
		return value;
	}
	
	public void performApply(ICResourceDescription src, ICResourceDescription dst) {
		if (cfgd != null) {// only for project, not for prefs
			if (page.isMultiCfg()) {
				if (src instanceof ICMultiItemsHolder && 
					dst instanceof ICMultiItemsHolder) {
					ICMultiItemsHolder s = (ICMultiItemsHolder)src;
					ICMultiItemsHolder d = (ICMultiItemsHolder)dst;
					ICResourceDescription[] r0 = (ICResourceDescription[])s.getItems();
					ICResourceDescription[] r1 = (ICResourceDescription[])d.getItems();
					if (r0.length != r1.length)
						return; // unprobable
					for (int i=0; i<r0.length; i++) {
						ICdtVariable[] vs = fUserSup.getMacros(CONTEXT, r0[i].getConfiguration());
						fUserSup.setMacros(vs, CONTEXT, r1[i].getConfiguration());
					}
				}
			} else {
				ICdtVariable[] vs = fUserSup.getMacros(CONTEXT, src.getConfiguration());
				fUserSup.setMacros(vs, CONTEXT, dst.getConfiguration());
			}
		} else if (vars != null)
			fUserSup.storeWorkspaceVariables(true);
	}

	/**
	 * Unlike other pages, workspace variables 
	 * should be stored explicitly on "OK".  
	 */
	protected void performOK() {
		if (vars != null) try {
			if (fUserSup.setWorkspaceVariables(vars))
				if (page instanceof PrefPage_Abstract)
					PrefPage_Abstract.isChanged = true;
		} catch (CoreException e) {}
		vars = null;
		super.performOK();
	}
	
	protected void performCancel() {
		vars = null;
		super.performCancel();
	}

	// This page can be displayed for project only
	public boolean canBeVisible() {
		return page.isForProject() || page.isForPrefs();
	}

	private static class EnvCmp implements Comparator {
		
		public int compare(Object a0, Object a1) {
			if (a0 == null || a1 == null)
				return 0;
			if (a0 instanceof ICdtVariable &&
				a1 instanceof ICdtVariable) {
				ICdtVariable x0 = (ICdtVariable)a0;
				ICdtVariable x1 = (ICdtVariable)a1;
				String s0 = x0.getName();
				if (s0 == null)
					s0 = AbstractPage.EMPTY_STR;
				String s1 = x1.getName();
				if (s1 == null)
					s1 = AbstractPage.EMPTY_STR;
				return(s0.compareTo(s1));
			} else 
				return 0;
		}
	}
	

}

Back to the top