KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.ui.samples;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceChangeEvent;
19 import org.eclipse.core.resources.IResourceChangeListener;
20 import org.eclipse.core.resources.IResourceDelta;
21 import org.eclipse.core.resources.IResourceDeltaVisitor;
22 import org.eclipse.core.resources.IStorage;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.debug.core.ILaunchManager;
26 import org.eclipse.debug.ui.ILaunchShortcut;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.internal.ui.PDEPluginImages;
32 import org.eclipse.pde.internal.ui.PDEUIMessages;
33 import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.BusyIndicator;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.ui.IEditorInput;
39 import org.eclipse.ui.IEditorSite;
40 import org.eclipse.ui.IFileEditorInput;
41 import org.eclipse.ui.ISharedImages;
42 import org.eclipse.ui.IStorageEditorInput;
43 import org.eclipse.ui.PartInitException;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.forms.events.HyperlinkAdapter;
46 import org.eclipse.ui.forms.events.HyperlinkEvent;
47 import org.eclipse.ui.forms.widgets.FormText;
48 import org.eclipse.ui.forms.widgets.FormToolkit;
49 import org.eclipse.ui.forms.widgets.Hyperlink;
50 import org.eclipse.ui.forms.widgets.ScrolledForm;
51 import org.eclipse.ui.forms.widgets.TableWrapData;
52 import org.eclipse.ui.forms.widgets.TableWrapLayout;
53 import org.eclipse.ui.part.EditorPart;
54 /**
55  * @see EditorPart
56  */

57 public class SampleEditor extends EditorPart {
58     private FormToolkit toolkit;
59     private ScrolledForm form;
60     private FormText descText;
61     private FormText instText;
62     private ILaunchShortcut defaultShortcut;
63     private InputFileListener inputFileListener;
64     
65     class InputFileListener implements IResourceChangeListener, IResourceDeltaVisitor {
66         public void resourceChanged(IResourceChangeEvent event) {
67             if (event.getType()==IResourceChangeEvent.POST_CHANGE) {
68                 IResourceDelta delta = event.getDelta();
69                 try {
70                     delta.accept(this);
71                 }
72                 catch (CoreException e) {
73                     PDEPlugin.logException(e);
74                 }
75             }
76         }
77         public boolean visit(IResourceDelta delta) throws CoreException {
78             IResource resource = delta.getResource();
79             if (resource instanceof IFile) {
80                 IFile file = (IFile)resource;
81                 if (file.equals(((IFileEditorInput)getEditorInput()).getFile())) {
82                     if (delta.getKind()==IResourceDelta.REMOVED ||
83                             delta.getKind()==IResourceDelta.REPLACED)
84                         close();
85                     return false;
86                 }
87             }
88             return true;
89         }
90     }
91     /**
92      *
93      */

94     public SampleEditor() {
95         defaultShortcut = new EclipseLaunchShortcut();
96         PDEPlugin.getDefault().getLabelProvider().connect(this);
97     }
98     /**
99      * @see EditorPart#createPartControl
100      */

101     public void createPartControl(Composite parent) {
102         toolkit = new FormToolkit(parent.getDisplay());
103         form = toolkit.createScrolledForm(parent);
104         Properties JavaDoc properties = loadContent();
105         form.setText(properties.getProperty("name")); //$NON-NLS-1$
106
TableWrapLayout layout = new TableWrapLayout();
107         layout.verticalSpacing = 10;
108         layout.topMargin = 10;
109         layout.bottomMargin = 10;
110         layout.leftMargin = 10;
111         layout.rightMargin = 10;
112         form.getBody().setLayout(layout);
113         
114         final String JavaDoc launcher = properties.getProperty("launcher"); //$NON-NLS-1$
115
final String JavaDoc launchTarget = properties.getProperty("launchTarget"); //$NON-NLS-1$
116

117         descText = toolkit.createFormText(form.getBody(), true);
118         descText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
119         String JavaDoc desc = properties.getProperty("description"); //$NON-NLS-1$
120
String JavaDoc content = NLS.bind(PDEUIMessages.SampleEditor_desc, (desc!=null?desc:"")); //$NON-NLS-1$
121
descText.setText(content, true, false);
122         final String JavaDoc helpURL = properties.getProperty("helpHref"); //$NON-NLS-1$
123
if (helpURL!=null) {
124             Hyperlink moreLink = toolkit.createHyperlink(form.getBody(), "Read More", SWT.NULL); //$NON-NLS-1$
125
moreLink.addHyperlinkListener(new HyperlinkAdapter() {
126                 public void linkActivated(HyperlinkEvent e) {
127                     PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL);
128                 }
129             });
130         }
131         instText = toolkit.createFormText(form.getBody(), true);
132         instText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
133         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134         buf.append(PDEUIMessages.SampleEditor_content);
135         instText.setText(buf.toString(), true, false);
136         instText.addHyperlinkListener(new HyperlinkAdapter() {
137             public void linkActivated(HyperlinkEvent e) {
138                 Object JavaDoc href = e.getHref();
139                 if (href.equals("help")) { //$NON-NLS-1$
140
PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(helpURL);
141                 }
142                 else if (href.equals("run")) { //$NON-NLS-1$
143
doRun(launcher, launchTarget, false);
144                 }
145                 else if (href.equals("debug")) { //$NON-NLS-1$
146
doRun(launcher, launchTarget, true);
147                 }
148             }
149         });
150         instText.setImage("run", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_RUN_EXC)); //$NON-NLS-1$
151
instText.setImage("debug", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_DEBUG_EXC)); //$NON-NLS-1$
152
instText.setImage("help", PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_INFO_TSK)); //$NON-NLS-1$
153
}
154     
155     private void doRun(String JavaDoc launcher, String JavaDoc target, final boolean debug) {
156         ILaunchShortcut shortcut = defaultShortcut;
157         final ISelection selection;
158         if (target!=null) {
159             selection = new StructuredSelection();
160         }
161         else
162             selection = new StructuredSelection();
163         final ILaunchShortcut fshortcut = shortcut;
164         BusyIndicator.showWhile(form.getDisplay(), new Runnable JavaDoc() {
165             public void run() {
166                 fshortcut.launch(selection, debug?ILaunchManager.DEBUG_MODE:ILaunchManager.RUN_MODE);
167             }
168         });
169     }
170     
171     private Properties JavaDoc loadContent() {
172         IStorageEditorInput input = (IStorageEditorInput)getEditorInput();
173         Properties JavaDoc properties = new Properties JavaDoc();
174         try {
175             IStorage storage = input.getStorage();
176             InputStream JavaDoc is = storage.getContents();
177             properties.load(is);
178             is.close();
179         }
180         catch (IOException JavaDoc e) {
181             PDEPlugin.logException(e);
182         }
183         catch (CoreException e) {
184             PDEPlugin.logException(e);
185         }
186         return properties;
187     }
188     
189     public void dispose() {
190         if (inputFileListener!=null) {
191             PDEPlugin.getWorkspace().removeResourceChangeListener(inputFileListener);
192             inputFileListener = null;
193         }
194         toolkit.dispose();
195         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
196         super.dispose();
197     }
198     /**
199      * @see EditorPart#setFocus
200      */

201     public void setFocus() {
202         form.setFocus();
203     }
204     /**
205      * @see EditorPart#doSave
206      */

207     public void doSave(IProgressMonitor monitor) {
208     }
209     /**
210      * @see EditorPart#doSaveAs
211      */

212     public void doSaveAs() {
213     }
214     /**
215      * @see EditorPart#isDirty
216      */

217     public boolean isDirty() {
218         return false;
219     }
220     /**
221      * @see EditorPart#isSaveAsAllowed
222      */

223     public boolean isSaveAsAllowed() {
224         return false;
225     }
226     /**
227      * @see EditorPart#init
228      */

229     public void init(IEditorSite site, IEditorInput input)
230             throws PartInitException {
231         setSite(site);
232         setInput(input);
233         inputFileListener = new InputFileListener();
234         PDEPlugin.getWorkspace().addResourceChangeListener(inputFileListener);
235     }
236     public void close() {
237         Display display = getSite().getShell().getDisplay();
238         display.asyncExec(new Runnable JavaDoc() {
239             public void run() {
240                 if (toolkit != null) {
241                     getSite().getPage().closeEditor(SampleEditor.this, false);
242                 }
243             }
244         });
245     }
246 }
247
Popular Tags