樂在其中

          以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,. 閱讀(1988) 評論(0)  編輯  收藏 所屬分類: Eclipse
          ©2005-2008 Suprasoft Inc., All right reserved.
          主站蜘蛛池模板: 卢氏县| 响水县| 来宾市| 新绛县| 平罗县| 灵寿县| 贵定县| 井陉县| 承德市| 镇安县| 浦北县| 大冶市| 垫江县| 德清县| 盱眙县| 工布江达县| 敦化市| 金塔县| 大余县| 东乡| 郸城县| 华亭县| 莆田市| 怀柔区| 宿州市| 北碚区| 山阳县| 辽宁省| 松原市| 灵璧县| 古田县| 汕尾市| 山阳县| 南木林县| 桑日县| 南溪县| 新乡县| 永泰县| 鹿泉市| 民勤县| 大悟县|