插件集成常用语句

//在项目根目录下创建devlib文件夹,将htmlunit的支持包放入devlib文件夹下,并添加到classpath中
		final IFolder tld = project.getFolder("devlib");
		if(!tld.exists()){
			tld.create(IResource.FORCE, true, null);	
		}

//添加到classpath
	private void addSourceFolder(IJavaProject project, IPath srcPath) throws JavaModelException {
		IClasspathEntry[] entries = project.getRawClasspath();
		IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
		System.arraycopy(entries, 0, newEntries, 0, entries.length);
		newEntries[entries.length] = JavaCore.newSourceEntry(srcPath);

		project.setRawClasspath(newEntries, null);

	}

//添加listener,servelet,mapping
private void createExoMetaData(WebApp webApp) {
		// Create the servlet instance and set up the parameters from data
		// model
		
		Listener listener = CommonFactory.eINSTANCE.createListener();
		listener.setListenerClassName("org.exoplatform.services.portletcontainer.impl.servlet.PortletApplicationListener");

		Servlet servlet = WebapplicationFactory.eINSTANCE.createServlet();

		servlet.setServletName("PortletWrapper");
		ServletType servletType = WebapplicationFactory.eINSTANCE.createServletType();
		servletType.setClassName("org.exoplatform.services.portletcontainer.impl.servlet.ServletWrapper");
		servlet.setWebType(servletType);

		// Add the servlet to the web application model
		webApp.getListeners().add(listener);
		webApp.getServlets().add(servlet);

		ServletMapping mapping = WebapplicationFactory.eINSTANCE.createServletMapping();
		mapping.setServlet(servlet);
		mapping.setName(servlet.getServletName());
		mapping.setUrlPattern("/PortletWrapper");
		webApp.getServletMappings().add(mapping);
	}

//参数
if (webApp.getJ2EEVersionID() >= J2EEVersionConstants.J2EE_1_4_ID) {
			ParamValue param = CommonFactory.eINSTANCE.createParamValue();
			param.setName("company_id");
			param.setValue("liferay.com");
			webApp.getContextParams().add(param);
		} else {
			ContextParam cp = WebapplicationFactory.eINSTANCE.createContextParam();
			cp.setParamName("company_id");
			cp.setParamValue("liferay.com");
			cp.setWebApp(webApp);
		}

//addNature,SubProgressMonitor,addServletLibToWebInf
/*
 * Copyright 2006 Cypal Solutions ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package in.cypal.studio.gwt.core.facet;

import in.cypal.studio.gwt.core.Activator;
import in.cypal.studio.gwt.core.common.Constants;
import in.cypal.studio.gwt.core.common.Util;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.project.facet.core.IDelegate;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;


/**
 * @author Prakash G.R.
 *
 */
public class InstallDelegate implements IDelegate{

	public void execute(IProject project, IProjectFacetVersion facetVersion, Object config, IProgressMonitor monitor) throws CoreException {
		
		monitor = Util.getNonNullMonitor(monitor);
		
		try {
		
			monitor.beginTask("", 3); 
			
			addNature(project, new SubProgressMonitor(monitor, 1)); 
			addUserLibToClassPath(project, new SubProgressMonitor(monitor, 1));
			addServletLibToWebInf(project, new SubProgressMonitor(monitor, 1));
			
		}catch(CoreException e) {
			monitor.setCanceled(true);
			Activator.logException(e);
		}finally {
			monitor.done();
		}
	}

	
	private void addServletLibToWebInf(IProject project, IProgressMonitor monitor){
		
		monitor = Util.getNonNullMonitor(monitor);
		try {
			
	    	IPath webContent = ComponentCore.createComponent(project).getRootFolder().getProjectRelativePath();
			IFile theLink = project.getFile(webContent.append("WEB-INF").append("lib").append("gwt-servlet.jar")); 
			IPath actualLocation = new Path(Constants.GWT_HOME_PATH+"/gwt-servlet.jar");  
			theLink.createLink(actualLocation, IResource.REPLACE, null);  
		} catch (CoreException e) {
			// the jar is already in the classpath.  
			Activator.logException(e);
		}finally {
			monitor.done();
		}
		
	}


	private void addUserLibToClassPath(IProject project, IProgressMonitor monitor){
		
		monitor = Util.getNonNullMonitor(monitor);

		try {
			monitor.beginTask("", 1); 
			
			IJavaProject javaProject = JavaCore.create(project);
			IClasspathEntry[] oldClasspath = javaProject.getRawClasspath();
			IClasspathEntry[] newClasspath = new IClasspathEntry[oldClasspath.length+1];
			System.arraycopy(oldClasspath, 0, newClasspath, 0, oldClasspath.length);
			IClasspathEntry gwtUserJarEntry = JavaCore.newVariableEntry(Util.getGwtUserLibPath(), null, null);
			IClasspathAttribute attr = JavaCore.newClasspathAttribute("org.eclipse.jst.component.dependency", "/WEB-INF/lib");
			gwtUserJarEntry = JavaCore.newVariableEntry(gwtUserJarEntry.getPath(), null, null, new IAccessRule[0], new IClasspathAttribute[] {attr}, false);
			newClasspath[oldClasspath.length] = gwtUserJarEntry;
			javaProject.setRawClasspath(newClasspath, monitor);

		} catch (JavaModelException e) {
			// the jar is already in the classpath.  
			Activator.logException(e);
		}finally {
			monitor.done();
		}
	}


	private void addNature(IProject project, IProgressMonitor monitor) throws CoreException {
		
		monitor = Util.getNonNullMonitor(monitor);
		
		try {
			
			monitor.beginTask("", 1); 
			
			IProjectDescription description = project.getDescription();
			String[] prevNatures= description.getNatureIds();
			String[] newNatures= new String[prevNatures.length + 1];
			System.arraycopy(prevNatures, 0, newNatures, 1, prevNatures.length);
			newNatures[0]= Constants.NATURE_ID;
			description.setNatureIds(newNatures);
			
			project.setDescription(description, IResource.FORCE, null);

		}finally {
			monitor.done();
		}
	}

}

、从TextEditor继承,调用setSourceViewerConfiguration,并传进去一个从SourceViewerConfiguration 继承的配置类,就可以实现各种代码editor。
2、swt尽量使用GridLayout布局(不是java.awt中的GridLayout,而是swt中的)和GridData域。文章:http://coolbear.yculblog.com/post.89429.html

3、得到文件的编辑器的方法:
  
 public static IEditorPart findEditor(IFile file){
        IEditorReference[] editors = getActivePage().getEditorReferences();;
        for (int i = 0; i < editors.length; i++) {
            IEditorPart part = (IEditorPart)editors[i].getPart(false);
            if (part != null ){
                IEditorInput input = part.getEditorInput();
                if(input instanceof FileEditorInput && ((FileEditorInput)input).getFile().equals(file))
                    return part;
            }               
        }
        return null;
    }

4、得到工作区中所有工程的方法:
      
 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
                .getProjects();

这在开发自己的工程向导的时候很有用处。
5、工程特有文件判断方法
project.getFile("cownew.prj").exists();
project.getDescription().hasNature();
给工程增加Nature的方法:
 IProjectDescription desc = project.getDescription();
String[] oldNatureIds = desc.getNatureIds();
                String[] newNatureIds  = new String[oldNatureIds.length +1];
                System.arraycopy(oldNatureIds, 0, newNatureIds, 0, oldNatureIds.length);
                newNatureIds[oldNatureIds.length] = "CowNewNature";
                desc.setNatureIds(newNatureIds);
                project.setDescription(desc, monitor);

6、创建文件夹的方法:
IFolder folder = project.getFolder("myfold");
if (folder!=null && !folder.exists())
  folder.create(false, true, null);

7、弹出包选择对话框的方法:
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
                    getShell(), new LabelProvider());
            dialog.setIgnoreCase(false);
dialog.setElements(getAllPackages().toArray());
            String path = currentPackage();
 dialog.setInitialSelections(new Object[] { path });
dialog.open();
fPKName.setText((String) dialog.getFirstResult());

public List getAllPackages() {
        List list = new ArrayList();
        IResource res = getFirstSelection();
        IProject project = res.getProject();
        File file = project.getFolder("src").getLocation().toFile();
        File[] fs = file.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].isDirectory())
                iterator("", fs[i], list);
        }
        Collections.sort(list);
        return list;
    }

8 objectClass="org.eclipse.core.resources.IFile"代表菜单应用到文件
9 透视图的的实现很简单,就是在构造函数里边打开一些视图,使一些action(这样菜单和按钮也就都可用)可以用,比如:
String editorArea = layout.getEditorArea();
        IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
                0.30f, editorArea);
        left.addView(PACKAGE_VIEW_ID);
 layout.setEditorAreaVisible(true);
 layout.addShowViewShortcut(IDESystem.BUSINESSVIEW_ID);

你可能感兴趣的:(apache,eclipse,Web,servlet,gwt)