Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d60dd9e17d620c78f202643fdf48c2f673aea686 (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 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.pde.internal.ui.shared.target;

import org.eclipse.pde.core.target.ITargetLocation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.ui.StringVariableSelectionDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.pde.internal.core.target.ProfileBundleContainer;
import org.eclipse.pde.internal.ui.IHelpContextIds;
import org.eclipse.pde.internal.ui.SWTFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.PlatformUI;

/**
 * Wizard page for creating a profile (installation) bundle container.
 * 
 * @see AddBundleContainerWizard
 * @see AddBundleContainerSelectionPage
 * @see ITargetLocation
 */
public class EditProfileContainerPage extends EditDirectoryContainerPage {

	private Button fUseDefaultConfig;
	private Label fConfigLabel;
	private Combo fConfigLocation;
	private Button fConfigBrowse;
	private Button fConfigVariables;

	/**
	 * Dialog settings key for the most recent config location
	 */
	private static final String SETTINGS_CONFIG_1 = "config1"; //$NON-NLS-1$

	/**
	 * Dialog settings key for the second most recent config location
	 */
	private static final String SETTINGS_CONFIG_2 = "config2"; //$NON-NLS-1$

	/**
	 * Dialog settings key for the third most recent config location
	 */
	private static final String SETTINGS_CONFIG_3 = "config3"; //$NON-NLS-1$

	public EditProfileContainerPage() {
		super(null, "EditProfileContainer"); //$NON-NLS-1$
	}

	public EditProfileContainerPage(ITargetLocation container) {
		super(container, "EditProfileContainer"); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.EditDirectoryContainerPage#getDefaultTitle()
	 */
	protected String getDefaultTitle() {
		if (fContainer != null) {
			return Messages.EditProfileContainerPage_3;
		}
		return Messages.AddProfileContainerPage_0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.EditDirectoryContainerPage#getDefaultMessage()
	 */
	protected String getDefaultMessage() {
		return Messages.AddProfileContainerPage_1;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.AddDirectoryContainerPage#createLocationArea(org.eclipse.swt.widgets.Composite)
	 */
	protected void createLocationArea(Composite parent) {
		super.createLocationArea(parent);

		Composite configComp = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_HORIZONTAL, 0, 0);

		if (fContainer == null) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IHelpContextIds.LOCATION_ADD_INSTALLATION_WIZARD);
		} else {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IHelpContextIds.LOCATION_EDIT_INSTALLATION_WIZARD);
		}

		fUseDefaultConfig = new Button(configComp, SWT.CHECK | SWT.RIGHT);
		GridData gd = new GridData();
		gd.horizontalSpan = 2;
		fUseDefaultConfig.setLayoutData(gd);
		fUseDefaultConfig.setFont(parent.getFont());
		fUseDefaultConfig.setText(Messages.AddProfileContainerPage_2);
		fUseDefaultConfig.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				updateConfigEnablement();
				containerChanged(0);
			}
		});

		fConfigLabel = SWTFactory.createLabel(configComp, Messages.AddProfileContainerPage_3, 1);
		((GridData) fConfigLabel.getLayoutData()).horizontalIndent = 15;

		fConfigLocation = SWTFactory.createCombo(configComp, SWT.BORDER, 1, getConfigComboItems());
		fConfigLocation.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				// If the text is a combo item, immediately try to resolve, otherwise wait in case they type more
				boolean isItem = false;
				String[] items = fConfigLocation.getItems();
				for (int i = 0; i < items.length; i++) {
					if (fConfigLocation.getText().equals(items[i])) {
						isItem = true;
						break;
					}
				}
				containerChanged(isItem ? 0 : TYPING_DELAY);
			}
		});

		Composite buttonComp = SWTFactory.createComposite(configComp, 2, 2, GridData.CENTER, 0, 0);
		gd = (GridData) buttonComp.getLayoutData();
		gd.horizontalAlignment = SWT.RIGHT;

		fConfigBrowse = SWTFactory.createPushButton(buttonComp, Messages.AddProfileContainerPage_4, null);
		fConfigBrowse.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(getShell());
				dialog.setFilterPath(fConfigLocation.getText());
				dialog.setText(Messages.AddProfileContainerPage_5);
				dialog.setMessage(Messages.AddProfileContainerPage_6);
				String result = dialog.open();
				if (result != null)
					fConfigLocation.setText(result);
			}
		});

		fConfigVariables = SWTFactory.createPushButton(buttonComp, Messages.EditProfileContainerPage_1, null);
		fConfigVariables.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
				dialog.open();
				String variable = dialog.getVariableExpression();
				if (variable != null) {
					fConfigLocation.setText(fConfigLocation.getText() + variable);
				}
			}
		});

	}

	private String[] getConfigComboItems() {
		List<String> previousConfigs = new ArrayList<String>(4);
		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			String location = settings.get(SETTINGS_CONFIG_1);
			if (location != null) {
				previousConfigs.add(location);
			}
			location = settings.get(SETTINGS_CONFIG_2);
			if (location != null) {
				previousConfigs.add(location);
			}
			location = settings.get(SETTINGS_CONFIG_3);
			if (location != null) {
				previousConfigs.add(location);
			}
		}
		previousConfigs.add("${eclipse_home}/configuration"); //$NON-NLS-1$
		return previousConfigs.toArray(new String[previousConfigs.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.EditDirectoryContainerPage#storeSettings()
	 */
	public void storeSettings() {
		super.storeSettings();
		if (fConfigLocation.isEnabled()) {
			String newLocation = fConfigLocation.getText().trim();
			String[] items = fConfigLocation.getItems();
			for (int i = 0; i < items.length; i++) {
				if (items[i].equals(newLocation)) {
					// Already have this location stored
					return;
				}
			}
			IDialogSettings settings = getDialogSettings();
			if (settings != null) {
				String location = settings.get(SETTINGS_CONFIG_2);
				if (location != null) {
					settings.put(SETTINGS_CONFIG_3, location);
				}
				location = settings.get(SETTINGS_CONFIG_1);
				if (location != null) {
					settings.put(SETTINGS_CONFIG_2, location);
				}
				settings.put(SETTINGS_CONFIG_1, newLocation);
			}
		}

	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.EditDirectoryContainerPage#initializeInputFields(org.eclipse.pde.core.target.ITargetLocation)
	 */
	protected void initializeInputFields(ITargetLocation container) {
		if (container instanceof ProfileBundleContainer) {
			String configLocation = ((ProfileBundleContainer) container).getConfigurationLocation();
			if (configLocation == null) {
				fUseDefaultConfig.setSelection(true);
				fConfigLocation.setText(""); //$NON-NLS-1$
			} else {
				fUseDefaultConfig.setSelection(false);
				fConfigLocation.setText(configLocation);
			}
		} else {
			fUseDefaultConfig.setSelection(true);
			fConfigLocation.setText(""); //$NON-NLS-1$
		}
		updateConfigEnablement();
		// Call super last as it will update the bundle tree
		super.initializeInputFields(container);
	}

	private void updateConfigEnablement() {
		boolean isDefault = fUseDefaultConfig.getSelection();
		fConfigLabel.setEnabled(!isDefault);
		fConfigLocation.setEnabled(!isDefault);
		fConfigBrowse.setEnabled(!isDefault);
		fConfigVariables.setEnabled(!isDefault);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.EditDirectoryContainerPage#validateInput()
	 */
	protected boolean validateInput() {
		boolean valid = super.validateInput();
		if (valid) {
			if (fConfigLocation.isEnabled()) {
				// Check if the text field is blank
				if (fConfigLocation.getText().trim().length() == 0) {
					setMessage(Messages.EditProfileContainerPage_2);
					return false;
				}

				// Resolve any variables
				String locationString = null;
				try {
					locationString = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(fConfigLocation.getText().trim());
				} catch (CoreException e) {
					setMessage(e.getMessage(), IMessageProvider.WARNING);
					return true;
				}
				File configLocation = new File(locationString);

				// Check that the directory exists
				if (!configLocation.isDirectory()) {
					setMessage(Messages.AddProfileContainerPage_8, IMessageProvider.WARNING);
				} else {
					setMessage(getDefaultMessage());
				}
			}
		}
		return valid;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.internal.ui.shared.target.EditDirectoryContainerPage#createContainer(org.eclipse.pde.core.target.ITargetLocation)
	 */
	protected ITargetLocation createContainer(ITargetLocation previous) throws CoreException {
		return getTargetPlatformService().newProfileLocation(fInstallLocation.getText(), fConfigLocation.isEnabled() ? fConfigLocation.getText() : null);
	}

}

Back to the top