1 11 package org.eclipse.pde.ui.internal.samples; 12 import java.io.*; 13 import java.util.Properties ; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.debug.core.ILaunchManager; 18 import org.eclipse.debug.ui.ILaunchShortcut; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.pde.internal.ui.*; 22 import org.eclipse.pde.internal.ui.launcher.RuntimeWorkbenchShortcut; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.custom.BusyIndicator; 25 import org.eclipse.swt.widgets.*; 26 import org.eclipse.ui.*; 27 import org.eclipse.ui.forms.events.*; 28 import org.eclipse.ui.forms.widgets.*; 29 import org.eclipse.ui.part.EditorPart; 30 33 public class SampleEditor extends EditorPart { 34 private FormToolkit toolkit; 35 private ScrolledForm form; 36 private FormText descText; 37 private FormText instText; 38 private ILaunchShortcut defaultShortcut; 39 private InputFileListener inputFileListener; 40 41 class InputFileListener implements IResourceChangeListener, IResourceDeltaVisitor { 42 public void resourceChanged(IResourceChangeEvent event) { 43 if (event.getType()==IResourceChangeEvent.POST_CHANGE) { 44 IResourceDelta delta = event.getDelta(); 45 try { 46 delta.accept(this); 47 } 48 catch (CoreException e) { 49 PDEPlugin.logException(e); 50 } 51 } 52 } 53 public boolean visit(IResourceDelta delta) throws CoreException { 54 IResource resource = delta.getResource(); 55 if (resource instanceof IFile) { 56 IFile file = (IFile)resource; 57 if (file.equals(((IFileEditorInput)getEditorInput()).getFile())) { 58 if (delta.getKind()==IResourceDelta.REMOVED || 59 delta.getKind()==IResourceDelta.REPLACED) 60 close(); 61 return false; 62 } 63 } 64 return true; 65 } 66 } 67 70 public SampleEditor() { 71 defaultShortcut = new RuntimeWorkbenchShortcut(); 72 PDEPlugin.getDefault().getLabelProvider().connect(this); 73 } 74 77 public void createPartControl(Composite parent) { 78 toolkit = new FormToolkit(parent.getDisplay()); 79 form = toolkit.createScrolledForm(parent); 80 Properties properties = loadContent(); 81 form.setText(properties.getProperty("name")); TableWrapLayout layout = new TableWrapLayout(); 83 layout.verticalSpacing = 10; 84 layout.topMargin = 10; 85 layout.bottomMargin = 10; 86 layout.leftMargin = 10; 87 layout.rightMargin = 10; 88 form.getBody().setLayout(layout); 89 90 final String launcher = properties.getProperty("launcher"); final String launchTarget = properties.getProperty("launchTarget"); 93 descText = toolkit.createFormText(form.getBody(), true); 94 descText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 95 String desc = properties.getProperty("description"); String content = NLS.bind(PDEUIMessages.SampleEditor_desc, (desc!=null?desc:"")); descText.setText(content, true, false); 98 final String helpURL = properties.getProperty("helpHref"); if (helpURL!=null) { 100 Hyperlink moreLink = toolkit.createHyperlink(form.getBody(), "Read More", SWT.NULL); moreLink.addHyperlinkListener(new HyperlinkAdapter() { 102 public void linkActivated(HyperlinkEvent e) { 103 PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL); 104 } 105 }); 106 } 107 instText = toolkit.createFormText(form.getBody(), true); 108 instText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 109 StringBuffer buf = new StringBuffer (); 110 buf.append(PDEUIMessages.SampleEditor_content); instText.setText(buf.toString(), true, false); 112 instText.addHyperlinkListener(new HyperlinkAdapter() { 113 public void linkActivated(HyperlinkEvent e) { 114 Object href = e.getHref(); 115 if (href.equals("help")) { PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL); 117 } 118 else if (href.equals("run")) { doRun(launcher, launchTarget, false); 120 } 121 else if (href.equals("debug")) { doRun(launcher, launchTarget, true); 123 } 124 } 125 }); 126 instText.setImage("run", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_RUN_EXC)); instText.setImage("debug", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_DEBUG_EXC)); instText.setImage("help", PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_INFO_TSK)); } 130 131 private void doRun(String launcher, String target, final boolean debug) { 132 ILaunchShortcut shortcut = defaultShortcut; 133 final ISelection selection; 134 if (target!=null) { 135 selection = new StructuredSelection(); 136 } 137 else 138 selection = new StructuredSelection(); 139 final ILaunchShortcut fshortcut = shortcut; 140 BusyIndicator.showWhile(form.getDisplay(), new Runnable () { 141 public void run() { 142 fshortcut.launch(selection, debug?ILaunchManager.DEBUG_MODE:ILaunchManager.RUN_MODE); 143 } 144 }); 145 } 146 147 private Properties loadContent() { 148 IStorageEditorInput input = (IStorageEditorInput)getEditorInput(); 149 Properties properties = new Properties (); 150 try { 151 IStorage storage = input.getStorage(); 152 InputStream is = storage.getContents(); 153 properties.load(is); 154 is.close(); 155 } 156 catch (IOException e) { 157 PDEPlugin.logException(e); 158 } 159 catch (CoreException e) { 160 PDEPlugin.logException(e); 161 } 162 return properties; 163 } 164 165 public void dispose() { 166 if (inputFileListener!=null) { 167 PDEPlugin.getWorkspace().removeResourceChangeListener(inputFileListener); 168 inputFileListener = null; 169 } 170 toolkit.dispose(); 171 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 172 super.dispose(); 173 } 174 177 public void setFocus() { 178 form.setFocus(); 179 } 180 183 public void doSave(IProgressMonitor monitor) { 184 } 185 188 public void doSaveAs() { 189 } 190 193 public boolean isDirty() { 194 return false; 195 } 196 199 public boolean isSaveAsAllowed() { 200 return false; 201 } 202 205 public void init(IEditorSite site, IEditorInput input) 206 throws PartInitException { 207 setSite(site); 208 setInput(input); 209 inputFileListener = new InputFileListener(); 210 PDEPlugin.getWorkspace().addResourceChangeListener(inputFileListener); 211 } 212 public void close() { 213 Display display = getSite().getShell().getDisplay(); 214 display.asyncExec(new Runnable () { 215 public void run() { 216 if (toolkit != null) { 217 getSite().getPage().closeEditor(SampleEditor.this, false); 218 } 219 } 220 }); 221 } 222 } 223 | Popular Tags |