[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform.swt] Re: Text#setText() causes SWT.Modify (SWT on MS Windows)

Setup a flag in your eventlistener which is set pior to calling setText if it is true skip the eventhandling code if it is false execute your code.

Regards
Stefan

Adam Pordzik wrote:
Hello,

I need to update an Observable every time a change occurs. So a added
a SWT.Modify listener wich updates this Observable and nofifies its
Observers about changes. But Since the class "Window" (see snippet
below) itself observes this Object,it will setText() the content of
Text, which in turn triggers a SWT.Modify again...

As you can see, this makes a perfect infinite loop. (from line 77)

With the optional argument to update() I can test whether the update
is emerged by itself and discard. But Problems begins as soon as I
have additional Text-Controls. Moreover, I don't now exactly which
other Controls activate such undesired Events.

The best solution I've found so far is to check just before an upcoming
setText(), whether it already contains the same value. But this still
calls handleEvent at least on time unnecessarily once per Control.

How do one handle this Problem superiorly?
Does this only hits MS Windows or any other supported platform as well?
Why is this behaviour disierd at all?

A

========================================================================

1 import java.util.Observable;
2 import java.util.Observer;
3
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.layout.FillLayout;
6 import org.eclipse.swt.widgets.Display;
7 import org.eclipse.swt.widgets.Event;
8 import org.eclipse.swt.widgets.Label;
9 import org.eclipse.swt.widgets.Listener;
10 import org.eclipse.swt.widgets.Shell;
11 import org.eclipse.swt.widgets.Text;
12
13 public class Dualist {
14
15 public static void main(String[] args) {
16 new Dualist().run();
17 }
18
19 private Display display;
20 private Window window1;
21 private Window window2;
22 private Value value;
23
24 public void run() {
25 display = new Display();
26
27 value = new Value();
28 window1 = new Window(display, value, "Window 1");
29 window2 = new Window(display, value, "Window 2");
30
31 while (display.getShells().length > 0) {
32 if (!display.readAndDispatch())
33 display.sleep();
34 }
35 display.dispose();
36 }
37
38 static class Value extends Observable {
39
40 private String string = "nix";
41
42 public void setValue(String string) {
43 this.string = string;
44 this.setChanged();
45 }
46
47 public String getValue() {
48 return string;
49 }
50 }
51
52 static class Window implements Observer, Listener {
53
54 private Shell shell;
55 private Label label;
56 private Text text;
57 private Value value;
58
59 public Window (Display display, Value val, String title) {
60 value = val;
61 shell = new Shell(display);
62 label = new Label(shell, SWT.NONE|SWT.WRAP);
63 text = new Text(shell, SWT.BORDER|SWT.MULTI);
64
65 value.addObserver(this);
66 text.addListener(SWT.Modify, this);
67 shell.addListener(SWT.Dispose, this);
68
69 shell.setText(title);
70 shell.setLayout(new FillLayout());
71 shell.pack();
72 shell.open();
73 }
74
75 public void update(Observable o, Object arg) {
76 label.setText(value.getValue());
77 // CAUTION!
78 if (!text.getText().equals(value.getValue()))
79 text.setText(value.getValue());
80 }
81
82 public void handleEvent(Event event) {
83 event.doit = false;
84 if (event.widget == text && event.type == SWT.Modify) {
85 value.setValue(text.getText());
86 value.notifyObservers(event);
87 } else if (event.widget == shell && event.type == SWT.Dispose) {
88 value.deleteObserver(this);
89 }
90 }
91 }
92 }