Skip to main content
summaryrefslogtreecommitdiffstats
blob: d9e20810b0cd661517e4de610a6e8ac28d1c0c3c (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
/*******************************************************************************
 *  Copyright (c) 2007, 2010 IBM 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.xlc.ui.properties;

import org.eclipse.cdt.managedbuilder.ui.properties.BuildOptionComboFieldEditor;
import org.eclipse.cdt.managedbuilder.xlc.ui.Messages;
import org.eclipse.cdt.managedbuilder.xlc.ui.XLCUIPlugin;
import org.eclipse.cdt.managedbuilder.xlc.ui.preferences.PreferenceConstants;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPropertyPage;

public class XLCompilerPropertyPage extends FieldEditorPreferencePage implements IWorkbenchPropertyPage {

	protected String originalMessage;
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
	 */
	protected void createFieldEditors() {
		createPathEditor();
		createVersionEditor();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors
	 * ()
	 */
	protected void createPathEditor() {

		Composite parent = getFieldEditorParent();

		fPathEditor = new DirectoryFieldEditor(PreferenceConstants.P_XL_COMPILER_ROOT,
				Messages.XLCompilerPropertyPage_0, parent) {
			protected boolean doCheckState() {
				// always return true, as we don't want to fail cases when
				// compiler is installed remotely
				// just warn user
				if (!super.doCheckState()) {
					setMessage(Messages.XLCompilerPropertyPage_2, IMessageProvider.WARNING);
				} else {
					setMessage(originalMessage);
				}

				return true;
			}

			protected boolean checkState() {
				return doCheckState();
			}

		};

		addField(fPathEditor);

		IProject project = ((IResource) (getElement().getAdapter(IResource.class))).getProject();

		String currentPath = null;

		try {
			currentPath = project.getPersistentProperty(new QualifiedName("", //$NON-NLS-1$
					PreferenceConstants.P_XL_COMPILER_ROOT));
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (currentPath == null) {
			// if the property isn't set, then use the workbench preference
			IPreferenceStore prefStore = XLCUIPlugin.getDefault().getPreferenceStore();
			currentPath = prefStore.getString(PreferenceConstants.P_XL_COMPILER_ROOT);
		}

		fPathEditor.setStringValue(currentPath);

	}

	protected void createVersionEditor() {

		IProject project = ((IResource) (getElement().getAdapter(IResource.class))).getProject();
		String[] versionEntries = { PreferenceConstants.P_XL_COMPILER_VERSION_8_NAME,
				PreferenceConstants.P_XL_COMPILER_VERSION_9_NAME, PreferenceConstants.P_XL_COMPILER_VERSION_10_NAME };

		Composite versionParent = getFieldEditorParent();

		fVersionEditor = new BuildOptionComboFieldEditor(PreferenceConstants.P_XLC_COMPILER_VERSION,
				Messages.XLCompilerPropertyPage_1, versionEntries, null, versionParent);

		addField(fVersionEditor);

		String currentVersion = null;
		try {
			currentVersion = project.getPersistentProperty(new QualifiedName("", //$NON-NLS-1$
					PreferenceConstants.P_XLC_COMPILER_VERSION));
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (currentVersion == null) {
			// if the property isn't set, then use the workbench preference
			IPreferenceStore prefStore = XLCUIPlugin.getDefault().getPreferenceStore();
			currentVersion = prefStore.getString(PreferenceConstants.P_XLC_COMPILER_VERSION);
		}

		Combo versionCombo = fVersionEditor.getComboControl(versionParent);
		versionCombo.setText(PreferenceConstants.getVersionLabel(currentVersion));

	}

	protected DirectoryFieldEditor fPathEditor;

	protected BuildOptionComboFieldEditor fVersionEditor;

	// private Composite parent;

	/**
	 * Constructor for SamplePropertyPage.
	 */
	public XLCompilerPropertyPage() {
		super(FieldEditorPreferencePage.FLAT);

		originalMessage = getMessage();
	}

	protected void performDefaults() {
		// default to whatever is set on the workbench preference
		IPreferenceStore prefStore = XLCUIPlugin.getDefault().getPreferenceStore();
		String currentPath = prefStore.getString(PreferenceConstants.P_XL_COMPILER_ROOT);
		String currentVersion = prefStore.getString(PreferenceConstants.P_XLC_COMPILER_VERSION);
		String currentVersionLabel = PreferenceConstants.getVersionLabel(currentVersion);
		if (fPathEditor != null) {
			fPathEditor.setStringValue(currentPath);
		}

		fVersionEditor.getComboControl(getFieldEditorParent()).setText(currentVersionLabel);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk()
	 */
	public boolean performOk() {
		// store the value in the owner text field
		try {
			IProject project = ((IResource) (getElement().getAdapter(IResource.class))).getProject();

			if (fPathEditor != null) {
				project.setPersistentProperty(new QualifiedName("", //$NON-NLS-1$
						PreferenceConstants.P_XL_COMPILER_ROOT), fPathEditor.getStringValue());
			}
			String version = null;
			if (fVersionEditor.getSelection() != null) {
				version = PreferenceConstants.getVersion(fVersionEditor.getSelection());

				project.setPersistentProperty(new QualifiedName("", //$NON-NLS-1$
						PreferenceConstants.P_XLC_COMPILER_VERSION), version);
			}
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

	/**
	 * The element.
	 */
	private IAdaptable element;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
	 */
	public IAdaptable getElement() {
		return element;
	}

	/**
	 * Sets the element that owns properties shown on this page.
	 * 
	 * @param element
	 *            the element
	 */
	public void setElement(IAdaptable element) {
		this.element = element;
	}

}

Back to the top