Skip to content

Commit 7f27314

Browse files
committed
Prompt to save changes when closing Organize Favorites dialog
Prompt users to save changes before closing the Organize Favorites dialog when the favorites list has been modified. This prevents accidentally losing changes made to the favorites list when the dialog is closed without pressing OK.
1 parent 29df77f commit 7f27314

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/FavoritesDialog.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2018 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,6 +15,7 @@
1515

1616
import java.text.MessageFormat;
1717
import java.util.ArrayList;
18+
import java.util.Arrays;
1819
import java.util.Collections;
1920
import java.util.Iterator;
2021
import java.util.List;
@@ -31,14 +32,17 @@
3132
import org.eclipse.debug.internal.ui.SWTFactory;
3233
import org.eclipse.debug.ui.DebugUITools;
3334
import org.eclipse.debug.ui.IDebugUIConstants;
35+
import org.eclipse.jface.dialogs.IDialogConstants;
3436
import org.eclipse.jface.dialogs.IDialogSettings;
37+
import org.eclipse.jface.dialogs.MessageDialog;
3538
import org.eclipse.jface.dialogs.TrayDialog;
3639
import org.eclipse.jface.viewers.IContentProvider;
3740
import org.eclipse.jface.viewers.ISelectionChangedListener;
3841
import org.eclipse.jface.viewers.IStructuredContentProvider;
3942
import org.eclipse.jface.viewers.IStructuredSelection;
4043
import org.eclipse.jface.viewers.TableViewer;
4144
import org.eclipse.jface.viewers.Viewer;
45+
import org.eclipse.jface.window.Window;
4246
import org.eclipse.swt.SWT;
4347
import org.eclipse.swt.events.KeyAdapter;
4448
import org.eclipse.swt.events.KeyEvent;
@@ -155,6 +159,57 @@ protected void handleAddConfigButtonSelected() {
155159
}
156160
}
157161

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+
158213
/**
159214
* The 'remove favorites' button has been pressed
160215
*/
@@ -398,10 +453,7 @@ protected IStatus run(IProgressMonitor monitor) {
398453
monitor.worked(1);
399454
}
400455

401-
// update added favorites
402-
Iterator<ILaunchConfiguration> favs = current.iterator();
403-
while (favs.hasNext()) {
404-
ILaunchConfiguration configuration = favs.next();
456+
for (ILaunchConfiguration configuration : current) {
405457
try {
406458
List<String> groups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List<String>) null);
407459
if (groups == null) {

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationsMessages.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,10 @@ public class LaunchConfigurationsMessages extends NLS {
319319

320320
public static String QuickGroupLaunchActionToolTip;
321321

322+
public static String FavoritesDialogPromptOnClose;
323+
324+
public static String FavoritesDialogPromptOnCloseTitle;
325+
326+
public static String FavoritesDialogPromptOnCloseSaveButton;
327+
322328
}

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchConfigurationsMessages.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ FavoritesDialog_6=Do&wn
258258
FavoritesDialog_7=Select &Launch Configurations:
259259
FavoritesDialog_0=Add {0} Favorites
260260
FavoritesDialog_8=Updating Favorites...
261+
FavoritesDialogPromptOnClose=Save changes to your favorite launches before closing?
262+
FavoritesDialogPromptOnCloseTitle=Save Changes
263+
FavoritesDialogPromptOnCloseSaveButton=Save
261264

262265
OrganizeFavoritesAction_0=Organize Fa&vorites...
263266
PerspectiveManager_12=Confirm Perspective Switch

0 commit comments

Comments
 (0)