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

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.   
  3.     class ContentProvider implements IStructuredContentProvider {  
  4.         public Object[] getElements(Object inputElement) {  
  5.             return new Object[] {};  
  6.         }  
  7.   
  8.         public void dispose() {  
  9.         }  
  10.   
  11.         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {  
  12.         }  
  13.     }  
  14.   
  15.     private Table table;  
  16.   
  17.     private CheckboxTableViewer ctv;  
  18.   
  19.     private TableViewer tv;  
  20.   
  21.     private java.util.List initialSelections;  
  22.   
  23.     private String[] result;  
  24.   
  25.     private String title;  
  26.   
  27.     private String label;  
  28.   
  29.     /** 
  30.      * Create the dialog 
  31.      *  
  32.      * @param parentShell 
  33.      */  
  34.     public CheckboxSelectionDialog (Shell parentShell,  
  35.             java.util.List initialSelections, String title, String label) {  
  36.         super(parentShell);  
  37.         this.initialSelections = initialSelections;  
  38.         this.title = title;  
  39.         this.label = label;  
  40.     }  
  41.   
  42.     /** 
  43.      * Create contents of the dialog 
  44.      *  
  45.      * @param parent 
  46.      */  
  47.     protected Control createDialogArea(Composite parent) {  
  48.         Composite container = (Composite) super.createDialogArea(parent);  
  49.         final GridLayout gridLayout = new GridLayout();  
  50.         container.setLayout(gridLayout);  
  51.   
  52.         final Label selectLabel = new Label(container, SWT.NONE);  
  53.         selectLabel.setLayoutData(new GridData());  
  54.         // selectLabel.setText("Select the ejb container.");  
  55.         selectLabel.setText(label);  
  56.   
  57.         tv = new TableViewer(container, SWT.CHECK | SWT.MULTI | SWT.BORDER  
  58.                 | SWT.FULL_SELECTION);  
  59.         // ctv = new CheckboxTableViewer(tv.getTable());  
  60.         tv.setContentProvider(new ContentProvider());  
  61.         tv.setInput(new Object());  
  62.         table = tv.getTable();  
  63.         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, truetrue));  
  64.         tv.add(initialSelections.toArray());  
  65.         //  
  66.         return container;  
  67.     }  
  68.   
  69.     /** 
  70.      * Create contents of the button bar 
  71.      *  
  72.      * @param parent 
  73.      */  
  74.     protected void createButtonsForButtonBar(Composite parent) {  
  75.         final Button button = createButton(parent, IDialogConstants.OK_ID,  
  76.                 IDialogConstants.OK_LABEL, true);  
  77.         if (initialSelections.size() <= 0)  
  78.             button.setEnabled(false);  
  79.         createButton(parent, IDialogConstants.CANCEL_ID,  
  80.                 IDialogConstants.CANCEL_LABEL, false);  
  81.     }  
  82.   
  83.     /** 
  84.      * Return the initial size of the dialog 
  85.      */  
  86.     protected Point getInitialSize() {  
  87.         return new Point(300375);  
  88.     }  
  89.   
  90.     public String[] getResult() {  
  91.         return this.result;  
  92.     }  
  93.   
  94.     protected void configureShell(Shell newShell) {  
  95.         super.configureShell(newShell);  
  96.         // newShell.setText("Select EJB Module");  
  97.         newShell.setText(title);  
  98.     }  
  99.   
  100.     protected void buttonPressed(int buttonId) {  
  101.         if (buttonId == IDialogConstants.OK_ID) {  
  102.             TableItem[] children = table.getItems();  
  103.             ArrayList v = new ArrayList(children.length);  
  104.             for (int i = 0; i < children.length; i++) {  
  105.                 TableItem item = children[i];  
  106.                 if (item.getChecked()) {  
  107.                     v.add(item.getData());  
  108.                 }  
  109.             }  
  110.             int size = v.size();  
  111.             result = new String[size];  
  112.             for (int i = 0; i < v.size(); i++)  
  113.                 result[i] = v.get(i).toString();  
  114.         }  
  115.         super.buttonPressed(buttonId);  
  116.     }  
  117.   
  118. }  
使用:
java 代码
 
  1. List list = new ArrayList();  
  2. for (int i = 0; i < file.length; i++) {  
  3.             list.add(i);  
  4. }  
  5.   
  6. CheckModuleSelectionDialog dialog = new CheckModuleSelectionDialog(  
  7.     ProjectUtil.getShell(), list, "Select a Project",  
  8.                 "Select the Project container.");  
  9. if (dialog.open() == ContainerSelectionDialog.OK) {  
  10.     String[] result = dialog.getResult();  
  11. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值