.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Tag Archives: rcp

How to get all files in a project eclipse RCP


In this post, I show you a very simple problem that how you can get all the file when you have your own project resource which is named iProject. The solution is using method member() in IProject and IFolder. At the first time I use this method for just project resource, then i search for earch resource got, and i check if it is a file. That’s a wrong thought. So, you have to go through each folder and get its member again.
And, the below snippet is the solution:

public static void getAllFilesInProject(
        final IProject project,
        final List<IFile> lstFiles,
        final String fileExtension)
    {
        try
        {
            if (project.exists())
            {
                if (!project.isOpen())
                {
                    project.open(null);
                }
                IResource[] resources = project.members(false);
                for (IResource resFo : resources)
                {
                    if (resFo instanceof IFolder)
                    {
                        IResource[] resFiles = ((IFolder) resFo).members();
                        for (IResource resFi : resFiles)
                        {
                            IFile file = (IFile) resFi;
                            if (fileExtension.equals(file.getFileExtension()))
                            {
                                lstFiles.add(file);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            LOG.info("Can not find any " + fileExtension + " file in the project " + project.getName());
        }
    }

And you pass your list file predefined at other place, and got out the files you want.

Advertisements

Create an command for opening editor from navigator


As you know, navigator view of an eclipse RCP application default allows you open an editor such a text editor. That is one of default editors, how about your own editor. Using command is one of the ways. By every step, we have:
I assume that I have an editor from the class FormEditor, at here we have its ID_EDITOR, besides I have also an Navigator with ID_NAVIGATOR
First, declare a command in extension points
In plugin.xml we declare:

<extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="com.my.example.OpenEditorHandler"
            id="com.my.example.OpenEditor"
            name="Open Editor">
      </command>
   </extension>

Read more of this post

How to customize the Tool bar Eclipse RCP


In a standard way, creating an eclipse RCP application have to include creating these files:

  • Activator
  • Application
  • WorkbenchWindowAdvisor
  • ActionBarAdvisor
  • WorkbenchAdvisor
  • As for me, you should hide all the default actions and commands.
    You can do that in the file WorkbenchWindowAdvisor which contains the postWindowOpen() function:

    public void postWindowOpen()
        {
            super.postWindowOpen();
            final String[] removeActionSets = new String[] { "org.eclipse.search.searchActionSet",
                "org.eclipse.ui.cheatsheets.actionSet", "org.eclipse.ui.actionSet.keyBindings",
                "org.eclipse.ui.edit.text.actionSet.navigation", "org.eclipse.ui.edit.text.actionSet.annotationNavigation",
                "org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo",
                "org.eclipse.ui.edit.text.actionSet.openExternalFile", "org.eclipse.ui.externaltools.ExternalToolsSet",
                "org.eclipse.ui.WorkingSetActionSet", "org.eclipse.update.ui.softwareUpdates",
                "org.eclipse.ui.actionSet.openFiles", "org.eclipse.mylyn.tasks.ui.navigation", };
            
            IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
            for (int i = 0; i < windows.length; ++i)
            {
                IWorkbenchPage page = windows[i].getActivePage();
                if (page != null)
                {
                    for (int j = 0; j < removeActionSets.length; j++)
                    {
                        page.hideActionSet(removeActionSets[j]);
                    }
                }
            }
        }
    

    Read more of this post

    How to make views communicate in Eclipse RCP


    In eclipse Rich Client Platform programming, some times you need to keep contact between views. How can we do that?
    After a couple of hours research, I found the way to make two views interact each other.
    There are a few articles on web, but I think they make us embarrassed.
    I will make it more shortly and clearly.
    We have two views: ViewA and ViewB.
    In this case: we assume that ViewB is the event sender, and ViewA is the rest one, the receiver.
    So now, what we have to do with each of them.
    ViewB
    we have to register the selection provider to the SelectionService of workbench. That simply we put this in the function createPartControl of ViewB: Read more of this post

    Bug when using DefferedContentProvider in TableViewer eclipse


    Bug
    When create an eclipse RCP application, times you use TableViewer need a content provider. There are many type of provider you can use for your table such as:
    ArrayContentProvider
    an implementation of ILazyContentProvider
    or,…DefferedContentProvider
    My favorite type is the last one, DefferedContentProvider.:D. But, at the first time I used it, it did work. And, i got stuck in this runtime bug:

    org.eclipse.core.runtime.AssertionFailedException: assertion failed: 
    	at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
    	at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
    	at org.eclipse.jface.viewers.deferred.DeferredContentProvider.inputChanged(DeferredContentProvider.java:140)
    	at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:276)
    	at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1690)
    	at com.gcs.rcp.app.ViewTableViewer.createViewer(ViewTableViewer.java:310)
    	at com.gcs.rcp.app.ViewTableViewer.createPartControl(ViewTableViewer.java:239)
    	at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPartControl(CompatibilityPart.java:142)
    	at org.eclipse.ui.internal.e4.compatibility.CompatibilityView.createPartControl(CompatibilityView.java:174)
    	at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create(CompatibilityPart.java:323)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    

    the error occurs while using DefferedContentProvider for TableViewer. Read more of this post

    %d bloggers like this: