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.Properties;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IResourceChangeEvent;
019: import org.eclipse.core.resources.IResourceChangeListener;
020: import org.eclipse.core.resources.IResourceDelta;
021: import org.eclipse.core.resources.IResourceDeltaVisitor;
022: import org.eclipse.core.resources.IStorage;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.core.runtime.IProgressMonitor;
025: import org.eclipse.debug.core.ILaunchManager;
026: import org.eclipse.debug.ui.ILaunchShortcut;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.StructuredSelection;
029: import org.eclipse.osgi.util.NLS;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEPluginImages;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
034: import org.eclipse.swt.SWT;
035: import org.eclipse.swt.custom.BusyIndicator;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Display;
038: import org.eclipse.ui.IEditorInput;
039: import org.eclipse.ui.IEditorSite;
040: import org.eclipse.ui.IFileEditorInput;
041: import org.eclipse.ui.ISharedImages;
042: import org.eclipse.ui.IStorageEditorInput;
043: import org.eclipse.ui.PartInitException;
044: import org.eclipse.ui.PlatformUI;
045: import org.eclipse.ui.forms.events.HyperlinkAdapter;
046: import org.eclipse.ui.forms.events.HyperlinkEvent;
047: import org.eclipse.ui.forms.widgets.FormText;
048: import org.eclipse.ui.forms.widgets.FormToolkit;
049: import org.eclipse.ui.forms.widgets.Hyperlink;
050: import org.eclipse.ui.forms.widgets.ScrolledForm;
051: import org.eclipse.ui.forms.widgets.TableWrapData;
052: import org.eclipse.ui.forms.widgets.TableWrapLayout;
053: import org.eclipse.ui.part.EditorPart;
054:
055: /**
056: * @see EditorPart
057: */
058: public class SampleEditor extends EditorPart {
059: private FormToolkit toolkit;
060: private ScrolledForm form;
061: private FormText descText;
062: private FormText instText;
063: private ILaunchShortcut defaultShortcut;
064: private InputFileListener inputFileListener;
065:
066: class InputFileListener implements IResourceChangeListener,
067: IResourceDeltaVisitor {
068: public void resourceChanged(IResourceChangeEvent event) {
069: if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
070: IResourceDelta delta = event.getDelta();
071: try {
072: delta.accept(this );
073: } catch (CoreException e) {
074: PDEPlugin.logException(e);
075: }
076: }
077: }
078:
079: public boolean visit(IResourceDelta delta) throws CoreException {
080: IResource resource = delta.getResource();
081: if (resource instanceof IFile) {
082: IFile file = (IFile) resource;
083: if (file.equals(((IFileEditorInput) getEditorInput())
084: .getFile())) {
085: if (delta.getKind() == IResourceDelta.REMOVED
086: || delta.getKind() == IResourceDelta.REPLACED)
087: close();
088: return false;
089: }
090: }
091: return true;
092: }
093: }
094:
095: /**
096: *
097: */
098: public SampleEditor() {
099: defaultShortcut = new EclipseLaunchShortcut();
100: PDEPlugin.getDefault().getLabelProvider().connect(this );
101: }
102:
103: /**
104: * @see EditorPart#createPartControl
105: */
106: public void createPartControl(Composite parent) {
107: toolkit = new FormToolkit(parent.getDisplay());
108: form = toolkit.createScrolledForm(parent);
109: Properties properties = loadContent();
110: form.setText(properties.getProperty("name")); //$NON-NLS-1$
111: TableWrapLayout layout = new TableWrapLayout();
112: layout.verticalSpacing = 10;
113: layout.topMargin = 10;
114: layout.bottomMargin = 10;
115: layout.leftMargin = 10;
116: layout.rightMargin = 10;
117: form.getBody().setLayout(layout);
118:
119: final String launcher = properties.getProperty("launcher"); //$NON-NLS-1$
120: final String launchTarget = properties
121: .getProperty("launchTarget"); //$NON-NLS-1$
122:
123: descText = toolkit.createFormText(form.getBody(), true);
124: descText.setLayoutData(new TableWrapData(
125: TableWrapData.FILL_GRAB));
126: String desc = properties.getProperty("description"); //$NON-NLS-1$
127: String content = NLS.bind(PDEUIMessages.SampleEditor_desc,
128: (desc != null ? desc : "")); //$NON-NLS-1$
129: descText.setText(content, true, false);
130: final String helpURL = properties.getProperty("helpHref"); //$NON-NLS-1$
131: if (helpURL != null) {
132: Hyperlink moreLink = toolkit.createHyperlink(
133: form.getBody(), "Read More", SWT.NULL); //$NON-NLS-1$
134: moreLink.addHyperlinkListener(new HyperlinkAdapter() {
135: public void linkActivated(HyperlinkEvent e) {
136: PlatformUI.getWorkbench().getHelpSystem()
137: .displayHelpResource(helpURL);
138: }
139: });
140: }
141: instText = toolkit.createFormText(form.getBody(), true);
142: instText.setLayoutData(new TableWrapData(
143: TableWrapData.FILL_GRAB));
144: StringBuffer buf = new StringBuffer();
145: buf.append(PDEUIMessages.SampleEditor_content);
146: instText.setText(buf.toString(), true, false);
147: instText.addHyperlinkListener(new HyperlinkAdapter() {
148: public void linkActivated(HyperlinkEvent e) {
149: Object href = e.getHref();
150: if (href.equals("help")) { //$NON-NLS-1$
151: PlatformUI.getWorkbench().getHelpSystem()
152: .displayHelpResource(helpURL);
153: } else if (href.equals("run")) { //$NON-NLS-1$
154: doRun(launcher, launchTarget, false);
155: } else if (href.equals("debug")) { //$NON-NLS-1$
156: doRun(launcher, launchTarget, true);
157: }
158: }
159: });
160: instText
161: .setImage(
162: "run", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_RUN_EXC)); //$NON-NLS-1$
163: instText
164: .setImage(
165: "debug", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_DEBUG_EXC)); //$NON-NLS-1$
166: instText
167: .setImage(
168: "help", PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_INFO_TSK)); //$NON-NLS-1$
169: }
170:
171: private void doRun(String launcher, String target,
172: final boolean debug) {
173: ILaunchShortcut shortcut = defaultShortcut;
174: final ISelection selection;
175: if (target != null) {
176: selection = new StructuredSelection();
177: } else
178: selection = new StructuredSelection();
179: final ILaunchShortcut fshortcut = shortcut;
180: BusyIndicator.showWhile(form.getDisplay(), new Runnable() {
181: public void run() {
182: fshortcut.launch(selection,
183: debug ? ILaunchManager.DEBUG_MODE
184: : ILaunchManager.RUN_MODE);
185: }
186: });
187: }
188:
189: private Properties loadContent() {
190: IStorageEditorInput input = (IStorageEditorInput) getEditorInput();
191: Properties properties = new Properties();
192: try {
193: IStorage storage = input.getStorage();
194: InputStream is = storage.getContents();
195: properties.load(is);
196: is.close();
197: } catch (IOException e) {
198: PDEPlugin.logException(e);
199: } catch (CoreException e) {
200: PDEPlugin.logException(e);
201: }
202: return properties;
203: }
204:
205: public void dispose() {
206: if (inputFileListener != null) {
207: PDEPlugin.getWorkspace().removeResourceChangeListener(
208: inputFileListener);
209: inputFileListener = null;
210: }
211: toolkit.dispose();
212: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
213: super .dispose();
214: }
215:
216: /**
217: * @see EditorPart#setFocus
218: */
219: public void setFocus() {
220: form.setFocus();
221: }
222:
223: /**
224: * @see EditorPart#doSave
225: */
226: public void doSave(IProgressMonitor monitor) {
227: }
228:
229: /**
230: * @see EditorPart#doSaveAs
231: */
232: public void doSaveAs() {
233: }
234:
235: /**
236: * @see EditorPart#isDirty
237: */
238: public boolean isDirty() {
239: return false;
240: }
241:
242: /**
243: * @see EditorPart#isSaveAsAllowed
244: */
245: public boolean isSaveAsAllowed() {
246: return false;
247: }
248:
249: /**
250: * @see EditorPart#init
251: */
252: public void init(IEditorSite site, IEditorInput input)
253: throws PartInitException {
254: setSite(site);
255: setInput(input);
256: inputFileListener = new InputFileListener();
257: PDEPlugin.getWorkspace().addResourceChangeListener(
258: inputFileListener);
259: }
260:
261: public void close() {
262: Display display = getSite().getShell().getDisplay();
263: display.asyncExec(new Runnable() {
264: public void run() {
265: if (toolkit != null) {
266: getSite().getPage().closeEditor(SampleEditor.this ,
267: false);
268: }
269: }
270: });
271: }
272: }
|