Troubleshooting guide
133
7: Creating connections
try {
int type, offset, count;
String value;
_dout.writeInt(JUST_OPEN);
_dout.flush();
for (;;) {
type = _din.readInt();
if (type == INSERT) {
offset = _din.readInt();
value = _din.readUTF();
insert(value, offset);
} else if (type == REMOVE) {
offset = _din.readInt();
count = _din.readInt();
remove(offset, count);
} else if (type == JUST_OPEN) {
// Send contents to desktop.
value = _infoField.getText();
if (value == null || value.equals(““)) {
_dout.writeInt(NO_CONTENTS);
_dout.flush();
} else {
_dout.writeInt(CONTENTS);
_dout.writeUTF(_infoField.getText());
_dout.flush();
}
} else if (type == CONTENTS) {
String contents = _din.readUTF();
synchronized(Application.getEventLock()) {
_infoField.setText(contents);
}
} else if (type == NO_CONTENTS) {
} else {
throw new RuntimeException();
}
}
} catch(IOException ioe) {
invokeLater(new Runnable() {
public void run() {
Dialog.alert(“Problems reading from or writing to serial port.”);
closePort();
System.exit(1);
}
});
}
}
}
private void insert(final String msg, final int offset) {
invokeLater(new Runnable() {
public void run() {
_infoField.setCursorPosition(offset);
_infoField.insert(msg);
}