|
Lines 11-35
Link Here
|
| 11 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
| 12 |
package org.eclipse.php.internal.ui.wizards; |
12 |
package org.eclipse.php.internal.ui.wizards; |
| 13 |
|
13 |
|
| 14 |
import java.io.File; |
14 |
import java.io.*; |
|
|
15 |
import java.lang.reflect.InvocationTargetException; |
| 15 |
import java.net.URI; |
16 |
import java.net.URI; |
|
|
17 |
import java.net.URISyntaxException; |
| 16 |
import java.util.Map; |
18 |
import java.util.Map; |
| 17 |
import java.util.Observable; |
19 |
import java.util.Observable; |
| 18 |
import java.util.Observer; |
20 |
import java.util.Observer; |
| 19 |
|
21 |
|
|
|
22 |
import org.eclipse.core.filesystem.EFS; |
| 23 |
import org.eclipse.core.filesystem.IFileStore; |
| 20 |
import org.eclipse.core.resources.IProject; |
24 |
import org.eclipse.core.resources.IProject; |
| 21 |
import org.eclipse.core.resources.IResource; |
25 |
import org.eclipse.core.resources.IResource; |
| 22 |
import org.eclipse.core.resources.IWorkspace; |
26 |
import org.eclipse.core.resources.IWorkspace; |
| 23 |
import org.eclipse.core.resources.ResourcesPlugin; |
27 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 24 |
import org.eclipse.core.runtime.*; |
28 |
import org.eclipse.core.runtime.*; |
|
|
29 |
import org.eclipse.dltk.core.DLTKCore; |
| 25 |
import org.eclipse.dltk.core.environment.EnvironmentManager; |
30 |
import org.eclipse.dltk.core.environment.EnvironmentManager; |
| 26 |
import org.eclipse.dltk.core.environment.IEnvironment; |
31 |
import org.eclipse.dltk.core.environment.IEnvironment; |
|
|
32 |
import org.eclipse.dltk.internal.corext.util.Messages; |
| 33 |
import org.eclipse.dltk.internal.ui.util.CoreUtility; |
| 27 |
import org.eclipse.dltk.internal.ui.wizards.NewWizardMessages; |
34 |
import org.eclipse.dltk.internal.ui.wizards.NewWizardMessages; |
| 28 |
import org.eclipse.dltk.internal.ui.wizards.dialogfields.*; |
35 |
import org.eclipse.dltk.internal.ui.wizards.dialogfields.*; |
| 29 |
import org.eclipse.dltk.ui.DLTKUIPlugin; |
36 |
import org.eclipse.dltk.ui.DLTKUIPlugin; |
| 30 |
import org.eclipse.dltk.ui.environment.IEnvironmentUI; |
37 |
import org.eclipse.dltk.ui.environment.IEnvironmentUI; |
|
|
38 |
import org.eclipse.dltk.ui.util.ExceptionHandler; |
| 31 |
import org.eclipse.jface.dialogs.Dialog; |
39 |
import org.eclipse.jface.dialogs.Dialog; |
| 32 |
import org.eclipse.jface.dialogs.IDialogConstants; |
40 |
import org.eclipse.jface.dialogs.IDialogConstants; |
|
|
41 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 33 |
import org.eclipse.jface.wizard.IWizardPage; |
42 |
import org.eclipse.jface.wizard.IWizardPage; |
| 34 |
import org.eclipse.jface.wizard.WizardPage; |
43 |
import org.eclipse.jface.wizard.WizardPage; |
| 35 |
import org.eclipse.php.internal.core.PHPVersion; |
44 |
import org.eclipse.php.internal.core.PHPVersion; |
|
Lines 37-42
Link Here
|
| 37 |
import org.eclipse.php.internal.ui.PHPUIMessages; |
46 |
import org.eclipse.php.internal.ui.PHPUIMessages; |
| 38 |
import org.eclipse.php.internal.ui.PHPUiPlugin; |
47 |
import org.eclipse.php.internal.ui.PHPUiPlugin; |
| 39 |
import org.eclipse.php.internal.ui.preferences.*; |
48 |
import org.eclipse.php.internal.ui.preferences.*; |
|
|
49 |
import org.eclipse.php.ui.util.PHPProjectUtils; |
| 40 |
import org.eclipse.swt.SWT; |
50 |
import org.eclipse.swt.SWT; |
| 41 |
import org.eclipse.swt.events.SelectionEvent; |
51 |
import org.eclipse.swt.events.SelectionEvent; |
| 42 |
import org.eclipse.swt.events.SelectionListener; |
52 |
import org.eclipse.swt.events.SelectionListener; |
|
Lines 44-49
Link Here
|
| 44 |
import org.eclipse.swt.layout.GridLayout; |
54 |
import org.eclipse.swt.layout.GridLayout; |
| 45 |
import org.eclipse.swt.widgets.*; |
55 |
import org.eclipse.swt.widgets.*; |
| 46 |
import org.eclipse.ui.PlatformUI; |
56 |
import org.eclipse.ui.PlatformUI; |
|
|
57 |
import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; |
| 47 |
import org.eclipse.ui.dialogs.PreferencesUtil; |
58 |
import org.eclipse.ui.dialogs.PreferencesUtil; |
| 48 |
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; |
59 |
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; |
| 49 |
|
60 |
|
|
Lines 52-57
Link Here
|
| 52 |
*/ |
63 |
*/ |
| 53 |
public class PHPProjectWizardFirstPage extends WizardPage implements |
64 |
public class PHPProjectWizardFirstPage extends WizardPage implements |
| 54 |
IPHPProjectCreateWizardPage { |
65 |
IPHPProjectCreateWizardPage { |
|
|
66 |
|
| 55 |
public PHPProjectWizardFirstPage() { |
67 |
public PHPProjectWizardFirstPage() { |
| 56 |
super(PAGE_NAME); |
68 |
super(PAGE_NAME); |
| 57 |
setPageComplete(false); |
69 |
setPageComplete(false); |
|
Lines 59-64
Link Here
|
| 59 |
setDescription(NewWizardMessages.ScriptProjectWizardFirstPage_page_description); |
71 |
setDescription(NewWizardMessages.ScriptProjectWizardFirstPage_page_description); |
| 60 |
fInitialName = ""; //$NON-NLS-1$ |
72 |
fInitialName = ""; //$NON-NLS-1$ |
| 61 |
} |
73 |
} |
|
|
74 |
|
| 75 |
private static final String FILENAME_PROJECT = ".project"; //$NON-NLS-1$ |
| 76 |
protected static final String FILENAME_BUILDPATH = ".buildpath"; //$NON-NLS-1$ |
| 77 |
protected URI fCurrProjectLocation; // null if location is platform location |
| 78 |
|
| 79 |
private boolean fKeepContent; |
| 80 |
|
| 81 |
private File fDotProjectBackup; |
| 82 |
private File fDotBuildpathBackup; |
| 83 |
private Boolean fIsAutobuild; |
| 62 |
|
84 |
|
| 63 |
private static final String PAGE_NAME = NewWizardMessages.ScriptProjectWizardFirstPage_page_title; |
85 |
private static final String PAGE_NAME = NewWizardMessages.ScriptProjectWizardFirstPage_page_title; |
| 64 |
public static final String ERROR_MESSAGE = "ErrorMessage"; |
86 |
public static final String ERROR_MESSAGE = "ErrorMessage"; |
|
Lines 617-632
Link Here
|
| 617 |
public void setVisible(boolean visible) { |
639 |
public void setVisible(boolean visible) { |
| 618 |
super.setVisible(visible); |
640 |
super.setVisible(visible); |
| 619 |
|
641 |
|
| 620 |
IWizardPage currentPage = getContainer().getCurrentPage(); |
642 |
if (visible) { |
| 621 |
if (!visible && currentPage != null) { |
643 |
// if there is a project we are going from 2nd to 1st |
| 622 |
// going forward from 1st page to 2nd one |
644 |
// remove the project |
|
|
645 |
removeProject(); |
| 646 |
} else { |
| 647 |
IWizardPage currentPage = getContainer().getCurrentPage(); |
| 623 |
if (currentPage instanceof IPHPProjectCreateWizardPage) { |
648 |
if (currentPage instanceof IPHPProjectCreateWizardPage) { |
|
|
649 |
// going forward from 1st page to 2nd one |
| 650 |
changeToNewProject(); |
| 624 |
((IPHPProjectCreateWizardPage) currentPage).initPage(); |
651 |
((IPHPProjectCreateWizardPage) currentPage).initPage(); |
| 625 |
} |
652 |
} |
| 626 |
} |
653 |
} |
| 627 |
|
654 |
|
| 628 |
} |
655 |
} |
| 629 |
|
656 |
|
|
|
657 |
private void removeProject() { |
| 658 |
if (fNameGroup == null || fNameGroup.getName() == null |
| 659 |
|| fNameGroup.getName().length() == 0) { |
| 660 |
return; |
| 661 |
} |
| 662 |
if (getProjectHandle() == null || !getProjectHandle().exists()) { |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
IRunnableWithProgress op = new IRunnableWithProgress() { |
| 667 |
public void run(IProgressMonitor monitor) |
| 668 |
throws InvocationTargetException, InterruptedException { |
| 669 |
doRemoveProject(monitor); |
| 670 |
} |
| 671 |
}; |
| 672 |
|
| 673 |
try { |
| 674 |
getContainer().run(true, true, |
| 675 |
new WorkspaceModifyDelegatingOperation(op)); |
| 676 |
} catch (InvocationTargetException e) { |
| 677 |
final String title = NewWizardMessages.ScriptProjectWizardSecondPage_error_remove_title; |
| 678 |
final String message = NewWizardMessages.ScriptProjectWizardSecondPage_error_remove_message; |
| 679 |
ExceptionHandler.handle(e, getShell(), title, message); |
| 680 |
} catch (InterruptedException e) { |
| 681 |
// cancel pressed |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
final void doRemoveProject(IProgressMonitor monitor) |
| 686 |
throws InvocationTargetException { |
| 687 |
final boolean noProgressMonitor = (fCurrProjectLocation == null); // inside |
| 688 |
// workspace |
| 689 |
if (monitor == null || noProgressMonitor) { |
| 690 |
monitor = new NullProgressMonitor(); |
| 691 |
} |
| 692 |
monitor.beginTask( |
| 693 |
NewWizardMessages.ScriptProjectWizardSecondPage_operation_remove, |
| 694 |
3); |
| 695 |
try { |
| 696 |
try { |
| 697 |
URI projLoc = getProjectHandle().getLocationURI(); |
| 698 |
|
| 699 |
boolean removeContent = !fKeepContent |
| 700 |
&& getProjectHandle().isSynchronized( |
| 701 |
IResource.DEPTH_INFINITE); |
| 702 |
getProjectHandle().delete(removeContent, false, |
| 703 |
new SubProgressMonitor(monitor, 2)); |
| 704 |
|
| 705 |
} finally { |
| 706 |
CoreUtility.enableAutoBuild(fIsAutobuild.booleanValue()); // fIsAutobuild |
| 707 |
// must |
| 708 |
// be |
| 709 |
// set |
| 710 |
fIsAutobuild = null; |
| 711 |
} |
| 712 |
} catch (CoreException e) { |
| 713 |
throw new InvocationTargetException(e); |
| 714 |
} finally { |
| 715 |
monitor.done(); |
| 716 |
fKeepContent = false; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
private void changeToNewProject() { |
| 721 |
fKeepContent = this.getDetect(); |
| 722 |
|
| 723 |
final IRunnableWithProgress op = new IRunnableWithProgress() { |
| 724 |
public void run(IProgressMonitor monitor) |
| 725 |
throws InvocationTargetException, InterruptedException { |
| 726 |
try { |
| 727 |
if (fIsAutobuild == null) { |
| 728 |
fIsAutobuild = Boolean.valueOf(CoreUtility |
| 729 |
.enableAutoBuild(false)); |
| 730 |
} |
| 731 |
updateProject(monitor); |
| 732 |
} catch (CoreException e) { |
| 733 |
throw new InvocationTargetException(e); |
| 734 |
} catch (OperationCanceledException e) { |
| 735 |
throw new InterruptedException(); |
| 736 |
} finally { |
| 737 |
monitor.done(); |
| 738 |
} |
| 739 |
} |
| 740 |
}; |
| 741 |
|
| 742 |
try { |
| 743 |
getContainer().run(true, false, |
| 744 |
new WorkspaceModifyDelegatingOperation(op)); |
| 745 |
} catch (InvocationTargetException e) { |
| 746 |
final String title = NewWizardMessages.ScriptProjectWizardSecondPage_error_title; |
| 747 |
final String message = NewWizardMessages.ScriptProjectWizardSecondPage_error_message; |
| 748 |
ExceptionHandler.handle(e, getShell(), title, message); |
| 749 |
} catch (InterruptedException e) { |
| 750 |
// cancel pressed |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Called from the wizard on cancel. |
| 756 |
*/ |
| 757 |
public void performCancel() { |
| 758 |
removeProject(); |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* Helper method to create and open a IProject. The project location is |
| 763 |
* configured. No natures are added. |
| 764 |
* |
| 765 |
* @param project |
| 766 |
* The handle of the project to create. |
| 767 |
* @param locationURI |
| 768 |
* The location of the project or <code>null</code> to create the |
| 769 |
* project in the workspace |
| 770 |
* @param monitor |
| 771 |
* a progress monitor to report progress or <code>null</code> if |
| 772 |
* progress reporting is not desired |
| 773 |
* @throws CoreException |
| 774 |
* if the project couldn't be created |
| 775 |
* @see org.eclipse.core.resources.IProjectDescription#setLocationURI(java.net.URI) |
| 776 |
* |
| 777 |
*/ |
| 778 |
public void createProject(IProject project, URI locationURI, |
| 779 |
IProgressMonitor monitor) throws CoreException { |
| 780 |
PHPProjectUtils.createProjectAt(project, locationURI, monitor); |
| 781 |
} |
| 782 |
|
| 783 |
protected void rememberExistingFiles(URI projectLocation) |
| 784 |
throws CoreException { |
| 785 |
fDotProjectBackup = null; |
| 786 |
fDotBuildpathBackup = null; |
| 787 |
|
| 788 |
IFileStore file = EFS.getStore(projectLocation); |
| 789 |
if (file.fetchInfo().exists()) { |
| 790 |
IFileStore projectFile = file.getChild(FILENAME_PROJECT); |
| 791 |
if (projectFile.fetchInfo().exists()) { |
| 792 |
fDotProjectBackup = createBackup(projectFile, "project-desc"); //$NON-NLS-1$ |
| 793 |
} |
| 794 |
IFileStore buildpathFile = file.getChild(FILENAME_BUILDPATH); |
| 795 |
if (buildpathFile.fetchInfo().exists()) { |
| 796 |
fDotBuildpathBackup = createBackup(buildpathFile, |
| 797 |
"buildpath-desc"); //$NON-NLS-1$ |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
private void restoreExistingFiles(URI projectLocation, |
| 803 |
IProgressMonitor monitor) throws CoreException { |
| 804 |
int ticks = ((fDotProjectBackup != null ? 1 : 0) + (fDotBuildpathBackup != null ? 1 |
| 805 |
: 0)) * 2; |
| 806 |
monitor.beginTask("", ticks); //$NON-NLS-1$ |
| 807 |
try { |
| 808 |
if (fDotProjectBackup != null) { |
| 809 |
IFileStore projectFile = EFS.getStore(projectLocation) |
| 810 |
.getChild(FILENAME_PROJECT); |
| 811 |
projectFile |
| 812 |
.delete(EFS.NONE, new SubProgressMonitor(monitor, 1)); |
| 813 |
copyFile(fDotProjectBackup, projectFile, |
| 814 |
new SubProgressMonitor(monitor, 1)); |
| 815 |
} |
| 816 |
} catch (IOException e) { |
| 817 |
IStatus status = new Status( |
| 818 |
IStatus.ERROR, |
| 819 |
DLTKUIPlugin.PLUGIN_ID, |
| 820 |
IStatus.ERROR, |
| 821 |
NewWizardMessages.ScriptProjectWizardSecondPage_problem_restore_project, |
| 822 |
e); |
| 823 |
throw new CoreException(status); |
| 824 |
} |
| 825 |
try { |
| 826 |
if (fDotBuildpathBackup != null) { |
| 827 |
IFileStore buildpathFile = EFS.getStore(projectLocation) |
| 828 |
.getChild(FILENAME_BUILDPATH); |
| 829 |
buildpathFile.delete(EFS.NONE, new SubProgressMonitor(monitor, |
| 830 |
1)); |
| 831 |
copyFile(fDotBuildpathBackup, buildpathFile, |
| 832 |
new SubProgressMonitor(monitor, 1)); |
| 833 |
} |
| 834 |
} catch (IOException e) { |
| 835 |
IStatus status = new Status( |
| 836 |
IStatus.ERROR, |
| 837 |
DLTKUIPlugin.PLUGIN_ID, |
| 838 |
IStatus.ERROR, |
| 839 |
NewWizardMessages.ScriptProjectWizardSecondPage_problem_restore_buildpath, |
| 840 |
e); |
| 841 |
throw new CoreException(status); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
private File createBackup(IFileStore source, String name) |
| 846 |
throws CoreException { |
| 847 |
try { |
| 848 |
File bak = File.createTempFile("eclipse-" + name, ".bak"); //$NON-NLS-1$//$NON-NLS-2$ |
| 849 |
copyFile(source, bak); |
| 850 |
return bak; |
| 851 |
} catch (IOException e) { |
| 852 |
IStatus status = new Status( |
| 853 |
IStatus.ERROR, |
| 854 |
DLTKUIPlugin.PLUGIN_ID, |
| 855 |
IStatus.ERROR, |
| 856 |
Messages.format( |
| 857 |
NewWizardMessages.ScriptProjectWizardSecondPage_problem_backup, |
| 858 |
name), e); |
| 859 |
throw new CoreException(status); |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
private void copyFile(IFileStore source, File target) throws IOException, |
| 864 |
CoreException { |
| 865 |
InputStream is = source.openInputStream(EFS.NONE, null); |
| 866 |
FileOutputStream os = new FileOutputStream(target); |
| 867 |
copyFile(is, os); |
| 868 |
} |
| 869 |
|
| 870 |
private void copyFile(File source, IFileStore target, |
| 871 |
IProgressMonitor monitor) throws IOException, CoreException { |
| 872 |
FileInputStream is = new FileInputStream(source); |
| 873 |
OutputStream os = target.openOutputStream(EFS.NONE, monitor); |
| 874 |
copyFile(is, os); |
| 875 |
} |
| 876 |
|
| 877 |
private void copyFile(InputStream is, OutputStream os) throws IOException { |
| 878 |
try { |
| 879 |
byte[] buffer = new byte[8192]; |
| 880 |
while (true) { |
| 881 |
int bytesRead = is.read(buffer); |
| 882 |
if (bytesRead == -1) |
| 883 |
break; |
| 884 |
|
| 885 |
os.write(buffer, 0, bytesRead); |
| 886 |
} |
| 887 |
} finally { |
| 888 |
try { |
| 889 |
is.close(); |
| 890 |
} finally { |
| 891 |
os.close(); |
| 892 |
} |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
protected URI getProjectLocationURI() throws CoreException { |
| 897 |
if (this.isInWorkspace()) { |
| 898 |
return null; |
| 899 |
} |
| 900 |
return this.getLocationURI(); |
| 901 |
} |
| 902 |
|
| 903 |
protected void updateProject(IProgressMonitor monitor) |
| 904 |
throws CoreException, InterruptedException { |
| 905 |
|
| 906 |
IProject projectHandle = this.getProjectHandle(); |
| 907 |
DLTKCore.create(projectHandle); |
| 908 |
fCurrProjectLocation = getProjectLocationURI(); |
| 909 |
|
| 910 |
if (monitor == null) { |
| 911 |
monitor = new NullProgressMonitor(); |
| 912 |
} |
| 913 |
try { |
| 914 |
monitor.beginTask( |
| 915 |
NewWizardMessages.ScriptProjectWizardSecondPage_operation_initialize, |
| 916 |
70); |
| 917 |
if (monitor.isCanceled()) { |
| 918 |
throw new OperationCanceledException(); |
| 919 |
} |
| 920 |
|
| 921 |
URI realLocation = fCurrProjectLocation; |
| 922 |
if (fCurrProjectLocation == null) { // inside workspace |
| 923 |
try { |
| 924 |
URI rootLocation = ResourcesPlugin.getWorkspace().getRoot() |
| 925 |
.getLocationURI(); |
| 926 |
realLocation = new URI(rootLocation.getScheme(), null, Path |
| 927 |
.fromPortableString(rootLocation.getPath()) |
| 928 |
.append(projectHandle.getName()).toString(), null); |
| 929 |
} catch (URISyntaxException e) { |
| 930 |
Assert.isTrue(false, "Can't happen"); //$NON-NLS-1$ |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
rememberExistingFiles(realLocation); |
| 935 |
|
| 936 |
createProject(projectHandle, fCurrProjectLocation, |
| 937 |
new SubProgressMonitor(monitor, 20)); |
| 938 |
} finally { |
| 939 |
monitor.done(); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 630 |
public void initPage() { |
943 |
public void initPage() { |
| 631 |
} |
944 |
} |
| 632 |
|
945 |
|