package org.wangzw.plugin.cppstyle.ui;

import org.eclipse.cdt.ui.ICEditor;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.wangzw.plugin.cppstyle.CppCodeFormatter;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * 
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class CppStyleHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public CppStyleHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow activeWorkbenchWindow = HandlerUtil
				.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = activeWorkbenchWindow.getActivePage();

		if (page == null) {
			return null;
		}

		IEditorPart editor = page.getActiveEditor();

		if (editor == null) {
			return null;
		}

		if (editor instanceof ICEditor) {
			ICEditor ceditor = (ICEditor) editor;

			if (!ceditor.isDirty()) {
				return null;
			}

			IFile file = ((IFileEditorInput) ceditor.getEditorInput())
					.getFile();

			CppCodeFormatter formater = new CppCodeFormatter();

			if (formater.runClangFormatOnSave(file)) {
				formater.formatAndApply(ceditor);
			}

			ceditor.doSave(null);

			formater.deleteAllMarkers(file);

			if (formater.runCpplintOnSave(file)) {
				formater.checkFileFormat(file);
			}
		}

		return null;
	}
}

@@ -401,29 +401,104 @@
 pse.ui.I
-WorkbenchPage
+SaveablePart;%0Aimport org.eclipse.ui.IWorkbenchPage;%0Aimport org.eclipse.ui.IWorkbenchPart
 ;%0Aimport
@@ -575,16 +575,58 @@
 erUtil;%0A
+import org.eclipse.ui.internal.util.Util;%0A
 import o
@@ -969,581 +969,525 @@
 %7D%0A%0A%09
-/**%0A%09 * the command has been executed, so extract extract the needed information%0A%09 * from the application context.%0A%09 */%0A%09public Object execute(ExecutionEvent event) throws ExecutionException %7B%0A%09%09IWorkbenchWindow activeWorkbenchWindow = HandlerUtil%0A%09%09%09%09.getActiveWorkbenchWindow(event);%0A%09%09IWorkbenchPage page = activeWorkbenchWindow.getActivePage();%0A%0A%09%09if (page == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09IEditorPart editor = page.getActiveEditor();%0A%0A%09%09if (editor == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09if (editor instanceof ICEditor) %7B%0A%09%09%09ICEditor ceditor = (ICEditor) editor;%0A%0A%09%09%09if (!c
+protected ICEditor getSaveableEditor(ExecutionEvent event) %7B%0A%0A%09%09IWorkbenchPart activePart = HandlerUtil.getActivePart(event);%0A%0A%09%09if (activePart instanceof ICEditor) %7B%0A%09%09%09return (ICEditor) activePart;%0A%09%09%7D%0A%0A%09%09return null;%0A%09%7D%0A%0A%09/**%0A%09 * the command has been executed, so extract extract the needed information%0A%09 * from the application context.%0A%09 */%0A%09public Object execute(ExecutionEvent event) throws ExecutionException %7B%0A%09%09ICEditor editor = getSaveableEditor(event);%0A%0A%09%09if (editor == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09if (!
 edit
@@ -1505,17 +1505,16 @@
 )) %7B%0A%09%09%09
-%09
 return n
@@ -1520,21 +1520,19 @@
 null;%0A%09%09
-%09
 %7D%0A%0A
-%09
 %09%09IFile 
@@ -1558,17 +1558,16 @@
 rInput) 
-c
 editor.g
@@ -1586,14 +1586,8 @@
 t())
-%0A%09%09%09%09%09
 .get
@@ -1597,17 +1597,16 @@
 e();%0A%0A%09%09
-%09
 CppCodeF
@@ -1648,25 +1648,24 @@
 atter();%0A%0A%09%09
-%09
 if (formater
@@ -1687,33 +1687,32 @@
 OnSave(file)) %7B%0A
-%09
 %09%09%09formater.form
@@ -1722,17 +1722,16 @@
 ndApply(
-c
 editor);
@@ -1735,41 +1735,100 @@
 r);%0A
-%09
 %09%09%7D%0A%0A%09%09
-%09ceditor.doSave(null
+IWorkbenchPage page = editor.getSite().getPage();%0A%09%09page.saveEditor(editor, false
 );%0A%0A
-%09
 %09%09fo
@@ -1859,17 +1859,16 @@
 file);%0A%0A
-%09
 %09%09if (fo
@@ -1903,17 +1903,16 @@
 )) %7B%0A%09%09%09
-%09
 formater
@@ -1939,13 +1939,8 @@
 e);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A

refactor saving file logic