package winterwell.markdown.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import winterwell.markdown.LogUtil;

public class OpenGfmView extends AbstractHandler {

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		try {
	        IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
	        String gfmViewId = "code.satyagraha.gfm.viewer.views.GfmView";
	        IViewPart gfmView = activePage.showView(gfmViewId);
	        activePage.activate(gfmView);
		} catch (PartInitException e) {
			showError(e);
	    } catch (Exception e) {
	    	showError(e);
	    }		
		return null;
	}

	private void showError(Exception e) {
		String title = "Exception while opening GitHub Flavored Markdown View";
		String message = title+" (code.satyagraha.gfm.viewer.views.GfmView)"
				+"\nCheck Error Log View and continue at https://github.com/winterstein/Eclipse-Markdown-Editor-Plugin/issues/42"
				+"\n\nYou can also right-click file in Project Explorer"
				+"\n and select \"Show in GFM view\".";
		LogUtil.error(message, e);
		MessageDialog.openError(Display.getDefault().getActiveShell(), title , message);
	}
}

@@ -176,32 +176,74 @@
 utionException;%0A
+import org.eclipse.core.runtime.Platform;%0A
 import org.eclip
@@ -459,16 +459,50 @@
 tformUI;
+%0Aimport org.osgi.framework.Bundle;
 %0A%0Aimport
@@ -589,114 +589,410 @@
 %7B%0A%0A%09
-@Override%0A%09public Object execute(final ExecutionEvent event) throws ExecutionException %7B%0A%09%09try %7B%0A%09        
+private static final String GFM_VIEW_ID = %22code.satyagraha.gfm.viewer.views.GfmView%22;%0A%09private static final String GFM_SYMBOLIC_NAME = %22code.satyagraha.gfm.viewer.plugin%22;%0A%0A%09@Override%0A%09public Object execute(final ExecutionEvent event) throws ExecutionException %7B%0A%09%09try %7B%0A%09%09%09Bundle gfmBundle = Platform.getBundle(GFM_SYMBOLIC_NAME);%0A%09%09%09if (gfmBundle == null) %7B%0A%09%09%09%09showInstallWarning();%0A%09%09%09%7D else %7B%0A%09%09%09%09
 IWor
@@ -1090,88 +1090,11 @@
 );%0A%09
-        String gfmViewId = %22code.satyagraha.gfm.viewer.views.GfmView%22;%0A%09        
+%09%09%09
 IVie
@@ -1133,29 +1133,26 @@
 iew(
-gfmViewId);%0A%09        
+GFM_VIEW_ID);%0A%09%09%09%09
 acti
@@ -1177,16 +1177,21 @@
 mView);%0A
+%09%09%09%7D%0A
 %09%09%7D catc
@@ -1234,20 +1234,17 @@
 or(e);%0A%09
-    
+%09
 %7D catch 
@@ -1260,20 +1260,17 @@
 n e) %7B%0A%09
-    
+%09
 %09showErr
@@ -1281,29 +1281,370 @@
 );%0A%09
-    %7D%09%09%0A%09%09return null
+%09%7D%0A%09%09return null;%0A%09%7D%0A%0A%09private void showInstallWarning() %7B%0A%09%09String title = %22GFM viewer was not found%22;%0A%09%09String message = title + %22 (%22 + GFM_SYMBOLIC_NAME + %22)%22%0A%09%09%09%09+ %22%5Cn%5CnPlease ensure that you have correctly installed the plugin, see https://github.com/satyagraha/gfm_viewer%22;%0A%09%09MessageDialog.openWarning(Display.getDefault().getActiveShell(), title, message)
 ;%0A%09%7D
@@ -1786,60 +1786,41 @@
 itle
-+%22 (code.satyagraha.gfm.viewer.views.GfmView
+ + %22 (%22 + GFM_VIEW_ID + %22
 )%22%0A%09%09%09%09+
 %22%5CnC
@@ -1815,16 +1815,17 @@
 )%22%0A%09%09%09%09+
+ 
 %22%5CnCheck
@@ -1934,16 +1934,17 @@
 2%22%0A%09%09%09%09+
+ 
 %22%5Cn%5CnYou
@@ -1996,16 +1996,17 @@
 r%22%0A%09%09%09%09+
+ 
 %22%5Cn and 
@@ -2135,17 +2135,16 @@
 ), title
- 
 , messag

Improve error handling when GFM plugin is not found