This issue is caused in the following method, when the closeEditor is called with the flag "false", which indicates "do not save the changes":
There are a few flaws with the above fix. I will discuss these flaws in terms of various scenarios:
Scenario 1
The Fix Introduces a New Bug
1. User creates a new server and double click on the server to open the server editor:
2. The user make some modification to the server configurations in the server editor. For example, in this scenario they will change the port numbers, which will turn "isDirty" flag of the server to "true":
3. The user deletes the server. It will not matter whether the user puts a checkmark to the "delete unused server configurations" or not:
4. Since the server configurations were modified without saving, the user gets prompted to either save the changes or not:
4a. If the user selects "NO" to discard the changes, everything will be fine. The server will get deleted and removed from the servers list, and the server editor gets closed.
4b. If the user selects "Cancel", the server will get deleted and removed from the servers list, but the server editor stays opened. I argue that the existence of the "Cancel" button is unnecessary because it cannot undo the deletion (more on this matter comes shortly), as well as it will introduce the original bug: deletion of a server will not close the editor.
4c. If the user selects "YES" to save the changes, another bug is introduced: a new server with the same configuration will get created and will be added to the servers list. I believe this behavior is caused because saving the changes will update the server configuration, which in turn will trigger some method(s) in the application to update the server list and creating a new server:
Scenario 2
The Fix Has Logical Flaw
The user creates a new server, and they will make some modifications to the server configurations in the server editor (without saving them).
Then the user decides to delete the server and will get prompted with the following dialog box:
The user puts a checkmark to "delete the unused configurations" , which means they want to delete the actual files on the computer.
After clicking on OK button to confirm the deletion, the user gets prompted again to whether they want to save or discard the changes in the server editor. Although it might not be a big deal, bug we have a logical flaw in this case. If the user wants to delete the server configuration, they will not care about saving the changes. Therefore, there is no need to prompt the user again.
Reason Behind the Issues in Both Scenarios
The main reason is that the server gets deleted and removed from the servers list before the server editor gets closed. Examining the code shows the "server" instance gets de-registered and removed from the "Servers List" before closing the server editor. That's why when the there are unsaved modifications in the server editor, the application behave oddly according to the user's selection as explained in above scenarios.
Initial Fix
My initial fix was to close the server editor (and in case of unsaved modifications, asking the user to either save them or discard them) before deleting the server. After examining the code, I realized doing this fix will require modification of many codes, changing many methods signatures, and introducing strong coupling between some classes from different packages. Besides, the logical flaw that I talked about will still exist. For example, if the user decides to delete the server including the configuration files, they still gets prompted to save the modified configurations, which is un-necessary since the user intends to completely deletes the configuration files.
Proposed Solution
Introduction
The DeleteServerDialog.Java in the org.eclipse.wst.server.ui.internal package, is the class that gets called when the user tries to delete a server. This class dynamically add checkboxes in the dialog box GUI according to the server's status. For example, if the server is running, and the user tries to delete the server, two checkboxes will be added dynamically at the runtime to the GUI:
The code responsible for this dynamic creation of checkboxes is located in the protected Control createCustomArea(Composite parent) method:
The code responsible for deleting the server is located in the protected void buttonPressed(int buttonId) method:
Solution
I propose that to fix the issues with all above scenarios, dynamically add a checkbox to the delete dialog box seeking the user's decision regarding the modified configurations in the server editor. I say "dynamically" because if there is no modifications in the server editor, there is no need to display that checkbox:
Then the application can either save or discard the changes made to the configurations based on the user's choice.
To prevent the logic flaw, I recommend that this checkbox will be disabled/enabled based on the selection of "Delete unused server configuration(s)". Again, it is because if the user wants to delete the server configurations, there is no need to save them.
The main advantage of this solution is that there is no need to make several modifications in the various methods/classes. Another advantage is that we don't need to show 2 dialog boxes to the user: one of deletion confirmation and another one for saving/discarding changes.
Solution Implementation
The first step is to add a checkbox dynamically to the dialog box GUI. This can be done with the following code in the protected Control createCustomArea(Composite parent) method of DeleteServerDialog.Java:
Moreover, the following code currently exist in the protected void buttonPressed(int buttonId) method of DeleteServerDialog.Java that is responsible for deleting the server (and if checked, deleting the server configurations):
Now we can modify this code to determine if we have to either save or delete the configurations based on the user's selection of the checkboxes: