package sd.wizards; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExecutableExtension; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.core.IClasspathContainer; import org.eclipse.jdt.core.IClasspathEntry; 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.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart; import org.eclipse.jdt.internal.ui.util.ExceptionHandler; import org.eclipse.jdt.internal.ui.wizards.NewElementWizard; import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport; import org.eclipse.jdt.ui.IPackagesViewPart; import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageOne; import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageTwo; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkingSet; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard; @SuppressWarnings("restriction") public class SampleNewWizard extends NewElementWizard implements IExecutableExtension { private NewJavaProjectWizardPageOne fFirstPage; private NewJavaProjectWizardPageTwo fSecondPage; private IConfigurationElement fConfigElement; public SampleNewWizard() { this(null, null); } public SampleNewWizard(NewJavaProjectWizardPageOne pageOne, NewJavaProjectWizardPageTwo pageTwo) { setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_NEWJPRJ); setDialogSettings(JavaPlugin.getDefault().getDialogSettings()); setWindowTitle(NewWizardMessages.JavaProjectWizard_title); this.fFirstPage = pageOne; this.fSecondPage = pageTwo; } public void addPages() { if (this.fFirstPage == null) this.fFirstPage = new NewJavaProjectWizardPageOne(); addPage(this.fFirstPage); if (this.fSecondPage == null) this.fSecondPage = new NewJavaProjectWizardPageTwo(this.fFirstPage); addPage(this.fSecondPage); this.fFirstPage.init(getSelection(), getActivePart()); } protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException { this.fSecondPage.performFinish(monitor); } public boolean performFinish() { IPath path = JavaCore.getClasspathVariable("VCI_HOME"); Hashtable> paths = getVCIUserLibraray(path .toFile().getAbsolutePath()); if (paths.isEmpty()) { MessageDialog.openError(getShell(), "Alert!", "Preferences VCI Datasource (VCI_HOME:) Config Wrong!"); return true; } // IEclipsePreferences instancePreferences = JavaModelManager // .getJavaModelManager().getInstancePreferences(); boolean res = super.performFinish(); if (res) { final IJavaElement newElement = getCreatedElement(); IWorkingSet[] workingSets = this.fFirstPage.getWorkingSets(); if (workingSets.length > 0) { PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(newElement, workingSets); } BasicNewProjectResourceWizard.updatePerspective(this.fConfigElement); selectAndReveal(this.fSecondPage.getJavaProject().getProject()); Display.getDefault().asyncExec(new Runnable() { public void run() { IWorkbenchPart activePart = SampleNewWizard.this.getActivePart(); if (activePart instanceof IPackagesViewPart) { PackageExplorerPart view = PackageExplorerPart.openInActivePerspective(); view.tryToReveal(newElement); } } }); } // JavaCore jCore = JavaCore.getJavaCore(); // IPath path = jCore.getClasspathVariable("VCI_HOME"); IClasspathEntry tEntry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_THIRDLIB")); IClasspathEntry pEntry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_PLATFORM")); IClasspathEntry cEntry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_CLIENT")); IClasspathEntry coEntry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_COMMONS")); IClasspathEntry sEntry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_SERVERS")); IProject Project = this.fSecondPage.getJavaProject().getProject(); IJavaProject jProject = JavaCore.create(Project); // ����java���̵��û��� try { IClasspathContainer classTContainer = new IClasspathContainer() { public IClasspathEntry[] getClasspathEntries() { return new IClasspathEntry[] { JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false) }; } public String getDescription() { return "Basic JDK Library container"; } public int getKind() { return IClasspathContainer.K_APPLICATION; } public IPath getPath() { return new Path("org.eclipse.jdt.USER_LIBRARY/VCI_THIRDLIB"); } }; IClasspathContainer classPContainer = new IClasspathContainer() { public IClasspathEntry[] getClasspathEntries() { return new IClasspathEntry[] { JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false) }; } public String getDescription() { return "Basic JDK Library container"; } public int getKind() { return IClasspathContainer.K_APPLICATION; } public IPath getPath() { return new Path("org.eclipse.jdt.USER_LIBRARY/VCI_PLATFORM"); } }; IClasspathContainer classCContainer = new IClasspathContainer() { public IClasspathEntry[] getClasspathEntries() { return new IClasspathEntry[] { JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false) }; } public String getDescription() { return "Basic JDK Library container"; } public int getKind() { return IClasspathContainer.K_APPLICATION; } public IPath getPath() { return new Path("org.eclipse.jdt.USER_LIBRARY/VCI_CLIENT"); } }; IClasspathContainer classCoContainer = new IClasspathContainer() { public IClasspathEntry[] getClasspathEntries() { return new IClasspathEntry[] { JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false) }; } public String getDescription() { return "Basic JDK Library container"; } public int getKind() { return IClasspathContainer.K_APPLICATION; } public IPath getPath() { return new Path("org.eclipse.jdt.USER_LIBRARY/VCI_COMMONS"); } }; IClasspathContainer classSContainer = new IClasspathContainer() { public IClasspathEntry[] getClasspathEntries() { return new IClasspathEntry[] { JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false) }; } public String getDescription() { return "Basic JDK Library container"; } public int getKind() { return IClasspathContainer.K_APPLICATION; } public IPath getPath() { return new Path("org.eclipse.jdt.USER_LIBRARY/VCI_SERVERS"); } }; BuildPathSupport.requestContainerUpdate(jProject, classTContainer, getClasspathEntry(paths, "VCI_THIRDLIB")); BuildPathSupport.requestContainerUpdate(jProject, classPContainer, getClasspathEntry(paths, "VCI_PLATFORM")); BuildPathSupport.requestContainerUpdate(jProject, classCContainer, getClasspathEntry(paths, "VCI_CLIENT")); BuildPathSupport.requestContainerUpdate(jProject, classCoContainer, getClasspathEntry(paths, "VCI_COMMONS")); BuildPathSupport.requestContainerUpdate(jProject, classSContainer, getClasspathEntry(paths, "VCI_SERVERS")); // JavaCore.setClasspathContainer(new Path( // "org.eclipse.jdt.USER_LIBRARY/My_1"), // new IJavaProject[] { jProject }, // new IClasspathContainer[] {classPContainer}, null); } catch (JavaModelException e1) { e1.printStackTrace(); } catch (CoreException localCoreException) { localCoreException.printStackTrace(); } // ����java���̵�Ŀ¼�ṹ // IWorkSpaceRoot root = new ResourcesPlugin().getWorkspace().getRoot(); IClasspathEntry[] oldclasspathEntrys; try { IFolder Common = jProject.getProject().getFolder("Common"); IFolder Server = jProject.getProject().getFolder("Server"); IFolder Client = jProject.getProject().getFolder("Client"); // IFolder properties = // jProject.getProject().getFolder("properties"); // IFolder Src = jProject.getProject().getFolder("src");\properties // try { Common.create(true, true, null); Server.create(true, true, null); Client.create(true, true, null); // properties.create(true, true, null); IClasspathEntry CommonSrc = JavaCore.newSourceEntry(Common.getFullPath()); IClasspathEntry ServerSrc = JavaCore.newSourceEntry(Server.getFullPath()); IClasspathEntry ClientSrc = JavaCore.newSourceEntry(Client.getFullPath()); // IClasspathEntry src = JavaCore.newSourceEntry(Src.getFullPath()); // } // ///// oldclasspathEntrys = jProject.getRawClasspath(); List oldclasspaths = new ArrayList(); for (IClasspathEntry entry : oldclasspathEntrys) { oldclasspaths.add(entry); } // userlibrary oldclasspaths.add((IClasspathEntry) tEntry); oldclasspaths.add((IClasspathEntry) pEntry); oldclasspaths.add((IClasspathEntry) cEntry); oldclasspaths.add((IClasspathEntry) coEntry); oldclasspaths.add((IClasspathEntry) sEntry); // src oldclasspaths.add((IClasspathEntry) CommonSrc); oldclasspaths.add((IClasspathEntry) ServerSrc); oldclasspaths.add((IClasspathEntry) ClientSrc); // remove src // if (oldclasspaths.contains(src)) { // oldclasspaths.remove(src); // } jProject.setRawClasspath(oldclasspaths.toArray(new IClasspathEntry[] {}), null); } catch (JavaModelException e) { e.printStackTrace(); } catch (CoreException e) { e.printStackTrace(); } return res; } private IClasspathEntry[] getClasspathEntry( Hashtable> paths, String userLibrary) { IPath path = JavaCore.getClasspathVariable("VCI_HOME"); List jarpaths = paths.get(userLibrary); IClasspathEntry[] classpathEntrys = new IClasspathEntry[jarpaths.size()]; int i = 0; for (String o : jarpaths) { String name = o.substring(o.lastIndexOf("\\") + 1, o.lastIndexOf(".")); String src = path + "\\dist\\" + name + "_src.jar"; String root = path + "\\dist"; File isExist = new File(src); if (isExist.exists()) { classpathEntrys[i] = JavaCore.newLibraryEntry(new Path(o), new Path(src), new Path(root), false); } else { classpathEntrys[i] = JavaCore.newLibraryEntry(new Path(o), null, null, false); } i++; } return classpathEntrys; } private IWorkbenchPart getActivePart() { IWorkbenchWindow activeWindow = getWorkbench() .getActiveWorkbenchWindow(); if (activeWindow != null) { IWorkbenchPage activePage = activeWindow.getActivePage(); if (activePage != null) { return activePage.getActivePart(); } } return null; } private Hashtable> getVCIUserLibraray(String VCI_HOME) { Hashtable> pathTables = new Hashtable>(); List modules = new ArrayList(); File file = new File(VCI_HOME + "\\libs"); File[] jarFiles = file.listFiles(); if (jarFiles == null) { return pathTables; } try { List platformpaths = new ArrayList(); List libpaths = new ArrayList(); List publicpaths = new ArrayList(); List privatepaths = new ArrayList(); List clientpaths = new ArrayList(); for (int i = 0; i < jarFiles.length; i++) { String filename = jarFiles[i].getName(); if (filename != null && jarFiles[i].isDirectory()){ if(filename.equals("modules")) { File[] modulefiles = jarFiles[i].listFiles(); for (File module : modulefiles) { if (module.getName().contains(".svn")) { continue; } if (module.isDirectory()) modules.add(module); } } else if (filename.equals("lib")) { File[] libfiles = jarFiles[i].listFiles(); for (int j = 0; j < libfiles.length; j++) { String libfilename = libfiles[j].getName(); if (libfiles[j].isFile() && isJarFile(libfilename)) { String jarPath = libfiles[j].getAbsolutePath(); libpaths.add(jarPath); } } } else if (filename.equals("base")) { File[] libfiles = jarFiles[i].listFiles(); for (int j = 0; j < libfiles.length; j++) { String libfilename = libfiles[j].getName(); if (libfiles[j].isFile() && isJarFile(libfilename)) { String jarPath = libfiles[j].getAbsolutePath(); platformpaths.add(jarPath); } } } } if (jarFiles[i].isFile() && isJarFile(filename)) { String jarPath = jarFiles[i].getAbsolutePath(); platformpaths.add(jarPath); } } List clients = new ArrayList(); List commons = new ArrayList(); List servers = new ArrayList(); for (File f : modules) { File[] files = f.listFiles(); if (files == null) continue; for (File folder : files) { // single if (!folder.isDirectory()) continue; String sfolderName = folder.getName(); if (sfolderName.equals("client")) { clients.add(folder); } else if (sfolderName.equals("common")) { commons.add(folder); } else if (sfolderName.equals("server")) { servers.add(folder); } else { File[] subFolders = folder.listFiles(); if (subFolders == null) continue; for (File subFolder : subFolders) { if (!subFolder.isDirectory()) continue; String folderName = subFolder.getName(); if (folderName.equals("client")) { clients.add(subFolder); } else if (folderName.equals("common")) { commons.add(subFolder); } else if (folderName.equals("server")) { servers.add(subFolder); } } } } } // servers for (File sFolder : servers) { File[] files = sFolder.listFiles(); if (files == null || files.length == 0) continue; for (int i = 0; i < files.length; i++) { String sfilename = files[i].getName(); if (files[i].isFile() && isJarFile(sfilename)) { String sjarPath = files[i].getAbsolutePath(); privatepaths.add(sjarPath); } } } // commons for (File coFolder : commons) { File[] files = coFolder.listFiles(); if (files == null || files.length == 0) continue; for (int i = 0; i < files.length; i++) { String cofilename = files[i].getName(); if (files[i].isFile() && isJarFile(cofilename)) { String cojarPath = files[i].getAbsolutePath(); publicpaths.add(cojarPath); } } } // client for (File cFolder : clients) { File[] files = cFolder.listFiles(); if (files == null || files.length == 0) continue; for (int i = 0; i < files.length; i++) { String cfilename = files[i].getName(); if (files[i].isFile() && isJarFile(cfilename)) { String cjarPath = files[i].getAbsolutePath(); clientpaths.add(cjarPath); } } } pathTables.put("VCI_THIRDLIB", libpaths); pathTables.put("VCI_PLATFORM", platformpaths); pathTables.put("VCI_CLIENT", clientpaths); pathTables.put("VCI_COMMONS", publicpaths); pathTables.put("VCI_SERVERS", privatepaths); }catch (Exception e1) { MessageDialog.openError(null, "错误!", "读取目录下jar文件失败!"); } return pathTables; } @SuppressWarnings("unused") private Hashtable> getVCIUserLibraray_Old(String VCI_HOME) { Hashtable> pathTables = new Hashtable>(); List modules = new ArrayList(); File file = new File(VCI_HOME + "\\libs"); File[] jarFiles = file.listFiles(); if (jarFiles == null) { return pathTables; } List libpaths = new ArrayList(); List platformpaths = new ArrayList(); List publicpaths = new ArrayList(); List privatepaths = new ArrayList(); List clientpaths = new ArrayList(); for (int i = 0; i < jarFiles.length; i++) { String filename = jarFiles[i].getName(); if (filename != null && jarFiles[i].isDirectory()){ if(filename.equals("modules")) { File[] modulefiles = jarFiles[i].listFiles(); for (File module : modulefiles) { if (module.getName().contains(".svn")) { continue; } if (module.isDirectory()) modules.add(module); } } else if (filename.equals("lib")) { File[] files = jarFiles[i].listFiles(); for (int j = 0; j < files.length; j++) { String libfilename = files[j].getName(); if (files[j].isFile() && isJarFile(libfilename)) { String jarPath = files[j].getAbsolutePath(); libpaths.add(jarPath); } } } else if (filename.equals("base")) { File[] files = jarFiles[i].listFiles(); for (int j = 0; j < files.length; j++) { String libfilename = files[j].getName(); if (files[j].isFile() && isJarFile(libfilename)) { String jarPath = files[j].getAbsolutePath(); platformpaths.add(jarPath); } } } } if (isJarFile(filename)) { String jarPath = jarFiles[i].getAbsolutePath(); platformpaths.add(jarPath); } } List clients = new ArrayList(); List commons = new ArrayList(); List servers = new ArrayList(); for (File f : modules) { File[] plugins = f.listFiles(); if (plugins == null) continue; for (int j = 0; j < plugins.length; j++) { // single File sfolder = plugins[j]; String sfolderName = sfolder.getName(); if (!sfolder.isDirectory()) continue; if (sfolderName.equals("client")) { clients.add(sfolder); } else if (sfolderName.equals("common")) { commons.add(sfolder); } else if (sfolderName.equals("server")) { servers.add(sfolder); } } for (int j = 0; j < plugins.length; j++) { // cascade File fileOne= plugins[j]; File[] folders = fileOne.listFiles(); for (File folder : folders) { String folderName = folder.getName(); if (!folder.isDirectory()) continue; if (folderName.equals("client")) { clients.add(folder); } else if (folderName.equals("common")) { commons.add(folder); } else if (folderName.equals("server")) { servers.add(folder); } } } } // servers for (File sFolder : servers) { File[] files = sFolder.listFiles(); for (int i = 0; i < files.length; i++) { String sfilename = files[i].getName(); if (files[i].isFile() && isJarFile(sfilename)) { String sjarPath = files[i].getAbsolutePath(); privatepaths.add(sjarPath); } } } // commons for (File coFolder : commons) { File[] files = coFolder.listFiles(); for (int i = 0; i < files.length; i++) { String cofilename = files[i].getName(); if (files[i].isFile() && isJarFile(cofilename)) { String cojarPath = files[i].getAbsolutePath(); publicpaths.add(cojarPath); } } } // client for (File cFolder : clients) { File[] files = cFolder.listFiles(); for (int i = 0; i < files.length; i++) { String cfilename = files[i].getName(); if (files[i].isFile() && isJarFile(cfilename)) { String cjarPath = files[i].getAbsolutePath(); clientpaths.add(cjarPath); } } } pathTables.put("VCI_THIRDLIB", libpaths); pathTables.put("VCI_PLATFORM", platformpaths); pathTables.put("VCI_CLIENT", clientpaths); pathTables.put("VCI_COMMONS", publicpaths); pathTables.put("VCI_SERVERS", privatepaths); return pathTables; } // private boolean isFolder(String fileName) { // int last = fileName.lastIndexOf("."); // if (last == -1) { // return true; // } else { // return false; // } // } private boolean isJarFile(String fileName) { int last = fileName.lastIndexOf("."); if (last == -1) { return false; } String suffix = fileName.substring(fileName.lastIndexOf(".")); if (suffix.equals(".jar")) return true; else return false; } // public IWizardPage getNextPage(IWizardPage page) // { // return super.getNextPage(page); // } protected void handleFinishException(Shell shell, InvocationTargetException e) { String title = NewWizardMessages.JavaProjectWizard_op_error_title; String message = NewWizardMessages.JavaProjectWizard_op_error_create_message; ExceptionHandler.handle(e, getShell(), title, message); } public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) { this.fConfigElement = cfig; } public boolean performCancel() { this.fSecondPage.performCancel(); return super.performCancel(); } public IJavaElement getCreatedElement() { return this.fSecondPage.getJavaProject(); } }