package com.onpositive.gae.tools;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import com.onpositive.gae.tools.core.CheckLaunchJob;
import com.onpositive.gae.tools.core.DebugServerLaunchManager;
public class OpenApplicationDashboardAction implements
IWorkbenchWindowActionDelegate {
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
public void run(IAction action) {
IJavaProject javaGaeProject = UIUtils.getJavaGaeProject();
if (javaGaeProject != null) {
String appId = GaeBridge.getAppId(javaGaeProject.getProject());
if (appId == null || appId.trim().length() == 0) {
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Error", "Please setup application id for "
+ javaGaeProject.getElementName()
+ " before using open dashboard action");
return;
}
try {
IWebBrowser createBrowser = PlatformUI
.getWorkbench()
.getBrowserSupport()
.createBrowser(SWT.NONE, "appwrench", "Dashboard",
"Application dashboard");
try {
if (isDebug()) {
openLocal(javaGaeProject, createBrowser);
} else {
createBrowser.openURL(new URL(
"http://appengine.google.com/dashboard?&app_id="
+ appId));
}
} catch (MalformedURLException e) {
Activator.log(e);
}
} catch (PartInitException e) {
Activator.log(e);
}
}
}
private boolean isDebug() {
boolean isLocal = Platform.getPreferencesService().getBoolean(
"com.onpositive.gae.base.manager", "CONNECTION_MODE", false,
null);
return isLocal;
}
private int openLocal(IJavaProject project, IWebBrowser createBrowser) {
int port = DebugServerLaunchManager.getPort(project);
if (port == -1) {
CheckLaunchJob checkLaunchJob = new CheckLaunchJob(
"Checking Launch", project, true);
// try {
checkLaunchJob.schedule();
int a = 0;
IStatus result = null;
long l1 = System.currentTimeMillis();
long l2 = System.currentTimeMillis();
while ((result = checkLaunchJob.getResult()) == null && l2-l1 <= 30000) {
Thread.yield();
Display.getCurrent().readAndDispatch();
l2 = System.currentTimeMillis();
a++;
}
// } catch (InterruptedException e) {
//
// }
port = DebugServerLaunchManager.getPort(project);
}
String connectionString = "http://localhost:" + port + "/_ah/admin";
try {
createBrowser.openURL(new URL(connectionString));
} catch (PartInitException e) {
Activator.log(e);
} catch (MalformedURLException e) {
Activator.log(e);
}
return port;
}
public void selectionChanged(IAction action, ISelection selection) {
}
}