Package com.onpositive.gae.tools

Source Code of com.onpositive.gae.tools.UIUtils

package com.onpositive.gae.tools;

import java.util.HashSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

public class UIUtils {
 
 

  public static IJavaProject getJavaGaeProject() {
    ISelection selection = PlatformUI.getWorkbench()
        .getActiveWorkbenchWindow().getSelectionService()
        .getSelection();
    if (selection instanceof IStructuredSelection) {
      IStructuredSelection s = (IStructuredSelection) selection;
      if (!s.isEmpty()) {
        Object firstElement = s.getFirstElement();
        if (firstElement instanceof IAdaptable
            && !(firstElement instanceof IResource)) {
          IAdaptable p = (IAdaptable) firstElement;
          firstElement = p.getAdapter(IResource.class);
        }
        if (firstElement instanceof IResource) {
          IResource r = (IResource) firstElement;
          final IProject m = r.getProject();
           
          IJavaProject create = JavaCore.create(m);
          if (create.exists()){
            try {
              if (create.getProject().getNature(
                  Constants.GOOGLE_APP_ENGINE_NATURE) != null) {
                return create;
              }
            } catch (CoreException e) {
              Activator.log(e);
            }   
          }
        }

      }
    }
    IJavaModel create = JavaCore.create(ResourcesPlugin.getWorkspace()
        .getRoot());
    try {
      IJavaProject[] javaProjects = create.getJavaProjects();
      HashSet<IJavaProject> p = new HashSet<IJavaProject>();
      for (IJavaProject pa : javaProjects) {
        if (pa.getProject().getNature(
            Constants.GOOGLE_APP_ENGINE_NATURE) != null) {
          p.add(pa);
        }
      }
      ProjectSelectionDialog projectSelectionDialog = new ProjectSelectionDialog(
          Display.getCurrent().getActiveShell(), p,
          "Please select Google Web Project",
          "Please select Google Web Project to work with:");
      int open = projectSelectionDialog.open();
      if (open == Dialog.OK) {
        IJavaProject pm = (IJavaProject) projectSelectionDialog
            .getFirstResult();
        if (pm != null) {
          return pm;
        }
      }
    } catch (Exception e) {
      Activator.log(e);
      return null;
    }

    return null;
  }
}
TOP

Related Classes of com.onpositive.gae.tools.UIUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.