1、选择java类的Dialog的使用
2、包选择的Dialog的使用
java 代码
- final IJavaSearchScope searchScope = SearchEngine
- .createWorkspaceScope();
- addButton.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- TypeSelectionDialog2 dialog = new TypeSelectionDialog2(
- getShell(), false,
- new ProgressMonitorDialog(getShell()), searchScope,
- IJavaSearchConstants.TYPE);
- dialog.setMessage("Select an Entity bean"); //$NON-NLS-1$
- dialog.setBlockOnOpen(true);
- dialog.setTitle("Type Selection");
- if (Dialog.OK == dialog.open()) {
- IType obj = (IType) dialog.getFirstResult();
- String className = obj.getResource().getName();
- String packageName = obj.getPackageFragment()
- .getElementName();
- list.add(packageName + "."+ className);
- }
- }
- });
java 代码
- browseButton.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- super.widgetSelected(e);
- IJavaProject javaProject = JavaCore.create(getProject(obj));
- SelectionDialog dialog = null;
- try {
- dialog = JavaUI
- .createPackageDialog(
- getShell(),
- javaProject,
- IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS);
- dialog.setTitle("Package Selection");
- dialog.setMessage("Choose a folder");
- } catch (JavaModelException e1) {
- // ExceptionHandler.handleExceptionAndAbort(e1);
- }
- if (dialog.open() != Window.OK) {
- return;
- }
- IPackageFragment pck = (IPackageFragment) dialog.getResult()[0];
- if (pck != null) {
- packageTxt.setText(pck.getElementName());
- }
- }
- });
3、自定义带有多选框的Dialog的创建及其使用
创建:
使用:
创建:
java 代码
- public class CheckboxSelectionDialog extends Dialog {
- class ContentProvider implements IStructuredContentProvider {
- public Object[] getElements(Object inputElement) {
- return new Object[] {};
- }
- public void dispose() {
- }
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
- }
- }
- private Table table;
- private CheckboxTableViewer ctv;
- private TableViewer tv;
- private java.util.List initialSelections;
- private String[] result;
- private String title;
- private String label;
- /**
- * Create the dialog
- *
- * @param parentShell
- */
- public CheckboxSelectionDialog (Shell parentShell,
- java.util.List initialSelections, String title, String label) {
- super(parentShell);
- this.initialSelections = initialSelections;
- this.title = title;
- this.label = label;
- }
- /**
- * Create contents of the dialog
- *
- * @param parent
- */
- protected Control createDialogArea(Composite parent) {
- Composite container = (Composite) super.createDialogArea(parent);
- final GridLayout gridLayout = new GridLayout();
- container.setLayout(gridLayout);
- final Label selectLabel = new Label(container, SWT.NONE);
- selectLabel.setLayoutData(new GridData());
- // selectLabel.setText("Select the ejb container.");
- selectLabel.setText(label);
- tv = new TableViewer(container, SWT.CHECK | SWT.MULTI | SWT.BORDER
- | SWT.FULL_SELECTION);
- // ctv = new CheckboxTableViewer(tv.getTable());
- tv.setContentProvider(new ContentProvider());
- tv.setInput(new Object());
- table = tv.getTable();
- table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- tv.add(initialSelections.toArray());
- //
- return container;
- }
- /**
- * Create contents of the button bar
- *
- * @param parent
- */
- protected void createButtonsForButtonBar(Composite parent) {
- final Button button = createButton(parent, IDialogConstants.OK_ID,
- IDialogConstants.OK_LABEL, true);
- if (initialSelections.size() <= 0)
- button.setEnabled(false);
- createButton(parent, IDialogConstants.CANCEL_ID,
- IDialogConstants.CANCEL_LABEL, false);
- }
- /**
- * Return the initial size of the dialog
- */
- protected Point getInitialSize() {
- return new Point(300, 375);
- }
- public String[] getResult() {
- return this.result;
- }
- protected void configureShell(Shell newShell) {
- super.configureShell(newShell);
- // newShell.setText("Select EJB Module");
- newShell.setText(title);
- }
- protected void buttonPressed(int buttonId) {
- if (buttonId == IDialogConstants.OK_ID) {
- TableItem[] children = table.getItems();
- ArrayList v = new ArrayList(children.length);
- for (int i = 0; i < children.length; i++) {
- TableItem item = children[i];
- if (item.getChecked()) {
- v.add(item.getData());
- }
- }
- int size = v.size();
- result = new String[size];
- for (int i = 0; i < v.size(); i++)
- result[i] = v.get(i).toString();
- }
- super.buttonPressed(buttonId);
- }
- }
java 代码
- List list = new ArrayList();
- for (int i = 0; i < file.length; i++) {
- list.add(i);
- }
- CheckModuleSelectionDialog dialog = new CheckModuleSelectionDialog(
- ProjectUtil.getShell(), list, "Select a Project",
- "Select the Project container.");
- if (dialog.open() == ContainerSelectionDialog.OK) {
- String[] result = dialog.getResult();
- }