.:: CODE SNIPPET ::.

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

Tag Archives: eclipse

What you should do when the arrow keys is locked in eclipse on Linux platform


When I use Eclipse IDE on a Linux machine, I usually encounter some problems when the arrow keys suddenly loose their effect. The enter and backspace key also is stuck by this way. It makes me angry and I often press these key many times to try to get out of this obstacle.
After many attempts, I can reproduce the case when this problem occur by these steps: Hover your mouse on a function to make eclipse show up its java doc popup window. Then you move your mouse and click inside that window to focus on that one. After that, you move the mouse out the java doc window and click on anywhere in the editor. By this way, the problem happens.

As my opinion, this is simply caused by the loosing of focus on your editor. The focus is still on the popup window.

With that cause, I find out the way to make the focus back on the editor. You can do something like: show up any more popup window (by right click on editor and select Quick Outline for instance) and then press Esc, the focus will back immediately and you can use your arrow keys as usual.

For conclusion: Right Click (on editor) -> Quick Outline -> press Esc key.

Install plugins for eclipse Luna via dropins directory


The simple way you can install plugins for eclipse Luna is create an external dropins directory. In this dropins directory you can easily download plugins and extract it to each folder. You don’t need to do anything else. The dropins folder after contain 3 plugins: Windowbuilder, Checkstyle and Jautodoc have the structure as below:

And the only other thing you have to do is add a property contain the absolute path to your dropins directory into eclipse.ini file.

-Dorg.eclipse.equinox.p2.reconciler.dropins.directory=d:\work\common\lib\eclipse_extension\extensions\dropins

Configure log4j


I will introduce step by step:
1. Download log4j library.
You can easily download the log4j library at link. After download, unzip it into any directory (called D:/log4j).
2. Create a configuration file for your log.
In your project create a new log4j.xml file with the content like:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
	xmlns:log4j='http://jakarta.apache.org/log4j/'>

	<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss.SSS} %5p %c{1} - %m%n" />
		</layout>
	</appender>

	<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
		<param name="file" value="log/example.log" />
		<param name="MaxFileSize" value="100KB" />
		<!-- Keep one backup file -->
		<param name="MaxBackupIndex" value="1" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%p %t %c - %m%n" />
		</layout>
	</appender>

	<root>
		<level value="INFO" />
		<appender-ref ref="consoleAppender" />
		<appender-ref ref="fileAppender" />
	</root>

</log4j:configuration>

Note that you should put the above xml file in the src directory of your project.
3. Create a system property and use the log.
At the beginning of the main method, you create a new system property named org.apache.commons.logging.Log like this snippet:

public static void main(String[] args)
    {
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
    }

At the same time, you can now use the log instance to log anything you want:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyClass
{
/** The Constant LOG. */
    private static final Log LOG = LogFactory.getLog(MyClass.class);

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
        LOG.info("Execute main method...");
    }
}

Happy logging!!

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.

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

    %d bloggers like this: