summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoProfileSetting.java
blob: f56fea4617df615b9b1a593ca610406424a32cf6 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
 * Copyright (c) 2012 BMW Car IT GmbH.
 * 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:
 * BMW Car IT - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.ide;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.yocto.sdk.ide.preferences.PreferenceConstants;
import org.yocto.sdk.ide.preferences.ProfileNameInputValidator;
import org.yocto.sdk.ide.preferences.YoctoSDKPreferencePage;
import org.yocto.sdk.ide.preferences.YoctoSDKProjectPropertyPage;

public class YoctoProfileSetting {
	private static final String PROFILES_TITLE = "Preferences.Profiles.Title";
	private static final String NEW_PROFILE_TITLE = "Preferences.Profile.New.Title";
	private static final String RENAME_PROFILE_TITLE = "Preferences.Profile.Rename.Title";
	private static final String RENAME_DIALOG_TITLE = "Preferences.Profile.Rename.Dialog.Title";
	private static final String RENAME_DIALOG_MESSAGE = "Preferences.Profile.Rename.Dialog.Message";
	private static final String REMOVE_PROFILE_TITLE = "Preferences.Profile.Remove.Title";
	private static final String REMOVE_DIALOG_TITLE = "Preferences.Profile.Remove.Dialog.Title";
	private static final String REMOVE_DIALOG_MESSAGE = "Preferences.Profile.Remove.Dialog.Message";
	private static final String MODIFY_STANDARD_TITLE = "Preferences.Profile.Standard.Modification.Title";
	private static final String MODIFY_STANDARD_MESSAGE = "Preferences.Profile.Standard.Modification.Message";

	private Combo sdkConfigsCombo;
	private Button btnConfigRename;
	private Button btnConfigRemove;
	private Button btnConfigSaveAs;

	private YoctoProfileElement profileElement;
	private PreferencePage preferencePage;
	private final boolean editable;

	public YoctoProfileSetting(YoctoProfileElement profileElement, PreferencePage preferencePage, final boolean editable) {
		this.profileElement = profileElement;
		this.preferencePage = preferencePage;
		this.editable = editable;
	}

	public void createComposite(Composite composite) {
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		GridLayout layout = new GridLayout(2, false);

		Group storeYoctoConfigurationsGroup = new Group (composite, SWT.NONE);
		layout = new GridLayout(1, false);
		if (isEditable()) {
			layout.numColumns = 3;
		}

		storeYoctoConfigurationsGroup.setLayout(layout);
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd.horizontalSpan = 2;
		storeYoctoConfigurationsGroup.setLayoutData(gd);
		storeYoctoConfigurationsGroup.setText(YoctoSDKMessages.getString(PROFILES_TITLE));

		sdkConfigsCombo = new Combo(storeYoctoConfigurationsGroup, SWT.READ_ONLY);
		addConfigs(sdkConfigsCombo);
		sdkConfigsCombo.select(sdkConfigsCombo.indexOf(profileElement.getSelectedProfile()));
		sdkConfigsCombo.setLayout(new GridLayout(2, false));
		sdkConfigsCombo.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, false));

		Listener selectionListener = new Listener() {
			@Override
			public void handleEvent(Event event) {
				Object source = event.widget;
				if (!(source instanceof Combo)) {
					return;
				}

				Combo sdkCombo = (Combo) source;
				if (sdkCombo.getSelectionIndex() < 0) {
					return;
				}

				String selectedItem = sdkCombo.getItem(sdkCombo.getSelectionIndex());
				profileElement.setSelectedProfile(selectedItem);

				if (preferencePage instanceof YoctoSDKPreferencePage) {
					((YoctoSDKPreferencePage) preferencePage).switchProfile(selectedItem);
				} else if (preferencePage instanceof YoctoSDKProjectPropertyPage) {
					((YoctoSDKProjectPropertyPage) preferencePage).switchProfile(selectedItem);
				}
			}
		};

		sdkConfigsCombo.addListener(SWT.Selection, selectionListener);
		sdkConfigsCombo.addListener(SWT.Modify, selectionListener);

		if (isEditable()) {
			createSaveAsProfileButton(storeYoctoConfigurationsGroup);
			createRenameButton(storeYoctoConfigurationsGroup);
			createRemoveButton(storeYoctoConfigurationsGroup);
		}
	}

	private void createSaveAsProfileButton(Group storeYoctoConfigurationsGroup) {
		btnConfigSaveAs = new Button(storeYoctoConfigurationsGroup, SWT.PUSH | SWT.LEAD);
		btnConfigSaveAs.setText(YoctoSDKMessages.getString(NEW_PROFILE_TITLE));
		btnConfigSaveAs.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (preferencePage instanceof YoctoSDKPreferencePage) {
					((YoctoSDKPreferencePage) preferencePage).performSaveAs();
				}
			}
		});
	}

	private void createRemoveButton(Group storeYoctoConfigurationsGroup) {
		btnConfigRemove = new Button(storeYoctoConfigurationsGroup, SWT.PUSH | SWT.LEAD);
		btnConfigRemove.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, false, 3, 1));
		btnConfigRemove.setText(YoctoSDKMessages.getString(REMOVE_PROFILE_TITLE));
		btnConfigRemove.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				saveChangesOnCurrentProfile();
				int selectionIndex = sdkConfigsCombo.getSelectionIndex();
				String selectedItem = sdkConfigsCombo.getItem(selectionIndex);

				if (selectedItem.equals(PreferenceConstants.STANDARD_PROFILE_NAME)) {
					MessageDialog.openInformation(null,
													YoctoSDKMessages.getString(MODIFY_STANDARD_TITLE),
													YoctoSDKMessages.getString(MODIFY_STANDARD_MESSAGE));
					return;
				}

				boolean deleteConfirmed =
						MessageDialog.openConfirm(null,
													YoctoSDKMessages.getString(REMOVE_DIALOG_TITLE),
													YoctoSDKMessages.getFormattedString(REMOVE_DIALOG_MESSAGE, selectedItem));

				if (!deleteConfirmed) {
					return;
				}

				sdkConfigsCombo.select(0);
				sdkConfigsCombo.remove(selectionIndex);
				profileElement.remove(selectedItem);

				if (preferencePage instanceof YoctoSDKPreferencePage) {
					((YoctoSDKPreferencePage) preferencePage).deleteProfile(selectedItem);
				}
			}
		});
	}

	private void createRenameButton(Group storeYoctoConfigurationsGroup) {
		btnConfigRename = new Button(storeYoctoConfigurationsGroup, SWT.PUSH | SWT.LEAD);
		btnConfigRename.setText(YoctoSDKMessages.getString(RENAME_PROFILE_TITLE));
		btnConfigRename.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				saveChangesOnCurrentProfile();
				int selectedIndex = sdkConfigsCombo.getSelectionIndex();
				final String selectedItem = sdkConfigsCombo.getItem(selectedIndex);

				if (selectedItem.equals(PreferenceConstants.STANDARD_PROFILE_NAME)) {
					MessageDialog.openInformation(null,
							YoctoSDKMessages.getString(MODIFY_STANDARD_TITLE),
							YoctoSDKMessages.getString(MODIFY_STANDARD_MESSAGE));
					return;
				}

				InputDialog profileNameDialog =
						new InputDialog(null,
										YoctoSDKMessages.getString(RENAME_DIALOG_TITLE),
										YoctoSDKMessages.getString(RENAME_DIALOG_MESSAGE),
										null,
										new ProfileNameInputValidator(profileElement, selectedItem));

				int returnCode = profileNameDialog.open();
				if (returnCode == IDialogConstants.CANCEL_ID) {
					return;
				}

				String newProfileName = profileNameDialog.getValue();
				profileElement.rename(selectedItem, profileNameDialog.getValue());

				if (preferencePage instanceof YoctoSDKPreferencePage) {
					((YoctoSDKPreferencePage) preferencePage).renameProfile(selectedItem, newProfileName);
				}

				sdkConfigsCombo.setItem(selectedIndex, newProfileName);
				sdkConfigsCombo.select(selectedIndex);
			}
		});
	}

	private void saveChangesOnCurrentProfile() {
		preferencePage.performOk();
	}

	private void addConfigs(Combo combo) {
		for (String profile : profileElement.getProfiles()) {
			combo.add(profile);
		}
	}

	public void addProfile(String profileName) {
		int index = sdkConfigsCombo.getItemCount();
		sdkConfigsCombo.add(profileName, index);
		sdkConfigsCombo.select(index);
	}

	public void setUIFormEnabledState(boolean isEnabled) {
		setButtonsEnabledState(isEnabled);
		sdkConfigsCombo.setEnabled(isEnabled);
	}

	public YoctoProfileElement getCurrentInput() {
		return profileElement;
	}

	public void setButtonsEnabledState(boolean isEnabled) {
		if (isEditable()) {
			btnConfigRename.setEnabled(isEnabled);
			btnConfigRemove.setEnabled(isEnabled);
			btnConfigSaveAs.setEnabled(isEnabled);
		}
	}

	private boolean isEditable() {
		return editable;
	}
}