How to Create plug in that reads Any Console in Eclipse

Many times I feel , I wish I could save the entire console from Eclipse in to a file. For past few days I was working on CVS (file repository system) & what I had to after committing hundreds files was that – I had to find out the file names, versions & previous versions from Eclipse CVS log & create a excel file with those data & send to my supervisor.
Trust me  this is really is a fun to create a plug-in using which my efforts were reduced from 1 or 2 hours to ONE SINGLE CLICK!!

So, to be precise, to create this, our first step will be
   To create a plug-in which  -
       a.Searches for a particular console( e.g. CVS)
       b. Reads data from that console.

Note: If you are not aware how to create a Eclipse Plugin using Java, please refer to this article - Create Plug in for Eclipse




Now, in execute method, we will write this -
public Object execute(ExecutionEvent event) 
throws ExecutionException{
IWorkbenchWindow window = HandlerUtil.
getActiveWorkbenchWindowChecked(event);
MessageConsole myConsole = findConsole("CVS");
String inputData = “”;
if(myConsole!=null){IDocument doc = myConsole.getDocument();
inputData = doc.get();
if (inputData != null)
            inputData = inputData.trim();
if (inputData != null && !inputData.equals("")) {
writeConsoleToFile();}
}
return null;
}

1. findConsole(“CVS”) is the method which searches for the console “CVS”.
2. SampleHandler is the class name in the plug in project which contains the execute method & a method named “writeConsoleToFile” which reads the console “CVS”, if found & stores the output in a text file.

findConsole()
private static MessageConsole findConsole(String name) {
 ConsolePlugin plugin = ConsolePlugin.getDefault();
 IConsoleManager conMan = plugin.getConsoleManager();
 IConsole[] existing = conMan.getConsoles();
 for (int i = 0; i < existing.length; i++)
  if (name.equals(existing[i].getName()))
   return (MessageConsole) existing[i];
}

If you require to create a new console if there is no console with name “CVS” is there, then just attach this section inside the above method

// no console found, so create a new one
 MessageConsole myConsole = new MessageConsole(name, null);
 conMan.addConsoles(new IConsole[] { myConsole });
 return myConsole;

Well, now you have your console in your hand. Now, what we need to to do is to read the console & store it in a file… A .txt file may be. This section will be done in the method - writeConsoleToFile()
If you require to create a new console if there is no console with name “CVS” is there, then just attach this section inside the above method

public void writeConsoleToFile(String input) {
 String filePath = Constant.FILE_PATH; //mention your 
     //fully qualified 
     //file path here
 FileWriter fileWriter = null;
 BufferedWriter bufferedWriter = null;
 try {
  File file = new File(filePath);
  if (!file.exists()) {
   file.createNewFile();
  } else {
   file.delete();
   file.createNewFile();
  }
  fileWriter = new FileWriter(file.
    getAbsoluteFile());
  bufferedWriter = 
   new BufferedWriter(fileWriter);
  bufferedWriter.write(input);
 } catch (IOException ex) {
  ex.printStackTrace();
 } finally {
  if (bufferedWriter != null)
   try {
    bufferedWriter.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  if (fileWriter != null)
   try {
    fileWriter.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
 }
}


Now, run the plug-in from eclipse & if there is any console named “CVS”, it will be read by the plug-in & stored in the path mentioned by you in the method : writeConsoleToFile().

For more help regarding creation of a simple eclipse plug-in please refer to this link -
Create Simple Plugin for Eclipse in Java or just drop a mail to me using “Contact Me” Tab

Happy Coding !!

"Doubt grows with knowledge." - Johann Wolfgang von Goethe 

No comments :

Post a Comment