001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.samples;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.ArrayList;
015: import java.util.Properties;
016:
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.runtime.CoreException;
022: import org.eclipse.core.runtime.IConfigurationElement;
023: import org.eclipse.core.runtime.Platform;
024: import org.eclipse.debug.core.ILaunchManager;
025: import org.eclipse.debug.ui.ILaunchShortcut;
026: import org.eclipse.jface.viewers.ISelection;
027: import org.eclipse.jface.viewers.StructuredSelection;
028: import org.eclipse.osgi.util.NLS;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.PDEPluginImages;
031: import org.eclipse.pde.internal.ui.PDEUIMessages;
032: import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
033: import org.eclipse.swt.SWT;
034: import org.eclipse.swt.custom.BusyIndicator;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Control;
037: import org.eclipse.ui.IMemento;
038: import org.eclipse.ui.ISharedImages;
039: import org.eclipse.ui.IViewPart;
040: import org.eclipse.ui.PlatformUI;
041: import org.eclipse.ui.forms.events.HyperlinkAdapter;
042: import org.eclipse.ui.forms.events.HyperlinkEvent;
043: import org.eclipse.ui.forms.widgets.FormText;
044: import org.eclipse.ui.forms.widgets.FormToolkit;
045: import org.eclipse.ui.forms.widgets.Hyperlink;
046: import org.eclipse.ui.forms.widgets.ScrolledForm;
047: import org.eclipse.ui.forms.widgets.TableWrapData;
048: import org.eclipse.ui.forms.widgets.TableWrapLayout;
049: import org.eclipse.ui.intro.IIntroPart;
050: import org.eclipse.ui.intro.config.IStandbyContentPart;
051: import org.eclipse.ui.part.ISetSelectionTarget;
052:
053: public class SampleStandbyContent implements IStandbyContentPart {
054: private ScrolledForm form;
055: private Hyperlink moreLink;
056: private String helpURL;
057: private String launcher;
058: private String launchTarget;
059: private FormText descText;
060: private FormText instText;
061: private ILaunchShortcut defaultShortcut;
062: private IConfigurationElement sample;
063: // cached input.
064: private String input;
065:
066: private static String MEMENTO_SAMPLE_ID_ATT = "sampleId"; //$NON-NLS-1$
067:
068: /**
069: *
070: */
071: public SampleStandbyContent() {
072: defaultShortcut = new EclipseLaunchShortcut();
073: PDEPlugin.getDefault().getLabelProvider().connect(this );
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#createPartControl(org.eclipse.swt.widgets.Composite,
080: * org.eclipse.ui.forms.widgets.FormToolkit)
081: */
082: public void createPartControl(Composite parent, FormToolkit toolkit) {
083: form = toolkit.createScrolledForm(parent);
084: //form.setBackgroundImage(PDEPlugin.getDefault().getLabelProvider().get(
085: // PDEPluginImages.DESC_FORM_BANNER));
086: TableWrapLayout layout = new TableWrapLayout();
087: layout.verticalSpacing = 10;
088: layout.topMargin = 10;
089: layout.bottomMargin = 10;
090: layout.leftMargin = 10;
091: layout.rightMargin = 10;
092: form.getBody().setLayout(layout);
093: descText = toolkit.createFormText(form.getBody(), true);
094: descText.setLayoutData(new TableWrapData(
095: TableWrapData.FILL_GRAB));
096: descText.setText("", false, false); //$NON-NLS-1$
097: moreLink = toolkit.createHyperlink(form.getBody(), "Read More", //$NON-NLS-1$
098: SWT.NULL);
099: moreLink.addHyperlinkListener(new HyperlinkAdapter() {
100: public void linkActivated(HyperlinkEvent e) {
101: if (helpURL != null)
102: PlatformUI.getWorkbench().getHelpSystem()
103: .displayHelpResource(helpURL);
104: }
105: });
106: instText = toolkit.createFormText(form.getBody(), true);
107: instText.setLayoutData(new TableWrapData(
108: TableWrapData.FILL_GRAB));
109: StringBuffer buf = new StringBuffer();
110: buf.append(PDEUIMessages.SampleStandbyContent_content);
111: 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")) { //$NON-NLS-1$
116: PlatformUI.getWorkbench().getHelpSystem()
117: .displayHelpResource(helpURL);
118: } else if (href.equals("browse")) { //$NON-NLS-1$
119: doBrowse();
120: } else if (href.equals("run")) { //$NON-NLS-1$
121: doRun(launcher, launchTarget, false);
122: } else if (href.equals("debug")) { //$NON-NLS-1$
123: doRun(launcher, launchTarget, true);
124: }
125: }
126: });
127: instText.setImage(
128: "run", PDEPlugin.getDefault().getLabelProvider().get( //$NON-NLS-1$
129: PDEPluginImages.DESC_RUN_EXC));
130: instText.setImage(
131: "debug", PDEPlugin.getDefault().getLabelProvider() //$NON-NLS-1$
132: .get(PDEPluginImages.DESC_DEBUG_EXC));
133: instText.setImage(
134: "help", PlatformUI.getWorkbench().getSharedImages() //$NON-NLS-1$
135: .getImage(ISharedImages.IMG_OBJS_INFO_TSK));
136: }
137:
138: private void doRun(String launcher, String target,
139: final boolean debug) {
140: ILaunchShortcut shortcut = defaultShortcut;
141: final ISelection selection;
142: if (target != null) {
143: selection = new StructuredSelection();
144: } else
145: selection = new StructuredSelection();
146: final ILaunchShortcut fshortcut = shortcut;
147: BusyIndicator.showWhile(form.getDisplay(), new Runnable() {
148: public void run() {
149: fshortcut.launch(selection,
150: debug ? ILaunchManager.DEBUG_MODE
151: : ILaunchManager.RUN_MODE);
152: }
153: });
154: }
155:
156: private void doBrowse() {
157: IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
158: IProject[] projects = root.getProjects();
159: ISetSelectionTarget target = findTarget();
160: if (target == null)
161: return;
162: String sid = sample.getAttribute("id"); //$NON-NLS-1$
163: if (sid == null)
164: return;
165: ArrayList items = new ArrayList();
166: for (int i = 0; i < projects.length; i++) {
167: IProject project = projects[i];
168: if (!project.exists() || !project.isOpen())
169: continue;
170: IFile pfile = project.getFile("sample.properties"); //$NON-NLS-1$
171: if (pfile.exists()) {
172: try {
173: InputStream is = pfile.getContents();
174: Properties prop = new Properties();
175: prop.load(is);
176: is.close();
177: String id = prop.getProperty("id"); //$NON-NLS-1$
178: if (id != null && id.equals(sid)) {
179: //match
180: IResource res = findSelectReveal(project, prop
181: .getProperty("projectName")); //$NON-NLS-1$
182: if (res != null)
183: items.add(res);
184: }
185: } catch (IOException e) {
186: PDEPlugin.logException(e);
187: } catch (CoreException e) {
188: PDEPlugin.logException(e);
189: }
190: }
191: }
192: if (items.size() > 0)
193: target.selectReveal(new StructuredSelection(items));
194: }
195:
196: private ISetSelectionTarget findTarget() {
197: String id = sample.getAttribute("targetViewId"); //$NON-NLS-1$
198: if (id == null)
199: return null;
200: IViewPart view = PDEPlugin.getActivePage().findView(id);
201: if (view == null || !(view instanceof ISetSelectionTarget))
202: return null;
203: return (ISetSelectionTarget) view;
204: }
205:
206: private IResource findSelectReveal(IProject project,
207: String originalName) {
208: IConfigurationElement[] projects = sample
209: .getChildren("project"); //$NON-NLS-1$
210: for (int i = 0; i < projects.length; i++) {
211: if (originalName.equals(projects[i].getAttribute("name"))) { //$NON-NLS-1$
212: String path = projects[i].getAttribute("selectReveal"); //$NON-NLS-1$
213: if (path == null)
214: continue;
215: IResource res = project.findMember(path);
216: if (res.exists())
217: return res;
218: }
219: }
220: return null;
221: }
222:
223: /*
224: * (non-Javadoc)
225: *
226: * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#getControl()
227: */
228: public Control getControl() {
229: return form;
230: }
231:
232: /*
233: * (non-Javadoc)
234: *
235: * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart)
236: */
237: public void init(IIntroPart introPart) {
238: }
239:
240: /*
241: * (non-Javadoc)
242: *
243: * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#setInput(java.lang.Object)
244: */
245: public void setInput(Object input) {
246: // if the new input is null, use cached input from momento.
247: if (input != null)
248: this .input = (String) input;
249: String sampleId = this .input.toString();
250: IConfigurationElement[] samples = Platform
251: .getExtensionRegistry().getConfigurationElementsFor(
252: "org.eclipse.pde.ui.samples"); //$NON-NLS-1$
253: for (int i = 0; i < samples.length; i++) {
254: IConfigurationElement sample = samples[i];
255: String id = sample.getAttribute("id"); //$NON-NLS-1$
256: if (id != null && id.equals(sampleId)) {
257: update(sample);
258: return;
259: }
260: }
261: update(null);
262: }
263:
264: private void update(IConfigurationElement sample) {
265: this .sample = sample;
266: if (form == null)
267: return;
268: String title = sample != null ? sample.getAttribute("name") : ""; //$NON-NLS-1$ //$NON-NLS-2$
269: form.setText(title);
270: if (sample != null) {
271: launcher = sample.getAttribute("launcher"); //$NON-NLS-1$
272: launchTarget = sample.getAttribute("launchTarget"); //$NON-NLS-1$
273: } else {
274: launcher = null;
275: launchTarget = null;
276: }
277: IConfigurationElement[] descConfig = sample != null ? sample
278: .getChildren("description") : null; //$NON-NLS-1$
279: if (descConfig != null && descConfig.length == 1) {
280: String desc = descConfig[0].getValue();
281: String content = NLS.bind(
282: PDEUIMessages.SampleStandbyContent_desc,
283: (desc != null ? desc : "")); //$NON-NLS-1$
284: helpURL = descConfig[0].getAttribute("helpHref"); //$NON-NLS-1$
285: moreLink.setVisible(helpURL != null);
286: descText.setText(content, true, false);
287: } else {
288: moreLink.setVisible(false);
289: descText.setText("", false, false); //$NON-NLS-1$
290: }
291: form.reflow(true);
292: } /*
293: * (non-Javadoc)
294: *
295: * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#setFocus()
296: */
297:
298: public void setFocus() {
299: form.setFocus();
300: }
301:
302: /*
303: * (non-Javadoc)
304: *
305: * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#dispose()
306: */
307: public void dispose() {
308: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
309: }
310:
311: /* (non-Javadoc)
312: * @see org.eclipse.ui.intro.config.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart, org.eclipse.ui.IMemento)
313: */
314: public void init(IIntroPart introPart, IMemento memento) {
315: // try to restore last state.
316: input = getCachedInput(memento);
317:
318: }
319:
320: /**
321: * Tries to create the last content part viewed, based on sample id.
322: *
323: * @param memento
324: * @return
325: */
326: private String getCachedInput(IMemento memento) {
327: if (memento == null)
328: return null;
329: return memento.getString(MEMENTO_SAMPLE_ID_ATT);
330:
331: }
332:
333: /* (non-Javadoc)
334: * @see org.eclipse.ui.intro.config.IStandbyContentPart#saveState(org.eclipse.ui.IMemento)
335: */
336: public void saveState(IMemento memento) {
337: String currentSampleId = input;
338: if (input != null)
339: memento.putString(MEMENTO_SAMPLE_ID_ATT, currentSampleId);
340:
341: }
342: }
|