1 11 package org.eclipse.pde.ui.internal.samples; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.util.ArrayList ; 15 import java.util.Properties ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IWorkspaceRoot; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.debug.core.ILaunchManager; 25 import org.eclipse.debug.ui.ILaunchShortcut; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.StructuredSelection; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.pde.internal.ui.PDEPlugin; 30 import org.eclipse.pde.internal.ui.PDEPluginImages; 31 import org.eclipse.pde.internal.ui.PDEUIMessages; 32 import org.eclipse.pde.internal.ui.launcher.RuntimeWorkbenchShortcut; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.custom.BusyIndicator; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.ui.IMemento; 38 import org.eclipse.ui.ISharedImages; 39 import org.eclipse.ui.IViewPart; 40 import org.eclipse.ui.PlatformUI; 41 import org.eclipse.ui.forms.events.HyperlinkAdapter; 42 import org.eclipse.ui.forms.events.HyperlinkEvent; 43 import org.eclipse.ui.forms.widgets.FormText; 44 import org.eclipse.ui.forms.widgets.FormToolkit; 45 import org.eclipse.ui.forms.widgets.Hyperlink; 46 import org.eclipse.ui.forms.widgets.ScrolledForm; 47 import org.eclipse.ui.forms.widgets.TableWrapData; 48 import org.eclipse.ui.forms.widgets.TableWrapLayout; 49 import org.eclipse.ui.intro.IIntroPart; 50 import org.eclipse.ui.intro.config.IStandbyContentPart; 51 import org.eclipse.ui.part.ISetSelectionTarget; 52 53 public class SampleStandbyContent implements IStandbyContentPart { 54 private ScrolledForm form; 55 private Hyperlink moreLink; 56 private String helpURL; 57 private String launcher; 58 private String launchTarget; 59 private FormText descText; 60 private FormText instText; 61 private ILaunchShortcut defaultShortcut; 62 private IConfigurationElement sample; 63 private String input; 65 66 private static String MEMENTO_SAMPLE_ID_ATT = "sampleId"; 68 71 public SampleStandbyContent() { 72 defaultShortcut = new RuntimeWorkbenchShortcut(); 73 PDEPlugin.getDefault().getLabelProvider().connect(this); 74 } 75 81 public void createPartControl(Composite parent, FormToolkit toolkit) { 82 form = toolkit.createScrolledForm(parent); 83 TableWrapLayout layout = new TableWrapLayout(); 86 layout.verticalSpacing = 10; 87 layout.topMargin = 10; 88 layout.bottomMargin = 10; 89 layout.leftMargin = 10; 90 layout.rightMargin = 10; 91 form.getBody().setLayout(layout); 92 descText = toolkit.createFormText(form.getBody(), true); 93 descText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 94 descText.setText("", false, false); moreLink = toolkit.createHyperlink(form.getBody(), "Read More", SWT.NULL); 97 moreLink.addHyperlinkListener(new HyperlinkAdapter() { 98 public void linkActivated(HyperlinkEvent e) { 99 if (helpURL != null) 100 PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL); 101 } 102 }); 103 instText = toolkit.createFormText(form.getBody(), true); 104 instText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 105 StringBuffer buf = new StringBuffer (); 106 buf.append(PDEUIMessages.SampleStandbyContent_content); instText.setText(buf.toString(), true, false); 108 instText.addHyperlinkListener(new HyperlinkAdapter() { 109 public void linkActivated(HyperlinkEvent e) { 110 Object href = e.getHref(); 111 if (href.equals("help")) { PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL); 113 } else if (href.equals("browse")) { doBrowse(); 115 } else if (href.equals("run")) { doRun(launcher, launchTarget, false); 117 } else if (href.equals("debug")) { doRun(launcher, launchTarget, true); 119 } 120 } 121 }); 122 instText.setImage("run", PDEPlugin.getDefault().getLabelProvider().get( PDEPluginImages.DESC_RUN_EXC)); 124 instText.setImage("debug", PDEPlugin.getDefault().getLabelProvider() .get(PDEPluginImages.DESC_DEBUG_EXC)); 126 instText.setImage("help", PlatformUI.getWorkbench().getSharedImages() .getImage(ISharedImages.IMG_OBJS_INFO_TSK)); 128 } 129 private void doRun(String launcher, String target, final boolean debug) { 130 ILaunchShortcut shortcut = defaultShortcut; 131 final ISelection selection; 132 if (target != null) { 133 selection = new StructuredSelection(); 134 } else 135 selection = new StructuredSelection(); 136 final ILaunchShortcut fshortcut = shortcut; 137 BusyIndicator.showWhile(form.getDisplay(), new Runnable () { 138 public void run() { 139 fshortcut.launch(selection, debug 140 ? ILaunchManager.DEBUG_MODE 141 : ILaunchManager.RUN_MODE); 142 } 143 }); 144 } 145 private void doBrowse() { 146 IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot(); 147 IProject[] projects = root.getProjects(); 148 ISetSelectionTarget target = findTarget(); 149 if (target == null) 150 return; 151 String sid = sample.getAttribute("id"); if (sid == null) 153 return; 154 ArrayList items = new ArrayList (); 155 for (int i = 0; i < projects.length; i++) { 156 IProject project = projects[i]; 157 if (!project.exists() || !project.isOpen()) 158 continue; 159 IFile pfile = project.getFile("sample.properties"); if (pfile.exists()) { 161 try { 162 InputStream is = pfile.getContents(); 163 Properties prop = new Properties (); 164 prop.load(is); 165 is.close(); 166 String id = prop.getProperty("id"); if (id != null && id.equals(sid)) { 168 IResource res = findSelectReveal(project, prop 170 .getProperty("projectName")); if (res != null) 172 items.add(res); 173 } 174 } catch (IOException e) { 175 PDEPlugin.logException(e); 176 } catch (CoreException e) { 177 PDEPlugin.logException(e); 178 } 179 } 180 } 181 if (items.size() > 0) 182 target.selectReveal(new StructuredSelection(items)); 183 } 184 private ISetSelectionTarget findTarget() { 185 String id = sample.getAttribute("targetViewId"); if (id == null) 187 return null; 188 IViewPart view = PDEPlugin.getActivePage().findView(id); 189 if (view == null || !(view instanceof ISetSelectionTarget)) 190 return null; 191 return (ISetSelectionTarget) view; 192 } 193 private IResource findSelectReveal(IProject project, String originalName) { 194 IConfigurationElement[] projects = sample.getChildren("project"); for (int i = 0; i < projects.length; i++) { 196 if (originalName.equals(projects[i].getAttribute("name"))) { String path = projects[i].getAttribute("selectReveal"); if (path == null) 199 continue; 200 IResource res = project.findMember(path); 201 if (res.exists()) 202 return res; 203 } 204 } 205 return null; 206 } 207 212 public Control getControl() { 213 return form; 214 } 215 220 public void init(IIntroPart introPart) { 221 } 222 227 public void setInput(Object input) { 228 if (input != null) 230 this.input = (String ) input; 231 String sampleId = this.input.toString(); 232 IConfigurationElement[] samples = Platform.getExtensionRegistry() 233 .getConfigurationElementsFor("org.eclipse.pde.ui.samples"); for (int i = 0; i < samples.length; i++) { 235 IConfigurationElement sample = samples[i]; 236 String id = sample.getAttribute("id"); if (id != null && id.equals(sampleId)) { 238 update(sample); 239 return; 240 } 241 } 242 update(null); 243 } 244 private void update(IConfigurationElement sample) { 245 this.sample = sample; 246 if (form == null) 247 return; 248 String title = sample != null ? sample.getAttribute("name") : ""; form.setText(title); 250 if (sample != null) { 251 launcher = sample.getAttribute("launcher"); launchTarget = sample.getAttribute("launchTarget"); } else { 254 launcher = null; 255 launchTarget = null; 256 } 257 IConfigurationElement[] descConfig = sample != null ? sample 258 .getChildren("description") : null; if (descConfig != null && descConfig.length == 1) { 260 String desc = descConfig[0].getValue(); 261 String content = NLS.bind(PDEUIMessages.SampleStandbyContent_desc, (desc != null ? desc : "")); helpURL = descConfig[0].getAttribute("helpHref"); moreLink.setVisible(helpURL != null); 264 descText.setText(content, true, false); 265 } else { 266 moreLink.setVisible(false); 267 descText.setText("", false, false); } 269 form.reflow(true); 270 } 275 public void setFocus() { 276 form.setFocus(); 277 } 278 283 public void dispose() { 284 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 285 } 286 289 public void init(IIntroPart introPart, IMemento memento) { 290 input = getCachedInput(memento); 292 293 } 294 295 301 private String getCachedInput(IMemento memento) { 302 if (memento == null) 303 return null; 304 return memento.getString(MEMENTO_SAMPLE_ID_ATT); 305 306 } 307 308 311 public void saveState(IMemento memento) { 312 String currentSampleId = input; 313 if (input != null) 314 memento.putString(MEMENTO_SAMPLE_ID_ATT, currentSampleId); 315 316 } 317 } 318 | Popular Tags |