KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.*;
13 import java.util.Properties JavaDoc;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.debug.core.ILaunchManager;
18 import org.eclipse.debug.ui.ILaunchShortcut;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.pde.internal.ui.*;
22 import org.eclipse.pde.internal.ui.launcher.RuntimeWorkbenchShortcut;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.BusyIndicator;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.ui.*;
27 import org.eclipse.ui.forms.events.*;
28 import org.eclipse.ui.forms.widgets.*;
29 import org.eclipse.ui.part.EditorPart;
30 /**
31  * @see EditorPart
32  */

33 public class SampleEditor extends EditorPart {
34     private FormToolkit toolkit;
35     private ScrolledForm form;
36     private FormText descText;
37     private FormText instText;
38     private ILaunchShortcut defaultShortcut;
39     private InputFileListener inputFileListener;
40     
41     class InputFileListener implements IResourceChangeListener, IResourceDeltaVisitor {
42         public void resourceChanged(IResourceChangeEvent event) {
43             if (event.getType()==IResourceChangeEvent.POST_CHANGE) {
44                 IResourceDelta delta = event.getDelta();
45                 try {
46                     delta.accept(this);
47                 }
48                 catch (CoreException e) {
49                     PDEPlugin.logException(e);
50                 }
51             }
52         }
53         public boolean visit(IResourceDelta delta) throws CoreException {
54             IResource resource = delta.getResource();
55             if (resource instanceof IFile) {
56                 IFile file = (IFile)resource;
57                 if (file.equals(((IFileEditorInput)getEditorInput()).getFile())) {
58                     if (delta.getKind()==IResourceDelta.REMOVED ||
59                             delta.getKind()==IResourceDelta.REPLACED)
60                         close();
61                     return false;
62                 }
63             }
64             return true;
65         }
66     }
67     /**
68      *
69      */

70     public SampleEditor() {
71         defaultShortcut = new RuntimeWorkbenchShortcut();
72         PDEPlugin.getDefault().getLabelProvider().connect(this);
73     }
74     /**
75      * @see EditorPart#createPartControl
76      */

77     public void createPartControl(Composite parent) {
78         toolkit = new FormToolkit(parent.getDisplay());
79         form = toolkit.createScrolledForm(parent);
80         Properties JavaDoc properties = loadContent();
81         form.setText(properties.getProperty("name")); //$NON-NLS-1$
82
TableWrapLayout layout = new TableWrapLayout();
83         layout.verticalSpacing = 10;
84         layout.topMargin = 10;
85         layout.bottomMargin = 10;
86         layout.leftMargin = 10;
87         layout.rightMargin = 10;
88         form.getBody().setLayout(layout);
89         
90         final String JavaDoc launcher = properties.getProperty("launcher"); //$NON-NLS-1$
91
final String JavaDoc launchTarget = properties.getProperty("launchTarget"); //$NON-NLS-1$
92

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

177     public void setFocus() {
178         form.setFocus();
179     }
180     /**
181      * @see EditorPart#doSave
182      */

183     public void doSave(IProgressMonitor monitor) {
184     }
185     /**
186      * @see EditorPart#doSaveAs
187      */

188     public void doSaveAs() {
189     }
190     /**
191      * @see EditorPart#isDirty
192      */

193     public boolean isDirty() {
194         return false;
195     }
196     /**
197      * @see EditorPart#isSaveAsAllowed
198      */

199     public boolean isSaveAsAllowed() {
200         return false;
201     }
202     /**
203      * @see EditorPart#init
204      */

205     public void init(IEditorSite site, IEditorInput input)
206             throws PartInitException {
207         setSite(site);
208         setInput(input);
209         inputFileListener = new InputFileListener();
210         PDEPlugin.getWorkspace().addResourceChangeListener(inputFileListener);
211     }
212     public void close() {
213         Display display = getSite().getShell().getDisplay();
214         display.asyncExec(new Runnable JavaDoc() {
215             public void run() {
216                 if (toolkit != null) {
217                     getSite().getPage().closeEditor(SampleEditor.this, false);
218                 }
219             }
220         });
221     }
222 }
223
Popular Tags