[jbosstools-commits] JBoss Tools SVN: r22881 - in trunk/hibernatetools: plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect and 2 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Jun 18 10:35:32 EDT 2010


Author: vyemialyanchyk
Date: 2010-06-18 10:35:31 -0400 (Fri, 18 Jun 2010)
New Revision: 22881

Removed:
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java
Modified:
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CompilationUnitCollector.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/HibernateJPAWizardDataFactory.java
   trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java
Log:
https://jira.jboss.org/browse/JBIDE-6468 - fixed

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java	2010-06-18 14:22:54 UTC (rev 22880)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java	2010-06-18 14:35:31 UTC (rev 22881)
@@ -10,6 +10,8 @@
   ******************************************************************************/
 package org.hibernate.eclipse.jdt.ui.internal.jpa.actions;
 
+import java.lang.ref.WeakReference;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -17,6 +19,10 @@
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.expressions.EvaluationContext;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.viewers.ISelection;
@@ -39,14 +45,15 @@
 public class JPAMapToolActionDelegate extends AbstractHandler implements IObjectActionDelegate,
 	IEditorActionDelegate, IViewActionDelegate {
 
-	public JPAMapToolActor actor = JPAMapToolActor.getInstance();
+	protected JPAMapToolActor actor = new JPAMapToolActor();
 
+	protected WeakReference<Object> refContextObject = null;
+
 	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
 	}
 
 	public void run(IAction action) {
-		actor.updateSelected(Integer.MAX_VALUE);
-		//actor.updateOpen();
+		runInternal();
 	}
 
 	public void selectionChanged(IAction action, ISelection selection) {
@@ -63,11 +70,17 @@
 	}
 
 	public Object execute(ExecutionEvent event) throws ExecutionException {
-		actor.updateSelected(Integer.MAX_VALUE);
-		//actor.updateOpen();
+		runInternal();
 		return null;
 	}
 
+	public void runInternal() {
+		if (refContextObject != null) {
+			processContextObjectElements();
+		}
+		actor.updateSelected(Integer.MAX_VALUE);
+	}
+
 	public void init(IViewPart view) {
 	}
 	public boolean isCUSelected() {
@@ -86,28 +99,77 @@
 		return false;
 	}
 
-	@SuppressWarnings("unchecked")
 	public void setEnabled(Object evaluationContext) {
-		boolean enable = false;
-		actor.setSelection(null);
-		actor.clearSelectionCU();
-		if (!enable && evaluationContext instanceof EvaluationContext) {
+		if (evaluationContext instanceof EvaluationContext) {
 			EvaluationContext ec = (EvaluationContext)evaluationContext;
 			Object obj = ec.getDefaultVariable();
-			if (obj instanceof List) {
-				Iterator it = ((List)obj).iterator();
-				while (it.hasNext()) {
-					obj = it.next();
-					actor.processJavaElements(obj);
+			refContextObject = new WeakReference<Object>(obj);
+		} else {
+			refContextObject = null;
+		}
+		setBaseEnabled(checkEnabled());
+	}
+
+	protected boolean checkEnabled() {
+		boolean enable = false;
+		Object obj = refContextObject != null ? refContextObject.get() : null;
+		if (obj == null) {
+			return enable;
+		}
+		List<?> list = null;
+		if (obj instanceof List) {
+			list = (List<?>)obj;
+		} else {
+			list = Collections.singletonList(obj);
+		}
+		Iterator<?> it = list.iterator();
+		while (it.hasNext() && !enable) {
+			Object obj2 = it.next();
+			if (!(obj2 instanceof IJavaElement)) {
+				continue;
+			}
+			int kind = IPackageFragmentRoot.K_SOURCE;
+			if (obj2 instanceof IPackageFragment) {
+				IPackageFragment pf = (IPackageFragment)obj2;
+				try {
+					kind = pf.getKind();
+				} catch (JavaModelException e) {
+					kind = IPackageFragmentRoot.K_BINARY;
 				}
-			} else {
-				actor.processJavaElements(obj);
+			} else if (obj2 instanceof IPackageFragmentRoot) {
+				IPackageFragmentRoot pfr = (IPackageFragmentRoot)obj2;
+				try {
+					kind = pfr.getKind();
+				} catch (JavaModelException e) {
+					kind = IPackageFragmentRoot.K_BINARY;
+				}
 			}
-			enable = actor.getSelectionCUSize() > 0;
+			if (kind == IPackageFragmentRoot.K_SOURCE) {
+				enable = true;
+			}
 		}
 		if (!enable) {
 			enable = isCUSelected();
 		}
-		setBaseEnabled(enable);
+		return enable;
 	}
+
+	public void processContextObjectElements() {
+		actor.setSelection(null);
+		actor.clearSelectionCU();
+		Object obj = refContextObject != null ? refContextObject.get() : null;
+		if (obj == null) {
+			return;
+		}
+		if (obj instanceof List) {
+			@SuppressWarnings("rawtypes")
+			Iterator it = ((List)obj).iterator();
+			while (it.hasNext()) {
+				obj = it.next();
+				actor.processJavaElements(obj);
+			}
+		} else {
+			actor.processJavaElements(obj);
+		}
+	}
 }

Deleted: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java	2010-06-18 14:22:54 UTC (rev 22880)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java	2010-06-18 14:35:31 UTC (rev 22881)
@@ -1,152 +0,0 @@
-/*******************************************************************************
-  * Copyright (c) 2007-2008 Red Hat, Inc.
-  * Distributed under license by Red Hat, Inc. All rights reserved.
-  * This program is 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
-  *
-  * Contributor:
-  *     Red Hat, Inc. - initial API and implementation
-  ******************************************************************************/
-package org.hibernate.eclipse.jdt.ui.internal.jpa.actions;
-
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.ActionContributionItem;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.action.IMenuCreator;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.swt.events.MenuAdapter;
-import org.eclipse.swt.events.MenuEvent;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Menu;
-import org.eclipse.swt.widgets.MenuItem;
-import org.eclipse.ui.IActionDelegate2;
-import org.eclipse.ui.IEditorActionDelegate;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
-import org.hibernate.eclipse.jdt.ui.internal.JdtUiMessages;
-
-/**
- * Context menu action delegate for "Generate Hibernate/JPA annotations..."
- *
- * @author Vitali
- */
-public class JPAMapToolActionPulldownDelegate implements
-		IWorkbenchWindowPulldownDelegate2, IEditorActionDelegate, IActionDelegate2, IMenuCreator {
-
-	/**
-	 * The menu created by this action
-	 */
-	private Menu fMenu;
-	protected boolean fRecreateMenu = false;
-
-	public JPAMapToolActor actor = JPAMapToolActor.getInstance();
-
-	public Menu getMenu(Menu parent) {
-		setMenu(new Menu(parent));
-		fillMenu(fMenu);
-		initMenu();
-		return fMenu;
-	}
-
-	public Menu getMenu(Control parent) {
-		setMenu(new Menu(parent));
-		fillMenu(fMenu);
-		initMenu();
-		return fMenu;
-	}
-
-	public void dispose() {
-		setMenu(null);
-	}
-
-	public void init(IWorkbenchWindow window) {
-	}
-
-	public void run(IAction action) {
-	}
-
-	public void selectionChanged(IAction action, ISelection selection) {
-		actor.setSelection(selection);
-		if (action != null) {
-			action.setEnabled(actor.getSelectedSourceSize() > 0);
-		}
-	}
-
-	protected void addToMenu(Menu menu, IAction action, int accelerator) {
-		StringBuffer label = new StringBuffer();
-		if (accelerator >= 0 && accelerator < 10) {
-			// add the numerical accelerator
-			label.append('&');
-			label.append(accelerator);
-			label.append(' ');
-		}
-		label.append(action.getText());
-		action.setText(label.toString());
-		ActionContributionItem item = new ActionContributionItem(action);
-		item.fill(menu, -1);
-	}
-
-	protected void fillMenu(Menu menu) {
-		IAction action = new Action(JdtUiMessages.JPAMapToolActionPulldownDelegate_menu) {
-			public void run() {
-				//actor.updateOpen();
-				actor.updateSelected(Integer.MAX_VALUE);
-			}
-		};
-		addToMenu(menu, action, -1);
-	}
-
-	private void initMenu() {
-		// Add listener to re-populate the menu each time
-		// it is shown because of dynamic list
-		fMenu.addMenuListener(new MenuAdapter() {
-			public void menuShown(MenuEvent e) {
-				Menu m = (Menu) e.widget;
-				if (fRecreateMenu) {
-					MenuItem[] items = m.getItems();
-					for (int i = 0; i < items.length; i++) {
-						items[i].dispose();
-					}
-					fillMenu(m);
-					fRecreateMenu = false;
-				}
-				m.setEnabled(actor.getSelectedSourceSize() > 0);
-				MenuItem[] items = m.getItems();
-				for (int i = 0; i < items.length; i++) {
-					items[i].setEnabled(actor.getSelectedSourceSize() > 0);
-				}
-			}
-			public void menuHidden(MenuEvent e) {
-				fRecreateMenu = true;
-			}
-		});
-	}
-
-	private void setMenu(Menu menu) {
-		if (fMenu != null) {
-			fMenu.dispose();
-		}
-		fMenu = menu;
-	}
-
-	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
-		if (action != null) {
-			action.setEnabled(actor.getSelectedSourceSize() > 0);
-		}
-	}
-
-	public void init(IAction action) {
-		if (action instanceof Action) {
-			((Action)action).setMenuCreator(this);
-		}
-	}
-
-	public void runWithEvent(IAction action, Event event) {
-		//actor.updateOpen();
-		actor.updateSelected(Integer.MAX_VALUE);
-	}
-
-}

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java	2010-06-18 14:22:54 UTC (rev 22880)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java	2010-06-18 14:35:31 UTC (rev 22881)
@@ -52,7 +52,6 @@
 
 /**
  * Actor to execute annotation generation.
- * It is singleton.
  *
  * @author Vitali Yemialyanchyk
  */
@@ -60,10 +59,6 @@
 public class JPAMapToolActor {
 
 	/**
-	 * instance
-	 */
-	private static JPAMapToolActor actor = null;
-	/**
 	 * selection - start point to generate annotations
 	 * could be java file, list of files, package, project, some other?
 	 */
@@ -82,7 +77,7 @@
 	 */
 	protected AllEntitiesProcessor processor = new AllEntitiesProcessor();
 
-	protected JPAMapToolActor() {
+	public JPAMapToolActor() {
 		initPreferences();
 	}
 
@@ -90,13 +85,6 @@
 		processor.initPreferences();
 	}
 
-	public static JPAMapToolActor getInstance() {
-		if (actor == null) {
-			actor = new JPAMapToolActor();
-		}
-		return actor;
-	}
-
 	/**
 	 * Cleanup collection of selected elements for processing
 	 */

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CompilationUnitCollector.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CompilationUnitCollector.java	2010-06-18 14:22:54 UTC (rev 22880)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CompilationUnitCollector.java	2010-06-18 14:35:31 UTC (rev 22881)
@@ -104,6 +104,7 @@
 	/**
 	 * Process object - java element to collect all it's children CompilationUnits
 	 * @param obj
+	 * @param bCollect
 	 */
 	public void processJavaElements(Object obj, boolean bCollect) {
 		if (obj instanceof ICompilationUnit) {

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/HibernateJPAWizardDataFactory.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/HibernateJPAWizardDataFactory.java	2010-06-18 14:22:54 UTC (rev 22880)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/HibernateJPAWizardDataFactory.java	2010-06-18 14:35:31 UTC (rev 22881)
@@ -49,12 +49,12 @@
 		return data;
 	}
 
-	@SuppressWarnings("unchecked")
 	static public IHibernateJPAWizardData createHibernateJPAWizardData(
 			final IStructuredSelection selection2Update, 
 			IHibernateJPAWizardParams params, int depth) {
 
 		CompilationUnitCollector compileUnitCollector = new CompilationUnitCollector();
+		@SuppressWarnings("rawtypes")
 		Iterator itSelection2Update = selection2Update.iterator();
 		while (itSelection2Update.hasNext()) {
 			Object obj = itSelection2Update.next();

Modified: trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java
===================================================================
--- trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java	2010-06-18 14:22:54 UTC (rev 22880)
+++ trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java	2010-06-18 14:35:31 UTC (rev 22881)
@@ -71,7 +71,7 @@
 
 	public void testJPAMapToolActor() {
 
-		final JPAMapToolActor jpaMapToolActor = JPAMapToolActor.getInstance();
+		final JPAMapToolActor jpaMapToolActor = new JPAMapToolActor();
 		
 		final AllEntitiesProcessor allEntitiesProcessor = context.mock(AllEntitiesProcessor.class);
 		final AllEntitiesInfoCollector allEntitiesInfoCollector = context.mock(AllEntitiesInfoCollector.class);



More information about the jbosstools-commits mailing list