package com.github.jknack.antlr4ide.ui.decorator;

import org.eclipse.jface.viewers.ILightweightLabelDecorator;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Antlr4SuffixDecorator implements ILightweightLabelDecorator
{
   // read first line from file
   // the ANTLR4 generated java files has first line indicating origin
   //    >// Generated from testissue.g4 by ANTLR 4.5.1<
   // the tokens files have no indication of origin except for the filename of course.

   public void decorate(Object element, IDecoration decoration) {
	   // Note the plugin.xml specify which types are passed to this decorator
	   IResource resource=(IResource)element;
	   String resourceName = resource.getName();
	   
//	   System.out.println(" Antlr4SuffixDecorator decorate Object>"+element.getClass()+"<"
//	   		+ ">"+resource.getName()+"<"
//	   	    + ">"+resource.getFullPath()+"<" // relative to workspace root
//	   	    + ">"+resource.getRawLocation()+"<" // full file system path
//	   		);
	   
	   if(resource.getType()!=IResource.FILE) return;
	   if(!resource.isDerived()) return;
	   
	   if(resourceName.endsWith("Lexer.tokens")) decoration.addSuffix(" ["+resourceName.substring(0,resourceName.length()-12)+".g4]");
	   else 
       if(resourceName.endsWith("Parser.tokens")) decoration.addSuffix(" ["+resourceName.substring(0,resourceName.length()-13)+".g4]");
	   else 
	   if(resourceName.endsWith(".tokens")) decoration.addSuffix(" ["+resourceName.substring(0,resourceName.length()-7)+".g4]");
	   else 
	   if(resourceName.endsWith(".java")) { // what about other target types?
		   // read first line
		    BufferedReader in=null;
			try {
				in = new BufferedReader(new InputStreamReader(((IFile)resource).getContents()));
 			    String text = in.readLine();
 			    if (text.startsWith("// Generated from ")) {
 				 String[] strArray = text.split(" ");
 			     decoration.addSuffix(" ["+strArray[3]+"]");  
 			    }
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if(in!=null) in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	   }
   }
   
   /*
    * invoked by eclipse when selecting decorator in preferences or by start
    */
   public void addListener(ILabelProviderListener listener) { 
   }
   
   /*
    * invoked before dispose, when unselecting the decorator in preferences
    */
   public void removeListener(ILabelProviderListener listener) { 
   }
   
   /*
    * invoked after removeListener, when unselecting the decorator in preferences
    */
   public void dispose() { 
//	   System.out.println(" Antlr4SuffixDecorator dispose"); 
   }
   
   public boolean isLabelProperty(Object element, String property) { 
//	   System.out.println(" Antlr4SuffixDecorator isLabelProperty>"+element.getClass()+"<>"+property+"<");
	   return false; 
   }
   
   
}

@@ -726,16 +726,228 @@
 course.%0D
+%0A%09%0D%0A   static final String%5B%5D checkSuffixes=%7B%22Lexer.tokens%22,%22Parser.tokens%22,%22.tokens%22 %7D;%0D%0A   static final int%5B%5D    checkSuffixesLen=%7BcheckSuffixes%5B0%5D.length(),checkSuffixes%5B1%5D.length(),checkSuffixes%5B2%5D.length()%7D;%0D
 %0A%0D%0A   pu
@@ -1578,438 +1578,8 @@
   %0D%0A
-%09   if(resourceName.endsWith(%22Lexer.tokens%22)) decoration.addSuffix(%22 %5B%22+resourceName.substring(0,resourceName.length()-12)+%22.g4%5D%22);%0D%0A%09   else %0D%0A       if(resourceName.endsWith(%22Parser.tokens%22)) decoration.addSuffix(%22 %5B%22+resourceName.substring(0,resourceName.length()-13)+%22.g4%5D%22);%0D%0A%09   else %0D%0A%09   if(resourceName.endsWith(%22.tokens%22)) decoration.addSuffix(%22 %5B%22+resourceName.substring(0,resourceName.length()-7)+%22.g4%5D%22);%0D%0A%09   else %0D%0A
 %09   
@@ -1836,24 +1836,45 @@
 eadLine();%0D%0A
+ %09%09%09    in.close();%0D%0A
  %09%09%09    if (
@@ -2091,106 +2091,270 @@
 %7D%0D%0A%09
-%09%09try %7B%0D%0A%09%09%09%09if(in!=null) in.close();%0D%0A%09%09%09%7D catch (IOException e
+   %7D%0D%0A%09   else %7B%0D%0A%09%09   for (int i=0;i%3CcheckSuffixes.length;i++) %7B%0D%0A%09%09%09   if(resourceName.endsWith(checkSuffixes%5Bi%5D)
 ) %7B
+ 
 %0D%0A%09%09%09%09
-e.printStackTrace();%0D%0A%09%09%09
+   decoration.addSuffix(asDecoratorSuffix(resourceName,checkSuffixesLen%5Bi%5D,%22.g4%22));%0D%0A%09%09%09%09   break; // stop at first match%0D%0A%09%09%09   %7D%0D%0A%09%09   
 %7D%0D%0A%09
@@ -2362,35 +2362,34 @@
   %7D%0D%0A   %7D%0D%0A   %0D%0A
-   
+%0D%0A
 /*%0D%0A    * invoke
@@ -3116,16 +3116,316 @@
 %7D%0D%0A   %0D%0A
-
    %0D%0A
+   private String asDecoratorSuffix(String fileName) %7B%0D%0A%09   return %22 %5B%22+fileName+%22%5D%22;%0D%0A   %7D%0D%0A%0D%0A   private String asDecoratorSuffix(String resourceName, int resourceSuffixLen, String ext) %7B%0D%0A%09   return asDecoratorSuffix(resourceName.substring(0,resourceName.length()-resourceSuffixLen)+ext);%0D%0A   %7D%0D%0A%0D%0A
 %7D%0D%0A

Use array for suffixes to check for. Test .java case first.