ITeye博客

Eclipse插件开发常用功能(以后会继续加入)

jackini · 2007-03-06 17:30 · Eclipse · Markdown

1、选择java类的Dialog的使用

java 代码

 

  1. final IJavaSearchScope searchScope = SearchEngine
  2. .createWorkspaceScope();
  3. addButton.addSelectionListener(new SelectionAdapter() {
  4. public void widgetSelected(SelectionEvent e) {
  5. TypeSelectionDialog2 dialog = new TypeSelectionDialog2(
  6. getShell(), false,
  7. new ProgressMonitorDialog(getShell()), searchScope,
  8. IJavaSearchConstants.TYPE);
  9. dialog.setMessage("Select an Entity bean"); //$NON-NLS-1$
  10. dialog.setBlockOnOpen(true);
  11. dialog.setTitle("Type Selection");
  12. if (Dialog.OK == dialog.open()) {
  13. IType obj = (IType) dialog.getFirstResult();
  14. String className = obj.getResource().getName();
  15. String packageName = obj.getPackageFragment()
  16. .getElementName();
  17. list.add(packageName + "."+ className);
  18. }
  19. }
  20. });

2、包选择的Dialog的使用

java 代码

 

  1. browseButton.addSelectionListener(new SelectionAdapter() {
  2. public void widgetSelected(SelectionEvent e) {
  3. super.widgetSelected(e);
  4. IJavaProject javaProject = JavaCore.create(getProject(obj));
  5. SelectionDialog dialog = null;
  6. try {
  7. dialog = JavaUI
  8. .createPackageDialog(
  9. getShell(),
  10. javaProject,
  11. IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS);
  12. dialog.setTitle("Package Selection");
  13. dialog.setMessage("Choose a folder");
  14. } catch (JavaModelException e1) {
  15. // ExceptionHandler.handleExceptionAndAbort(e1);
  16. }
  17. if (dialog.open() != Window.OK) {
  18. return;
  19. }
  20. IPackageFragment pck = (IPackageFragment) dialog.getResult()[0];
  21. if (pck != null) {
  22. packageTxt.setText(pck.getElementName());
  23. }
  24. }
  25. });

 

 3、自定义带有多选框的Dialog的创建及其使用
创建:

java 代码

 

  1. public class CheckboxSelectionDialog extends Dialog {
  2. class ContentProvider implements IStructuredContentProvider {
  3. public Object[] getElements(Object inputElement) {
  4. return new Object[] {};
  5. }
  6. public void dispose() {
  7. }
  8. public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  9. }
  10. }
  11. private Table table;
  12. private CheckboxTableViewer ctv;
  13. private TableViewer tv;
  14. private java.util.List initialSelections;
  15. private String[] result;
  16. private String title;
  17. private String label;
  18. /**
  19. * Create the dialog
  20. *
  21. * @param parentShell
  22. */
  23. public CheckboxSelectionDialog (Shell parentShell,
  24. java.util.List initialSelections, String title, String label) {
  25. super(parentShell);
  26. this.initialSelections = initialSelections;
  27. this.title = title;
  28. this.label = label;
  29. }
  30. /**
  31. * Create contents of the dialog
  32. *
  33. * @param parent
  34. */
  35. protected Control createDialogArea(Composite parent) {
  36. Composite container = (Composite) super.createDialogArea(parent);
  37. final GridLayout gridLayout = new GridLayout();
  38. container.setLayout(gridLayout);
  39. final Label selectLabel = new Label(container, SWT.NONE);
  40. selectLabel.setLayoutData(new GridData());
  41. // selectLabel.setText("Select the ejb container.");
  42. selectLabel.setText(label);
  43. tv = new TableViewer(container, SWT.CHECK | SWT.MULTI | SWT.BORDER
  44. | SWT.FULL_SELECTION);
  45. // ctv = new CheckboxTableViewer(tv.getTable());
  46. tv.setContentProvider(new ContentProvider());
  47. tv.setInput(new Object());
  48. table = tv.getTable();
  49. table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
  50. tv.add(initialSelections.toArray());
  51. //
  52. return container;
  53. }
  54. /**
  55. * Create contents of the button bar
  56. *
  57. * @param parent
  58. */
  59. protected void createButtonsForButtonBar(Composite parent) {
  60. final Button button = createButton(parent, IDialogConstants.OK_ID,
  61. IDialogConstants.OK_LABEL, true);
  62. if (initialSelections.size() <= 0)
  63. button.setEnabled(false);
  64. createButton(parent, IDialogConstants.CANCEL_ID,
  65. IDialogConstants.CANCEL_LABEL, false);
  66. }
  67. /**
  68. * Return the initial size of the dialog
  69. */
  70. protected Point getInitialSize() {
  71. return new Point(300, 375);
  72. }
  73. public String[] getResult() {
  74. return this.result;
  75. }
  76. protected void configureShell(Shell newShell) {
  77. super.configureShell(newShell);
  78. // newShell.setText("Select EJB Module");
  79. newShell.setText(title);
  80. }
  81. protected void buttonPressed(int buttonId) {
  82. if (buttonId == IDialogConstants.OK_ID) {
  83. TableItem[] children = table.getItems();
  84. ArrayList v = new ArrayList(children.length);
  85. for (int i = 0; i < children.length; i++) {
  86. TableItem item = children[i];
  87. if (item.getChecked()) {
  88. v.add(item.getData());
  89. }
  90. }
  91. int size = v.size();
  92. result = new String[size];
  93. for (int i = 0; i < v.size(); i++)
  94. result[i] = v.get(i).toString();
  95. }
  96. super.buttonPressed(buttonId);
  97. }
  98. }

使用:

java 代码

 

  1. List list = new ArrayList();
  2. for (int i = 0; i < file.length; i++) {
  3. list.add(i);
  4. }
  5. CheckModuleSelectionDialog dialog = new CheckModuleSelectionDialog(
  6. ProjectUtil.getShell(), list, "Select a Project",
  7. "Select the Project container.");
  8. if (dialog.open() == ContainerSelectionDialog.OK) {
  9. String[] result = dialog.getResult();
  10. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。