実際のSWTリスナー以外に、コードを介してリスナーを無視する方法はありますか?
たとえば、SWTテキストウィジェットを実装するJavaプログラムがあり、ウィジェットには次のものがあります。
- SWT.Verifyリスナーを使用して、不要なテキスト入力を除外します。
- 有効な入力文字の正しい数を待機し、(setFocus()を使用して)フォーカスを次の有効なフィールドに自動的に設定し、タブ順の他のテキストウィジェットをスキップするようにModifyListenersを実行します。
- focusLost(FocusEvent)テキストウィジェットからフォーカスが失われるのを待って追加の入力検証を実行し、ユーザー入力に基づいてSQLクエリを実行するFocusListeners。
私が遭遇した問題は、テキストウィジェットをクリアすることです。ウィジェットの1つは「####-##」(4つの数字、ハイフン、次に2つの数字)の形式であり、SWTSnippetSnippet179の修正バージョンであるこのリスナーを実装しました。このテキストウィジェットの最初のテキストは「-」で、予想される形式に関してユーザーに視覚的なフィードバックを提供します。数字のみが受け入れ可能な入力であり、プログラムは適切なポイントでハイフンを自動的にスキップします。
/*
* This listener was adapted from the "verify input in a template (YYYY/MM/DD)" SWT Code
* Snippet (also known as Snippet179), from the Snippets page of the SWT Project.
* SWT Code Snippets can be found at:
* http://www.eclipse.org/swt/snippets/
*/
textBox.addListener(SWT.Verify, new Listener()
{
boolean ignore;
public void handleEvent(Event e)
{
if (ignore) return;
e.doit = false;
StringBuffer buffer = new StringBuffer(e.text);
char[] chars = new char[buffer.length()];
buffer.getChars(0, chars.length, chars, 0);
if (e.character == '\b')
{
for (int i = e.start; i < e.end; i++)
{
switch (i)
{
case 0: /* [x]xxx-xx */
case 1: /* x[x]xx-xx */
case 2: /* xx[x]x-xx */
case 3: /* xxx[x]-xx */
case 5: /* xxxx-[x]x */
case 6: /* xxxx-x[x] */
{
buffer.append(' ');
break;
}
case 4: /* xxxx[-]xx */
{
buffer.append('-');
break;
}
default:
return;
}
}
textBox.setSelection(e.start, e.start + buffer.length());
ignore = true;
textBox.insert(buffer.toString());
ignore = false;
textBox.setSelection(e.start, e.start);
return;
}
int start = e.start;
if (start > 6) return;
int index = 0;
for (int i = 0; i < chars.length; i++)
{
if (start + index == 4)
{
if (chars[i] == '-')
{
index++;
continue;
}
buffer.insert(index++, '-');
}
if (chars[i] < '0' || '9' < chars[i]) return;
index++;
}
String newText = buffer.toString();
int length = newText.length();
textBox.setSelection(e.start, e.start + length);
ignore = true;
textBox.insert(newText);
ignore = false;
/*
* After a valid key press, verifying if the input is completed
* and passing the cursor to the next text box.
*/
if (7 == textBox.getCaretPosition())
{
/*
* Attempting to change the text after receiving a known valid input that has no results (0000-00).
*/
if ("0000-00".equals(textBox.getText()))
{
// "0000-00" is the special "Erase Me" code for these text boxes.
ignore = true;
textBox.setText(" - ");
ignore = false;
}
// Changing focus to a different textBox by using "setFocus()" method.
differentTextBox.setFocus();
}
}
}
);
ご覧のとおり、コード内の別のポイントからこのテキストウィジェットをクリアするために私が理解した唯一の方法は、「0000-00」を割り当てることです。
textBox.setText("000000")
リスナーでその入力をチェックします。その入力を受け取ると、リスナーはテキストを「-」に戻します(4つのスペース、ハイフン、次に2つのスペース)。
このテキストウィジェットのスペースを解析するfocusLostリスナーもあり、不要なSQLクエリを回避するために、入力が無効な場合(つまり、スペースが含まれている場合)にすべてのフィールドをクリア/リセットします。
// Adding focus listener to textBox to wait for loss of focus to perform SQL statement.
textBox.addFocusListener(new FocusAdapter()
{
@Override
public void focusLost(FocusEvent evt)
{
// Get the contents of otherTextBox and textBox. (otherTextBox must be <= textBox)
String boxFour = otherTextBox.getText();
String boxFive = textBox.getText();
// If either text box has spaces in it, don't perform the search.
if (boxFour.contains(" ") || boxFive.contains(" "))
{
// Don't perform SQL statements. Debug statement.
System.out.println("Tray Position input contains spaces. Ignoring.");
//Make all previous results invisible, if any.
labels.setVisible(false);
differentTextBox.setText("");
labelResults.setVisible(false);
}
else
{
//... Perform SQL statement ...
}
}
}
);
わかった。多くの場合、このコードでSWT MessageBoxウィジェットを使用してユーザーと通信したり、入力を確認した後にテキストウィジェットを空の状態に戻したいと考えています。問題は、メッセージボックスがfocusLostイベントを作成しているように見え、.setText(string)メソッドを使用すると、テキストウィジェットに存在するSWT.Verifyリスナーの影響を受けることです。
コード内でこれらのリスナーを選択的に無視し、他のすべてのユーザー入力に対してそれらを存在させ続けることに関する提案はありますか?
よろしくお願いします。