package qq.builder;
|
|
import java.io.File;
|
import java.io.FilenameFilter;
|
import java.util.ArrayList;
|
import java.util.Hashtable;
|
import java.util.Iterator;
|
import java.util.List;
|
|
import org.eclipse.core.resources.IProject;
|
//import org.eclipse.core.resources.IProjectDescription;
|
//import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.IAdaptable;
|
import org.eclipse.core.runtime.IPath;
|
import org.eclipse.core.runtime.Path;
|
import org.eclipse.jdt.core.IClasspathContainer;
|
import org.eclipse.jdt.core.IClasspathEntry;
|
import org.eclipse.jdt.core.IJavaProject;
|
import org.eclipse.jdt.core.JavaCore;
|
import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport;
|
import org.eclipse.jface.action.IAction;
|
import org.eclipse.jface.dialogs.MessageDialog;
|
import org.eclipse.jface.viewers.ISelection;
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
import org.eclipse.ui.IObjectActionDelegate;
|
import org.eclipse.ui.IWorkbenchPart;
|
|
@SuppressWarnings("restriction")
|
public class ToggleNatureAction implements IObjectActionDelegate {
|
|
private ISelection selection;
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
|
*/
|
@SuppressWarnings("rawtypes")
|
public void run(IAction action) {
|
if (selection instanceof IStructuredSelection) {
|
for (Iterator it = ((IStructuredSelection) selection).iterator(); it
|
.hasNext();) {
|
Object element = it.next();
|
IProject project = null;
|
if (element instanceof IProject) {
|
project = (IProject) element;
|
} else if (element instanceof IAdaptable) {
|
project = (IProject) ((IAdaptable) element)
|
.getAdapter(IProject.class);
|
}
|
if (project != null) {
|
toggleNature(project);
|
}
|
}
|
}
|
}
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see
|
* org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
|
* .IAction, org.eclipse.jface.viewers.ISelection)
|
*/
|
public void selectionChanged(IAction action, ISelection selection) {
|
this.selection = selection;
|
}
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see
|
* org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
|
* action.IAction, org.eclipse.ui.IWorkbenchPart)
|
*/
|
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
|
}
|
|
/**
|
* Toggles sample nature on a project
|
*
|
* @param project
|
* to have sample nature added or removed
|
*/
|
private void toggleNature(IProject project) {
|
IPath path = JavaCore.getClasspathVariable("VCI_HOME");
|
|
Hashtable<String, List<String>> paths = getVCIUserLibraray(path.toFile().getAbsolutePath());
|
IJavaProject jProject = JavaCore.create(project);
|
//IClasspathContainer classPContainer;
|
try {
|
IClasspathContainer classTContainer = JavaCore.getClasspathContainer(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_THIRDLIB"), jProject);
|
|
IClasspathContainer classPContainer = JavaCore.getClasspathContainer(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_PLATFORM"), jProject);
|
|
IClasspathContainer classCoContainer = JavaCore.getClasspathContainer(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_COMMONS"), jProject);
|
|
IClasspathContainer classCContainer = JavaCore.getClasspathContainer(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_CLIENT"), jProject);
|
|
IClasspathContainer classSContainer = JavaCore.getClasspathContainer(new Path("org.eclipse.jdt.USER_LIBRARY/VCI_SERVERS"), jProject);
|
|
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"));
|
} catch (Exception e1) {
|
MessageDialog.openError(null, "Alert!",
|
"Preferences VCI Datasource (VCI_HOME:) Config Wrong!");
|
}
|
|
// try {
|
// IProjectDescription description = project.getDescription();
|
// String[] natures = description.getNatureIds();
|
//
|
// for (int i = 0; i < natures.length; ++i) {
|
// if (SampleNature.NATURE_ID.equals(natures[i])) {
|
// // Remove the nature
|
// String[] newNatures = new String[natures.length - 1];
|
// System.arraycopy(natures, 0, newNatures, 0, i);
|
// System.arraycopy(natures, i + 1, newNatures, i,
|
// natures.length - i - 1);
|
// description.setNatureIds(newNatures);
|
// project.setDescription(description, null);
|
// return;
|
// }
|
// }
|
//
|
// // Add the nature
|
// String[] newNatures = new String[natures.length + 1];
|
// System.arraycopy(natures, 0, newNatures, 0, natures.length);
|
// newNatures[natures.length] = SampleNature.NATURE_ID;
|
// description.setNatureIds(newNatures);
|
// project.setDescription(description, null);
|
// } catch (CoreException e) {
|
// }
|
}
|
|
private IClasspathEntry[] getClasspathEntry(
|
Hashtable<String, List<String>> paths, String userLibrary) {
|
IPath path = JavaCore.getClasspathVariable("VCI_HOME");
|
List<String> 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 Hashtable<String, List<String>> getVCIUserLibraray(String VCI_HOME) {
|
Hashtable<String, List<String>> pathTables = new Hashtable<String, List<String>>();
|
List<File> modules = new ArrayList<File>();
|
File file = new File(VCI_HOME + "\\libs");
|
File[] jarFiles = file.listFiles();
|
if (jarFiles == null) {
|
return pathTables;
|
}
|
|
try {
|
List<String> platformpaths = new ArrayList<String>();
|
List<String> libpaths = new ArrayList<String>();
|
List<String> publicpaths = new ArrayList<String>();
|
List<String> privatepaths = new ArrayList<String>();
|
List<String> clientpaths = new ArrayList<String>();
|
|
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<File> clients = new ArrayList<File>();
|
List<File> commons = new ArrayList<File>();
|
List<File> servers = new ArrayList<File>();
|
|
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;
|
}
|
|
// 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;
|
}
|
|
}
|