diff options
Diffstat (limited to 'plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoProfileSetting.java')
| -rw-r--r-- | plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoProfileSetting.java | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoProfileSetting.java b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoProfileSetting.java new file mode 100644 index 0000000..f56fea4 --- /dev/null +++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoProfileSetting.java | |||
| @@ -0,0 +1,245 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2012 BMW Car IT GmbH. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the Eclipse Public License v1.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.eclipse.org/legal/epl-v10.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * BMW Car IT - initial API and implementation | ||
| 10 | *******************************************************************************/ | ||
| 11 | package org.yocto.sdk.ide; | ||
| 12 | |||
| 13 | import org.eclipse.jface.dialogs.IDialogConstants; | ||
| 14 | import org.eclipse.jface.dialogs.InputDialog; | ||
| 15 | import org.eclipse.jface.dialogs.MessageDialog; | ||
| 16 | import org.eclipse.jface.preference.PreferencePage; | ||
| 17 | import org.eclipse.swt.SWT; | ||
| 18 | import org.eclipse.swt.events.MouseAdapter; | ||
| 19 | import org.eclipse.swt.events.MouseEvent; | ||
| 20 | import org.eclipse.swt.layout.GridData; | ||
| 21 | import org.eclipse.swt.layout.GridLayout; | ||
| 22 | import org.eclipse.swt.widgets.Button; | ||
| 23 | import org.eclipse.swt.widgets.Combo; | ||
| 24 | import org.eclipse.swt.widgets.Composite; | ||
| 25 | import org.eclipse.swt.widgets.Event; | ||
| 26 | import org.eclipse.swt.widgets.Group; | ||
| 27 | import org.eclipse.swt.widgets.Listener; | ||
| 28 | import org.yocto.sdk.ide.preferences.PreferenceConstants; | ||
| 29 | import org.yocto.sdk.ide.preferences.ProfileNameInputValidator; | ||
| 30 | import org.yocto.sdk.ide.preferences.YoctoSDKPreferencePage; | ||
| 31 | import org.yocto.sdk.ide.preferences.YoctoSDKProjectPropertyPage; | ||
| 32 | |||
| 33 | public class YoctoProfileSetting { | ||
| 34 | private static final String PROFILES_TITLE = "Preferences.Profiles.Title"; | ||
| 35 | private static final String NEW_PROFILE_TITLE = "Preferences.Profile.New.Title"; | ||
| 36 | private static final String RENAME_PROFILE_TITLE = "Preferences.Profile.Rename.Title"; | ||
| 37 | private static final String RENAME_DIALOG_TITLE = "Preferences.Profile.Rename.Dialog.Title"; | ||
| 38 | private static final String RENAME_DIALOG_MESSAGE = "Preferences.Profile.Rename.Dialog.Message"; | ||
| 39 | private static final String REMOVE_PROFILE_TITLE = "Preferences.Profile.Remove.Title"; | ||
| 40 | private static final String REMOVE_DIALOG_TITLE = "Preferences.Profile.Remove.Dialog.Title"; | ||
| 41 | private static final String REMOVE_DIALOG_MESSAGE = "Preferences.Profile.Remove.Dialog.Message"; | ||
| 42 | private static final String MODIFY_STANDARD_TITLE = "Preferences.Profile.Standard.Modification.Title"; | ||
| 43 | private static final String MODIFY_STANDARD_MESSAGE = "Preferences.Profile.Standard.Modification.Message"; | ||
| 44 | |||
| 45 | private Combo sdkConfigsCombo; | ||
| 46 | private Button btnConfigRename; | ||
| 47 | private Button btnConfigRemove; | ||
| 48 | private Button btnConfigSaveAs; | ||
| 49 | |||
| 50 | private YoctoProfileElement profileElement; | ||
| 51 | private PreferencePage preferencePage; | ||
| 52 | private final boolean editable; | ||
| 53 | |||
| 54 | public YoctoProfileSetting(YoctoProfileElement profileElement, PreferencePage preferencePage, final boolean editable) { | ||
| 55 | this.profileElement = profileElement; | ||
| 56 | this.preferencePage = preferencePage; | ||
| 57 | this.editable = editable; | ||
| 58 | } | ||
| 59 | |||
| 60 | public void createComposite(Composite composite) { | ||
| 61 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | ||
| 62 | GridLayout layout = new GridLayout(2, false); | ||
| 63 | |||
| 64 | Group storeYoctoConfigurationsGroup = new Group (composite, SWT.NONE); | ||
| 65 | layout = new GridLayout(1, false); | ||
| 66 | if (isEditable()) { | ||
| 67 | layout.numColumns = 3; | ||
| 68 | } | ||
| 69 | |||
| 70 | storeYoctoConfigurationsGroup.setLayout(layout); | ||
| 71 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | ||
| 72 | gd.horizontalSpan = 2; | ||
| 73 | storeYoctoConfigurationsGroup.setLayoutData(gd); | ||
| 74 | storeYoctoConfigurationsGroup.setText(YoctoSDKMessages.getString(PROFILES_TITLE)); | ||
| 75 | |||
| 76 | sdkConfigsCombo = new Combo(storeYoctoConfigurationsGroup, SWT.READ_ONLY); | ||
| 77 | addConfigs(sdkConfigsCombo); | ||
| 78 | sdkConfigsCombo.select(sdkConfigsCombo.indexOf(profileElement.getSelectedProfile())); | ||
| 79 | sdkConfigsCombo.setLayout(new GridLayout(2, false)); | ||
| 80 | sdkConfigsCombo.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, false)); | ||
| 81 | |||
| 82 | Listener selectionListener = new Listener() { | ||
| 83 | @Override | ||
| 84 | public void handleEvent(Event event) { | ||
| 85 | Object source = event.widget; | ||
| 86 | if (!(source instanceof Combo)) { | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | Combo sdkCombo = (Combo) source; | ||
| 91 | if (sdkCombo.getSelectionIndex() < 0) { | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | String selectedItem = sdkCombo.getItem(sdkCombo.getSelectionIndex()); | ||
| 96 | profileElement.setSelectedProfile(selectedItem); | ||
| 97 | |||
| 98 | if (preferencePage instanceof YoctoSDKPreferencePage) { | ||
| 99 | ((YoctoSDKPreferencePage) preferencePage).switchProfile(selectedItem); | ||
| 100 | } else if (preferencePage instanceof YoctoSDKProjectPropertyPage) { | ||
| 101 | ((YoctoSDKProjectPropertyPage) preferencePage).switchProfile(selectedItem); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | }; | ||
| 105 | |||
| 106 | sdkConfigsCombo.addListener(SWT.Selection, selectionListener); | ||
| 107 | sdkConfigsCombo.addListener(SWT.Modify, selectionListener); | ||
| 108 | |||
| 109 | if (isEditable()) { | ||
| 110 | createSaveAsProfileButton(storeYoctoConfigurationsGroup); | ||
| 111 | createRenameButton(storeYoctoConfigurationsGroup); | ||
| 112 | createRemoveButton(storeYoctoConfigurationsGroup); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | private void createSaveAsProfileButton(Group storeYoctoConfigurationsGroup) { | ||
| 117 | btnConfigSaveAs = new Button(storeYoctoConfigurationsGroup, SWT.PUSH | SWT.LEAD); | ||
| 118 | btnConfigSaveAs.setText(YoctoSDKMessages.getString(NEW_PROFILE_TITLE)); | ||
| 119 | btnConfigSaveAs.addMouseListener(new MouseAdapter() { | ||
| 120 | @Override | ||
| 121 | public void mouseDown(MouseEvent e) { | ||
| 122 | if (preferencePage instanceof YoctoSDKPreferencePage) { | ||
| 123 | ((YoctoSDKPreferencePage) preferencePage).performSaveAs(); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | }); | ||
| 127 | } | ||
| 128 | |||
| 129 | private void createRemoveButton(Group storeYoctoConfigurationsGroup) { | ||
| 130 | btnConfigRemove = new Button(storeYoctoConfigurationsGroup, SWT.PUSH | SWT.LEAD); | ||
| 131 | btnConfigRemove.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, false, 3, 1)); | ||
| 132 | btnConfigRemove.setText(YoctoSDKMessages.getString(REMOVE_PROFILE_TITLE)); | ||
| 133 | btnConfigRemove.addMouseListener(new MouseAdapter() { | ||
| 134 | @Override | ||
| 135 | public void mouseDown(MouseEvent e) { | ||
| 136 | saveChangesOnCurrentProfile(); | ||
| 137 | int selectionIndex = sdkConfigsCombo.getSelectionIndex(); | ||
| 138 | String selectedItem = sdkConfigsCombo.getItem(selectionIndex); | ||
| 139 | |||
| 140 | if (selectedItem.equals(PreferenceConstants.STANDARD_PROFILE_NAME)) { | ||
| 141 | MessageDialog.openInformation(null, | ||
| 142 | YoctoSDKMessages.getString(MODIFY_STANDARD_TITLE), | ||
| 143 | YoctoSDKMessages.getString(MODIFY_STANDARD_MESSAGE)); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | boolean deleteConfirmed = | ||
| 148 | MessageDialog.openConfirm(null, | ||
| 149 | YoctoSDKMessages.getString(REMOVE_DIALOG_TITLE), | ||
| 150 | YoctoSDKMessages.getFormattedString(REMOVE_DIALOG_MESSAGE, selectedItem)); | ||
| 151 | |||
| 152 | if (!deleteConfirmed) { | ||
| 153 | return; | ||
| 154 | } | ||
| 155 | |||
| 156 | sdkConfigsCombo.select(0); | ||
| 157 | sdkConfigsCombo.remove(selectionIndex); | ||
| 158 | profileElement.remove(selectedItem); | ||
| 159 | |||
| 160 | if (preferencePage instanceof YoctoSDKPreferencePage) { | ||
| 161 | ((YoctoSDKPreferencePage) preferencePage).deleteProfile(selectedItem); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | }); | ||
| 165 | } | ||
| 166 | |||
| 167 | private void createRenameButton(Group storeYoctoConfigurationsGroup) { | ||
| 168 | btnConfigRename = new Button(storeYoctoConfigurationsGroup, SWT.PUSH | SWT.LEAD); | ||
| 169 | btnConfigRename.setText(YoctoSDKMessages.getString(RENAME_PROFILE_TITLE)); | ||
| 170 | btnConfigRename.addMouseListener(new MouseAdapter() { | ||
| 171 | @Override | ||
| 172 | public void mouseDown(MouseEvent e) { | ||
| 173 | saveChangesOnCurrentProfile(); | ||
| 174 | int selectedIndex = sdkConfigsCombo.getSelectionIndex(); | ||
| 175 | final String selectedItem = sdkConfigsCombo.getItem(selectedIndex); | ||
| 176 | |||
| 177 | if (selectedItem.equals(PreferenceConstants.STANDARD_PROFILE_NAME)) { | ||
| 178 | MessageDialog.openInformation(null, | ||
| 179 | YoctoSDKMessages.getString(MODIFY_STANDARD_TITLE), | ||
| 180 | YoctoSDKMessages.getString(MODIFY_STANDARD_MESSAGE)); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | InputDialog profileNameDialog = | ||
| 185 | new InputDialog(null, | ||
| 186 | YoctoSDKMessages.getString(RENAME_DIALOG_TITLE), | ||
| 187 | YoctoSDKMessages.getString(RENAME_DIALOG_MESSAGE), | ||
| 188 | null, | ||
| 189 | new ProfileNameInputValidator(profileElement, selectedItem)); | ||
| 190 | |||
| 191 | int returnCode = profileNameDialog.open(); | ||
| 192 | if (returnCode == IDialogConstants.CANCEL_ID) { | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | String newProfileName = profileNameDialog.getValue(); | ||
| 197 | profileElement.rename(selectedItem, profileNameDialog.getValue()); | ||
| 198 | |||
| 199 | if (preferencePage instanceof YoctoSDKPreferencePage) { | ||
| 200 | ((YoctoSDKPreferencePage) preferencePage).renameProfile(selectedItem, newProfileName); | ||
| 201 | } | ||
| 202 | |||
| 203 | sdkConfigsCombo.setItem(selectedIndex, newProfileName); | ||
| 204 | sdkConfigsCombo.select(selectedIndex); | ||
| 205 | } | ||
| 206 | }); | ||
| 207 | } | ||
| 208 | |||
| 209 | private void saveChangesOnCurrentProfile() { | ||
| 210 | preferencePage.performOk(); | ||
| 211 | } | ||
| 212 | |||
| 213 | private void addConfigs(Combo combo) { | ||
| 214 | for (String profile : profileElement.getProfiles()) { | ||
| 215 | combo.add(profile); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | public void addProfile(String profileName) { | ||
| 220 | int index = sdkConfigsCombo.getItemCount(); | ||
| 221 | sdkConfigsCombo.add(profileName, index); | ||
| 222 | sdkConfigsCombo.select(index); | ||
| 223 | } | ||
| 224 | |||
| 225 | public void setUIFormEnabledState(boolean isEnabled) { | ||
| 226 | setButtonsEnabledState(isEnabled); | ||
| 227 | sdkConfigsCombo.setEnabled(isEnabled); | ||
| 228 | } | ||
| 229 | |||
| 230 | public YoctoProfileElement getCurrentInput() { | ||
| 231 | return profileElement; | ||
| 232 | } | ||
| 233 | |||
| 234 | public void setButtonsEnabledState(boolean isEnabled) { | ||
| 235 | if (isEditable()) { | ||
| 236 | btnConfigRename.setEnabled(isEnabled); | ||
| 237 | btnConfigRemove.setEnabled(isEnabled); | ||
| 238 | btnConfigSaveAs.setEnabled(isEnabled); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | private boolean isEditable() { | ||
| 243 | return editable; | ||
| 244 | } | ||
| 245 | } | ||
