Advertisement
Guest User

Untitled

a guest
May 30th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. public class SampleStandbyContent implements IStandbyContentPart {
  2. private ScrolledForm form;
  3. private Hyperlink moreLink;
  4. private String helpURL;
  5. private String launcher;
  6. private String launchTarget;
  7. private FormText descText;
  8. private FormText instText;
  9. private ILaunchShortcut defaultShortcut;
  10. private IConfigurationElement sample;
  11. // cached input.
  12. private String input;
  13.  
  14. private static String MEMENTO_SAMPLE_ID_ATT = "sampleId"; //$NON-NLS-1$
  15.  
  16. /**
  17. *
  18. */
  19. public SampleStandbyContent() {
  20. defaultShortcut = new EclipseLaunchShortcut();
  21. PDEPlugin.getDefault().getLabelProvider().connect(this);
  22. }
  23.  
  24. /*
  25. * (non-Javadoc)
  26. *
  27. * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#createPartControl(org.eclipse.swt.widgets.Composite,
  28. * org.eclipse.ui.forms.widgets.FormToolkit)
  29. */
  30. public void createPartControl(Composite parent, FormToolkit toolkit) {
  31. form = toolkit.createScrolledForm(parent);
  32. //form.setBackgroundImage(PDEPlugin.getDefault().getLabelProvider().get(
  33. // PDEPluginImages.DESC_FORM_BANNER));
  34. TableWrapLayout layout = new TableWrapLayout();
  35. layout.verticalSpacing = 10;
  36. layout.topMargin = 10;
  37. layout.bottomMargin = 10;
  38. layout.leftMargin = 10;
  39. layout.rightMargin = 10;
  40. form.getBody().setLayout(layout);
  41. descText = toolkit.createFormText(form.getBody(), true);
  42. descText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
  43. descText.setText("", false, false); //$NON-NLS-1$
  44. moreLink = toolkit.createHyperlink(form.getBody(), "Read More", //$NON-NLS-1$
  45. SWT.NULL);
  46. moreLink.addHyperlinkListener(new HyperlinkAdapter() {
  47. public void linkActivated(HyperlinkEvent e) {
  48. if (helpURL != null)
  49. PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL);
  50. }
  51. });
  52. instText = toolkit.createFormText(form.getBody(), true);
  53. instText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
  54. StringBuffer buf = new StringBuffer();
  55. buf.append(PDEUIMessages.SampleStandbyContent_content);
  56. instText.setText(buf.toString(), true, false);
  57. instText.addHyperlinkListener(new HyperlinkAdapter() {
  58. public void linkActivated(HyperlinkEvent e) {
  59. Object href = e.getHref();
  60. if (href.equals("help")) { //$NON-NLS-1$
  61. PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL);
  62. } else if (href.equals("browse")) { //$NON-NLS-1$
  63. doBrowse();
  64. } else if (href.equals("run")) { //$NON-NLS-1$
  65. doRun(launcher, launchTarget, false);
  66. } else if (href.equals("debug")) { //$NON-NLS-1$
  67. doRun(launcher, launchTarget, true);
  68. }
  69. }
  70. });
  71. instText.setImage("run", PDEPlugin.getDefault().getLabelProvider().get( //$NON-NLS-1$
  72. PDEPluginImages.DESC_RUN_EXC));
  73. instText.setImage("debug", PDEPlugin.getDefault().getLabelProvider() //$NON-NLS-1$
  74. .get(PDEPluginImages.DESC_DEBUG_EXC));
  75. instText.setImage("help", PlatformUI.getWorkbench().getSharedImages() //$NON-NLS-1$
  76. .getImage(ISharedImages.IMG_OBJS_INFO_TSK));
  77. }
  78.  
  79. private void doRun(String launcher, String target, final boolean debug) {
  80. ILaunchShortcut shortcut = defaultShortcut;
  81. final ISelection selection;
  82. if (target != null) {
  83. selection = new StructuredSelection();
  84. } else
  85. selection = new StructuredSelection();
  86. final ILaunchShortcut fshortcut = shortcut;
  87. BusyIndicator.showWhile(form.getDisplay(), new Runnable() {
  88. public void run() {
  89. fshortcut.launch(selection, debug ? ILaunchManager.DEBUG_MODE : ILaunchManager.RUN_MODE);
  90. }
  91. });
  92. }
  93.  
  94. private void doBrowse() {
  95. IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
  96. IProject[] projects = root.getProjects();
  97. ISetSelectionTarget target = findTarget();
  98. if (target == null)
  99. return;
  100. String sid = sample.getAttribute("id"); //$NON-NLS-1$
  101. if (sid == null)
  102. return;
  103. ArrayList items = new ArrayList();
  104. for (int i = 0; i < projects.length; i++) {
  105. IProject project = projects[i];
  106. if (!project.exists() || !project.isOpen())
  107. continue;
  108. IFile pfile = project.getFile("sample.properties"); //$NON-NLS-1$
  109. if (pfile.exists()) {
  110. try {
  111. InputStream is = pfile.getContents();
  112. Properties prop = new Properties();
  113. prop.load(is);
  114. is.close();
  115. String id = prop.getProperty("id"); //$NON-NLS-1$
  116. if (id != null && id.equals(sid)) {
  117. //match
  118. IResource res = findSelectReveal(project, prop.getProperty("projectName")); //$NON-NLS-1$
  119. if (res != null)
  120. items.add(res);
  121. }
  122. } catch (IOException e) {
  123. PDEPlugin.logException(e);
  124. } catch (CoreException e) {
  125. PDEPlugin.logException(e);
  126. }
  127. }
  128. }
  129. if (items.size() > 0)
  130. target.selectReveal(new StructuredSelection(items));
  131. }
  132.  
  133. private ISetSelectionTarget findTarget() {
  134. String id = sample.getAttribute("targetViewId"); //$NON-NLS-1$
  135. if (id == null)
  136. return null;
  137. IViewPart view = PDEPlugin.getActivePage().findView(id);
  138. if (view == null || !(view instanceof ISetSelectionTarget))
  139. return null;
  140. return (ISetSelectionTarget) view;
  141. }
  142.  
  143. private IResource findSelectReveal(IProject project, String originalName) {
  144. IConfigurationElement[] projects = sample.getChildren("project"); //$NON-NLS-1$
  145. for (int i = 0; i < projects.length; i++) {
  146. if (originalName.equals(projects[i].getAttribute("name"))) { //$NON-NLS-1$
  147. String path = projects[i].getAttribute("selectReveal"); //$NON-NLS-1$
  148. if (path == null)
  149. continue;
  150. IResource res = project.findMember(path);
  151. if (res.exists())
  152. return res;
  153. }
  154. }
  155. return null;
  156. }
  157.  
  158. /*
  159. * (non-Javadoc)
  160. *
  161. * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#getControl()
  162. */
  163. public Control getControl() {
  164. return form;
  165. }
  166.  
  167. /*
  168. * (non-Javadoc)
  169. *
  170. * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart)
  171. */
  172. public void init(IIntroPart introPart) {
  173. }
  174.  
  175. /*
  176. * (non-Javadoc)
  177. *
  178. * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#setInput(java.lang.Object)
  179. */
  180. public void setInput(Object input) {
  181. // if the new input is null, use cached input from momento.
  182. if (input != null)
  183. this.input = (String) input;
  184. String sampleId = this.input.toString();
  185. IConfigurationElement[] samples = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.pde.ui.samples"); //$NON-NLS-1$
  186. for (int i = 0; i < samples.length; i++) {
  187. IConfigurationElement sample = samples[i];
  188. String id = sample.getAttribute("id"); //$NON-NLS-1$
  189. if (id != null && id.equals(sampleId)) {
  190. update(sample);
  191. return;
  192. }
  193. }
  194. update(null);
  195. }
  196.  
  197. private void update(IConfigurationElement sample) {
  198. this.sample = sample;
  199. if (form == null)
  200. return;
  201. String title = sample != null ? sample.getAttribute("name") : ""; //$NON-NLS-1$ //$NON-NLS-2$
  202. form.setText(title);
  203. if (sample != null) {
  204. launcher = sample.getAttribute("launcher"); //$NON-NLS-1$
  205. launchTarget = sample.getAttribute("launchTarget"); //$NON-NLS-1$
  206. } else {
  207. launcher = null;
  208. launchTarget = null;
  209. }
  210. IConfigurationElement[] descConfig = sample != null ? sample.getChildren("description") : null; //$NON-NLS-1$
  211. if (descConfig != null && descConfig.length == 1) {
  212. String desc = descConfig[0].getValue();
  213. String content = NLS.bind(PDEUIMessages.SampleStandbyContent_desc, (desc != null ? desc : "")); //$NON-NLS-1$
  214. helpURL = descConfig[0].getAttribute("helpHref"); //$NON-NLS-1$
  215. moreLink.setVisible(helpURL != null);
  216. descText.setText(content, true, false);
  217. } else {
  218. moreLink.setVisible(false);
  219. descText.setText("", false, false); //$NON-NLS-1$
  220. }
  221. form.reflow(true);
  222. } /*
  223. * (non-Javadoc)
  224. *
  225. * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#setFocus()
  226. */
  227.  
  228. public void setFocus() {
  229. form.setFocus();
  230. }
  231.  
  232. /*
  233. * (non-Javadoc)
  234. *
  235. * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#dispose()
  236. */
  237. public void dispose() {
  238. PDEPlugin.getDefault().getLabelProvider().disconnect(this);
  239. }
  240.  
  241. /* (non-Javadoc)
  242. * @see org.eclipse.ui.intro.config.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart, org.eclipse.ui.IMemento)
  243. */
  244. public void init(IIntroPart introPart, IMemento memento) {
  245. // try to restore last state.
  246. input = getCachedInput(memento);
  247.  
  248. }
  249.  
  250. /**
  251. * Tries to create the last content part viewed, based on sample id.
  252. *
  253. * @param memento
  254. * @return
  255. */
  256. private String getCachedInput(IMemento memento) {
  257. if (memento == null)
  258. return null;
  259. return memento.getString(MEMENTO_SAMPLE_ID_ATT);
  260.  
  261. }
  262.  
  263. /* (non-Javadoc)
  264. * @see org.eclipse.ui.intro.config.IStandbyContentPart#saveState(org.eclipse.ui.IMemento)
  265. */
  266. public void saveState(IMemento memento) {
  267. String currentSampleId = input;
  268. if (input != null)
  269. memento.putString(MEMENTO_SAMPLE_ID_ATT, currentSampleId);
  270.  
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement