1 11 package org.eclipse.pde.internal.ui.editor.target; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.IFile; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 import org.eclipse.jface.action.ControlContribution; 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.jface.operation.IRunnableWithProgress; 23 import org.eclipse.pde.internal.core.LoadTargetOperation; 24 import org.eclipse.pde.internal.core.itarget.ITarget; 25 import org.eclipse.pde.internal.core.itarget.ITargetModel; 26 import org.eclipse.pde.internal.ui.PDEPlugin; 27 import org.eclipse.pde.internal.ui.PDEUIMessages; 28 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.ui.IEditorInput; 33 import org.eclipse.ui.IFileEditorInput; 34 import org.eclipse.ui.PlatformUI; 35 import org.eclipse.ui.forms.IManagedForm; 36 import org.eclipse.ui.forms.editor.FormEditor; 37 import org.eclipse.ui.forms.events.HyperlinkEvent; 38 import org.eclipse.ui.forms.events.IHyperlinkListener; 39 import org.eclipse.ui.forms.widgets.ImageHyperlink; 40 import org.eclipse.ui.forms.widgets.ScrolledForm; 41 import org.eclipse.ui.progress.IProgressService; 42 43 public abstract class AbstractTargetPage extends PDEFormPage { 44 45 public AbstractTargetPage(FormEditor editor, String id, String title) { 46 super(editor, id, title); 47 } 48 49 protected void createFormContent(final IManagedForm managedForm) { 50 ScrolledForm form = managedForm.getForm(); 51 ControlContribution save = new ControlContribution("Set") { protected Control createControl(Composite parent) { 53 final ImageHyperlink hyperlink = new ImageHyperlink(parent, SWT.NONE); 54 hyperlink.setText(PDEUIMessages.AbstractTargetPage_setTarget); 55 hyperlink.setUnderlined(true); 56 hyperlink.setForeground(managedForm.getToolkit().getHyperlinkGroup().getForeground()); 57 hyperlink.addHyperlinkListener(new IHyperlinkListener() { 58 public void linkActivated(HyperlinkEvent e) { 59 doLoadTarget(); 60 } 61 62 public void linkEntered(HyperlinkEvent e) { 63 hyperlink.setForeground(managedForm.getToolkit().getHyperlinkGroup().getActiveForeground()); 64 } 65 66 public void linkExited(HyperlinkEvent e) { 67 hyperlink.setForeground(managedForm.getToolkit().getHyperlinkGroup().getForeground()); 68 } 69 }); 70 return hyperlink; 71 } 72 }; 73 form.getToolBarManager().add(save); 74 form.getToolBarManager().update(true); 75 super.createFormContent(managedForm); 76 } 77 78 private void doLoadTarget() { 79 IRunnableWithProgress run = new IRunnableWithProgress() { 80 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 81 try { 82 ITargetModel model = getTargetModel(); 83 if (!model.isLoaded()) { 84 MessageDialog.openError(getManagedForm().getForm().getShell(), PDEUIMessages.TargetPlatformPreferencePage_invalidTitle, PDEUIMessages.TargetPlatformPreferencePage_invalidDescription); 85 monitor.done(); 86 return; 87 } 88 LoadTargetOperation op = new LoadTargetOperation(getTarget(), getFilePath()); 89 PDEPlugin.getWorkspace().run(op, monitor); 90 Object [] features = op.getMissingFeatures(); 91 Object [] plugins = op.getMissingPlugins(); 92 if (plugins.length + features.length > 0) 93 TargetErrorDialog.showDialog(getManagedForm().getForm().getShell(), features, plugins); 94 } catch (CoreException e) { 95 throw new InvocationTargetException (e); 96 } catch (OperationCanceledException e) { 97 throw new InterruptedException (e.getMessage()); 98 } finally { 99 monitor.done(); 100 } 101 } 102 }; 103 IProgressService service = PlatformUI.getWorkbench().getProgressService(); 104 try { 105 service.runInUI(service, run, PDEPlugin.getWorkspace().getRoot()); 106 } catch (InvocationTargetException e) { 107 } catch (InterruptedException e) { 108 } 109 } 110 111 private ITarget getTarget() { 112 return getTargetModel().getTarget(); 113 } 114 115 private ITargetModel getTargetModel() { 116 return ((ITargetModel) getPDEEditor().getAggregateModel()); 117 } 118 119 private IPath getFilePath() { 120 IEditorInput input = getEditorInput(); 121 if (input instanceof IFileEditorInput) { 122 IFile file = ((IFileEditorInput)input).getFile(); 123 if (file != null) 124 return file.getFullPath(); 125 } 126 return null; 127 } 128 129 130 } 131 | Popular Tags |