1 11 package org.eclipse.pde.internal.ui.wizards.imports; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.util.ArrayList ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.MultiStatus; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.jface.dialogs.Dialog; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.viewers.CheckboxTableViewer; 29 import org.eclipse.jface.viewers.IStructuredContentProvider; 30 import org.eclipse.jface.viewers.StructuredViewer; 31 import org.eclipse.jface.wizard.WizardPage; 32 import org.eclipse.pde.internal.core.PDECore; 33 import org.eclipse.pde.internal.core.feature.ExternalFeatureModel; 34 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 35 import org.eclipse.pde.internal.ui.IHelpContextIds; 36 import org.eclipse.pde.internal.ui.PDEPlugin; 37 import org.eclipse.pde.internal.ui.PDEUIMessages; 38 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 39 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart; 40 import org.eclipse.pde.internal.ui.wizards.ListUtil; 41 import org.eclipse.swt.SWT; 42 import org.eclipse.swt.layout.GridData; 43 import org.eclipse.swt.layout.GridLayout; 44 import org.eclipse.swt.widgets.Composite; 45 import org.eclipse.ui.PlatformUI; 46 import org.eclipse.ui.forms.widgets.FormToolkit; 47 48 public class FeatureImportWizardDetailedPage extends WizardPage { 49 50 private FeatureImportWizardFirstPage fFirstPage; 51 private boolean fIsOtherLocation; 52 private IPath fDropLocation; 53 private CheckboxTableViewer fFeatureViewer; 54 private TablePart fTablePart; 55 private IFeatureModel[] fModels; 56 57 public class ContentProvider 58 extends DefaultContentProvider 59 implements IStructuredContentProvider { 60 public Object [] getElements(Object parent) { 61 return getModels(); 62 } 63 } 64 65 class TablePart extends WizardCheckboxTablePart { 66 public TablePart(String mainLabel) { 67 super(mainLabel); 68 } 69 70 public void updateCounter(int count) { 71 super.updateCounter(count); 72 dialogChanged(); 73 } 74 protected StructuredViewer createStructuredViewer( 75 Composite parent, 76 int style, 77 FormToolkit toolkit) { 78 StructuredViewer viewer = 79 super.createStructuredViewer(parent, style, toolkit); 80 viewer.setSorter(ListUtil.FEATURE_SORTER); 81 return viewer; 82 } 83 } 84 85 public FeatureImportWizardDetailedPage(FeatureImportWizardFirstPage firstPage) { 86 super("FeatureImportWizardDetailedPage"); setTitle(PDEUIMessages.FeatureImportWizard_DetailedPage_title); setDescription(PDEUIMessages.FeatureImportWizard_DetailedPage_desc); 90 fFirstPage = firstPage; 91 fIsOtherLocation = false; 92 fDropLocation = null; 93 fTablePart = new TablePart(PDEUIMessages.FeatureImportWizard_DetailedPage_featureList); PDEPlugin.getDefault().getLabelProvider().connect(this); 95 } 96 97 private void initializeFields(boolean isOtherLocation, IPath dropLocation) { 98 if (isOtherLocation != fIsOtherLocation 99 || !dropLocation.equals(this.fDropLocation)) { 100 this.fIsOtherLocation = fFirstPage.isOtherLocation(); 101 this.fDropLocation = dropLocation; 102 fModels = null; 103 } 104 if (fModels == null) { 105 getModels(); IRunnableWithProgress op = new IRunnableWithProgress() { 107 public void run(IProgressMonitor monitor) { 108 monitor.beginTask( 109 PDEUIMessages.FeatureImportWizard_messages_updating, IProgressMonitor.UNKNOWN); 111 fFeatureViewer 112 .getControl() 113 .getDisplay() 114 .asyncExec(new Runnable () { 115 public void run() { 116 fFeatureViewer.setInput(PDEPlugin.getDefault()); 117 if (getModels() != null) 118 fFeatureViewer.setCheckedElements(getModels()); 119 fTablePart.updateCounter(getModels().length); 120 } 121 }); 122 monitor.done(); 123 } 124 }; 125 try { 126 getContainer().run(true, false, op); 127 } catch (InterruptedException e) { 128 } catch (InvocationTargetException e) { 129 PDEPlugin.logException(e); 130 } finally { 131 dialogChanged(); 132 } 133 } 135 } 136 137 140 public void setVisible(boolean visible) { 141 super.setVisible(visible); 142 if (visible) { 143 initializeFields(fFirstPage.isOtherLocation(), fFirstPage.getDropLocation()); 144 } 145 } 146 147 150 public void createControl(Composite parent) { 151 initializeDialogUnits(parent); 152 153 Composite container = new Composite(parent, SWT.NONE); 154 GridLayout layout = new GridLayout(); 155 layout.numColumns = 2; 156 layout.marginHeight = 0; 157 layout.marginWidth = 5; 158 container.setLayout(layout); 159 160 fTablePart.createControl(container); 161 fFeatureViewer = fTablePart.getTableViewer(); 162 fFeatureViewer.setContentProvider(new ContentProvider()); 163 fFeatureViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 164 GridData gd = (GridData) fTablePart.getControl().getLayoutData(); 165 gd.heightHint = 300; 166 gd.widthHint = 300; 167 setControl(container); 168 dialogChanged(); 169 Dialog.applyDialogFont(container); 170 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.FEATURE_IMPORT_SECOND_PAGE); 171 } 172 173 public void dispose() { 174 super.dispose(); 175 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 176 } 177 178 public IFeatureModel[] getModels() { 179 if (fModels != null) 180 return fModels; 181 182 final ArrayList result = new ArrayList (); 183 final IPath home = fDropLocation; 184 if (home != null) { 185 IRunnableWithProgress op = new IRunnableWithProgress() { 186 public void run(IProgressMonitor monitor) 187 throws InvocationTargetException { 188 monitor.beginTask( 189 PDEUIMessages.FeatureImportWizard_messages_loadingFile, IProgressMonitor.UNKNOWN); 191 192 try { 193 if (!fIsOtherLocation) { 194 IFeatureModel[] allModels = PDECore.getDefault() 195 .getFeatureModelManager().getModels(); 196 for (int i = 0; i < allModels.length; i++) { 197 if (allModels[i].getUnderlyingResource() == null) { 198 result.add(allModels[i]); 199 } 200 } 201 } else { 202 MultiStatus errors = doLoadFeatures(result, 203 createPath(home), monitor); 204 if (errors != null 205 && errors.getChildren().length > 0) { 206 PDEPlugin.log(errors); 207 } 208 } 209 fModels = 210 (IFeatureModel[]) result.toArray( 211 new IFeatureModel[result.size()]); 212 213 } catch (CoreException e) { 214 throw new InvocationTargetException (e); 215 } finally { 216 monitor.done(); 217 } 218 } 219 }; 220 try { 221 getContainer().run(true, false, op); 222 } catch (InterruptedException e) { 223 return null; 224 } catch (InvocationTargetException e) { 225 PDEPlugin.logException(e); 226 } 227 } 228 return fModels; 229 } 230 231 private File createPath(IPath dropLocation) { 232 File featuresDir = new File (dropLocation.toFile(), "features"); if (featuresDir.exists()) 234 return featuresDir; 235 return null; 236 } 237 238 private MultiStatus doLoadFeatures( 239 ArrayList result, 240 File path, 241 IProgressMonitor monitor) 242 throws CoreException { 243 if (path == null) 244 return null; 245 File [] dirs = path.listFiles(); 246 if (dirs == null) 247 return null; 248 monitor.beginTask(PDEUIMessages.FeatureImportWizard_DetailedPage_loading, dirs.length); ArrayList resultStatus = new ArrayList (); 250 for (int i = 0; i < dirs.length; i++) { 251 File dir = dirs[i]; 252 if (dir.isDirectory()) { 253 File manifest = new File (dir, "feature.xml"); if (manifest.exists()) { 255 IStatus status = doLoadFeature(dir, manifest, result); 256 if (status != null) 257 resultStatus.add(status); 258 } 259 monitor.worked(1); 260 } 261 } 262 if (resultStatus != null) { 263 IStatus[] children = 264 (IStatus[]) resultStatus.toArray(new IStatus[resultStatus.size()]); 265 MultiStatus multiStatus = 266 new MultiStatus( 267 PDEPlugin.PLUGIN_ID, 268 IStatus.OK, 269 children, 270 PDEUIMessages.FeatureImportWizard_DetailedPage_problemsLoading, null); 272 return multiStatus; 273 } 274 return null; 275 } 276 277 private IStatus doLoadFeature(File dir, File manifest, ArrayList result) { 278 ExternalFeatureModel model = new ExternalFeatureModel(); 279 model.setInstallLocation(dir.getAbsolutePath()); 280 IStatus status = null; 281 282 InputStream stream = null; 283 284 try { 285 stream = new FileInputStream (manifest); 286 model.load(stream, false); 287 if(!model.isValid()){ 288 status = 289 new Status( 290 IStatus.WARNING, 291 PDEPlugin.PLUGIN_ID, 292 IStatus.OK, 293 "Import location "+dir+" contains invalid feature.", null); 295 } 296 } catch (Exception e) { 297 status = 299 new Status( 300 IStatus.ERROR, 301 PDEPlugin.PLUGIN_ID, 302 IStatus.OK, 303 e.getMessage(), 304 e); 305 } 306 if (stream != null) { 307 try { 308 stream.close(); 309 } catch (IOException e) { 310 } 311 } 312 if (status == null) 313 result.add(model); 314 return status; 315 } 316 317 public IFeatureModel[] getSelectedModels() { 318 Object [] selected = fFeatureViewer.getCheckedElements(); 319 IFeatureModel[] result = new IFeatureModel[selected.length]; 320 System.arraycopy(selected, 0, result, 0, selected.length); 321 return result; 322 } 323 324 private void dialogChanged() { 325 String message = null; 326 if (fFeatureViewer != null && fFeatureViewer.getTable().getItemCount() == 0) { 327 message = PDEUIMessages.FeatureImportWizard_messages_noFeatures; } 329 setMessage(message, WizardPage.INFORMATION); 330 setPageComplete(fTablePart.getSelectionCount() > 0); 331 } 332 333 336 public boolean isPageComplete() { 337 return fTablePart.getSelectionCount() > 0; 338 } 339 340 } 341 | Popular Tags |