Java tutorial
// ======================================================================== // Copyright (c) 2009 Intalio, Inc. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // You may elect to redistribute this code under either of these licenses. // Contributors: // Hugues Malphettes - initial API and implementation // ======================================================================== package org.eclipse.jetty.osgi.pde.launch.ui.tabs; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jetty.osgi.pde.launch.JettyLaunchConfigurationConstants; import org.eclipse.jetty.osgi.pde.launch.JettyLauncherMessages; import org.eclipse.jetty.osgi.pde.launch.JettyOSGiPDEPlugin; import org.eclipse.jetty.osgi.pde.launch.internal.NaiveJavaProjectToWebappIdentifier; import org.eclipse.jface.viewers.CheckStateChangedEvent; import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.ICheckStateListener; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableLayout; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.pde.internal.ui.SWTFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; /** * List all the java projects in the workspace that are not OSGi projects. * Let the user select the ones that should be deployed as a webapp. * TODO: Let the user configure the webapp(s) embedded in each one of them. * <p> * This is a work in progress. * </p> * <p> * Shows a master slave UI * The master is a list with checkboxes of each of the projects in the workspace. * The slave is where the deployment of the webapp is configured. */ public class JavaProjectsAsWebappsBlock extends AbstractLaunchConfigurationTab { private CheckboxTableViewer _javaProjectsViewer; private Map<IProject, NaiveJavaProjectToWebappIdentifier> _webappsInJavaProjects; private static final int COL_JAVA_PROJ_NAME = 0; private static final int COL_CONTEXT_PATH = 1; private static final int COL_WEBAPP_BASE_FOLDER = 2; /** * Calls create control of the super (which is final) * then go an tweak the created control so that its purpose is for jetty home * instead of a working directory selection. */ public void createControl(Composite parent) { parent = SWTFactory.createComposite(parent, 1, 2, GridData.FILL_BOTH); super.setControl(parent); GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); parent.setLayoutData(gridData); _webappsInJavaProjects = new HashMap<IProject, NaiveJavaProjectToWebappIdentifier>(); Group grp = SWTFactory.createGroup(parent, JettyLauncherMessages.JavaProjectsAsWebappsBlock_Select_java_projects_to_run_as_webapps, 1, 1, GridData.FILL_BOTH); gridData = new GridData(SWT.FILL, SWT.FILL, true, true); grp.setLayoutData(gridData); _javaProjectsViewer = CheckboxTableViewer.newCheckList(grp, SWT.BORDER); Table table = _javaProjectsViewer.getTable(); TableLayout layout = new TableLayout(); table.setLinesVisible(true); table.setHeaderVisible(true); TableColumn tc0 = new TableColumn(table, SWT.NONE, 0); TableColumn tc1 = new TableColumn(table, SWT.NONE, 1); TableColumn tc2 = new TableColumn(table, SWT.NONE, 2); // TableColumn tc3 = new TableColumn(table,SWT.NONE, 3); tc0.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_JavaProject); tc1.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_ContextPath); tc2.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_WebappBaseFolder); // tc3.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_JettyContextFilePathes); layout.addColumnData(new ColumnWeightData(28)); layout.addColumnData(new ColumnWeightData(12)); layout.addColumnData(new ColumnWeightData(60)); table.setLayout(layout); _javaProjectsViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); _javaProjectsViewer.setContentProvider(new JavaProjectsOrWebBundlesContentProvider(true)); _javaProjectsViewer.setLabelProvider(new JavaProjectsOrWebBundleLabelProvider()); _javaProjectsViewer.addCheckStateListener(new ICheckStateListener() { public void checkStateChanged(CheckStateChangedEvent event) { setDirty(true); updateLaunchConfigurationDialog(); } }); _javaProjectsViewer.setInput(ResourcesPlugin.getWorkspace().getRoot()); //now for the read-only list of web-bundles. grp = SWTFactory.createGroup(parent, JettyLauncherMessages.JavaProjectsAsWebappsBlock_List_webbundles, 1, 1, GridData.FILL_BOTH); gridData = new GridData(SWT.FILL, SWT.FILL, true, true); grp.setLayoutData(gridData); TableViewer webBundles = new TableViewer(grp, SWT.BORDER); table = webBundles.getTable(); layout = new TableLayout(); table.setLinesVisible(true); table.setHeaderVisible(true); tc0 = new TableColumn(table, SWT.NONE, 0); tc1 = new TableColumn(table, SWT.NONE, 1); tc2 = new TableColumn(table, SWT.NONE, 2); // TableColumn tc3 = new TableColumn(table,SWT.NONE, 3); tc0.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_WebBundleProject); tc1.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_ContextPath); tc2.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_WebappBaseFolder); // tc3.setText(JettyLauncherMessages.JavaProjectsAsWebappsBlock_Column_JettyContextFilePathes); layout.addColumnData(new ColumnWeightData(28)); layout.addColumnData(new ColumnWeightData(12)); layout.addColumnData(new ColumnWeightData(60)); table.setLayout(layout); webBundles.setContentProvider(new JavaProjectsOrWebBundlesContentProvider(false)); webBundles.setLabelProvider(new JavaProjectsOrWebBundleLabelProvider()); webBundles.setInput(ResourcesPlugin.getWorkspace().getRoot()); webBundles.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); } public String getName() { return JettyLauncherMessages.JavaProjectsAsWebappsBlock_Section_title; } public void initializeFrom(ILaunchConfiguration configuration) { try { String selectedJavaProjects = configuration .getAttribute(JettyLaunchConfigurationConstants.JAVA_PROJECTS_SELECTED, ""); //$NON-NLS-1$ if (selectedJavaProjects.length() == 0) { return; } for (String javaProj : selectedJavaProjects.split(",")) //$NON-NLS-1$ { IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(javaProj); if (JettyOSGiPDEPlugin.isJavaProjectButNotAPlugin(proj)) { _javaProjectsViewer.setChecked(proj, true); } } } catch (CoreException e) { e.printStackTrace(); } } public void performApply(ILaunchConfigurationWorkingCopy configuration) { StringBuilder sb = new StringBuilder(); for (Object p : _javaProjectsViewer.getCheckedElements()) { if (sb.length() != 0) { sb.append(","); //$NON-NLS-1$ } sb.append(((IProject) p).getName()); } configuration.setAttribute(JettyLaunchConfigurationConstants.JAVA_PROJECTS_SELECTED, sb.toString()); } public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { //nothing: we select no java projects by default for now. } class JavaProjectsOrWebBundlesContentProvider implements IStructuredContentProvider { private final boolean _lookJavaProj; /** * @param lookJavaProj true to list the java projects that are not osgi projects only. * false to only look into the osgi projects that do have a web-bundle manifest header */ JavaProjectsOrWebBundlesContentProvider(boolean lookJavaProj) { _lookJavaProj = lookJavaProj; } public void dispose() { /*nothing to do.*/ } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { /*the input is always the same anyways.*/ } /** * @return The java projects that could contain */ public Object[] getElements(Object inputElement) { //make sure we are talking about the right thing: if (!(inputElement instanceof IWorkspaceRoot)) { throw new IllegalArgumentException( "Expecting an IWorkspaceRoot. This is not expected " + inputElement); //$NON-NLS-1$ } ArrayList<IProject> children = new ArrayList<IProject>(); IWorkspaceRoot root = (IWorkspaceRoot) inputElement; for (IProject proj : root.getProjects()) { if (_lookJavaProj) { if (JettyOSGiPDEPlugin.isJavaProjectButNotAPlugin(proj)) { try { IJavaProject javaProj = JavaCore.create(proj); NaiveJavaProjectToWebappIdentifier app = new NaiveJavaProjectToWebappIdentifier( javaProj, false); _webappsInJavaProjects.put(proj, app); if (app.hasWebapp()) { children.add(proj); } } catch (Throwable t) { t.printStackTrace(); } } } else { try { if (JettyOSGiPDEPlugin.isProcessedAsOSGiProject(proj)) //proj.hasNature(JettyOSGiPDEPlugin.NATURE_PLUGIN)) { IJavaProject javaProj = JavaCore.create(proj); NaiveJavaProjectToWebappIdentifier app = new NaiveJavaProjectToWebappIdentifier( javaProj, true); if (app.hasOSGiWebBundleData()) { _webappsInJavaProjects.put(proj, app); children.add(proj); } } } catch (Throwable t) { t.printStackTrace(); } } } return children.toArray(); } } class JavaProjectsOrWebBundleLabelProvider implements ITableLabelProvider { public void removeListener(ILabelProviderListener listener) { /*nothing to do.*/ } public boolean isLabelProperty(Object element, String property) { return false; } public void dispose() { /*nothing to do.*/ } public void addListener(ILabelProviderListener listener) { /*nothing to do.*/ } public String getColumnText(Object element, int columnIndex) { switch (columnIndex) { case COL_CONTEXT_PATH: NaiveJavaProjectToWebappIdentifier p = _webappsInJavaProjects.get((IProject) element); if (p == null || p.getContextPath() == null) { return "none"; } else { return p.getContextPath(); } case COL_WEBAPP_BASE_FOLDER: IProject proj = (IProject) element; p = _webappsInJavaProjects.get(proj); if (p == null || p.getWebappBaseFolderPath() == null) { List<String> contexts = p.getJettyContextFiles(); if (contexts != null) { StringBuilder sb = new StringBuilder(); for (String c : contexts) { if (sb.length() != 0) { sb.append(", "); } sb.append(c); } return "Jetty-Context-Files: " + sb.toString(); } return "none"; } else { if (p.getWebappBaseFolderPathRelativeToFolder() != null) { String relPath = p.getWebappBaseFolderPathRelativeToFolder(); if (relPath.startsWith("/")) { relPath = relPath.substring(1); } return "${workspace_loc:" + proj.getName() + "}/" + relPath; } return p.getWebappBaseFolderPath(); } case COL_JAVA_PROJ_NAME: default: return ((IProject) element).getName(); } } public Image getColumnImage(Object element, int columnIndex) { //there is a single column and the object is always an IProject return null;//IDEWorkbenchPlugin.getIDEImageDescriptor(relativePath) IDE.SharedImages.IMG_OBJ_PROJECT; } } }