KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > internal > samples > SampleStandbyContent


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.ui.internal.samples;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Properties JavaDoc;
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 JavaDoc helpURL;
57     private String JavaDoc launcher;
58     private String JavaDoc launchTarget;
59     private FormText descText;
60     private FormText instText;
61     private ILaunchShortcut defaultShortcut;
62     private IConfigurationElement sample;
63     // cached input.
64
private String JavaDoc input;
65     
66     private static String JavaDoc MEMENTO_SAMPLE_ID_ATT = "sampleId"; //$NON-NLS-1$
67

68     /**
69      *
70      */

71     public SampleStandbyContent() {
72         defaultShortcut = new RuntimeWorkbenchShortcut();
73         PDEPlugin.getDefault().getLabelProvider().connect(this);
74     }
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#createPartControl(org.eclipse.swt.widgets.Composite,
79      * org.eclipse.ui.forms.widgets.FormToolkit)
80      */

81     public void createPartControl(Composite parent, FormToolkit toolkit) {
82         form = toolkit.createScrolledForm(parent);
83         //form.setBackgroundImage(PDEPlugin.getDefault().getLabelProvider().get(
84
// PDEPluginImages.DESC_FORM_BANNER));
85
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); //$NON-NLS-1$
95
moreLink = toolkit.createHyperlink(form.getBody(), "Read More", //$NON-NLS-1$
96
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 JavaDoc buf = new StringBuffer JavaDoc();
106         buf.append(PDEUIMessages.SampleStandbyContent_content); //$NON-NLS-1$
107
instText.setText(buf.toString(), true, false);
108         instText.addHyperlinkListener(new HyperlinkAdapter() {
109             public void linkActivated(HyperlinkEvent e) {
110                 Object JavaDoc href = e.getHref();
111                 if (href.equals("help")) { //$NON-NLS-1$
112
PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL);
113                 } else if (href.equals("browse")) { //$NON-NLS-1$
114
doBrowse();
115                 } else if (href.equals("run")) { //$NON-NLS-1$
116
doRun(launcher, launchTarget, false);
117                 } else if (href.equals("debug")) { //$NON-NLS-1$
118
doRun(launcher, launchTarget, true);
119                 }
120             }
121         });
122         instText.setImage("run", PDEPlugin.getDefault().getLabelProvider().get( //$NON-NLS-1$
123
PDEPluginImages.DESC_RUN_EXC));
124         instText.setImage("debug", PDEPlugin.getDefault().getLabelProvider() //$NON-NLS-1$
125
.get(PDEPluginImages.DESC_DEBUG_EXC));
126         instText.setImage("help", PlatformUI.getWorkbench().getSharedImages() //$NON-NLS-1$
127
.getImage(ISharedImages.IMG_OBJS_INFO_TSK));
128     }
129     private void doRun(String JavaDoc launcher, String JavaDoc 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 JavaDoc() {
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 JavaDoc sid = sample.getAttribute("id"); //$NON-NLS-1$
152
if (sid == null)
153             return;
154         ArrayList JavaDoc items = new ArrayList JavaDoc();
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"); //$NON-NLS-1$
160
if (pfile.exists()) {
161                 try {
162                     InputStream JavaDoc is = pfile.getContents();
163                     Properties JavaDoc prop = new Properties JavaDoc();
164                     prop.load(is);
165                     is.close();
166                     String JavaDoc id = prop.getProperty("id"); //$NON-NLS-1$
167
if (id != null && id.equals(sid)) {
168                         //match
169
IResource res = findSelectReveal(project, prop
170                                 .getProperty("projectName")); //$NON-NLS-1$
171
if (res != null)
172                             items.add(res);
173                     }
174                 } catch (IOException JavaDoc 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 JavaDoc id = sample.getAttribute("targetViewId"); //$NON-NLS-1$
186
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 JavaDoc originalName) {
194         IConfigurationElement[] projects = sample.getChildren("project"); //$NON-NLS-1$
195
for (int i = 0; i < projects.length; i++) {
196             if (originalName.equals(projects[i].getAttribute("name"))) { //$NON-NLS-1$
197
String JavaDoc path = projects[i].getAttribute("selectReveal"); //$NON-NLS-1$
198
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     /*
208      * (non-Javadoc)
209      *
210      * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#getControl()
211      */

212     public Control getControl() {
213         return form;
214     }
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart)
219      */

220     public void init(IIntroPart introPart) {
221     }
222     /*
223      * (non-Javadoc)
224      *
225      * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#setInput(java.lang.Object)
226      */

227     public void setInput(Object JavaDoc input) {
228         // if the new input is null, use cached input from momento.
229
if (input != null)
230             this.input = (String JavaDoc) input;
231         String JavaDoc sampleId = this.input.toString();
232         IConfigurationElement[] samples = Platform.getExtensionRegistry()
233                 .getConfigurationElementsFor("org.eclipse.pde.ui.samples"); //$NON-NLS-1$
234
for (int i = 0; i < samples.length; i++) {
235             IConfigurationElement sample = samples[i];
236             String JavaDoc id = sample.getAttribute("id"); //$NON-NLS-1$
237
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 JavaDoc title = sample != null ? sample.getAttribute("name") : ""; //$NON-NLS-1$ //$NON-NLS-2$
249
form.setText(title);
250         if (sample != null) {
251             launcher = sample.getAttribute("launcher"); //$NON-NLS-1$
252
launchTarget = sample.getAttribute("launchTarget"); //$NON-NLS-1$
253
} else {
254             launcher = null;
255             launchTarget = null;
256         }
257         IConfigurationElement[] descConfig = sample != null ? sample
258                 .getChildren("description") : null; //$NON-NLS-1$
259
if (descConfig != null && descConfig.length == 1) {
260             String JavaDoc desc = descConfig[0].getValue();
261             String JavaDoc content = NLS.bind(PDEUIMessages.SampleStandbyContent_desc, (desc != null ? desc : "")); //$NON-NLS-1$ //$NON-NLS-2$
262
helpURL = descConfig[0].getAttribute("helpHref"); //$NON-NLS-1$
263
moreLink.setVisible(helpURL != null);
264             descText.setText(content, true, false);
265         } else {
266             moreLink.setVisible(false);
267             descText.setText("", false, false); //$NON-NLS-1$
268
}
269         form.reflow(true);
270     } /*
271        * (non-Javadoc)
272        *
273        * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#setFocus()
274        */

275     public void setFocus() {
276         form.setFocus();
277     }
278     /*
279      * (non-Javadoc)
280      *
281      * @see org.eclipse.ui.intro.internal.parts.IStandbyContentPart#dispose()
282      */

283     public void dispose() {
284         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
285     }
286     /* (non-Javadoc)
287      * @see org.eclipse.ui.intro.config.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart, org.eclipse.ui.IMemento)
288      */

289     public void init(IIntroPart introPart, IMemento memento) {
290         // try to restore last state.
291
input = getCachedInput(memento);
292
293     }
294     
295      /**
296      * Tries to create the last content part viewed, based on sample id.
297      *
298      * @param memento
299      * @return
300      */

301     private String JavaDoc getCachedInput(IMemento memento) {
302         if (memento == null)
303             return null;
304         return memento.getString(MEMENTO_SAMPLE_ID_ATT);
305
306     }
307     
308     /* (non-Javadoc)
309      * @see org.eclipse.ui.intro.config.IStandbyContentPart#saveState(org.eclipse.ui.IMemento)
310      */

311     public void saveState(IMemento memento) {
312         String JavaDoc currentSampleId = input;
313         if (input != null)
314             memento.putString(MEMENTO_SAMPLE_ID_ATT, currentSampleId);
315
316     }
317 }
318
Popular Tags