|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2000, 2018 IBM Corporation and others. |
| 2 | + * Copyright (c) 2000, 2026 IBM Corporation and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
15 | 15 |
|
16 | 16 | import java.text.MessageFormat; |
17 | 17 | import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
18 | 19 | import java.util.Collections; |
19 | 20 | import java.util.Iterator; |
20 | 21 | import java.util.List; |
|
31 | 32 | import org.eclipse.debug.internal.ui.SWTFactory; |
32 | 33 | import org.eclipse.debug.ui.DebugUITools; |
33 | 34 | import org.eclipse.debug.ui.IDebugUIConstants; |
| 35 | +import org.eclipse.jface.dialogs.IDialogConstants; |
34 | 36 | import org.eclipse.jface.dialogs.IDialogSettings; |
| 37 | +import org.eclipse.jface.dialogs.MessageDialog; |
35 | 38 | import org.eclipse.jface.dialogs.TrayDialog; |
36 | 39 | import org.eclipse.jface.viewers.IContentProvider; |
37 | 40 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
38 | 41 | import org.eclipse.jface.viewers.IStructuredContentProvider; |
39 | 42 | import org.eclipse.jface.viewers.IStructuredSelection; |
40 | 43 | import org.eclipse.jface.viewers.TableViewer; |
41 | 44 | import org.eclipse.jface.viewers.Viewer; |
| 45 | +import org.eclipse.jface.window.Window; |
42 | 46 | import org.eclipse.swt.SWT; |
43 | 47 | import org.eclipse.swt.events.KeyAdapter; |
44 | 48 | import org.eclipse.swt.events.KeyEvent; |
@@ -155,6 +159,57 @@ protected void handleAddConfigButtonSelected() { |
155 | 159 | } |
156 | 160 | } |
157 | 161 |
|
| 162 | + /** |
| 163 | + * Returns whether the favorites have been modified. |
| 164 | + * |
| 165 | + * @return whether there are unsaved changes |
| 166 | + */ |
| 167 | + private boolean isDirty() { |
| 168 | + return !Arrays.equals(getInitialFavorites(), getArray(getFavorites())); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + protected void cancelPressed() { |
| 173 | + if (confirmSaveBeforeClose()) { |
| 174 | + super.cancelPressed(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + protected void handleShellCloseEvent() { |
| 180 | + if (confirmSaveBeforeClose()) { |
| 181 | + super.handleShellCloseEvent(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Prompts to save changes before closing the dialog. |
| 187 | + * |
| 188 | + * @return whether the dialog should be closed |
| 189 | + */ |
| 190 | + private boolean confirmSaveBeforeClose() { |
| 191 | + if (!isDirty()) { |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + MessageDialog dialog = new MessageDialog(getShell(), |
| 196 | + LaunchConfigurationsMessages.FavoritesDialogPromptOnCloseTitle, null, |
| 197 | + LaunchConfigurationsMessages.FavoritesDialogPromptOnClose, MessageDialog.QUESTION, |
| 198 | + new String[] { LaunchConfigurationsMessages.FavoritesDialogPromptOnCloseSaveButton, |
| 199 | + IDialogConstants.CANCEL_LABEL }, |
| 200 | + 0); |
| 201 | + |
| 202 | + int option = dialog.open(); |
| 203 | + if (option == Window.OK) { |
| 204 | + saveFavorites(); |
| 205 | + return true; |
| 206 | + } |
| 207 | + if (option == Window.CANCEL) { |
| 208 | + return true; |
| 209 | + } |
| 210 | + return false; |
| 211 | + } |
| 212 | + |
158 | 213 | /** |
159 | 214 | * The 'remove favorites' button has been pressed |
160 | 215 | */ |
@@ -398,10 +453,7 @@ protected IStatus run(IProgressMonitor monitor) { |
398 | 453 | monitor.worked(1); |
399 | 454 | } |
400 | 455 |
|
401 | | - // update added favorites |
402 | | - Iterator<ILaunchConfiguration> favs = current.iterator(); |
403 | | - while (favs.hasNext()) { |
404 | | - ILaunchConfiguration configuration = favs.next(); |
| 456 | + for (ILaunchConfiguration configuration : current) { |
405 | 457 | try { |
406 | 458 | List<String> groups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List<String>) null); |
407 | 459 | if (groups == null) { |
|
0 commit comments