package com.onpositive.google.scrapbook;
import java.io.ByteArrayInputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import com.onpositive.gae.tools.Constants;
import com.onpositive.gae.tools.GaeToolsViewerAction;
import com.onpositive.gae.tools.UIUtils;
import com.onpositive.gae.tools.deploy.CheckTools;
import com.onpositive.gae.tools.deploy.ConfigureTools;
import com.onpositive.gae.tools.deploy.PrepareGaeProject;
public class OpenConsoleAction extends GaeToolsViewerAction implements IWorkbenchWindowActionDelegate {
public OpenConsoleAction(String text, boolean debug2) {
super(text, debug2);
}
private IJavaProject fJavaProject;
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
public void run(IAction action) {
run();
}
public void run() {
final IJavaProject javaGaeProject = UIUtils.getJavaGaeProject();
if (javaGaeProject == null) {
return;
}
fJavaProject = javaGaeProject;
CheckTools check = new CheckTools("Check tools", fJavaProject,
Activator.getDefault().getBundle());
if (!check.doCheck()) {
boolean openConfirm = MessageDialog
.openConfirm(
Display.getCurrent().getActiveShell(),
"Project should be configured to add OnPositive Concole Support",
"Do you want to configure project to add OnPositive Console Support now?");
if (openConfirm) {
PrepareGaeProject prepareGaeProject = new ConfigureTools(
"Preparing Google Web Application Project",
fJavaProject, Activator.getDefault().getBundle(),true);
PlatformUI.getWorkbench().getProgressService().showInDialog(
null, prepareGaeProject);
prepareGaeProject.schedule();
prepareGaeProject.addJobChangeListener(new JobChangeAdapter(){
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()){
Display.getDefault().asyncExec(new Runnable(){
public void run() {
internalRun();
}
});
}
super.done(event);
}
});
}
} else {
internalRun();
}
}
private void internalRun() {
IJavaProject fJavaProject2 = fJavaProject;
openConsole(fJavaProject2,this.debug);
}
public static void openConsole(IJavaProject fJavaProject2,boolean isDebug) {
IFolder folder = fJavaProject2.getProject().getFolder(
Constants.TOOLS_FILES_FOLDER);
try {
if (!folder.exists()) {
folder.create(true, true, new NullProgressMonitor());
}
IFile file = folder.getFile("console.snippet");
if (!file.exists()) {
file.create(new ByteArrayInputStream(("import com.google.appengine.api.datastore.*;\r\n" +
"import com.google.appengine.api.images.*;\r\n" +
"import com.google.appengine.api.xmpp.*;\r\n" +
"import com.google.appengine.api.mail.*;\r\n" +
"import com.google.appengine.api.urlfetch.*;\r\n" +
"import com.google.appengine.api.memcache.*;\r\n" +
"import com.google.appengine.api.labs.taskqueue.*;\r\n" +
"//Say Hello\r\n" +
"out.print(\"Hello world\");").getBytes()),
true, new NullProgressMonitor());
}
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage();
ConsoleEditor openEditor = (ConsoleEditor) activePage.openEditor(new FileEditorInput(file),
"com.onpositive.google.scrapbook.javaeditor");
} catch (CoreException e) {
Activator.getDefault().log(e);
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Error", e.getMessage());
}
}
public void selectionChanged(IAction action, ISelection selection) {
}
protected void openViewer(IJavaProject project, String url)
throws PartInitException {
}
}