---
title: "Eclipse插件开发常用功能（以后会继续加入）"
author: "jackini"
date: "2007-03-06T17:30:33Z"
tags: []
slug: "eclipse"
draft: false
lastmod: "2007-03-06T17:30:33Z"
id: 57820
url: "https://jackini.iteye.com/blog/57820"
---

1、选择java类的Dialog的使用

java 代码

 

01. final IJavaSearchScope searchScope = SearchEngine
02. .createWorkspaceScope();
03. addButton.addSelectionListener(new SelectionAdapter() {
04. public void widgetSelected(SelectionEvent e) {
05. TypeSelectionDialog2 dialog = new TypeSelectionDialog2(
06. getShell(), false,
07. new ProgressMonitorDialog(getShell()), searchScope,
08. IJavaSearchConstants.TYPE);
09. 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 代码

 

01. browseButton.addSelectionListener(new SelectionAdapter() {
02. public void widgetSelected(SelectionEvent e) {
03. super.widgetSelected(e);
04. IJavaProject javaProject = JavaCore.create(getProject(obj));
05. SelectionDialog dialog = null;
06. try {
07. dialog = JavaUI
08. .createPackageDialog(
09. 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 代码

 

01. public class CheckboxSelectionDialog extends Dialog {
02. class ContentProvider implements IStructuredContentProvider {
03. public Object\[] getElements(Object inputElement) {
04. return new Object\[] {};
05. }
06. public void dispose() {
07. }
08. public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
09. }
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() &lt;= 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 &lt; 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 &lt; v.size(); i++)
94. result\[i] = v.get(i).toString();
95. }
96. super.buttonPressed(buttonId);
97. }
98. }

使用：

java 代码

 

01. List list = new ArrayList();
02. for (int i = 0; i &lt; file.length; i++) {
03. list.add(i);
04. }
05. CheckModuleSelectionDialog dialog = new CheckModuleSelectionDialog(
06. ProjectUtil.getShell(), list, "Select a Project",
07. "Select the Project container.");
08. if (dialog.open() == ContainerSelectionDialog.OK) {
09. String\[] result = dialog.getResult();
10. }