<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    樂在其中

    以JEE為主攻,以Flex為點(diǎn)綴,以Eclipse RCP為樂趣
    請?jiān)L問http://www.inframesh.org

    首頁 新隨筆 聯(lián)系 管理
      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代碼 復(fù)制代碼
    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代碼 復(fù)制代碼
    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代碼 復(fù)制代碼
    1. 源碼如下:幫助剛學(xué)習(xí)的同學(xué)一起進(jìn)步  
    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代碼 復(fù)制代碼
    1. TypeSelectionDialog  
    2.     TypeSelectionDialog是JDT內(nèi)核中的一個UI顯示部件,是不鼓勵使用的,不過我覺得很好用,拿來與大家分享,呵呵:)  
    3.     TypeSelectionDialog提供一個在給定的搜索域內(nèi)搜索Java Type的選擇對話框。比如我們在新建Class時(shí)選擇Super Class時(shí)那樣。選擇后得到了一個或多個Eclipse平臺提供的針對Java Type的對象:IType。  
    4.     所以在使用時(shí),需要以下幾步工作:  
    5.     1、利用SearchEngine創(chuàng)建搜索域  
    6.     2、指定搜索內(nèi)容為Class還是Interface還是其它的什么  
    7.     3、創(chuàng)建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代碼 復(fù)制代碼
    1. ElementTreeSelectionDialog  
    2.    ElementTreeSelectionDialog不屬于JDT內(nèi)核UI,但是我們構(gòu)造了合適的validator和filter之后,ElementTreeSelectionDialog就可以提供出一個選擇Java源文件容器的對話框,在Eclipse JDT 的概念中,Java程序中的每一個Package叫做IPackageFragment,那么這個Java源文件容器叫做IPackageFragmentRoot。  
    3.    示例代碼:  
    4.      ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(...);  
    5.      dialog.setValidator(validator);//校驗(yàn)所選中的是否是合法的  
    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)//將工作區(qū)的Java模型傳入  
    11.      dialog.setInitialSelection(fWorkspaceRoot.getProject());//初始選擇  
    12.   ... ... 
     
    Java代碼 復(fù)制代碼
    1. ElementListSelectionDialog  
    2.   ElementListSelectionDialog也不是JDT核心UI,但是我們通過構(gòu)造這個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,. 閱讀(1986) 評論(0)  編輯  收藏 所屬分類: Eclipse
    ©2005-2008 Suprasoft Inc., All right reserved.
    主站蜘蛛池模板: 日韩欧毛片免费视频| 美女被免费网站在线视频免费 | a毛片免费全部播放完整成| 一级毛片在线免费视频| 一级毛片aaaaaa视频免费看| 国产成人va亚洲电影| 老司机午夜性生免费福利| 免费无码专区毛片高潮喷水| 污污的视频在线免费观看| 一级黄色片免费观看| 久久久精品视频免费观看| 中文字字幕在线高清免费电影| 日本高清不卡aⅴ免费网站| 免费一区二区无码东京热| 日本高清免费观看| 久久精品无码专区免费东京热| 特级精品毛片免费观看| 国产一卡二卡四卡免费| 成人午夜免费福利| 亚洲AV无码之日韩精品| 亚洲热线99精品视频| 亚洲网站免费观看| 国产精品亚洲午夜一区二区三区| 亚洲综合精品第一页| 黄色一级视频免费观看| 中文字幕免费在线视频| 无码精品国产一区二区三区免费| 无码精品A∨在线观看免费| 免费看大黄高清网站视频在线| 亚洲第一福利网站在线观看| 亚洲色成人中文字幕网站| 亚洲精品天天影视综合网| 亚洲一区二区三区在线观看蜜桃| 亚洲色精品三区二区一区| 伊人久久国产免费观看视频| 久久免费观看国产99精品| 91在线视频免费91| va亚洲va日韩不卡在线观看| 亚洲成AV人片在WWW色猫咪| 亚洲一区中文字幕| 一级毛片免费视频网站|