<更新記録>
2007年 12月 29日
執筆

姉妹サイト検索 Web検索


InputDialog

InputDialog

InputDialogクラスは、1行の文字列入力のダイアログボックスの作成を行います

InputDialog(
	Shell parentShell,
	String dialogTitle,
	String dialogMessage,
	String initialValue,
	IInputValidator validator
)
initialValie引数には、ダイアログ表示時のデフォルトのテキストを指定します。 validator引数には、IInputValidatorオブジェクトを指定します。

interface IInputValidator {
	String isValid(String newText)
}
IInputValidator実装クラスは、isValidメソッドを実装します。 isValidメソッドは、newText引数が、許可する文字列かどうかを判断するメソッドです。 newTextで渡された文字列が許可できるテキストならばnullを、許可できないテキストならばエラーメッセージを返します。

InputDialogDemonstrate.java
import java.util.regex.Pattern;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class InputDialogDemonstrate extends ApplicationWindow {

	private InputDialogDemonstrate() {
		super(null);
	}
	
	final IInputValidator validator = new IInputValidator() {
		public String isValid(String newText) {
			if (newText.length() < 4) {
				return "4文字以上入力して下さい。";
			} else if (newText.length() > 8) {
				return "8文字以内で入力して下さい。";
			} else if (!Pattern.matches("[0-9A-Za-z]*", newText)) {
				return "半角英数字しか入力できません。";
			} else {
				return null;
			}
		}
	};
	
	protected Control createContents(Composite parent) {
		
		Button inputDlgOpenner = new Button(parent, SWT.PUSH);
		inputDlgOpenner.setText("open input dialog");
		inputDlgOpenner.pack();
		
		inputDlgOpenner.addSelectionListener(new SelectionAdapter() {
			
			public void widgetSelected(SelectionEvent e) {
				InputDialog inputDialog = new InputDialog(
						getShell(),
						"入力",
						"半角英数字を入力して下さい。",
						null,
						validator
				);
				
				inputDialog.open();
				String value = inputDialog.getValue();
			}
		});
		
		return parent;
	}
	
	public static void main(String[] args) {
		Window w = new InputDialogDemonstrate();
		w.setBlockOnOpen(true);
		w.open();
		Display.getCurrent().dispose();
	}
}


Powered by VeryEasyCMS