⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generatemenu.java

📁 eclipse的hibernate插件,支持eclipse2.*版本
💻 JAVA
字号:
package com.tanghan.plugin.hibernate.popup;import java.io.File;import java.io.FileWriter;import java.io.Writer;import java.util.ArrayList;import java.util.List;import java.util.ResourceBundle;import net.sf.hibernate.tool.class2hbm.MapGenerator;import org.apache.log4j.Logger;import org.eclipse.core.resources.IFile;import org.eclipse.core.resources.IResource;import org.eclipse.core.resources.ResourcesPlugin;import org.eclipse.jdt.core.IClasspathEntry;import org.eclipse.jdt.core.ICompilationUnit;import org.eclipse.jdt.core.IField;import org.eclipse.jdt.core.IType;import org.eclipse.jdt.core.JavaCore;import org.eclipse.jdt.core.JavaModelException;import org.eclipse.jface.action.IAction;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.util.Assert;import org.eclipse.jface.viewers.ISelection;import org.eclipse.jface.viewers.StructuredSelection;import org.eclipse.jface.window.Window;import org.eclipse.swt.widgets.Shell;import org.eclipse.ui.IObjectActionDelegate;import org.eclipse.ui.IWorkbenchPart;import com.tanghan.plugin.TanghanClassLoader;import com.tanghan.plugin.TanghanPlugin;import com.tanghan.util.Log;public class GenerateMenu implements IObjectActionDelegate {	private static Logger log = Log.getInstanse().getLogger(GenerateMenu.class);	private static ResourceBundle res = TanghanPlugin.getDefault().getResourceBundle();			private StructuredSelection currentSelection;	/**	 * Constructor for Action1.	 */	public GenerateMenu() {		super();	}	/**	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)	 */	public void setActivePart(IAction action, IWorkbenchPart targetPart) {	}	/**	 * @see IActionDelegate#run(IAction)	 */	public void run(IAction action) {			if(currentSelection==null || currentSelection.getFirstElement()== null){			return;		}				ICompilationUnit javaFile = getJavaFile();		if(javaFile==null){			Assert.isNotNull(javaFile);			return;		}		Writer outputWriter=null;		MapGenerator map = new MapGenerator( null, getTanghanClassLoader(javaFile).getClassLoader());		String pkgName =  getSelectClass(javaFile,true).replace('.', File.separatorChar);		//构建输出路径		outputWriter = makeWriter( getSourceDir(javaFile)+"/"+pkgName + ".hbm.xml" );		ArrayList ls= new ArrayList(5);		if(isNeedSetUID(javaFile,ls)){			boolean bCheck = true;			if(ls.size()>0){				SelectUIDDialog suid = new SelectUIDDialog(new Shell(),ls);				if(suid.open()==Window.OK){					map.addUID(suid.getSelectedUID());					bCheck = false;				}else{					MessageDialog.openWarning(new Shell(),res.getString("Tanghan.Plugin.Hibernate.menu.Warn"),																	res.getString("Tanghan.Plugin.Hibernate.menu.SelectUID"));					return;				}			}else{				MessageDialog.openWarning(new Shell(),res.getString("Tanghan.Plugin.Hibernate.menu.Warn"),																			res.getString("Tanghan.Plugin.Hibernate.menu.NoField"));				return;			}		}		map.addClass(getSelectClass(javaFile,true),true);		map.writeXML(outputWriter);		try{			outputWriter.close();		}catch(Exception ex){			log.error(ex.getMessage(),ex);		}				try{			javaFile.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE,null);		}catch(Exception ex){			log.error(ex.getMessage(),ex);		}	}		/**	 * @param javaFile	 * @return	 */	private boolean isNeedSetUID(ICompilationUnit javaFile, List fieldLs){		if(javaFile==null)			return true;		boolean bCheck = false;		try {			IType[] types = javaFile.getTypes();			if(types==null && types.length<1){				return false;			}			IField[] fields = types[0].getFields();			log.debug("IType : "+types[0].getElementName());			for(int j = 0; j< fields.length; j++){				log.debug("IField : "+fields[j].getElementName());				fieldLs.add(fields[j].getElementName());				for(int m = 0; m < MapGenerator.defaultKeys.length; m++){					if(MapGenerator.defaultKeys[m].equals(fields[j].getElementName())){						bCheck = true;						break;					}				}				if(bCheck){					break;				}			}		} catch (JavaModelException e) {			log.error(e.getMessage(),e);		}		return !bCheck;	}	private String getSourceDir(ICompilationUnit javaFile){		String dir = "";		try{			IClasspathEntry [] classPaths = javaFile.getJavaProject().getRawClasspath();			if(classPaths!=null && classPaths.length>0){				boolean bCheck = false; 				for(int i = 0; i<classPaths.length; i++){					switch(classPaths[i].getEntryKind()){						case IClasspathEntry.CPE_SOURCE :							log.debug("IClasspathEntry.CPE_SOURCE");							dir =ResourcesPlugin.getWorkspace().getRoot().findMember(classPaths[i].getPath()).getLocation().toOSString();							bCheck = true;							break;					}					if(bCheck){						break;					}				}			}		}catch(JavaModelException ex){			log.error(ex.getMessage(),ex);		}		return dir;	}	private TanghanClassLoader getTanghanClassLoader(ICompilationUnit javaFile){		//ICompilationUnit javaFile = getJavaFile();		TanghanClassLoader classLoader = new TanghanClassLoader(JavaCore.class.getClassLoader());		if(javaFile!=null){			try{				classLoader.loadJarFile(ResourcesPlugin.getWorkspace().getRoot().findMember(javaFile.getJavaProject().getOutputLocation()).getLocation().toOSString());				log.debug("ClassPath output:"+ResourcesPlugin.getWorkspace().getRoot().findMember(javaFile.getJavaProject().getOutputLocation()).getLocation().toOSString());			}catch(Exception ex){				log.error(ex.getMessage(),ex);			}		}		return classLoader;	}		private ICompilationUnit getJavaFile(){		ICompilationUnit javaFile =null;		if(currentSelection == null || currentSelection.getFirstElement()==null){			return null;		}		if(currentSelection.getFirstElement() instanceof ICompilationUnit){			javaFile = (ICompilationUnit)currentSelection.getFirstElement();		} else if(currentSelection.getFirstElement() instanceof IFile){			javaFile = JavaCore.createCompilationUnitFrom((IFile)currentSelection.getFirstElement());		}				return javaFile;	}	/**	 * @param fullClassName 	 * */	private String getSelectClass(ICompilationUnit javaFile,boolean fullClassName){		try{			IType[] types = javaFile.getTypes();			if(types!=null && types.length>0){				if(fullClassName){					return types[0].getFullyQualifiedName();				}else{					return types[0].getElementName();				}			}		}catch(Exception ex){			log.error(ex.getMessage(),ex);		}		return "";		}		/**	 * @see IActionDelegate#selectionChanged(IAction, ISelection)	 */	public void selectionChanged(IAction action, ISelection selection) {		if(selection instanceof StructuredSelection){			currentSelection = (StructuredSelection)selection;			//isNeedSetUID(this.getJavaFile());		}		log.debug(action.getClass());	}	private static FileWriter makeWriter( String fileName ) {		try {			FileWriter fw = new FileWriter( fileName );			return fw;		}		catch(Exception e) {			System.err.println("<!-- Error making FileWriter " + e.getMessage() + "-->");			//e.printStackTrace();			return null;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -