Author: snjeza
Date: 2011-09-13 08:26:47 -0400 (Tue, 13 Sep 2011)
New Revision: 34677
Added:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/ImportDefaultMavenProjectExample.java
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples.cheatsheet/src/org/jboss/tools/project/examples/cheatsheet/actions/ImportProjectExample.java
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/ProjectExamplesActivator.java
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java
trunk/examples/tests/org.jboss.tools.project.examples.test/src/org/jboss/tools/project/examples/test/ProjectTest.java
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java
Log:
JBIDE-9702 Refactor project examples plugin so that it can be used inside JBoss Central
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/ProjectExamplesActivator.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/ProjectExamplesActivator.java 2011-09-13
12:20:40 UTC (rev 34676)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/ProjectExamplesActivator.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -10,12 +10,23 @@
************************************************************************************/
package org.jboss.tools.project.examples;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
@@ -25,12 +36,22 @@
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.browser.IWebBrowser;
+import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
+import org.eclipse.ui.internal.cheatsheets.state.DefaultStateManager;
+import org.eclipse.ui.internal.cheatsheets.views.CheatSheetView;
+import org.eclipse.ui.internal.cheatsheets.views.ViewUtilities;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.wst.validation.internal.operations.ValidationBuilder;
import org.jboss.tools.project.examples.fixes.ProjectExamplesFix;
@@ -39,6 +60,8 @@
import org.jboss.tools.project.examples.model.IImportProjectExample;
import org.jboss.tools.project.examples.model.Project;
import org.jboss.tools.project.examples.model.ProjectFix;
+import org.jboss.tools.project.examples.model.ProjectUtil;
+import org.jboss.tools.project.examples.wizard.ImportDefaultMavenProjectExample;
import org.osgi.framework.BundleContext;
/**
@@ -86,6 +109,7 @@
};
private Map<String, IImportProjectExample> importProjectExamplesMap;
+ private ImportDefaultMavenProjectExample defaultImportProjectExample;
/**
* The constructor
@@ -209,11 +233,15 @@
public IImportProjectExample getImportProjectExample(String importType) {
initImportProjectExamples();
+ if (importType == null) {
+ return defaultImportProjectExample;
+ }
return importProjectExamplesMap.get(importType);
}
private void initImportProjectExamples() {
if (importProjectExamplesMap == null) {
+ defaultImportProjectExample = new ImportDefaultMavenProjectExample();
importProjectExamplesMap = new HashMap<String,IImportProjectExample>();
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint extensionPoint = registry
@@ -264,4 +292,153 @@
return null;
}
}
+
+ public static boolean downloadProject(Project project, IProgressMonitor monitor) {
+ if (project.isURLRequired()) {
+ String urlString = project.getUrl();
+ String name = project.getName();
+ URL url = null;
+ try {
+ url = new URL(urlString);
+ } catch (MalformedURLException e) {
+ ProjectExamplesActivator.log(e);
+ return false;
+ }
+ File file = ProjectUtil.getProjectExamplesFile(url, name,
+ ".zip", monitor); //$NON-NLS-1$
+ if (file == null) {
+ return false;
+ }
+ project.setFile(file);
+ }
+ return true;
+ }
+
+ public static void openWelcome(List<Project> projects) {
+ if (projects == null) {
+ return;
+ }
+ for(final Project project:projects) {
+ if (project.isWelcome()) {
+ String urlString = project.getWelcomeURL();
+ URL url = null;
+ if (urlString.startsWith("/")) { //$NON-NLS-1$
+ IPath path = new Path(urlString);
+ IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
+ if (resource instanceof IFile && resource.isAccessible()) {
+ try {
+ url = resource.getRawLocationURI().toURL();
+ } catch (MalformedURLException e) {
+ ProjectExamplesActivator.log(e);
+ }
+ } else {
+ ProjectExamplesActivator.log(NLS.bind(Messages.NewProjectExamplesWizard_File_does_not_exist,urlString));
+ }
+ } else {
+ try {
+ url = new URL(urlString);
+ } catch (MalformedURLException e) {
+ ProjectExamplesActivator.log(e);
+ }
+ }
+ if (url!=null) {
+ final URL finalURL = url;
+ Display.getDefault().asyncExec(new Runnable() {
+
+ public void run() {
+ if (ProjectUtil.CHEATSHEETS.equals(project.getType())) {
+ CheatSheetView view = ViewUtilities.showCheatSheetView();
+ if (view == null) {
+ return;
+ }
+ IPath filePath = new Path(finalURL.getPath());
+ String id = filePath.lastSegment();
+ if (id == null) {
+ id = ""; //$NON-NLS-1$
+ }
+ view.getCheatSheetViewer().setInput(id, id, finalURL, new DefaultStateManager(),
false);
+ } else {
+ try {
+ IWorkbenchBrowserSupport browserSupport =
ProjectExamplesActivator.getDefault().getWorkbench().getBrowserSupport();
+ IWebBrowser browser =
browserSupport.createBrowser(IWorkbenchBrowserSupport.LOCATION_BAR |
IWorkbenchBrowserSupport.NAVIGATION_BAR, null, null, null);
+ browser.openURL(finalURL);
+ } catch (PartInitException e) {
+ ProjectExamplesActivator.log(e);
+ }
+ }
+ }
+
+ });
+
+ }
+ }
+ }
+ }
+
+ public static boolean extractZipFile(File file, File destination,
+ IProgressMonitor monitor) {
+ ZipFile zipFile = null;
+ destination.mkdirs();
+ try {
+ zipFile = new ZipFile(file);
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
+ while (entries.hasMoreElements()) {
+ if (monitor.isCanceled()) {
+ return false;
+ }
+ ZipEntry entry = (ZipEntry) entries.nextElement();
+ if (entry.isDirectory()) {
+ monitor.setTaskName("Extracting " + entry.getName());
+ File dir = new File(destination, entry.getName());
+ dir.mkdirs();
+ continue;
+ }
+ monitor.setTaskName("Extracting " + entry.getName());
+ File entryFile = new File(destination, entry.getName());
+ entryFile.getParentFile().mkdirs();
+ InputStream in = null;
+ OutputStream out = null;
+ try {
+ in = zipFile.getInputStream(entry);
+ out = new FileOutputStream(entryFile);
+ copy(in, out);
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ if (out != null) {
+ try {
+ out.close();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ }
+ }
+ } catch (IOException e) {
+ ProjectExamplesActivator.log(e);
+ return false;
+ } finally {
+ if (zipFile != null) {
+ try {
+ zipFile.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+ return true;
+ }
+
+ public static void copy(InputStream in, OutputStream out) throws IOException {
+ byte[] buffer = new byte[16 * 1024];
+ int len;
+ while ((len = in.read(buffer)) >= 0) {
+ out.write(buffer, 0, len);
+ }
+ }
}
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java 2011-09-13
12:20:40 UTC (rev 34676)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -10,6 +10,7 @@
************************************************************************************/
package org.jboss.tools.project.examples.model;
+import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@@ -39,6 +40,7 @@
private String importType;
private String importTypeDescription;
private ArchetypeModel archetypeModel = new ArchetypeModel();
+ private File file;
public Project() {
name=""; //$NON-NLS-1$
@@ -210,4 +212,12 @@
public boolean isURLRequired() {
return !ProjectExamplesActivator.MAVEN_ARCHETYPE.equals(importType);
}
+
+ public File getFile() {
+ return file;
+ }
+
+ public void setFile(File file) {
+ this.file = file;
+ }
}
Added:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/ImportDefaultMavenProjectExample.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/ImportDefaultMavenProjectExample.java
(rev 0)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/ImportDefaultMavenProjectExample.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -0,0 +1,211 @@
+package org.jboss.tools.project.examples.wizard;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+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.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.internal.core.JavaProject;
+import org.eclipse.jdt.internal.core.OpenableElementInfo;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.dialogs.IOverwriteQuery;
+import org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider;
+import org.eclipse.ui.wizards.datatransfer.ImportOperation;
+import org.jboss.tools.project.examples.Messages;
+import org.jboss.tools.project.examples.ProjectExamplesActivator;
+import org.jboss.tools.project.examples.model.AbstractImportProjectExample;
+import org.jboss.tools.project.examples.model.Project;
+
+public class ImportDefaultMavenProjectExample extends
+ AbstractImportProjectExample {
+
+ private static final IOverwriteQuery OVERWRITE_ALL_QUERY = new IOverwriteQuery() {
+ public String queryOverwrite(String pathString) {
+ return IOverwriteQuery.ALL;
+ }
+ };
+
+ @Override
+ public List<Project> importProject(Project projectDescription, File file,
+ IProgressMonitor monitor) throws Exception {
+ List<Project> projects = new ArrayList<Project>();
+ if (projectDescription.getIncludedProjects() == null) {
+ importSingleProject(projectDescription, file, monitor);
+ projects.add(projectDescription);
+ return projects;
+ } else {
+ List<String> projectNames = projectDescription.getIncludedProjects();
+ for (final String projectName : projectNames) {
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IProject project = workspace.getRoot().getProject(projectName);
+ final boolean[] ret = new boolean[1];
+ if (project.exists()) {
+ Display.getDefault().syncExec(new Runnable() {
+
+ public void run() {
+ ret[0] = MessageDialog.openQuestion(getActiveShell(),
+ Messages.NewProjectExamplesWizard_Question,
NLS.bind(Messages.NewProjectExamplesWizard_OverwriteProject,
+ projectName));
+ }
+
+ });
+ if (!ret[0]) {
+ return projects;
+ }
+ project.delete(true, true, monitor);
+ }
+ project.create(monitor);
+ project.open(monitor);
+ ZipFile sourceFile = new ZipFile(file);
+ ZipLeveledStructureProvider structureProvider = new ZipLeveledStructureProvider(
+ sourceFile);
+
+ Enumeration<? extends ZipEntry> entries = sourceFile.entries();
+ ZipEntry entry = null;
+ List<ZipEntry> filesToImport = new ArrayList<ZipEntry>();
+ List<ZipEntry> directories = new ArrayList<ZipEntry>();
+ String prefix = projectName + "/"; //$NON-NLS-1$
+ while (entries.hasMoreElements()) {
+ entry = entries.nextElement();
+ if (entry.getName().startsWith(prefix)) {
+ if (!entry.isDirectory()) {
+ filesToImport.add(entry);
+ } else {
+ directories.add(entry);
+ }
+ }
+ }
+
+ structureProvider.setStrip(1);
+ ImportOperation operation = new ImportOperation(project.getFullPath(),
structureProvider.getRoot(),
+ structureProvider, OVERWRITE_ALL_QUERY, filesToImport);
+ operation.setContext(getActiveShell());
+ operation.run(monitor);
+ for (ZipEntry directory:directories) {
+ IPath resourcePath = new Path(directory.getName());
+ try {
+ workspace.getRoot().getFolder(resourcePath).create(false, true, null);
+ } catch (Exception e) {
+ ProjectExamplesActivator.log(e);
+ }
+ }
+ reconfigure(project, monitor);
+ }
+ }
+ return projects;
+ }
+
+ private void importSingleProject(Project projectDescription, File file,
+ IProgressMonitor monitor) throws CoreException, ZipException,
+ IOException, InvocationTargetException, InterruptedException {
+ final String projectName = projectDescription.getName();
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IProject project = workspace.getRoot().getProject(projectName);
+ final boolean[] ret = new boolean[1];
+ if (project.exists()) {
+ Display.getDefault().syncExec(new Runnable() {
+
+ public void run() {
+ ret[0] = MessageDialog.openQuestion(getActiveShell(),
+ Messages.NewProjectExamplesWizard_Question,
NLS.bind(Messages.NewProjectExamplesWizard_OverwriteProject,
+ projectName));
+ }
+
+ });
+ if (!ret[0]) {
+ return;
+ }
+ project.delete(true, true, monitor);
+ }
+ project.create(monitor);
+ project.open(monitor);
+ ZipFile sourceFile = new ZipFile(file);
+ ZipLeveledStructureProvider structureProvider = new ZipLeveledStructureProvider(
+ sourceFile);
+
+ Enumeration<? extends ZipEntry> entries = sourceFile.entries();
+ ZipEntry entry = null;
+ List<ZipEntry> filesToImport = new ArrayList<ZipEntry>();
+ List<ZipEntry> directories = new ArrayList<ZipEntry>();
+ String prefix = projectName + "/"; //$NON-NLS-1$
+ while (entries.hasMoreElements()) {
+ entry = entries.nextElement();
+ if (entry.getName().startsWith(prefix)) {
+ if (!entry.isDirectory()) {
+ filesToImport.add(entry);
+ } else {
+ directories.add(entry);
+ }
+ }
+ }
+
+ structureProvider.setStrip(1);
+ ImportOperation operation = new ImportOperation(project.getFullPath(),
structureProvider.getRoot(),
+ structureProvider, OVERWRITE_ALL_QUERY, filesToImport);
+ operation.setContext(getActiveShell());
+ operation.run(monitor);
+ for (ZipEntry directory:directories) {
+ IPath resourcePath = new Path(directory.getName());
+ try {
+ workspace.getRoot().getFolder(resourcePath).create(false, true, null);
+ } catch (Exception e) {
+ ProjectExamplesActivator.log(e);
+ }
+ }
+ reconfigure(project, monitor);
+
+ }
+
+ private static Shell getActiveShell() {
+ Display display = Display.getDefault();
+ final Shell[] ret = new Shell[1];
+ display.syncExec(new Runnable() {
+
+ public void run() {
+ ret[0] = Display.getCurrent().getActiveShell();
+ }
+
+ });
+ return ret[0];
+ }
+
+ private static void reconfigure(IProject project, IProgressMonitor monitor) throws
CoreException {
+ if (project == null || !project.exists() || !project.isOpen() ||
!project.hasNature(JavaCore.NATURE_ID)) {
+ return;
+ }
+ project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
+ IJavaProject javaProject = JavaCore.create(project);
+ if (javaProject != null && javaProject.exists() && javaProject.isOpen()
&& javaProject instanceof JavaProject) {
+ Object object = ((JavaProject) javaProject).getElementInfo();
+ if (object instanceof OpenableElementInfo) {
+ // copied from JavaProject.buildStructure(...)
+ OpenableElementInfo info = (OpenableElementInfo) object;
+ IClasspathEntry[] resolvedClasspath = ((JavaProject)
javaProject).getResolvedClasspath();
+ IPackageFragmentRoot[] children = ((JavaProject)
javaProject).computePackageFragmentRoots(resolvedClasspath,false, null /* no reverse map
*/);
+ info.setChildren(children);
+ ((JavaProject) javaProject).getPerProjectInfo().rememberExternalLibTimestamps();
+ }
+ }
+ }
+
+}
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java 2011-09-13
12:20:40 UTC (rev 34676)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -15,25 +15,18 @@
*
*/
import java.io.File;
-import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
-import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipException;
-import java.util.zip.ZipFile;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
-import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
@@ -44,16 +37,9 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
-import org.eclipse.jdt.core.IClasspathEntry;
-import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.IPackageFragmentRoot;
-import org.eclipse.jdt.core.JavaCore;
-import org.eclipse.jdt.internal.core.JavaProject;
-import org.eclipse.jdt.internal.core.OpenableElementInfo;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
-import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
@@ -79,7 +65,6 @@
import org.eclipse.ui.activities.WorkbenchActivityHelper;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
-import org.eclipse.ui.dialogs.IOverwriteQuery;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.IPreferenceConstants;
import org.eclipse.ui.internal.WorkbenchPlugin;
@@ -89,45 +74,21 @@
import org.eclipse.ui.internal.ide.IDEInternalPreferences;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
import org.eclipse.ui.internal.util.PrefUtil;
-import org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider;
import org.eclipse.ui.internal.wizards.newresource.ResourceMessages;
import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
-import org.eclipse.ui.wizards.datatransfer.ImportOperation;
import org.jboss.tools.project.examples.Messages;
import org.jboss.tools.project.examples.ProjectExamplesActivator;
import org.jboss.tools.project.examples.dialog.MarkerDialog;
-import org.jboss.tools.project.examples.fixes.SeamRuntimeFix;
-import org.jboss.tools.project.examples.fixes.WTPRuntimeFix;
import org.jboss.tools.project.examples.model.IImportProjectExample;
import org.jboss.tools.project.examples.model.Project;
-import org.jboss.tools.project.examples.model.ProjectFix;
import org.jboss.tools.project.examples.model.ProjectUtil;
public class NewProjectExamplesWizard extends Wizard implements INewWizard {
- private static final IOverwriteQuery OVERWRITE_ALL_QUERY = new IOverwriteQuery() {
- public String queryOverwrite(String pathString) {
- return IOverwriteQuery.ALL;
- }
- };
-
private List<Project> projects = new ArrayList<Project>();
- /**
- * The workbench.
- */
- private IWorkbench workbench;
-
- /**
- * The current selection.
- */
- private IStructuredSelection selection;
-
+
private NewProjectExamplesWizardPage page;
- private static Shell shell;
-
- protected static boolean overwrite;
-
private WorkspaceJob workspaceJob;
public NewProjectExamplesWizard() {
@@ -154,59 +115,35 @@
IStructuredSelection selection = page.getSelection();
Iterator iterator = selection.iterator();
projects.clear();
- List<File> files = new ArrayList<File>();
- File file = null;
while (iterator.hasNext()) {
Object object = iterator.next();
if (object instanceof Project) {
Project project = (Project) object;
- if (project.isURLRequired()) {
- String urlString = project.getUrl();
- String name = project.getName();
- URL url = null;
- try {
- url = new URL(urlString);
- } catch (MalformedURLException e) {
- ProjectExamplesActivator.log(e);
- continue;
- }
- file = ProjectUtil.getProjectExamplesFile(url, name,
- ".zip", monitor); //$NON-NLS-1$
- if (file == null) {
- return Status.CANCEL_STATUS;
- }
+ boolean success = ProjectExamplesActivator.downloadProject(project, monitor);
+ if (success) {
+ projects.add(project);
}
- projects.add(project);
- files.add(file);
}
}
try {
- int i = 0;
setName(Messages.NewProjectExamplesWizard_Importing);
for (final Project project : projects) {
- if (project.getImportType() == null) {
- importProject(project, files.get(i++), monitor);
- ProjectExamplesActivator.fix(project, monitor);
- } else {
- IImportProjectExample importProjectExample =
- ProjectExamplesActivator.getDefault().getImportProjectExample(project.getImportType());
- if (importProjectExample == null) {
- Display.getDefault().syncExec(new Runnable() {
+ IImportProjectExample importProjectExample =
+ ProjectExamplesActivator.getDefault().getImportProjectExample(project.getImportType());
+ if (importProjectExample == null) {
+ Display.getDefault().syncExec(new Runnable() {
- public void run() {
- MessageDialogWithToggle.openError(getShell(),
- Messages.NewProjectExamplesWizard_Error,
- "Cannot import a project of the '" + project.getImportType() +
"' type.");
- }
-
- });
- return Status.OK_STATUS;
- }
- importProjectExample.importProject(project, files.get(i++), monitor);
- importProjectExample.fix(project, monitor);
+ public void run() {
+ MessageDialogWithToggle.openError(getShell(),
+ Messages.NewProjectExamplesWizard_Error,
+ "Cannot import a project of the '" + project.getImportType() +
"' type.");
+ }
+ });
+ return Status.OK_STATUS;
}
+ importProjectExample.importProject(project, project.getFile(), monitor);
+ importProjectExample.fix(project, monitor);
}
-
} catch (final Exception e) {
Display.getDefault().syncExec(new Runnable() {
@@ -261,7 +198,7 @@
showQuickFix(projects);
}
}
- openWelcome();
+ ProjectExamplesActivator.openWelcome(projects);
}
public void running(IJobChangeEvent event) {
@@ -279,7 +216,7 @@
});
} else {
updatePerspective();
- openWelcome();
+ ProjectExamplesActivator.openWelcome(projects);
}
workspaceJob.schedule();
return true;
@@ -481,64 +418,7 @@
// Set the perspective.
page.setPerspective(persp);
}
- private void openWelcome() {
- for(final Project project:projects) {
- if (project.isWelcome()) {
- String urlString = project.getWelcomeURL();
- URL url = null;
- if (urlString.startsWith("/")) { //$NON-NLS-1$
- IPath path = new Path(urlString);
- IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
- if (resource instanceof IFile && resource.isAccessible()) {
- try {
- url = resource.getRawLocationURI().toURL();
- } catch (MalformedURLException e) {
- ProjectExamplesActivator.log(e);
- }
- } else {
- ProjectExamplesActivator.log(NLS.bind(Messages.NewProjectExamplesWizard_File_does_not_exist,urlString));
- }
- } else {
- try {
- url = new URL(urlString);
- } catch (MalformedURLException e) {
- ProjectExamplesActivator.log(e);
- }
- }
- if (url!=null) {
- final URL finalURL = url;
- Display.getDefault().asyncExec(new Runnable() {
- public void run() {
- if (ProjectUtil.CHEATSHEETS.equals(project.getType())) {
- CheatSheetView view = ViewUtilities.showCheatSheetView();
- if (view == null) {
- return;
- }
- IPath filePath = new Path(finalURL.getPath());
- String id = filePath.lastSegment();
- if (id == null) {
- id = ""; //$NON-NLS-1$
- }
- view.getCheatSheetViewer().setInput(id, id, finalURL, new DefaultStateManager(),
false);
- } else {
- try {
- IWorkbenchBrowserSupport browserSupport =
ProjectExamplesActivator.getDefault().getWorkbench().getBrowserSupport();
- IWebBrowser browser =
browserSupport.createBrowser(IWorkbenchBrowserSupport.LOCATION_BAR |
IWorkbenchBrowserSupport.NAVIGATION_BAR, null, null, null);
- browser.openURL(finalURL);
- } catch (PartInitException e) {
- ProjectExamplesActivator.log(e);
- }
- }
- }
-
- });
-
- }
- }
- }
- }
-
public static void showQuickFix(final List<Project> projects) {
Display.getDefault().asyncExec(new Runnable() {
@@ -554,145 +434,8 @@
});
}
- public static void importProject(Project projectDescription, File file,
- IProgressMonitor monitor) throws Exception {
- if (projectDescription.getIncludedProjects() == null) {
- importSingleProject(projectDescription, file, monitor);
- } else {
- List<String> projects = projectDescription.getIncludedProjects();
- for (final String projectName : projects) {
- IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IProject project = workspace.getRoot().getProject(projectName);
- if (project.exists()) {
- Display.getDefault().syncExec(new Runnable() {
-
- public void run() {
- overwrite = MessageDialog.openQuestion(getActiveShell(),
- Messages.NewProjectExamplesWizard_Question,
NLS.bind(Messages.NewProjectExamplesWizard_OverwriteProject,
- projectName));
- }
-
- });
- if (!overwrite) {
- return;
- }
- project.delete(true, true, monitor);
- }
- project.create(monitor);
- project.open(monitor);
- ZipFile sourceFile = new ZipFile(file);
- ZipLeveledStructureProvider structureProvider = new ZipLeveledStructureProvider(
- sourceFile);
-
- Enumeration<? extends ZipEntry> entries = sourceFile.entries();
- ZipEntry entry = null;
- List<ZipEntry> filesToImport = new ArrayList<ZipEntry>();
- List<ZipEntry> directories = new ArrayList<ZipEntry>();
- String prefix = projectName + "/"; //$NON-NLS-1$
- while (entries.hasMoreElements()) {
- entry = entries.nextElement();
- if (entry.getName().startsWith(prefix)) {
- if (!entry.isDirectory()) {
- filesToImport.add(entry);
- } else {
- directories.add(entry);
- }
- }
- }
-
- structureProvider.setStrip(1);
- ImportOperation operation = new ImportOperation(project.getFullPath(),
structureProvider.getRoot(),
- structureProvider, OVERWRITE_ALL_QUERY, filesToImport);
- operation.setContext(getActiveShell());
- operation.run(monitor);
- for (ZipEntry directory:directories) {
- IPath resourcePath = new Path(directory.getName());
- try {
- workspace.getRoot().getFolder(resourcePath).create(false, true, null);
- } catch (Exception e) {
- ProjectExamplesActivator.log(e);
- }
- }
- reconfigure(project, monitor);
- }
- }
- }
-
- private static Shell getActiveShell() {
- Display display = Display.getDefault();
- shell = null;
- display.syncExec(new Runnable() {
-
- public void run() {
- shell = Display.getCurrent().getActiveShell();
- }
-
- });
- return shell;
- }
- private static void importSingleProject(Project projectDescription, File file,
- IProgressMonitor monitor) throws CoreException, ZipException,
- IOException, InvocationTargetException, InterruptedException {
- final String projectName = projectDescription.getName();
- IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IProject project = workspace.getRoot().getProject(projectName);
- if (project.exists()) {
- Display.getDefault().syncExec(new Runnable() {
-
- public void run() {
- overwrite = MessageDialog.openQuestion(getActiveShell(),
- Messages.NewProjectExamplesWizard_Question,
NLS.bind(Messages.NewProjectExamplesWizard_OverwriteProject,
- projectName));
- }
-
- });
- if (!overwrite) {
- return;
- }
- project.delete(true, true, monitor);
- }
- project.create(monitor);
- project.open(monitor);
- ZipFile sourceFile = new ZipFile(file);
- ZipLeveledStructureProvider structureProvider = new ZipLeveledStructureProvider(
- sourceFile);
-
- Enumeration<? extends ZipEntry> entries = sourceFile.entries();
- ZipEntry entry = null;
- List<ZipEntry> filesToImport = new ArrayList<ZipEntry>();
- List<ZipEntry> directories = new ArrayList<ZipEntry>();
- String prefix = projectName + "/"; //$NON-NLS-1$
- while (entries.hasMoreElements()) {
- entry = entries.nextElement();
- if (entry.getName().startsWith(prefix)) {
- if (!entry.isDirectory()) {
- filesToImport.add(entry);
- } else {
- directories.add(entry);
- }
- }
- }
-
- structureProvider.setStrip(1);
- ImportOperation operation = new ImportOperation(project.getFullPath(),
structureProvider.getRoot(),
- structureProvider, OVERWRITE_ALL_QUERY, filesToImport);
- operation.setContext(getActiveShell());
- operation.run(monitor);
- for (ZipEntry directory:directories) {
- IPath resourcePath = new Path(directory.getName());
- try {
- workspace.getRoot().getFolder(resourcePath).create(false, true, null);
- } catch (Exception e) {
- ProjectExamplesActivator.log(e);
- }
- }
- reconfigure(project, monitor);
-
- }
-
+
public void init(IWorkbench workbench, IStructuredSelection selection) {
- this.workbench = workbench;
- this.selection = selection;
initializeDefaultPageImageDescriptor();
}
@@ -709,46 +452,5 @@
page = new NewProjectExamplesWizardPage();
addPage(page);
}
-
- private static List prepareFileList(IImportStructureProvider structure,
- ZipEntry entry, List list) {
- if (structure == null || entry == null)
- return null;
- if (list == null) {
- list = new ArrayList();
- }
- List son = structure.getChildren(entry);
- if (son == null)
- return list;
- Iterator it = son.iterator();
- while (it.hasNext()) {
- ZipEntry temp = (ZipEntry) it.next();
- if (temp.isDirectory()) {
- prepareFileList(structure, temp, list);
- } else {
- list.add(temp);
- }
- }
- return list;
- }
- private static void reconfigure(IProject project, IProgressMonitor monitor) throws
CoreException {
- if (project == null || !project.exists() || !project.isOpen() ||
!project.hasNature(JavaCore.NATURE_ID)) {
- return;
- }
- project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
- IJavaProject javaProject = JavaCore.create(project);
- if (javaProject != null && javaProject.exists() && javaProject.isOpen()
&& javaProject instanceof JavaProject) {
- Object object = ((JavaProject) javaProject).getElementInfo();
- if (object instanceof OpenableElementInfo) {
- // copied from JavaProject.buildStructure(...)
- OpenableElementInfo info = (OpenableElementInfo) object;
- IClasspathEntry[] resolvedClasspath = ((JavaProject)
javaProject).getResolvedClasspath();
- IPackageFragmentRoot[] children = ((JavaProject)
javaProject).computePackageFragmentRoots(resolvedClasspath,false, null /* no reverse map
*/);
- info.setChildren(children);
- ((JavaProject) javaProject).getPerProjectInfo().rememberExternalLibTimestamps();
- }
- }
- }
-
}
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples.cheatsheet/src/org/jboss/tools/project/examples/cheatsheet/actions/ImportProjectExample.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples.cheatsheet/src/org/jboss/tools/project/examples/cheatsheet/actions/ImportProjectExample.java 2011-09-13
12:20:40 UTC (rev 34676)
+++
trunk/examples/plugins/org.jboss.tools.project.examples.cheatsheet/src/org/jboss/tools/project/examples/cheatsheet/actions/ImportProjectExample.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -21,6 +21,7 @@
import org.jboss.tools.project.examples.Messages;
import org.jboss.tools.project.examples.ProjectExamplesActivator;
import org.jboss.tools.project.examples.cheatsheet.Activator;
+import org.jboss.tools.project.examples.model.IImportProjectExample;
import org.jboss.tools.project.examples.model.Project;
import org.jboss.tools.project.examples.model.ProjectUtil;
import org.jboss.tools.project.examples.wizard.NewProjectExamplesWizard;
@@ -68,7 +69,9 @@
setName(Messages.NewProjectExamplesWizard_Importing);
try {
- NewProjectExamplesWizard.importProject(project, file, monitor);
+ IImportProjectExample importProjectExample =
ProjectExamplesActivator.getDefault().getImportProjectExample(project.getImportType());
+ importProjectExample.importProject(project, file, monitor);
+ importProjectExample.fix(project, monitor);
} catch (Exception e) {
IStatus status = new Status(IStatus.ERROR,Activator.PLUGIN_ID,e.getMessage(),e);
throw new CoreException(status);
Modified:
trunk/examples/tests/org.jboss.tools.project.examples.test/src/org/jboss/tools/project/examples/test/ProjectTest.java
===================================================================
---
trunk/examples/tests/org.jboss.tools.project.examples.test/src/org/jboss/tools/project/examples/test/ProjectTest.java 2011-09-13
12:20:40 UTC (rev 34676)
+++
trunk/examples/tests/org.jboss.tools.project.examples.test/src/org/jboss/tools/project/examples/test/ProjectTest.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -31,6 +31,7 @@
import org.jboss.tools.project.examples.Messages;
import org.jboss.tools.project.examples.ProjectExamplesActivator;
import org.jboss.tools.project.examples.model.Category;
+import org.jboss.tools.project.examples.model.IImportProjectExample;
import org.jboss.tools.project.examples.model.Project;
import org.jboss.tools.project.examples.model.ProjectUtil;
import org.jboss.tools.project.examples.wizard.NewProjectExamplesWizard;
@@ -101,8 +102,9 @@
File file = ProjectUtil.getProjectExamplesFile(
url, name, ".zip", monitor); //$NON-NLS-1$
assertNotNull(file);
- NewProjectExamplesWizard.importProject(projectExample, file, monitor);
- ProjectExamplesActivator.fix(projectExample, monitor);
+ IImportProjectExample importProjectExample =
ProjectExamplesActivator.getDefault().getImportProjectExample(projectExample.getImportType());
+ importProjectExample.importProject(projectExample, file, monitor);
+ importProjectExample.fix(projectExample, monitor);
}
@Test
Modified:
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java 2011-09-13
12:20:40 UTC (rev 34676)
+++
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java 2011-09-13
12:26:47 UTC (rev 34677)
@@ -11,16 +11,9 @@
package org.jboss.tools.maven.project.examples;
import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Enumeration;
import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import org.apache.maven.model.Model;
import org.eclipse.core.resources.IProject;
@@ -43,10 +36,10 @@
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction;
-import org.eclipse.m2e.core.ui.internal.console.MavenConsole;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.progress.IProgressConstants;
+import org.jboss.tools.project.examples.ProjectExamplesActivator;
import org.jboss.tools.project.examples.job.ProjectExamplesJob;
import org.jboss.tools.project.examples.model.AbstractImportProjectExample;
import org.jboss.tools.project.examples.model.Project;
@@ -113,7 +106,7 @@
return projects;
}
}
- boolean ok = extractFile(file, destination, monitor);
+ boolean ok = ProjectExamplesActivator.extractZipFile(file, destination, monitor);
monitor.setTaskName("");
if (monitor.isCanceled()) {
return projects;
@@ -229,74 +222,7 @@
private static Shell getActiveShell() {
return Display.getDefault().getActiveShell();
}
-
- public boolean extractFile(File file, File destination,
- IProgressMonitor monitor) {
- ZipFile zipFile = null;
- destination.mkdirs();
- try {
- zipFile = new ZipFile(file);
- Enumeration<? extends ZipEntry> entries = zipFile.entries();
- while (entries.hasMoreElements()) {
- if (monitor.isCanceled()) {
- return false;
- }
- ZipEntry entry = (ZipEntry) entries.nextElement();
- if (entry.isDirectory()) {
- monitor.setTaskName("Extracting " + entry.getName());
- File dir = new File(destination, entry.getName());
- dir.mkdirs();
- continue;
- }
- monitor.setTaskName("Extracting " + entry.getName());
- File entryFile = new File(destination, entry.getName());
- entryFile.getParentFile().mkdirs();
- InputStream in = null;
- OutputStream out = null;
- try {
- in = zipFile.getInputStream(entry);
- out = new FileOutputStream(entryFile);
- copy(in, out);
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (Exception e) {
- // ignore
- }
- }
- if (out != null) {
- try {
- out.close();
- } catch (Exception e) {
- // ignore
- }
- }
- }
- }
- } catch (IOException e) {
- MavenProjectExamplesActivator.log(e);
- return false;
- } finally {
- if (zipFile != null) {
- try {
- zipFile.close();
- } catch (IOException e) {
- // ignore
- }
- }
- }
- return true;
- }
- public static void copy(InputStream in, OutputStream out) throws IOException {
- byte[] buffer = new byte[16 * 1024];
- int len;
- while ((len = in.read(buffer)) >= 0) {
- out.write(buffer, 0, len);
- }
- }
-
private static boolean deleteDirectory(File path, IProgressMonitor monitor) {
if (path.exists()) {
File[] files = path.listFiles();