aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert von Massow2011-03-02 10:25:50 (EST)
committersefftinge2011-03-02 11:46:55 (EST)
commite05c8d41bf50157c77ce3200bc7c5ccfd1b6269b (patch)
treef954be95360d24e79110e303d5cd239384ef449c
parent33cbb4885db25c0dacb5ad0006800901ed2e084d (diff)
downloadorg.eclipse.xtext-e05c8d41bf50157c77ce3200bc7c5ccfd1b6269b.zip
org.eclipse.xtext-e05c8d41bf50157c77ce3200bc7c5ccfd1b6269b.tar.gz
org.eclipse.xtext-e05c8d41bf50157c77ce3200bc7c5ccfd1b6269b.tar.bz2
New class wizard field initialization util
-rw-r--r--plugins/org.eclipse.xtext.xtend2.ui/src/org/eclipse/xtext/xtend2/ui/wizards/FieldInitializerUtil.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/plugins/org.eclipse.xtext.xtend2.ui/src/org/eclipse/xtext/xtend2/ui/wizards/FieldInitializerUtil.java b/plugins/org.eclipse.xtext.xtend2.ui/src/org/eclipse/xtext/xtend2/ui/wizards/FieldInitializerUtil.java
new file mode 100644
index 0000000..ffd60f9
--- /dev/null
+++ b/plugins/org.eclipse.xtext.xtend2.ui/src/org/eclipse/xtext/xtend2/ui/wizards/FieldInitializerUtil.java
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.eclipse.xtext.xtend2.ui.wizards;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.views.contentoutline.ContentOutline;
+import org.eclipse.xtext.ui.editor.XtextEditor;
+import org.eclipse.xtext.ui.editor.model.IXtextDocument;
+
+/**
+ * @author Robert von Massow - Initial contribution and API
+ */
+public class FieldInitializerUtil {
+
+ public IJavaElement getSelectedResource(IStructuredSelection selection) {
+ IJavaElement elem = null;
+ if(selection != null && !selection.isEmpty()){
+ Object o = selection.getFirstElement();
+ if(o == null ) {
+ } else if(o instanceof IAdaptable) {
+ IAdaptable adaptable = (IAdaptable)o;
+ elem = (IJavaElement)adaptable.getAdapter(IJavaElement.class);
+ if(elem == null){
+ elem = getPackage(adaptable);
+ }
+ }
+ }
+ if (elem == null) {
+ IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ IWorkbenchPart part = activePage.getActivePart();
+ if (part instanceof ContentOutline) {
+ part= activePage.getActiveEditor();
+ }
+ if (part instanceof XtextEditor) {
+ IXtextDocument doc = ((XtextEditor)part).getDocument();
+ IFile file = doc.getAdapter(IFile.class);
+ elem = getPackage(file);
+ }
+ }
+ if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
+ try {
+ IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
+ if (projects.length == 1) {
+ elem= projects[0];
+ }
+ } catch (JavaModelException e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+ return elem;
+ }
+
+ private IJavaElement getPackage(IAdaptable adaptable) {
+ IJavaElement elem = null;
+ IResource resource = (IResource) adaptable.getAdapter(IResource.class);
+ if (resource != null && resource.getType() != IResource.ROOT) {
+ while(elem == null && resource.getType() != IResource.PROJECT){
+ resource = resource.getParent();
+ elem = (IJavaElement) resource.getAdapter(IJavaElement.class);
+ }
+ }
+ if (elem == null) {
+ elem = JavaCore.create(resource);
+ }
+ return elem;
+ }
+}