Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to prompt a custom dialog when close editor and application
How to prompt a custom dialog when close editor and application [message #546747] Wed, 14 July 2010 05:45
yuanyun.ken is currently offline yuanyun.kenFriend
Messages: 25
Registered: July 2009
Junior Member
Hi, all:
I am developing a rcp application, every editor represents a connection, when
user closes one editor, I want to ask whether user wants to exit this editor.
When user closes the application, if there are multiple editors/connections
opened, then I would like to show a dialog listing all opened editors/connections,
and ask user whether he wants to close all, and exit the application.

I tried two ways, but no one works as I expected.
1. I tried to override editor's method: public void doSave(IProgressMonitor progressMonitor). But this way, it(in SaveablesList.promptForSaving) prompts a dialog, the message is:
WorkbenchMessages.EditorManager_saveChangesQuestion = ''{0}'' has been modified. Save changes?
This is not I want, I tried to copy the bundle org.eclipse.ui.internal/messages.properties to my rcp application, and change the text, but it doesn't work.
So is there a way to customize this message?

2. The editor implements ISaveablePart2, and provide method: public int promptToSaveOnClose().
In this method, I would prompt a dialog to ask whether user wants to close the connection.

I also overwrite preWindowShellClose method in WorkbenchWindowAdvisor.
It will prompt a dialog to list all connections, and ask whether user wants to close all and exit, if user click yes, it will close all connections and editors.
The code is as following:
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
{
	public boolean preWindowShellClose() {
		// prompt a dialog to let user close all connections
		super.preWindowShellClose();

		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getShell();
		if (!ConnectionManager.getInstance()
				.getAllOpenedConnections().isEmpty()) {
			CloseAllConnectionsDialog closeAllDlg = new CloseAllConnectionsDialog(
					shell);
			int result = closeAllDlg.open();
			return result == IDialogConstants.YES_ID;
		} else {
			boolean confirmed = MessageDialog.openConfirm(shell,
					"Exit application",
					"Are you sure you want to exit the application?");
			return confirmed;
		}
	}
}

public class CloseAllConnectionsDialog extends TitleAreaDialog {
	protected void buttonPressed(int buttonId) {
		super.buttonPressed(buttonId);
		setReturnCode(buttonId);
		if (buttonId == IDialogConstants.YES_ID) {
			// at first, I call closeAllOpenedConnections, it
			// will fire event, and every editor or view will correspond, and
			// close itself
			// ConnectionManager.getInstance()
			// .closeAllOpenedConnections();

			// but it doesn't work, so I close editor manually here, but ei
			Collection openedCollections = ConnectionManager.getInstance()
					.getAllOpenedConnections();
			Iterator iterator = openedCollections.iterator();
			while (iterator.hasNext()) {
				Connection connection = (Connection) iterator
						.next();
				ConnectionManager.getInstance().removeAndCloseConnection(
						connection.getId());
				CommandEditor editor = (CommandEditor) PlatformUI
						.getWorkbench()
						.getActiveWorkbenchWindow()
						.getActivePage()
						.findEditor(
								new CommandEditorInput(connection));
				editor.close(false);
			}
		}
		close();
	}
}

After user clicks yes in the popup CloseAllConnectionsDialog, all editors are closed.
But the problem is that, the editor's promptToSaveOnClose still functions, for each editor, it still prompt a dialog to ask whether user wants to close this connection.

Anybody has any suggestion about how to achieve the requirement:
1. when close one editor, prompt a customary dialog to ask whether user wants to close this connection.
2. when exit application, if there are editor/connection opened, prompt a customary dialog to let user close all connections and exit.

Many thanks for any help in advance: )
Previous Topic:Tap into eclipse preferences page
Next Topic:How to remove toolbar when Console view is firstly created.
Goto Forum:
  


Current Time: Sat Dec 20 22:24:10 GMT 2014

Powered by FUDForum. Page generated in 0.01735 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software