MessageBox and the return values

11:31 PM / Comments (3) / by Kevin Vu

Good news for me since Peter Liu is looking into my bug :). His knowledge and experience is sure going to help speeding up the progress of this bug :). Anyway, This post is to answer his email and to clear up my first walk through a bit.

My first walk through ends at the saveALl() method which is belong to class: EditorManager.class (package: org.eclipse.ui.internal;)


Method: saveAll()
Package: org.eclipse.ui.internal

// Use a simpler dialog if there's only one
if (modelsToSave.size() == 1) {
Saveable model = (Saveable) modelsToSave.get(0);
String message = NLS.bind(WorkbenchMessages.EditorManager_saveChangesQuestion, model.getName());
// Show a dialog.
String[] buttons = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
MessageDialog d = new MessageDialog(
shellProvider.getShell(), WorkbenchMessages.Save_Resource,
null, message, MessageDialog.QUESTION, buttons, 0);


int choice = SaveableHelper.testGetAutomatedResponse();
if (SaveableHelper.testGetAutomatedResponse() == SaveableHelper.USER_RESPONSE) {
choice = d.open();
}

// Branch on the user choice.
// The choice id is based on the order of button labels
// above.
switch (choice) {
case ISaveablePart2.YES: // yes
break;
case ISaveablePart2.NO: // no
return true;
default:
case ISaveablePart2.CANCEL: // cancel
return false;
}
}


From the blocks of codes above:
MessageDialog d = new MessageDialog(...);
will define the Save Resource dialog

choice=d.open()
will display the dialog and get the return values when user click YES,NO,or CANCEL.

METHOD THAT DEFINE THE MESSAGEDIALOG:

package org.eclipse.jface.dialogs;
MessageDialog.class


public MessageDialog(Shell parentShell, String dialogTitle,
Image dialogTitleImage, String dialogMessage, int dialogImageType,
String[] dialogButtonLabels, int defaultIndex) {
super(parentShell);
this.title = dialogTitle;
this.titleImage = dialogTitleImage;
this.message = dialogMessage;

switch (dialogImageType) {
case ERROR: {
this.image = getErrorImage();
break;
}
case INFORMATION: {
this.image = getInfoImage();
break;
}
case QUESTION: {
this.image = getQuestionImage();
break;
}
case WARNING: {
this.image = getWarningImage();
break;
}
}
this.buttonLabels = dialogButtonLabels;
this.defaultButtonIndex = defaultIndex;
}


METHOD THAT DISPLAY AND GET THE RETURN VALUE:

package org.eclipse.jface.window;
Window.class

public int open() {

if (shell == null || shell.isDisposed()) {
shell = null;
// create the window
create();
}

// limit the shell size to the display size
constrainShellSize();

// open the window
shell.open();

// run the event loop if specified
if (block) {
runEventLoop(shell);
}

return returnCode;
}

The runEvenLoop(shell) will display and wait until user select one of the button (YES, NO, CANCEL).
The returnCode will return the value that user selected. I tried with YES, NO, CANCEL and the return values are:
YES: returnCode=0
NO: returnCode=1
CANCEL: returnCode=2


GO back to the saveAll() method.

switch (choice) {
case ISaveablePart2.YES: // yes
break;
case ISaveablePart2.NO: // no
return true;
default:
case ISaveablePart2.CANCEL: // cancel
return false;
}

choice will have the return value of open() method above.
If choice=YES, the saveAll() will process on and save all changes (code below), go back out, and server is launched with new configuration.
if choice is NO or CANCEL, saveAll() wont save and just return true or false, go back out, and server is launched with old configuration.


CODES THAT SAVE THE SERVER EDITOR belong to saveAll() method above.

// Create save block.
final List finalModels = modelsToSave;
IRunnableWithProgress progressOp = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) {
IProgressMonitor monitorWrap = new EventLoopProgressMonitor(
monitor);
monitorWrap.beginTask("", finalModels.size()); //$NON-NLS-1$
for (Iterator i = finalModels.iterator(); i.hasNext();) {
Saveable model = (Saveable) i.next();
// handle case where this model got saved as a result of saving another
if (!model.isDirty()) {
monitor.worked(1);
continue;
}
SaveableHelper.doSaveModel(model, new SubProgressMonitor(monitorWrap, 1), shellProvider, closing || confirm);
if (monitorWrap.isCanceled()) {
break;
}
}
monitorWrap.done();
}
};

// Do the save.
return SaveableHelper.runProgressMonitorOperation(
WorkbenchMessages.Save_All, progressOp, runnableContext, shellProvider);

3 comments:

Anonymous @ March 25, 2009 at 10:00 AM

Hi, Kevin.

This helps. Based on this posting, have you posted a question to the webtools newsgroup AND Bugzilla?
Remember including the link to this posting. See if you will get feedback from the WTP developers.
I will let know you once I've noticed
something...

Peter.

Kevin Vu @ March 25, 2009 at 3:24 PM

hi Peter,

I posted a question to newsgroup the day before yesterday but haven't got the answer yet.

Anonymous @ March 25, 2009 at 8:25 PM

Hi, Kevin.

The heading for your question on
the newsgroup might be too general to
catch some attention. How about post it AGAIN and use the keywords from
your question and the bug #.
Include a link to this blog posting.
See if you'll get a reply sooner.

Peter.