aboutsummaryrefslogtreecommitdiffstats
blob: a83fe70bacc795e1c205aa4056ff110c0dc0e0aa (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation
 * 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:
 *    Otavio Busatto Pontes <obusatto@br.ibm.com> - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.tools.launch.ui.properties;

import java.util.LinkedList;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.linuxtools.tools.launch.core.LaunchCoreConstants;
import org.eclipse.linuxtools.tools.launch.core.properties.LinuxtoolsPathProperty;
import org.eclipse.linuxtools.tools.launch.ui.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.dialogs.PropertyPage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
 * <p>
 * Preference page and property page implementation for configuring the Linuxtools Path property.
 * </p>
 *
 * <p>
 * In this property page, it is possible to change the PATH used to run LinuxTools commands.
 * This is necessary for users that want to use tools not located in system PATH.
 * With this page you can, for example, have 2 different versions of valgrind installed in your
 * system and select which one will be used to profile your application.
 * </p>
 *
 * @author Otavio Pontes
 */
public class LinuxtoolsPathPropertyPage extends PropertyPage {
	public static final String LINUXTOOLS_PATH_COMBO_NAME = LaunchCoreConstants.PLUGIN_ID + ".LinuxtoolsPathCombo"; //$NON-NLS-1$
	private static final String LINUXTOOLS_PATH_EXT_POINT = "LinuxtoolsPathOptions"; //$NON-NLS-1$
	private static final String LINUXTOOLS_PATH_OPTION = "option"; //$NON-NLS-1$
	private static final String LINUXTOOLS_PATH_OPTION_NAME = "name"; //$NON-NLS-1$
	private static final String LINUXTOOLS_PATH_OPTION_PATH = "path"; //$NON-NLS-1$

	private static final String[][] DEFAULT_PATHS= {
				{"Custom", ""}, //$NON-NLS-1$  //$NON-NLS-2$
	};
	private StringFieldEditor linuxtoolsPath;
	private ComboFieldEditor linuxtoolsPathCombo;
	private IAdaptable element = null;
	private Composite result;
	private Button systemEnvButton, customButton;
	private boolean customSelected;

	private String [][]fillPaths() {
		LinkedList<String[]> list = new LinkedList<String[]>();
		for (String[] t : DEFAULT_PATHS)
			list.add(t);

		IExtensionPoint extPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchCoreConstants.PLUGIN_ID, LINUXTOOLS_PATH_EXT_POINT);
		IConfigurationElement[] configs = extPoint.getConfigurationElements();
		for (IConfigurationElement config : configs)
			if (config.getName().equals(LINUXTOOLS_PATH_OPTION)) {
				String path = config.getAttribute(LINUXTOOLS_PATH_OPTION_PATH);
				String name = config.getAttribute(LINUXTOOLS_PATH_OPTION_NAME);
				list.add(new String[]{name, path});
			}
		return list.toArray(new String[0][0]);
	}

	@Override
	protected Control createContents(Composite parent) {
		initializeDialogUnits(parent);

		result= new Composite(parent, SWT.NONE);
		GridLayout layout= new GridLayout();
		layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth= 0;
		layout.numColumns= 1;
		result.setLayout(layout);
		String paths[][] = fillPaths();

		//defaults
		getPreferenceStore().setDefault(LaunchCoreConstants.LINUXTOOLS_PATH_SYSTEM_NAME, LinuxtoolsPathProperty.getInstance().getLinuxtoolsPathSystemDefault());
		getPreferenceStore().setDefault(LINUXTOOLS_PATH_COMBO_NAME, LinuxtoolsPathProperty.getInstance().getLinuxtoolsPathDefault());

		// Add radio buttons
		Composite radios = new Composite(result, SWT.NONE);
		GridLayout layoutRadios= new GridLayout();
		layoutRadios.marginWidth= 0;
		layoutRadios.numColumns= 1;
		radios.setLayout(layoutRadios);
		Composite space = new Composite(result, SWT.NONE);

		boolean systemPathSelected = getPreferenceStore().getBoolean(LaunchCoreConstants.LINUXTOOLS_PATH_SYSTEM_NAME);
		systemEnvButton = new Button(radios, SWT.RADIO);
		systemEnvButton.setText(Messages.LINUXTOOLS_PATH_SYSTEM_ENV);
		systemEnvButton.setSelection(systemPathSelected);
		systemEnvButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					updateOptionsEnable();
				}
		});

		customButton = new Button(radios, SWT.RADIO);
		customButton.setText(Messages.LINUXTOOLS_PATH_CUSTOM);
		customButton.setToolTipText(Messages.LINUXTOOLS_PATH_CUSTOM_TOOLTIP);
		customButton.setSelection(!systemPathSelected);

		//Add combo box
		linuxtoolsPathCombo = new ComboFieldEditor(
									LINUXTOOLS_PATH_COMBO_NAME,
									Messages.LINUXTOOLS_PATH_COMBO,
									paths,
									result);
		linuxtoolsPathCombo.setPage(this);
		linuxtoolsPathCombo.setPreferenceStore(getPreferenceStore());
		linuxtoolsPathCombo.load();
		linuxtoolsPathCombo.setPropertyChangeListener(new IPropertyChangeListener (){
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				customSelected = event.getNewValue().toString().equals(""); //$NON-NLS-1$
				if (!customSelected)
					linuxtoolsPath.setStringValue(event.getNewValue().toString());
				updateOptionsEnable();
			}
		});

		//Add textbox
		linuxtoolsPath = new StringFieldEditor(
				LaunchCoreConstants.LINUXTOOLS_PATH_NAME,
				Messages.LINUXTOOLS_PATH, result);

		linuxtoolsPath.setPage(this);
		linuxtoolsPath.setPreferenceStore(getPreferenceStore());
		linuxtoolsPath.getTextControl(result).setToolTipText(Messages.LINUXTOOLS_PATH_TOOLTIP);

		String selected = getPreferenceStore().getString(LINUXTOOLS_PATH_COMBO_NAME);
		customSelected = selected.equals(""); //$NON-NLS-1$
		getPreferenceStore().setDefault(LaunchCoreConstants.LINUXTOOLS_PATH_NAME, LinuxtoolsPathProperty.getInstance().getLinuxtoolsPathDefault());
		linuxtoolsPath.load();

		Dialog.applyDialogFont(result);
		updateOptionsEnable();
		return result;
	}

	private void updateOptionsEnable() {
		if (systemEnvButton.getSelection()) {
			linuxtoolsPath.setEnabled(false, result);
			linuxtoolsPathCombo.setEnabled(false, result);
		} else {
			linuxtoolsPath.setEnabled(customSelected, result);
			linuxtoolsPathCombo.setEnabled(true, result);
		}
	}

	@Override
	protected void performDefaults() {
		linuxtoolsPath.loadDefault();
		linuxtoolsPathCombo.loadDefault();
		customButton.setSelection(!LinuxtoolsPathProperty.getInstance().getLinuxtoolsPathSystemDefault());
		systemEnvButton.setSelection(LinuxtoolsPathProperty.getInstance().getLinuxtoolsPathSystemDefault());
		updateOptionsEnable();
	}

	@Override
	public boolean performOk() {
		linuxtoolsPath.store();
		linuxtoolsPathCombo.store();
		getPreferenceStore().setValue(LaunchCoreConstants.LINUXTOOLS_PATH_SYSTEM_NAME, systemEnvButton.getSelection());
		return super.performOk();
	}

	@Override
	protected void performApply() {
		linuxtoolsPath.store();
		linuxtoolsPathCombo.store();
		getPreferenceStore().setValue(LaunchCoreConstants.LINUXTOOLS_PATH_SYSTEM_NAME, systemEnvButton.getSelection());
		super.performApply();
	}

	/**
	 * Receives the object that owns the properties shown in this property page.
	 *
	 * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
	 */
	@Override
	public void setElement(IAdaptable element) {
		this.element = element;
		IAdaptable e = getElement();
		if (e != null) {
			setPreferenceStore(new ScopedPreferenceStore(
						new ProjectScope((IProject) e),
						LaunchCoreConstants.PLUGIN_ID));
		}
	}

	/**
	 * Delivers the object that owns the properties shown in this property page.
	 *
	 * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
	 */
	@Override
	public IAdaptable getElement() {
		if (element == null)
			return element;
		if (!(element instanceof IProject))
			return (IAdaptable) element.getAdapter(IProject.class);
		return element;
	}

}