Beispiel #1
0
  /**
   * Creates an input dialog instance. The dialog remains invisible until {@link #open()} is called.
   *
   * @param parent The parent shell.
   * @param title The window title for this input dialog.
   * @param msg The message to display on this input dialog.
   * @param defaultValue The default value for the input field.
   */
  public InputDialog(Shell parent, String title, String msg, String defaultValue) {
    shell = new Shell(parent, SWT.PRIMARY_MODAL | SWT.DIALOG_TRIM | SWT.RESIZE);
    shell.setText(title);

    Label label = new Label(shell, SWT.NONE);
    text = new Combo(shell, SWT.BORDER | SWT.SINGLE);
    Util.selectAllOnFocus(text);
    Label fillerLabel = new Label(shell, SWT.NONE);
    Label separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Button okBt = new Button(shell, SWT.PUSH);
    Button cancelBt = new Button(shell, SWT.PUSH);

    label.setText(msg);
    text.setText(defaultValue);
    text.setSelection(new Point(0, text.getText().length()));
    okBt.setText(AppUtil.Messages.ok.get());
    cancelBt.setText(AppUtil.Messages.cancel.get());
    Button[] okCancelBts = Util.maybeSwapButtons(okBt, cancelBt);

    shell.setLayout(Util.createFormLayout(5));
    FormDataFactory fdf = FormDataFactory.getInstance();
    fdf.top().left().right().applyTo(label);
    fdf.top(label).applyTo(text);
    fdf.reset().minWidth(Util.BTW).bottom().right().applyTo(okCancelBts[1]);
    fdf.right(okCancelBts[1]).applyTo(okCancelBts[0]);
    fdf.reset().left().right().bottom(okCancelBts[1]).applyTo(separator);
    fdf.top(text).bottom(separator).applyTo(fillerLabel);

    okBt.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            answer = text.getText();
            shell.close();
          }
        });

    cancelBt.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            shell.close();
          }
        });

    text.addKeyListener(
        new KeyAdapter() {
          public void keyReleased(KeyEvent e) {
            if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
              answer = text.getText();
              shell.close();
            }
          }
        });
  }