swtaddressbook.java

来自「java的swt图形程序」· Java 代码 · 共 1,335 行 · 第 1/3 页

JAVA
1,335
字号

    // Help Menu
    MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
    item.setText("Help_menu_title");
    Menu menu = new Menu(shell, SWT.DROP_DOWN);
    item.setMenu(menu);

    // Help -> About Text Editor
    MenuItem subItem = new MenuItem(menu, SWT.NULL);
    subItem.setText("About");
    subItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        MessageBox box = new MessageBox(shell, SWT.NONE);
        box.setText("About_1"
            + shell.getText());
        box.setMessage(shell.getText()
            + "About_2");
        box.open();
      }
    });
  }

  /**
   * To compare entries (rows) by the given column
   */
  private class RowComparator implements Comparator {
    private int column;

    /**
     * Constructs a RowComparator given the column index
     * 
     * @param col
     *            The index (starting at zero) of the column
     */
    public RowComparator(int col) {
      column = col;
    }

    /**
     * Compares two rows (type String[]) using the specified column entry.
     * 
     * @param obj1
     *            First row to compare
     * @param obj2
     *            Second row to compare
     * @return negative if obj1 less than obj2, positive if obj1 greater
     *         than obj2, and zero if equal.
     */
    public int compare(Object obj1, Object obj2) {
      String[] row1 = (String[]) obj1;
      String[] row2 = (String[]) obj2;

      return row1[column].compareTo(row2[column]);
    }
  }
}

/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
/**
 * SearchDialog is a simple class that uses <code>org.eclipse.swt</code>
 * libraries to implement a basic search dialog.
 */
class SearchDialog {

  Shell shell;

  Text searchText;

  Combo searchArea;

  Label searchAreaLabel;

  Button matchCase;

  Button matchWord;

  Button findButton;

  Button down;

  FindListener findHandler;

  /**
   * Class constructor that sets the parent shell and the table widget that
   * the dialog will search.
   * 
   * @param parent
   *            Shell The shell that is the parent of the dialog.
   */
  public SearchDialog(Shell parent) {
    shell = new Shell(parent, SWT.CLOSE | SWT.BORDER | SWT.TITLE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    shell.setLayout(layout);
    shell.setText("Search");
    shell.addShellListener(new ShellAdapter() {
      public void shellClosed(ShellEvent e) {
        // don't dispose of the shell, just hide it for later use
        e.doit = false;
        shell.setVisible(false);
      }
    });

    Label label = new Label(shell, SWT.LEFT);
    label.setText("Dialog_find_what");
    searchText = new Text(shell, SWT.BORDER);
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.widthHint = 200;
    searchText.setLayoutData(gridData);
    searchText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        boolean enableFind = (searchText.getCharCount() != 0);
        findButton.setEnabled(enableFind);
      }
    });

    searchAreaLabel = new Label(shell, SWT.LEFT);
    searchArea = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.widthHint = 200;
    searchArea.setLayoutData(gridData);

    matchCase = new Button(shell, SWT.CHECK);
    matchCase.setText("Dialog_match_case");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    matchCase.setLayoutData(gridData);

    matchWord = new Button(shell, SWT.CHECK);
    matchWord.setText("Dialog_match_word");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    matchWord.setLayoutData(gridData);

    Group direction = new Group(shell, SWT.NONE);
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    direction.setLayoutData(gridData);
    direction.setLayout(new FillLayout());
    direction.setText("Dialog_direction");

    Button up = new Button(direction, SWT.RADIO);
    up.setText("Dialog_dir_up");
    up.setSelection(false);

    down = new Button(direction, SWT.RADIO);
    down.setText("Dialog_dir_down");
    down.setSelection(true);

    Composite composite = new Composite(shell, SWT.NONE);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.horizontalSpan = 2;
    composite.setLayoutData(gridData);
    layout = new GridLayout();
    layout.numColumns = 2;
    layout.makeColumnsEqualWidth = true;
    composite.setLayout(layout);

    findButton = new Button(composite, SWT.PUSH);
    findButton.setText("Dialog_find");
    findButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
    findButton.setEnabled(false);
    findButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        if (!findHandler.find()) {
          MessageBox box = new MessageBox(shell, SWT.ICON_INFORMATION
              | SWT.OK | SWT.PRIMARY_MODAL);
          box.setText(shell.getText());
          box.setMessage("Cannot_find"
              + "\"" + searchText.getText() + "\"");
          box.open();
        }
      }
    });

    Button cancelButton = new Button(composite, SWT.PUSH);
    cancelButton.setText("Cancel");
    cancelButton.setLayoutData(new GridData(
        GridData.HORIZONTAL_ALIGN_BEGINNING));
    cancelButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        shell.setVisible(false);
      }
    });

    shell.pack();
  }

  public String getSearchAreaLabel(String label) {
    return searchAreaLabel.getText();
  }

  public String[] getsearchAreaNames() {
    return searchArea.getItems();
  }

  public boolean getMatchCase() {
    return matchCase.getSelection();
  }

  public boolean getMatchWord() {
    return matchWord.getSelection();
  }

  public String getSearchString() {
    return searchText.getText();
  }

  public boolean getSearchDown() {
    return down.getSelection();
  }

  public int getSelectedSearchArea() {
    return searchArea.getSelectionIndex();
  }

  public void open() {
    if (shell.isVisible()) {
      shell.setFocus();
    } else {
      shell.open();
    }
    searchText.setFocus();
  }

  public void setSearchAreaNames(String[] names) {
    for (int i = 0; i < names.length; i++) {
      searchArea.add(names[i]);
    }
    searchArea.select(0);
  }

  public void setSearchAreaLabel(String label) {
    searchAreaLabel.setText(label);
  }

  public void setMatchCase(boolean match) {
    matchCase.setSelection(match);
  }

  public void setMatchWord(boolean match) {
    matchWord.setSelection(match);
  }

  public void setSearchDown(boolean searchDown) {
    down.setSelection(searchDown);
  }

  public void setSearchString(String searchString) {
    searchText.setText(searchString);
  }

  public void setSelectedSearchArea(int index) {
    searchArea.select(index);
  }

  public void addFindListener(FindListener listener) {
    this.findHandler = listener;
  }

  public void removeFindListener(FindListener listener) {
    this.findHandler = null;
  }
}

/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
interface FindListener {

  public boolean find();

}

/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
/**
 * DataEntryDialog class uses <code>org.eclipse.swt</code> libraries to
 * implement a dialog that accepts basic personal information that is added to a
 * <code>Table</code> widget or edits a <code>TableItem</code> entry to
 * represent the entered data.
 */
class DataEntryDialog {

  Shell shell;

  String[] values;

  String[] labels;

  public DataEntryDialog(Shell parent) {
    shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
    shell.setLayout(new GridLayout());
  }

  private void addTextListener(final Text text) {
    text.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        Integer index = (Integer) (text.getData("index"));
        values[index.intValue()] = text.getText();
      }
    });
  }

  private void createControlButtons() {
    Composite composite = new Composite(shell, SWT.NULL);
    composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    composite.setLayout(layout);

    Button okButton = new Button(composite, SWT.PUSH);
    okButton.setText("OK");
    okButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        shell.close();
      }
    });

    Button cancelButton = new Button(composite, SWT.PUSH);
    cancelButton.setText("Cancel");
    cancelButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        values = null;
        shell.close();
      }
    });

    shell.setDefaultButton(okButton);
  }

  private void createTextWidgets() {
    if (labels == null)
      return;

    Composite composite = new Composite(shell, SWT.NULL);
    composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    composite.setLayout(layout);

    if (values == null)
      values = new String[labels.length];

    for (int i = 0; i < labels.length; i++) {
      Label label = new Label(composite, SWT.RIGHT);
      label.setText(labels[i]);
      Text text = new Text(composite, SWT.BORDER);
      GridData gridData = new GridData();
      gridData.widthHint = 400;
      text.setLayoutData(gridData);
      if (values[i] != null) {
        text.setText(values[i]);
      }
      text.setData("index", new Integer(i));
      addTextListener(text);
    }
  }

  public String[] getLabels() {
    return labels;
  }

  public String getTitle() {
    return shell.getText();
  }

  /**
   * Returns the contents of the <code>Text</code> widgets in the dialog in
   * a <code>String</code> array.
   * 
   * @return String[] The contents of the text widgets of the dialog. May
   *         return null if all text widgets are empty.
   */
  public String[] getValues() {
    return values;
  }

  /**
   * Opens the dialog in the given state. Sets <code>Text</code> widget
   * contents and dialog behaviour accordingly.
   * 
   * @param dialogState
   *            int The state the dialog should be opened in.
   */
  public String[] open() {
    createTextWidgets();
    createControlButtons();
    shell.pack();
    shell.open();
    Display display = shell.getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    return getValues();
  }

  public void setLabels(String[] labels) {
    this.labels = labels;
  }

  public void setTitle(String title) {
    shell.setText(title);
  }

  /**
   * Sets the values of the <code>Text</code> widgets of the dialog to the
   * values supplied in the parameter array.
   * 
   * @param itemInfo
   *            String[] The values to which the dialog contents will be set.
   */
  public void setValues(String[] itemInfo) {
    if (labels == null)
      return;

    if (values == null)
      values = new String[labels.length];

    int numItems = Math.min(values.length, itemInfo.length);
    for (int i = 0; i < numItems; i++) {
      values[i] = itemInfo[i];
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?