1 11 package org.eclipse.pde.internal.ui.wizards.imports; 12 13 import org.eclipse.core.resources.IProject; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.jface.dialogs.IDialogConstants; 21 import org.eclipse.jface.dialogs.IDialogSettings; 22 import org.eclipse.jface.dialogs.MessageDialog; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.wizard.Wizard; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 27 import org.eclipse.pde.internal.ui.PDEPlugin; 28 import org.eclipse.pde.internal.ui.PDEPluginImages; 29 import org.eclipse.pde.internal.ui.PDEUIMessages; 30 import org.eclipse.pde.internal.ui.wizards.imports.FeatureImportOperation.IReplaceQuery; 31 import org.eclipse.swt.widgets.Display; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.ui.IImportWizard; 34 import org.eclipse.ui.IWorkbench; 35 36 public class FeatureImportWizard extends Wizard implements IImportWizard { 37 38 private static final String STORE_SECTION = "FeatureImportWizard"; private FeatureImportWizardPage fPage; 40 41 public FeatureImportWizard() { 42 IDialogSettings masterSettings = PDEPlugin.getDefault().getDialogSettings(); 43 setDialogSettings(getSettingsSection(masterSettings)); 44 setDefaultPageImageDescriptor(PDEPluginImages.DESC_FEATURE_IMPORT_WIZ); 45 setWindowTitle(PDEUIMessages.FeatureImportWizard_title); 46 } 47 48 51 public void init(IWorkbench workbench, IStructuredSelection selection) { 52 } 53 54 57 public void addPages() { 58 setNeedsProgressMonitor(false); 59 60 fPage = new FeatureImportWizardPage(); 61 addPage(fPage); 62 } 63 64 private IDialogSettings getSettingsSection(IDialogSettings master) { 65 IDialogSettings setting = master.getSection(STORE_SECTION); 66 if (setting == null) { 67 setting = master.addNewSection(STORE_SECTION); 68 } 69 return setting; 70 } 71 72 75 public boolean performFinish() { 76 IFeatureModel[] models = fPage.getSelectedModels(); 77 fPage.storeSettings(true); 78 IPath targetPath = computeTargetPath(); 79 80 IReplaceQuery query = new ReplaceQuery(getShell()); 81 final FeatureImportOperation op = 82 new FeatureImportOperation(models, fPage.isBinary(), targetPath, query); 83 Job job = new Job(PDEUIMessages.FeatureImportWizard_title) { 84 protected IStatus run(IProgressMonitor monitor) { 85 try { 86 PDEPlugin.getWorkspace().run(op, monitor); 87 } catch (CoreException e) { 88 PDEPlugin.logException(e); 89 return Status.CANCEL_STATUS; 90 } 91 return Status.OK_STATUS; 92 } 93 }; 94 job.setUser(true); 95 job.schedule(); 96 return true; 97 } 98 99 private IPath computeTargetPath() { 103 IPath pluginsLocation = PDEPlugin.getWorkspace().getRoot().getLocation(); 104 if("plugins".equals(pluginsLocation.lastSegment())) return pluginsLocation.removeLastSegments(1).append("features"); return null; 107 } 108 109 private static class ReplaceDialog extends MessageDialog { 110 public ReplaceDialog(Shell parentShell, String dialogMessage) { 111 super( 112 parentShell, 113 PDEUIMessages.FeatureImportWizard_messages_title, 114 null, 115 dialogMessage, 116 MessageDialog.QUESTION, 117 new String [] { 118 IDialogConstants.YES_LABEL, 119 IDialogConstants.YES_TO_ALL_LABEL, 120 IDialogConstants.NO_LABEL, 121 PDEUIMessages.FeatureImportWizard_noToAll, 122 IDialogConstants.CANCEL_LABEL }, 123 0); 124 } 125 } 126 127 public static class ReplaceQuery implements IReplaceQuery { 128 public ReplaceQuery(Shell shell) {} 129 130 private int yesToAll = 0; 131 private int[] RETURNCODES = { 132 IReplaceQuery.YES, 133 IReplaceQuery.YES, 134 IReplaceQuery.NO, 135 IReplaceQuery.NO, 136 IReplaceQuery.CANCEL }; 137 138 public int doQuery(IProject project) { 139 if (yesToAll != 0) 140 return yesToAll > 0 ? IReplaceQuery.YES : IReplaceQuery.NO; 141 142 final String message = NLS.bind( 143 PDEUIMessages.FeatureImportWizard_messages_exists, 144 project.getName()); 145 final int[] result = { IReplaceQuery.CANCEL }; 146 147 Display.getDefault().syncExec(new Runnable () { 148 public void run() { 149 ReplaceDialog dialog = 150 new ReplaceDialog(Display.getDefault().getActiveShell(), message); 151 int retVal = dialog.open(); 152 if (retVal >= 0) { 153 result[0] = RETURNCODES[retVal]; 154 if (retVal == 1) { 155 yesToAll = 1; 156 } else if (retVal == 3) { 157 yesToAll = -1; 158 } 159 } 160 } 161 }); 162 return result[0]; 163 } 164 } 165 } 166 | Popular Tags |