乐在其中

以JEE为主攻,以Flex为点缀,以Eclipse RCP为乐趣
请访问http://www.inframesh.org

首页 新随笔 联系 管理
  43 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks
IContainer 中相对路径的选择文件对话框:
 
 
 
import java.util.ArrayList;
import java.util.List;
 
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
 
/**
* Provides content for a tree viewer that shows only containers.
*/
public class FileContentProvider implements ITreeContentProvider {
  private boolean showClosedProjects = true;
 
  /**
  * Creates a new ContainerContentProvider.
  */
  public FileContentProvider() {
  }
 
  /**
  * The visual part that is using this content provider is about
  * to be disposed. Deallocate all allocated SWT resources.
  */
  public void dispose() {
  }
 
  /*
  * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
  */
  public Object[] getChildren(Object element) {
    if (element instanceof IWorkspace) {
        // check if closed projects should be shown
        IProject[] allProjects = ((IWorkspace) element).getRoot()
            .getProjects();
        if (showClosedProjects) {
               return allProjects;
           }
 
        ArrayList accessibleProjects = new ArrayList();
        for (int i = 0; i < allProjects.length; i++) {
          if (allProjects.isOpen()) {
            accessibleProjects.add(allProjects);
          }
        }
        return accessibleProjects.toArray();
    } else if (element instanceof IContainer) {
        IContainer container = (IContainer) element;
        if (container.isAccessible()) {
          try {
            List children = new ArrayList();
            IResource[] members = container.members();
            for (int i = 0; i < members.length; i++) {
                if (members.getType() == IResource.FILE) {
                   IResource res = members;
                   if(isValidateFile(res.getName())){
                       children.add(members);    
                   }                    
                }else{
                   children.add(members);    
                }
                
            }
            return children.toArray();
          } catch (CoreException e) {
            // this should never happen because we call #isAccessible before invoking #members
          }
        }
    }
    return new Object[0];
  }
  public boolean isValidateFile(String fileName){
     return true;
  }
 
  /*
  * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
  */
  public Object[] getElements(Object element) {
    return getChildren(element);
  }
 
  /*
  * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
  */
  public Object getParent(Object element) {
    if (element instanceof IResource) {
           return ((IResource) element).getParent();
       }
    return null;
  }
 
  /*
  * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
  */
  public boolean hasChildren(Object element) {
    return getChildren(element).length > 0;
  }
 
  /*
  * @see org.eclipse.jface.viewers.IContentProvider#inputChanged
  */
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  }
 
  /**
  * Specify whether or not to show closed projects in the tree 
  * viewer. Default is to show closed projects.
  * 
  * @param show boolean if false, do not show closed projects in the tree
  */
  public void showClosedProjects(boolean show) {
    showClosedProjects = show;
  }
 
}
//上边那个是改ContainerFolderProvider
 
IProject project = WorkbenchPlugin.getCurrentProject();
       ILabelProvider labelProvider = WorkbenchLabelProvider
       .getDecoratingWorkbenchLabelProvider();
       FileContentProvider contentProvider = new FileContentProvider(){
           public boolean isValidateFile(String fileName){
               if(fileName.endsWith(".java")){
                   return true;
               }
               return false;
           }
       };
       Shell shell = WorkbenchPlugin.getShell();
       ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell,labelProvider,contentProvider){
           protected void okPressed() {
               Object result = this.getFirstResult();
               if(result instanceof IFile){
                   super.okPressed();
               }
           }
       };

 

 

Java代码 复制代码
  1. import org.eclipse.core.runtime.CoreException;  
  2. import org.eclipse.core.runtime.OperationCanceledException;  
  3. import org.eclipse.jdt.core.IType;  
  4. import org.eclipse.jdt.core.search.IJavaSearchConstants;  
  5. import org.eclipse.jdt.core.search.SearchEngine;  
  6. import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;  
  7. import org.eclipse.jdt.internal.ui.JavaPlugin;  
  8. import org.eclipse.jdt.internal.ui.JavaPluginImages;  
  9. import org.eclipse.jdt.internal.ui.JavaUIMessages;  
  10. import org.eclipse.jdt.internal.ui.actions.OpenTypeAction;  
  11. import org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog2;  
  12. import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;  
  13. import org.eclipse.jdt.internal.ui.util.ExceptionHandler;  
  14. import org.eclipse.jface.action.IAction;  
  15. import org.eclipse.jface.dialogs.IDialogConstants;  
  16. import org.eclipse.swt.widgets.Shell;  
  17. import org.eclipse.ui.IEditorPart;  
  18. import org.eclipse.ui.PlatformUI;  
  19. import org.eclipse.ui.help.WorkbenchHelp;  
  20.  
  21. public class OpenJavaAction extends OpenTypeAction {  
  22.     private OpenTypeSelectionDialog2 dialog;  
  23.  
  24.     private Shell parent;  
  25.       
  26.     private boolean bool = true;  
  27.     public OpenJavaAction() {  
  28.         super();  
  29.         setText(JavaUIMessages.OpenTypeAction_label); //$NON-NLS-1$  
  30.         setDescription(JavaUIMessages.OpenTypeAction_description); //$NON-NLS-1$  
  31.         setToolTipText(JavaUIMessages.OpenTypeAction_tooltip); //$NON-NLS-1$  
  32.         setImageDescriptor(JavaPluginImages.DESC_TOOL_OPENTYPE);  
  33.         WorkbenchHelp.setHelp(this, IJavaHelpContextIds.OPEN_TYPE_ACTION);  
  34.         parent = JavaPlugin.getActiveWorkbenchShell();  
  35.         try {  
  36.             dialog = new OpenTypeSelectionDialog2(parent,true, PlatformUI  
  37.                     .getWorkbench().getProgressService(),  
  38.                      SearchEngine  
  39.                             .createWorkspaceScope(),IJavaSearchConstants.TYPE);  
  40.         } catch (OperationCanceledException e) {  
  41.             return;  
  42.         }  
  43.     }  
  44.  
  45.     public OpenTypeSelectionDialog2 getDialog() {  
  46.         return dialog;  
  47.     }  
  48.       
  49.     public Object[] getType(){  
  50.         return dialog.getResult();  
  51.     }  
  52.       
  53.     public void setBool(boolean bool){  
  54.         this.bool = bool;  
  55.     }  
  56.  
  57.     public void run() {  
  58.  
  59. //      dialog.setMatchEmptyString(true);  
  60.         dialog.setTitle(JavaUIMessages.OpenTypeAction_dialogTitle); //$NON-NLS-1$  
  61.         dialog.setMessage(JavaUIMessages.OpenTypeAction_dialogMessage); //$NON-NLS-1$ //$NON-NLS-1$       
  62.         int result = dialog.open();  
  63.         if (result != IDialogConstants.OK_ID)  
  64.             return;  
  65.  
  66.         Object[] types = dialog.getResult();  
  67.         if (types != null && types.length > 0) {  
  68.             IType type = (IType) types[0];  
  69.             if(bool){  
  70.                 try {  
  71.                     IEditorPart part = EditorUtility.openInEditor(type, true);  
  72.                     EditorUtility.revealInEditor(part, type);  
  73.                 } catch (CoreException x) {  
  74.                     String title= JavaUIMessages.OpenTypeAction_errorTitle; //$NON-NLS-1$  
  75.                     String message= JavaUIMessages.OpenTypeAction_errorMessage; //$NON-NLS-1$ //$NON-NLS-1$  
  76.                     ExceptionHandler.handle(x, title, message);  
  77.                 }  
  78.             }  
  79.               
  80.         }  
  81.     }  
  82.  
  83.     public void run(IAction action) {  
  84.         run();  
  85.     }  
  86.  
 
Java代码 复制代码
  1. import org.eclipse.core.runtime.OperationCanceledException;  
  2. import org.eclipse.jdt.core.IType;  
  3. import org.eclipse.jdt.core.search.IJavaSearchConstants;  
  4. import org.eclipse.jdt.core.search.SearchEngine;  
  5. import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;  
  6. import org.eclipse.jdt.internal.ui.JavaPlugin;  
  7. import org.eclipse.jdt.internal.ui.JavaPluginImages;  
  8. import org.eclipse.jdt.internal.ui.JavaUIMessages;  
  9. import org.eclipse.jdt.internal.ui.actions.OpenTypeAction;  
  10. import org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog2;  
  11. import org.eclipse.jface.action.IAction;  
  12. import org.eclipse.jface.dialogs.IDialogConstants;  
  13. import org.eclipse.swt.widgets.Shell;  
  14. import org.eclipse.swt.widgets.Text;  
  15. import org.eclipse.ui.PlatformUI;  
  16. import org.eclipse.ui.help.WorkbenchHelp;  
  17. public class SelectionClassAction extends OpenTypeAction {  
  18.     private OpenTypeSelectionDialog2 dialog;  
  19.  
  20.     private Shell parent;  
  21.  
  22.     private Text classValue;  
  23.  
  24.     public SelectionClassAction() {  
  25.         super();  
  26.         setText(JavaUIMessages.OpenTypeAction_label); //$NON-NLS-1$  
  27.         setDescription(JavaUIMessages.OpenTypeAction_description); //$NON-NLS-1$  
  28.         setToolTipText(JavaUIMessages.OpenTypeAction_tooltip); //$NON-NLS-1$  
  29.         setImageDescriptor(JavaPluginImages.DESC_TOOL_OPENTYPE);  
  30.         WorkbenchHelp.setHelp(this, IJavaHelpContextIds.OPEN_TYPE_ACTION);  
  31.         parent = JavaPlugin.getActiveWorkbenchShell();  
  32.         try {  
  33.             dialog = new OpenTypeSelectionDialog2(parent, true, PlatformUI  
  34.                     .getWorkbench().getProgressService(), SearchEngine  
  35.                     .createWorkspaceScope(), IJavaSearchConstants.TYPE);  
  36.         } catch (OperationCanceledException e) {  
  37.             return;  
  38.         }  
  39.     }  
  40.  
  41.     public OpenTypeSelectionDialog2 getDialog() {  
  42.         return dialog;  
  43.     }  
  44.  
  45.     public void run() {  
  46.         // dialog.setMatchEmptyString(true);  
  47.         dialog.setTitle(JavaUIMessages.OpenTypeAction_dialogTitle); //$NON-NLS-1$  
  48.         dialog.setMessage(JavaUIMessages.OpenTypeAction_dialogMessage); //$NON-NLS-1$//$NON-NLS-1$        
  49.         int result = dialog.open();  
  50.         if (result != IDialogConstants.OK_ID)  
  51.             return;  
  52.  
  53.         Object[] types = dialog.getResult();  
  54.         if (types != null && types.length > 0) {  
  55.             IType type = (IType) types[0];  
  56.             type.getPackageFragment().getElementName();  
  57.             classValue.setText(type.getPackageFragment().getElementName() + "." 
  58.                     + type.getElementName());  
  59.         }  
  60.     }  
  61.  
  62.     public void run(IAction action) {  
  63.         run();  
  64.     }  
  65.  
  66.     public void run(Text classValue) {  
  67.         this.classValue = classValue;  
  68.         run();  
  69.     }  

  对于Floder:

Java代码 复制代码
  1. 源码如下:帮助刚学习的同学一起进步  
  2. IJavaProject currProject = ActionUtil.findSelectedJavaProject(this.selection);  
  3.         ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), currProject.getProject().getWorkspace().getRoot(), false,"Select Mapping Folder");  
  4.         if (dialog.open() == ContainerSelectionDialog.OK) {  
  5.             Object[] result = dialog.getResult();  
  6.             if (result.length == 1) {<A>javascript:;</A>  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.                 mappingPath.setText(((Path) result[0]).toString());  
  13.             }  
  14.         }  
  15.  
  16.  
  17. 其中  
  18. public static IJavaProject findSelectedJavaProject(ISelection selection) {  
  19.         IJavaProject currentProject = null;  
  20.         if (selection != null) {  
  21.             if (selection instanceof IStructuredSelection) {  
  22.                 IStructuredSelection ss = (IStructuredSelection)selection;  
  23.                 Object obj = ss.getFirstElement();  
  24.                 if (obj instanceof IJavaProject) {  
  25.                     currentProject = (IJavaProject)obj;  
  26.                 }  
  27.             }  
  28.         }  
  29.         return currentProject;  
  30.     }  
  31.  
  32. 例子:  
  33. ContainerSelectionDialog= new ContainerSelectionDialog(...);  
  34.     if(dialog.open == ContainerSelectionDialog.OK){  
  35.         Object[] result = dialog.getResult();  
  36.         String containerFullName  = ((Path)reslut[0]).toString();  
  37.         createFile();  
  38.     }  
  39.     public createFile(){  
  40.         ....  
  41.     } 
Java代码 复制代码
  1. TypeSelectionDialog  
  2.     TypeSelectionDialog是JDT内核中的一个UI显示部件,是不鼓励使用的,不过我觉得很好用,拿来与大家分享,呵呵:)  
  3.     TypeSelectionDialog提供一个在给定的搜索域内搜索Java Type的选择对话框。比如我们在新建Class时选择Super Class时那样。选择后得到了一个或多个Eclipse平台提供的针对Java Type的对象:IType。  
  4.     所以在使用时,需要以下几步工作:  
  5.     1、利用SearchEngine创建搜索域  
  6.     2、指定搜索内容为Class还是Interface还是其它的什么  
  7.     3、创建Dialog:  
  8.   try{  
  9.         TypeSelectionDialog2 dialog = new TypeSelectionDialog2(...);  
  10.         dialog.setFilter(...);  
  11.         dialog.setTitle(...);  
  12.         dialog.setMessage(...);  
  13.           
  14.         if (dialog.open() == IDialogConstants.CANCEL_ID)   
  15.           return;  
  16.  
  17.         Object[] types= dialog.getResult();  
  18.         if (types == null || types.length == 0)   
  19.           return;  
  20.           
  21.         IType type = (IType)types[0];  
  22.         IResource res = type.getResource();  
  23.     }catch(Exception e){  
  24.         ...  
  25.     } 

 

Java代码 复制代码
  1. ElementTreeSelectionDialog  
  2.    ElementTreeSelectionDialog不属于JDT内核UI,但是我们构造了合适的validator和filter之后,ElementTreeSelectionDialog就可以提供出一个选择Java源文件容器的对话框,在Eclipse JDT 的概念中,Java程序中的每一个Package叫做IPackageFragment,那么这个Java源文件容器叫做IPackageFragmentRoot。  
  3.    示例代码:  
  4.      ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(...);  
  5.      dialog.setValidator(validator);//校验所选中的是否是合法的  
  6.     dialog.setSorter(new JavaElementSorter());  
  7.      dialog.setTitle("Select Java Source Container");  
  8.      dialog.setMessage("Select one of java source containers from workspace.");  
  9.      dialog.addFilter(filter);//对于选择操作的过滤  
  10.      dialog.setInput(IJavaModel)//将工作区的Java模型传入  
  11.      dialog.setInitialSelection(fWorkspaceRoot.getProject());//初始选择  
  12.   ... ... 
 
Java代码 复制代码
  1. ElementListSelectionDialog  
  2.   ElementListSelectionDialog也不是JDT核心UI,但是我们通过构造这个Dialog中的元素列表,让其提供出一个Java 程序中Package选择列表对话框。  
  3.   ElementListSelectionDialog dialog= new ElementListSelectionDialog(...));  
  4.   dialog.setIgnoreCase(false);  
  5.   dialog.setTitle("Select Packages From Java Project ");  
  6.   dialog.setMessage("Select packages from java project which you pre-selected.");  
  7.   dialog.setEmptyListMessage("There is no package in selected project.");  
  8.   dialog.setElements(packages);//列表中的元素 
 
  • 描述: TypeSelectionDialog
  • 大小: 20.2 KB
  • 描述: ElementTreeSelectionDialog
  • 大小: 17.1 KB
  • 描述: ElementListSelectionDialog
  • 大小: 16.7 KB
posted on 2009-02-11 19:55 suprasoft Inc,. 阅读(1980) 评论(0)  编辑  收藏 所属分类: Eclipse

只有注册用户登录后才能发表评论。


网站导航:
 
©2005-2008 Suprasoft Inc., All right reserved.