[SWT] How to enable and disable windows correctly, if using dialogs?
Hi,
I am programming a configuration gui for a hardware simulator. There I
wrote some gui elements, which enable the user to set up some
parameters used by the simulator.
For example it is possible to enter values in textfields. I do a check,
if the entered value even is an int, and if not, I create a short error
message. For example like this:
textwidth.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
try {
width = Integer.parseInt(textwidth.getText());
} catch (NumberFormatException nfe){
if (!textwidth.getText().equals("")) {
width = 0;
ErrorHandling error = new ErrorHandling(
"Please enter numbers only!\r\nWidth is
reset to 0.",
selscomposite.getShell());
textwidth.setText("0");
}
}
//modify the scale so it represents the entered value
scalewidth.setSelection(scalewidth.getMaximum() - width
+ scalewidth.getMinimum());
}
});
textwidth is the textfield, representing the value chosen through a
scale and enabling the user to enter a value manually, and the
scale-representation is updated if done so.
ErrorHandling is the class I use throughout the project to display
small error messages. It looks like this:
public class ErrorHandling {
Shell error;
Shell parentshell;
public ErrorHandling(String errormessage, Shell shell) {
//shell displaying the errormessage
error = new Shell(SWT.TITLE | SWT.BORDER);
error.setText("User input error");
//the layout for the error shell
FillLayout filllayout = new FillLayout(SWT.VERTICAL);
filllayout.marginHeight = 5;
filllayout.marginWidth = 5;
filllayout.spacing = 3;
error.setLayout(filllayout);
//disable the parent shell
parentshell = shell;
parentshell.setEnabled(false);
//set the errormessage
CLabel errorlabelmessage = new CLabel(error, SWT.CENTER |
SWT.SHADOW_IN);
errorlabelmessage.setText(errormessage);
//a button to close the errormessage
Button errorreceived = new Button(error, SWT.PUSH |
SWT.CENTER);
errorreceived.setText("Ok, i got it!");
errorreceived.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
parentshell.setEnabled(true);
error.close();
}
});
//create the errorshell
error.setActive();
error.pack();
error.open();
}
}
parentshell is needed, so I can disable the shell, in which the error
occured until the user hast closed the errormessage by using the "Ok, I
got it!"-button. I don't even know if this is good style, but it's all
I know to do so far. I need to have a new shell-variable (e.g.
parentshell), because otherwise I could not use it in the inner class
within the listener-context. This leads to the first question: Is there
no better way to handle this, but the new Object "parentshell"?
The second problem with this implementation is, that if I do not use
the SWT.BORDER and SWT.TITLE constants within the initialization of the
error-Shell-object, the small window displaying the error message looks
real crappy. The title and a window border is needed, but if I have
those, I also have a close-button on the upper right of the window.
Because a disabled the parent shell:
parentshell = shell;
parentshell.setEnabled(false);
I have to enable it, if the error message is closed. I am able to
enable it, if the user choses the button, but if the user closes the
window by the close-button of the window-manager, the parent-shell
never ever gets accessible, because there never came a command to
enable it again.
A last few words: I tried several constants in the creation of the
error-Shell-object, but most of them (like ON_TOP, or several modal
state specifiers) only have the effect, that the error-message has no
border and no title any more. I am using Linux and the X-Server, so I
don't know what the behaviour of these constants would be on other
window-architectures.
Now this was a whole lot of information, but ask me, if you want to
know something more specific. Any comments are welcome, even those,
telling me what I have done is totally crap and I should do it like
this or that.
Thank you in advance!
PS: Sorry for any errors and the bad english.